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 int no_pinentry
= (client
->flags
& FLAG_NO_PINENTRY
);
215 struct pinentry_option_s pin_opts
;
217 struct pinentry_option_s agent_pin_opts
;
220 memcpy (&agent_pin_opts
, &crypto
->agent
->pinentry_opts
,
221 sizeof(struct pinentry_option_s
));
224 memcpy (&pin_opts
, &client
->pinentry_opts
, sizeof(struct pinentry_option_s
));
225 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
226 free_client (client
);
227 memset (client
, 0, sizeof (struct client_s
));
228 client
->crypto
= crypto
;
231 client
->lock_timeout
= lock_timeout
;
232 memcpy (&client
->pinentry_opts
, &pin_opts
, sizeof(struct pinentry_option_s
));
233 client
->flags
|= no_pinentry
? FLAG_NO_PINENTRY
: 0;
235 memcpy (&crypto
->agent
->pinentry_opts
, &agent_pin_opts
,
236 sizeof(struct pinentry_option_s
));
241 open_finalize (assuan_context_t ctx
, char *key
, size_t keylen
)
243 struct client_s
*client
= assuan_get_pointer (ctx
);
245 struct cache_data_s
*cdata
= cache_get_data (client
->md5file
);
246 int cached
= 0, keyarg
= key
? 1 : 0;
248 unsigned char *salted_key
= NULL
;
251 client
->crypto
->filename
= str_dup (client
->filename
);
252 if (cdata
|| client
->flags
& FLAG_NEW
)
254 if (!(client
->flags
& FLAG_NEW
))
256 rc
= decrypt_xml (client
->crypto
, cdata
->doc
, cdata
->doclen
);
261 cached
= cdata
!= NULL
;
265 if (!key
&& !IS_PKCS (client
->crypto
))
267 if (client
->flags
& FLAG_NO_PINENTRY
)
269 rc
= inquire_passphrase (ctx
, "PASSPHRASE", (unsigned char **)&key
,
276 client
->pinentry_opts
.timeout
= config_get_integer (client
->filename
,
278 rc
= getpin_common (client
->ctx
, client
->filename
, PINENTRY_OPEN
,
285 if (!IS_PKCS (client
->crypto
))
287 algo
= cipher_to_gcrypt (client
->crypto
->hdr
.flags
);
288 rc
= hash_key (algo
, client
->crypto
->hdr
.salt
,
289 sizeof(client
->crypto
->hdr
.salt
), key
, keylen
,
290 (void **)&salted_key
, &keysize
);
295 rc
= decrypt_data (client
->ctx
, client
->crypto
, salted_key
, keysize
);
300 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
302 return GPG_ERR_ENOMEM
;
304 cdata
->key
= salted_key
;
305 cdata
->keylen
= keysize
;
308 rc
= decrypt_data (client
->ctx
, client
->crypto
, NULL
, 0);
313 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
316 int timeout
= config_get_integer (client
->filename
,
322 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
324 rc
= encrypt_xml (NULL
, cache_key
, cache_keysize
,
325 GCRY_CIPHER_AES
, client
->crypto
->plaintext
,
326 client
->crypto
->plaintext_len
, &cdata
->doc
,
327 &cdata
->doclen
, &cache_iv
, &cache_blocksize
,
329 if (!rc
&& !(client
->flags
& FLAG_NEW
) && IS_PKCS (client
->crypto
))
331 rc
= gcry_sexp_build ((gcry_sexp_t
*) & cdata
->pubkey
, NULL
,
332 "%S", client
->crypto
->pkey_sexp
);
337 gcry_sexp_release (cdata
->sigkey
);
339 cdata
->sigkey
= NULL
;
341 if (!rc
&& IS_PKCS (client
->crypto
))
342 rc
= gcry_sexp_build ((gcry_sexp_t
*) &cdata
->sigkey
, NULL
,
343 "%S", client
->crypto
->sigpkey_sexp
);
346 && !cache_add_file (client
->md5file
, (client
->flags
& FLAG_NEW
)
347 ? NULL
: client
->crypto
->grip
, cdata
, timeout
))
351 free_cache_data_once (cdata
);
352 xmlFreeDoc (client
->doc
);
359 client
->flags
|= FLAG_OPEN
;
364 update_checksum (client
);
366 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
367 rc
= do_lock (client
, 0);
373 req_cleanup (void *arg
)
378 strv_free ((char **) arg
);
382 parse_open_opt_lock (void *data
, void *value
)
384 struct client_s
*client
= data
;
386 client
->opts
|= OPT_LOCK_ON_OPEN
;
391 parse_opt_inquire (void *data
, void *value
)
393 struct client_s
*client
= data
;
396 client
->opts
|= OPT_INQUIRE
;
401 update_checksum (struct client_s
*client
)
405 struct cache_data_s
*cdata
;
406 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
413 cdata
= cache_get_data (client
->md5file
);
417 cdata
->crc
= xmalloc (len
);
418 memcpy (cdata
->crc
, crc
, len
);
425 validate_checksum (struct client_s
*client
, struct cache_data_s
*cdata
)
432 if (cdata
&& !cdata
->crc
)
433 return GPG_ERR_CHECKSUM
;
435 rc
= get_checksum (client
->filename
, &crc
, &len
);
440 n
= memcmp (cdata
->crc
, crc
, len
);
441 else if (client
->crc
)
442 n
= memcmp (client
->crc
, crc
, len
);
445 return n
? GPG_ERR_CHECKSUM
: 0;
449 do_open (assuan_context_t ctx
, const char *filename
, const char *password
)
451 struct client_s
*client
= assuan_get_pointer (ctx
);
452 struct cache_data_s
*cdata
;
456 if (!valid_filename (filename
))
457 return GPG_ERR_INV_VALUE
;
459 gcry_md_hash_buffer (GCRY_MD_MD5
, client
->md5file
, filename
,
461 client
->filename
= str_dup (filename
);
462 if (!client
->filename
)
463 return GPG_ERR_ENOMEM
;
466 cdata
= cache_get_data (client
->md5file
);
467 if (cdata
&& cdata
->doc
)
471 rc
= validate_checksum (client
, cdata
);
472 /* This will check that the key is cached in the agent which needs to
473 * be determined for files that share a keygrip. */
476 rc
= cache_iscached (client
->filename
, &defer
);
478 rc
= GPG_ERR_KEY_EXPIRED
;
482 if (!rc
&& client
->thd
->remote
483 && config_get_boolean (NULL
, "tcp_require_key"))
484 rc
= GPG_ERR_KEY_EXPIRED
;
487 if (!rc
&& !password
)
489 rc
= read_data_header (client
->filename
, &client
->crypto
->hdr
,
493 if (IS_PKCS (client
->crypto
))
495 gcry_sexp_build (&client
->crypto
->pkey_sexp
, NULL
, "%S",
497 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
498 client
->crypto
->grip
);
499 gcry_sexp_build (&client
->crypto
->sigpkey_sexp
, NULL
, "%S",
501 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
502 client
->crypto
->sign_grip
);
507 rc
= open_finalize (ctx
, NULL
, 0);
513 /* There was an error accessing the file so clear the cache entry. The
514 * real error will be returned from read_data_file() since the file
515 * may have only disappeared. */
518 log_write ("%s: %s", filename
, pwmd_strerror (rc
));
519 rc
= cache_clear (client
->md5file
);
520 send_status_all (STATUS_CACHE
, NULL
);
527 rc
= read_data_file (client
->filename
, client
->crypto
);
530 if (gpg_err_source (rc
) != GPG_ERR_SOURCE_DEFAULT
||
531 gpg_err_code (rc
) != GPG_ERR_ENOENT
)
533 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
537 client
->flags
|= FLAG_NEW
;
538 memset (client
->crypto
->grip
, 0, sizeof (client
->crypto
->grip
));
539 memset (client
->crypto
->sign_grip
, 0,
540 sizeof (client
->crypto
->sign_grip
));
541 rc
= open_finalize (ctx
, NULL
, 0);
545 if (password
&& IS_PKCS (client
->crypto
))
548 rc
= set_agent_passphrase (client
->crypto
, password
, strlen (password
));
554 rc
= open_finalize (ctx
, (char *)password
, password
? strlen (password
) : 0);
559 open_command (assuan_context_t ctx
, char *line
)
562 struct client_s
*client
= assuan_get_pointer (ctx
);
563 char **req
, *password
= NULL
, *filename
;
564 unsigned char md5file
[16];
566 struct argv_s
*args
[] = {
567 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
571 rc
= parse_options (&line
, args
, client
);
573 return send_error (ctx
, rc
);
575 req
= str_split (line
, " ", 2);
577 return send_error (ctx
, GPG_ERR_SYNTAX
);
580 if (client
->thd
->remote
)
582 rc
= tls_validate_access (client
, req
[0]);
585 if (rc
== GPG_ERR_INV_USER_ID
)
586 log_write (_("client validation failed for file '%s'"), req
[0]);
588 return send_error (ctx
, rc
);
591 /* Remote pinentries are not supported. */
592 client
->flags
|= FLAG_NO_PINENTRY
;
596 pthread_cleanup_push (req_cleanup
, req
);
598 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
599 /* This client may have locked a different file with ISCACHED --lock than
600 * the current filename. This will remove that lock. */
601 same_file
= !memcmp (md5file
, client
->md5file
, 16) ? 1 : 0;
602 if (client
->flags
& FLAG_OPEN
||
603 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
605 typeof (client
->opts
) opts
= client
->opts
;
606 typeof (client
->flags
) flags
= client
->flags
;
609 client
->flags
|= FLAG_KEEP_LOCK
;
611 cleanup_client (client
);
613 client
->flags
|= flags
;
614 client
->flags
&= ~(FLAG_LOCK_CMD
| FLAG_NEW
);
616 client
->flags
&= ~(FLAG_HAS_LOCK
);
619 /* Need to lock the mutex here because file_modified() cannot without
620 * knowing the filename. */
621 memcpy (client
->md5file
, md5file
, 16);
622 rc
= lock_file_mutex (client
, 1);
625 password
= req
[1] && *req
[1] ? req
[1] : NULL
;
627 if (IS_PKCS (client
->crypto
))
628 rc
= set_pinentry_mode (client
->crypto
->agent
,
629 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
634 rc
= do_open (ctx
, filename
, password
);
636 cleanup_client (client
);
638 cleanup_crypto_stage1 (client
->crypto
);
642 pthread_cleanup_pop (1);
644 if (!rc
&& client
->flags
& FLAG_NEW
)
645 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
648 (void) kill_scd (client
->crypto
->agent
);
650 return send_error (ctx
, rc
);
654 parse_save_opt_no_passphrase (void *data
, void *value
)
656 struct client_s
*client
= data
;
659 client
->opts
|= OPT_NO_PASSPHRASE
;
664 parse_save_opt_no_agent (void *data
, void *value
)
666 struct client_s
*client
= data
;
668 client
->opts
|= OPT_NO_AGENT
;
673 parse_save_opt_cipher (void *data
, void *value
)
675 struct client_s
*client
= data
;
676 int algo
= cipher_string_to_gcrypt ((char *) value
);
677 file_header_t
*hdr
= &client
->crypto
->save
.hdr
;
680 return GPG_ERR_INV_VALUE
;
682 hdr
->flags
= set_cipher_flag (hdr
->flags
, algo
);
687 parse_save_opt_keygrip (void *data
, void *value
)
689 struct client_s
*client
= data
;
691 if (!IS_PKCS (client
->crypto
))
692 return GPG_ERR_INV_ARG
;
695 if (client
->crypto
->save
.pkey
)
696 gcry_sexp_release (client
->crypto
->save
.pkey
);
698 client
->crypto
->save
.pkey
= NULL
;
699 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.pkey
);
701 return GPG_ERR_INV_ARG
;
706 parse_save_opt_sign_keygrip (void *data
, void *value
)
708 struct client_s
*client
= data
;
710 if (!IS_PKCS (client
->crypto
))
711 return GPG_ERR_INV_ARG
;
714 if (client
->crypto
->save
.sigpkey
)
715 gcry_sexp_release (client
->crypto
->save
.sigpkey
);
717 client
->crypto
->save
.sigpkey
= NULL
;
718 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.sigpkey
);
720 return GPG_ERR_INV_ARG
;
725 parse_opt_s2k_count (void *data
, void *value
)
727 struct client_s
*client
= data
;
731 return GPG_ERR_INV_VALUE
;
733 client
->crypto
->save
.s2k_count
= strtoul (v
, NULL
, 10);
738 parse_save_opt_iterations (void *data
, void *value
)
740 struct client_s
*client
= data
;
745 return GPG_ERR_INV_VALUE
;
748 n
= strtoull (v
, &p
, 10);
749 if (n
== UINT64_MAX
&& errno
)
750 return gpg_error_from_syserror ();
752 return GPG_ERR_INV_VALUE
;
754 client
->crypto
->save
.hdr
.iterations
= n
;
759 save_finalize (assuan_context_t ctx
)
761 struct client_s
*client
= assuan_get_pointer (ctx
);
763 xmlChar
*xmlbuf
= NULL
;
768 xmlDocDumpFormatMemory (client
->doc
, &xmlbuf
, &xmlbuflen
, 0);
770 return GPG_ERR_ENOMEM
;
772 pthread_cleanup_push (xmlFree
, xmlbuf
);
774 if (!use_agent
|| ((client
->flags
& FLAG_NEW
)
775 && (client
->opts
& OPT_NO_AGENT
))
776 || !(client
->crypto
->hdr
.flags
& PWMD_FLAG_PKCS
))
778 rc
= export_common (ctx
, client
->flags
& FLAG_NO_PINENTRY
,
779 client
->crypto
, xmlbuf
, xmlbuflen
, client
->filename
,
780 NULL
, &key
, &keylen
, 1, 1);
785 gcry_sexp_t pubkey
= client
->crypto
->save
.pkey
;
786 gcry_sexp_t sigkey
= client
->crypto
->save
.sigpkey
;
789 pubkey
= client
->crypto
->pkey_sexp
;
793 sigkey
= client
->crypto
->sigpkey_sexp
;
796 gcry_sexp_build (&client
->crypto
->save
.sigpkey
, 0, "%S", pubkey
);
797 sigkey
= client
->crypto
->save
.sigpkey
;
801 rc
= encrypt_data_file (client
->ctx
, client
->crypto
, pubkey
, sigkey
,
802 client
->filename
, xmlbuf
, xmlbuflen
);
803 if (pubkey
== client
->crypto
->save
.pkey
)
807 gcry_sexp_release (client
->crypto
->pkey_sexp
);
808 client
->crypto
->pkey_sexp
= client
->crypto
->save
.pkey
;
809 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
810 client
->crypto
->grip
);
813 gcry_sexp_release (pubkey
);
815 client
->crypto
->save
.pkey
= NULL
;
819 gcry_pk_get_keygrip (sigkey
, client
->crypto
->sign_grip
);
821 if (sigkey
== client
->crypto
->save
.sigpkey
)
825 if (client
->crypto
->sigpkey_sexp
)
826 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
828 client
->crypto
->sigpkey_sexp
= client
->crypto
->save
.sigpkey
;
829 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
830 client
->crypto
->sign_grip
);
833 gcry_sexp_release (sigkey
);
835 client
->crypto
->save
.sigpkey
= NULL
;
844 rc
= save_common (client
->filename
, client
->crypto
, xmlbuf
, xmlbuflen
,
845 key
, keylen
, &cached
, client
->opts
& OPT_NO_AGENT
);
849 if (!rc
&& (!cached
|| (client
->flags
& FLAG_NEW
)))
850 send_status_all (STATUS_CACHE
, NULL
);
854 update_checksum (client
);
855 client
->flags
&= ~(FLAG_NEW
);
861 pthread_cleanup_pop (1); // xmlFree
866 parse_opt_reset (void *data
, void *value
)
868 struct client_s
*client
= data
;
871 client
->opts
|= OPT_RESET
;
876 save_command (assuan_context_t ctx
, char *line
)
878 struct client_s
*client
= assuan_get_pointer (ctx
);
881 struct argv_s
*args
[] = {
882 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
,
883 parse_save_opt_no_passphrase
},
884 &(struct argv_s
) {"cipher", OPTION_TYPE_ARG
, parse_save_opt_cipher
},
885 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
887 &(struct argv_s
) {"keygrip", OPTION_TYPE_ARG
, parse_save_opt_keygrip
},
888 &(struct argv_s
) {"sign-keygrip", OPTION_TYPE_ARG
,
889 parse_save_opt_sign_keygrip
},
890 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
891 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
892 &(struct argv_s
) {"cipher-iterations", OPTION_TYPE_ARG
,
893 parse_save_opt_iterations
},
894 &(struct argv_s
) {"no-agent", OPTION_TYPE_NOARG
,
895 parse_save_opt_no_agent
},
899 cleanup_save (&client
->crypto
->save
);
900 memcpy (&client
->crypto
->save
.hdr
, &client
->crypto
->hdr
,
901 sizeof (file_header_t
));
902 client
->crypto
->save
.s2k_count
=
903 config_get_ulong (client
->filename
, "s2k_count");
905 if (client
->flags
& FLAG_NEW
)
906 client
->crypto
->save
.hdr
.iterations
=
907 config_get_ulonglong (client
->filename
, "cipher_iterations");
909 rc
= parse_options (&line
, args
, client
);
911 return send_error (ctx
, rc
);
913 if (!(client
->flags
& FLAG_NEW
))
914 client
->opts
&= ~OPT_NO_AGENT
;
915 else if (client
->opts
& OPT_NO_AGENT
)
917 client
->crypto
->save
.hdr
.flags
&= ~PWMD_FLAG_PKCS
;
918 client
->crypto
->hdr
.flags
&= ~PWMD_FLAG_PKCS
;
921 if ((client
->opts
& OPT_NO_PASSPHRASE
) && !(client
->flags
& FLAG_NEW
)
922 && !(client
->opts
& OPT_INQUIRE
))
923 return send_error (ctx
, GPG_ERR_WRONG_KEY_USAGE
);
925 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
926 return send_error (ctx
, gpg_error_from_syserror ());
928 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
930 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
931 return send_error (ctx
, GPG_ERR_ENOANO
);
935 rc
= cache_iscached (client
->filename
, &defer
);
938 log_write ("%s: %s", client
->filename
,
939 pwmd_strerror (GPG_ERR_KEY_EXPIRED
));
940 client
->opts
|= OPT_RESET
;
943 if (client
->opts
& OPT_RESET
)
945 rc
= cache_clear (client
->md5file
);
947 return send_error (ctx
, rc
);
949 send_status_all (STATUS_CACHE
, NULL
);
952 rc
= update_element_mtime (xmlDocGetRootElement (client
->doc
));
954 return send_error (ctx
, rc
);
957 if (!rc
&& use_agent
&& !client
->crypto
->save
.pkey
&&
958 (client
->flags
& FLAG_NEW
|| client
->opts
& OPT_INQUIRE
)
959 && !(client
->opts
& OPT_NO_AGENT
))
961 rc
= send_status (client
->ctx
, STATUS_GENKEY
, NULL
);
964 struct inquire_data_s idata
= { 0 };
965 char *params
= client
->opts
& OPT_INQUIRE
? NULL
966 : default_key_params (client
->crypto
);
968 pthread_cleanup_push (xfree
, params
);
969 idata
.crypto
= client
->crypto
;
974 idata
.len
= strlen (params
);
979 client
->crypto
->agent
->inquire_data
= &idata
;
980 client
->crypto
->agent
->inquire_cb
= NULL
;
981 rc
= generate_key (client
->crypto
, params
,
982 (client
->opts
& OPT_NO_PASSPHRASE
), 1);
983 pthread_cleanup_pop (1);
989 rc
= save_finalize (ctx
);
991 cleanup_crypto_stage1 (client
->crypto
);
993 (void) kill_scd (client
->crypto
->agent
);
995 return send_error (ctx
, rc
);
999 do_delete (assuan_context_t ctx
, char *line
)
1001 struct client_s
*client
= assuan_get_pointer (ctx
);
1006 if (strchr (line
, '\t'))
1007 req
= str_split (line
, "\t", 0);
1009 req
= str_split (line
, " ", 0);
1012 return GPG_ERR_SYNTAX
;
1014 n
= find_root_element (client
->doc
, &req
, &rc
, NULL
, 0, 0);
1022 * No sub-node defined. Remove the entire node (root element).
1028 rc
= unlink_node (n
);
1037 find_elements (client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
1045 rc
= unlink_node (n
);
1053 delete_command (assuan_context_t ctx
, char *line
)
1055 struct client_s
*client
= assuan_get_pointer (ctx
);
1057 struct argv_s
*args
[] = {
1058 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1062 rc
= parse_options (&line
, args
, client
);
1064 return send_error (ctx
, rc
);
1066 if (client
->opts
& OPT_INQUIRE
)
1068 unsigned char *result
;
1071 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1073 return send_error (ctx
, rc
);
1075 line
= (char *) result
;
1078 rc
= do_delete (ctx
, line
);
1080 if (client
->opts
& OPT_INQUIRE
)
1083 return send_error (ctx
, rc
);
1087 store_command (assuan_context_t ctx
, char *line
)
1089 struct client_s
*client
= assuan_get_pointer (ctx
);
1092 unsigned char *result
;
1094 xmlNodePtr n
, parent
;
1096 char *content
= NULL
;
1098 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1100 return send_error (ctx
, rc
);
1102 req
= str_split ((char *) result
, "\t", 0);
1106 return send_error (ctx
, GPG_ERR_SYNTAX
);
1108 len
= strv_length (req
);
1109 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1110 if (*(req
+ 1) && !valid_element_path (req
, has_content
))
1113 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1116 if (has_content
|| !*req
[len
- 1])
1119 content
= req
[len
- 1];
1120 req
[len
- 1] = NULL
;
1124 n
= find_root_element (client
->doc
, &req
, &rc
, NULL
, 0, 0);
1125 if (rc
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1127 rc
= new_root_element (client
->doc
, *req
);
1131 return send_error (ctx
, GPG_ERR_SYNTAX
);
1140 return send_error (ctx
, rc
);
1145 if (req
[1] && *req
[1])
1148 parent
= create_elements_cb (n
, req
+ 1, &rc
, NULL
);
1150 parent
= find_elements (client
->doc
, n
->children
, req
+ 1, &rc
,
1151 NULL
, NULL
, create_elements_cb
, 0, 0, NULL
,
1157 n
= find_text_node (parent
->children
);
1159 xmlNodeSetContent (n
, (xmlChar
*) content
);
1161 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1163 update_element_mtime (parent
);
1168 return send_error (ctx
, rc
);
1172 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1177 int progress
= config_get_integer ("global", "xfer_progress");
1181 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1182 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1183 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1191 if (sent
+ to_send
> total
)
1192 to_send
= total
- sent
;
1194 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1195 flush
? 0 : to_send
);
1198 sent
+= flush
? 0 : to_send
;
1200 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1201 (sent
== total
&& flush
))
1202 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1204 if (!flush
&& !rc
&& sent
== total
)
1211 while (!rc
&& sent
< total
);
1217 do_get (assuan_context_t ctx
, char *line
)
1219 struct client_s
*client
= assuan_get_pointer (ctx
);
1224 req
= str_split (line
, "\t", 0);
1229 return GPG_ERR_SYNTAX
;
1232 n
= find_root_element (client
->doc
, &req
, &rc
, NULL
, 0, 0);
1241 find_elements (client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
1248 if (!n
|| !n
->children
)
1249 return GPG_ERR_NO_DATA
;
1251 n
= find_text_node (n
->children
);
1252 if (!n
|| !n
->content
|| !*n
->content
)
1253 return GPG_ERR_NO_DATA
;
1255 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1260 get_command (assuan_context_t ctx
, char *line
)
1262 struct client_s
*client
= assuan_get_pointer (ctx
);
1264 struct argv_s
*args
[] = {
1265 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1269 rc
= parse_options (&line
, args
, client
);
1271 return send_error (ctx
, rc
);
1273 if (client
->opts
& OPT_INQUIRE
)
1275 unsigned char *result
;
1278 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1280 return send_error (ctx
, rc
);
1282 line
= (char *) result
;
1285 rc
= do_get (ctx
, line
);
1287 if (client
->opts
& OPT_INQUIRE
)
1290 return send_error (ctx
, rc
);
1293 static void list_command_cleanup1 (void *arg
);
1295 realpath_command (assuan_context_t ctx
, char *line
)
1297 struct string_s
*string
= NULL
;
1299 struct client_s
*client
= assuan_get_pointer (ctx
);
1300 struct argv_s
*args
[] = {
1301 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1305 rc
= parse_options (&line
, args
, client
);
1307 return send_error (ctx
, rc
);
1309 if (client
->opts
& OPT_INQUIRE
)
1311 unsigned char *result
;
1314 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1316 return send_error (ctx
, rc
);
1318 line
= (char *) result
;
1321 rc
= build_realpath (client
->doc
, line
, &string
);
1324 pthread_cleanup_push (list_command_cleanup1
, string
);
1325 rc
= xfer_data (ctx
, string
->str
, string
->len
);
1326 pthread_cleanup_pop (1);
1329 if (client
->opts
& OPT_INQUIRE
)
1332 return send_error (ctx
, rc
);
1336 list_command_cleanup1 (void *arg
)
1339 string_free ((struct string_s
*) arg
, 1);
1343 list_command_cleanup2 (void *arg
)
1345 struct element_list_s
*elements
= arg
;
1351 int total
= slist_length (elements
->list
);
1354 for (i
= 0; i
< total
; i
++)
1356 char *tmp
= slist_nth_data (elements
->list
, i
);
1360 slist_free (elements
->list
);
1363 if (elements
->prefix
)
1364 xfree (elements
->prefix
);
1367 strv_free (elements
->req
);
1374 parse_list_opt_norecurse (void *data
, void *value
)
1376 struct client_s
*client
= data
;
1378 client
->opts
&= ~(OPT_LIST_RECURSE
);
1383 parse_list_opt_verbose (void *data
, void *value
)
1385 struct client_s
*client
= data
;
1387 client
->opts
|= OPT_LIST_VERBOSE
;
1392 parse_list_opt_target (void *data
, void *value
)
1394 struct client_s
*client
= data
;
1396 client
->opts
|= OPT_LIST_WITH_TARGET
;
1401 parse_list_opt_all (void *data
, void *value
)
1403 struct client_s
*client
= data
;
1405 client
->opts
|= OPT_LIST_ALL
;
1410 list_path_once (struct client_s
*client
, char *line
,
1411 struct element_list_s
*elements
, struct string_s
*result
)
1415 elements
->req
= str_split (line
, " ", 0);
1417 strv_printf (&elements
->req
, "%s", line
);
1419 rc
= create_path_list (client
->doc
, elements
, *elements
->req
);
1420 if (rc
== GPG_ERR_ELOOP
&& elements
->verbose
)
1427 int total
= slist_length (elements
->list
);
1431 rc
= GPG_ERR_NO_DATA
;
1437 for (i
= 0; i
< total
; i
++)
1439 char *tmp
= slist_nth_data (elements
->list
, i
);
1441 string_append_printf (result
, "%s%s", tmp
,
1442 i
+ 1 == total
? "" : "\n");
1448 rc
= GPG_ERR_NO_DATA
;
1455 has_list_flag (char *path
, char *flags
)
1459 while (*p
&& *++p
!= ' ');
1468 for (f
= flags
; *f
&& *f
!= ' '; f
++)
1479 do_list (assuan_context_t ctx
, char *line
)
1481 struct client_s
*client
= assuan_get_pointer (ctx
);
1483 struct element_list_s
*elements
= NULL
;
1485 elements
= xcalloc (1, sizeof (struct element_list_s
));
1487 return GPG_ERR_ENOMEM
;
1489 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1491 (client
->opts
& OPT_LIST_VERBOSE
) | (client
->opts
& OPT_LIST_WITH_TARGET
);
1492 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1494 if (!line
|| !*line
)
1496 struct string_s
*str
= NULL
;
1498 pthread_cleanup_push (list_command_cleanup2
, elements
);
1499 rc
= list_root_elements (client
->doc
, &str
, elements
->verbose
,
1500 elements
->with_target
);
1501 pthread_cleanup_pop (1);
1502 pthread_cleanup_push (list_command_cleanup1
, str
);
1506 if (client
->opts
& OPT_LIST_ALL
)
1508 char **roots
= str_split (str
->str
, "\n", 0);
1511 pthread_cleanup_push (req_cleanup
, roots
);
1512 string_truncate (str
, 0);
1514 for (p
= roots
; *p
; p
++)
1516 if (strchr (*p
, ' '))
1518 if (has_list_flag (*p
, "EO"))
1520 string_append_printf (str
, "%s%s", *p
,
1521 *(p
+ 1) ? "\n" : "");
1526 elements
= xcalloc (1, sizeof (struct element_list_s
));
1529 rc
= GPG_ERR_ENOMEM
;
1533 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1535 (client
->opts
& OPT_LIST_VERBOSE
) | (client
->opts
&
1536 OPT_LIST_WITH_TARGET
);
1537 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1538 pthread_cleanup_push (list_command_cleanup2
, elements
);
1539 rc
= list_path_once (client
, *p
, elements
, str
);
1540 pthread_cleanup_pop (1);
1545 string_append (str
, "\n");
1548 pthread_cleanup_pop (1);
1552 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1555 pthread_cleanup_pop (1);
1559 pthread_cleanup_push (list_command_cleanup2
, elements
);
1560 struct string_s
*str
= string_new (NULL
);
1561 pthread_cleanup_push (list_command_cleanup1
, str
);
1562 rc
= list_path_once (client
, line
, elements
, str
);
1564 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1566 pthread_cleanup_pop (1);
1567 pthread_cleanup_pop (1);
1572 list_command (assuan_context_t ctx
, char *line
)
1574 struct client_s
*client
= assuan_get_pointer (ctx
);
1576 struct argv_s
*args
[] = {
1577 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
,
1578 parse_list_opt_norecurse
},
1579 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_list_opt_verbose
},
1580 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1581 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
,
1582 parse_list_opt_target
},
1583 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_all
},
1587 if (disable_list_and_dump
== 1)
1588 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1590 client
->opts
|= OPT_LIST_RECURSE
;
1591 rc
= parse_options (&line
, args
, client
);
1593 return send_error (ctx
, rc
);
1595 if (client
->opts
& OPT_LIST_ALL
)
1596 client
->opts
|= OPT_LIST_RECURSE
| OPT_LIST_VERBOSE
;
1598 if (client
->opts
& OPT_INQUIRE
)
1600 unsigned char *result
;
1603 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1605 return send_error (ctx
, rc
);
1607 line
= (char *) result
;
1610 rc
= do_list (ctx
, line
);
1612 if (client
->opts
& OPT_INQUIRE
)
1615 return send_error (ctx
, rc
);
1619 * req[0] - element path
1622 attribute_list (assuan_context_t ctx
, char **req
)
1624 struct client_s
*client
= assuan_get_pointer (ctx
);
1625 char **attrlist
= NULL
;
1633 if (!req
|| !req
[0])
1634 return GPG_ERR_SYNTAX
;
1636 if ((path
= str_split (req
[0], "\t", 0)) == NULL
)
1639 * The first argument may be only a root element.
1641 if ((path
= str_split (req
[0], " ", 0)) == NULL
)
1642 return GPG_ERR_SYNTAX
;
1645 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
1655 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
1656 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1667 for (a
= n
->properties
; a
; a
= a
->next
)
1671 if ((pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *))) == NULL
)
1674 strv_free (attrlist
);
1676 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1677 pwmd_strerror (GPG_ERR_ENOMEM
));
1678 return GPG_ERR_ENOMEM
;
1683 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
,
1685 && an
->content
? (char *) an
->content
: "");
1689 strv_free (attrlist
);
1690 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1691 pwmd_strerror (GPG_ERR_ENOMEM
));
1692 return GPG_ERR_ENOMEM
;
1695 attrlist
[++i
] = NULL
;
1699 return GPG_ERR_NO_DATA
;
1701 line
= strv_join ("\n", attrlist
);
1705 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1706 pwmd_strerror (GPG_ERR_ENOMEM
));
1707 strv_free (attrlist
);
1708 return GPG_ERR_ENOMEM
;
1711 pthread_cleanup_push (xfree
, line
);
1712 pthread_cleanup_push (req_cleanup
, attrlist
);
1713 rc
= xfer_data (ctx
, line
, strlen (line
));
1714 pthread_cleanup_pop (1);
1715 pthread_cleanup_pop (1);
1720 * req[0] - attribute
1721 * req[1] - element path
1724 attribute_delete (struct client_s
*client
, char **req
)
1730 if (!req
|| !req
[0] || !req
[1])
1731 return GPG_ERR_SYNTAX
;
1733 if (!strcmp (req
[0], "_name"))
1734 return GPG_ERR_INV_ATTR
;
1736 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1739 * The first argument may be only a root element.
1741 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1742 return GPG_ERR_SYNTAX
;
1745 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
1751 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
1752 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1757 rc
= delete_attribute (n
, (xmlChar
*) req
[0]);
1765 create_element_path (struct client_s
*client
,
1766 char ***elements
, gpg_error_t
* rc
, xmlNodePtr parent
)
1768 char **req
= *elements
;
1769 char **req_orig
= strv_dup (req
);
1770 xmlNodePtr n
= NULL
;
1776 *rc
= GPG_ERR_ENOMEM
;
1777 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1778 pwmd_strerror (GPG_ERR_ENOMEM
));
1783 n
= find_root_element (client
->doc
, &req
, rc
, NULL
, 0, 0);
1786 if (*rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1789 *rc
= new_root_element (client
->doc
, req
[0]);
1795 else if (n
== parent
)
1797 *rc
= GPG_ERR_CONFLICT
;
1804 n
= create_target_elements_cb (n
, req
+ 1, rc
, NULL
);
1806 n
= find_elements (client
->doc
, n
->children
, req
+ 1, rc
, NULL
, NULL
,
1807 create_target_elements_cb
, 0, 0, parent
, 0);
1813 * Reset the position of the element tree now that the elements
1814 * have been created.
1819 n
= find_root_element (client
->doc
, &req
, rc
, NULL
, 0, 0);
1823 n
= find_elements (client
->doc
, n
->children
, req
+ 1, rc
,
1824 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1831 strv_free (req_orig
);
1838 * Creates a "target" attribute. When other commands encounter an element with
1839 * this attribute, the element path is modified to the target value. If the
1840 * source element path doesn't exist when using 'ATTR SET target', it is
1841 * created, but the destination element path must exist.
1843 * req[0] - source element path
1844 * req[1] - destination element path
1847 target_attribute (struct client_s
*client
, char **req
)
1849 char **src
, **dst
, *line
= NULL
, **odst
= NULL
;
1853 if (!req
|| !req
[0] || !req
[1])
1854 return GPG_ERR_SYNTAX
;
1856 if ((src
= str_split (req
[0], "\t", 0)) == NULL
)
1859 * The first argument may be only a root element.
1861 if ((src
= str_split (req
[0], " ", 0)) == NULL
)
1862 return GPG_ERR_SYNTAX
;
1865 if (!valid_element_path (src
, 0))
1866 return GPG_ERR_INV_VALUE
;
1868 if ((dst
= str_split (req
[1], "\t", 0)) == NULL
)
1871 * The first argument may be only a root element.
1873 if ((dst
= str_split (req
[1], " ", 0)) == NULL
)
1875 rc
= GPG_ERR_SYNTAX
;
1880 odst
= strv_dup (dst
);
1883 rc
= GPG_ERR_ENOMEM
;
1887 n
= find_root_element (client
->doc
, &dst
, &rc
, NULL
, 0, 0);
1889 * Make sure the destination element path exists.
1896 n
= find_elements (client
->doc
, n
->children
, dst
+ 1, &rc
,
1897 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1902 rc
= validate_target_attribute (client
->doc
, req
[0], n
);
1903 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1906 n
= create_element_path (client
, &src
, &rc
, NULL
);
1910 line
= strv_join ("\t", odst
);
1913 rc
= GPG_ERR_ENOMEM
;
1917 rc
= add_attribute (n
, "target", line
);
1928 * req[0] - attribute
1929 * req[1] - element path
1932 attribute_get (assuan_context_t ctx
, char **req
)
1934 struct client_s
*client
= assuan_get_pointer (ctx
);
1940 if (!req
|| !req
[0] || !req
[1])
1941 return GPG_ERR_SYNTAX
;
1943 if (strchr (req
[1], '\t'))
1945 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1946 return GPG_ERR_SYNTAX
;
1950 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1951 return GPG_ERR_SYNTAX
;
1954 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
1961 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
1962 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1970 if ((a
= xmlGetProp (n
, (xmlChar
*) req
[0])) == NULL
)
1971 return GPG_ERR_NOT_FOUND
;
1973 pthread_cleanup_push (xmlFree
, a
);
1976 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
1978 rc
= GPG_ERR_NO_DATA
;
1980 pthread_cleanup_pop (1);
1989 * req[0] - attribute
1990 * req[1] - element path
1994 attribute_set (struct client_s
*client
, char **req
)
2000 if (!req
|| !req
[0] || !req
[1])
2001 return GPG_ERR_SYNTAX
;
2004 * Reserved attribute names.
2006 if (!strcmp (req
[0], "_name"))
2007 return GPG_ERR_INV_ATTR
;
2008 else if (!strcmp (req
[0], "target"))
2009 return target_attribute (client
, req
+ 1);
2011 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2014 * The first argument may be only a root element.
2016 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2017 return GPG_ERR_SYNTAX
;
2020 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
2027 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
2028 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2034 rc
= add_attribute (n
, req
[0], req
[2]);
2043 * req[1] - attribute name or element path if command is LIST
2044 * req[2] - element path
2045 * req[2] - element path or value
2049 do_attr (assuan_context_t ctx
, char *line
)
2051 struct client_s
*client
= assuan_get_pointer (ctx
);
2055 req
= str_split (line
, " ", 4);
2056 if (!req
|| !req
[0] || !req
[1])
2059 return GPG_ERR_SYNTAX
;
2062 pthread_cleanup_push (req_cleanup
, req
);
2064 if (strcasecmp (req
[0], "SET") == 0)
2065 rc
= attribute_set (client
, req
+ 1);
2066 else if (strcasecmp (req
[0], "GET") == 0)
2067 rc
= attribute_get (ctx
, req
+ 1);
2068 else if (strcasecmp (req
[0], "DELETE") == 0)
2069 rc
= attribute_delete (client
, req
+ 1);
2070 else if (strcasecmp (req
[0], "LIST") == 0)
2071 rc
= attribute_list (ctx
, req
+ 1);
2073 rc
= GPG_ERR_SYNTAX
;
2075 pthread_cleanup_pop (1);
2080 attr_command (assuan_context_t ctx
, char *line
)
2082 struct client_s
*client
= assuan_get_pointer (ctx
);
2084 struct argv_s
*args
[] = {
2085 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2089 rc
= parse_options (&line
, args
, client
);
2091 return send_error (ctx
, rc
);
2093 if (client
->opts
& OPT_INQUIRE
)
2095 unsigned char *result
;
2098 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2100 return send_error (ctx
, rc
);
2102 line
= (char *) result
;
2105 rc
= do_attr (ctx
, line
);
2107 if (client
->opts
& OPT_INQUIRE
)
2110 return send_error (ctx
, rc
);
2114 parse_iscached_opt_lock (void *data
, void *value
)
2116 struct client_s
*client
= data
;
2119 client
->opts
|= OPT_LOCK
;
2124 iscached_command (assuan_context_t ctx
, char *line
)
2126 struct client_s
*client
= assuan_get_pointer (ctx
);
2128 unsigned char md5file
[16];
2129 struct argv_s
*args
[] = {
2130 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2134 if (!line
|| !*line
)
2135 return send_error (ctx
, GPG_ERR_SYNTAX
);
2137 rc
= parse_options (&line
, args
, client
);
2139 return send_error (ctx
, rc
);
2140 else if (!valid_filename (line
))
2141 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2143 rc
= cache_iscached (line
, NULL
);
2144 if (client
->opts
& OPT_LOCK
2145 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2147 gpg_error_t trc
= rc
;
2148 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2149 if (memcmp (md5file
, client
->md5file
, 16))
2150 cleanup_client (client
);
2152 memcpy (client
->md5file
, md5file
, 16);
2153 rc
= do_lock (client
, 1);
2158 return send_error (ctx
, rc
);
2162 clearcache_command (assuan_context_t ctx
, char *line
)
2164 gpg_error_t rc
= 0, all_rc
= 0;
2165 unsigned char md5file
[16];
2169 struct client_thread_s
*once
= NULL
;
2172 MUTEX_LOCK (&cn_mutex
);
2173 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
2175 if (!line
|| !*line
)
2178 t
= slist_length (cn_thread_list
);
2180 for (i
= 0; i
< t
; i
++)
2182 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2187 /* Lock each connected clients' file mutex to prevent any other client
2188 * from accessing the cache entry (the file mutex is locked upon
2189 * command startup). The cache for the entry is not cleared if the
2190 * file mutex is locked by another client to prevent this function
2195 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
2196 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2198 if (pthread_equal (pthread_self (), thd
->tid
))
2202 if (!thd
->cl
->filename
||
2203 cache_iscached (thd
->cl
->filename
,
2204 NULL
) == GPG_ERR_NO_DATA
)
2210 cache_defer_clear (thd
->cl
->md5file
);
2213 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2221 rc
= cache_clear (thd
->cl
->md5file
);
2225 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2232 /* A single data filename was specified. Lock only this data file
2233 * mutex and free the cache entry. */
2236 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2238 if (!memcmp (thd
->cl
->md5file
, md5file
, sizeof (md5file
)))
2240 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0,
2242 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2244 if (pthread_equal (pthread_self (), thd
->tid
))
2251 rc
= cache_clear (thd
->cl
->md5file
);
2252 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2256 cache_defer_clear (thd
->cl
->md5file
);
2264 /* Only connected clients' cache entries have been cleared. Now clear any
2265 * remaining cache entries without clients but only if there wasn't an
2266 * error from above since this would defeat the locking check of the
2267 * remaining entries. */
2273 /* No clients are using the specified file. */
2274 else if (!all_rc
&& !rc
&& !once
)
2276 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2277 rc
= cache_clear (md5file
);
2280 /* Release the connection mutex. */
2281 pthread_cleanup_pop (1);
2285 send_status_all (STATUS_CACHE
, NULL
);
2287 /* One or more files were locked while clearing all cache entries. */
2291 return send_error (ctx
, rc
);
2295 cachetimeout_command (assuan_context_t ctx
, char *line
)
2297 unsigned char md5file
[16];
2299 char **req
= str_split (line
, " ", 0);
2303 if (!req
|| !*req
|| !req
[1])
2306 return send_error (ctx
, GPG_ERR_SYNTAX
);
2310 timeout
= (int) strtol (req
[1], &p
, 10);
2311 if (errno
!= 0 || *p
|| timeout
< -1)
2314 return send_error (ctx
, GPG_ERR_SYNTAX
);
2317 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, req
[0], strlen (req
[0]));
2318 rc
= cache_set_timeout (md5file
, timeout
);
2319 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2322 MUTEX_LOCK (&rcfile_mutex
);
2323 config_set_int_param (&global_config
, req
[0], "cache_timeout", req
[1]);
2324 MUTEX_UNLOCK (&rcfile_mutex
);
2328 return send_error (ctx
, rc
);
2332 dump_command (assuan_context_t ctx
, char *line
)
2336 struct client_s
*client
= assuan_get_pointer (ctx
);
2339 if (disable_list_and_dump
== 1)
2340 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2342 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2346 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2347 pwmd_strerror (GPG_ERR_ENOMEM
));
2348 return send_error (ctx
, GPG_ERR_ENOMEM
);
2351 pthread_cleanup_push (xmlFree
, xml
);
2352 rc
= xfer_data (ctx
, (char *) xml
, len
);
2353 pthread_cleanup_pop (1);
2354 return send_error (ctx
, rc
);
2358 getconfig_command (assuan_context_t ctx
, char *line
)
2360 struct client_s
*client
= assuan_get_pointer (ctx
);
2362 char filename
[255] = { 0 }, param
[747] =
2365 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2367 if (!line
|| !*line
)
2368 return send_error (ctx
, GPG_ERR_SYNTAX
);
2370 if (strchr (line
, ' '))
2372 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2377 if (fp
&& !valid_filename (fp
))
2378 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2380 paramp
= str_down (paramp
);
2381 if (!strcmp (paramp
, "cipher") && fp
)
2383 struct crypto_s
*crypto
;
2385 rc
= init_client_crypto (&crypto
);
2388 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2392 gcry_cipher_algo_name (cipher_to_gcrypt (crypto
->hdr
.flags
));
2402 cleanup_crypto (&crypto
);
2403 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2404 return send_error (ctx
, rc
);
2409 else if (!strcmp (paramp
, "cipher_iterations") && fp
)
2411 struct crypto_s
*crypto
;
2413 rc
= init_client_crypto (&crypto
);
2416 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2419 tmp
= str_asprintf ("%llu",
2420 (unsigned long long) crypto
->hdr
.
2423 rc
= GPG_ERR_ENOMEM
;
2427 cleanup_crypto (&crypto
);
2428 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2429 return send_error (ctx
, rc
);
2434 else if (!strcmp (paramp
, "passphrase"))
2435 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2437 p
= config_get_value (fp
? fp
: "global", paramp
);
2439 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2441 tmp
= expand_homedir (p
);
2445 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2446 pwmd_strerror (GPG_ERR_ENOMEM
));
2447 return send_error (ctx
, GPG_ERR_ENOMEM
);
2452 pthread_cleanup_push (xfree
, p
);
2453 rc
= xfer_data (ctx
, p
, strlen (p
));
2454 pthread_cleanup_pop (1);
2455 return send_error (ctx
, rc
);
2460 xmlXPathContextPtr xp
;
2461 xmlXPathObjectPtr result
;
2467 xpath_command_cleanup (void *arg
)
2469 struct xpath_s
*xpath
= arg
;
2474 req_cleanup (xpath
->req
);
2477 xmlBufferFree (xpath
->buf
);
2480 xmlXPathFreeObject (xpath
->result
);
2483 xmlXPathFreeContext (xpath
->xp
);
2487 do_xpath (assuan_context_t ctx
, char *line
)
2490 struct client_s
*client
= assuan_get_pointer (ctx
);
2491 struct xpath_s _x
= { 0 };
2492 struct xpath_s
*xpath
= &_x
;
2494 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2496 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2497 return GPG_ERR_ENOMEM
;
2500 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2503 rc
= GPG_ERR_BAD_DATA
;
2508 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2511 rc
= GPG_ERR_BAD_DATA
;
2515 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2517 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2521 rc
= recurse_xpath_nodeset (client
->doc
, xpath
->result
->nodesetval
,
2522 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2526 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2528 rc
= GPG_ERR_NO_DATA
;
2531 else if (xpath
->req
[1])
2537 pthread_cleanup_push (xpath_command_cleanup
, &xpath
);
2538 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2539 xmlBufferLength (xpath
->buf
));
2540 pthread_cleanup_pop (0);
2542 xpath_command_cleanup (xpath
);
2547 xpath_command (assuan_context_t ctx
, char *line
)
2549 struct client_s
*client
= assuan_get_pointer (ctx
);
2551 struct argv_s
*args
[] = {
2552 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2556 if (disable_list_and_dump
== 1)
2557 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2559 rc
= parse_options (&line
, args
, client
);
2561 return send_error (ctx
, rc
);
2563 if (client
->opts
& OPT_INQUIRE
)
2565 unsigned char *result
;
2568 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2570 return send_error (ctx
, rc
);
2572 line
= (char *) result
;
2575 if (!line
|| !*line
)
2576 rc
= GPG_ERR_SYNTAX
;
2579 rc
= do_xpath (ctx
, line
);
2581 if (client
->opts
& OPT_INQUIRE
)
2584 return send_error (ctx
, rc
);
2588 do_xpathattr (assuan_context_t ctx
, char *line
)
2590 struct client_s
*client
= assuan_get_pointer (ctx
);
2594 struct xpath_s _x
= { 0 };
2595 struct xpath_s
*xpath
= &_x
;
2597 if ((req
= str_split (line
, " ", 3)) == NULL
)
2598 return GPG_ERR_ENOMEM
;
2602 rc
= GPG_ERR_SYNTAX
;
2606 if (!strcasecmp (req
[0], "SET"))
2608 else if (!strcasecmp (req
[0], "DELETE"))
2612 rc
= GPG_ERR_SYNTAX
;
2616 if (!req
[1] || !req
[2])
2618 rc
= GPG_ERR_SYNTAX
;
2622 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2624 rc
= GPG_ERR_ENOMEM
;
2628 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2630 rc
= GPG_ERR_SYNTAX
;
2634 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2637 rc
= GPG_ERR_BAD_DATA
;
2642 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2645 rc
= GPG_ERR_BAD_DATA
;
2649 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2651 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2655 rc
= recurse_xpath_nodeset (client
->doc
, xpath
->result
->nodesetval
,
2656 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2657 (xmlChar
*) req
[1]);
2660 xpath_command_cleanup (xpath
);
2665 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2667 xpathattr_command (assuan_context_t ctx
, char *line
)
2669 struct client_s
*client
= assuan_get_pointer (ctx
);
2671 struct argv_s
*args
[] = {
2672 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2676 if (disable_list_and_dump
== 1)
2677 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2679 rc
= parse_options (&line
, args
, client
);
2681 return send_error (ctx
, rc
);
2683 if (client
->opts
& OPT_INQUIRE
)
2685 unsigned char *result
;
2688 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2690 return send_error (ctx
, rc
);
2692 line
= (char *) result
;
2695 if (!line
|| !*line
)
2696 rc
= GPG_ERR_SYNTAX
;
2699 rc
= do_xpathattr (ctx
, line
);
2701 if (client
->opts
& OPT_INQUIRE
)
2704 return send_error (ctx
, rc
);
2708 do_import (struct client_s
*client
, unsigned char *line
)
2710 char **req
, **path
= NULL
, **path_orig
= NULL
, *content
;
2711 xmlDocPtr doc
= NULL
;
2712 xmlNodePtr n
, root
, copy
;
2715 req
= str_split ((char *) line
, "\t", 2);
2718 return GPG_ERR_SYNTAX
;
2721 path
= str_split (req
[1], "\t", 0);
2722 if (!content
|| !*content
)
2724 rc
= GPG_ERR_SYNTAX
;
2728 if (path
&& !valid_element_path (path
, 0))
2730 rc
= GPG_ERR_INV_VALUE
;
2734 doc
= xmlReadDoc ((xmlChar
*) content
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2737 rc
= GPG_ERR_BAD_DATA
;
2741 root
= xmlDocGetRootElement (doc
);
2742 rc
= validate_import (root
);
2748 path_orig
= strv_dup (path
);
2751 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2752 pwmd_strerror (GPG_ERR_ENOMEM
));
2753 rc
= GPG_ERR_ENOMEM
;
2757 xmlChar
*a
= xmlGetProp (root
, (xmlChar
*) "_name");
2760 strv_free (path_orig
);
2761 rc
= GPG_ERR_ENOMEM
;
2765 if (strv_printf (&path
, "%s", (char *) a
) == 0)
2768 strv_free (path_orig
);
2769 rc
= GPG_ERR_ENOMEM
;
2774 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
2776 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2778 strv_free (path_orig
);
2785 find_elements (client
->doc
, n
->children
, path
+ 1, &rc
, NULL
,
2786 NULL
, NULL
, 0, 0, NULL
, 1);
2788 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2790 strv_free (path_orig
);
2795 xmlNodePtr parent
= n
->parent
;
2806 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
2808 n
= create_element_path (client
, &path
, &rc
, NULL
);
2814 copy
= xmlCopyNodeList (root
);
2815 n
= xmlAddChildList (n
, copy
);
2817 rc
= GPG_ERR_BAD_DATA
;
2821 /* Check if the content root element can create a DTD root element. */
2822 if (!xmlStrEqual ((xmlChar
*) "element", root
->name
))
2824 rc
= GPG_ERR_SYNTAX
;
2830 if ((a
= xmlGetProp (root
, (xmlChar
*) "_name")) == NULL
)
2832 rc
= GPG_ERR_SYNTAX
;
2836 char *tmp
= str_dup ((char *) a
);
2838 int literal
= is_literal_element (&tmp
);
2840 if (!valid_xml_element ((xmlChar
*) tmp
) || literal
)
2843 rc
= GPG_ERR_INV_VALUE
;
2847 if (strv_printf (&path
, "%s", tmp
) == 0)
2850 rc
= GPG_ERR_ENOMEM
;
2855 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 1);
2857 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2859 rc
= GPG_ERR_BAD_DATA
;
2863 /* Overwriting the existing tree. */
2867 xmlFreeNodeList (n
);
2871 xmlSetProp (root
, (xmlChar
*) "_name", (xmlChar
*) path
[0]);
2872 n
= xmlCopyNode (root
, 1);
2873 n
= xmlAddChildList (xmlDocGetRootElement (client
->doc
), n
);
2877 rc
= update_element_mtime (n
->parent
);
2891 import_command (assuan_context_t ctx
, char *line
)
2894 struct client_s
*client
= assuan_get_pointer (ctx
);
2895 unsigned char *result
;
2898 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2900 return send_error (ctx
, rc
);
2902 rc
= do_import (client
, result
);
2903 return send_error (ctx
, rc
);
2907 do_lock (struct client_s
*client
, int add
)
2909 gpg_error_t rc
= lock_file_mutex (client
, add
);
2912 client
->flags
|= FLAG_LOCK_CMD
;
2918 lock_command (assuan_context_t ctx
, char *line
)
2920 struct client_s
*client
= assuan_get_pointer (ctx
);
2921 gpg_error_t rc
= do_lock (client
, 0);
2923 return send_error (ctx
, rc
);
2927 unlock_command (assuan_context_t ctx
, char *line
)
2929 struct client_s
*client
= assuan_get_pointer (ctx
);
2932 rc
= unlock_file_mutex (client
, 0);
2933 return send_error (ctx
, rc
);
2937 option_command (assuan_context_t ctx
, char *line
)
2939 struct client_s
*client
= assuan_get_pointer (ctx
);
2941 struct pinentry_option_s
*pin_opts
= &client
->pinentry_opts
;
2943 struct agent_s
*agent
= client
->crypto
->agent
;
2945 char namebuf
[255] = { 0 };
2946 char *name
= namebuf
;
2947 char *value
= NULL
, *p
;
2949 p
= strchr (line
, '=');
2951 strncpy (namebuf
, line
, sizeof(namebuf
));
2954 strncpy (namebuf
, line
, strlen (line
)-strlen (p
));
2958 log_write1 ("OPTION name='%s' value='%s'", name
, value
);
2960 if (strcasecmp (name
, (char *) "log_level") == 0)
2966 l
= strtol (value
, NULL
, 10);
2969 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2972 MUTEX_LOCK (&rcfile_mutex
);
2973 config_set_int_param (&global_config
, "global", "log_level", value
);
2974 MUTEX_UNLOCK (&rcfile_mutex
);
2977 else if (strcasecmp (name
, (char *) "lock-timeout") == 0)
2984 n
= strtol (value
, &p
, 10);
2986 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2989 client
->lock_timeout
= n
;
2992 else if (strcasecmp (name
, (char *) "NAME") == 0)
2994 char *tmp
= pthread_getspecific (thread_name_key
);
2999 if (!value
|| !*value
)
3001 pthread_setspecific (thread_name_key
, str_dup (""));
3005 pthread_setspecific (thread_name_key
, str_dup (value
));
3008 else if (strcasecmp (name
, (char *) "lc-messages") == 0)
3010 xfree (pin_opts
->lc_messages
);
3011 pin_opts
->lc_messages
= NULL
;
3012 if (value
&& *value
)
3013 pin_opts
->lc_messages
= str_dup (value
);
3016 rc
= set_agent_option (client
->crypto
->agent
, "lc-messages", value
);
3019 else if (strcasecmp (name
, (char *) "lc-ctype") == 0)
3021 xfree (pin_opts
->lc_ctype
);
3022 pin_opts
->lc_ctype
= NULL
;
3023 if (value
&& *value
)
3024 pin_opts
->lc_ctype
= str_dup (value
);
3027 rc
= set_agent_option (client
->crypto
->agent
, "lc-ctype", value
);
3030 else if (strcasecmp (name
, (char *) "ttyname") == 0)
3032 xfree (pin_opts
->ttyname
);
3033 pin_opts
->ttyname
= NULL
;
3034 if (value
&& *value
)
3035 pin_opts
->ttyname
= str_dup (value
);
3038 rc
= set_agent_option (client
->crypto
->agent
, "ttyname", value
);
3041 else if (strcasecmp (name
, (char *) "ttytype") == 0)
3043 xfree (pin_opts
->ttytype
);
3044 pin_opts
->ttytype
= NULL
;
3045 if (value
&& *value
)
3046 pin_opts
->ttytype
= str_dup (value
);
3049 rc
= set_agent_option (client
->crypto
->agent
, "ttytype", value
);
3052 else if (strcasecmp (name
, (char *) "display") == 0)
3054 xfree (pin_opts
->display
);
3055 pin_opts
->display
= NULL
;
3056 if (value
&& *value
)
3057 pin_opts
->display
= str_dup (value
);
3060 rc
= set_agent_option (client
->crypto
->agent
, "display", value
);
3063 else if (strcasecmp (name
, (char *) "pinentry-desc") == 0)
3065 xfree (pin_opts
->desc
);
3066 pin_opts
->desc
= NULL
;
3067 if (value
&& *value
)
3068 pin_opts
->desc
= str_dup (value
);
3070 else if (strcasecmp (name
, (char *) "pinentry-title") == 0)
3072 xfree (pin_opts
->title
);
3073 pin_opts
->title
= NULL
;
3074 if (value
&& *value
)
3075 pin_opts
->title
= str_dup (value
);
3077 else if (strcasecmp (name
, (char *) "pinentry-prompt") == 0)
3079 xfree (pin_opts
->prompt
);
3080 pin_opts
->prompt
= NULL
;
3081 if (value
&& *value
)
3082 pin_opts
->prompt
= str_dup (value
);
3085 else if (strcasecmp (name
, "pinentry-timeout") == 0)
3093 n
= (int) strtol (value
, &p
, 10);
3096 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3098 pin_opts
->timeout
= n
;
3099 MUTEX_LOCK (&rcfile_mutex
);
3100 config_set_int_param (&global_config
,
3101 client
->filename
? client
->filename
: "global",
3102 "pinentry_timeout", value
);
3103 MUTEX_UNLOCK (&rcfile_mutex
);
3106 else if (strcasecmp (name
, "disable-pinentry") == 0)
3111 if (value
&& *value
)
3113 n
= (int) strtol (value
, &p
, 10);
3114 if (*p
|| n
< 0 || n
> 1)
3115 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3119 client
->flags
|= FLAG_NO_PINENTRY
;
3121 client
->flags
&= ~FLAG_NO_PINENTRY
;
3124 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
3128 if (!rc
&& use_agent
&& agent
)
3130 rc
= pinentry_merge_options (&client
->crypto
->agent
->pinentry_opts
,
3135 return send_error (ctx
, rc
);
3139 do_rename (assuan_context_t ctx
, char *line
)
3141 struct client_s
*client
= assuan_get_pointer (ctx
);
3143 char **req
, **src
, *dst
;
3146 req
= str_split (line
, " ", 0);
3148 if (!req
|| !req
[0] || !req
[1])
3151 return GPG_ERR_SYNTAX
;
3155 is_literal_element (&dst
);
3157 if (!valid_xml_element ((xmlChar
*) dst
))
3160 return GPG_ERR_INV_VALUE
;
3163 if (strchr (req
[0], '\t'))
3164 src
= str_split (req
[0], "\t", 0);
3166 src
= str_split (req
[0], " ", 0);
3170 rc
= GPG_ERR_SYNTAX
;
3174 n
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3176 n
= find_elements (client
->doc
, n
->children
, src
+ 1, &rc
, NULL
, NULL
,
3177 NULL
, 0, 0, NULL
, 0);
3182 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
3185 rc
= GPG_ERR_ENOMEM
;
3189 /* To prevent unwanted effects:
3191 * <root name="a"><b/></root>
3195 if (xmlStrEqual (a
, (xmlChar
*) dst
))
3198 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3208 for (p
= src
; *p
; p
++)
3212 strv_printf (&tmp
, "%s", *p
);
3216 strv_printf (&tmp
, "!%s", dst
);
3217 ndst
= find_root_element (client
->doc
, &tmp
, &rc
, NULL
, 0, 0);
3218 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3225 ndst
= find_elements (client
->doc
, ndst
->children
, tmp
+ 1, &rc
, NULL
,
3226 NULL
, NULL
, 0, 0, NULL
, 0);
3229 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3234 /* Target may exist:
3237 * <root name="b" target="a"/>
3246 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3253 xmlFreeNodeList (ndst
);
3256 rc
= add_attribute (n
, "_name", dst
);
3265 rename_command (assuan_context_t ctx
, char *line
)
3267 struct client_s
*client
= assuan_get_pointer (ctx
);
3269 struct argv_s
*args
[] = {
3270 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3274 rc
= parse_options (&line
, args
, client
);
3276 return send_error (ctx
, rc
);
3278 if (client
->opts
& OPT_INQUIRE
)
3280 unsigned char *result
;
3283 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3285 return send_error (ctx
, rc
);
3287 line
= (char *) result
;
3290 rc
= do_rename (ctx
, line
);
3292 if (client
->opts
& OPT_INQUIRE
)
3295 return send_error (ctx
, rc
);
3299 do_copy (assuan_context_t ctx
, char *line
)
3301 struct client_s
*client
= assuan_get_pointer (ctx
);
3303 char **req
, **src
= NULL
, **dst
= NULL
;
3304 xmlNodePtr nsrc
, ndst
, new = NULL
;
3306 req
= str_split (line
, " ", 0);
3307 if (!req
|| !req
[0] || !req
[1])
3310 return GPG_ERR_SYNTAX
;
3313 if (strchr (req
[0], '\t'))
3314 src
= str_split (req
[0], "\t", 0);
3316 src
= str_split (req
[0], " ", 0);
3320 rc
= GPG_ERR_SYNTAX
;
3324 if (strchr (req
[1], '\t'))
3325 dst
= str_split (req
[1], "\t", 0);
3327 dst
= str_split (req
[1], " ", 0);
3331 rc
= GPG_ERR_SYNTAX
;
3335 if (!valid_element_path (dst
, 0))
3337 rc
= GPG_ERR_INV_VALUE
;
3341 nsrc
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3343 nsrc
= find_elements (client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3344 NULL
, NULL
, 0, 0, NULL
, 0);
3349 new = xmlCopyNodeList (nsrc
);
3352 rc
= GPG_ERR_ENOMEM
;
3357 ndst
= find_root_element (client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3361 ndst
= find_elements (client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3362 NULL
, create_target_elements_cb
, 0, 0, NULL
, 0);
3369 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3373 ndst
= create_element_path (client
, &dst
, &rc
, NULL
);
3378 /* Merge any attributes from the src node to the initial dst node. */
3379 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
)
3381 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3384 xmlAttrPtr a
= xmlHasProp (ndst
, attr
->name
);
3388 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3389 xmlNewProp (ndst
, attr
->name
, tmp
);
3391 rc
= add_attribute (ndst
, NULL
, NULL
);
3394 xmlNodePtr n
= ndst
->children
;
3396 xmlFreeNodeList (n
);
3397 ndst
->children
= NULL
;
3401 n
= xmlCopyNodeList (new->children
);
3404 rc
= GPG_ERR_ENOMEM
;
3408 n
= xmlAddChildList (ndst
, n
);
3411 rc
= GPG_ERR_ENOMEM
;
3415 rc
= update_element_mtime (xmlDocGetRootElement (client
->doc
) ==
3416 ndst
->parent
? ndst
: ndst
->parent
);
3422 xmlUnlinkNode (new);
3423 xmlFreeNodeList (new);
3439 copy_command (assuan_context_t ctx
, char *line
)
3441 struct client_s
*client
= assuan_get_pointer (ctx
);
3443 struct argv_s
*args
[] = {
3444 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3448 rc
= parse_options (&line
, args
, client
);
3450 return send_error (ctx
, rc
);
3452 if (client
->opts
& OPT_INQUIRE
)
3454 unsigned char *result
;
3457 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3459 return send_error (ctx
, rc
);
3461 line
= (char *) result
;
3464 rc
= do_copy (ctx
, line
);
3466 if (client
->opts
& OPT_INQUIRE
)
3469 return send_error (ctx
, rc
);
3473 do_move (assuan_context_t ctx
, char *line
)
3475 struct client_s
*client
= assuan_get_pointer (ctx
);
3477 char **req
, **src
= NULL
, **dst
= NULL
;
3478 xmlNodePtr nsrc
, ndst
= NULL
;
3480 req
= str_split (line
, " ", 0);
3482 if (!req
|| !req
[0] || !req
[1])
3485 return GPG_ERR_SYNTAX
;
3488 if (strchr (req
[0], '\t'))
3489 src
= str_split (req
[0], "\t", 0);
3491 src
= str_split (req
[0], " ", 0);
3495 rc
= GPG_ERR_SYNTAX
;
3499 if (strchr (req
[1], '\t'))
3500 dst
= str_split (req
[1], "\t", 0);
3502 dst
= str_split (req
[1], " ", 0);
3504 nsrc
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3506 nsrc
= find_elements (client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3507 NULL
, NULL
, 0, 0, NULL
, 0);
3514 if (!valid_element_path (dst
, 0))
3516 rc
= GPG_ERR_INV_VALUE
;
3520 ndst
= find_root_element (client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3522 ndst
= find_elements (client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3523 NULL
, NULL
, 0, 0, NULL
, 0);
3526 ndst
= xmlDocGetRootElement (client
->doc
);
3528 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
)
3532 rc
= GPG_ERR_CONFLICT
;
3537 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3544 xmlChar
*a
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3545 xmlNodePtr dup
= find_element (ndst
->children
, (char *) a
, NULL
);
3553 if (ndst
== xmlDocGetRootElement (client
->doc
))
3555 xmlNodePtr n
= nsrc
;
3558 while (n
->parent
&& n
->parent
!= ndst
)
3561 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
3562 xmlChar
*b
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3564 if (xmlStrEqual (a
, b
))
3567 xmlUnlinkNode (nsrc
);
3569 xmlFreeNodeList (n
);
3577 xmlUnlinkNode (dup
);
3578 xmlFreeNodeList (dup
);
3582 xmlUnlinkNode (dup
);
3588 xmlChar
*name
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3590 if (nsrc
->parent
== xmlDocGetRootElement (client
->doc
)
3591 && !strcmp ((char *) name
, *dst
))
3594 rc
= GPG_ERR_CONFLICT
;
3599 ndst
= create_element_path (client
, &dst
, &rc
, nsrc
);
3605 update_element_mtime (nsrc
->parent
);
3606 xmlUnlinkNode (nsrc
);
3607 ndst
= xmlAddChildList (ndst
, nsrc
);
3610 rc
= GPG_ERR_ENOMEM
;
3612 update_element_mtime (ndst
->parent
);
3628 move_command (assuan_context_t ctx
, char *line
)
3630 struct client_s
*client
= assuan_get_pointer (ctx
);
3632 struct argv_s
*args
[] = {
3633 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3637 rc
= parse_options (&line
, args
, client
);
3639 return send_error (ctx
, rc
);
3641 if (client
->opts
& OPT_INQUIRE
)
3643 unsigned char *result
;
3646 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3648 return send_error (ctx
, rc
);
3650 line
= (char *) result
;
3653 rc
= do_move (ctx
, line
);
3655 if (client
->opts
& OPT_INQUIRE
)
3658 return send_error (ctx
, rc
);
3662 ls_command (assuan_context_t ctx
, char *line
)
3665 char *tmp
= str_asprintf ("%s/data", homedir
);
3666 char *dir
= expand_homedir (tmp
);
3667 DIR *d
= opendir (dir
);
3669 rc
= gpg_error_from_syserror ();
3675 return send_error (ctx
, rc
);
3679 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
3680 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
3686 while (!readdir_r (d
, p
, &cur
) && cur
)
3688 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
3690 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.'
3691 && cur
->d_name
[2] == '\0')
3694 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
3701 rc
= GPG_ERR_ENOMEM
;
3713 return send_error (ctx
, rc
);
3716 return send_error (ctx
, GPG_ERR_NO_DATA
);
3718 list
[strlen (list
) - 1] = 0;
3719 rc
= xfer_data (ctx
, list
, strlen (list
));
3721 return send_error (ctx
, rc
);
3725 bye_notify (assuan_context_t ctx
, char *line
)
3727 struct client_s
*cl
= assuan_get_pointer (ctx
);
3730 if (cl
->thd
->remote
)
3736 struct timeval tv
= { 0, 50000 };
3738 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
3739 if (rc
== GNUTLS_E_AGAIN
)
3740 select (0, NULL
, NULL
, NULL
, &tv
);
3742 while (rc
== GNUTLS_E_AGAIN
);
3746 /* This will let assuan_process_next() return. */
3747 fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
);
3748 cl
->last_rc
= 0; // BYE command result
3753 reset_notify (assuan_context_t ctx
, char *line
)
3755 struct client_s
*client
= assuan_get_pointer (ctx
);
3758 cleanup_client (client
);
3764 * This is called before every Assuan command.
3767 command_startup (assuan_context_t ctx
, const char *name
)
3769 struct client_s
*client
= assuan_get_pointer (ctx
);
3771 struct command_table_s
*cmd
= NULL
;
3773 log_write1 ("command='%s'", name
);
3774 client
->last_rc
= client
->opts
= 0;
3776 for (int i
= 0; command_table
[i
]; i
++)
3778 if (!strcasecmp (name
, command_table
[i
]->name
))
3780 if (command_table
[i
]->ignore_startup
)
3782 cmd
= command_table
[i
];
3787 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
3792 * This is called after every Assuan command.
3795 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
3797 struct client_s
*client
= assuan_get_pointer (ctx
);
3799 if (!(client
->flags
& FLAG_LOCK_CMD
))
3800 unlock_file_mutex (client
, 0);
3802 log_write1 (_("command completed: rc=%u"), client
->last_rc
);
3803 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
3807 help_command (assuan_context_t ctx
, char *line
)
3812 if (!line
|| !*line
)
3815 char *help
= str_dup (_("Usage: HELP [<COMMAND>]\n"
3816 "For commands that take an element path as an argument, each element is "
3817 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3820 for (i
= 0; command_table
[i
]; i
++)
3822 if (!command_table
[i
]->help
)
3825 tmp
= str_asprintf ("%s %s", help
, command_table
[i
]->name
);
3830 tmp
= strip_texi_and_wrap (help
);
3832 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3834 return send_error (ctx
, rc
);
3837 for (i
= 0; command_table
[i
]; i
++)
3839 if (!strcasecmp (line
, command_table
[i
]->name
))
3843 if (!command_table
[i
]->help
)
3846 help
= strip_texi_and_wrap (command_table
[i
]->help
);
3847 tmp
= str_asprintf (_("Usage: %s"), help
);
3849 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3851 return send_error (ctx
, rc
);
3855 return send_error (ctx
, GPG_ERR_INV_NAME
);
3859 new_command (const char *name
, int ignore
, int unlock
,
3860 gpg_error_t (*handler
) (assuan_context_t
, char *),
3866 for (i
= 0; command_table
[i
]; i
++);
3869 xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
3870 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
3871 command_table
[i
]->name
= name
;
3872 command_table
[i
]->handler
= handler
;
3873 command_table
[i
]->ignore_startup
= ignore
;
3874 command_table
[i
]->unlock
= unlock
;
3875 command_table
[i
++]->help
= help
;
3876 command_table
[i
] = NULL
;
3884 for (i
= 0; command_table
[i
]; i
++)
3885 xfree (command_table
[i
]);
3887 xfree (command_table
);
3891 sort_commands (const void *arg1
, const void *arg2
)
3893 struct command_table_s
*const *a
= arg1
;
3894 struct command_table_s
*const *b
= arg2
;
3903 return strcmp ((*a
)->name
, (*b
)->name
);
3907 passwd_command (assuan_context_t ctx
, char *line
)
3909 struct client_s
*client
= assuan_get_pointer (ctx
);
3911 struct argv_s
*args
[] = {
3912 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
3913 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
3917 if (client
->flags
& FLAG_NEW
)
3918 return send_error (ctx
, GPG_ERR_INV_STATE
);
3920 client
->crypto
->save
.s2k_count
=
3921 config_get_ulong (client
->filename
, "s2k_count");
3922 rc
= parse_options (&line
, args
, client
);
3924 return send_error (ctx
, rc
);
3926 if (!rc
&& client
->opts
& OPT_RESET
)
3928 rc
= cache_clear (client
->md5file
);
3930 send_status_all (STATUS_CACHE
, NULL
);
3935 if (!IS_PKCS (client
->crypto
))
3937 struct crypto_s
*crypto
;
3939 xfree (client
->crypto
->filename
);
3940 client
->crypto
->filename
= str_dup (client
->filename
);
3941 rc
= change_passwd (ctx
, client
->filename
,
3942 client
->flags
& FLAG_NO_PINENTRY
, &crypto
);
3945 cleanup_crypto (&client
->crypto
);
3946 client
->crypto
= crypto
;
3947 update_checksum (client
);
3948 cleanup_crypto_stage1 (client
->crypto
);
3954 if (client
->crypto
->save
.s2k_count
)
3955 rc
= send_to_agent (client
->crypto
->agent
, NULL
, NULL
,
3956 "OPTION s2k-count=%lu",
3957 client
->crypto
->save
.s2k_count
);
3960 rc
= agent_passwd (client
->crypto
);
3965 return send_error (ctx
, rc
);
3969 parse_keygrip_opt_sign (void *data
, void *value
)
3971 struct client_s
*client
= data
;
3974 client
->opts
|= OPT_SIGN
;
3979 keygrip_command (assuan_context_t ctx
, char *line
)
3981 struct client_s
*client
= assuan_get_pointer (ctx
);
3983 struct crypto_s
*crypto
;
3984 struct argv_s
*args
[] = {
3985 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_keygrip_opt_sign
},
3989 if (!line
|| !*line
)
3990 return send_error (ctx
, GPG_ERR_SYNTAX
);
3992 rc
= parse_options (&line
, args
, client
);
3994 return send_error (ctx
, rc
);
3996 if (!valid_filename (line
))
3997 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3999 rc
= init_client_crypto (&crypto
);
4001 return send_error (ctx
, rc
);
4003 rc
= read_data_file (line
, crypto
);
4006 char *hexgrip
= NULL
;
4008 if (!IS_PKCS (crypto
))
4010 cleanup_crypto (&crypto
);
4011 return send_error (ctx
, GPG_ERR_NOT_SUPPORTED
);
4014 if (client
->opts
& OPT_SIGN
)
4016 if (valid_keygrip (crypto
->sign_grip
, sizeof (crypto
->sign_grip
)))
4017 hexgrip
= bin2hex (crypto
->sign_grip
, sizeof (crypto
->sign_grip
));
4021 hexgrip
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
4024 rc
= GPG_ERR_ENOMEM
;
4026 rc
= xfer_data (ctx
, hexgrip
, strlen (hexgrip
));
4031 cleanup_crypto (&crypto
);
4032 return send_error (ctx
, rc
);
4036 parse_opt_data (void *data
, void *value
)
4038 struct client_s
*client
= data
;
4041 client
->opts
|= OPT_DATA
;
4046 getinfo_command (assuan_context_t ctx
, char *line
)
4048 struct client_s
*client
= assuan_get_pointer (ctx
);
4050 char buf
[ASSUAN_LINELENGTH
];
4051 struct argv_s
*args
[] = {
4052 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4056 rc
= parse_options (&line
, args
, client
);
4058 return send_error (ctx
, rc
);
4060 if (!strcasecmp (line
, "clients"))
4062 if (client
->opts
& OPT_DATA
)
4064 MUTEX_LOCK (&cn_mutex
);
4065 snprintf (buf
, sizeof (buf
), "%i", slist_length (cn_thread_list
));
4066 MUTEX_UNLOCK (&cn_mutex
);
4067 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4070 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4072 else if (!strcasecmp (line
, "cache"))
4074 if (client
->opts
& OPT_DATA
)
4076 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4077 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4080 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4082 else if (!strcasecmp (line
, "pid"))
4085 pid_t pid
= getpid ();
4087 snprintf (buf
, sizeof (buf
), "%i", pid
);
4088 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4090 else if (!strcasecmp (line
, "version"))
4092 char *buf
= str_asprintf ("0x%06x %s", VERSION_HEX
,
4106 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4109 else if (!strcasecmp (line
, "last_error"))
4111 if (client
->last_error
)
4112 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4114 rc
= GPG_ERR_NO_DATA
;
4117 rc
= gpg_error (GPG_ERR_SYNTAX
);
4119 return send_error (ctx
, rc
);
4124 send_data_cb (void *user
, const void *buf
, size_t len
)
4126 assuan_context_t ctx
= user
;
4128 return assuan_send_data (ctx
, buf
, len
);
4132 send_status_cb (void *user
, const char *line
)
4134 assuan_context_t ctx
= user
;
4135 char keyword
[200], *k
;
4138 for (p
= line
, k
= keyword
; *p
; p
++)
4150 while (isspace (*p
))
4153 return assuan_write_status (ctx
, keyword
, *p
? p
: NULL
);
4158 agent_command (assuan_context_t ctx
, char *line
)
4163 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
4166 struct client_s
*client
= assuan_get_pointer (ctx
);
4168 if (!line
|| !*line
)
4169 return send_error (ctx
, GPG_ERR_SYNTAX
);
4171 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 1);
4172 rc
= assuan_transact (client
->crypto
->agent
->ctx
, line
, send_data_cb
,
4173 client
->ctx
, agent_loopback_cb
, client
->crypto
,
4174 send_status_cb
, client
->ctx
);
4175 if (gpg_err_code (rc
) == GPG_ERR_ASS_CANCELED
)
4180 rc
= assuan_write_line (client
->crypto
->agent
->ctx
, "CAN");
4183 rc
= assuan_read_line (client
->crypto
->agent
->ctx
, &line
, &len
);
4185 rc
= gpg_error (GPG_ERR_ASS_CANCELED
);
4189 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 0);
4191 return send_error (ctx
, rc
);
4197 /* !BEGIN-HELP-TEXT!
4199 * This comment is used as a marker to generate the offline documentation
4200 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4201 * script to determine where commands begin and end.
4203 new_command("HELP", 1, 1, help_command
, _(
4204 "HELP [<COMMAND>]\n"
4205 "Show available commands or command specific help text."
4208 new_command("AGENT", 1, 1, agent_command
, _(
4210 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4211 "@command{gpg-agent}."
4214 new_command("GETINFO", 1, 1, getinfo_command
, _(
4215 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4216 "Get server and other information: @var{cache} returns the number of cached "
4217 "documents via a status message. @var{clients} returns the number of "
4218 "connected clients via a status message. @var{pid} returns the process ID "
4219 "number of the server via a data response. @var{VERSION} returns the server "
4220 "version number and compile-time features with a data response with each "
4221 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4222 "the last failed command when available. @xref{Status Messages}. "
4224 "When the @option{--data} option is specified then the result will be send "
4225 "via a data response rather than a status message."
4228 new_command("PASSWD", 0, 0, passwd_command
, _(
4229 "PASSWD [--reset] [--s2k-count=N]\n"
4230 "Changes the passphrase of the secret key required to open the current "
4231 "file. When the @option{--reset} option is passed then the cache entry for "
4232 "the current file will be reset and the passphrase, if any, will be required "
4233 "during the next @code{OPEN}. @xref{OPEN}."
4235 "The @option{--s2k-count} option sets number of hash iterations for a "
4236 "passphrase and must be either @code{0} to use the calibrated count of the "
4237 "machine (the default), or a value greater than or equal to @code{65536}. "
4241 new_command("KEYGRIP", 1, 1, keygrip_command
, _(
4242 "KEYGRIP [--sign] <filename>\n"
4243 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4246 "When the @option{--sign} option is specified then the key used for signing "
4247 "of the specified @var{filename} will be returned."
4250 new_command("OPEN", 1, 1, open_command
, _(
4251 "OPEN [--lock] <filename> [<passphrase>]\n"
4252 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4253 "found on the file-system then a new document will be created. If the file "
4254 "is found, it is looked for in the file cache. If cached and no "
4255 "@var{passphrase} was specified then the cached document is opened. When not "
4256 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4257 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4260 "When the @option{--lock} option is passed then the file mutex will be "
4261 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4262 "file has been opened."
4265 new_command("SAVE", 0, 0, save_command
, _(
4266 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring [--sign-keygrip=hexstring]]\n"
4267 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4268 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4269 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4270 "keypair will be generated and a pinentry will be used to prompt for the "
4271 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4272 "passed, in which case the data file will not be passphrase protected."
4274 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4275 "passphrase retrieval and caching for new files and behaves as if the "
4276 "@option{--no-agent} commmand line option to @command{pwmd} was specified."
4278 "The @option{--reset} option will clear the cache entry for the current file "
4281 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4282 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4283 "(@pxref{Configuration}) for available ciphers."
4285 "The @option{--cipher-iterations} option specifies the number of times to "
4286 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4288 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4289 "the client to obtain the key paramaters to use when generating a new "
4290 "keypair. The inquired data is expected to be an S-expression. If not "
4291 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4292 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4293 "that when this option is specified a new keypair will be generated "
4294 "reguardless if the file is a new one or not."
4296 "You can encrypt the data file to a public key other than the one that it "
4297 "was originally encrypted with by passing the @option{--keygrip} option with "
4298 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4299 "be of any key that @command{gpg-agent} knows about. The "
4300 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4301 "secret key. This option may be needed when using a smartcard."
4303 "The @option{--s2k-count} option sets number of hash iterations for a "
4304 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4305 "value which is the default. This setting only affects new files. To change "
4306 "the setting, use the @code{PASSWD} command (@pxref{PASSWD})."
4309 new_command("ISCACHED", 1, 0, iscached_command
, _(
4310 "ISCACHED [--lock] <filename>\n"
4311 "An @emph{OK} response is returned if the specified @var{filename} is found "
4312 "in the file cache. If not found in the cache but exists on the filesystem "
4313 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4316 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4317 "file exists; it does not need to be opened nor cached."
4320 new_command("CLEARCACHE", 1, 1, clearcache_command
, _(
4321 "CLEARCACHE [<filename>]\n"
4322 "Clears a file cache entry for all or the specified @var{filename}."
4325 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command
, _(
4326 "CACHETIMEOUT <filename> <seconds>\n"
4327 "The time in @var{seconds} until @var{filename} will be removed from the "
4328 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4329 "the passphrase for each @code{OPEN} command (@pxref{OPEN}). "
4330 "@xref{Configuration}, and the @code{cache_timeout} parameter."
4333 new_command("LIST", 0, 1, list_command
, _(
4334 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4335 "If no element path is given then a newline separated list of root elements "
4336 "is returned with a data response. If given, then all reachable elements "
4337 "of the specified element path are returned unless the @option{--no-recurse} "
4338 "option is specified. If specified, only the child elements of the element "
4339 "path are returned without recursing into grandchildren. Each resulting "
4340 "element is prefixed with the literal @code{!} character when the element "
4341 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4343 "When the @option{--verbose} option is passed then each element path "
4344 "returned will have zero or more flags appened to it. These flags are "
4345 "delimited from the element path by a single space character. A flag itself "
4346 "is a single character. Flag @code{+} indicates that there are child nodes of "
4347 "the current element path. Flag @code{E} indicates that an element of an "
4348 "element path contained in a @var{target} attribute could not be found. Flag "
4349 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4350 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4351 "of a @var{target} attribute (see below)."
4353 "The @option{--with-target} option implies @option{--verbose} and will append "
4354 "an additional flag @code{T} followed by a single space then an element path. "
4355 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4356 "current element when it contains a @var{target} attribute. When no "
4357 "@var{target} attribute is found then no flag will be appended."
4359 "The @option{--no-recurse} option limits the amount of data returned to only "
4360 "the listing of children of the specified element path and not any "
4363 "The @option{--all} option lists the entire element tree for each root "
4364 "element. This option also implies option @option{--verbose}."
4366 "When the @option{--inquire} option is passed then all remaining non-option "
4367 "arguments are retrieved via a server @emph{INQUIRE}."
4370 new_command("REALPATH", 0, 1, realpath_command
, _(
4371 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4372 "Resolves all @code{target} attributes of the specified element path and "
4373 "returns the result with a data response. @xref{Target Attribute}, for details."
4375 "When the @option{--inquire} option is passed then all remaining non-option "
4376 "arguments are retrieved via a server @emph{INQUIRE}."
4379 new_command("STORE", 0, 1, store_command
, _(
4380 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4381 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4383 "Creates a new element path or modifies the @var{content} of an existing "
4384 "element. If only a single element is specified then a new root element is "
4385 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4386 "set to the final @key{TAB} delimited element. If no @var{content} is "
4387 "specified after the final @key{TAB}, then the content of the element will "
4388 "be removed, or empty when creating a new element."
4390 "The only restriction of an element name is that it not contain whitespace "
4391 "or begin with the literal element character @code{!} unless specifying a "
4392 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4393 "the @key{TAB} delimited elements. It is recommended that the content of an "
4394 "element be base64 encoded when it contains control or @key{TAB} characters "
4395 "to prevent @abbr{XML} and @command{pwmd} parsing errors."
4398 new_command("RENAME", 0, 1, rename_command
, _(
4399 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4400 "Renames the specified @var{element} to the new @var{value}. If an element of "
4401 "the same name as the @var{value} already exists it will be overwritten."
4403 "When the @option{--inquire} option is passed then all remaining non-option "
4404 "arguments are retrieved via a server @emph{INQUIRE}."
4407 new_command("COPY", 0, 1, copy_command
, _(
4408 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4409 "Copies the entire element tree starting from the child node of the source "
4410 "element, to the destination element path. If the destination element path "
4411 "does not exist then it will be created; otherwise it is overwritten."
4413 "Note that attributes from the source element are merged into the "
4414 "destination element when the destination element path exists. When an "
4415 "attribute of the same name exists in both the source and destination "
4416 "elements then the destination attribute will be updated to the source "
4419 "When the @option{--inquire} option is passed then all remaining non-option "
4420 "arguments are retrieved via a server @emph{INQUIRE}."
4423 new_command("MOVE", 0, 1, move_command
, _(
4424 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4425 "Moves the source element path to the destination element path. If the "
4426 "destination is not specified then it will be moved to the root node of the "
4427 "document. If the destination is specified and exists then it will be "
4428 "overwritten; otherwise it will be created."
4430 "When the @option{--inquire} option is passed then all remaining non-option "
4431 "arguments are retrieved via a server @emph{INQUIRE}."
4434 new_command("DELETE", 0, 1, delete_command
, _(
4435 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4436 "Removes the specified element path and all of its children. This may break "
4437 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4438 "refers to this element or any of its children."
4440 "When the @option{--inquire} option is passed then all remaining non-option "
4441 "arguments are retrieved via a server @emph{INQUIRE}."
4444 new_command("GET", 0, 1, get_command
, _(
4445 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4446 "Retrieves the content of the specified element. The content is returned "
4447 "with a data response."
4449 "When the @option{--inquire} option is passed then all remaining non-option "
4450 "arguments are retrieved via a server @emph{INQUIRE}."
4453 new_command("ATTR", 0, 1, attr_command
, _(
4454 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4456 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4458 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4459 "element. When no @var{value} is specified any existing value will be removed."
4461 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4463 " Removes an @var{attribute} from an element."
4465 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4467 " Retrieves a newline separated list of attributes names and values "
4468 "from the specified element. Each attribute name and value is space delimited."
4470 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4472 " Retrieves the value of an @var{attribute} from an element."
4475 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4476 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4479 "The @code{_mtime} attribute is updated each time an element is modified by "
4480 "either storing content, editing attributes or by deleting a child element. "
4481 "The @code{_ctime} attribute is created for each new element in an element "
4484 "When the @option{--inquire} option is passed then all remaining non-option "
4485 "arguments are retrieved via a server @emph{INQUIRE}."
4487 "@xref{Target Attribute}, for details about this special attribute."
4490 new_command("XPATH", 0, 1, xpath_command
, _(
4491 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4492 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4493 "specified, it is assumed the expression is a request to return a result. "
4494 "Otherwise, the result is set to the @var{value} argument and the document is "
4495 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4496 "is assumed to be empty and the document is updated. For example:"
4499 "XPATH //element[@@_name='password']@key{TAB}\n"
4502 "would clear the content of all @code{password} elements in the data file "
4503 "while leaving off the trailing @key{TAB} would return all @code{password} "
4504 "elements in @abbr{XML} format."
4506 "When the @option{--inquire} option is passed then all remaining non-option "
4507 "arguments are retrieved via a server @emph{INQUIRE}."
4509 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4510 "expression syntax."
4513 new_command("XPATHATTR", 0, 1, xpathattr_command
, _(
4514 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4515 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4516 "attributes and does not return a result. For the @var{SET} operation the "
4517 "@var{value} is optional but the field is required. If not specified then "
4518 "the attribute value will be empty. For example:"
4521 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4524 "would create an @code{password} attribute for each @code{password} element "
4525 "found in the document. The attribute value will be empty but still exist."
4527 "When the @option{--inquire} option is passed then all remaining non-option "
4528 "arguments are retrieved via a server @emph{INQUIRE}."
4530 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4531 "expression syntax."
4534 new_command("IMPORT", 0, 1, import_command
, _(
4535 "IMPORT <content>[<TAB>[!]element[<TAB>[!]child[..]]]\n"
4536 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4538 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4539 "argument is raw @abbr{XML} data. The content is created as a child of the "
4540 "specified element path and will overwrite an existing element of the same "
4541 "name. If an element of the element path does not exist then it will be "
4544 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4548 new_command("DUMP", 0, 1, dump_command
, _(
4550 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4551 "dumping a specific node."
4554 new_command("LOCK", 0, 0, lock_command
, _(
4556 "Locks the mutex associated with the opened file. This prevents other clients "
4557 "from sending commands to the same opened file until the client "
4558 "that sent this command either disconnects or sends the @code{UNLOCK} "
4559 "command. @xref{UNLOCK}."
4562 new_command("UNLOCK", 1, 0, unlock_command
, _(
4564 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4565 "a commands' @option{lock} option. @xref{LOCK}."
4568 new_command("GETCONFIG", 1, 1, getconfig_command
, _(
4569 "GETCONFIG [filename] <parameter>\n"
4570 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4571 "data response. If no file has been opened then the value for @var{filename} "
4572 "or the default from the @samp{global} section will be returned. If a file "
4573 "has been opened and no @var{filename} is specified, a value previously "
4574 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4577 new_command("OPTION", 1, 1, option_command
, _(
4578 "OPTION <NAME>=<VALUE>\n"
4579 "Sets a client option @var{name} to @var{value}. The value for an option is "
4580 "kept for the duration of the connection."
4583 "@item DISABLE-PINENTRY\n"
4584 " Disable use of @command{pinentry} for passphrase retrieval. When set, a "
4585 " server inquire is sent to the client to obtain the passphrase. This option "
4586 " may be set as needed before the @pxref{OPEN}, @pxref{PASSWD}, and "
4587 " @pxref{SAVE} commands."
4590 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4593 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4596 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4598 "@item PINENTRY-DESC\n"
4599 " Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4601 "@item PINENTRY-TITLE\n"
4602 " Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
4604 "@item PINENTRY-PROMPT\n"
4605 " Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
4608 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4610 "@item LC-MESSAGES\n"
4611 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4614 " Associates the thread ID of the connection with the specified textual "
4615 "representation. Useful for debugging log messages."
4617 "@item LOCK-TIMEOUT\n"
4618 " When not @code{0}, the duration in tenths of a second to wait for the file "
4619 "mutex which has been locked by another thread to be released before returning "
4620 "an error. When @code{-1}, then an error will be returned immediately."
4624 new_command("LS", 1, 1, ls_command
, _(
4626 "Lists the available data files stored in the data directory "
4627 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4630 new_command("RESET", 1, 1, NULL
, _(
4632 "Closes the currently opened file but keeps any previously set client options."
4635 new_command("NOP", 1, 1, NULL
, _(
4637 "Does nothing. Always returns successfully."
4640 /* !END-HELP-TEXT! */
4641 new_command ("CANCEL", 1, 1, NULL
, NULL
);
4642 new_command ("END", 1, 1, NULL
, NULL
);
4643 new_command ("BYE", 1, 1, NULL
, NULL
);
4646 for (i
= 0; command_table
[i
]; i
++);
4647 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
4652 register_commands (assuan_context_t ctx
)
4656 for (; command_table
[i
]; i
++)
4658 if (!command_table
[i
]->handler
)
4661 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
4662 command_table
[i
]->handler
,
4663 command_table
[i
]->help
);
4668 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
4672 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
4676 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
4680 return assuan_register_post_cmd_notify (ctx
, command_finalize
);