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
);
2216 MUTEX_LOCK (&rcfile_mutex
);
2217 config_set_int_param (&global_config
, req
[0], "cache_timeout", req
[1]);
2218 MUTEX_UNLOCK (&rcfile_mutex
);
2222 return send_error (ctx
, rc
);
2226 dump_command (assuan_context_t ctx
, char *line
)
2230 struct client_s
*client
= assuan_get_pointer (ctx
);
2233 if (disable_list_and_dump
== 1)
2234 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2236 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2240 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2241 pwmd_strerror (GPG_ERR_ENOMEM
));
2242 return send_error (ctx
, GPG_ERR_ENOMEM
);
2245 pthread_cleanup_push (xmlFree
, xml
);
2246 rc
= xfer_data (ctx
, (char *) xml
, len
);
2247 pthread_cleanup_pop (1);
2248 return send_error (ctx
, rc
);
2252 getconfig_command (assuan_context_t ctx
, char *line
)
2254 struct client_s
*client
= assuan_get_pointer (ctx
);
2256 char filename
[255] = { 0 }, param
[747] =
2259 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2261 if (!line
|| !*line
)
2262 return send_error (ctx
, GPG_ERR_SYNTAX
);
2264 if (strchr (line
, ' '))
2266 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2271 if (fp
&& !valid_filename (fp
))
2272 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2274 paramp
= str_down (paramp
);
2275 if (!strcmp (paramp
, "cipher") && fp
)
2277 struct crypto_s
*crypto
;
2279 rc
= init_client_crypto (&crypto
);
2282 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2286 gcry_cipher_algo_name (cipher_to_gcrypt (crypto
->hdr
.flags
));
2296 cleanup_crypto (&crypto
);
2297 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2298 return send_error (ctx
, rc
);
2303 else if (!strcmp (paramp
, "cipher_iterations") && fp
)
2305 struct crypto_s
*crypto
;
2307 rc
= init_client_crypto (&crypto
);
2310 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2313 tmp
= str_asprintf ("%llu",
2314 (unsigned long long) crypto
->hdr
.
2317 rc
= GPG_ERR_ENOMEM
;
2321 cleanup_crypto (&crypto
);
2322 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2323 return send_error (ctx
, rc
);
2328 else if (!strcmp (paramp
, "passphrase"))
2329 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2331 p
= config_get_value (fp
? fp
: "global", paramp
);
2333 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2335 tmp
= expand_homedir (p
);
2339 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2340 pwmd_strerror (GPG_ERR_ENOMEM
));
2341 return send_error (ctx
, GPG_ERR_ENOMEM
);
2346 pthread_cleanup_push (xfree
, p
);
2347 rc
= xfer_data (ctx
, p
, strlen (p
));
2348 pthread_cleanup_pop (1);
2349 return send_error (ctx
, rc
);
2354 xmlXPathContextPtr xp
;
2355 xmlXPathObjectPtr result
;
2361 xpath_command_cleanup (void *arg
)
2363 struct xpath_s
*xpath
= arg
;
2368 req_cleanup (xpath
->req
);
2371 xmlBufferFree (xpath
->buf
);
2374 xmlXPathFreeObject (xpath
->result
);
2377 xmlXPathFreeContext (xpath
->xp
);
2381 do_xpath (assuan_context_t ctx
, char *line
)
2384 struct client_s
*client
= assuan_get_pointer (ctx
);
2385 struct xpath_s _x
= { 0 };
2386 struct xpath_s
*xpath
= &_x
;
2388 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2390 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2391 return GPG_ERR_ENOMEM
;
2394 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2397 rc
= GPG_ERR_BAD_DATA
;
2402 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2405 rc
= GPG_ERR_BAD_DATA
;
2409 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2411 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2415 rc
= recurse_xpath_nodeset (client
->doc
, xpath
->result
->nodesetval
,
2416 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2420 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2422 rc
= GPG_ERR_NO_DATA
;
2425 else if (xpath
->req
[1])
2431 pthread_cleanup_push (xpath_command_cleanup
, &xpath
);
2432 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2433 xmlBufferLength (xpath
->buf
));
2434 pthread_cleanup_pop (0);
2436 xpath_command_cleanup (xpath
);
2441 xpath_command (assuan_context_t ctx
, char *line
)
2443 struct client_s
*client
= assuan_get_pointer (ctx
);
2445 struct argv_s
*args
[] = {
2446 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2450 if (disable_list_and_dump
== 1)
2451 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2453 rc
= parse_options (&line
, args
, client
);
2455 return send_error (ctx
, rc
);
2457 if (client
->opts
& OPT_INQUIRE
)
2459 unsigned char *result
;
2462 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2464 return send_error (ctx
, rc
);
2466 line
= (char *) result
;
2469 if (!line
|| !*line
)
2470 rc
= GPG_ERR_SYNTAX
;
2473 rc
= do_xpath (ctx
, line
);
2475 if (client
->opts
& OPT_INQUIRE
)
2478 return send_error (ctx
, rc
);
2482 do_xpathattr (assuan_context_t ctx
, char *line
)
2484 struct client_s
*client
= assuan_get_pointer (ctx
);
2488 struct xpath_s _x
= { 0 };
2489 struct xpath_s
*xpath
= &_x
;
2491 if ((req
= str_split (line
, " ", 3)) == NULL
)
2492 return GPG_ERR_ENOMEM
;
2496 rc
= GPG_ERR_SYNTAX
;
2500 if (!strcasecmp (req
[0], "SET"))
2502 else if (!strcasecmp (req
[0], "DELETE"))
2506 rc
= GPG_ERR_SYNTAX
;
2510 if (!req
[1] || !req
[2])
2512 rc
= GPG_ERR_SYNTAX
;
2516 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2518 rc
= GPG_ERR_ENOMEM
;
2522 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2524 rc
= GPG_ERR_SYNTAX
;
2528 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2531 rc
= GPG_ERR_BAD_DATA
;
2536 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2539 rc
= GPG_ERR_BAD_DATA
;
2543 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2545 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2549 rc
= recurse_xpath_nodeset (client
->doc
, xpath
->result
->nodesetval
,
2550 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2551 (xmlChar
*) req
[1]);
2554 xpath_command_cleanup (xpath
);
2559 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2561 xpathattr_command (assuan_context_t ctx
, char *line
)
2563 struct client_s
*client
= assuan_get_pointer (ctx
);
2565 struct argv_s
*args
[] = {
2566 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2570 if (disable_list_and_dump
== 1)
2571 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2573 rc
= parse_options (&line
, args
, client
);
2575 return send_error (ctx
, rc
);
2577 if (client
->opts
& OPT_INQUIRE
)
2579 unsigned char *result
;
2582 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2584 return send_error (ctx
, rc
);
2586 line
= (char *) result
;
2589 if (!line
|| !*line
)
2590 rc
= GPG_ERR_SYNTAX
;
2593 rc
= do_xpathattr (ctx
, line
);
2595 if (client
->opts
& OPT_INQUIRE
)
2598 return send_error (ctx
, rc
);
2602 do_import (struct client_s
*client
, unsigned char *line
)
2604 char **req
, **path
= NULL
, **path_orig
= NULL
, *content
;
2605 xmlDocPtr doc
= NULL
;
2606 xmlNodePtr n
, root
, copy
;
2609 req
= str_split ((char *) line
, "\t", 2);
2612 return GPG_ERR_SYNTAX
;
2615 path
= str_split (req
[1], "\t", 0);
2616 if (!content
|| !*content
)
2618 rc
= GPG_ERR_SYNTAX
;
2622 if (path
&& !valid_element_path (path
, 0))
2624 rc
= GPG_ERR_INV_VALUE
;
2628 doc
= xmlReadDoc ((xmlChar
*) content
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2631 rc
= GPG_ERR_BAD_DATA
;
2635 root
= xmlDocGetRootElement (doc
);
2636 rc
= validate_import (root
);
2642 path_orig
= strv_dup (path
);
2645 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2646 pwmd_strerror (GPG_ERR_ENOMEM
));
2647 rc
= GPG_ERR_ENOMEM
;
2651 xmlChar
*a
= xmlGetProp (root
, (xmlChar
*) "_name");
2654 strv_free (path_orig
);
2655 rc
= GPG_ERR_ENOMEM
;
2659 if (strv_printf (&path
, "%s", (char *) a
) == 0)
2662 strv_free (path_orig
);
2663 rc
= GPG_ERR_ENOMEM
;
2668 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
2670 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2672 strv_free (path_orig
);
2679 find_elements (client
->doc
, n
->children
, path
+ 1, &rc
, NULL
,
2680 NULL
, NULL
, 0, 0, NULL
, 1);
2682 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2684 strv_free (path_orig
);
2689 xmlNodePtr parent
= n
->parent
;
2700 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
2702 n
= create_element_path (client
, &path
, &rc
, NULL
);
2708 copy
= xmlCopyNodeList (root
);
2709 n
= xmlAddChildList (n
, copy
);
2711 rc
= GPG_ERR_BAD_DATA
;
2715 /* Check if the content root element can create a DTD root element. */
2716 if (!xmlStrEqual ((xmlChar
*) "element", root
->name
))
2718 rc
= GPG_ERR_SYNTAX
;
2724 if ((a
= xmlGetProp (root
, (xmlChar
*) "_name")) == NULL
)
2726 rc
= GPG_ERR_SYNTAX
;
2730 char *tmp
= str_dup ((char *) a
);
2732 int literal
= is_literal_element (&tmp
);
2734 if (!valid_xml_element ((xmlChar
*) tmp
) || literal
)
2737 rc
= GPG_ERR_INV_VALUE
;
2741 if (strv_printf (&path
, "%s", tmp
) == 0)
2744 rc
= GPG_ERR_ENOMEM
;
2749 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 1);
2751 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2753 rc
= GPG_ERR_BAD_DATA
;
2757 /* Overwriting the existing tree. */
2761 xmlFreeNodeList (n
);
2765 xmlSetProp (root
, (xmlChar
*) "_name", (xmlChar
*) path
[0]);
2766 n
= xmlCopyNode (root
, 1);
2767 n
= xmlAddChildList (xmlDocGetRootElement (client
->doc
), n
);
2771 rc
= update_element_mtime (n
->parent
);
2785 import_command (assuan_context_t ctx
, char *line
)
2788 struct client_s
*client
= assuan_get_pointer (ctx
);
2789 unsigned char *result
;
2792 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2794 return send_error (ctx
, rc
);
2796 rc
= do_import (client
, result
);
2797 return send_error (ctx
, rc
);
2801 do_lock (struct client_s
*client
, int add
)
2803 gpg_error_t rc
= lock_file_mutex (client
, add
);
2806 client
->flags
|= FLAG_LOCK_CMD
;
2812 lock_command (assuan_context_t ctx
, char *line
)
2814 struct client_s
*client
= assuan_get_pointer (ctx
);
2815 gpg_error_t rc
= do_lock (client
, 0);
2817 return send_error (ctx
, rc
);
2821 unlock_command (assuan_context_t ctx
, char *line
)
2823 struct client_s
*client
= assuan_get_pointer (ctx
);
2826 rc
= unlock_file_mutex (client
, 0);
2827 return send_error (ctx
, rc
);
2831 option_command (assuan_context_t ctx
, const char *name
, const char *value
)
2833 struct client_s
*client
= assuan_get_pointer (ctx
);
2834 struct agent_s
*agent
= client
->crypto
->agent
;
2837 log_write1 ("OPTION name='%s' value='%s'", name
, value
);
2839 if (strcasecmp (name
, (char *) "log_level") == 0)
2845 l
= strtol (value
, NULL
, 10);
2848 return gpg_error (GPG_ERR_INV_VALUE
);
2851 MUTEX_LOCK (&rcfile_mutex
);
2852 config_set_int_param (&global_config
, "global", "log_level", value
);
2853 MUTEX_UNLOCK (&rcfile_mutex
);
2856 else if (strcasecmp (name
, (char *) "lock-timeout") == 0)
2863 n
= strtol (value
, &p
, 10);
2865 return gpg_error (GPG_ERR_INV_VALUE
);
2868 client
->lock_timeout
= n
;
2871 else if (strcasecmp (name
, (char *) "NAME") == 0)
2873 char *tmp
= pthread_getspecific (thread_name_key
);
2878 if (!value
|| !*value
)
2880 pthread_setspecific (thread_name_key
, str_dup (""));
2884 pthread_setspecific (thread_name_key
, str_dup (value
));
2887 else if (strcasecmp (name
, (char *) "lc-messages") == 0)
2889 rc
= set_agent_option (client
->crypto
->agent
, "lc-messages", value
);
2892 xfree (agent
->lc_messages
);
2893 agent
->lc_messages
= str_dup (value
);
2896 else if (strcasecmp (name
, (char *) "lc-ctype") == 0)
2898 rc
= set_agent_option (client
->crypto
->agent
, "lc-ctype", value
);
2901 xfree (agent
->lc_ctype
);
2902 agent
->lc_ctype
= str_dup (value
);
2905 else if (strcasecmp (name
, (char *) "ttyname") == 0)
2907 rc
= set_agent_option (client
->crypto
->agent
, "ttyname", value
);
2910 xfree (agent
->ttyname
);
2911 agent
->ttyname
= str_dup (value
);
2914 else if (strcasecmp (name
, (char *) "ttytype") == 0)
2916 rc
= set_agent_option (client
->crypto
->agent
, "ttytype", value
);
2919 xfree (agent
->ttytype
);
2920 agent
->ttytype
= str_dup (value
);
2923 else if (strcasecmp (name
, (char *) "display") == 0)
2925 rc
= set_agent_option (client
->crypto
->agent
, "display", value
);
2928 xfree (agent
->display
);
2929 agent
->display
= str_dup (value
);
2932 else if (strcasecmp (name
, (char *) "desc") == 0)
2934 if (client
->crypto
->agent
->desc
)
2935 xfree (client
->crypto
->agent
->desc
);
2937 client
->crypto
->agent
->desc
= str_dup (value
);
2938 if (!client
->crypto
->agent
->desc
)
2939 rc
= GPG_ERR_ENOMEM
;
2942 else if (strcasecmp (name
, "pinentry_timeout") == 0)
2950 n
= (int) strtol (value
, &p
, 10);
2953 return gpg_error (GPG_ERR_INV_VALUE
);
2955 MUTEX_LOCK (&rcfile_mutex
);
2956 config_set_int_param (global_config
,
2957 client
->filename
? client
->filename
: "global",
2958 "pinentry_timeout", value
);
2959 MUTEX_UNLOCK (&rcfile_mutex
);
2964 return gpg_error (GPG_ERR_UNKNOWN_OPTION
);
2971 do_rename (assuan_context_t ctx
, char *line
)
2973 struct client_s
*client
= assuan_get_pointer (ctx
);
2975 char **req
, **src
, *dst
;
2978 req
= str_split (line
, " ", 0);
2980 if (!req
|| !req
[0] || !req
[1])
2983 return GPG_ERR_SYNTAX
;
2987 is_literal_element (&dst
);
2989 if (!valid_xml_element ((xmlChar
*) dst
))
2992 return GPG_ERR_INV_VALUE
;
2995 if (strchr (req
[0], '\t'))
2996 src
= str_split (req
[0], "\t", 0);
2998 src
= str_split (req
[0], " ", 0);
3002 rc
= GPG_ERR_SYNTAX
;
3006 n
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3008 n
= find_elements (client
->doc
, n
->children
, src
+ 1, &rc
, NULL
, NULL
,
3009 NULL
, 0, 0, NULL
, 0);
3014 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
3017 rc
= GPG_ERR_ENOMEM
;
3021 /* To prevent unwanted effects:
3023 * <root name="a"><b/></root>
3027 if (xmlStrEqual (a
, (xmlChar
*) dst
))
3030 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3040 for (p
= src
; *p
; p
++)
3044 strv_printf (&tmp
, "%s", *p
);
3048 strv_printf (&tmp
, "!%s", dst
);
3049 ndst
= find_root_element (client
->doc
, &tmp
, &rc
, NULL
, 0, 0);
3050 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3057 ndst
= find_elements (client
->doc
, ndst
->children
, tmp
+ 1, &rc
, NULL
,
3058 NULL
, NULL
, 0, 0, NULL
, 0);
3061 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3066 /* Target may exist:
3069 * <root name="b" target="a"/>
3078 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3085 xmlFreeNodeList (ndst
);
3088 rc
= add_attribute (n
, "_name", dst
);
3097 rename_command (assuan_context_t ctx
, char *line
)
3099 struct client_s
*client
= assuan_get_pointer (ctx
);
3101 struct argv_s
*args
[] = {
3102 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3106 rc
= parse_options (&line
, args
, client
);
3108 return send_error (ctx
, rc
);
3110 if (client
->opts
& OPT_INQUIRE
)
3112 unsigned char *result
;
3115 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3117 return send_error (ctx
, rc
);
3119 line
= (char *) result
;
3122 rc
= do_rename (ctx
, line
);
3124 if (client
->opts
& OPT_INQUIRE
)
3127 return send_error (ctx
, rc
);
3131 do_copy (assuan_context_t ctx
, char *line
)
3133 struct client_s
*client
= assuan_get_pointer (ctx
);
3135 char **req
, **src
= NULL
, **dst
= NULL
;
3136 xmlNodePtr nsrc
, ndst
, new = NULL
;
3138 req
= str_split (line
, " ", 0);
3139 if (!req
|| !req
[0] || !req
[1])
3142 return GPG_ERR_SYNTAX
;
3145 if (strchr (req
[0], '\t'))
3146 src
= str_split (req
[0], "\t", 0);
3148 src
= str_split (req
[0], " ", 0);
3152 rc
= GPG_ERR_SYNTAX
;
3156 if (strchr (req
[1], '\t'))
3157 dst
= str_split (req
[1], "\t", 0);
3159 dst
= str_split (req
[1], " ", 0);
3163 rc
= GPG_ERR_SYNTAX
;
3167 if (!valid_element_path (dst
, 0))
3169 rc
= GPG_ERR_INV_VALUE
;
3173 nsrc
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3175 nsrc
= find_elements (client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3176 NULL
, NULL
, 0, 0, NULL
, 0);
3181 new = xmlCopyNodeList (nsrc
);
3184 rc
= GPG_ERR_ENOMEM
;
3189 ndst
= find_root_element (client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3193 ndst
= find_elements (client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3194 NULL
, create_target_elements_cb
, 0, 0, NULL
, 0);
3201 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3205 ndst
= create_element_path (client
, &dst
, &rc
, NULL
);
3210 /* Merge any attributes from the src node to the initial dst node. */
3211 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
)
3213 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3216 xmlAttrPtr a
= xmlHasProp (ndst
, attr
->name
);
3220 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3221 xmlNewProp (ndst
, attr
->name
, tmp
);
3223 rc
= add_attribute (ndst
, NULL
, NULL
);
3226 xmlNodePtr n
= ndst
->children
;
3228 xmlFreeNodeList (n
);
3229 ndst
->children
= NULL
;
3233 n
= xmlCopyNodeList (new->children
);
3236 rc
= GPG_ERR_ENOMEM
;
3240 n
= xmlAddChildList (ndst
, n
);
3243 rc
= GPG_ERR_ENOMEM
;
3247 rc
= update_element_mtime (xmlDocGetRootElement (client
->doc
) ==
3248 ndst
->parent
? ndst
: ndst
->parent
);
3254 xmlUnlinkNode (new);
3255 xmlFreeNodeList (new);
3271 copy_command (assuan_context_t ctx
, char *line
)
3273 struct client_s
*client
= assuan_get_pointer (ctx
);
3275 struct argv_s
*args
[] = {
3276 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3280 rc
= parse_options (&line
, args
, client
);
3282 return send_error (ctx
, rc
);
3284 if (client
->opts
& OPT_INQUIRE
)
3286 unsigned char *result
;
3289 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3291 return send_error (ctx
, rc
);
3293 line
= (char *) result
;
3296 rc
= do_copy (ctx
, line
);
3298 if (client
->opts
& OPT_INQUIRE
)
3301 return send_error (ctx
, rc
);
3305 do_move (assuan_context_t ctx
, char *line
)
3307 struct client_s
*client
= assuan_get_pointer (ctx
);
3309 char **req
, **src
= NULL
, **dst
= NULL
;
3310 xmlNodePtr nsrc
, ndst
= NULL
;
3312 req
= str_split (line
, " ", 0);
3314 if (!req
|| !req
[0] || !req
[1])
3317 return GPG_ERR_SYNTAX
;
3320 if (strchr (req
[0], '\t'))
3321 src
= str_split (req
[0], "\t", 0);
3323 src
= str_split (req
[0], " ", 0);
3327 rc
= GPG_ERR_SYNTAX
;
3331 if (strchr (req
[1], '\t'))
3332 dst
= str_split (req
[1], "\t", 0);
3334 dst
= str_split (req
[1], " ", 0);
3336 nsrc
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3338 nsrc
= find_elements (client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3339 NULL
, NULL
, 0, 0, NULL
, 0);
3346 if (!valid_element_path (dst
, 0))
3348 rc
= GPG_ERR_INV_VALUE
;
3352 ndst
= find_root_element (client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3354 ndst
= find_elements (client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3355 NULL
, NULL
, 0, 0, NULL
, 0);
3358 ndst
= xmlDocGetRootElement (client
->doc
);
3360 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
)
3364 rc
= GPG_ERR_CONFLICT
;
3369 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3376 xmlChar
*a
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3377 xmlNodePtr dup
= find_element (ndst
->children
, (char *) a
, NULL
);
3385 if (ndst
== xmlDocGetRootElement (client
->doc
))
3387 xmlNodePtr n
= nsrc
;
3390 while (n
->parent
&& n
->parent
!= ndst
)
3393 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
3394 xmlChar
*b
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3396 if (xmlStrEqual (a
, b
))
3399 xmlUnlinkNode (nsrc
);
3401 xmlFreeNodeList (n
);
3409 xmlUnlinkNode (dup
);
3410 xmlFreeNodeList (dup
);
3414 xmlUnlinkNode (dup
);
3420 xmlChar
*name
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3422 if (nsrc
->parent
== xmlDocGetRootElement (client
->doc
)
3423 && !strcmp ((char *) name
, *dst
))
3426 rc
= GPG_ERR_CONFLICT
;
3431 ndst
= create_element_path (client
, &dst
, &rc
, nsrc
);
3437 update_element_mtime (nsrc
->parent
);
3438 xmlUnlinkNode (nsrc
);
3439 ndst
= xmlAddChildList (ndst
, nsrc
);
3442 rc
= GPG_ERR_ENOMEM
;
3444 update_element_mtime (ndst
->parent
);
3460 move_command (assuan_context_t ctx
, char *line
)
3462 struct client_s
*client
= assuan_get_pointer (ctx
);
3464 struct argv_s
*args
[] = {
3465 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3469 rc
= parse_options (&line
, args
, client
);
3471 return send_error (ctx
, rc
);
3473 if (client
->opts
& OPT_INQUIRE
)
3475 unsigned char *result
;
3478 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3480 return send_error (ctx
, rc
);
3482 line
= (char *) result
;
3485 rc
= do_move (ctx
, line
);
3487 if (client
->opts
& OPT_INQUIRE
)
3490 return send_error (ctx
, rc
);
3494 ls_command (assuan_context_t ctx
, char *line
)
3497 char *tmp
= str_asprintf ("%s/data", homedir
);
3498 char *dir
= expand_homedir (tmp
);
3499 DIR *d
= opendir (dir
);
3501 rc
= gpg_error_from_syserror ();
3507 return send_error (ctx
, rc
);
3511 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
3512 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
3518 while (!readdir_r (d
, p
, &cur
) && cur
)
3520 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
3522 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.'
3523 && cur
->d_name
[2] == '\0')
3526 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
3533 rc
= GPG_ERR_ENOMEM
;
3545 return send_error (ctx
, rc
);
3548 return send_error (ctx
, GPG_ERR_NO_DATA
);
3550 list
[strlen (list
) - 1] = 0;
3551 rc
= xfer_data (ctx
, list
, strlen (list
));
3553 return send_error (ctx
, rc
);
3557 bye_notify (assuan_context_t ctx
, char *line
)
3559 struct client_s
*cl
= assuan_get_pointer (ctx
);
3562 if (cl
->thd
->remote
)
3568 struct timeval tv
= { 0, 50000 };
3570 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
3571 if (rc
== GNUTLS_E_AGAIN
)
3572 select (0, NULL
, NULL
, NULL
, &tv
);
3574 while (rc
== GNUTLS_E_AGAIN
);
3578 /* This will let assuan_process_next() return. */
3579 fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
);
3580 cl
->last_rc
= 0; // BYE command result
3585 reset_notify (assuan_context_t ctx
, char *line
)
3587 struct client_s
*client
= assuan_get_pointer (ctx
);
3590 cleanup_client (client
);
3596 * This is called before every Assuan command.
3599 command_startup (assuan_context_t ctx
, const char *name
)
3601 struct client_s
*client
= assuan_get_pointer (ctx
);
3603 struct command_table_s
*cmd
= NULL
;
3605 log_write1 ("command='%s'", name
);
3606 client
->last_rc
= client
->opts
= 0;
3608 for (int i
= 0; command_table
[i
]; i
++)
3610 if (!strcasecmp (name
, command_table
[i
]->name
))
3612 if (command_table
[i
]->ignore_startup
)
3614 cmd
= command_table
[i
];
3619 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
3624 * This is called after every Assuan command.
3627 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
3629 struct client_s
*client
= assuan_get_pointer (ctx
);
3631 if (!(client
->flags
& FLAG_LOCK_CMD
))
3632 unlock_file_mutex (client
, 0);
3634 log_write1 (_("command completed: rc=%u"), client
->last_rc
);
3635 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
3639 help_command (assuan_context_t ctx
, char *line
)
3644 if (!line
|| !*line
)
3647 char *help
= str_dup (_("Usage: HELP [<COMMAND>]\n"
3648 "For commands that take an element path as an argument, each element is "
3649 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3652 for (i
= 0; command_table
[i
]; i
++)
3654 if (!command_table
[i
]->help
)
3657 tmp
= str_asprintf ("%s %s", help
, command_table
[i
]->name
);
3662 tmp
= strip_texi_and_wrap (help
);
3664 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3666 return send_error (ctx
, rc
);
3669 for (i
= 0; command_table
[i
]; i
++)
3671 if (!strcasecmp (line
, command_table
[i
]->name
))
3675 if (!command_table
[i
]->help
)
3678 help
= strip_texi_and_wrap (command_table
[i
]->help
);
3679 tmp
= str_asprintf (_("Usage: %s"), help
);
3681 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3683 return send_error (ctx
, rc
);
3687 return send_error (ctx
, GPG_ERR_INV_NAME
);
3691 new_command (const char *name
, int ignore
, int unlock
,
3692 gpg_error_t (*handler
) (assuan_context_t
, char *),
3698 for (i
= 0; command_table
[i
]; i
++);
3701 xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
3702 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
3703 command_table
[i
]->name
= name
;
3704 command_table
[i
]->handler
= handler
;
3705 command_table
[i
]->ignore_startup
= ignore
;
3706 command_table
[i
]->unlock
= unlock
;
3707 command_table
[i
++]->help
= help
;
3708 command_table
[i
] = NULL
;
3716 for (i
= 0; command_table
[i
]; i
++)
3717 xfree (command_table
[i
]);
3719 xfree (command_table
);
3723 sort_commands (const void *arg1
, const void *arg2
)
3725 struct command_table_s
*const *a
= arg1
;
3726 struct command_table_s
*const *b
= arg2
;
3735 return strcmp ((*a
)->name
, (*b
)->name
);
3739 passwd_command (assuan_context_t ctx
, char *line
)
3741 struct client_s
*client
= assuan_get_pointer (ctx
);
3743 struct argv_s
*args
[] = {
3744 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
3745 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
3749 if (client
->flags
& FLAG_NEW
)
3750 return send_error (ctx
, GPG_ERR_INV_STATE
);
3752 client
->crypto
->save
.s2k_count
=
3753 config_get_ulong (client
->filename
, "s2k_count");
3754 rc
= parse_options (&line
, args
, client
);
3756 return send_error (ctx
, rc
);
3758 if (!rc
&& client
->opts
& OPT_RESET
)
3760 rc
= cache_clear (client
->md5file
);
3762 send_status_all (STATUS_CACHE
, NULL
);
3767 if (client
->crypto
->save
.s2k_count
)
3768 rc
= send_to_agent (client
->crypto
->agent
, NULL
, NULL
,
3769 "OPTION s2k-count=%lu",
3770 client
->crypto
->save
.s2k_count
);
3773 rc
= agent_passwd (client
->crypto
);
3776 return send_error (ctx
, rc
);
3780 parse_keygrip_opt_sign (void *data
, void *value
)
3782 struct client_s
*client
= data
;
3785 client
->opts
|= OPT_SIGN
;
3790 keygrip_command (assuan_context_t ctx
, char *line
)
3792 struct client_s
*client
= assuan_get_pointer (ctx
);
3794 struct crypto_s
*crypto
;
3795 struct argv_s
*args
[] = {
3796 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_keygrip_opt_sign
},
3800 if (!line
|| !*line
)
3801 return send_error (ctx
, GPG_ERR_SYNTAX
);
3803 rc
= parse_options (&line
, args
, client
);
3805 return send_error (ctx
, rc
);
3807 if (!valid_filename (line
))
3808 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3810 rc
= init_client_crypto (&crypto
);
3812 return send_error (ctx
, rc
);
3814 rc
= read_data_file (line
, crypto
);
3817 char *hexgrip
= NULL
;
3819 if (client
->opts
& OPT_SIGN
)
3821 if (valid_keygrip (crypto
->sign_grip
, sizeof (crypto
->sign_grip
)))
3822 hexgrip
= bin2hex (crypto
->sign_grip
, sizeof (crypto
->sign_grip
));
3826 hexgrip
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
3829 rc
= GPG_ERR_ENOMEM
;
3831 rc
= xfer_data (ctx
, hexgrip
, strlen (hexgrip
));
3836 cleanup_crypto (&crypto
);
3837 return send_error (ctx
, rc
);
3841 parse_opt_data (void *data
, void *value
)
3843 struct client_s
*client
= data
;
3846 client
->opts
|= OPT_DATA
;
3851 getinfo_command (assuan_context_t ctx
, char *line
)
3853 struct client_s
*client
= assuan_get_pointer (ctx
);
3855 char buf
[ASSUAN_LINELENGTH
];
3856 struct argv_s
*args
[] = {
3857 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
3861 rc
= parse_options (&line
, args
, client
);
3863 return send_error (ctx
, rc
);
3865 if (!strcasecmp (line
, "clients"))
3867 if (client
->opts
& OPT_DATA
)
3869 MUTEX_LOCK (&cn_mutex
);
3870 snprintf (buf
, sizeof (buf
), "%i", slist_length (cn_thread_list
));
3871 MUTEX_UNLOCK (&cn_mutex
);
3872 rc
= xfer_data (ctx
, buf
, strlen (buf
));
3875 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
3877 else if (!strcasecmp (line
, "cache"))
3879 if (client
->opts
& OPT_DATA
)
3881 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
3882 rc
= xfer_data (ctx
, buf
, strlen (buf
));
3885 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
3887 else if (!strcasecmp (line
, "pid"))
3890 pid_t pid
= getpid ();
3892 snprintf (buf
, sizeof (buf
), "%i", pid
);
3893 rc
= xfer_data (ctx
, buf
, strlen (buf
));
3895 else if (!strcasecmp (line
, "version"))
3897 char *buf
= str_asprintf ("0x%06x %s", VERSION_HEX
,
3905 rc
= xfer_data (ctx
, buf
, strlen (buf
));
3908 else if (!strcasecmp (line
, "last_error"))
3910 if (client
->last_error
)
3911 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
3913 rc
= GPG_ERR_NO_DATA
;
3916 rc
= gpg_error (GPG_ERR_SYNTAX
);
3918 return send_error (ctx
, rc
);
3922 send_data_cb (void *user
, const void *buf
, size_t len
)
3924 assuan_context_t ctx
= user
;
3926 return assuan_send_data (ctx
, buf
, len
);
3930 send_status_cb (void *user
, const char *line
)
3932 assuan_context_t ctx
= user
;
3933 char keyword
[200], *k
;
3936 for (p
= line
, k
= keyword
; *p
; p
++)
3948 while (isspace (*p
))
3951 return assuan_write_status (ctx
, keyword
, *p
? p
: NULL
);
3955 agent_command (assuan_context_t ctx
, char *line
)
3958 struct client_s
*client
= assuan_get_pointer (ctx
);
3960 if (!line
|| !*line
)
3961 return send_error (ctx
, GPG_ERR_SYNTAX
);
3963 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 1);
3964 rc
= assuan_transact (client
->crypto
->agent
->ctx
, line
, send_data_cb
,
3965 client
->ctx
, agent_loopback_cb
, client
->crypto
,
3966 send_status_cb
, client
->ctx
);
3967 if (gpg_err_code (rc
) == GPG_ERR_ASS_CANCELED
)
3972 rc
= assuan_write_line (client
->crypto
->agent
->ctx
, "CAN");
3975 rc
= assuan_read_line (client
->crypto
->agent
->ctx
, &line
, &len
);
3977 rc
= gpg_error (GPG_ERR_ASS_CANCELED
);
3981 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 0);
3982 return send_error (ctx
, rc
);
3988 /* !BEGIN-HELP-TEXT!
3990 * This comment is used as a marker to generate the offline documentation
3991 * found in doc/pwmd.info. The indentation needs to be kept for the awk
3992 * script to determine where commands begin and end.
3994 new_command("HELP", 1, 1, help_command
, _(
3995 "HELP [<COMMAND>]\n"
3996 "Show available commands or command specific help text."
3999 new_command("AGENT", 1, 1, agent_command
, _(
4001 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4002 "@command{gpg-agent}."
4005 new_command("GETINFO", 1, 1, getinfo_command
, _(
4006 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4007 "Get server and other information: @var{cache} returns the number of cached "
4008 "documents via a status message. @var{clients} returns the number of "
4009 "connected clients via a status message. @var{pid} returns the process ID "
4010 "number of the server via a data response. @var{VERSION} returns the server "
4011 "version number and compile-time features with a data response with each "
4012 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4013 "the last failed command when available. @xref{Status Messages}. "
4015 "When the @option{--data} option is specified then the result will be send "
4016 "via a data response rather than a status message."
4019 new_command("PASSWD", 0, 0, passwd_command
, _(
4020 "PASSWD [--reset] [--s2k-count=N]\n"
4021 "Changes the passphrase of the secret key required to open the current "
4022 "file. When the @option{--reset} option is passed then the cache entry for "
4023 "the current file will be reset and the passphrase, if any, will be required "
4024 "during the next @code{OPEN}. @xref{OPEN}."
4026 "The @option{--s2k-count} option sets number of hash iterations for a "
4027 "passphrase and must be either @code{0} to use the calibrated count of the "
4028 "machine (the default), or a value greater than or equal to @code{65536}. "
4032 new_command("KEYGRIP", 1, 1, keygrip_command
, _(
4033 "KEYGRIP [--sign] <filename>\n"
4034 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4037 "When the @option{--sign} option is specified then the key used for signing "
4038 "of the specified @var{filename} will be returned."
4041 new_command("OPEN", 1, 1, open_command
, _(
4042 "OPEN [--lock] [--no-pinentry] <filename> [<passphrase>]\n"
4043 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4044 "found on the file-system then a new document will be created. If the file "
4045 "is found, it is looked for in the file cache. If cached and no "
4046 "@var{passphrase} was specified then the cached document is opened. When not "
4047 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4048 "for decryption unless @option{--no-pinentry} was specified (see below)."
4050 "When the @option{--lock} option is passed then the file mutex will be "
4051 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4052 "file has been opened."
4054 "By default, a pinentry is used to retrieve a passphrase when required. "
4055 "Passing @option{--no-pinentry} will disable pinentry use for the rest of "
4056 "the session. When pinentry use is disabled but required for some operation "
4057 "then a server @emph{INQUIRE} will be send to the client to retrieve the "
4058 "passphrase. See the @code{OPTION} command (@pxref{OPTION}), for pinentry "
4062 new_command("SAVE", 0, 0, save_command
, _(
4063 "SAVE [--no-passphrase] [--reset] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring [--sign-keygrip=hexstring]]\n"
4064 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4065 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4066 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4067 "keypair will be generated and a pinentry will be used to prompt for the "
4068 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4069 "passed, in which case the data file will not be passphrase protected."
4071 "The @option{--reset} option will clear the cache entry for the current file "
4074 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4075 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4076 "(@pxref{Configuration}) for available ciphers."
4078 "The @option{--cipher-iterations} option specifies the number of times to "
4079 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4081 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4082 "the client to obtain the key paramaters to use when generating a new "
4083 "keypair. The inquired data is expected to be an S-expression. If not "
4084 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4085 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4086 "that when this option is specified a new keypair will be generated "
4087 "reguardless if the file is a new one or not."
4089 "You can encrypt the data file to a public key other than the one that it "
4090 "was originally encrypted with by passing the @option{--keygrip} option with "
4091 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4092 "be of any key that @command{gpg-agent} knows about. The "
4093 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4094 "secret key. This option may be needed when using a smartcard."
4096 "The @option{--s2k-count} option sets number of hash iterations for a "
4097 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4098 "value which is the default. This setting only affects new files. To change "
4099 "the setting, use the @code{PASSWD} command (@pxref{PASSWD})."
4102 new_command("ISCACHED", 1, 0, iscached_command
, _(
4103 "ISCACHED [--lock] <filename>\n"
4104 "An @emph{OK} response is returned if the specified @var{filename} is found "
4105 "in the file cache. If not found in the cache but exists on the filesystem "
4106 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4109 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4110 "file exists; it does not need to be opened nor cached."
4113 new_command("CLEARCACHE", 1, 1, clearcache_command
, _(
4114 "CLEARCACHE [<filename>]\n"
4115 "Clears a file cache entry for all or the specified @var{filename}."
4118 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command
, _(
4119 "CACHETIMEOUT <filename> <seconds>\n"
4120 "The time in @var{seconds} until @var{filename} will be removed from the "
4121 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4122 "the passphrase for each @code{OPEN} command (@pxref{OPEN}). "
4123 "@xref{Configuration}, and the @code{cache_timeout} parameter."
4126 new_command("LIST", 0, 1, list_command
, _(
4127 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4128 "If no element path is given then a newline separated list of root elements "
4129 "is returned with a data response. If given, then all reachable elements "
4130 "of the specified element path are returned unless the @option{--no-recurse} "
4131 "option is specified. If specified, only the child elements of the element "
4132 "path are returned without recursing into grandchildren. Each resulting "
4133 "element is prefixed with the literal @code{!} character when the element "
4134 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4136 "When the @option{--verbose} option is passed then each element path "
4137 "returned will have zero or more flags appened to it. These flags are "
4138 "delimited from the element path by a single space character. A flag itself "
4139 "is a single character. Flag @code{+} indicates that there are child nodes of "
4140 "the current element path. Flag @code{E} indicates that an element of an "
4141 "element path contained in a @var{target} attribute could not be found. Flag "
4142 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4143 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4144 "of a @var{target} attribute (see below)."
4146 "The @option{--with-target} option implies @option{--verbose} and will append "
4147 "an additional flag @code{T} followed by a single space then an element path. "
4148 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4149 "current element when it contains a @var{target} attribute. When no "
4150 "@var{target} attribute is found then no flag will be appended."
4152 "The @option{--no-recurse} option limits the amount of data returned to only "
4153 "the listing of children of the specified element path and not any "
4156 "The @option{--all} option lists the entire element tree for each root "
4157 "element. This option also implies option @option{--verbose}."
4159 "When the @option{--inquire} option is passed then all remaining non-option "
4160 "arguments are retrieved via a server @emph{INQUIRE}."
4163 new_command("REALPATH", 0, 1, realpath_command
, _(
4164 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4165 "Resolves all @code{target} attributes of the specified element path and "
4166 "returns the result with a data response. @xref{Target Attribute}, for details."
4168 "When the @option{--inquire} option is passed then all remaining non-option "
4169 "arguments are retrieved via a server @emph{INQUIRE}."
4172 new_command("STORE", 0, 1, store_command
, _(
4173 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4174 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4176 "Creates a new element path or modifies the @var{content} of an existing "
4177 "element. If only a single element is specified then a new root element is "
4178 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4179 "set to the final @key{TAB} delimited element. If no @var{content} is "
4180 "specified after the final @key{TAB}, then the content of the element will "
4181 "be removed, or empty when creating a new element."
4183 "The only restriction of an element name is that it not contain whitespace "
4184 "or begin with the literal element character @code{!} unless specifying a "
4185 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4186 "the @key{TAB} delimited elements. It is recommended that the content of an "
4187 "element be base64 encoded when it contains control or @key{TAB} characters "
4188 "to prevent @abbr{XML} and @command{pwmd} parsing errors."
4191 new_command("RENAME", 0, 1, rename_command
, _(
4192 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4193 "Renames the specified @var{element} to the new @var{value}. If an element of "
4194 "the same name as the @var{value} already exists it will be overwritten."
4196 "When the @option{--inquire} option is passed then all remaining non-option "
4197 "arguments are retrieved via a server @emph{INQUIRE}."
4200 new_command("COPY", 0, 1, copy_command
, _(
4201 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4202 "Copies the entire element tree starting from the child node of the source "
4203 "element, to the destination element path. If the destination element path "
4204 "does not exist then it will be created; otherwise it is overwritten."
4206 "Note that attributes from the source element are merged into the "
4207 "destination element when the destination element path exists. When an "
4208 "attribute of the same name exists in both the source and destination "
4209 "elements then the destination attribute will be updated to the source "
4212 "When the @option{--inquire} option is passed then all remaining non-option "
4213 "arguments are retrieved via a server @emph{INQUIRE}."
4216 new_command("MOVE", 0, 1, move_command
, _(
4217 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4218 "Moves the source element path to the destination element path. If the "
4219 "destination is not specified then it will be moved to the root node of the "
4220 "document. If the destination is specified and exists then it will be "
4221 "overwritten; otherwise it will be created."
4223 "When the @option{--inquire} option is passed then all remaining non-option "
4224 "arguments are retrieved via a server @emph{INQUIRE}."
4227 new_command("DELETE", 0, 1, delete_command
, _(
4228 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4229 "Removes the specified element path and all of its children. This may break "
4230 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4231 "refers to this element or any of its children."
4233 "When the @option{--inquire} option is passed then all remaining non-option "
4234 "arguments are retrieved via a server @emph{INQUIRE}."
4237 new_command("GET", 0, 1, get_command
, _(
4238 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4239 "Retrieves the content of the specified element. The content is returned "
4240 "with a data response."
4242 "When the @option{--inquire} option is passed then all remaining non-option "
4243 "arguments are retrieved via a server @emph{INQUIRE}."
4246 new_command("ATTR", 0, 1, attr_command
, _(
4247 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4249 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4251 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4252 "element. When no @var{value} is specified any existing value will be removed."
4254 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4256 " Removes an @var{attribute} from an element."
4258 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4260 " Retrieves a newline separated list of attributes names and values "
4261 "from the specified element. Each attribute name and value is space delimited."
4263 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4265 " Retrieves the value of an @var{attribute} from an element."
4268 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4269 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4272 "The @code{_mtime} attribute is updated each time an element is modified by "
4273 "either storing content, editing attributes or by deleting a child element. "
4274 "The @code{_ctime} attribute is created for each new element in an element "
4277 "When the @option{--inquire} option is passed then all remaining non-option "
4278 "arguments are retrieved via a server @emph{INQUIRE}."
4280 "@xref{Target Attribute}, for details about this special attribute."
4283 new_command("XPATH", 0, 1, xpath_command
, _(
4284 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4285 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4286 "specified, it is assumed the expression is a request to return a result. "
4287 "Otherwise, the result is set to the @var{value} argument and the document is "
4288 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4289 "is assumed to be empty and the document is updated. For example:"
4292 "XPATH //element[@@_name='password']@key{TAB}\n"
4295 "would clear the content of all @code{password} elements in the data file "
4296 "while leaving off the trailing @key{TAB} would return all @code{password} "
4297 "elements in @abbr{XML} format."
4299 "When the @option{--inquire} option is passed then all remaining non-option "
4300 "arguments are retrieved via a server @emph{INQUIRE}."
4302 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4303 "expression syntax."
4306 new_command("XPATHATTR", 0, 1, xpathattr_command
, _(
4307 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4308 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4309 "attributes and does not return a result. For the @var{SET} operation the "
4310 "@var{value} is optional but the field is required. If not specified then "
4311 "the attribute value will be empty. For example:"
4314 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4317 "would create an @code{password} attribute for each @code{password} element "
4318 "found in the document. The attribute value will be empty but still exist."
4320 "When the @option{--inquire} option is passed then all remaining non-option "
4321 "arguments are retrieved via a server @emph{INQUIRE}."
4323 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4324 "expression syntax."
4327 new_command("IMPORT", 0, 1, import_command
, _(
4328 "IMPORT <content>[<TAB>[!]element[<TAB>[!]child[..]]]\n"
4329 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4331 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4332 "argument is raw @abbr{XML} data. The content is created as a child of the "
4333 "specified element path and will overwrite an existing element of the same "
4334 "name. If an element of the element path does not exist then it will be "
4337 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4341 new_command("DUMP", 0, 1, dump_command
, _(
4343 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4344 "dumping a specific node."
4347 new_command("LOCK", 0, 0, lock_command
, _(
4349 "Locks the mutex associated with the opened file. This prevents other clients "
4350 "from sending commands to the same opened file until the client "
4351 "that sent this command either disconnects or sends the @code{UNLOCK} "
4352 "command. @xref{UNLOCK}."
4355 new_command("UNLOCK", 1, 0, unlock_command
, _(
4357 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4358 "a commands' @option{lock} option. @xref{LOCK}."
4361 new_command("GETCONFIG", 1, 1, getconfig_command
, _(
4362 "GETCONFIG [filename] <parameter>\n"
4363 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4364 "data response. If no file has been opened then the value for @var{filename} "
4365 "or the default from the @samp{global} section will be returned. If a file "
4366 "has been opened and no @var{filename} is specified, a value previously "
4367 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4370 new_command("OPTION", 1, 1, NULL
, _(
4371 "OPTION <NAME>=<VALUE>\n"
4372 "Sets a client option @var{name} to @var{value}. The value for an option is "
4373 "kept for the duration of the connection."
4377 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4380 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4383 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4386 " Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4389 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4391 "@item LC-MESSAGES\n"
4392 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4395 " Associates the thread ID of the connection with the specified textual "
4396 "representation. Useful for debugging log messages."
4398 "@item LOCK-TIMEOUT\n"
4399 " When not @code{0}, the duration in tenths of a second to wait for the file "
4400 "mutex which has been locked by another thread to be released before returning "
4401 "an error. When @code{-1}, then an error will be returned immediately."
4405 new_command("LS", 1, 1, ls_command
, _(
4407 "Lists the available data files stored in the data directory "
4408 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4411 new_command("RESET", 1, 1, NULL
, _(
4413 "Closes the currently opened file but keeps any previously set client options."
4416 new_command("NOP", 1, 1, NULL
, _(
4418 "Does nothing. Always returns successfully."
4421 /* !END-HELP-TEXT! */
4422 new_command ("CANCEL", 1, 1, NULL
, NULL
);
4423 new_command ("END", 1, 1, NULL
, NULL
);
4424 new_command ("BYE", 1, 1, NULL
, NULL
);
4427 for (i
= 0; command_table
[i
]; i
++);
4428 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
4433 register_commands (assuan_context_t ctx
)
4437 for (; command_table
[i
]; i
++)
4439 if (!command_table
[i
]->handler
)
4442 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
4443 command_table
[i
]->handler
,
4444 command_table
[i
]->help
);
4449 rc
= assuan_register_option_handler (ctx
, option_command
);
4453 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
4457 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
4461 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
4465 return assuan_register_post_cmd_notify (ctx
, command_finalize
);