2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of pwmd.
7 Pwmd is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 Pwmd is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/types.h>
41 #include "pwmd-error.h"
49 #include "util-misc.h"
53 /* Flags needed to be retained for a client across commands. */
54 #define FLAG_NEW 0x0001
55 #define FLAG_HAS_LOCK 0x0002
56 #define FLAG_LOCK_CMD 0x0004
57 #define FLAG_NO_PINENTRY 0x0008
58 #define FLAG_OPEN 0x0010
59 #define FLAG_KEEP_LOCK 0x0020
61 /* These are command option flags. */
62 #define OPT_INQUIRE 0x0001
63 #define OPT_NO_PASSPHRASE 0x0002
64 #define OPT_RESET 0x0004
65 #define OPT_LIST_RECURSE 0x0008
66 #define OPT_LIST_VERBOSE 0x0010
67 #define OPT_LOCK 0x0020
68 #define OPT_LOCK_ON_OPEN 0x0040
69 #define OPT_SIGN 0x0080
70 #define OPT_LIST_ALL 0x0100
71 #define OPT_LIST_WITH_TARGET 0x0200
72 #define OPT_DATA 0x0400
74 struct command_table_s
77 gpg_error_t (*handler
) (assuan_context_t
, char *line
);
80 int unlock
; // unlock the file mutex after validating the checksum
83 static struct command_table_s
**command_table
;
85 static gpg_error_t
do_lock (struct client_s
*client
, int add
);
86 static gpg_error_t
validate_checksum (struct client_s
*,
87 struct cache_data_s
*);
88 static gpg_error_t
update_checksum (struct client_s
*client
);
91 unlock_file_mutex (struct client_s
*client
, int remove
)
95 // OPEN: keep the lock for the same file being reopened.
96 if (client
->flags
& FLAG_KEEP_LOCK
&& client
->flags
& FLAG_HAS_LOCK
)
99 if (!(client
->flags
& FLAG_HAS_LOCK
))
100 return GPG_ERR_NOT_LOCKED
;
102 rc
= cache_unlock_mutex (client
->md5file
, remove
);
104 rc
= GPG_ERR_INV_STATE
;
106 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_LOCK_CMD
);
112 lock_file_mutex (struct client_s
*client
, int add
)
115 int timeout
= config_get_integer (client
->filename
, "cache_timeout");
117 if (client
->flags
& FLAG_HAS_LOCK
)
120 rc
= cache_lock_mutex (client
->ctx
, client
->md5file
,
121 client
->lock_timeout
, add
, timeout
);
123 client
->flags
|= FLAG_HAS_LOCK
;
129 file_modified (struct client_s
*client
, struct command_table_s
*cmd
)
133 if (!(client
->flags
& FLAG_OPEN
))
134 return GPG_ERR_INV_STATE
;
136 rc
= lock_file_mutex (client
, 0);
137 if (!rc
|| rc
== GPG_ERR_NO_DATA
)
139 rc
= validate_checksum (client
, NULL
);
140 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
)
144 if ((cmd
->unlock
&& !(client
->flags
& FLAG_HAS_LOCK
)) || rc
)
145 unlock_file_mutex (client
, 0);
151 parse_xml (assuan_context_t ctx
, int new)
153 struct client_s
*client
= assuan_get_pointer (ctx
);
154 int cached
= client
->doc
!= NULL
;
158 client
->doc
= new_document ();
161 xmlDocDumpFormatMemory (client
->doc
,
162 (xmlChar
**) & client
->crypto
->plaintext
,
163 (int *) &client
->crypto
->plaintext_len
, 0);
164 if (!client
->crypto
->plaintext
)
166 xmlFreeDoc (client
->doc
);
172 client
->doc
= parse_doc ((char *) client
->crypto
->plaintext
,
173 client
->crypto
->plaintext_len
);
175 return !client
->doc
? GPG_ERR_ENOMEM
: 0;
179 free_client (struct client_s
*client
)
182 xmlFreeDoc (client
->doc
);
185 xfree (client
->filename
);
186 xfree (client
->last_error
);
190 cleanup_crypto_stage2 (client
->crypto
);
191 if (client
->crypto
->pkey_sexp
)
192 gcry_sexp_release (client
->crypto
->pkey_sexp
);
194 client
->crypto
->pkey_sexp
= NULL
;
195 memset (client
->crypto
->sign_grip
, 0,
196 sizeof (client
->crypto
->sign_grip
));
199 if (client
->xml_error
)
200 xmlResetError (client
->xml_error
);
204 cleanup_client (struct client_s
*client
)
206 assuan_context_t ctx
= client
->ctx
;
207 struct client_thread_s
*thd
= client
->thd
;
208 struct crypto_s
*crypto
= client
->crypto
;
209 long lock_timeout
= client
->lock_timeout
;
211 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
212 free_client (client
);
213 memset (client
, 0, sizeof (struct client_s
));
214 client
->crypto
= crypto
;
217 client
->lock_timeout
= lock_timeout
;
221 open_finalize (assuan_context_t ctx
)
223 struct client_s
*client
= assuan_get_pointer (ctx
);
225 struct cache_data_s
*cdata
= cache_get_data (client
->md5file
);
228 client
->crypto
->filename
= str_dup (client
->filename
);
229 if (cdata
|| client
->flags
& FLAG_NEW
)
231 if (!(client
->flags
& FLAG_NEW
))
233 rc
= decrypt_xml (client
->crypto
, cdata
->doc
, cdata
->doclen
);
238 cached
= cdata
!= NULL
;
242 rc
= decrypt_data (client
->ctx
, client
->crypto
);
247 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
250 int timeout
= config_get_integer (client
->filename
,
255 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
256 rc
= encrypt_xml (NULL
, cache_key
, cache_keysize
,
257 GCRY_CIPHER_AES
, client
->crypto
->plaintext
,
258 client
->crypto
->plaintext_len
, &cdata
->doc
,
259 &cdata
->doclen
, &cache_iv
, &cache_blocksize
,
261 if (!rc
&& !(client
->flags
& FLAG_NEW
))
263 rc
= gcry_sexp_build ((gcry_sexp_t
*) & cdata
->pubkey
, NULL
,
264 "%S", client
->crypto
->pkey_sexp
);
269 gcry_sexp_release (cdata
->sigkey
);
272 rc
= gcry_sexp_build ((gcry_sexp_t
*) & cdata
->sigkey
, NULL
,
273 "%S", client
->crypto
->sigpkey_sexp
);
276 && !cache_add_file (client
->md5file
,
277 (client
->flags
& FLAG_NEW
) ? NULL
: client
->
278 crypto
->grip
, cdata
, timeout
))
282 free_cache_data_once (cdata
);
283 xmlFreeDoc (client
->doc
);
290 client
->flags
|= FLAG_OPEN
;
295 update_checksum (client
);
297 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
298 rc
= do_lock (client
, 0);
304 req_cleanup (void *arg
)
309 strv_free ((char **) arg
);
313 parse_open_opt_lock (void *data
, void *value
)
315 struct client_s
*client
= data
;
317 client
->opts
|= OPT_LOCK_ON_OPEN
;
322 parse_opt_pinentry (void *data
, void *value
)
324 struct client_s
*client
= data
;
326 client
->flags
|= FLAG_NO_PINENTRY
;
331 parse_opt_inquire (void *data
, void *value
)
333 struct client_s
*client
= data
;
336 client
->opts
|= OPT_INQUIRE
;
341 update_checksum (struct client_s
*client
)
345 struct cache_data_s
*cdata
;
346 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
353 cdata
= cache_get_data (client
->md5file
);
357 cdata
->crc
= xmalloc (len
);
358 memcpy (cdata
->crc
, crc
, len
);
365 validate_checksum (struct client_s
*client
, struct cache_data_s
*cdata
)
372 if (cdata
&& !cdata
->crc
)
373 return GPG_ERR_CHECKSUM
;
375 rc
= get_checksum (client
->filename
, &crc
, &len
);
380 n
= memcmp (cdata
->crc
, crc
, len
);
381 else if (client
->crc
)
382 n
= memcmp (client
->crc
, crc
, len
);
385 return n
? GPG_ERR_CHECKSUM
: 0;
389 do_open (assuan_context_t ctx
, const char *filename
, const char *password
)
391 struct client_s
*client
= assuan_get_pointer (ctx
);
392 struct cache_data_s
*cdata
;
396 if (!valid_filename (filename
))
397 return GPG_ERR_INV_VALUE
;
399 gcry_md_hash_buffer (GCRY_MD_MD5
, client
->md5file
, filename
,
401 client
->filename
= str_dup (filename
);
402 if (!client
->filename
)
403 return GPG_ERR_ENOMEM
;
406 cdata
= cache_get_data (client
->md5file
);
407 if (cdata
&& cdata
->doc
)
411 rc
= validate_checksum (client
, cdata
);
412 /* This will check that the key is cached in the agent which needs to
413 * be determined for files that share a keygrip. */
416 rc
= cache_iscached (client
->filename
, &defer
);
418 rc
= GPG_ERR_KEY_EXPIRED
;
422 if (!rc
&& client
->thd
->remote
&&
423 config_get_boolean (NULL
, "tcp_require_key"))
424 rc
= GPG_ERR_KEY_EXPIRED
;
427 if (!rc
&& !password
)
429 rc
= read_data_header (client
->filename
, &client
->crypto
->hdr
,
433 gcry_sexp_build (&client
->crypto
->pkey_sexp
, NULL
, "%S",
435 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
436 client
->crypto
->grip
);
437 gcry_sexp_build (&client
->crypto
->sigpkey_sexp
, NULL
, "%S",
439 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
440 client
->crypto
->sign_grip
);
441 rc
= open_finalize (ctx
);
446 /* There was an error accessing the file so clear the cache entry. The
447 * real error will be returned from read_data_file() since the file
448 * may have only disappeared. */
451 log_write ("%s: %s", filename
, pwmd_strerror (rc
));
452 rc
= cache_clear (client
->md5file
);
453 send_status_all (STATUS_CACHE
, NULL
);
460 rc
= read_data_file (client
->filename
, client
->crypto
);
463 if (gpg_err_source (rc
) != GPG_ERR_SOURCE_DEFAULT
||
464 gpg_err_code (rc
) != GPG_ERR_ENOENT
)
466 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
470 client
->flags
|= FLAG_NEW
;
471 memset (client
->crypto
->grip
, 0, sizeof (client
->crypto
->grip
));
472 memset (client
->crypto
->sign_grip
, 0,
473 sizeof (client
->crypto
->sign_grip
));
474 rc
= open_finalize (ctx
);
480 rc
= set_agent_passphrase (client
->crypto
, password
, strlen (password
));
485 rc
= open_finalize (ctx
);
490 open_command (assuan_context_t ctx
, char *line
)
493 struct client_s
*client
= assuan_get_pointer (ctx
);
494 char **req
, *password
= NULL
, *filename
;
495 unsigned char md5file
[16];
497 struct argv_s
*args
[] = {
498 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
499 &(struct argv_s
) {"no-pinentry", OPTION_TYPE_NOARG
,
504 client
->flags
&= ~(FLAG_NO_PINENTRY
);
505 rc
= parse_options (&line
, args
, client
);
507 return send_error (ctx
, rc
);
509 req
= str_split (line
, " ", 2);
511 return send_error (ctx
, GPG_ERR_SYNTAX
);
514 if (client
->thd
->remote
)
516 rc
= tls_validate_access (client
, req
[0]);
519 if (rc
== GPG_ERR_INV_USER_ID
)
520 log_write (_("client validation failed for file '%s'"), req
[0]);
522 return send_error (ctx
, rc
);
525 /* Remote pinentries are not supported. */
526 client
->flags
|= FLAG_NO_PINENTRY
;
530 pthread_cleanup_push (req_cleanup
, req
);
532 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
533 /* This client may have locked a different file with ISCACHED --lock than
534 * the current filename. This will remove that lock. */
535 same_file
= !memcmp (md5file
, client
->md5file
, 16) ? 1 : 0;
536 if (client
->flags
& FLAG_OPEN
||
537 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
539 typeof (client
->opts
) opts
= client
->opts
;
540 typeof (client
->flags
) flags
= client
->flags
;
543 client
->flags
|= FLAG_KEEP_LOCK
;
545 cleanup_client (client
);
547 client
->flags
|= flags
;
548 client
->flags
&= ~(FLAG_LOCK_CMD
| FLAG_NEW
);
550 client
->flags
&= ~(FLAG_HAS_LOCK
);
553 /* Need to lock the mutex here because file_modified() cannot without
554 * knowing the filename. */
555 memcpy (client
->md5file
, md5file
, 16);
556 rc
= lock_file_mutex (client
, 1);
559 password
= req
[1] && *req
[1] ? req
[1] : NULL
;
560 rc
= set_pinentry_mode (client
->crypto
->agent
,
561 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
565 rc
= do_open (ctx
, filename
, password
);
567 cleanup_client (client
);
569 cleanup_crypto_stage1 (client
->crypto
);
573 pthread_cleanup_pop (1);
575 if (!rc
&& client
->flags
& FLAG_NEW
)
576 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
578 (void) kill_scd (client
->crypto
->agent
);
579 return send_error (ctx
, rc
);
583 parse_save_opt_no_passphrase (void *data
, void *value
)
585 struct client_s
*client
= data
;
588 client
->opts
|= OPT_NO_PASSPHRASE
;
593 parse_save_opt_cipher (void *data
, void *value
)
595 struct client_s
*client
= data
;
596 int algo
= cipher_string_to_gcrypt ((char *) value
);
597 file_header_t
*hdr
= &client
->crypto
->save
.hdr
;
600 return GPG_ERR_INV_VALUE
;
602 hdr
->flags
= set_cipher_flag (hdr
->flags
, algo
);
607 parse_save_opt_keygrip (void *data
, void *value
)
609 struct client_s
*client
= data
;
611 if (client
->crypto
->save
.pkey
)
612 gcry_sexp_release (client
->crypto
->save
.pkey
);
614 client
->crypto
->save
.pkey
= NULL
;
615 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.pkey
);
619 parse_save_opt_sign_keygrip (void *data
, void *value
)
621 struct client_s
*client
= data
;
623 if (client
->crypto
->save
.sigpkey
)
624 gcry_sexp_release (client
->crypto
->save
.sigpkey
);
626 client
->crypto
->save
.sigpkey
= NULL
;
627 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.sigpkey
);
631 parse_opt_s2k_count (void *data
, void *value
)
633 struct client_s
*client
= data
;
637 return GPG_ERR_INV_VALUE
;
639 client
->crypto
->save
.s2k_count
= strtoul (v
, NULL
, 10);
644 parse_save_opt_iterations (void *data
, void *value
)
646 struct client_s
*client
= data
;
651 return GPG_ERR_INV_VALUE
;
654 n
= strtoull (v
, &p
, 10);
655 if (n
== UINT64_MAX
&& errno
)
656 return gpg_error_from_syserror ();
658 return GPG_ERR_INV_VALUE
;
660 client
->crypto
->save
.hdr
.iterations
= n
;
665 save_finalize (assuan_context_t ctx
)
667 struct client_s
*client
= assuan_get_pointer (ctx
);
669 xmlChar
*xmlbuf
= NULL
;
671 gcry_sexp_t pubkey
= client
->crypto
->save
.pkey
;
672 gcry_sexp_t sigkey
= client
->crypto
->save
.sigpkey
;
673 struct cache_data_s
*cdata
;
675 xmlDocDumpFormatMemory (client
->doc
, &xmlbuf
, &xmlbuflen
, 0);
677 return GPG_ERR_ENOMEM
;
680 pubkey
= client
->crypto
->pkey_sexp
;
684 sigkey
= client
->crypto
->sigpkey_sexp
;
687 gcry_sexp_build (&client
->crypto
->save
.sigpkey
, 0, "%S", pubkey
);
688 sigkey
= client
->crypto
->save
.sigpkey
;
692 pthread_cleanup_push (xmlFree
, xmlbuf
);
693 rc
= encrypt_data_file (client
->ctx
, client
->crypto
, pubkey
, sigkey
,
694 client
->filename
, xmlbuf
, xmlbuflen
);
695 if (pubkey
== client
->crypto
->save
.pkey
)
699 gcry_sexp_release (client
->crypto
->pkey_sexp
);
700 client
->crypto
->pkey_sexp
= client
->crypto
->save
.pkey
;
701 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
702 client
->crypto
->grip
);
705 gcry_sexp_release (pubkey
);
707 client
->crypto
->save
.pkey
= NULL
;
711 gcry_pk_get_keygrip (sigkey
, client
->crypto
->sign_grip
);
713 if (sigkey
== client
->crypto
->save
.sigpkey
)
717 if (client
->crypto
->sigpkey_sexp
)
718 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
720 client
->crypto
->sigpkey_sexp
= client
->crypto
->save
.sigpkey
;
721 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
722 client
->crypto
->sign_grip
);
725 gcry_sexp_release (sigkey
);
727 client
->crypto
->save
.sigpkey
= NULL
;
732 /* This is safe since it is a fast operation. */
734 pthread_cleanup_push (cleanup_cache_mutex
, NULL
);
735 cdata
= cache_get_data (client
->md5file
);
736 int cached
= cdata
!= NULL
;
739 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
741 /* Update in case of any --keygrip argument */
743 gcry_sexp_release (cdata
->pubkey
);
745 gcry_sexp_build ((gcry_sexp_t
*) & cdata
->pubkey
, NULL
, "%S",
746 client
->crypto
->pkey_sexp
);
749 gcry_sexp_release (cdata
->sigkey
);
751 gcry_sexp_build ((gcry_sexp_t
*) & cdata
->sigkey
, NULL
, "%S",
752 client
->crypto
->sigpkey_sexp
);
754 gcry_free (cdata
->doc
);
756 rc
= encrypt_xml (NULL
, cache_key
, cache_keysize
, GCRY_CIPHER_AES
,
757 xmlbuf
, xmlbuflen
, &cdata
->doc
, &cdata
->doclen
,
758 &cache_iv
, &cache_blocksize
, 0);
760 rc
= cache_set_data (client
->md5file
, cdata
, client
->crypto
->grip
);
761 if (!rc
&& (!cached
|| (client
->flags
& FLAG_NEW
)))
762 send_status_all (STATUS_CACHE
, NULL
);
766 update_checksum (client
);
767 client
->flags
&= ~(FLAG_NEW
);
772 pthread_cleanup_pop (1); // mutex unlock
775 pthread_cleanup_pop (1); // xmlFree
780 parse_opt_reset (void *data
, void *value
)
782 struct client_s
*client
= data
;
785 client
->opts
|= OPT_RESET
;
790 save_command (assuan_context_t ctx
, char *line
)
792 struct client_s
*client
= assuan_get_pointer (ctx
);
795 struct argv_s
*args
[] = {
796 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
,
797 parse_save_opt_no_passphrase
},
798 &(struct argv_s
) {"cipher", OPTION_TYPE_ARG
, parse_save_opt_cipher
},
799 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
801 &(struct argv_s
) {"keygrip", OPTION_TYPE_ARG
, parse_save_opt_keygrip
},
802 &(struct argv_s
) {"sign-keygrip", OPTION_TYPE_ARG
,
803 parse_save_opt_sign_keygrip
},
804 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
805 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
806 &(struct argv_s
) {"cipher-iterations", OPTION_TYPE_ARG
,
807 parse_save_opt_iterations
},
811 cleanup_save (&client
->crypto
->save
);
812 memcpy (&client
->crypto
->save
.hdr
, &client
->crypto
->hdr
,
813 sizeof (file_header_t
));
814 client
->crypto
->save
.s2k_count
=
815 config_get_ulong (client
->filename
, "s2k_count");
817 if (client
->flags
& FLAG_NEW
)
818 client
->crypto
->save
.hdr
.iterations
=
819 config_get_ulonglong (client
->filename
, "cipher_iterations");
821 rc
= parse_options (&line
, args
, client
);
823 return send_error (ctx
, rc
);
825 if ((client
->opts
& OPT_NO_PASSPHRASE
) && !(client
->flags
& FLAG_NEW
)
826 && !(client
->opts
& OPT_INQUIRE
))
827 return send_error (ctx
, GPG_ERR_WRONG_KEY_USAGE
);
829 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
830 return send_error (ctx
, gpg_error_from_syserror ());
832 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
834 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
835 return send_error (ctx
, GPG_ERR_ENOANO
);
839 rc
= cache_iscached (client
->filename
, &defer
);
842 log_write ("%s: %s", client
->filename
,
843 pwmd_strerror (GPG_ERR_KEY_EXPIRED
));
844 client
->opts
|= OPT_RESET
;
847 if (client
->opts
& OPT_RESET
)
849 rc
= cache_clear (client
->md5file
);
851 return send_error (ctx
, rc
);
853 send_status_all (STATUS_CACHE
, NULL
);
856 rc
= update_element_mtime (xmlDocGetRootElement (client
->doc
));
858 return send_error (ctx
, rc
);
860 if (!rc
&& !client
->crypto
->save
.pkey
&&
861 (client
->flags
& FLAG_NEW
|| client
->opts
& OPT_INQUIRE
))
863 rc
= send_status (client
->ctx
, STATUS_GENKEY
, NULL
);
866 struct inquire_data_s idata
= { 0 };
867 char *params
= client
->opts
& OPT_INQUIRE
? NULL
868 : default_key_params (client
->crypto
);
870 pthread_cleanup_push (xfree
, params
);
871 idata
.crypto
= client
->crypto
;
876 idata
.len
= strlen (params
);
881 client
->crypto
->agent
->inquire_data
= &idata
;
882 client
->crypto
->agent
->inquire_cb
= NULL
;
883 rc
= generate_key (client
->crypto
, params
,
884 (client
->opts
& OPT_NO_PASSPHRASE
), 1);
885 pthread_cleanup_pop (1);
890 rc
= save_finalize (ctx
);
892 cleanup_crypto_stage1 (client
->crypto
);
893 (void) kill_scd (client
->crypto
->agent
);
894 return send_error (ctx
, rc
);
898 do_delete (assuan_context_t ctx
, char *line
)
900 struct client_s
*client
= assuan_get_pointer (ctx
);
905 if (strchr (line
, '\t'))
906 req
= str_split (line
, "\t", 0);
908 req
= str_split (line
, " ", 0);
911 return GPG_ERR_SYNTAX
;
913 n
= find_root_element (client
->doc
, &req
, &rc
, NULL
, 0, 0);
921 * No sub-node defined. Remove the entire node (root element).
927 rc
= unlink_node (n
);
936 find_elements (client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
944 rc
= unlink_node (n
);
952 delete_command (assuan_context_t ctx
, char *line
)
954 struct client_s
*client
= assuan_get_pointer (ctx
);
956 struct argv_s
*args
[] = {
957 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
961 rc
= parse_options (&line
, args
, client
);
963 return send_error (ctx
, rc
);
965 if (client
->opts
& OPT_INQUIRE
)
967 unsigned char *result
;
970 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
972 return send_error (ctx
, rc
);
974 line
= (char *) result
;
977 rc
= do_delete (ctx
, line
);
979 if (client
->opts
& OPT_INQUIRE
)
982 return send_error (ctx
, rc
);
986 store_command (assuan_context_t ctx
, char *line
)
988 struct client_s
*client
= assuan_get_pointer (ctx
);
991 unsigned char *result
;
993 xmlNodePtr n
, parent
;
995 char *content
= NULL
;
997 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
999 return send_error (ctx
, rc
);
1001 req
= str_split ((char *) result
, "\t", 0);
1005 return send_error (ctx
, GPG_ERR_SYNTAX
);
1007 len
= strv_length (req
);
1008 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1009 if (*(req
+ 1) && !valid_element_path (req
, has_content
))
1012 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1015 if (has_content
|| !*req
[len
- 1])
1018 content
= req
[len
- 1];
1019 req
[len
- 1] = NULL
;
1023 n
= find_root_element (client
->doc
, &req
, &rc
, NULL
, 0, 0);
1024 if (rc
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1026 rc
= new_root_element (client
->doc
, *req
);
1030 return send_error (ctx
, GPG_ERR_SYNTAX
);
1039 return send_error (ctx
, rc
);
1044 if (req
[1] && *req
[1])
1047 parent
= create_elements_cb (n
, req
+ 1, &rc
, NULL
);
1049 parent
= find_elements (client
->doc
, n
->children
, req
+ 1, &rc
,
1050 NULL
, NULL
, create_elements_cb
, 0, 0, NULL
,
1056 n
= find_text_node (parent
->children
);
1058 xmlNodeSetContent (n
, (xmlChar
*) content
);
1060 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1062 update_element_mtime (parent
);
1067 return send_error (ctx
, rc
);
1071 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1076 int progress
= config_get_integer ("global", "xfer_progress");
1080 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1081 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1082 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1090 if (sent
+ to_send
> total
)
1091 to_send
= total
- sent
;
1093 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1094 flush
? 0 : to_send
);
1097 sent
+= flush
? 0 : to_send
;
1099 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1100 (sent
== total
&& flush
))
1101 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1103 if (!flush
&& !rc
&& sent
== total
)
1110 while (!rc
&& sent
< total
);
1116 do_get (assuan_context_t ctx
, char *line
)
1118 struct client_s
*client
= assuan_get_pointer (ctx
);
1123 req
= str_split (line
, "\t", 0);
1128 return GPG_ERR_SYNTAX
;
1131 n
= find_root_element (client
->doc
, &req
, &rc
, NULL
, 0, 0);
1140 find_elements (client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
1147 if (!n
|| !n
->children
)
1148 return GPG_ERR_NO_DATA
;
1150 n
= find_text_node (n
->children
);
1151 if (!n
|| !n
->content
|| !*n
->content
)
1152 return GPG_ERR_NO_DATA
;
1154 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1159 get_command (assuan_context_t ctx
, char *line
)
1161 struct client_s
*client
= assuan_get_pointer (ctx
);
1163 struct argv_s
*args
[] = {
1164 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1168 rc
= parse_options (&line
, args
, client
);
1170 return send_error (ctx
, rc
);
1172 if (client
->opts
& OPT_INQUIRE
)
1174 unsigned char *result
;
1177 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1179 return send_error (ctx
, rc
);
1181 line
= (char *) result
;
1184 rc
= do_get (ctx
, line
);
1186 if (client
->opts
& OPT_INQUIRE
)
1189 return send_error (ctx
, rc
);
1192 static void list_command_cleanup1 (void *arg
);
1194 realpath_command (assuan_context_t ctx
, char *line
)
1196 struct string_s
*string
= NULL
;
1198 struct client_s
*client
= assuan_get_pointer (ctx
);
1199 struct argv_s
*args
[] = {
1200 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1204 rc
= parse_options (&line
, args
, client
);
1206 return send_error (ctx
, rc
);
1208 if (client
->opts
& OPT_INQUIRE
)
1210 unsigned char *result
;
1213 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1215 return send_error (ctx
, rc
);
1217 line
= (char *) result
;
1220 rc
= build_realpath (client
->doc
, line
, &string
);
1223 pthread_cleanup_push (list_command_cleanup1
, string
);
1224 rc
= xfer_data (ctx
, string
->str
, string
->len
);
1225 pthread_cleanup_pop (1);
1228 if (client
->opts
& OPT_INQUIRE
)
1231 return send_error (ctx
, rc
);
1235 list_command_cleanup1 (void *arg
)
1238 string_free ((struct string_s
*) arg
, 1);
1242 list_command_cleanup2 (void *arg
)
1244 struct element_list_s
*elements
= arg
;
1250 int total
= slist_length (elements
->list
);
1253 for (i
= 0; i
< total
; i
++)
1255 char *tmp
= slist_nth_data (elements
->list
, i
);
1259 slist_free (elements
->list
);
1262 if (elements
->prefix
)
1263 xfree (elements
->prefix
);
1266 strv_free (elements
->req
);
1273 parse_list_opt_norecurse (void *data
, void *value
)
1275 struct client_s
*client
= data
;
1277 client
->opts
&= ~(OPT_LIST_RECURSE
);
1282 parse_list_opt_verbose (void *data
, void *value
)
1284 struct client_s
*client
= data
;
1286 client
->opts
|= OPT_LIST_VERBOSE
;
1291 parse_list_opt_target (void *data
, void *value
)
1293 struct client_s
*client
= data
;
1295 client
->opts
|= OPT_LIST_WITH_TARGET
;
1300 parse_list_opt_all (void *data
, void *value
)
1302 struct client_s
*client
= data
;
1304 client
->opts
|= OPT_LIST_ALL
;
1309 list_path_once (struct client_s
*client
, char *line
,
1310 struct element_list_s
*elements
, struct string_s
*result
)
1314 elements
->req
= str_split (line
, " ", 0);
1316 strv_printf (&elements
->req
, "%s", line
);
1318 rc
= create_path_list (client
->doc
, elements
, *elements
->req
);
1319 if (rc
== GPG_ERR_ELOOP
&& elements
->verbose
)
1326 int total
= slist_length (elements
->list
);
1330 rc
= GPG_ERR_NO_DATA
;
1336 for (i
= 0; i
< total
; i
++)
1338 char *tmp
= slist_nth_data (elements
->list
, i
);
1340 string_append_printf (result
, "%s%s", tmp
,
1341 i
+ 1 == total
? "" : "\n");
1347 rc
= GPG_ERR_NO_DATA
;
1354 has_list_flag (char *path
, char *flags
)
1358 while (*p
&& *++p
!= ' ');
1367 for (f
= flags
; *f
&& *f
!= ' '; f
++)
1378 do_list (assuan_context_t ctx
, char *line
)
1380 struct client_s
*client
= assuan_get_pointer (ctx
);
1382 struct element_list_s
*elements
= NULL
;
1384 elements
= xcalloc (1, sizeof (struct element_list_s
));
1386 return GPG_ERR_ENOMEM
;
1388 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1390 (client
->opts
& OPT_LIST_VERBOSE
) | (client
->opts
& OPT_LIST_WITH_TARGET
);
1391 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1393 if (!line
|| !*line
)
1395 struct string_s
*str
= NULL
;
1397 pthread_cleanup_push (list_command_cleanup2
, elements
);
1398 rc
= list_root_elements (client
->doc
, &str
, elements
->verbose
,
1399 elements
->with_target
);
1400 pthread_cleanup_pop (1);
1401 pthread_cleanup_push (list_command_cleanup1
, str
);
1405 if (client
->opts
& OPT_LIST_ALL
)
1407 char **roots
= str_split (str
->str
, "\n", 0);
1410 pthread_cleanup_push (req_cleanup
, roots
);
1411 string_truncate (str
, 0);
1413 for (p
= roots
; *p
; p
++)
1415 if (strchr (*p
, ' '))
1417 if (has_list_flag (*p
, "EO"))
1419 string_append_printf (str
, "%s%s", *p
,
1420 *(p
+ 1) ? "\n" : "");
1425 elements
= xcalloc (1, sizeof (struct element_list_s
));
1428 rc
= GPG_ERR_ENOMEM
;
1432 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1434 (client
->opts
& OPT_LIST_VERBOSE
) | (client
->opts
&
1435 OPT_LIST_WITH_TARGET
);
1436 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1437 pthread_cleanup_push (list_command_cleanup2
, elements
);
1438 rc
= list_path_once (client
, *p
, elements
, str
);
1439 pthread_cleanup_pop (1);
1444 string_append (str
, "\n");
1447 pthread_cleanup_pop (1);
1451 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1454 pthread_cleanup_pop (1);
1458 pthread_cleanup_push (list_command_cleanup2
, elements
);
1459 struct string_s
*str
= string_new (NULL
);
1460 pthread_cleanup_push (list_command_cleanup1
, str
);
1461 rc
= list_path_once (client
, line
, elements
, str
);
1463 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1465 pthread_cleanup_pop (1);
1466 pthread_cleanup_pop (1);
1471 list_command (assuan_context_t ctx
, char *line
)
1473 struct client_s
*client
= assuan_get_pointer (ctx
);
1475 struct argv_s
*args
[] = {
1476 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
,
1477 parse_list_opt_norecurse
},
1478 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_list_opt_verbose
},
1479 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1480 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
,
1481 parse_list_opt_target
},
1482 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_all
},
1486 if (disable_list_and_dump
== 1)
1487 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1489 client
->opts
|= OPT_LIST_RECURSE
;
1490 rc
= parse_options (&line
, args
, client
);
1492 return send_error (ctx
, rc
);
1494 if (client
->opts
& OPT_LIST_ALL
)
1495 client
->opts
|= OPT_LIST_RECURSE
| OPT_LIST_VERBOSE
;
1497 if (client
->opts
& OPT_INQUIRE
)
1499 unsigned char *result
;
1502 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1504 return send_error (ctx
, rc
);
1506 line
= (char *) result
;
1509 rc
= do_list (ctx
, line
);
1511 if (client
->opts
& OPT_INQUIRE
)
1514 return send_error (ctx
, rc
);
1518 * req[0] - element path
1521 attribute_list (assuan_context_t ctx
, char **req
)
1523 struct client_s
*client
= assuan_get_pointer (ctx
);
1524 char **attrlist
= NULL
;
1532 if (!req
|| !req
[0])
1533 return GPG_ERR_SYNTAX
;
1535 if ((path
= str_split (req
[0], "\t", 0)) == NULL
)
1538 * The first argument may be only a root element.
1540 if ((path
= str_split (req
[0], " ", 0)) == NULL
)
1541 return GPG_ERR_SYNTAX
;
1544 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
1554 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
1555 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1566 for (a
= n
->properties
; a
; a
= a
->next
)
1570 if ((pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *))) == NULL
)
1573 strv_free (attrlist
);
1575 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1576 pwmd_strerror (GPG_ERR_ENOMEM
));
1577 return GPG_ERR_ENOMEM
;
1582 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
,
1584 && an
->content
? (char *) an
->content
: "");
1588 strv_free (attrlist
);
1589 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1590 pwmd_strerror (GPG_ERR_ENOMEM
));
1591 return GPG_ERR_ENOMEM
;
1594 attrlist
[++i
] = NULL
;
1598 return GPG_ERR_NO_DATA
;
1600 line
= strv_join ("\n", attrlist
);
1604 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1605 pwmd_strerror (GPG_ERR_ENOMEM
));
1606 strv_free (attrlist
);
1607 return GPG_ERR_ENOMEM
;
1610 pthread_cleanup_push (xfree
, line
);
1611 pthread_cleanup_push (req_cleanup
, attrlist
);
1612 rc
= xfer_data (ctx
, line
, strlen (line
));
1613 pthread_cleanup_pop (1);
1614 pthread_cleanup_pop (1);
1619 * req[0] - attribute
1620 * req[1] - element path
1623 attribute_delete (struct client_s
*client
, char **req
)
1629 if (!req
|| !req
[0] || !req
[1])
1630 return GPG_ERR_SYNTAX
;
1632 if (!strcmp (req
[0], "_name"))
1633 return GPG_ERR_INV_ATTR
;
1635 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1638 * The first argument may be only a root element.
1640 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1641 return GPG_ERR_SYNTAX
;
1644 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
1650 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
1651 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1656 rc
= delete_attribute (n
, (xmlChar
*) req
[0]);
1664 create_element_path (struct client_s
*client
,
1665 char ***elements
, gpg_error_t
* rc
, xmlNodePtr parent
)
1667 char **req
= *elements
;
1668 char **req_orig
= strv_dup (req
);
1669 xmlNodePtr n
= NULL
;
1675 *rc
= GPG_ERR_ENOMEM
;
1676 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1677 pwmd_strerror (GPG_ERR_ENOMEM
));
1682 n
= find_root_element (client
->doc
, &req
, rc
, NULL
, 0, 0);
1685 if (*rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1688 *rc
= new_root_element (client
->doc
, req
[0]);
1694 else if (n
== parent
)
1696 *rc
= GPG_ERR_CONFLICT
;
1703 n
= create_target_elements_cb (n
, req
+ 1, rc
, NULL
);
1705 n
= find_elements (client
->doc
, n
->children
, req
+ 1, rc
, NULL
, NULL
,
1706 create_target_elements_cb
, 0, 0, parent
, 0);
1712 * Reset the position of the element tree now that the elements
1713 * have been created.
1718 n
= find_root_element (client
->doc
, &req
, rc
, NULL
, 0, 0);
1722 n
= find_elements (client
->doc
, n
->children
, req
+ 1, rc
,
1723 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1730 strv_free (req_orig
);
1737 * Creates a "target" attribute. When other commands encounter an element with
1738 * this attribute, the element path is modified to the target value. If the
1739 * source element path doesn't exist when using 'ATTR SET target', it is
1740 * created, but the destination element path must exist.
1742 * req[0] - source element path
1743 * req[1] - destination element path
1746 target_attribute (struct client_s
*client
, char **req
)
1748 char **src
, **dst
, *line
= NULL
, **odst
= NULL
;
1752 if (!req
|| !req
[0] || !req
[1])
1753 return GPG_ERR_SYNTAX
;
1755 if ((src
= str_split (req
[0], "\t", 0)) == NULL
)
1758 * The first argument may be only a root element.
1760 if ((src
= str_split (req
[0], " ", 0)) == NULL
)
1761 return GPG_ERR_SYNTAX
;
1764 if (!valid_element_path (src
, 0))
1765 return GPG_ERR_INV_VALUE
;
1767 if ((dst
= str_split (req
[1], "\t", 0)) == NULL
)
1770 * The first argument may be only a root element.
1772 if ((dst
= str_split (req
[1], " ", 0)) == NULL
)
1774 rc
= GPG_ERR_SYNTAX
;
1779 odst
= strv_dup (dst
);
1782 rc
= GPG_ERR_ENOMEM
;
1786 n
= find_root_element (client
->doc
, &dst
, &rc
, NULL
, 0, 0);
1788 * Make sure the destination element path exists.
1795 n
= find_elements (client
->doc
, n
->children
, dst
+ 1, &rc
,
1796 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1801 rc
= validate_target_attribute (client
->doc
, req
[0], n
);
1802 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1805 n
= create_element_path (client
, &src
, &rc
, NULL
);
1809 line
= strv_join ("\t", odst
);
1812 rc
= GPG_ERR_ENOMEM
;
1816 rc
= add_attribute (n
, "target", line
);
1827 * req[0] - attribute
1828 * req[1] - element path
1831 attribute_get (assuan_context_t ctx
, char **req
)
1833 struct client_s
*client
= assuan_get_pointer (ctx
);
1839 if (!req
|| !req
[0] || !req
[1])
1840 return GPG_ERR_SYNTAX
;
1842 if (strchr (req
[1], '\t'))
1844 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1845 return GPG_ERR_SYNTAX
;
1849 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1850 return GPG_ERR_SYNTAX
;
1853 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
1860 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
1861 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1869 if ((a
= xmlGetProp (n
, (xmlChar
*) req
[0])) == NULL
)
1870 return GPG_ERR_NOT_FOUND
;
1872 pthread_cleanup_push (xmlFree
, a
);
1875 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
1877 rc
= GPG_ERR_NO_DATA
;
1879 pthread_cleanup_pop (1);
1888 * req[0] - attribute
1889 * req[1] - element path
1893 attribute_set (struct client_s
*client
, char **req
)
1899 if (!req
|| !req
[0] || !req
[1])
1900 return GPG_ERR_SYNTAX
;
1903 * Reserved attribute names.
1905 if (!strcmp (req
[0], "_name"))
1906 return GPG_ERR_INV_ATTR
;
1907 else if (!strcmp (req
[0], "target"))
1908 return target_attribute (client
, req
+ 1);
1910 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1913 * The first argument may be only a root element.
1915 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1916 return GPG_ERR_SYNTAX
;
1919 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
1926 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
1927 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1933 rc
= add_attribute (n
, req
[0], req
[2]);
1942 * req[1] - attribute name or element path if command is LIST
1943 * req[2] - element path
1944 * req[2] - element path or value
1948 do_attr (assuan_context_t ctx
, char *line
)
1950 struct client_s
*client
= assuan_get_pointer (ctx
);
1954 req
= str_split (line
, " ", 4);
1955 if (!req
|| !req
[0] || !req
[1])
1958 return GPG_ERR_SYNTAX
;
1961 pthread_cleanup_push (req_cleanup
, req
);
1963 if (strcasecmp (req
[0], "SET") == 0)
1964 rc
= attribute_set (client
, req
+ 1);
1965 else if (strcasecmp (req
[0], "GET") == 0)
1966 rc
= attribute_get (ctx
, req
+ 1);
1967 else if (strcasecmp (req
[0], "DELETE") == 0)
1968 rc
= attribute_delete (client
, req
+ 1);
1969 else if (strcasecmp (req
[0], "LIST") == 0)
1970 rc
= attribute_list (ctx
, req
+ 1);
1972 rc
= GPG_ERR_SYNTAX
;
1974 pthread_cleanup_pop (1);
1979 attr_command (assuan_context_t ctx
, char *line
)
1981 struct client_s
*client
= assuan_get_pointer (ctx
);
1983 struct argv_s
*args
[] = {
1984 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1988 rc
= parse_options (&line
, args
, client
);
1990 return send_error (ctx
, rc
);
1992 if (client
->opts
& OPT_INQUIRE
)
1994 unsigned char *result
;
1997 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1999 return send_error (ctx
, rc
);
2001 line
= (char *) result
;
2004 rc
= do_attr (ctx
, line
);
2006 if (client
->opts
& OPT_INQUIRE
)
2009 return send_error (ctx
, rc
);
2013 parse_iscached_opt_lock (void *data
, void *value
)
2015 struct client_s
*client
= data
;
2018 client
->opts
|= OPT_LOCK
;
2023 iscached_command (assuan_context_t ctx
, char *line
)
2025 struct client_s
*client
= assuan_get_pointer (ctx
);
2027 unsigned char md5file
[16];
2028 struct argv_s
*args
[] = {
2029 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2033 if (!line
|| !*line
)
2034 return send_error (ctx
, GPG_ERR_SYNTAX
);
2036 rc
= parse_options (&line
, args
, client
);
2038 return send_error (ctx
, rc
);
2039 else if (!valid_filename (line
))
2040 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2042 rc
= cache_iscached (line
, NULL
);
2043 if (client
->opts
& OPT_LOCK
2044 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2046 gpg_error_t trc
= rc
;
2047 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2048 if (memcmp (md5file
, client
->md5file
, 16))
2049 cleanup_client (client
);
2051 memcpy (client
->md5file
, md5file
, 16);
2052 rc
= do_lock (client
, 1);
2057 return send_error (ctx
, rc
);
2061 clearcache_command (assuan_context_t ctx
, char *line
)
2063 gpg_error_t rc
= 0, all_rc
= 0;
2064 unsigned char md5file
[16];
2068 struct client_thread_s
*once
= NULL
;
2071 MUTEX_LOCK (&cn_mutex
);
2072 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
2074 if (!line
|| !*line
)
2077 t
= slist_length (cn_thread_list
);
2079 for (i
= 0; i
< t
; i
++)
2081 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2086 /* Lock each connected clients' file mutex to prevent any other client
2087 * from accessing the cache entry (the file mutex is locked upon
2088 * command startup). The cache for the entry is not cleared if the
2089 * file mutex is locked by another client to prevent this function
2094 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
2095 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2097 if (pthread_equal (pthread_self (), thd
->tid
))
2101 if (!thd
->cl
->filename
||
2102 cache_iscached (thd
->cl
->filename
,
2103 NULL
) == GPG_ERR_NO_DATA
)
2109 cache_defer_clear (thd
->cl
->md5file
);
2112 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2120 rc
= cache_clear (thd
->cl
->md5file
);
2124 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2131 /* A single data filename was specified. Lock only this data file
2132 * mutex and free the cache entry. */
2135 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2137 if (!memcmp (thd
->cl
->md5file
, md5file
, sizeof (md5file
)))
2140 cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
2141 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2143 if (pthread_equal (pthread_self (), thd
->tid
))
2150 rc
= cache_clear (thd
->cl
->md5file
);
2151 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2154 cache_defer_clear (thd
->cl
->md5file
);
2161 /* Only connected clients' cache entries have been cleared. Now clear any
2162 * remaining cache entries without clients but only if there wasn't an
2163 * error from above since this would defeat the locking check of the
2164 * remaining entries. */
2167 /* No clients are using the specified file. */
2168 else if (!all_rc
&& !rc
&& !once
)
2170 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2171 rc
= cache_clear (md5file
);
2174 /* Release the connection mutex. */
2175 pthread_cleanup_pop (1);
2179 send_status_all (STATUS_CACHE
, NULL
);
2181 /* One or more files were locked while clearing all cache entries. */
2185 return send_error (ctx
, rc
);
2189 cachetimeout_command (assuan_context_t ctx
, char *line
)
2191 unsigned char md5file
[16];
2193 char **req
= str_split (line
, " ", 0);
2197 if (!req
|| !*req
|| !req
[1])
2200 return send_error (ctx
, GPG_ERR_SYNTAX
);
2204 timeout
= (int) strtol (req
[1], &p
, 10);
2205 if (errno
!= 0 || *p
!= 0 || timeout
< -1)
2208 return send_error (ctx
, GPG_ERR_SYNTAX
);
2211 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, req
[0], strlen (req
[0]));
2213 rc
= cache_set_timeout (md5file
, timeout
);
2214 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2217 MUTEX_LOCK (&rcfile_mutex
);
2218 config_set_int_param (&global_config
, req
[0], "cache_timeout", req
[1]);
2219 MUTEX_UNLOCK (&rcfile_mutex
);
2223 return send_error (ctx
, rc
);
2227 dump_command (assuan_context_t ctx
, char *line
)
2231 struct client_s
*client
= assuan_get_pointer (ctx
);
2234 if (disable_list_and_dump
== 1)
2235 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2237 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2241 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2242 pwmd_strerror (GPG_ERR_ENOMEM
));
2243 return send_error (ctx
, GPG_ERR_ENOMEM
);
2246 pthread_cleanup_push (xmlFree
, xml
);
2247 rc
= xfer_data (ctx
, (char *) xml
, len
);
2248 pthread_cleanup_pop (1);
2249 return send_error (ctx
, rc
);
2253 getconfig_command (assuan_context_t ctx
, char *line
)
2255 struct client_s
*client
= assuan_get_pointer (ctx
);
2257 char filename
[255] = { 0 }, param
[747] =
2260 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2262 if (!line
|| !*line
)
2263 return send_error (ctx
, GPG_ERR_SYNTAX
);
2265 if (strchr (line
, ' '))
2267 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2272 if (fp
&& !valid_filename (fp
))
2273 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2275 paramp
= str_down (paramp
);
2276 if (!strcmp (paramp
, "cipher") && fp
)
2278 struct crypto_s
*crypto
;
2280 rc
= init_client_crypto (&crypto
);
2283 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2287 gcry_cipher_algo_name (cipher_to_gcrypt (crypto
->hdr
.flags
));
2297 cleanup_crypto (&crypto
);
2298 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2299 return send_error (ctx
, rc
);
2304 else if (!strcmp (paramp
, "cipher_iterations") && fp
)
2306 struct crypto_s
*crypto
;
2308 rc
= init_client_crypto (&crypto
);
2311 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2314 tmp
= str_asprintf ("%llu",
2315 (unsigned long long) crypto
->hdr
.
2318 rc
= GPG_ERR_ENOMEM
;
2322 cleanup_crypto (&crypto
);
2323 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2324 return send_error (ctx
, rc
);
2329 else if (!strcmp (paramp
, "passphrase"))
2330 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2332 p
= config_get_value (fp
? fp
: "global", paramp
);
2334 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2336 tmp
= expand_homedir (p
);
2340 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2341 pwmd_strerror (GPG_ERR_ENOMEM
));
2342 return send_error (ctx
, GPG_ERR_ENOMEM
);
2347 pthread_cleanup_push (xfree
, p
);
2348 rc
= xfer_data (ctx
, p
, strlen (p
));
2349 pthread_cleanup_pop (1);
2350 return send_error (ctx
, rc
);
2355 xmlXPathContextPtr xp
;
2356 xmlXPathObjectPtr result
;
2362 xpath_command_cleanup (void *arg
)
2364 struct xpath_s
*xpath
= arg
;
2369 req_cleanup (xpath
->req
);
2372 xmlBufferFree (xpath
->buf
);
2375 xmlXPathFreeObject (xpath
->result
);
2378 xmlXPathFreeContext (xpath
->xp
);
2382 do_xpath (assuan_context_t ctx
, char *line
)
2385 struct client_s
*client
= assuan_get_pointer (ctx
);
2386 struct xpath_s _x
= { 0 };
2387 struct xpath_s
*xpath
= &_x
;
2389 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2391 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2392 return GPG_ERR_ENOMEM
;
2395 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2398 rc
= GPG_ERR_BAD_DATA
;
2403 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2406 rc
= GPG_ERR_BAD_DATA
;
2410 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2412 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2416 rc
= recurse_xpath_nodeset (client
->doc
, xpath
->result
->nodesetval
,
2417 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2421 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2423 rc
= GPG_ERR_NO_DATA
;
2426 else if (xpath
->req
[1])
2432 pthread_cleanup_push (xpath_command_cleanup
, &xpath
);
2433 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2434 xmlBufferLength (xpath
->buf
));
2435 pthread_cleanup_pop (0);
2437 xpath_command_cleanup (xpath
);
2442 xpath_command (assuan_context_t ctx
, char *line
)
2444 struct client_s
*client
= assuan_get_pointer (ctx
);
2446 struct argv_s
*args
[] = {
2447 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2451 if (disable_list_and_dump
== 1)
2452 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2454 rc
= parse_options (&line
, args
, client
);
2456 return send_error (ctx
, rc
);
2458 if (client
->opts
& OPT_INQUIRE
)
2460 unsigned char *result
;
2463 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2465 return send_error (ctx
, rc
);
2467 line
= (char *) result
;
2470 if (!line
|| !*line
)
2471 rc
= GPG_ERR_SYNTAX
;
2474 rc
= do_xpath (ctx
, line
);
2476 if (client
->opts
& OPT_INQUIRE
)
2479 return send_error (ctx
, rc
);
2483 do_xpathattr (assuan_context_t ctx
, char *line
)
2485 struct client_s
*client
= assuan_get_pointer (ctx
);
2489 struct xpath_s _x
= { 0 };
2490 struct xpath_s
*xpath
= &_x
;
2492 if ((req
= str_split (line
, " ", 3)) == NULL
)
2493 return GPG_ERR_ENOMEM
;
2497 rc
= GPG_ERR_SYNTAX
;
2501 if (!strcasecmp (req
[0], "SET"))
2503 else if (!strcasecmp (req
[0], "DELETE"))
2507 rc
= GPG_ERR_SYNTAX
;
2511 if (!req
[1] || !req
[2])
2513 rc
= GPG_ERR_SYNTAX
;
2517 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2519 rc
= GPG_ERR_ENOMEM
;
2523 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2525 rc
= GPG_ERR_SYNTAX
;
2529 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2532 rc
= GPG_ERR_BAD_DATA
;
2537 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2540 rc
= GPG_ERR_BAD_DATA
;
2544 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2546 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2550 rc
= recurse_xpath_nodeset (client
->doc
, xpath
->result
->nodesetval
,
2551 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2552 (xmlChar
*) req
[1]);
2555 xpath_command_cleanup (xpath
);
2560 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2562 xpathattr_command (assuan_context_t ctx
, char *line
)
2564 struct client_s
*client
= assuan_get_pointer (ctx
);
2566 struct argv_s
*args
[] = {
2567 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2571 if (disable_list_and_dump
== 1)
2572 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2574 rc
= parse_options (&line
, args
, client
);
2576 return send_error (ctx
, rc
);
2578 if (client
->opts
& OPT_INQUIRE
)
2580 unsigned char *result
;
2583 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2585 return send_error (ctx
, rc
);
2587 line
= (char *) result
;
2590 if (!line
|| !*line
)
2591 rc
= GPG_ERR_SYNTAX
;
2594 rc
= do_xpathattr (ctx
, line
);
2596 if (client
->opts
& OPT_INQUIRE
)
2599 return send_error (ctx
, rc
);
2603 do_import (struct client_s
*client
, unsigned char *line
)
2605 char **req
, **path
= NULL
, **path_orig
= NULL
, *content
;
2606 xmlDocPtr doc
= NULL
;
2607 xmlNodePtr n
, root
, copy
;
2610 req
= str_split ((char *) line
, "\t", 2);
2613 return GPG_ERR_SYNTAX
;
2616 path
= str_split (req
[1], "\t", 0);
2617 if (!content
|| !*content
)
2619 rc
= GPG_ERR_SYNTAX
;
2623 if (path
&& !valid_element_path (path
, 0))
2625 rc
= GPG_ERR_INV_VALUE
;
2629 doc
= xmlReadDoc ((xmlChar
*) content
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2632 rc
= GPG_ERR_BAD_DATA
;
2636 root
= xmlDocGetRootElement (doc
);
2637 rc
= validate_import (root
);
2643 path_orig
= strv_dup (path
);
2646 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2647 pwmd_strerror (GPG_ERR_ENOMEM
));
2648 rc
= GPG_ERR_ENOMEM
;
2652 xmlChar
*a
= xmlGetProp (root
, (xmlChar
*) "_name");
2655 strv_free (path_orig
);
2656 rc
= GPG_ERR_ENOMEM
;
2660 if (strv_printf (&path
, "%s", (char *) a
) == 0)
2663 strv_free (path_orig
);
2664 rc
= GPG_ERR_ENOMEM
;
2669 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
2671 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2673 strv_free (path_orig
);
2680 find_elements (client
->doc
, n
->children
, path
+ 1, &rc
, NULL
,
2681 NULL
, NULL
, 0, 0, NULL
, 1);
2683 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2685 strv_free (path_orig
);
2690 xmlNodePtr parent
= n
->parent
;
2701 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
2703 n
= create_element_path (client
, &path
, &rc
, NULL
);
2709 copy
= xmlCopyNodeList (root
);
2710 n
= xmlAddChildList (n
, copy
);
2712 rc
= GPG_ERR_BAD_DATA
;
2716 /* Check if the content root element can create a DTD root element. */
2717 if (!xmlStrEqual ((xmlChar
*) "element", root
->name
))
2719 rc
= GPG_ERR_SYNTAX
;
2725 if ((a
= xmlGetProp (root
, (xmlChar
*) "_name")) == NULL
)
2727 rc
= GPG_ERR_SYNTAX
;
2731 char *tmp
= str_dup ((char *) a
);
2733 int literal
= is_literal_element (&tmp
);
2735 if (!valid_xml_element ((xmlChar
*) tmp
) || literal
)
2738 rc
= GPG_ERR_INV_VALUE
;
2742 if (strv_printf (&path
, "%s", tmp
) == 0)
2745 rc
= GPG_ERR_ENOMEM
;
2750 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 1);
2752 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2754 rc
= GPG_ERR_BAD_DATA
;
2758 /* Overwriting the existing tree. */
2762 xmlFreeNodeList (n
);
2766 xmlSetProp (root
, (xmlChar
*) "_name", (xmlChar
*) path
[0]);
2767 n
= xmlCopyNode (root
, 1);
2768 n
= xmlAddChildList (xmlDocGetRootElement (client
->doc
), n
);
2772 rc
= update_element_mtime (n
->parent
);
2786 import_command (assuan_context_t ctx
, char *line
)
2789 struct client_s
*client
= assuan_get_pointer (ctx
);
2790 unsigned char *result
;
2793 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2795 return send_error (ctx
, rc
);
2797 rc
= do_import (client
, result
);
2798 return send_error (ctx
, rc
);
2802 do_lock (struct client_s
*client
, int add
)
2804 gpg_error_t rc
= lock_file_mutex (client
, add
);
2807 client
->flags
|= FLAG_LOCK_CMD
;
2813 lock_command (assuan_context_t ctx
, char *line
)
2815 struct client_s
*client
= assuan_get_pointer (ctx
);
2816 gpg_error_t rc
= do_lock (client
, 0);
2818 return send_error (ctx
, rc
);
2822 unlock_command (assuan_context_t ctx
, char *line
)
2824 struct client_s
*client
= assuan_get_pointer (ctx
);
2827 rc
= unlock_file_mutex (client
, 0);
2828 return send_error (ctx
, rc
);
2832 option_command (assuan_context_t ctx
, const char *name
, const char *value
)
2834 struct client_s
*client
= assuan_get_pointer (ctx
);
2835 struct agent_s
*agent
= client
->crypto
->agent
;
2838 log_write1 ("OPTION name='%s' value='%s'", name
, value
);
2840 if (strcasecmp (name
, (char *) "log_level") == 0)
2846 l
= strtol (value
, NULL
, 10);
2849 return gpg_error (GPG_ERR_INV_VALUE
);
2852 MUTEX_LOCK (&rcfile_mutex
);
2853 config_set_int_param (&global_config
, "global", "log_level", value
);
2854 MUTEX_UNLOCK (&rcfile_mutex
);
2857 else if (strcasecmp (name
, (char *) "lock-timeout") == 0)
2864 n
= strtol (value
, &p
, 10);
2866 return gpg_error (GPG_ERR_INV_VALUE
);
2869 client
->lock_timeout
= n
;
2872 else if (strcasecmp (name
, (char *) "NAME") == 0)
2874 char *tmp
= pthread_getspecific (thread_name_key
);
2879 if (!value
|| !*value
)
2881 pthread_setspecific (thread_name_key
, str_dup (""));
2885 pthread_setspecific (thread_name_key
, str_dup (value
));
2888 else if (strcasecmp (name
, (char *) "lc-messages") == 0)
2890 rc
= set_agent_option (client
->crypto
->agent
, "lc-messages", value
);
2893 xfree (agent
->lc_messages
);
2894 agent
->lc_messages
= str_dup (value
);
2897 else if (strcasecmp (name
, (char *) "lc-ctype") == 0)
2899 rc
= set_agent_option (client
->crypto
->agent
, "lc-ctype", value
);
2902 xfree (agent
->lc_ctype
);
2903 agent
->lc_ctype
= str_dup (value
);
2906 else if (strcasecmp (name
, (char *) "ttyname") == 0)
2908 rc
= set_agent_option (client
->crypto
->agent
, "ttyname", value
);
2911 xfree (agent
->ttyname
);
2912 agent
->ttyname
= str_dup (value
);
2915 else if (strcasecmp (name
, (char *) "ttytype") == 0)
2917 rc
= set_agent_option (client
->crypto
->agent
, "ttytype", value
);
2920 xfree (agent
->ttytype
);
2921 agent
->ttytype
= str_dup (value
);
2924 else if (strcasecmp (name
, (char *) "display") == 0)
2926 rc
= set_agent_option (client
->crypto
->agent
, "display", value
);
2929 xfree (agent
->display
);
2930 agent
->display
= str_dup (value
);
2933 else if (strcasecmp (name
, (char *) "desc") == 0)
2935 if (client
->crypto
->agent
->desc
)
2936 xfree (client
->crypto
->agent
->desc
);
2938 client
->crypto
->agent
->desc
= str_dup (value
);
2939 if (!client
->crypto
->agent
->desc
)
2940 rc
= GPG_ERR_ENOMEM
;
2943 else if (strcasecmp (name
, "pinentry_timeout") == 0)
2951 n
= (int) strtol (value
, &p
, 10);
2954 return gpg_error (GPG_ERR_INV_VALUE
);
2956 MUTEX_LOCK (&rcfile_mutex
);
2957 config_set_int_param (global_config
,
2958 client
->filename
? client
->filename
: "global",
2959 "pinentry_timeout", value
);
2960 MUTEX_UNLOCK (&rcfile_mutex
);
2965 return gpg_error (GPG_ERR_UNKNOWN_OPTION
);
2972 do_rename (assuan_context_t ctx
, char *line
)
2974 struct client_s
*client
= assuan_get_pointer (ctx
);
2976 char **req
, **src
, *dst
;
2979 req
= str_split (line
, " ", 0);
2981 if (!req
|| !req
[0] || !req
[1])
2984 return GPG_ERR_SYNTAX
;
2988 is_literal_element (&dst
);
2990 if (!valid_xml_element ((xmlChar
*) dst
))
2993 return GPG_ERR_INV_VALUE
;
2996 if (strchr (req
[0], '\t'))
2997 src
= str_split (req
[0], "\t", 0);
2999 src
= str_split (req
[0], " ", 0);
3003 rc
= GPG_ERR_SYNTAX
;
3007 n
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3009 n
= find_elements (client
->doc
, n
->children
, src
+ 1, &rc
, NULL
, NULL
,
3010 NULL
, 0, 0, NULL
, 0);
3015 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
3018 rc
= GPG_ERR_ENOMEM
;
3022 /* To prevent unwanted effects:
3024 * <root name="a"><b/></root>
3028 if (xmlStrEqual (a
, (xmlChar
*) dst
))
3031 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3041 for (p
= src
; *p
; p
++)
3045 strv_printf (&tmp
, "%s", *p
);
3049 strv_printf (&tmp
, "!%s", dst
);
3050 ndst
= find_root_element (client
->doc
, &tmp
, &rc
, NULL
, 0, 0);
3051 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3058 ndst
= find_elements (client
->doc
, ndst
->children
, tmp
+ 1, &rc
, NULL
,
3059 NULL
, NULL
, 0, 0, NULL
, 0);
3062 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3067 /* Target may exist:
3070 * <root name="b" target="a"/>
3079 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3086 xmlFreeNodeList (ndst
);
3089 rc
= add_attribute (n
, "_name", dst
);
3098 rename_command (assuan_context_t ctx
, char *line
)
3100 struct client_s
*client
= assuan_get_pointer (ctx
);
3102 struct argv_s
*args
[] = {
3103 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3107 rc
= parse_options (&line
, args
, client
);
3109 return send_error (ctx
, rc
);
3111 if (client
->opts
& OPT_INQUIRE
)
3113 unsigned char *result
;
3116 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3118 return send_error (ctx
, rc
);
3120 line
= (char *) result
;
3123 rc
= do_rename (ctx
, line
);
3125 if (client
->opts
& OPT_INQUIRE
)
3128 return send_error (ctx
, rc
);
3132 do_copy (assuan_context_t ctx
, char *line
)
3134 struct client_s
*client
= assuan_get_pointer (ctx
);
3136 char **req
, **src
= NULL
, **dst
= NULL
;
3137 xmlNodePtr nsrc
, ndst
, new = NULL
;
3139 req
= str_split (line
, " ", 0);
3140 if (!req
|| !req
[0] || !req
[1])
3143 return GPG_ERR_SYNTAX
;
3146 if (strchr (req
[0], '\t'))
3147 src
= str_split (req
[0], "\t", 0);
3149 src
= str_split (req
[0], " ", 0);
3153 rc
= GPG_ERR_SYNTAX
;
3157 if (strchr (req
[1], '\t'))
3158 dst
= str_split (req
[1], "\t", 0);
3160 dst
= str_split (req
[1], " ", 0);
3164 rc
= GPG_ERR_SYNTAX
;
3168 if (!valid_element_path (dst
, 0))
3170 rc
= GPG_ERR_INV_VALUE
;
3174 nsrc
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3176 nsrc
= find_elements (client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3177 NULL
, NULL
, 0, 0, NULL
, 0);
3182 new = xmlCopyNodeList (nsrc
);
3185 rc
= GPG_ERR_ENOMEM
;
3190 ndst
= find_root_element (client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3194 ndst
= find_elements (client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3195 NULL
, create_target_elements_cb
, 0, 0, NULL
, 0);
3202 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3206 ndst
= create_element_path (client
, &dst
, &rc
, NULL
);
3211 /* Merge any attributes from the src node to the initial dst node. */
3212 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
)
3214 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3217 xmlAttrPtr a
= xmlHasProp (ndst
, attr
->name
);
3221 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3222 xmlNewProp (ndst
, attr
->name
, tmp
);
3224 rc
= add_attribute (ndst
, NULL
, NULL
);
3227 xmlNodePtr n
= ndst
->children
;
3229 xmlFreeNodeList (n
);
3230 ndst
->children
= NULL
;
3234 n
= xmlCopyNodeList (new->children
);
3237 rc
= GPG_ERR_ENOMEM
;
3241 n
= xmlAddChildList (ndst
, n
);
3244 rc
= GPG_ERR_ENOMEM
;
3248 rc
= update_element_mtime (xmlDocGetRootElement (client
->doc
) ==
3249 ndst
->parent
? ndst
: ndst
->parent
);
3255 xmlUnlinkNode (new);
3256 xmlFreeNodeList (new);
3272 copy_command (assuan_context_t ctx
, char *line
)
3274 struct client_s
*client
= assuan_get_pointer (ctx
);
3276 struct argv_s
*args
[] = {
3277 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3281 rc
= parse_options (&line
, args
, client
);
3283 return send_error (ctx
, rc
);
3285 if (client
->opts
& OPT_INQUIRE
)
3287 unsigned char *result
;
3290 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3292 return send_error (ctx
, rc
);
3294 line
= (char *) result
;
3297 rc
= do_copy (ctx
, line
);
3299 if (client
->opts
& OPT_INQUIRE
)
3302 return send_error (ctx
, rc
);
3306 do_move (assuan_context_t ctx
, char *line
)
3308 struct client_s
*client
= assuan_get_pointer (ctx
);
3310 char **req
, **src
= NULL
, **dst
= NULL
;
3311 xmlNodePtr nsrc
, ndst
= NULL
;
3313 req
= str_split (line
, " ", 0);
3315 if (!req
|| !req
[0] || !req
[1])
3318 return GPG_ERR_SYNTAX
;
3321 if (strchr (req
[0], '\t'))
3322 src
= str_split (req
[0], "\t", 0);
3324 src
= str_split (req
[0], " ", 0);
3328 rc
= GPG_ERR_SYNTAX
;
3332 if (strchr (req
[1], '\t'))
3333 dst
= str_split (req
[1], "\t", 0);
3335 dst
= str_split (req
[1], " ", 0);
3337 nsrc
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3339 nsrc
= find_elements (client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3340 NULL
, NULL
, 0, 0, NULL
, 0);
3347 if (!valid_element_path (dst
, 0))
3349 rc
= GPG_ERR_INV_VALUE
;
3353 ndst
= find_root_element (client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3355 ndst
= find_elements (client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3356 NULL
, NULL
, 0, 0, NULL
, 0);
3359 ndst
= xmlDocGetRootElement (client
->doc
);
3361 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
)
3365 rc
= GPG_ERR_CONFLICT
;
3370 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3377 xmlChar
*a
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3378 xmlNodePtr dup
= find_element (ndst
->children
, (char *) a
, NULL
);
3386 if (ndst
== xmlDocGetRootElement (client
->doc
))
3388 xmlNodePtr n
= nsrc
;
3391 while (n
->parent
&& n
->parent
!= ndst
)
3394 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
3395 xmlChar
*b
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3397 if (xmlStrEqual (a
, b
))
3400 xmlUnlinkNode (nsrc
);
3402 xmlFreeNodeList (n
);
3410 xmlUnlinkNode (dup
);
3411 xmlFreeNodeList (dup
);
3415 xmlUnlinkNode (dup
);
3421 xmlChar
*name
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3423 if (nsrc
->parent
== xmlDocGetRootElement (client
->doc
)
3424 && !strcmp ((char *) name
, *dst
))
3427 rc
= GPG_ERR_CONFLICT
;
3432 ndst
= create_element_path (client
, &dst
, &rc
, nsrc
);
3438 update_element_mtime (nsrc
->parent
);
3439 xmlUnlinkNode (nsrc
);
3440 ndst
= xmlAddChildList (ndst
, nsrc
);
3443 rc
= GPG_ERR_ENOMEM
;
3445 update_element_mtime (ndst
->parent
);
3461 move_command (assuan_context_t ctx
, char *line
)
3463 struct client_s
*client
= assuan_get_pointer (ctx
);
3465 struct argv_s
*args
[] = {
3466 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3470 rc
= parse_options (&line
, args
, client
);
3472 return send_error (ctx
, rc
);
3474 if (client
->opts
& OPT_INQUIRE
)
3476 unsigned char *result
;
3479 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3481 return send_error (ctx
, rc
);
3483 line
= (char *) result
;
3486 rc
= do_move (ctx
, line
);
3488 if (client
->opts
& OPT_INQUIRE
)
3491 return send_error (ctx
, rc
);
3495 ls_command (assuan_context_t ctx
, char *line
)
3498 char *tmp
= str_asprintf ("%s/data", homedir
);
3499 char *dir
= expand_homedir (tmp
);
3500 DIR *d
= opendir (dir
);
3502 rc
= gpg_error_from_syserror ();
3508 return send_error (ctx
, rc
);
3512 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
3513 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
3519 while (!readdir_r (d
, p
, &cur
) && cur
)
3521 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
3523 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.'
3524 && cur
->d_name
[2] == '\0')
3527 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
3534 rc
= GPG_ERR_ENOMEM
;
3546 return send_error (ctx
, rc
);
3549 return send_error (ctx
, GPG_ERR_NO_DATA
);
3551 list
[strlen (list
) - 1] = 0;
3552 rc
= xfer_data (ctx
, list
, strlen (list
));
3554 return send_error (ctx
, rc
);
3558 bye_notify (assuan_context_t ctx
, char *line
)
3560 struct client_s
*cl
= assuan_get_pointer (ctx
);
3563 if (cl
->thd
->remote
)
3569 struct timeval tv
= { 0, 50000 };
3571 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
3572 if (rc
== GNUTLS_E_AGAIN
)
3573 select (0, NULL
, NULL
, NULL
, &tv
);
3575 while (rc
== GNUTLS_E_AGAIN
);
3579 /* This will let assuan_process_next() return. */
3580 fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
);
3581 cl
->last_rc
= 0; // BYE command result
3586 reset_notify (assuan_context_t ctx
, char *line
)
3588 struct client_s
*client
= assuan_get_pointer (ctx
);
3591 cleanup_client (client
);
3597 * This is called before every Assuan command.
3600 command_startup (assuan_context_t ctx
, const char *name
)
3602 struct client_s
*client
= assuan_get_pointer (ctx
);
3604 struct command_table_s
*cmd
= NULL
;
3606 log_write1 ("command='%s'", name
);
3607 client
->last_rc
= client
->opts
= 0;
3609 for (int i
= 0; command_table
[i
]; i
++)
3611 if (!strcasecmp (name
, command_table
[i
]->name
))
3613 if (command_table
[i
]->ignore_startup
)
3615 cmd
= command_table
[i
];
3620 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
3625 * This is called after every Assuan command.
3628 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
3630 struct client_s
*client
= assuan_get_pointer (ctx
);
3632 if (!(client
->flags
& FLAG_LOCK_CMD
))
3633 unlock_file_mutex (client
, 0);
3635 log_write1 (_("command completed: rc=%u"), client
->last_rc
);
3636 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
3640 help_command (assuan_context_t ctx
, char *line
)
3645 if (!line
|| !*line
)
3648 char *help
= str_dup (_("Usage: HELP [<COMMAND>]\n"
3649 "For commands that take an element path as an argument, each element is "
3650 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3653 for (i
= 0; command_table
[i
]; i
++)
3655 if (!command_table
[i
]->help
)
3658 tmp
= str_asprintf ("%s %s", help
, command_table
[i
]->name
);
3663 tmp
= strip_texi_and_wrap (help
);
3665 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3667 return send_error (ctx
, rc
);
3670 for (i
= 0; command_table
[i
]; i
++)
3672 if (!strcasecmp (line
, command_table
[i
]->name
))
3676 if (!command_table
[i
]->help
)
3679 help
= strip_texi_and_wrap (command_table
[i
]->help
);
3680 tmp
= str_asprintf (_("Usage: %s"), help
);
3682 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3684 return send_error (ctx
, rc
);
3688 return send_error (ctx
, GPG_ERR_INV_NAME
);
3692 new_command (const char *name
, int ignore
, int unlock
,
3693 gpg_error_t (*handler
) (assuan_context_t
, char *),
3699 for (i
= 0; command_table
[i
]; i
++);
3702 xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
3703 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
3704 command_table
[i
]->name
= name
;
3705 command_table
[i
]->handler
= handler
;
3706 command_table
[i
]->ignore_startup
= ignore
;
3707 command_table
[i
]->unlock
= unlock
;
3708 command_table
[i
++]->help
= help
;
3709 command_table
[i
] = NULL
;
3717 for (i
= 0; command_table
[i
]; i
++)
3718 xfree (command_table
[i
]);
3720 xfree (command_table
);
3724 sort_commands (const void *arg1
, const void *arg2
)
3726 struct command_table_s
*const *a
= arg1
;
3727 struct command_table_s
*const *b
= arg2
;
3736 return strcmp ((*a
)->name
, (*b
)->name
);
3740 passwd_command (assuan_context_t ctx
, char *line
)
3742 struct client_s
*client
= assuan_get_pointer (ctx
);
3744 struct argv_s
*args
[] = {
3745 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
3746 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
3750 if (client
->flags
& FLAG_NEW
)
3751 return send_error (ctx
, GPG_ERR_INV_STATE
);
3753 client
->crypto
->save
.s2k_count
=
3754 config_get_ulong (client
->filename
, "s2k_count");
3755 rc
= parse_options (&line
, args
, client
);
3757 return send_error (ctx
, rc
);
3759 if (!rc
&& client
->opts
& OPT_RESET
)
3761 rc
= cache_clear (client
->md5file
);
3763 send_status_all (STATUS_CACHE
, NULL
);
3768 if (client
->crypto
->save
.s2k_count
)
3769 rc
= send_to_agent (client
->crypto
->agent
, NULL
, NULL
,
3770 "OPTION s2k-count=%lu",
3771 client
->crypto
->save
.s2k_count
);
3774 rc
= agent_passwd (client
->crypto
);
3777 return send_error (ctx
, rc
);
3781 parse_keygrip_opt_sign (void *data
, void *value
)
3783 struct client_s
*client
= data
;
3786 client
->opts
|= OPT_SIGN
;
3791 keygrip_command (assuan_context_t ctx
, char *line
)
3793 struct client_s
*client
= assuan_get_pointer (ctx
);
3795 struct crypto_s
*crypto
;
3796 struct argv_s
*args
[] = {
3797 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_keygrip_opt_sign
},
3801 if (!line
|| !*line
)
3802 return send_error (ctx
, GPG_ERR_SYNTAX
);
3804 rc
= parse_options (&line
, args
, client
);
3806 return send_error (ctx
, rc
);
3808 if (!valid_filename (line
))
3809 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3811 rc
= init_client_crypto (&crypto
);
3813 return send_error (ctx
, rc
);
3815 rc
= read_data_file (line
, crypto
);
3818 char *hexgrip
= NULL
;
3820 if (client
->opts
& OPT_SIGN
)
3822 if (valid_keygrip (crypto
->sign_grip
, sizeof (crypto
->sign_grip
)))
3823 hexgrip
= bin2hex (crypto
->sign_grip
, sizeof (crypto
->sign_grip
));
3827 hexgrip
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
3830 rc
= GPG_ERR_ENOMEM
;
3832 rc
= xfer_data (ctx
, hexgrip
, strlen (hexgrip
));
3837 cleanup_crypto (&crypto
);
3838 return send_error (ctx
, rc
);
3842 parse_opt_data (void *data
, void *value
)
3844 struct client_s
*client
= data
;
3847 client
->opts
|= OPT_DATA
;
3852 getinfo_command (assuan_context_t ctx
, char *line
)
3854 struct client_s
*client
= assuan_get_pointer (ctx
);
3856 char buf
[ASSUAN_LINELENGTH
];
3857 struct argv_s
*args
[] = {
3858 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
3862 rc
= parse_options (&line
, args
, client
);
3864 return send_error (ctx
, rc
);
3866 if (!strcasecmp (line
, "clients"))
3868 if (client
->opts
& OPT_DATA
)
3870 MUTEX_LOCK (&cn_mutex
);
3871 snprintf (buf
, sizeof (buf
), "%i", slist_length (cn_thread_list
));
3872 MUTEX_UNLOCK (&cn_mutex
);
3873 rc
= xfer_data (ctx
, buf
, strlen (buf
));
3876 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
3878 else if (!strcasecmp (line
, "cache"))
3880 if (client
->opts
& OPT_DATA
)
3882 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
3883 rc
= xfer_data (ctx
, buf
, strlen (buf
));
3886 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
3888 else if (!strcasecmp (line
, "pid"))
3891 pid_t pid
= getpid ();
3893 snprintf (buf
, sizeof (buf
), "%i", pid
);
3894 rc
= xfer_data (ctx
, buf
, strlen (buf
));
3896 else if (!strcasecmp (line
, "version"))
3898 char *buf
= str_asprintf ("0x%06x %s", VERSION_HEX
,
3906 rc
= xfer_data (ctx
, buf
, strlen (buf
));
3909 else if (!strcasecmp (line
, "last_error"))
3911 if (client
->last_error
)
3912 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
3914 rc
= GPG_ERR_NO_DATA
;
3917 rc
= gpg_error (GPG_ERR_SYNTAX
);
3919 return send_error (ctx
, rc
);
3923 send_data_cb (void *user
, const void *buf
, size_t len
)
3925 assuan_context_t ctx
= user
;
3927 return assuan_send_data (ctx
, buf
, len
);
3931 send_status_cb (void *user
, const char *line
)
3933 assuan_context_t ctx
= user
;
3934 char keyword
[200], *k
;
3937 for (p
= line
, k
= keyword
; *p
; p
++)
3949 while (isspace (*p
))
3952 return assuan_write_status (ctx
, keyword
, *p
? p
: NULL
);
3956 agent_command (assuan_context_t ctx
, char *line
)
3959 struct client_s
*client
= assuan_get_pointer (ctx
);
3961 if (!line
|| !*line
)
3962 return send_error (ctx
, GPG_ERR_SYNTAX
);
3964 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 1);
3965 rc
= assuan_transact (client
->crypto
->agent
->ctx
, line
, send_data_cb
,
3966 client
->ctx
, agent_loopback_cb
, client
->crypto
,
3967 send_status_cb
, client
->ctx
);
3968 if (gpg_err_code (rc
) == GPG_ERR_ASS_CANCELED
)
3973 rc
= assuan_write_line (client
->crypto
->agent
->ctx
, "CAN");
3976 rc
= assuan_read_line (client
->crypto
->agent
->ctx
, &line
, &len
);
3978 rc
= gpg_error (GPG_ERR_ASS_CANCELED
);
3982 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 0);
3983 return send_error (ctx
, rc
);
3989 /* !BEGIN-HELP-TEXT!
3991 * This comment is used as a marker to generate the offline documentation
3992 * found in doc/pwmd.info. The indentation needs to be kept for the awk
3993 * script to determine where commands begin and end.
3995 new_command("HELP", 1, 1, help_command
, _(
3996 "HELP [<COMMAND>]\n"
3997 "Show available commands or command specific help text."
4000 new_command("AGENT", 1, 1, agent_command
, _(
4002 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4003 "@command{gpg-agent}."
4006 new_command("GETINFO", 1, 1, getinfo_command
, _(
4007 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4008 "Get server and other information: @var{cache} returns the number of cached "
4009 "documents via a status message. @var{clients} returns the number of "
4010 "connected clients via a status message. @var{pid} returns the process ID "
4011 "number of the server via a data response. @var{VERSION} returns the server "
4012 "version number and compile-time features with a data response with each "
4013 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4014 "the last failed command when available. @xref{Status Messages}. "
4016 "When the @option{--data} option is specified then the result will be send "
4017 "via a data response rather than a status message."
4020 new_command("PASSWD", 0, 0, passwd_command
, _(
4021 "PASSWD [--reset] [--s2k-count=N]\n"
4022 "Changes the passphrase of the secret key required to open the current "
4023 "file. When the @option{--reset} option is passed then the cache entry for "
4024 "the current file will be reset and the passphrase, if any, will be required "
4025 "during the next @code{OPEN}. @xref{OPEN}."
4027 "The @option{--s2k-count} option sets number of hash iterations for a "
4028 "passphrase and must be either @code{0} to use the calibrated count of the "
4029 "machine (the default), or a value greater than or equal to @code{65536}. "
4033 new_command("KEYGRIP", 1, 1, keygrip_command
, _(
4034 "KEYGRIP [--sign] <filename>\n"
4035 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4038 "When the @option{--sign} option is specified then the key used for signing "
4039 "of the specified @var{filename} will be returned."
4042 new_command("OPEN", 1, 1, open_command
, _(
4043 "OPEN [--lock] [--no-pinentry] <filename> [<passphrase>]\n"
4044 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4045 "found on the file-system then a new document will be created. If the file "
4046 "is found, it is looked for in the file cache. If cached and no "
4047 "@var{passphrase} was specified then the cached document is opened. When not "
4048 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4049 "for decryption unless @option{--no-pinentry} was specified (see below)."
4051 "When the @option{--lock} option is passed then the file mutex will be "
4052 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4053 "file has been opened."
4055 "By default, a pinentry is used to retrieve a passphrase when required. "
4056 "Passing @option{--no-pinentry} will disable pinentry use for the rest of "
4057 "the session. When pinentry use is disabled but required for some operation "
4058 "then a server @emph{INQUIRE} will be send to the client to retrieve the "
4059 "passphrase. See the @code{OPTION} command (@pxref{OPTION}), for pinentry "
4063 new_command("SAVE", 0, 0, save_command
, _(
4064 "SAVE [--no-passphrase] [--reset] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring [--sign-keygrip=hexstring]]\n"
4065 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4066 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4067 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4068 "keypair will be generated and a pinentry will be used to prompt for the "
4069 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4070 "passed, in which case the data file will not be passphrase protected."
4072 "The @option{--reset} option will clear the cache entry for the current file "
4075 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4076 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4077 "(@pxref{Configuration}) for available ciphers."
4079 "The @option{--cipher-iterations} option specifies the number of times to "
4080 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4082 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4083 "the client to obtain the key paramaters to use when generating a new "
4084 "keypair. The inquired data is expected to be an S-expression. If not "
4085 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4086 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4087 "that when this option is specified a new keypair will be generated "
4088 "reguardless if the file is a new one or not."
4090 "You can encrypt the data file to a public key other than the one that it "
4091 "was originally encrypted with by passing the @option{--keygrip} option with "
4092 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4093 "be of any key that @command{gpg-agent} knows about. The "
4094 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4095 "secret key. This option may be needed when using a smartcard."
4097 "The @option{--s2k-count} option sets number of hash iterations for a "
4098 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4099 "value which is the default. This setting only affects new files. To change "
4100 "the setting, use the @code{PASSWD} command (@pxref{PASSWD})."
4103 new_command("ISCACHED", 1, 0, iscached_command
, _(
4104 "ISCACHED [--lock] <filename>\n"
4105 "An @emph{OK} response is returned if the specified @var{filename} is found "
4106 "in the file cache. If not found in the cache but exists on the filesystem "
4107 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4110 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4111 "file exists; it does not need to be opened nor cached."
4114 new_command("CLEARCACHE", 1, 1, clearcache_command
, _(
4115 "CLEARCACHE [<filename>]\n"
4116 "Clears a file cache entry for all or the specified @var{filename}."
4119 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command
, _(
4120 "CACHETIMEOUT <filename> <seconds>\n"
4121 "The time in @var{seconds} until @var{filename} will be removed from the "
4122 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4123 "the passphrase for each @code{OPEN} command (@pxref{OPEN}). "
4124 "@xref{Configuration}, and the @code{cache_timeout} parameter."
4127 new_command("LIST", 0, 1, list_command
, _(
4128 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4129 "If no element path is given then a newline separated list of root elements "
4130 "is returned with a data response. If given, then all reachable elements "
4131 "of the specified element path are returned unless the @option{--no-recurse} "
4132 "option is specified. If specified, only the child elements of the element "
4133 "path are returned without recursing into grandchildren. Each resulting "
4134 "element is prefixed with the literal @code{!} character when the element "
4135 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4137 "When the @option{--verbose} option is passed then each element path "
4138 "returned will have zero or more flags appened to it. These flags are "
4139 "delimited from the element path by a single space character. A flag itself "
4140 "is a single character. Flag @code{+} indicates that there are child nodes of "
4141 "the current element path. Flag @code{E} indicates that an element of an "
4142 "element path contained in a @var{target} attribute could not be found. Flag "
4143 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4144 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4145 "of a @var{target} attribute (see below)."
4147 "The @option{--with-target} option implies @option{--verbose} and will append "
4148 "an additional flag @code{T} followed by a single space then an element path. "
4149 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4150 "current element when it contains a @var{target} attribute. When no "
4151 "@var{target} attribute is found then no flag will be appended."
4153 "The @option{--no-recurse} option limits the amount of data returned to only "
4154 "the listing of children of the specified element path and not any "
4157 "The @option{--all} option lists the entire element tree for each root "
4158 "element. This option also implies option @option{--verbose}."
4160 "When the @option{--inquire} option is passed then all remaining non-option "
4161 "arguments are retrieved via a server @emph{INQUIRE}."
4164 new_command("REALPATH", 0, 1, realpath_command
, _(
4165 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4166 "Resolves all @code{target} attributes of the specified element path and "
4167 "returns the result with a data response. @xref{Target Attribute}, for details."
4169 "When the @option{--inquire} option is passed then all remaining non-option "
4170 "arguments are retrieved via a server @emph{INQUIRE}."
4173 new_command("STORE", 0, 1, store_command
, _(
4174 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4175 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4177 "Creates a new element path or modifies the @var{content} of an existing "
4178 "element. If only a single element is specified then a new root element is "
4179 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4180 "set to the final @key{TAB} delimited element. If no @var{content} is "
4181 "specified after the final @key{TAB}, then the content of the element will "
4182 "be removed, or empty when creating a new element."
4184 "The only restriction of an element name is that it not contain whitespace "
4185 "or begin with the literal element character @code{!} unless specifying a "
4186 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4187 "the @key{TAB} delimited elements. It is recommended that the content of an "
4188 "element be base64 encoded when it contains control or @key{TAB} characters "
4189 "to prevent @abbr{XML} and @command{pwmd} parsing errors."
4192 new_command("RENAME", 0, 1, rename_command
, _(
4193 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4194 "Renames the specified @var{element} to the new @var{value}. If an element of "
4195 "the same name as the @var{value} already exists it will be overwritten."
4197 "When the @option{--inquire} option is passed then all remaining non-option "
4198 "arguments are retrieved via a server @emph{INQUIRE}."
4201 new_command("COPY", 0, 1, copy_command
, _(
4202 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4203 "Copies the entire element tree starting from the child node of the source "
4204 "element, to the destination element path. If the destination element path "
4205 "does not exist then it will be created; otherwise it is overwritten."
4207 "Note that attributes from the source element are merged into the "
4208 "destination element when the destination element path exists. When an "
4209 "attribute of the same name exists in both the source and destination "
4210 "elements then the destination attribute will be updated to the source "
4213 "When the @option{--inquire} option is passed then all remaining non-option "
4214 "arguments are retrieved via a server @emph{INQUIRE}."
4217 new_command("MOVE", 0, 1, move_command
, _(
4218 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4219 "Moves the source element path to the destination element path. If the "
4220 "destination is not specified then it will be moved to the root node of the "
4221 "document. If the destination is specified and exists then it will be "
4222 "overwritten; otherwise it will be created."
4224 "When the @option{--inquire} option is passed then all remaining non-option "
4225 "arguments are retrieved via a server @emph{INQUIRE}."
4228 new_command("DELETE", 0, 1, delete_command
, _(
4229 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4230 "Removes the specified element path and all of its children. This may break "
4231 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4232 "refers to this element or any of its children."
4234 "When the @option{--inquire} option is passed then all remaining non-option "
4235 "arguments are retrieved via a server @emph{INQUIRE}."
4238 new_command("GET", 0, 1, get_command
, _(
4239 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4240 "Retrieves the content of the specified element. The content is returned "
4241 "with a data response."
4243 "When the @option{--inquire} option is passed then all remaining non-option "
4244 "arguments are retrieved via a server @emph{INQUIRE}."
4247 new_command("ATTR", 0, 1, attr_command
, _(
4248 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4250 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4252 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4253 "element. When no @var{value} is specified any existing value will be removed."
4255 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4257 " Removes an @var{attribute} from an element."
4259 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4261 " Retrieves a newline separated list of attributes names and values "
4262 "from the specified element. Each attribute name and value is space delimited."
4264 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4266 " Retrieves the value of an @var{attribute} from an element."
4269 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4270 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4273 "The @code{_mtime} attribute is updated each time an element is modified by "
4274 "either storing content, editing attributes or by deleting a child element. "
4275 "The @code{_ctime} attribute is created for each new element in an element "
4278 "When the @option{--inquire} option is passed then all remaining non-option "
4279 "arguments are retrieved via a server @emph{INQUIRE}."
4281 "@xref{Target Attribute}, for details about this special attribute."
4284 new_command("XPATH", 0, 1, xpath_command
, _(
4285 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4286 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4287 "specified, it is assumed the expression is a request to return a result. "
4288 "Otherwise, the result is set to the @var{value} argument and the document is "
4289 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4290 "is assumed to be empty and the document is updated. For example:"
4293 "XPATH //element[@@_name='password']@key{TAB}\n"
4296 "would clear the content of all @code{password} elements in the data file "
4297 "while leaving off the trailing @key{TAB} would return all @code{password} "
4298 "elements in @abbr{XML} format."
4300 "When the @option{--inquire} option is passed then all remaining non-option "
4301 "arguments are retrieved via a server @emph{INQUIRE}."
4303 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4304 "expression syntax."
4307 new_command("XPATHATTR", 0, 1, xpathattr_command
, _(
4308 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4309 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4310 "attributes and does not return a result. For the @var{SET} operation the "
4311 "@var{value} is optional but the field is required. If not specified then "
4312 "the attribute value will be empty. For example:"
4315 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4318 "would create an @code{password} attribute for each @code{password} element "
4319 "found in the document. The attribute value will be empty but still exist."
4321 "When the @option{--inquire} option is passed then all remaining non-option "
4322 "arguments are retrieved via a server @emph{INQUIRE}."
4324 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4325 "expression syntax."
4328 new_command("IMPORT", 0, 1, import_command
, _(
4329 "IMPORT <content>[<TAB>[!]element[<TAB>[!]child[..]]]\n"
4330 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4332 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4333 "argument is raw @abbr{XML} data. The content is created as a child of the "
4334 "specified element path and will overwrite an existing element of the same "
4335 "name. If an element of the element path does not exist then it will be "
4338 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4342 new_command("DUMP", 0, 1, dump_command
, _(
4344 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4345 "dumping a specific node."
4348 new_command("LOCK", 0, 0, lock_command
, _(
4350 "Locks the mutex associated with the opened file. This prevents other clients "
4351 "from sending commands to the same opened file until the client "
4352 "that sent this command either disconnects or sends the @code{UNLOCK} "
4353 "command. @xref{UNLOCK}."
4356 new_command("UNLOCK", 1, 0, unlock_command
, _(
4358 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4359 "a commands' @option{lock} option. @xref{LOCK}."
4362 new_command("GETCONFIG", 1, 1, getconfig_command
, _(
4363 "GETCONFIG [filename] <parameter>\n"
4364 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4365 "data response. If no file has been opened then the value for @var{filename} "
4366 "or the default from the @samp{global} section will be returned. If a file "
4367 "has been opened and no @var{filename} is specified, a value previously "
4368 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4371 new_command("OPTION", 1, 1, NULL
, _(
4372 "OPTION <NAME>=<VALUE>\n"
4373 "Sets a client option @var{name} to @var{value}. The value for an option is "
4374 "kept for the duration of the connection."
4378 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4381 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4384 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4387 " Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4390 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4392 "@item LC-MESSAGES\n"
4393 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4396 " Associates the thread ID of the connection with the specified textual "
4397 "representation. Useful for debugging log messages."
4399 "@item LOCK-TIMEOUT\n"
4400 " When not @code{0}, the duration in tenths of a second to wait for the file "
4401 "mutex which has been locked by another thread to be released before returning "
4402 "an error. When @code{-1}, then an error will be returned immediately."
4406 new_command("LS", 1, 1, ls_command
, _(
4408 "Lists the available data files stored in the data directory "
4409 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4412 new_command("RESET", 1, 1, NULL
, _(
4414 "Closes the currently opened file but keeps any previously set client options."
4417 new_command("NOP", 1, 1, NULL
, _(
4419 "Does nothing. Always returns successfully."
4422 /* !END-HELP-TEXT! */
4423 new_command ("CANCEL", 1, 1, NULL
, NULL
);
4424 new_command ("END", 1, 1, NULL
, NULL
);
4425 new_command ("BYE", 1, 1, NULL
, NULL
);
4428 for (i
= 0; command_table
[i
]; i
++);
4429 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
4434 register_commands (assuan_context_t ctx
)
4438 for (; command_table
[i
]; i
++)
4440 if (!command_table
[i
]->handler
)
4443 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
4444 command_table
[i
]->handler
,
4445 command_table
[i
]->help
);
4450 rc
= assuan_register_option_handler (ctx
, option_command
);
4454 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
4458 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
4462 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
4466 return assuan_register_post_cmd_notify (ctx
, command_finalize
);