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"
46 #include "util-misc.h"
55 /* Flags needed to be retained for a client across commands. */
56 #define FLAG_NEW 0x0001
57 #define FLAG_HAS_LOCK 0x0002
58 #define FLAG_LOCK_CMD 0x0004
59 #define FLAG_NO_PINENTRY 0x0008
60 #define FLAG_OPEN 0x0010
61 #define FLAG_KEEP_LOCK 0x0020
63 /* These are command option flags. */
64 #define OPT_INQUIRE 0x0001
65 #define OPT_NO_PASSPHRASE 0x0002
66 #define OPT_RESET 0x0004
67 #define OPT_LIST_RECURSE 0x0008
68 #define OPT_LIST_VERBOSE 0x0010
69 #define OPT_LOCK 0x0020
70 #define OPT_LOCK_ON_OPEN 0x0040
71 #define OPT_SIGN 0x0080
72 #define OPT_LIST_ALL 0x0100
73 #define OPT_LIST_WITH_TARGET 0x0200
74 #define OPT_DATA 0x0400
75 #define OPT_NO_AGENT 0x0800
77 struct command_table_s
80 gpg_error_t (*handler
) (assuan_context_t
, char *line
);
83 int unlock
; // unlock the file mutex after validating the checksum
86 static struct command_table_s
**command_table
;
88 static gpg_error_t
do_lock (struct client_s
*client
, int add
);
89 static gpg_error_t
validate_checksum (struct client_s
*,
90 struct cache_data_s
*);
91 static gpg_error_t
update_checksum (struct client_s
*client
);
94 unlock_file_mutex (struct client_s
*client
, int remove
)
98 // OPEN: keep the lock for the same file being reopened.
99 if (client
->flags
& FLAG_KEEP_LOCK
&& client
->flags
& FLAG_HAS_LOCK
)
102 if (!(client
->flags
& FLAG_HAS_LOCK
))
103 return GPG_ERR_NOT_LOCKED
;
105 rc
= cache_unlock_mutex (client
->md5file
, remove
);
107 rc
= GPG_ERR_INV_STATE
;
109 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_LOCK_CMD
);
115 lock_file_mutex (struct client_s
*client
, int add
)
118 int timeout
= config_get_integer (client
->filename
, "cache_timeout");
120 if (client
->flags
& FLAG_HAS_LOCK
)
123 rc
= cache_lock_mutex (client
->ctx
, client
->md5file
,
124 client
->lock_timeout
, add
, timeout
);
126 client
->flags
|= FLAG_HAS_LOCK
;
132 file_modified (struct client_s
*client
, struct command_table_s
*cmd
)
136 if (!(client
->flags
& FLAG_OPEN
))
137 return GPG_ERR_INV_STATE
;
139 rc
= lock_file_mutex (client
, 0);
140 if (!rc
|| rc
== GPG_ERR_NO_DATA
)
142 rc
= validate_checksum (client
, NULL
);
143 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
)
147 if ((cmd
->unlock
&& !(client
->flags
& FLAG_HAS_LOCK
)) || rc
)
148 unlock_file_mutex (client
, 0);
154 parse_xml (assuan_context_t ctx
, int new)
156 struct client_s
*client
= assuan_get_pointer (ctx
);
157 int cached
= client
->doc
!= NULL
;
161 client
->doc
= new_document ();
164 xmlDocDumpFormatMemory (client
->doc
,
165 (xmlChar
**) & client
->crypto
->plaintext
,
166 (int *) &client
->crypto
->plaintext_len
, 0);
167 if (!client
->crypto
->plaintext
)
169 xmlFreeDoc (client
->doc
);
175 client
->doc
= parse_doc ((char *) client
->crypto
->plaintext
,
176 client
->crypto
->plaintext_len
);
178 return !client
->doc
? GPG_ERR_ENOMEM
: 0;
182 free_client (struct client_s
*client
)
185 xmlFreeDoc (client
->doc
);
188 xfree (client
->filename
);
189 xfree (client
->last_error
);
193 cleanup_crypto_stage2 (client
->crypto
);
194 if (client
->crypto
->pkey_sexp
)
195 gcry_sexp_release (client
->crypto
->pkey_sexp
);
197 client
->crypto
->pkey_sexp
= NULL
;
198 memset (client
->crypto
->sign_grip
, 0,
199 sizeof (client
->crypto
->sign_grip
));
200 memset (client
->crypto
->grip
, 0, sizeof(client
->crypto
->grip
));
203 if (client
->xml_error
)
204 xmlResetError (client
->xml_error
);
208 cleanup_client (struct client_s
*client
)
210 assuan_context_t ctx
= client
->ctx
;
211 struct client_thread_s
*thd
= client
->thd
;
212 struct crypto_s
*crypto
= client
->crypto
;
213 long lock_timeout
= client
->lock_timeout
;
214 struct pinentry_option_s pin_opts
;
216 struct pinentry_option_s agent_pin_opts
;
219 memcpy (&agent_pin_opts
, &crypto
->agent
->pinentry_opts
,
220 sizeof(struct pinentry_option_s
));
223 memcpy (&pin_opts
, &client
->pinentry_opts
, sizeof(struct pinentry_option_s
));
224 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
225 free_client (client
);
226 memset (client
, 0, sizeof (struct client_s
));
227 client
->crypto
= crypto
;
230 client
->lock_timeout
= lock_timeout
;
231 memcpy (&client
->pinentry_opts
, &pin_opts
, sizeof(struct pinentry_option_s
));
233 memcpy (&crypto
->agent
->pinentry_opts
, &agent_pin_opts
,
234 sizeof(struct pinentry_option_s
));
239 open_finalize (assuan_context_t ctx
, char *key
, size_t keylen
)
241 struct client_s
*client
= assuan_get_pointer (ctx
);
243 struct cache_data_s
*cdata
= cache_get_data (client
->md5file
);
244 int cached
= 0, keyarg
= key
? 1 : 0;
246 unsigned char *salted_key
= NULL
;
249 client
->crypto
->filename
= str_dup (client
->filename
);
250 if (cdata
|| client
->flags
& FLAG_NEW
)
252 if (!(client
->flags
& FLAG_NEW
))
254 rc
= decrypt_xml (client
->crypto
, cdata
->doc
, cdata
->doclen
);
259 cached
= cdata
!= NULL
;
263 if (!key
&& !IS_PKCS (client
->crypto
))
265 if (client
->flags
& FLAG_NO_PINENTRY
)
267 rc
= inquire_passphrase (ctx
, "PASSPHRASE", (unsigned char **)&key
,
274 client
->pinentry_opts
.timeout
= config_get_integer (client
->filename
,
276 rc
= getpin_common (client
->ctx
, client
->filename
, PINENTRY_OPEN
,
283 if (!IS_PKCS (client
->crypto
))
285 algo
= cipher_to_gcrypt (client
->crypto
->hdr
.flags
);
286 rc
= hash_key (algo
, client
->crypto
->hdr
.salt
,
287 sizeof(client
->crypto
->hdr
.salt
), key
, keylen
,
288 (void **)&salted_key
, &keysize
);
293 rc
= decrypt_data (client
->ctx
, client
->crypto
, salted_key
, keysize
);
298 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
300 return GPG_ERR_ENOMEM
;
302 cdata
->key
= salted_key
;
303 cdata
->keylen
= keysize
;
306 rc
= decrypt_data (client
->ctx
, client
->crypto
, NULL
, 0);
311 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
314 int timeout
= config_get_integer (client
->filename
,
320 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
322 rc
= encrypt_xml (NULL
, cache_key
, cache_keysize
,
323 GCRY_CIPHER_AES
, client
->crypto
->plaintext
,
324 client
->crypto
->plaintext_len
, &cdata
->doc
,
325 &cdata
->doclen
, &cache_iv
, &cache_blocksize
,
327 if (!rc
&& !(client
->flags
& FLAG_NEW
) && IS_PKCS (client
->crypto
))
329 rc
= gcry_sexp_build ((gcry_sexp_t
*) & cdata
->pubkey
, NULL
,
330 "%S", client
->crypto
->pkey_sexp
);
335 gcry_sexp_release (cdata
->sigkey
);
337 cdata
->sigkey
= NULL
;
339 if (!rc
&& IS_PKCS (client
->crypto
))
340 rc
= gcry_sexp_build ((gcry_sexp_t
*) &cdata
->sigkey
, NULL
,
341 "%S", client
->crypto
->sigpkey_sexp
);
344 && !cache_add_file (client
->md5file
, (client
->flags
& FLAG_NEW
)
345 ? NULL
: client
->crypto
->grip
, cdata
, timeout
))
349 free_cache_data_once (cdata
);
350 xmlFreeDoc (client
->doc
);
357 client
->flags
|= FLAG_OPEN
;
362 update_checksum (client
);
364 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
365 rc
= do_lock (client
, 0);
371 req_cleanup (void *arg
)
376 strv_free ((char **) arg
);
380 parse_open_opt_lock (void *data
, void *value
)
382 struct client_s
*client
= data
;
384 client
->opts
|= OPT_LOCK_ON_OPEN
;
389 parse_opt_pinentry (void *data
, void *value
)
391 struct client_s
*client
= data
;
393 client
->flags
|= FLAG_NO_PINENTRY
;
398 parse_opt_inquire (void *data
, void *value
)
400 struct client_s
*client
= data
;
403 client
->opts
|= OPT_INQUIRE
;
408 update_checksum (struct client_s
*client
)
412 struct cache_data_s
*cdata
;
413 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
420 cdata
= cache_get_data (client
->md5file
);
424 cdata
->crc
= xmalloc (len
);
425 memcpy (cdata
->crc
, crc
, len
);
432 validate_checksum (struct client_s
*client
, struct cache_data_s
*cdata
)
439 if (cdata
&& !cdata
->crc
)
440 return GPG_ERR_CHECKSUM
;
442 rc
= get_checksum (client
->filename
, &crc
, &len
);
447 n
= memcmp (cdata
->crc
, crc
, len
);
448 else if (client
->crc
)
449 n
= memcmp (client
->crc
, crc
, len
);
452 return n
? GPG_ERR_CHECKSUM
: 0;
456 do_open (assuan_context_t ctx
, const char *filename
, const char *password
)
458 struct client_s
*client
= assuan_get_pointer (ctx
);
459 struct cache_data_s
*cdata
;
463 if (!valid_filename (filename
))
464 return GPG_ERR_INV_VALUE
;
466 gcry_md_hash_buffer (GCRY_MD_MD5
, client
->md5file
, filename
,
468 client
->filename
= str_dup (filename
);
469 if (!client
->filename
)
470 return GPG_ERR_ENOMEM
;
473 cdata
= cache_get_data (client
->md5file
);
474 if (cdata
&& cdata
->doc
)
478 rc
= validate_checksum (client
, cdata
);
479 /* This will check that the key is cached in the agent which needs to
480 * be determined for files that share a keygrip. */
483 rc
= cache_iscached (client
->filename
, &defer
);
485 rc
= GPG_ERR_KEY_EXPIRED
;
489 if (!rc
&& client
->thd
->remote
490 && config_get_boolean (NULL
, "tcp_require_key"))
491 rc
= GPG_ERR_KEY_EXPIRED
;
494 if (!rc
&& !password
)
496 rc
= read_data_header (client
->filename
, &client
->crypto
->hdr
,
500 if (IS_PKCS (client
->crypto
))
502 gcry_sexp_build (&client
->crypto
->pkey_sexp
, NULL
, "%S",
504 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
505 client
->crypto
->grip
);
506 gcry_sexp_build (&client
->crypto
->sigpkey_sexp
, NULL
, "%S",
508 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
509 client
->crypto
->sign_grip
);
514 rc
= open_finalize (ctx
, NULL
, 0);
520 /* There was an error accessing the file so clear the cache entry. The
521 * real error will be returned from read_data_file() since the file
522 * may have only disappeared. */
525 log_write ("%s: %s", filename
, pwmd_strerror (rc
));
526 rc
= cache_clear (client
->md5file
);
527 send_status_all (STATUS_CACHE
, NULL
);
534 rc
= read_data_file (client
->filename
, client
->crypto
);
537 if (gpg_err_source (rc
) != GPG_ERR_SOURCE_DEFAULT
||
538 gpg_err_code (rc
) != GPG_ERR_ENOENT
)
540 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
544 client
->flags
|= FLAG_NEW
;
545 memset (client
->crypto
->grip
, 0, sizeof (client
->crypto
->grip
));
546 memset (client
->crypto
->sign_grip
, 0,
547 sizeof (client
->crypto
->sign_grip
));
548 rc
= open_finalize (ctx
, NULL
, 0);
552 if (password
&& IS_PKCS (client
->crypto
))
555 rc
= set_agent_passphrase (client
->crypto
, password
, strlen (password
));
561 rc
= open_finalize (ctx
, (char *)password
, password
? strlen (password
) : 0);
566 open_command (assuan_context_t ctx
, char *line
)
569 struct client_s
*client
= assuan_get_pointer (ctx
);
570 char **req
, *password
= NULL
, *filename
;
571 unsigned char md5file
[16];
573 struct argv_s
*args
[] = {
574 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
575 &(struct argv_s
) {"no-pinentry", OPTION_TYPE_NOARG
,
580 client
->flags
&= ~(FLAG_NO_PINENTRY
);
581 rc
= parse_options (&line
, args
, client
);
583 return send_error (ctx
, rc
);
585 req
= str_split (line
, " ", 2);
587 return send_error (ctx
, GPG_ERR_SYNTAX
);
590 if (client
->thd
->remote
)
592 rc
= tls_validate_access (client
, req
[0]);
595 if (rc
== GPG_ERR_INV_USER_ID
)
596 log_write (_("client validation failed for file '%s'"), req
[0]);
598 return send_error (ctx
, rc
);
601 /* Remote pinentries are not supported. */
602 client
->flags
|= FLAG_NO_PINENTRY
;
606 pthread_cleanup_push (req_cleanup
, req
);
608 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
609 /* This client may have locked a different file with ISCACHED --lock than
610 * the current filename. This will remove that lock. */
611 same_file
= !memcmp (md5file
, client
->md5file
, 16) ? 1 : 0;
612 if (client
->flags
& FLAG_OPEN
||
613 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
615 typeof (client
->opts
) opts
= client
->opts
;
616 typeof (client
->flags
) flags
= client
->flags
;
619 client
->flags
|= FLAG_KEEP_LOCK
;
621 cleanup_client (client
);
623 client
->flags
|= flags
;
624 client
->flags
&= ~(FLAG_LOCK_CMD
| FLAG_NEW
);
626 client
->flags
&= ~(FLAG_HAS_LOCK
);
629 /* Need to lock the mutex here because file_modified() cannot without
630 * knowing the filename. */
631 memcpy (client
->md5file
, md5file
, 16);
632 rc
= lock_file_mutex (client
, 1);
635 password
= req
[1] && *req
[1] ? req
[1] : NULL
;
637 if (IS_PKCS (client
->crypto
))
638 rc
= set_pinentry_mode (client
->crypto
->agent
,
639 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
644 rc
= do_open (ctx
, filename
, password
);
646 cleanup_client (client
);
648 cleanup_crypto_stage1 (client
->crypto
);
652 pthread_cleanup_pop (1);
654 if (!rc
&& client
->flags
& FLAG_NEW
)
655 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
658 (void) kill_scd (client
->crypto
->agent
);
660 return send_error (ctx
, rc
);
664 parse_save_opt_no_passphrase (void *data
, void *value
)
666 struct client_s
*client
= data
;
669 client
->opts
|= OPT_NO_PASSPHRASE
;
674 parse_save_opt_no_agent (void *data
, void *value
)
676 struct client_s
*client
= data
;
678 client
->opts
|= OPT_NO_AGENT
;
683 parse_save_opt_cipher (void *data
, void *value
)
685 struct client_s
*client
= data
;
686 int algo
= cipher_string_to_gcrypt ((char *) value
);
687 file_header_t
*hdr
= &client
->crypto
->save
.hdr
;
690 return GPG_ERR_INV_VALUE
;
692 hdr
->flags
= set_cipher_flag (hdr
->flags
, algo
);
697 parse_save_opt_keygrip (void *data
, void *value
)
699 struct client_s
*client
= data
;
701 if (!IS_PKCS (client
->crypto
))
702 return GPG_ERR_INV_ARG
;
705 if (client
->crypto
->save
.pkey
)
706 gcry_sexp_release (client
->crypto
->save
.pkey
);
708 client
->crypto
->save
.pkey
= NULL
;
709 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.pkey
);
711 return GPG_ERR_INV_ARG
;
716 parse_save_opt_sign_keygrip (void *data
, void *value
)
718 struct client_s
*client
= data
;
720 if (!IS_PKCS (client
->crypto
))
721 return GPG_ERR_INV_ARG
;
724 if (client
->crypto
->save
.sigpkey
)
725 gcry_sexp_release (client
->crypto
->save
.sigpkey
);
727 client
->crypto
->save
.sigpkey
= NULL
;
728 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.sigpkey
);
730 return GPG_ERR_INV_ARG
;
735 parse_opt_s2k_count (void *data
, void *value
)
737 struct client_s
*client
= data
;
741 return GPG_ERR_INV_VALUE
;
743 client
->crypto
->save
.s2k_count
= strtoul (v
, NULL
, 10);
748 parse_save_opt_iterations (void *data
, void *value
)
750 struct client_s
*client
= data
;
755 return GPG_ERR_INV_VALUE
;
758 n
= strtoull (v
, &p
, 10);
759 if (n
== UINT64_MAX
&& errno
)
760 return gpg_error_from_syserror ();
762 return GPG_ERR_INV_VALUE
;
764 client
->crypto
->save
.hdr
.iterations
= n
;
769 save_finalize (assuan_context_t ctx
)
771 struct client_s
*client
= assuan_get_pointer (ctx
);
773 xmlChar
*xmlbuf
= NULL
;
778 xmlDocDumpFormatMemory (client
->doc
, &xmlbuf
, &xmlbuflen
, 0);
780 return GPG_ERR_ENOMEM
;
782 pthread_cleanup_push (xmlFree
, xmlbuf
);
784 if (!use_agent
|| ((client
->flags
& FLAG_NEW
)
785 && (client
->opts
& OPT_NO_AGENT
))
786 || !(client
->crypto
->hdr
.flags
& PWMD_FLAG_PKCS
))
788 rc
= export_common (ctx
, client
->flags
& FLAG_NO_PINENTRY
,
789 client
->crypto
, xmlbuf
, xmlbuflen
, client
->filename
,
790 NULL
, &key
, &keylen
, 1, 1);
795 gcry_sexp_t pubkey
= client
->crypto
->save
.pkey
;
796 gcry_sexp_t sigkey
= client
->crypto
->save
.sigpkey
;
799 pubkey
= client
->crypto
->pkey_sexp
;
803 sigkey
= client
->crypto
->sigpkey_sexp
;
806 gcry_sexp_build (&client
->crypto
->save
.sigpkey
, 0, "%S", pubkey
);
807 sigkey
= client
->crypto
->save
.sigpkey
;
811 rc
= encrypt_data_file (client
->ctx
, client
->crypto
, pubkey
, sigkey
,
812 client
->filename
, xmlbuf
, xmlbuflen
);
813 if (pubkey
== client
->crypto
->save
.pkey
)
817 gcry_sexp_release (client
->crypto
->pkey_sexp
);
818 client
->crypto
->pkey_sexp
= client
->crypto
->save
.pkey
;
819 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
820 client
->crypto
->grip
);
823 gcry_sexp_release (pubkey
);
825 client
->crypto
->save
.pkey
= NULL
;
829 gcry_pk_get_keygrip (sigkey
, client
->crypto
->sign_grip
);
831 if (sigkey
== client
->crypto
->save
.sigpkey
)
835 if (client
->crypto
->sigpkey_sexp
)
836 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
838 client
->crypto
->sigpkey_sexp
= client
->crypto
->save
.sigpkey
;
839 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
840 client
->crypto
->sign_grip
);
843 gcry_sexp_release (sigkey
);
845 client
->crypto
->save
.sigpkey
= NULL
;
854 rc
= save_common (client
->filename
, client
->crypto
, xmlbuf
, xmlbuflen
,
855 key
, keylen
, &cached
, client
->opts
& OPT_NO_AGENT
);
859 if (!rc
&& (!cached
|| (client
->flags
& FLAG_NEW
)))
860 send_status_all (STATUS_CACHE
, NULL
);
864 update_checksum (client
);
865 client
->flags
&= ~(FLAG_NEW
);
871 pthread_cleanup_pop (1); // xmlFree
876 parse_opt_reset (void *data
, void *value
)
878 struct client_s
*client
= data
;
881 client
->opts
|= OPT_RESET
;
886 save_command (assuan_context_t ctx
, char *line
)
888 struct client_s
*client
= assuan_get_pointer (ctx
);
891 struct argv_s
*args
[] = {
892 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
,
893 parse_save_opt_no_passphrase
},
894 &(struct argv_s
) {"cipher", OPTION_TYPE_ARG
, parse_save_opt_cipher
},
895 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
897 &(struct argv_s
) {"keygrip", OPTION_TYPE_ARG
, parse_save_opt_keygrip
},
898 &(struct argv_s
) {"sign-keygrip", OPTION_TYPE_ARG
,
899 parse_save_opt_sign_keygrip
},
900 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
901 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
902 &(struct argv_s
) {"cipher-iterations", OPTION_TYPE_ARG
,
903 parse_save_opt_iterations
},
904 &(struct argv_s
) {"no-agent", OPTION_TYPE_NOARG
,
905 parse_save_opt_no_agent
},
909 cleanup_save (&client
->crypto
->save
);
910 memcpy (&client
->crypto
->save
.hdr
, &client
->crypto
->hdr
,
911 sizeof (file_header_t
));
912 client
->crypto
->save
.s2k_count
=
913 config_get_ulong (client
->filename
, "s2k_count");
915 if (client
->flags
& FLAG_NEW
)
916 client
->crypto
->save
.hdr
.iterations
=
917 config_get_ulonglong (client
->filename
, "cipher_iterations");
919 rc
= parse_options (&line
, args
, client
);
921 return send_error (ctx
, rc
);
923 if (!(client
->flags
& FLAG_NEW
))
924 client
->opts
&= ~OPT_NO_AGENT
;
925 else if (client
->opts
& OPT_NO_AGENT
)
927 client
->crypto
->save
.hdr
.flags
&= ~PWMD_FLAG_PKCS
;
928 client
->crypto
->hdr
.flags
&= ~PWMD_FLAG_PKCS
;
931 if ((client
->opts
& OPT_NO_PASSPHRASE
) && !(client
->flags
& FLAG_NEW
)
932 && !(client
->opts
& OPT_INQUIRE
))
933 return send_error (ctx
, GPG_ERR_WRONG_KEY_USAGE
);
935 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
936 return send_error (ctx
, gpg_error_from_syserror ());
938 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
940 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
941 return send_error (ctx
, GPG_ERR_ENOANO
);
945 rc
= cache_iscached (client
->filename
, &defer
);
948 log_write ("%s: %s", client
->filename
,
949 pwmd_strerror (GPG_ERR_KEY_EXPIRED
));
950 client
->opts
|= OPT_RESET
;
953 if (client
->opts
& OPT_RESET
)
955 rc
= cache_clear (client
->md5file
);
957 return send_error (ctx
, rc
);
959 send_status_all (STATUS_CACHE
, NULL
);
962 rc
= update_element_mtime (xmlDocGetRootElement (client
->doc
));
964 return send_error (ctx
, rc
);
967 if (!rc
&& use_agent
&& !client
->crypto
->save
.pkey
&&
968 (client
->flags
& FLAG_NEW
|| client
->opts
& OPT_INQUIRE
)
969 && !(client
->opts
& OPT_NO_AGENT
))
971 rc
= send_status (client
->ctx
, STATUS_GENKEY
, NULL
);
974 struct inquire_data_s idata
= { 0 };
975 char *params
= client
->opts
& OPT_INQUIRE
? NULL
976 : default_key_params (client
->crypto
);
978 pthread_cleanup_push (xfree
, params
);
979 idata
.crypto
= client
->crypto
;
984 idata
.len
= strlen (params
);
989 client
->crypto
->agent
->inquire_data
= &idata
;
990 client
->crypto
->agent
->inquire_cb
= NULL
;
991 rc
= generate_key (client
->crypto
, params
,
992 (client
->opts
& OPT_NO_PASSPHRASE
), 1);
993 pthread_cleanup_pop (1);
999 rc
= save_finalize (ctx
);
1001 cleanup_crypto_stage1 (client
->crypto
);
1003 (void) kill_scd (client
->crypto
->agent
);
1005 return send_error (ctx
, rc
);
1009 do_delete (assuan_context_t ctx
, char *line
)
1011 struct client_s
*client
= assuan_get_pointer (ctx
);
1016 if (strchr (line
, '\t'))
1017 req
= str_split (line
, "\t", 0);
1019 req
= str_split (line
, " ", 0);
1022 return GPG_ERR_SYNTAX
;
1024 n
= find_root_element (client
->doc
, &req
, &rc
, NULL
, 0, 0);
1032 * No sub-node defined. Remove the entire node (root element).
1038 rc
= unlink_node (n
);
1047 find_elements (client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
1055 rc
= unlink_node (n
);
1063 delete_command (assuan_context_t ctx
, char *line
)
1065 struct client_s
*client
= assuan_get_pointer (ctx
);
1067 struct argv_s
*args
[] = {
1068 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1072 rc
= parse_options (&line
, args
, client
);
1074 return send_error (ctx
, rc
);
1076 if (client
->opts
& OPT_INQUIRE
)
1078 unsigned char *result
;
1081 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1083 return send_error (ctx
, rc
);
1085 line
= (char *) result
;
1088 rc
= do_delete (ctx
, line
);
1090 if (client
->opts
& OPT_INQUIRE
)
1093 return send_error (ctx
, rc
);
1097 store_command (assuan_context_t ctx
, char *line
)
1099 struct client_s
*client
= assuan_get_pointer (ctx
);
1102 unsigned char *result
;
1104 xmlNodePtr n
, parent
;
1106 char *content
= NULL
;
1108 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1110 return send_error (ctx
, rc
);
1112 req
= str_split ((char *) result
, "\t", 0);
1116 return send_error (ctx
, GPG_ERR_SYNTAX
);
1118 len
= strv_length (req
);
1119 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1120 if (*(req
+ 1) && !valid_element_path (req
, has_content
))
1123 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1126 if (has_content
|| !*req
[len
- 1])
1129 content
= req
[len
- 1];
1130 req
[len
- 1] = NULL
;
1134 n
= find_root_element (client
->doc
, &req
, &rc
, NULL
, 0, 0);
1135 if (rc
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1137 rc
= new_root_element (client
->doc
, *req
);
1141 return send_error (ctx
, GPG_ERR_SYNTAX
);
1150 return send_error (ctx
, rc
);
1155 if (req
[1] && *req
[1])
1158 parent
= create_elements_cb (n
, req
+ 1, &rc
, NULL
);
1160 parent
= find_elements (client
->doc
, n
->children
, req
+ 1, &rc
,
1161 NULL
, NULL
, create_elements_cb
, 0, 0, NULL
,
1167 n
= find_text_node (parent
->children
);
1169 xmlNodeSetContent (n
, (xmlChar
*) content
);
1171 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1173 update_element_mtime (parent
);
1178 return send_error (ctx
, rc
);
1182 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1187 int progress
= config_get_integer ("global", "xfer_progress");
1191 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1192 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1193 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1201 if (sent
+ to_send
> total
)
1202 to_send
= total
- sent
;
1204 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1205 flush
? 0 : to_send
);
1208 sent
+= flush
? 0 : to_send
;
1210 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1211 (sent
== total
&& flush
))
1212 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1214 if (!flush
&& !rc
&& sent
== total
)
1221 while (!rc
&& sent
< total
);
1227 do_get (assuan_context_t ctx
, char *line
)
1229 struct client_s
*client
= assuan_get_pointer (ctx
);
1234 req
= str_split (line
, "\t", 0);
1239 return GPG_ERR_SYNTAX
;
1242 n
= find_root_element (client
->doc
, &req
, &rc
, NULL
, 0, 0);
1251 find_elements (client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
1258 if (!n
|| !n
->children
)
1259 return GPG_ERR_NO_DATA
;
1261 n
= find_text_node (n
->children
);
1262 if (!n
|| !n
->content
|| !*n
->content
)
1263 return GPG_ERR_NO_DATA
;
1265 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1270 get_command (assuan_context_t ctx
, char *line
)
1272 struct client_s
*client
= assuan_get_pointer (ctx
);
1274 struct argv_s
*args
[] = {
1275 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1279 rc
= parse_options (&line
, args
, client
);
1281 return send_error (ctx
, rc
);
1283 if (client
->opts
& OPT_INQUIRE
)
1285 unsigned char *result
;
1288 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1290 return send_error (ctx
, rc
);
1292 line
= (char *) result
;
1295 rc
= do_get (ctx
, line
);
1297 if (client
->opts
& OPT_INQUIRE
)
1300 return send_error (ctx
, rc
);
1303 static void list_command_cleanup1 (void *arg
);
1305 realpath_command (assuan_context_t ctx
, char *line
)
1307 struct string_s
*string
= NULL
;
1309 struct client_s
*client
= assuan_get_pointer (ctx
);
1310 struct argv_s
*args
[] = {
1311 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1315 rc
= parse_options (&line
, args
, client
);
1317 return send_error (ctx
, rc
);
1319 if (client
->opts
& OPT_INQUIRE
)
1321 unsigned char *result
;
1324 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1326 return send_error (ctx
, rc
);
1328 line
= (char *) result
;
1331 rc
= build_realpath (client
->doc
, line
, &string
);
1334 pthread_cleanup_push (list_command_cleanup1
, string
);
1335 rc
= xfer_data (ctx
, string
->str
, string
->len
);
1336 pthread_cleanup_pop (1);
1339 if (client
->opts
& OPT_INQUIRE
)
1342 return send_error (ctx
, rc
);
1346 list_command_cleanup1 (void *arg
)
1349 string_free ((struct string_s
*) arg
, 1);
1353 list_command_cleanup2 (void *arg
)
1355 struct element_list_s
*elements
= arg
;
1361 int total
= slist_length (elements
->list
);
1364 for (i
= 0; i
< total
; i
++)
1366 char *tmp
= slist_nth_data (elements
->list
, i
);
1370 slist_free (elements
->list
);
1373 if (elements
->prefix
)
1374 xfree (elements
->prefix
);
1377 strv_free (elements
->req
);
1384 parse_list_opt_norecurse (void *data
, void *value
)
1386 struct client_s
*client
= data
;
1388 client
->opts
&= ~(OPT_LIST_RECURSE
);
1393 parse_list_opt_verbose (void *data
, void *value
)
1395 struct client_s
*client
= data
;
1397 client
->opts
|= OPT_LIST_VERBOSE
;
1402 parse_list_opt_target (void *data
, void *value
)
1404 struct client_s
*client
= data
;
1406 client
->opts
|= OPT_LIST_WITH_TARGET
;
1411 parse_list_opt_all (void *data
, void *value
)
1413 struct client_s
*client
= data
;
1415 client
->opts
|= OPT_LIST_ALL
;
1420 list_path_once (struct client_s
*client
, char *line
,
1421 struct element_list_s
*elements
, struct string_s
*result
)
1425 elements
->req
= str_split (line
, " ", 0);
1427 strv_printf (&elements
->req
, "%s", line
);
1429 rc
= create_path_list (client
->doc
, elements
, *elements
->req
);
1430 if (rc
== GPG_ERR_ELOOP
&& elements
->verbose
)
1437 int total
= slist_length (elements
->list
);
1441 rc
= GPG_ERR_NO_DATA
;
1447 for (i
= 0; i
< total
; i
++)
1449 char *tmp
= slist_nth_data (elements
->list
, i
);
1451 string_append_printf (result
, "%s%s", tmp
,
1452 i
+ 1 == total
? "" : "\n");
1458 rc
= GPG_ERR_NO_DATA
;
1465 has_list_flag (char *path
, char *flags
)
1469 while (*p
&& *++p
!= ' ');
1478 for (f
= flags
; *f
&& *f
!= ' '; f
++)
1489 do_list (assuan_context_t ctx
, char *line
)
1491 struct client_s
*client
= assuan_get_pointer (ctx
);
1493 struct element_list_s
*elements
= NULL
;
1495 elements
= xcalloc (1, sizeof (struct element_list_s
));
1497 return GPG_ERR_ENOMEM
;
1499 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1501 (client
->opts
& OPT_LIST_VERBOSE
) | (client
->opts
& OPT_LIST_WITH_TARGET
);
1502 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1504 if (!line
|| !*line
)
1506 struct string_s
*str
= NULL
;
1508 pthread_cleanup_push (list_command_cleanup2
, elements
);
1509 rc
= list_root_elements (client
->doc
, &str
, elements
->verbose
,
1510 elements
->with_target
);
1511 pthread_cleanup_pop (1);
1512 pthread_cleanup_push (list_command_cleanup1
, str
);
1516 if (client
->opts
& OPT_LIST_ALL
)
1518 char **roots
= str_split (str
->str
, "\n", 0);
1521 pthread_cleanup_push (req_cleanup
, roots
);
1522 string_truncate (str
, 0);
1524 for (p
= roots
; *p
; p
++)
1526 if (strchr (*p
, ' '))
1528 if (has_list_flag (*p
, "EO"))
1530 string_append_printf (str
, "%s%s", *p
,
1531 *(p
+ 1) ? "\n" : "");
1536 elements
= xcalloc (1, sizeof (struct element_list_s
));
1539 rc
= GPG_ERR_ENOMEM
;
1543 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1545 (client
->opts
& OPT_LIST_VERBOSE
) | (client
->opts
&
1546 OPT_LIST_WITH_TARGET
);
1547 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1548 pthread_cleanup_push (list_command_cleanup2
, elements
);
1549 rc
= list_path_once (client
, *p
, elements
, str
);
1550 pthread_cleanup_pop (1);
1555 string_append (str
, "\n");
1558 pthread_cleanup_pop (1);
1562 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1565 pthread_cleanup_pop (1);
1569 pthread_cleanup_push (list_command_cleanup2
, elements
);
1570 struct string_s
*str
= string_new (NULL
);
1571 pthread_cleanup_push (list_command_cleanup1
, str
);
1572 rc
= list_path_once (client
, line
, elements
, str
);
1574 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1576 pthread_cleanup_pop (1);
1577 pthread_cleanup_pop (1);
1582 list_command (assuan_context_t ctx
, char *line
)
1584 struct client_s
*client
= assuan_get_pointer (ctx
);
1586 struct argv_s
*args
[] = {
1587 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
,
1588 parse_list_opt_norecurse
},
1589 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_list_opt_verbose
},
1590 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1591 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
,
1592 parse_list_opt_target
},
1593 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_all
},
1597 if (disable_list_and_dump
== 1)
1598 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1600 client
->opts
|= OPT_LIST_RECURSE
;
1601 rc
= parse_options (&line
, args
, client
);
1603 return send_error (ctx
, rc
);
1605 if (client
->opts
& OPT_LIST_ALL
)
1606 client
->opts
|= OPT_LIST_RECURSE
| OPT_LIST_VERBOSE
;
1608 if (client
->opts
& OPT_INQUIRE
)
1610 unsigned char *result
;
1613 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1615 return send_error (ctx
, rc
);
1617 line
= (char *) result
;
1620 rc
= do_list (ctx
, line
);
1622 if (client
->opts
& OPT_INQUIRE
)
1625 return send_error (ctx
, rc
);
1629 * req[0] - element path
1632 attribute_list (assuan_context_t ctx
, char **req
)
1634 struct client_s
*client
= assuan_get_pointer (ctx
);
1635 char **attrlist
= NULL
;
1643 if (!req
|| !req
[0])
1644 return GPG_ERR_SYNTAX
;
1646 if ((path
= str_split (req
[0], "\t", 0)) == NULL
)
1649 * The first argument may be only a root element.
1651 if ((path
= str_split (req
[0], " ", 0)) == NULL
)
1652 return GPG_ERR_SYNTAX
;
1655 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
1665 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
1666 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1677 for (a
= n
->properties
; a
; a
= a
->next
)
1681 if ((pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *))) == NULL
)
1684 strv_free (attrlist
);
1686 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1687 pwmd_strerror (GPG_ERR_ENOMEM
));
1688 return GPG_ERR_ENOMEM
;
1693 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
,
1695 && an
->content
? (char *) an
->content
: "");
1699 strv_free (attrlist
);
1700 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1701 pwmd_strerror (GPG_ERR_ENOMEM
));
1702 return GPG_ERR_ENOMEM
;
1705 attrlist
[++i
] = NULL
;
1709 return GPG_ERR_NO_DATA
;
1711 line
= strv_join ("\n", attrlist
);
1715 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1716 pwmd_strerror (GPG_ERR_ENOMEM
));
1717 strv_free (attrlist
);
1718 return GPG_ERR_ENOMEM
;
1721 pthread_cleanup_push (xfree
, line
);
1722 pthread_cleanup_push (req_cleanup
, attrlist
);
1723 rc
= xfer_data (ctx
, line
, strlen (line
));
1724 pthread_cleanup_pop (1);
1725 pthread_cleanup_pop (1);
1730 * req[0] - attribute
1731 * req[1] - element path
1734 attribute_delete (struct client_s
*client
, char **req
)
1740 if (!req
|| !req
[0] || !req
[1])
1741 return GPG_ERR_SYNTAX
;
1743 if (!strcmp (req
[0], "_name"))
1744 return GPG_ERR_INV_ATTR
;
1746 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1749 * The first argument may be only a root element.
1751 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1752 return GPG_ERR_SYNTAX
;
1755 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
1761 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
1762 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1767 rc
= delete_attribute (n
, (xmlChar
*) req
[0]);
1775 create_element_path (struct client_s
*client
,
1776 char ***elements
, gpg_error_t
* rc
, xmlNodePtr parent
)
1778 char **req
= *elements
;
1779 char **req_orig
= strv_dup (req
);
1780 xmlNodePtr n
= NULL
;
1786 *rc
= GPG_ERR_ENOMEM
;
1787 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1788 pwmd_strerror (GPG_ERR_ENOMEM
));
1793 n
= find_root_element (client
->doc
, &req
, rc
, NULL
, 0, 0);
1796 if (*rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1799 *rc
= new_root_element (client
->doc
, req
[0]);
1805 else if (n
== parent
)
1807 *rc
= GPG_ERR_CONFLICT
;
1814 n
= create_target_elements_cb (n
, req
+ 1, rc
, NULL
);
1816 n
= find_elements (client
->doc
, n
->children
, req
+ 1, rc
, NULL
, NULL
,
1817 create_target_elements_cb
, 0, 0, parent
, 0);
1823 * Reset the position of the element tree now that the elements
1824 * have been created.
1829 n
= find_root_element (client
->doc
, &req
, rc
, NULL
, 0, 0);
1833 n
= find_elements (client
->doc
, n
->children
, req
+ 1, rc
,
1834 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1841 strv_free (req_orig
);
1848 * Creates a "target" attribute. When other commands encounter an element with
1849 * this attribute, the element path is modified to the target value. If the
1850 * source element path doesn't exist when using 'ATTR SET target', it is
1851 * created, but the destination element path must exist.
1853 * req[0] - source element path
1854 * req[1] - destination element path
1857 target_attribute (struct client_s
*client
, char **req
)
1859 char **src
, **dst
, *line
= NULL
, **odst
= NULL
;
1863 if (!req
|| !req
[0] || !req
[1])
1864 return GPG_ERR_SYNTAX
;
1866 if ((src
= str_split (req
[0], "\t", 0)) == NULL
)
1869 * The first argument may be only a root element.
1871 if ((src
= str_split (req
[0], " ", 0)) == NULL
)
1872 return GPG_ERR_SYNTAX
;
1875 if (!valid_element_path (src
, 0))
1876 return GPG_ERR_INV_VALUE
;
1878 if ((dst
= str_split (req
[1], "\t", 0)) == NULL
)
1881 * The first argument may be only a root element.
1883 if ((dst
= str_split (req
[1], " ", 0)) == NULL
)
1885 rc
= GPG_ERR_SYNTAX
;
1890 odst
= strv_dup (dst
);
1893 rc
= GPG_ERR_ENOMEM
;
1897 n
= find_root_element (client
->doc
, &dst
, &rc
, NULL
, 0, 0);
1899 * Make sure the destination element path exists.
1906 n
= find_elements (client
->doc
, n
->children
, dst
+ 1, &rc
,
1907 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1912 rc
= validate_target_attribute (client
->doc
, req
[0], n
);
1913 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1916 n
= create_element_path (client
, &src
, &rc
, NULL
);
1920 line
= strv_join ("\t", odst
);
1923 rc
= GPG_ERR_ENOMEM
;
1927 rc
= add_attribute (n
, "target", line
);
1938 * req[0] - attribute
1939 * req[1] - element path
1942 attribute_get (assuan_context_t ctx
, char **req
)
1944 struct client_s
*client
= assuan_get_pointer (ctx
);
1950 if (!req
|| !req
[0] || !req
[1])
1951 return GPG_ERR_SYNTAX
;
1953 if (strchr (req
[1], '\t'))
1955 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1956 return GPG_ERR_SYNTAX
;
1960 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1961 return GPG_ERR_SYNTAX
;
1964 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
1971 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
1972 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1980 if ((a
= xmlGetProp (n
, (xmlChar
*) req
[0])) == NULL
)
1981 return GPG_ERR_NOT_FOUND
;
1983 pthread_cleanup_push (xmlFree
, a
);
1986 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
1988 rc
= GPG_ERR_NO_DATA
;
1990 pthread_cleanup_pop (1);
1999 * req[0] - attribute
2000 * req[1] - element path
2004 attribute_set (struct client_s
*client
, char **req
)
2010 if (!req
|| !req
[0] || !req
[1])
2011 return GPG_ERR_SYNTAX
;
2014 * Reserved attribute names.
2016 if (!strcmp (req
[0], "_name"))
2017 return GPG_ERR_INV_ATTR
;
2018 else if (!strcmp (req
[0], "target"))
2019 return target_attribute (client
, req
+ 1);
2021 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2024 * The first argument may be only a root element.
2026 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2027 return GPG_ERR_SYNTAX
;
2030 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
2037 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
2038 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2044 rc
= add_attribute (n
, req
[0], req
[2]);
2053 * req[1] - attribute name or element path if command is LIST
2054 * req[2] - element path
2055 * req[2] - element path or value
2059 do_attr (assuan_context_t ctx
, char *line
)
2061 struct client_s
*client
= assuan_get_pointer (ctx
);
2065 req
= str_split (line
, " ", 4);
2066 if (!req
|| !req
[0] || !req
[1])
2069 return GPG_ERR_SYNTAX
;
2072 pthread_cleanup_push (req_cleanup
, req
);
2074 if (strcasecmp (req
[0], "SET") == 0)
2075 rc
= attribute_set (client
, req
+ 1);
2076 else if (strcasecmp (req
[0], "GET") == 0)
2077 rc
= attribute_get (ctx
, req
+ 1);
2078 else if (strcasecmp (req
[0], "DELETE") == 0)
2079 rc
= attribute_delete (client
, req
+ 1);
2080 else if (strcasecmp (req
[0], "LIST") == 0)
2081 rc
= attribute_list (ctx
, req
+ 1);
2083 rc
= GPG_ERR_SYNTAX
;
2085 pthread_cleanup_pop (1);
2090 attr_command (assuan_context_t ctx
, char *line
)
2092 struct client_s
*client
= assuan_get_pointer (ctx
);
2094 struct argv_s
*args
[] = {
2095 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2099 rc
= parse_options (&line
, args
, client
);
2101 return send_error (ctx
, rc
);
2103 if (client
->opts
& OPT_INQUIRE
)
2105 unsigned char *result
;
2108 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2110 return send_error (ctx
, rc
);
2112 line
= (char *) result
;
2115 rc
= do_attr (ctx
, line
);
2117 if (client
->opts
& OPT_INQUIRE
)
2120 return send_error (ctx
, rc
);
2124 parse_iscached_opt_lock (void *data
, void *value
)
2126 struct client_s
*client
= data
;
2129 client
->opts
|= OPT_LOCK
;
2134 iscached_command (assuan_context_t ctx
, char *line
)
2136 struct client_s
*client
= assuan_get_pointer (ctx
);
2138 unsigned char md5file
[16];
2139 struct argv_s
*args
[] = {
2140 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2144 if (!line
|| !*line
)
2145 return send_error (ctx
, GPG_ERR_SYNTAX
);
2147 rc
= parse_options (&line
, args
, client
);
2149 return send_error (ctx
, rc
);
2150 else if (!valid_filename (line
))
2151 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2153 rc
= cache_iscached (line
, NULL
);
2154 if (client
->opts
& OPT_LOCK
2155 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2157 gpg_error_t trc
= rc
;
2158 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2159 if (memcmp (md5file
, client
->md5file
, 16))
2160 cleanup_client (client
);
2162 memcpy (client
->md5file
, md5file
, 16);
2163 rc
= do_lock (client
, 1);
2168 return send_error (ctx
, rc
);
2172 clearcache_command (assuan_context_t ctx
, char *line
)
2174 gpg_error_t rc
= 0, all_rc
= 0;
2175 unsigned char md5file
[16];
2179 struct client_thread_s
*once
= NULL
;
2182 MUTEX_LOCK (&cn_mutex
);
2183 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
2185 if (!line
|| !*line
)
2188 t
= slist_length (cn_thread_list
);
2190 for (i
= 0; i
< t
; i
++)
2192 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2197 /* Lock each connected clients' file mutex to prevent any other client
2198 * from accessing the cache entry (the file mutex is locked upon
2199 * command startup). The cache for the entry is not cleared if the
2200 * file mutex is locked by another client to prevent this function
2205 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
2206 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2208 if (pthread_equal (pthread_self (), thd
->tid
))
2212 if (!thd
->cl
->filename
||
2213 cache_iscached (thd
->cl
->filename
,
2214 NULL
) == GPG_ERR_NO_DATA
)
2220 cache_defer_clear (thd
->cl
->md5file
);
2223 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2231 rc
= cache_clear (thd
->cl
->md5file
);
2235 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2242 /* A single data filename was specified. Lock only this data file
2243 * mutex and free the cache entry. */
2246 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2248 if (!memcmp (thd
->cl
->md5file
, md5file
, sizeof (md5file
)))
2250 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0,
2252 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2254 if (pthread_equal (pthread_self (), thd
->tid
))
2261 rc
= cache_clear (thd
->cl
->md5file
);
2262 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2266 cache_defer_clear (thd
->cl
->md5file
);
2274 /* Only connected clients' cache entries have been cleared. Now clear any
2275 * remaining cache entries without clients but only if there wasn't an
2276 * error from above since this would defeat the locking check of the
2277 * remaining entries. */
2283 /* No clients are using the specified file. */
2284 else if (!all_rc
&& !rc
&& !once
)
2286 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2287 rc
= cache_clear (md5file
);
2290 /* Release the connection mutex. */
2291 pthread_cleanup_pop (1);
2295 send_status_all (STATUS_CACHE
, NULL
);
2297 /* One or more files were locked while clearing all cache entries. */
2301 return send_error (ctx
, rc
);
2305 cachetimeout_command (assuan_context_t ctx
, char *line
)
2307 unsigned char md5file
[16];
2309 char **req
= str_split (line
, " ", 0);
2313 if (!req
|| !*req
|| !req
[1])
2316 return send_error (ctx
, GPG_ERR_SYNTAX
);
2320 timeout
= (int) strtol (req
[1], &p
, 10);
2321 if (errno
!= 0 || *p
|| timeout
< -1)
2324 return send_error (ctx
, GPG_ERR_SYNTAX
);
2327 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, req
[0], strlen (req
[0]));
2328 rc
= cache_set_timeout (md5file
, timeout
);
2331 MUTEX_LOCK (&rcfile_mutex
);
2332 config_set_int_param (&global_config
, req
[0], "cache_timeout", req
[1]);
2333 MUTEX_UNLOCK (&rcfile_mutex
);
2337 return send_error (ctx
, rc
);
2341 dump_command (assuan_context_t ctx
, char *line
)
2345 struct client_s
*client
= assuan_get_pointer (ctx
);
2348 if (disable_list_and_dump
== 1)
2349 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2351 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2355 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2356 pwmd_strerror (GPG_ERR_ENOMEM
));
2357 return send_error (ctx
, GPG_ERR_ENOMEM
);
2360 pthread_cleanup_push (xmlFree
, xml
);
2361 rc
= xfer_data (ctx
, (char *) xml
, len
);
2362 pthread_cleanup_pop (1);
2363 return send_error (ctx
, rc
);
2367 getconfig_command (assuan_context_t ctx
, char *line
)
2369 struct client_s
*client
= assuan_get_pointer (ctx
);
2371 char filename
[255] = { 0 }, param
[747] =
2374 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2376 if (!line
|| !*line
)
2377 return send_error (ctx
, GPG_ERR_SYNTAX
);
2379 if (strchr (line
, ' '))
2381 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2386 if (fp
&& !valid_filename (fp
))
2387 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2389 paramp
= str_down (paramp
);
2390 if (!strcmp (paramp
, "cipher") && fp
)
2392 struct crypto_s
*crypto
;
2394 rc
= init_client_crypto (&crypto
);
2397 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2401 gcry_cipher_algo_name (cipher_to_gcrypt (crypto
->hdr
.flags
));
2411 cleanup_crypto (&crypto
);
2412 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2413 return send_error (ctx
, rc
);
2418 else if (!strcmp (paramp
, "cipher_iterations") && fp
)
2420 struct crypto_s
*crypto
;
2422 rc
= init_client_crypto (&crypto
);
2425 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2428 tmp
= str_asprintf ("%llu",
2429 (unsigned long long) crypto
->hdr
.
2432 rc
= GPG_ERR_ENOMEM
;
2436 cleanup_crypto (&crypto
);
2437 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2438 return send_error (ctx
, rc
);
2443 else if (!strcmp (paramp
, "passphrase"))
2444 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2446 p
= config_get_value (fp
? fp
: "global", paramp
);
2448 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2450 tmp
= expand_homedir (p
);
2454 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2455 pwmd_strerror (GPG_ERR_ENOMEM
));
2456 return send_error (ctx
, GPG_ERR_ENOMEM
);
2461 pthread_cleanup_push (xfree
, p
);
2462 rc
= xfer_data (ctx
, p
, strlen (p
));
2463 pthread_cleanup_pop (1);
2464 return send_error (ctx
, rc
);
2469 xmlXPathContextPtr xp
;
2470 xmlXPathObjectPtr result
;
2476 xpath_command_cleanup (void *arg
)
2478 struct xpath_s
*xpath
= arg
;
2483 req_cleanup (xpath
->req
);
2486 xmlBufferFree (xpath
->buf
);
2489 xmlXPathFreeObject (xpath
->result
);
2492 xmlXPathFreeContext (xpath
->xp
);
2496 do_xpath (assuan_context_t ctx
, char *line
)
2499 struct client_s
*client
= assuan_get_pointer (ctx
);
2500 struct xpath_s _x
= { 0 };
2501 struct xpath_s
*xpath
= &_x
;
2503 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2505 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2506 return GPG_ERR_ENOMEM
;
2509 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2512 rc
= GPG_ERR_BAD_DATA
;
2517 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2520 rc
= GPG_ERR_BAD_DATA
;
2524 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2526 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2530 rc
= recurse_xpath_nodeset (client
->doc
, xpath
->result
->nodesetval
,
2531 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2535 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2537 rc
= GPG_ERR_NO_DATA
;
2540 else if (xpath
->req
[1])
2546 pthread_cleanup_push (xpath_command_cleanup
, &xpath
);
2547 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2548 xmlBufferLength (xpath
->buf
));
2549 pthread_cleanup_pop (0);
2551 xpath_command_cleanup (xpath
);
2556 xpath_command (assuan_context_t ctx
, char *line
)
2558 struct client_s
*client
= assuan_get_pointer (ctx
);
2560 struct argv_s
*args
[] = {
2561 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2565 if (disable_list_and_dump
== 1)
2566 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2568 rc
= parse_options (&line
, args
, client
);
2570 return send_error (ctx
, rc
);
2572 if (client
->opts
& OPT_INQUIRE
)
2574 unsigned char *result
;
2577 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2579 return send_error (ctx
, rc
);
2581 line
= (char *) result
;
2584 if (!line
|| !*line
)
2585 rc
= GPG_ERR_SYNTAX
;
2588 rc
= do_xpath (ctx
, line
);
2590 if (client
->opts
& OPT_INQUIRE
)
2593 return send_error (ctx
, rc
);
2597 do_xpathattr (assuan_context_t ctx
, char *line
)
2599 struct client_s
*client
= assuan_get_pointer (ctx
);
2603 struct xpath_s _x
= { 0 };
2604 struct xpath_s
*xpath
= &_x
;
2606 if ((req
= str_split (line
, " ", 3)) == NULL
)
2607 return GPG_ERR_ENOMEM
;
2611 rc
= GPG_ERR_SYNTAX
;
2615 if (!strcasecmp (req
[0], "SET"))
2617 else if (!strcasecmp (req
[0], "DELETE"))
2621 rc
= GPG_ERR_SYNTAX
;
2625 if (!req
[1] || !req
[2])
2627 rc
= GPG_ERR_SYNTAX
;
2631 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2633 rc
= GPG_ERR_ENOMEM
;
2637 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2639 rc
= GPG_ERR_SYNTAX
;
2643 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2646 rc
= GPG_ERR_BAD_DATA
;
2651 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2654 rc
= GPG_ERR_BAD_DATA
;
2658 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2660 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2664 rc
= recurse_xpath_nodeset (client
->doc
, xpath
->result
->nodesetval
,
2665 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2666 (xmlChar
*) req
[1]);
2669 xpath_command_cleanup (xpath
);
2674 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2676 xpathattr_command (assuan_context_t ctx
, char *line
)
2678 struct client_s
*client
= assuan_get_pointer (ctx
);
2680 struct argv_s
*args
[] = {
2681 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2685 if (disable_list_and_dump
== 1)
2686 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2688 rc
= parse_options (&line
, args
, client
);
2690 return send_error (ctx
, rc
);
2692 if (client
->opts
& OPT_INQUIRE
)
2694 unsigned char *result
;
2697 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2699 return send_error (ctx
, rc
);
2701 line
= (char *) result
;
2704 if (!line
|| !*line
)
2705 rc
= GPG_ERR_SYNTAX
;
2708 rc
= do_xpathattr (ctx
, line
);
2710 if (client
->opts
& OPT_INQUIRE
)
2713 return send_error (ctx
, rc
);
2717 do_import (struct client_s
*client
, unsigned char *line
)
2719 char **req
, **path
= NULL
, **path_orig
= NULL
, *content
;
2720 xmlDocPtr doc
= NULL
;
2721 xmlNodePtr n
, root
, copy
;
2724 req
= str_split ((char *) line
, "\t", 2);
2727 return GPG_ERR_SYNTAX
;
2730 path
= str_split (req
[1], "\t", 0);
2731 if (!content
|| !*content
)
2733 rc
= GPG_ERR_SYNTAX
;
2737 if (path
&& !valid_element_path (path
, 0))
2739 rc
= GPG_ERR_INV_VALUE
;
2743 doc
= xmlReadDoc ((xmlChar
*) content
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2746 rc
= GPG_ERR_BAD_DATA
;
2750 root
= xmlDocGetRootElement (doc
);
2751 rc
= validate_import (root
);
2757 path_orig
= strv_dup (path
);
2760 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2761 pwmd_strerror (GPG_ERR_ENOMEM
));
2762 rc
= GPG_ERR_ENOMEM
;
2766 xmlChar
*a
= xmlGetProp (root
, (xmlChar
*) "_name");
2769 strv_free (path_orig
);
2770 rc
= GPG_ERR_ENOMEM
;
2774 if (strv_printf (&path
, "%s", (char *) a
) == 0)
2777 strv_free (path_orig
);
2778 rc
= GPG_ERR_ENOMEM
;
2783 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
2785 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2787 strv_free (path_orig
);
2794 find_elements (client
->doc
, n
->children
, path
+ 1, &rc
, NULL
,
2795 NULL
, NULL
, 0, 0, NULL
, 1);
2797 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2799 strv_free (path_orig
);
2804 xmlNodePtr parent
= n
->parent
;
2815 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
2817 n
= create_element_path (client
, &path
, &rc
, NULL
);
2823 copy
= xmlCopyNodeList (root
);
2824 n
= xmlAddChildList (n
, copy
);
2826 rc
= GPG_ERR_BAD_DATA
;
2830 /* Check if the content root element can create a DTD root element. */
2831 if (!xmlStrEqual ((xmlChar
*) "element", root
->name
))
2833 rc
= GPG_ERR_SYNTAX
;
2839 if ((a
= xmlGetProp (root
, (xmlChar
*) "_name")) == NULL
)
2841 rc
= GPG_ERR_SYNTAX
;
2845 char *tmp
= str_dup ((char *) a
);
2847 int literal
= is_literal_element (&tmp
);
2849 if (!valid_xml_element ((xmlChar
*) tmp
) || literal
)
2852 rc
= GPG_ERR_INV_VALUE
;
2856 if (strv_printf (&path
, "%s", tmp
) == 0)
2859 rc
= GPG_ERR_ENOMEM
;
2864 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 1);
2866 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2868 rc
= GPG_ERR_BAD_DATA
;
2872 /* Overwriting the existing tree. */
2876 xmlFreeNodeList (n
);
2880 xmlSetProp (root
, (xmlChar
*) "_name", (xmlChar
*) path
[0]);
2881 n
= xmlCopyNode (root
, 1);
2882 n
= xmlAddChildList (xmlDocGetRootElement (client
->doc
), n
);
2886 rc
= update_element_mtime (n
->parent
);
2900 import_command (assuan_context_t ctx
, char *line
)
2903 struct client_s
*client
= assuan_get_pointer (ctx
);
2904 unsigned char *result
;
2907 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2909 return send_error (ctx
, rc
);
2911 rc
= do_import (client
, result
);
2912 return send_error (ctx
, rc
);
2916 do_lock (struct client_s
*client
, int add
)
2918 gpg_error_t rc
= lock_file_mutex (client
, add
);
2921 client
->flags
|= FLAG_LOCK_CMD
;
2927 lock_command (assuan_context_t ctx
, char *line
)
2929 struct client_s
*client
= assuan_get_pointer (ctx
);
2930 gpg_error_t rc
= do_lock (client
, 0);
2932 return send_error (ctx
, rc
);
2936 unlock_command (assuan_context_t ctx
, char *line
)
2938 struct client_s
*client
= assuan_get_pointer (ctx
);
2941 rc
= unlock_file_mutex (client
, 0);
2942 return send_error (ctx
, rc
);
2946 option_command (assuan_context_t ctx
, const char *name
, const char *value
)
2948 struct client_s
*client
= assuan_get_pointer (ctx
);
2950 struct pinentry_option_s
*pin_opts
= &client
->pinentry_opts
;
2952 struct agent_s
*agent
= client
->crypto
->agent
;
2955 log_write1 ("OPTION name='%s' value='%s'", name
, value
);
2957 if (strcasecmp (name
, (char *) "log_level") == 0)
2963 l
= strtol (value
, NULL
, 10);
2966 return gpg_error (GPG_ERR_INV_VALUE
);
2969 MUTEX_LOCK (&rcfile_mutex
);
2970 config_set_int_param (&global_config
, "global", "log_level", value
);
2971 MUTEX_UNLOCK (&rcfile_mutex
);
2974 else if (strcasecmp (name
, (char *) "lock-timeout") == 0)
2981 n
= strtol (value
, &p
, 10);
2983 return gpg_error (GPG_ERR_INV_VALUE
);
2986 client
->lock_timeout
= n
;
2989 else if (strcasecmp (name
, (char *) "NAME") == 0)
2991 char *tmp
= pthread_getspecific (thread_name_key
);
2996 if (!value
|| !*value
)
2998 pthread_setspecific (thread_name_key
, str_dup (""));
3002 pthread_setspecific (thread_name_key
, str_dup (value
));
3005 else if (strcasecmp (name
, (char *) "lc-messages") == 0)
3007 xfree (pin_opts
->lc_messages
);
3008 pin_opts
->lc_messages
= str_dup (value
);
3011 rc
= set_agent_option (client
->crypto
->agent
, "lc-messages", value
);
3014 else if (strcasecmp (name
, (char *) "lc-ctype") == 0)
3016 xfree (pin_opts
->lc_ctype
);
3017 pin_opts
->lc_ctype
= str_dup (value
);
3020 rc
= set_agent_option (client
->crypto
->agent
, "lc-ctype", value
);
3023 else if (strcasecmp (name
, (char *) "ttyname") == 0)
3025 xfree (pin_opts
->ttyname
);
3026 pin_opts
->ttyname
= str_dup (value
);
3029 rc
= set_agent_option (client
->crypto
->agent
, "ttyname", value
);
3032 else if (strcasecmp (name
, (char *) "ttytype") == 0)
3034 xfree (pin_opts
->ttytype
);
3035 pin_opts
->ttytype
= str_dup (value
);
3038 rc
= set_agent_option (client
->crypto
->agent
, "ttytype", value
);
3041 else if (strcasecmp (name
, (char *) "display") == 0)
3043 xfree (pin_opts
->display
);
3044 pin_opts
->display
= str_dup (value
);
3047 rc
= set_agent_option (client
->crypto
->agent
, "display", value
);
3050 else if (strcasecmp (name
, (char *) "desc") == 0)
3052 xfree (pin_opts
->desc
);
3053 pin_opts
->desc
= str_dup (value
);
3055 else if (strcasecmp (name
, "pinentry_timeout") == 0)
3063 n
= (int) strtol (value
, &p
, 10);
3066 return gpg_error (GPG_ERR_INV_VALUE
);
3068 pin_opts
->timeout
= n
;
3069 MUTEX_LOCK (&rcfile_mutex
);
3070 config_set_int_param (&global_config
,
3071 client
->filename
? client
->filename
: "global",
3072 "pinentry_timeout", value
);
3073 MUTEX_UNLOCK (&rcfile_mutex
);
3077 return gpg_error (GPG_ERR_UNKNOWN_OPTION
);
3081 if (!rc
&& use_agent
&& agent
)
3083 rc
= pinentry_merge_options (&client
->crypto
->agent
->pinentry_opts
,
3092 do_rename (assuan_context_t ctx
, char *line
)
3094 struct client_s
*client
= assuan_get_pointer (ctx
);
3096 char **req
, **src
, *dst
;
3099 req
= str_split (line
, " ", 0);
3101 if (!req
|| !req
[0] || !req
[1])
3104 return GPG_ERR_SYNTAX
;
3108 is_literal_element (&dst
);
3110 if (!valid_xml_element ((xmlChar
*) dst
))
3113 return GPG_ERR_INV_VALUE
;
3116 if (strchr (req
[0], '\t'))
3117 src
= str_split (req
[0], "\t", 0);
3119 src
= str_split (req
[0], " ", 0);
3123 rc
= GPG_ERR_SYNTAX
;
3127 n
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3129 n
= find_elements (client
->doc
, n
->children
, src
+ 1, &rc
, NULL
, NULL
,
3130 NULL
, 0, 0, NULL
, 0);
3135 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
3138 rc
= GPG_ERR_ENOMEM
;
3142 /* To prevent unwanted effects:
3144 * <root name="a"><b/></root>
3148 if (xmlStrEqual (a
, (xmlChar
*) dst
))
3151 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3161 for (p
= src
; *p
; p
++)
3165 strv_printf (&tmp
, "%s", *p
);
3169 strv_printf (&tmp
, "!%s", dst
);
3170 ndst
= find_root_element (client
->doc
, &tmp
, &rc
, NULL
, 0, 0);
3171 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3178 ndst
= find_elements (client
->doc
, ndst
->children
, tmp
+ 1, &rc
, NULL
,
3179 NULL
, NULL
, 0, 0, NULL
, 0);
3182 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3187 /* Target may exist:
3190 * <root name="b" target="a"/>
3199 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3206 xmlFreeNodeList (ndst
);
3209 rc
= add_attribute (n
, "_name", dst
);
3218 rename_command (assuan_context_t ctx
, char *line
)
3220 struct client_s
*client
= assuan_get_pointer (ctx
);
3222 struct argv_s
*args
[] = {
3223 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3227 rc
= parse_options (&line
, args
, client
);
3229 return send_error (ctx
, rc
);
3231 if (client
->opts
& OPT_INQUIRE
)
3233 unsigned char *result
;
3236 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3238 return send_error (ctx
, rc
);
3240 line
= (char *) result
;
3243 rc
= do_rename (ctx
, line
);
3245 if (client
->opts
& OPT_INQUIRE
)
3248 return send_error (ctx
, rc
);
3252 do_copy (assuan_context_t ctx
, char *line
)
3254 struct client_s
*client
= assuan_get_pointer (ctx
);
3256 char **req
, **src
= NULL
, **dst
= NULL
;
3257 xmlNodePtr nsrc
, ndst
, new = NULL
;
3259 req
= str_split (line
, " ", 0);
3260 if (!req
|| !req
[0] || !req
[1])
3263 return GPG_ERR_SYNTAX
;
3266 if (strchr (req
[0], '\t'))
3267 src
= str_split (req
[0], "\t", 0);
3269 src
= str_split (req
[0], " ", 0);
3273 rc
= GPG_ERR_SYNTAX
;
3277 if (strchr (req
[1], '\t'))
3278 dst
= str_split (req
[1], "\t", 0);
3280 dst
= str_split (req
[1], " ", 0);
3284 rc
= GPG_ERR_SYNTAX
;
3288 if (!valid_element_path (dst
, 0))
3290 rc
= GPG_ERR_INV_VALUE
;
3294 nsrc
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3296 nsrc
= find_elements (client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3297 NULL
, NULL
, 0, 0, NULL
, 0);
3302 new = xmlCopyNodeList (nsrc
);
3305 rc
= GPG_ERR_ENOMEM
;
3310 ndst
= find_root_element (client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3314 ndst
= find_elements (client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3315 NULL
, create_target_elements_cb
, 0, 0, NULL
, 0);
3322 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3326 ndst
= create_element_path (client
, &dst
, &rc
, NULL
);
3331 /* Merge any attributes from the src node to the initial dst node. */
3332 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
)
3334 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3337 xmlAttrPtr a
= xmlHasProp (ndst
, attr
->name
);
3341 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3342 xmlNewProp (ndst
, attr
->name
, tmp
);
3344 rc
= add_attribute (ndst
, NULL
, NULL
);
3347 xmlNodePtr n
= ndst
->children
;
3349 xmlFreeNodeList (n
);
3350 ndst
->children
= NULL
;
3354 n
= xmlCopyNodeList (new->children
);
3357 rc
= GPG_ERR_ENOMEM
;
3361 n
= xmlAddChildList (ndst
, n
);
3364 rc
= GPG_ERR_ENOMEM
;
3368 rc
= update_element_mtime (xmlDocGetRootElement (client
->doc
) ==
3369 ndst
->parent
? ndst
: ndst
->parent
);
3375 xmlUnlinkNode (new);
3376 xmlFreeNodeList (new);
3392 copy_command (assuan_context_t ctx
, char *line
)
3394 struct client_s
*client
= assuan_get_pointer (ctx
);
3396 struct argv_s
*args
[] = {
3397 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3401 rc
= parse_options (&line
, args
, client
);
3403 return send_error (ctx
, rc
);
3405 if (client
->opts
& OPT_INQUIRE
)
3407 unsigned char *result
;
3410 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3412 return send_error (ctx
, rc
);
3414 line
= (char *) result
;
3417 rc
= do_copy (ctx
, line
);
3419 if (client
->opts
& OPT_INQUIRE
)
3422 return send_error (ctx
, rc
);
3426 do_move (assuan_context_t ctx
, char *line
)
3428 struct client_s
*client
= assuan_get_pointer (ctx
);
3430 char **req
, **src
= NULL
, **dst
= NULL
;
3431 xmlNodePtr nsrc
, ndst
= NULL
;
3433 req
= str_split (line
, " ", 0);
3435 if (!req
|| !req
[0] || !req
[1])
3438 return GPG_ERR_SYNTAX
;
3441 if (strchr (req
[0], '\t'))
3442 src
= str_split (req
[0], "\t", 0);
3444 src
= str_split (req
[0], " ", 0);
3448 rc
= GPG_ERR_SYNTAX
;
3452 if (strchr (req
[1], '\t'))
3453 dst
= str_split (req
[1], "\t", 0);
3455 dst
= str_split (req
[1], " ", 0);
3457 nsrc
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3459 nsrc
= find_elements (client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3460 NULL
, NULL
, 0, 0, NULL
, 0);
3467 if (!valid_element_path (dst
, 0))
3469 rc
= GPG_ERR_INV_VALUE
;
3473 ndst
= find_root_element (client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3475 ndst
= find_elements (client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3476 NULL
, NULL
, 0, 0, NULL
, 0);
3479 ndst
= xmlDocGetRootElement (client
->doc
);
3481 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
)
3485 rc
= GPG_ERR_CONFLICT
;
3490 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3497 xmlChar
*a
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3498 xmlNodePtr dup
= find_element (ndst
->children
, (char *) a
, NULL
);
3506 if (ndst
== xmlDocGetRootElement (client
->doc
))
3508 xmlNodePtr n
= nsrc
;
3511 while (n
->parent
&& n
->parent
!= ndst
)
3514 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
3515 xmlChar
*b
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3517 if (xmlStrEqual (a
, b
))
3520 xmlUnlinkNode (nsrc
);
3522 xmlFreeNodeList (n
);
3530 xmlUnlinkNode (dup
);
3531 xmlFreeNodeList (dup
);
3535 xmlUnlinkNode (dup
);
3541 xmlChar
*name
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3543 if (nsrc
->parent
== xmlDocGetRootElement (client
->doc
)
3544 && !strcmp ((char *) name
, *dst
))
3547 rc
= GPG_ERR_CONFLICT
;
3552 ndst
= create_element_path (client
, &dst
, &rc
, nsrc
);
3558 update_element_mtime (nsrc
->parent
);
3559 xmlUnlinkNode (nsrc
);
3560 ndst
= xmlAddChildList (ndst
, nsrc
);
3563 rc
= GPG_ERR_ENOMEM
;
3565 update_element_mtime (ndst
->parent
);
3581 move_command (assuan_context_t ctx
, char *line
)
3583 struct client_s
*client
= assuan_get_pointer (ctx
);
3585 struct argv_s
*args
[] = {
3586 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3590 rc
= parse_options (&line
, args
, client
);
3592 return send_error (ctx
, rc
);
3594 if (client
->opts
& OPT_INQUIRE
)
3596 unsigned char *result
;
3599 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3601 return send_error (ctx
, rc
);
3603 line
= (char *) result
;
3606 rc
= do_move (ctx
, line
);
3608 if (client
->opts
& OPT_INQUIRE
)
3611 return send_error (ctx
, rc
);
3615 ls_command (assuan_context_t ctx
, char *line
)
3618 char *tmp
= str_asprintf ("%s/data", homedir
);
3619 char *dir
= expand_homedir (tmp
);
3620 DIR *d
= opendir (dir
);
3622 rc
= gpg_error_from_syserror ();
3628 return send_error (ctx
, rc
);
3632 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
3633 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
3639 while (!readdir_r (d
, p
, &cur
) && cur
)
3641 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
3643 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.'
3644 && cur
->d_name
[2] == '\0')
3647 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
3654 rc
= GPG_ERR_ENOMEM
;
3666 return send_error (ctx
, rc
);
3669 return send_error (ctx
, GPG_ERR_NO_DATA
);
3671 list
[strlen (list
) - 1] = 0;
3672 rc
= xfer_data (ctx
, list
, strlen (list
));
3674 return send_error (ctx
, rc
);
3678 bye_notify (assuan_context_t ctx
, char *line
)
3680 struct client_s
*cl
= assuan_get_pointer (ctx
);
3683 if (cl
->thd
->remote
)
3689 struct timeval tv
= { 0, 50000 };
3691 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
3692 if (rc
== GNUTLS_E_AGAIN
)
3693 select (0, NULL
, NULL
, NULL
, &tv
);
3695 while (rc
== GNUTLS_E_AGAIN
);
3699 /* This will let assuan_process_next() return. */
3700 fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
);
3701 cl
->last_rc
= 0; // BYE command result
3706 reset_notify (assuan_context_t ctx
, char *line
)
3708 struct client_s
*client
= assuan_get_pointer (ctx
);
3711 cleanup_client (client
);
3717 * This is called before every Assuan command.
3720 command_startup (assuan_context_t ctx
, const char *name
)
3722 struct client_s
*client
= assuan_get_pointer (ctx
);
3724 struct command_table_s
*cmd
= NULL
;
3726 log_write1 ("command='%s'", name
);
3727 client
->last_rc
= client
->opts
= 0;
3729 for (int i
= 0; command_table
[i
]; i
++)
3731 if (!strcasecmp (name
, command_table
[i
]->name
))
3733 if (command_table
[i
]->ignore_startup
)
3735 cmd
= command_table
[i
];
3740 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
3745 * This is called after every Assuan command.
3748 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
3750 struct client_s
*client
= assuan_get_pointer (ctx
);
3752 if (!(client
->flags
& FLAG_LOCK_CMD
))
3753 unlock_file_mutex (client
, 0);
3755 log_write1 (_("command completed: rc=%u"), client
->last_rc
);
3756 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
3760 help_command (assuan_context_t ctx
, char *line
)
3765 if (!line
|| !*line
)
3768 char *help
= str_dup (_("Usage: HELP [<COMMAND>]\n"
3769 "For commands that take an element path as an argument, each element is "
3770 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3773 for (i
= 0; command_table
[i
]; i
++)
3775 if (!command_table
[i
]->help
)
3778 tmp
= str_asprintf ("%s %s", help
, command_table
[i
]->name
);
3783 tmp
= strip_texi_and_wrap (help
);
3785 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3787 return send_error (ctx
, rc
);
3790 for (i
= 0; command_table
[i
]; i
++)
3792 if (!strcasecmp (line
, command_table
[i
]->name
))
3796 if (!command_table
[i
]->help
)
3799 help
= strip_texi_and_wrap (command_table
[i
]->help
);
3800 tmp
= str_asprintf (_("Usage: %s"), help
);
3802 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3804 return send_error (ctx
, rc
);
3808 return send_error (ctx
, GPG_ERR_INV_NAME
);
3812 new_command (const char *name
, int ignore
, int unlock
,
3813 gpg_error_t (*handler
) (assuan_context_t
, char *),
3819 for (i
= 0; command_table
[i
]; i
++);
3822 xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
3823 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
3824 command_table
[i
]->name
= name
;
3825 command_table
[i
]->handler
= handler
;
3826 command_table
[i
]->ignore_startup
= ignore
;
3827 command_table
[i
]->unlock
= unlock
;
3828 command_table
[i
++]->help
= help
;
3829 command_table
[i
] = NULL
;
3837 for (i
= 0; command_table
[i
]; i
++)
3838 xfree (command_table
[i
]);
3840 xfree (command_table
);
3844 sort_commands (const void *arg1
, const void *arg2
)
3846 struct command_table_s
*const *a
= arg1
;
3847 struct command_table_s
*const *b
= arg2
;
3856 return strcmp ((*a
)->name
, (*b
)->name
);
3860 passwd_command (assuan_context_t ctx
, char *line
)
3862 struct client_s
*client
= assuan_get_pointer (ctx
);
3864 struct argv_s
*args
[] = {
3865 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
3866 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
3870 if (client
->flags
& FLAG_NEW
)
3871 return send_error (ctx
, GPG_ERR_INV_STATE
);
3873 client
->crypto
->save
.s2k_count
=
3874 config_get_ulong (client
->filename
, "s2k_count");
3875 rc
= parse_options (&line
, args
, client
);
3877 return send_error (ctx
, rc
);
3879 if (!rc
&& client
->opts
& OPT_RESET
)
3881 rc
= cache_clear (client
->md5file
);
3883 send_status_all (STATUS_CACHE
, NULL
);
3888 if (!IS_PKCS (client
->crypto
))
3890 struct crypto_s
*crypto
;
3892 xfree (client
->crypto
->filename
);
3893 client
->crypto
->filename
= str_dup (client
->filename
);
3894 rc
= change_passwd (ctx
, client
->filename
,
3895 client
->flags
& FLAG_NO_PINENTRY
, &crypto
);
3898 cleanup_crypto (&client
->crypto
);
3899 client
->crypto
= crypto
;
3900 update_checksum (client
);
3901 cleanup_crypto_stage1 (client
->crypto
);
3907 if (client
->crypto
->save
.s2k_count
)
3908 rc
= send_to_agent (client
->crypto
->agent
, NULL
, NULL
,
3909 "OPTION s2k-count=%lu",
3910 client
->crypto
->save
.s2k_count
);
3913 rc
= agent_passwd (client
->crypto
);
3918 return send_error (ctx
, rc
);
3922 parse_keygrip_opt_sign (void *data
, void *value
)
3924 struct client_s
*client
= data
;
3927 client
->opts
|= OPT_SIGN
;
3932 keygrip_command (assuan_context_t ctx
, char *line
)
3934 struct client_s
*client
= assuan_get_pointer (ctx
);
3936 struct crypto_s
*crypto
;
3937 struct argv_s
*args
[] = {
3938 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_keygrip_opt_sign
},
3942 if (!line
|| !*line
)
3943 return send_error (ctx
, GPG_ERR_SYNTAX
);
3945 rc
= parse_options (&line
, args
, client
);
3947 return send_error (ctx
, rc
);
3949 if (!valid_filename (line
))
3950 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3952 rc
= init_client_crypto (&crypto
);
3954 return send_error (ctx
, rc
);
3956 rc
= read_data_file (line
, crypto
);
3959 char *hexgrip
= NULL
;
3961 if (!IS_PKCS (crypto
))
3963 cleanup_crypto (&crypto
);
3964 return send_error (ctx
, GPG_ERR_NOT_SUPPORTED
);
3967 if (client
->opts
& OPT_SIGN
)
3969 if (valid_keygrip (crypto
->sign_grip
, sizeof (crypto
->sign_grip
)))
3970 hexgrip
= bin2hex (crypto
->sign_grip
, sizeof (crypto
->sign_grip
));
3974 hexgrip
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
3977 rc
= GPG_ERR_ENOMEM
;
3979 rc
= xfer_data (ctx
, hexgrip
, strlen (hexgrip
));
3984 cleanup_crypto (&crypto
);
3985 return send_error (ctx
, rc
);
3989 parse_opt_data (void *data
, void *value
)
3991 struct client_s
*client
= data
;
3994 client
->opts
|= OPT_DATA
;
3999 getinfo_command (assuan_context_t ctx
, char *line
)
4001 struct client_s
*client
= assuan_get_pointer (ctx
);
4003 char buf
[ASSUAN_LINELENGTH
];
4004 struct argv_s
*args
[] = {
4005 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4009 rc
= parse_options (&line
, args
, client
);
4011 return send_error (ctx
, rc
);
4013 if (!strcasecmp (line
, "clients"))
4015 if (client
->opts
& OPT_DATA
)
4017 MUTEX_LOCK (&cn_mutex
);
4018 snprintf (buf
, sizeof (buf
), "%i", slist_length (cn_thread_list
));
4019 MUTEX_UNLOCK (&cn_mutex
);
4020 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4023 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4025 else if (!strcasecmp (line
, "cache"))
4027 if (client
->opts
& OPT_DATA
)
4029 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4030 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4033 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4035 else if (!strcasecmp (line
, "pid"))
4038 pid_t pid
= getpid ();
4040 snprintf (buf
, sizeof (buf
), "%i", pid
);
4041 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4043 else if (!strcasecmp (line
, "version"))
4045 char *buf
= str_asprintf ("0x%06x %s", VERSION_HEX
,
4059 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4062 else if (!strcasecmp (line
, "last_error"))
4064 if (client
->last_error
)
4065 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4067 rc
= GPG_ERR_NO_DATA
;
4070 rc
= gpg_error (GPG_ERR_SYNTAX
);
4072 return send_error (ctx
, rc
);
4077 send_data_cb (void *user
, const void *buf
, size_t len
)
4079 assuan_context_t ctx
= user
;
4081 return assuan_send_data (ctx
, buf
, len
);
4085 send_status_cb (void *user
, const char *line
)
4087 assuan_context_t ctx
= user
;
4088 char keyword
[200], *k
;
4091 for (p
= line
, k
= keyword
; *p
; p
++)
4103 while (isspace (*p
))
4106 return assuan_write_status (ctx
, keyword
, *p
? p
: NULL
);
4111 agent_command (assuan_context_t ctx
, char *line
)
4116 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
4119 struct client_s
*client
= assuan_get_pointer (ctx
);
4121 if (!line
|| !*line
)
4122 return send_error (ctx
, GPG_ERR_SYNTAX
);
4124 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 1);
4125 rc
= assuan_transact (client
->crypto
->agent
->ctx
, line
, send_data_cb
,
4126 client
->ctx
, agent_loopback_cb
, client
->crypto
,
4127 send_status_cb
, client
->ctx
);
4128 if (gpg_err_code (rc
) == GPG_ERR_ASS_CANCELED
)
4133 rc
= assuan_write_line (client
->crypto
->agent
->ctx
, "CAN");
4136 rc
= assuan_read_line (client
->crypto
->agent
->ctx
, &line
, &len
);
4138 rc
= gpg_error (GPG_ERR_ASS_CANCELED
);
4142 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 0);
4144 return send_error (ctx
, rc
);
4150 /* !BEGIN-HELP-TEXT!
4152 * This comment is used as a marker to generate the offline documentation
4153 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4154 * script to determine where commands begin and end.
4156 new_command("HELP", 1, 1, help_command
, _(
4157 "HELP [<COMMAND>]\n"
4158 "Show available commands or command specific help text."
4161 new_command("AGENT", 1, 1, agent_command
, _(
4163 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4164 "@command{gpg-agent}."
4167 new_command("GETINFO", 1, 1, getinfo_command
, _(
4168 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4169 "Get server and other information: @var{cache} returns the number of cached "
4170 "documents via a status message. @var{clients} returns the number of "
4171 "connected clients via a status message. @var{pid} returns the process ID "
4172 "number of the server via a data response. @var{VERSION} returns the server "
4173 "version number and compile-time features with a data response with each "
4174 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4175 "the last failed command when available. @xref{Status Messages}. "
4177 "When the @option{--data} option is specified then the result will be send "
4178 "via a data response rather than a status message."
4181 new_command("PASSWD", 0, 0, passwd_command
, _(
4182 "PASSWD [--reset] [--s2k-count=N]\n"
4183 "Changes the passphrase of the secret key required to open the current "
4184 "file. When the @option{--reset} option is passed then the cache entry for "
4185 "the current file will be reset and the passphrase, if any, will be required "
4186 "during the next @code{OPEN}. @xref{OPEN}."
4188 "The @option{--s2k-count} option sets number of hash iterations for a "
4189 "passphrase and must be either @code{0} to use the calibrated count of the "
4190 "machine (the default), or a value greater than or equal to @code{65536}. "
4194 new_command("KEYGRIP", 1, 1, keygrip_command
, _(
4195 "KEYGRIP [--sign] <filename>\n"
4196 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4199 "When the @option{--sign} option is specified then the key used for signing "
4200 "of the specified @var{filename} will be returned."
4203 new_command("OPEN", 1, 1, open_command
, _(
4204 "OPEN [--lock] [--no-pinentry] <filename> [<passphrase>]\n"
4205 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4206 "found on the file-system then a new document will be created. If the file "
4207 "is found, it is looked for in the file cache. If cached and no "
4208 "@var{passphrase} was specified then the cached document is opened. When not "
4209 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4210 "for decryption unless @option{--no-pinentry} was specified (see below)."
4212 "When the @option{--lock} option is passed then the file mutex will be "
4213 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4214 "file has been opened."
4216 "By default, a pinentry is used to retrieve a passphrase when required. "
4217 "Passing @option{--no-pinentry} will disable pinentry use for the rest of "
4218 "the session. When pinentry use is disabled but required for some operation "
4219 "then a server @emph{INQUIRE} will be send to the client to retrieve the "
4220 "passphrase. See the @code{OPTION} command (@pxref{OPTION}), for pinentry "
4224 new_command("SAVE", 0, 0, save_command
, _(
4225 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring [--sign-keygrip=hexstring]]\n"
4226 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4227 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4228 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4229 "keypair will be generated and a pinentry will be used to prompt for the "
4230 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4231 "passed, in which case the data file will not be passphrase protected."
4233 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4234 "passphrase retrieval and caching for new files and behaves as if the "
4235 "@option{--no-agent} commmand line option to @command{pwmd} was specified."
4237 "The @option{--reset} option will clear the cache entry for the current file "
4240 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4241 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4242 "(@pxref{Configuration}) for available ciphers."
4244 "The @option{--cipher-iterations} option specifies the number of times to "
4245 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4247 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4248 "the client to obtain the key paramaters to use when generating a new "
4249 "keypair. The inquired data is expected to be an S-expression. If not "
4250 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4251 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4252 "that when this option is specified a new keypair will be generated "
4253 "reguardless if the file is a new one or not."
4255 "You can encrypt the data file to a public key other than the one that it "
4256 "was originally encrypted with by passing the @option{--keygrip} option with "
4257 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4258 "be of any key that @command{gpg-agent} knows about. The "
4259 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4260 "secret key. This option may be needed when using a smartcard."
4262 "The @option{--s2k-count} option sets number of hash iterations for a "
4263 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4264 "value which is the default. This setting only affects new files. To change "
4265 "the setting, use the @code{PASSWD} command (@pxref{PASSWD})."
4268 new_command("ISCACHED", 1, 0, iscached_command
, _(
4269 "ISCACHED [--lock] <filename>\n"
4270 "An @emph{OK} response is returned if the specified @var{filename} is found "
4271 "in the file cache. If not found in the cache but exists on the filesystem "
4272 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4275 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4276 "file exists; it does not need to be opened nor cached."
4279 new_command("CLEARCACHE", 1, 1, clearcache_command
, _(
4280 "CLEARCACHE [<filename>]\n"
4281 "Clears a file cache entry for all or the specified @var{filename}."
4284 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command
, _(
4285 "CACHETIMEOUT <filename> <seconds>\n"
4286 "The time in @var{seconds} until @var{filename} will be removed from the "
4287 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4288 "the passphrase for each @code{OPEN} command (@pxref{OPEN}). "
4289 "@xref{Configuration}, and the @code{cache_timeout} parameter."
4292 new_command("LIST", 0, 1, list_command
, _(
4293 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4294 "If no element path is given then a newline separated list of root elements "
4295 "is returned with a data response. If given, then all reachable elements "
4296 "of the specified element path are returned unless the @option{--no-recurse} "
4297 "option is specified. If specified, only the child elements of the element "
4298 "path are returned without recursing into grandchildren. Each resulting "
4299 "element is prefixed with the literal @code{!} character when the element "
4300 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4302 "When the @option{--verbose} option is passed then each element path "
4303 "returned will have zero or more flags appened to it. These flags are "
4304 "delimited from the element path by a single space character. A flag itself "
4305 "is a single character. Flag @code{+} indicates that there are child nodes of "
4306 "the current element path. Flag @code{E} indicates that an element of an "
4307 "element path contained in a @var{target} attribute could not be found. Flag "
4308 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4309 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4310 "of a @var{target} attribute (see below)."
4312 "The @option{--with-target} option implies @option{--verbose} and will append "
4313 "an additional flag @code{T} followed by a single space then an element path. "
4314 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4315 "current element when it contains a @var{target} attribute. When no "
4316 "@var{target} attribute is found then no flag will be appended."
4318 "The @option{--no-recurse} option limits the amount of data returned to only "
4319 "the listing of children of the specified element path and not any "
4322 "The @option{--all} option lists the entire element tree for each root "
4323 "element. This option also implies option @option{--verbose}."
4325 "When the @option{--inquire} option is passed then all remaining non-option "
4326 "arguments are retrieved via a server @emph{INQUIRE}."
4329 new_command("REALPATH", 0, 1, realpath_command
, _(
4330 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4331 "Resolves all @code{target} attributes of the specified element path and "
4332 "returns the result with a data response. @xref{Target Attribute}, for details."
4334 "When the @option{--inquire} option is passed then all remaining non-option "
4335 "arguments are retrieved via a server @emph{INQUIRE}."
4338 new_command("STORE", 0, 1, store_command
, _(
4339 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4340 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4342 "Creates a new element path or modifies the @var{content} of an existing "
4343 "element. If only a single element is specified then a new root element is "
4344 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4345 "set to the final @key{TAB} delimited element. If no @var{content} is "
4346 "specified after the final @key{TAB}, then the content of the element will "
4347 "be removed, or empty when creating a new element."
4349 "The only restriction of an element name is that it not contain whitespace "
4350 "or begin with the literal element character @code{!} unless specifying a "
4351 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4352 "the @key{TAB} delimited elements. It is recommended that the content of an "
4353 "element be base64 encoded when it contains control or @key{TAB} characters "
4354 "to prevent @abbr{XML} and @command{pwmd} parsing errors."
4357 new_command("RENAME", 0, 1, rename_command
, _(
4358 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4359 "Renames the specified @var{element} to the new @var{value}. If an element of "
4360 "the same name as the @var{value} already exists it will be overwritten."
4362 "When the @option{--inquire} option is passed then all remaining non-option "
4363 "arguments are retrieved via a server @emph{INQUIRE}."
4366 new_command("COPY", 0, 1, copy_command
, _(
4367 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4368 "Copies the entire element tree starting from the child node of the source "
4369 "element, to the destination element path. If the destination element path "
4370 "does not exist then it will be created; otherwise it is overwritten."
4372 "Note that attributes from the source element are merged into the "
4373 "destination element when the destination element path exists. When an "
4374 "attribute of the same name exists in both the source and destination "
4375 "elements then the destination attribute will be updated to the source "
4378 "When the @option{--inquire} option is passed then all remaining non-option "
4379 "arguments are retrieved via a server @emph{INQUIRE}."
4382 new_command("MOVE", 0, 1, move_command
, _(
4383 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4384 "Moves the source element path to the destination element path. If the "
4385 "destination is not specified then it will be moved to the root node of the "
4386 "document. If the destination is specified and exists then it will be "
4387 "overwritten; otherwise it will be created."
4389 "When the @option{--inquire} option is passed then all remaining non-option "
4390 "arguments are retrieved via a server @emph{INQUIRE}."
4393 new_command("DELETE", 0, 1, delete_command
, _(
4394 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4395 "Removes the specified element path and all of its children. This may break "
4396 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4397 "refers to this element or any of its children."
4399 "When the @option{--inquire} option is passed then all remaining non-option "
4400 "arguments are retrieved via a server @emph{INQUIRE}."
4403 new_command("GET", 0, 1, get_command
, _(
4404 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4405 "Retrieves the content of the specified element. The content is returned "
4406 "with a data response."
4408 "When the @option{--inquire} option is passed then all remaining non-option "
4409 "arguments are retrieved via a server @emph{INQUIRE}."
4412 new_command("ATTR", 0, 1, attr_command
, _(
4413 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4415 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4417 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4418 "element. When no @var{value} is specified any existing value will be removed."
4420 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4422 " Removes an @var{attribute} from an element."
4424 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4426 " Retrieves a newline separated list of attributes names and values "
4427 "from the specified element. Each attribute name and value is space delimited."
4429 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4431 " Retrieves the value of an @var{attribute} from an element."
4434 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4435 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4438 "The @code{_mtime} attribute is updated each time an element is modified by "
4439 "either storing content, editing attributes or by deleting a child element. "
4440 "The @code{_ctime} attribute is created for each new element in an element "
4443 "When the @option{--inquire} option is passed then all remaining non-option "
4444 "arguments are retrieved via a server @emph{INQUIRE}."
4446 "@xref{Target Attribute}, for details about this special attribute."
4449 new_command("XPATH", 0, 1, xpath_command
, _(
4450 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4451 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4452 "specified, it is assumed the expression is a request to return a result. "
4453 "Otherwise, the result is set to the @var{value} argument and the document is "
4454 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4455 "is assumed to be empty and the document is updated. For example:"
4458 "XPATH //element[@@_name='password']@key{TAB}\n"
4461 "would clear the content of all @code{password} elements in the data file "
4462 "while leaving off the trailing @key{TAB} would return all @code{password} "
4463 "elements in @abbr{XML} format."
4465 "When the @option{--inquire} option is passed then all remaining non-option "
4466 "arguments are retrieved via a server @emph{INQUIRE}."
4468 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4469 "expression syntax."
4472 new_command("XPATHATTR", 0, 1, xpathattr_command
, _(
4473 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4474 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4475 "attributes and does not return a result. For the @var{SET} operation the "
4476 "@var{value} is optional but the field is required. If not specified then "
4477 "the attribute value will be empty. For example:"
4480 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4483 "would create an @code{password} attribute for each @code{password} element "
4484 "found in the document. The attribute value will be empty but still exist."
4486 "When the @option{--inquire} option is passed then all remaining non-option "
4487 "arguments are retrieved via a server @emph{INQUIRE}."
4489 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4490 "expression syntax."
4493 new_command("IMPORT", 0, 1, import_command
, _(
4494 "IMPORT <content>[<TAB>[!]element[<TAB>[!]child[..]]]\n"
4495 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4497 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4498 "argument is raw @abbr{XML} data. The content is created as a child of the "
4499 "specified element path and will overwrite an existing element of the same "
4500 "name. If an element of the element path does not exist then it will be "
4503 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4507 new_command("DUMP", 0, 1, dump_command
, _(
4509 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4510 "dumping a specific node."
4513 new_command("LOCK", 0, 0, lock_command
, _(
4515 "Locks the mutex associated with the opened file. This prevents other clients "
4516 "from sending commands to the same opened file until the client "
4517 "that sent this command either disconnects or sends the @code{UNLOCK} "
4518 "command. @xref{UNLOCK}."
4521 new_command("UNLOCK", 1, 0, unlock_command
, _(
4523 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4524 "a commands' @option{lock} option. @xref{LOCK}."
4527 new_command("GETCONFIG", 1, 1, getconfig_command
, _(
4528 "GETCONFIG [filename] <parameter>\n"
4529 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4530 "data response. If no file has been opened then the value for @var{filename} "
4531 "or the default from the @samp{global} section will be returned. If a file "
4532 "has been opened and no @var{filename} is specified, a value previously "
4533 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4536 new_command("OPTION", 1, 1, NULL
, _(
4537 "OPTION <NAME>=<VALUE>\n"
4538 "Sets a client option @var{name} to @var{value}. The value for an option is "
4539 "kept for the duration of the connection."
4543 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4546 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4549 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4552 " Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4555 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4557 "@item LC-MESSAGES\n"
4558 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4561 " Associates the thread ID of the connection with the specified textual "
4562 "representation. Useful for debugging log messages."
4564 "@item LOCK-TIMEOUT\n"
4565 " When not @code{0}, the duration in tenths of a second to wait for the file "
4566 "mutex which has been locked by another thread to be released before returning "
4567 "an error. When @code{-1}, then an error will be returned immediately."
4571 new_command("LS", 1, 1, ls_command
, _(
4573 "Lists the available data files stored in the data directory "
4574 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4577 new_command("RESET", 1, 1, NULL
, _(
4579 "Closes the currently opened file but keeps any previously set client options."
4582 new_command("NOP", 1, 1, NULL
, _(
4584 "Does nothing. Always returns successfully."
4587 /* !END-HELP-TEXT! */
4588 new_command ("CANCEL", 1, 1, NULL
, NULL
);
4589 new_command ("END", 1, 1, NULL
, NULL
);
4590 new_command ("BYE", 1, 1, NULL
, NULL
);
4593 for (i
= 0; command_table
[i
]; i
++);
4594 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
4599 register_commands (assuan_context_t ctx
)
4603 for (; command_table
[i
]; i
++)
4605 if (!command_table
[i
]->handler
)
4608 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
4609 command_table
[i
]->handler
,
4610 command_table
[i
]->help
);
4615 rc
= assuan_register_option_handler (ctx
, option_command
);
4619 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
4623 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
4627 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
4631 return assuan_register_post_cmd_notify (ctx
, command_finalize
);