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
78 /* The GETCONFIG command, for example, may fail because pwmd lost the
79 * gpg-agent connection. Update the recovered agent ctx. */
80 #define UPDATE_AGENT_CTX(client, crypto) do { \
81 if (crypto && crypto->agent && client->crypto && client->crypto->agent \
82 && client->crypto->agent->ctx && \
83 crypto->agent->ctx != client->crypto->agent->ctx) \
85 client->crypto->agent->ctx = crypto->agent->ctx; \
86 crypto->agent->ctx = NULL; \
90 #define UPDATE_AGENT_CTX(client, crypto)
93 struct command_table_s
96 gpg_error_t (*handler
) (assuan_context_t
, char *line
);
99 int unlock
; // unlock the file mutex after validating the checksum
102 static struct command_table_s
**command_table
;
104 static gpg_error_t
do_lock (struct client_s
*client
, int add
);
105 static gpg_error_t
validate_checksum (struct client_s
*,
106 struct cache_data_s
*);
107 static gpg_error_t
update_checksum (struct client_s
*client
);
110 unlock_file_mutex (struct client_s
*client
, int remove
)
114 // OPEN: keep the lock for the same file being reopened.
115 if (client
->flags
& FLAG_KEEP_LOCK
&& client
->flags
& FLAG_HAS_LOCK
)
118 if (!(client
->flags
& FLAG_HAS_LOCK
))
119 return GPG_ERR_NOT_LOCKED
;
121 rc
= cache_unlock_mutex (client
->md5file
, remove
);
123 rc
= GPG_ERR_INV_STATE
;
125 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_LOCK_CMD
);
131 lock_file_mutex (struct client_s
*client
, int add
)
134 int timeout
= config_get_integer (client
->filename
, "cache_timeout");
136 if (client
->flags
& FLAG_HAS_LOCK
)
139 rc
= cache_lock_mutex (client
->ctx
, client
->md5file
,
140 client
->lock_timeout
, add
, timeout
);
142 client
->flags
|= FLAG_HAS_LOCK
;
148 file_modified (struct client_s
*client
, struct command_table_s
*cmd
)
152 if (!(client
->flags
& FLAG_OPEN
))
153 return GPG_ERR_INV_STATE
;
155 rc
= lock_file_mutex (client
, 0);
156 if (!rc
|| rc
== GPG_ERR_NO_DATA
)
158 rc
= validate_checksum (client
, NULL
);
159 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
)
163 if ((cmd
->unlock
&& !(client
->flags
& FLAG_HAS_LOCK
)) || rc
)
164 unlock_file_mutex (client
, 0);
170 parse_xml (assuan_context_t ctx
, int new)
172 struct client_s
*client
= assuan_get_pointer (ctx
);
173 int cached
= client
->doc
!= NULL
;
177 client
->doc
= new_document ();
180 xmlDocDumpFormatMemory (client
->doc
,
181 (xmlChar
**) & client
->crypto
->plaintext
,
182 (int *) &client
->crypto
->plaintext_len
, 0);
183 if (!client
->crypto
->plaintext
)
185 xmlFreeDoc (client
->doc
);
191 client
->doc
= parse_doc ((char *) client
->crypto
->plaintext
,
192 client
->crypto
->plaintext_len
);
194 return !client
->doc
? GPG_ERR_ENOMEM
: 0;
198 free_client (struct client_s
*client
)
201 xmlFreeDoc (client
->doc
);
204 xfree (client
->filename
);
205 xfree (client
->last_error
);
209 cleanup_crypto_stage2 (client
->crypto
);
210 if (client
->crypto
->pkey_sexp
)
211 gcry_sexp_release (client
->crypto
->pkey_sexp
);
213 client
->crypto
->pkey_sexp
= NULL
;
214 memset (client
->crypto
->sign_grip
, 0,
215 sizeof (client
->crypto
->sign_grip
));
216 memset (client
->crypto
->grip
, 0, sizeof(client
->crypto
->grip
));
219 if (client
->xml_error
)
220 xmlResetError (client
->xml_error
);
224 cleanup_client (struct client_s
*client
)
226 assuan_context_t ctx
= client
->ctx
;
227 struct client_thread_s
*thd
= client
->thd
;
228 struct crypto_s
*crypto
= client
->crypto
;
229 long lock_timeout
= client
->lock_timeout
;
230 int no_pinentry
= (client
->flags
& FLAG_NO_PINENTRY
);
231 struct pinentry_option_s pin_opts
;
233 struct pinentry_option_s agent_pin_opts
;
235 if (crypto
&& crypto
->agent
)
236 memcpy (&agent_pin_opts
, &crypto
->agent
->pinentry_opts
,
237 sizeof(struct pinentry_option_s
));
240 memcpy (&pin_opts
, &client
->pinentry_opts
, sizeof(struct pinentry_option_s
));
241 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
242 free_client (client
);
243 memset (client
, 0, sizeof (struct client_s
));
244 client
->crypto
= crypto
;
247 client
->lock_timeout
= lock_timeout
;
248 memcpy (&client
->pinentry_opts
, &pin_opts
, sizeof(struct pinentry_option_s
));
249 client
->flags
|= no_pinentry
? FLAG_NO_PINENTRY
: 0;
251 if (crypto
&& crypto
->agent
)
252 memcpy (&crypto
->agent
->pinentry_opts
, &agent_pin_opts
,
253 sizeof(struct pinentry_option_s
));
258 open_finalize (assuan_context_t ctx
, char *key
, size_t keylen
)
260 struct client_s
*client
= assuan_get_pointer (ctx
);
262 struct cache_data_s
*cdata
= cache_get_data (client
->md5file
);
263 int cached
= 0, keyarg
= key
? 1 : 0;
265 unsigned char *salted_key
= NULL
;
268 client
->crypto
->filename
= str_dup (client
->filename
);
269 if (cdata
|| client
->flags
& FLAG_NEW
)
271 if (!(client
->flags
& FLAG_NEW
))
273 rc
= decrypt_xml (client
->crypto
, cdata
->doc
, cdata
->doclen
);
278 cached
= cdata
!= NULL
;
282 if (!key
&& !IS_PKCS (client
->crypto
))
284 if (client
->flags
& FLAG_NO_PINENTRY
)
286 rc
= inquire_passphrase (ctx
, "PASSPHRASE", (unsigned char **)&key
,
293 client
->pinentry_opts
.timeout
= config_get_integer (client
->filename
,
295 rc
= getpin_common (client
->ctx
, client
->filename
, PINENTRY_OPEN
,
302 if (!IS_PKCS (client
->crypto
))
304 algo
= cipher_to_gcrypt (client
->crypto
->hdr
.flags
);
305 rc
= hash_key (algo
, client
->crypto
->hdr
.salt
,
306 sizeof(client
->crypto
->hdr
.salt
), key
, keylen
,
307 (void **)&salted_key
, &keysize
);
312 rc
= decrypt_data (client
->ctx
, client
->crypto
, salted_key
, keysize
);
317 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
319 return GPG_ERR_ENOMEM
;
321 cdata
->key
= salted_key
;
322 cdata
->keylen
= keysize
;
325 rc
= decrypt_data (client
->ctx
, client
->crypto
, NULL
, 0);
330 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
333 int timeout
= config_get_integer (client
->filename
,
339 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
341 rc
= encrypt_xml (NULL
, cache_key
, cache_keysize
,
342 GCRY_CIPHER_AES
, client
->crypto
->plaintext
,
343 client
->crypto
->plaintext_len
, &cdata
->doc
,
344 &cdata
->doclen
, &cache_iv
, &cache_blocksize
,
346 if (!rc
&& !(client
->flags
& FLAG_NEW
) && IS_PKCS (client
->crypto
))
348 rc
= gcry_sexp_build ((gcry_sexp_t
*) & cdata
->pubkey
, NULL
,
349 "%S", client
->crypto
->pkey_sexp
);
354 gcry_sexp_release (cdata
->sigkey
);
356 cdata
->sigkey
= NULL
;
358 if (!rc
&& IS_PKCS (client
->crypto
))
359 rc
= gcry_sexp_build ((gcry_sexp_t
*) &cdata
->sigkey
, NULL
,
360 "%S", client
->crypto
->sigpkey_sexp
);
363 && !cache_add_file (client
->md5file
, (client
->flags
& FLAG_NEW
)
364 ? NULL
: client
->crypto
->grip
, cdata
, timeout
))
368 free_cache_data_once (cdata
);
369 xmlFreeDoc (client
->doc
);
376 client
->flags
|= FLAG_OPEN
;
381 update_checksum (client
);
383 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
384 rc
= do_lock (client
, 0);
390 req_cleanup (void *arg
)
395 strv_free ((char **) arg
);
399 parse_open_opt_lock (void *data
, void *value
)
401 struct client_s
*client
= data
;
403 client
->opts
|= OPT_LOCK_ON_OPEN
;
408 parse_opt_inquire (void *data
, void *value
)
410 struct client_s
*client
= data
;
413 client
->opts
|= OPT_INQUIRE
;
418 update_checksum (struct client_s
*client
)
422 struct cache_data_s
*cdata
;
423 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
430 cdata
= cache_get_data (client
->md5file
);
434 cdata
->crc
= xmalloc (len
);
435 memcpy (cdata
->crc
, crc
, len
);
442 validate_checksum (struct client_s
*client
, struct cache_data_s
*cdata
)
449 if (cdata
&& !cdata
->crc
)
450 return GPG_ERR_CHECKSUM
;
452 rc
= get_checksum (client
->filename
, &crc
, &len
);
457 n
= memcmp (cdata
->crc
, crc
, len
);
458 else if (client
->crc
)
459 n
= memcmp (client
->crc
, crc
, len
);
462 return n
? GPG_ERR_CHECKSUM
: 0;
466 do_open (assuan_context_t ctx
, const char *filename
, const char *password
)
468 struct client_s
*client
= assuan_get_pointer (ctx
);
469 struct cache_data_s
*cdata
;
473 if (!valid_filename (filename
))
474 return GPG_ERR_INV_VALUE
;
476 gcry_md_hash_buffer (GCRY_MD_MD5
, client
->md5file
, filename
,
478 client
->filename
= str_dup (filename
);
479 if (!client
->filename
)
480 return GPG_ERR_ENOMEM
;
483 cdata
= cache_get_data (client
->md5file
);
484 if (cdata
&& cdata
->doc
)
488 rc
= validate_checksum (client
, cdata
);
489 /* This will check that the key is cached in the agent which needs to
490 * be determined for files that share a keygrip. */
493 rc
= cache_iscached (client
->filename
, &defer
);
495 rc
= GPG_ERR_KEY_EXPIRED
;
499 if (!rc
&& client
->thd
->remote
500 && config_get_boolean (NULL
, "tcp_require_key"))
501 rc
= GPG_ERR_KEY_EXPIRED
;
504 if (!rc
&& !password
)
506 rc
= read_data_header (client
->filename
, &client
->crypto
->hdr
,
510 if (IS_PKCS (client
->crypto
))
512 gcry_sexp_build (&client
->crypto
->pkey_sexp
, NULL
, "%S",
514 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
515 client
->crypto
->grip
);
516 gcry_sexp_build (&client
->crypto
->sigpkey_sexp
, NULL
, "%S",
518 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
519 client
->crypto
->sign_grip
);
524 rc
= open_finalize (ctx
, NULL
, 0);
530 /* There was an error accessing the file so clear the cache entry. The
531 * real error will be returned from read_data_file() since the file
532 * may have only disappeared. */
535 log_write ("%s: %s", filename
, pwmd_strerror (rc
));
536 rc
= cache_clear (client
->md5file
);
537 send_status_all (STATUS_CACHE
, NULL
);
544 rc
= read_data_file (client
->filename
, client
->crypto
);
547 if (gpg_err_source (rc
) != GPG_ERR_SOURCE_DEFAULT
||
548 gpg_err_code (rc
) != GPG_ERR_ENOENT
)
550 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
554 client
->flags
|= FLAG_NEW
;
555 memset (client
->crypto
->grip
, 0, sizeof (client
->crypto
->grip
));
556 memset (client
->crypto
->sign_grip
, 0,
557 sizeof (client
->crypto
->sign_grip
));
558 rc
= open_finalize (ctx
, NULL
, 0);
562 if (password
&& IS_PKCS (client
->crypto
))
565 rc
= set_agent_passphrase (client
->crypto
, password
, strlen (password
));
571 rc
= open_finalize (ctx
, (char *)password
, password
? strlen (password
) : 0);
576 open_command (assuan_context_t ctx
, char *line
)
579 struct client_s
*client
= assuan_get_pointer (ctx
);
580 char **req
, *password
= NULL
, *filename
;
581 unsigned char md5file
[16];
583 struct argv_s
*args
[] = {
584 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
588 rc
= parse_options (&line
, args
, client
);
590 return send_error (ctx
, rc
);
592 req
= str_split (line
, " ", 2);
594 return send_error (ctx
, GPG_ERR_SYNTAX
);
597 if (client
->thd
->remote
)
599 rc
= tls_validate_access (client
, req
[0]);
602 if (rc
== GPG_ERR_INV_USER_ID
)
603 log_write (_("client validation failed for file '%s'"), req
[0]);
605 return send_error (ctx
, rc
);
608 /* Remote pinentries are not supported. */
609 client
->flags
|= FLAG_NO_PINENTRY
;
613 pthread_cleanup_push (req_cleanup
, req
);
615 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
616 /* This client may have locked a different file with ISCACHED --lock than
617 * the current filename. This will remove that lock. */
618 same_file
= !memcmp (md5file
, client
->md5file
, 16) ? 1 : 0;
619 if (client
->flags
& FLAG_OPEN
||
620 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
622 typeof (client
->opts
) opts
= client
->opts
;
623 typeof (client
->flags
) flags
= client
->flags
;
626 client
->flags
|= FLAG_KEEP_LOCK
;
628 cleanup_client (client
);
630 client
->flags
|= flags
;
631 client
->flags
&= ~(FLAG_LOCK_CMD
| FLAG_NEW
);
633 client
->flags
&= ~(FLAG_HAS_LOCK
);
636 /* Need to lock the mutex here because file_modified() cannot without
637 * knowing the filename. */
638 memcpy (client
->md5file
, md5file
, 16);
639 rc
= lock_file_mutex (client
, 1);
642 password
= req
[1] && *req
[1] ? req
[1] : NULL
;
644 if (IS_PKCS (client
->crypto
))
645 rc
= set_pinentry_mode (client
->crypto
->agent
,
646 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
651 rc
= do_open (ctx
, filename
, password
);
653 cleanup_client (client
);
655 cleanup_crypto_stage1 (client
->crypto
);
659 pthread_cleanup_pop (1);
661 if (!rc
&& client
->flags
& FLAG_NEW
)
662 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
665 (void) kill_scd (client
->crypto
->agent
);
667 return send_error (ctx
, rc
);
671 parse_save_opt_no_passphrase (void *data
, void *value
)
673 struct client_s
*client
= data
;
676 client
->opts
|= OPT_NO_PASSPHRASE
;
681 parse_save_opt_no_agent (void *data
, void *value
)
683 struct client_s
*client
= data
;
685 client
->opts
|= OPT_NO_AGENT
;
690 parse_save_opt_cipher (void *data
, void *value
)
692 struct client_s
*client
= data
;
693 int algo
= cipher_string_to_gcrypt ((char *) value
);
694 file_header_t
*hdr
= &client
->crypto
->save
.hdr
;
697 return GPG_ERR_INV_VALUE
;
699 hdr
->flags
= set_cipher_flag (hdr
->flags
, algo
);
704 parse_save_opt_keygrip (void *data
, void *value
)
706 struct client_s
*client
= data
;
708 if (!IS_PKCS (client
->crypto
))
709 return GPG_ERR_INV_ARG
;
712 if (client
->crypto
->save
.pkey
)
713 gcry_sexp_release (client
->crypto
->save
.pkey
);
715 client
->crypto
->save
.pkey
= NULL
;
716 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.pkey
);
718 return GPG_ERR_INV_ARG
;
723 parse_save_opt_sign_keygrip (void *data
, void *value
)
725 struct client_s
*client
= data
;
727 if (!IS_PKCS (client
->crypto
))
728 return GPG_ERR_INV_ARG
;
731 if (client
->crypto
->save
.sigpkey
)
732 gcry_sexp_release (client
->crypto
->save
.sigpkey
);
734 client
->crypto
->save
.sigpkey
= NULL
;
735 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.sigpkey
);
737 return GPG_ERR_INV_ARG
;
742 parse_opt_s2k_count (void *data
, void *value
)
744 struct client_s
*client
= data
;
748 return GPG_ERR_INV_VALUE
;
750 client
->crypto
->save
.s2k_count
= strtoul (v
, NULL
, 10);
755 parse_save_opt_iterations (void *data
, void *value
)
757 struct client_s
*client
= data
;
762 return GPG_ERR_INV_VALUE
;
765 n
= strtoull (v
, &p
, 10);
766 if (n
== UINT64_MAX
&& errno
)
767 return gpg_error_from_syserror ();
769 return GPG_ERR_INV_VALUE
;
771 client
->crypto
->save
.hdr
.iterations
= n
;
776 save_finalize (assuan_context_t ctx
)
778 struct client_s
*client
= assuan_get_pointer (ctx
);
780 xmlChar
*xmlbuf
= NULL
;
785 xmlDocDumpFormatMemory (client
->doc
, &xmlbuf
, &xmlbuflen
, 0);
787 return GPG_ERR_ENOMEM
;
789 pthread_cleanup_push (xmlFree
, xmlbuf
);
791 if (!use_agent
|| ((client
->flags
& FLAG_NEW
)
792 && (client
->opts
& OPT_NO_AGENT
))
793 || !(client
->crypto
->hdr
.flags
& PWMD_FLAG_PKCS
))
795 rc
= export_common (ctx
, client
->flags
& FLAG_NO_PINENTRY
,
796 client
->crypto
, xmlbuf
, xmlbuflen
, client
->filename
,
797 NULL
, &key
, &keylen
, 1, 1);
802 gcry_sexp_t pubkey
= client
->crypto
->save
.pkey
;
803 gcry_sexp_t sigkey
= client
->crypto
->save
.sigpkey
;
806 pubkey
= client
->crypto
->pkey_sexp
;
810 sigkey
= client
->crypto
->sigpkey_sexp
;
813 gcry_sexp_build (&client
->crypto
->save
.sigpkey
, 0, "%S", pubkey
);
814 sigkey
= client
->crypto
->save
.sigpkey
;
818 rc
= encrypt_data_file (client
->ctx
, client
->crypto
, pubkey
, sigkey
,
819 client
->filename
, xmlbuf
, xmlbuflen
);
820 if (pubkey
== client
->crypto
->save
.pkey
)
824 gcry_sexp_release (client
->crypto
->pkey_sexp
);
825 client
->crypto
->pkey_sexp
= client
->crypto
->save
.pkey
;
826 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
827 client
->crypto
->grip
);
830 gcry_sexp_release (pubkey
);
832 client
->crypto
->save
.pkey
= NULL
;
836 gcry_pk_get_keygrip (sigkey
, client
->crypto
->sign_grip
);
838 if (sigkey
== client
->crypto
->save
.sigpkey
)
842 if (client
->crypto
->sigpkey_sexp
)
843 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
845 client
->crypto
->sigpkey_sexp
= client
->crypto
->save
.sigpkey
;
846 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
847 client
->crypto
->sign_grip
);
850 gcry_sexp_release (sigkey
);
852 client
->crypto
->save
.sigpkey
= NULL
;
861 rc
= save_common (client
->filename
, client
->crypto
, xmlbuf
, xmlbuflen
,
862 key
, keylen
, &cached
, client
->opts
& OPT_NO_AGENT
);
866 if (!rc
&& (!cached
|| (client
->flags
& FLAG_NEW
)))
867 send_status_all (STATUS_CACHE
, NULL
);
871 update_checksum (client
);
872 client
->flags
&= ~(FLAG_NEW
);
878 pthread_cleanup_pop (1); // xmlFree
883 parse_opt_reset (void *data
, void *value
)
885 struct client_s
*client
= data
;
888 client
->opts
|= OPT_RESET
;
893 save_command (assuan_context_t ctx
, char *line
)
895 struct client_s
*client
= assuan_get_pointer (ctx
);
898 struct argv_s
*args
[] = {
899 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
,
900 parse_save_opt_no_passphrase
},
901 &(struct argv_s
) {"cipher", OPTION_TYPE_ARG
, parse_save_opt_cipher
},
902 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
904 &(struct argv_s
) {"keygrip", OPTION_TYPE_ARG
, parse_save_opt_keygrip
},
905 &(struct argv_s
) {"sign-keygrip", OPTION_TYPE_ARG
,
906 parse_save_opt_sign_keygrip
},
907 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
908 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
909 &(struct argv_s
) {"cipher-iterations", OPTION_TYPE_ARG
,
910 parse_save_opt_iterations
},
911 &(struct argv_s
) {"no-agent", OPTION_TYPE_NOARG
,
912 parse_save_opt_no_agent
},
916 cleanup_save (&client
->crypto
->save
);
917 memcpy (&client
->crypto
->save
.hdr
, &client
->crypto
->hdr
,
918 sizeof (file_header_t
));
919 client
->crypto
->save
.s2k_count
=
920 config_get_ulong (client
->filename
, "s2k_count");
922 if (client
->flags
& FLAG_NEW
)
923 client
->crypto
->save
.hdr
.iterations
=
924 config_get_ulonglong (client
->filename
, "cipher_iterations");
926 rc
= parse_options (&line
, args
, client
);
928 return send_error (ctx
, rc
);
930 if (!(client
->flags
& FLAG_NEW
))
931 client
->opts
&= ~OPT_NO_AGENT
;
932 else if (client
->opts
& OPT_NO_AGENT
)
934 client
->crypto
->save
.hdr
.flags
&= ~PWMD_FLAG_PKCS
;
935 client
->crypto
->hdr
.flags
&= ~PWMD_FLAG_PKCS
;
938 if ((client
->opts
& OPT_NO_PASSPHRASE
) && !(client
->flags
& FLAG_NEW
)
939 && !(client
->opts
& OPT_INQUIRE
))
940 return send_error (ctx
, GPG_ERR_WRONG_KEY_USAGE
);
942 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
943 return send_error (ctx
, gpg_error_from_syserror ());
945 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
947 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
948 return send_error (ctx
, GPG_ERR_ENOANO
);
952 rc
= cache_iscached (client
->filename
, &defer
);
955 log_write ("%s: %s", client
->filename
,
956 pwmd_strerror (GPG_ERR_KEY_EXPIRED
));
957 client
->opts
|= OPT_RESET
;
960 if (client
->opts
& OPT_RESET
)
962 rc
= cache_clear (client
->md5file
);
964 return send_error (ctx
, rc
);
966 send_status_all (STATUS_CACHE
, NULL
);
969 rc
= update_element_mtime (xmlDocGetRootElement (client
->doc
));
971 return send_error (ctx
, rc
);
974 if (!rc
&& use_agent
&& !client
->crypto
->save
.pkey
&&
975 (client
->flags
& FLAG_NEW
|| client
->opts
& OPT_INQUIRE
)
976 && !(client
->opts
& OPT_NO_AGENT
))
978 rc
= send_status (client
->ctx
, STATUS_GENKEY
, NULL
);
981 struct inquire_data_s idata
= { 0 };
982 char *params
= client
->opts
& OPT_INQUIRE
? NULL
983 : default_key_params (client
->crypto
);
985 pthread_cleanup_push (xfree
, params
);
986 idata
.crypto
= client
->crypto
;
991 idata
.len
= strlen (params
);
996 client
->crypto
->agent
->inquire_data
= &idata
;
997 client
->crypto
->agent
->inquire_cb
= NULL
;
998 rc
= generate_key (client
->crypto
, params
,
999 (client
->opts
& OPT_NO_PASSPHRASE
), 1);
1000 pthread_cleanup_pop (1);
1006 rc
= save_finalize (ctx
);
1008 cleanup_crypto_stage1 (client
->crypto
);
1010 (void) kill_scd (client
->crypto
->agent
);
1012 return send_error (ctx
, rc
);
1016 do_delete (assuan_context_t ctx
, char *line
)
1018 struct client_s
*client
= assuan_get_pointer (ctx
);
1023 if (strchr (line
, '\t'))
1024 req
= str_split (line
, "\t", 0);
1026 req
= str_split (line
, " ", 0);
1029 return GPG_ERR_SYNTAX
;
1031 n
= find_root_element (client
->doc
, &req
, &rc
, NULL
, 0, 0);
1039 * No sub-node defined. Remove the entire node (root element).
1045 rc
= unlink_node (n
);
1054 find_elements (client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
1062 rc
= unlink_node (n
);
1070 delete_command (assuan_context_t ctx
, char *line
)
1072 struct client_s
*client
= assuan_get_pointer (ctx
);
1074 struct argv_s
*args
[] = {
1075 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1079 rc
= parse_options (&line
, args
, client
);
1081 return send_error (ctx
, rc
);
1083 if (client
->opts
& OPT_INQUIRE
)
1085 unsigned char *result
;
1088 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1090 return send_error (ctx
, rc
);
1092 line
= (char *) result
;
1095 rc
= do_delete (ctx
, line
);
1097 if (client
->opts
& OPT_INQUIRE
)
1100 return send_error (ctx
, rc
);
1104 store_command (assuan_context_t ctx
, char *line
)
1106 struct client_s
*client
= assuan_get_pointer (ctx
);
1109 unsigned char *result
;
1111 xmlNodePtr n
, parent
;
1113 char *content
= NULL
;
1115 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1117 return send_error (ctx
, rc
);
1119 req
= str_split ((char *) result
, "\t", 0);
1123 return send_error (ctx
, GPG_ERR_SYNTAX
);
1125 len
= strv_length (req
);
1126 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1127 if (*(req
+ 1) && !valid_element_path (req
, has_content
))
1130 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1133 if (has_content
|| !*req
[len
- 1])
1136 content
= req
[len
- 1];
1137 req
[len
- 1] = NULL
;
1141 n
= find_root_element (client
->doc
, &req
, &rc
, NULL
, 0, 0);
1142 if (rc
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1144 rc
= new_root_element (client
->doc
, *req
);
1148 return send_error (ctx
, GPG_ERR_SYNTAX
);
1157 return send_error (ctx
, rc
);
1162 if (req
[1] && *req
[1])
1165 parent
= create_elements_cb (n
, req
+ 1, &rc
, NULL
);
1167 parent
= find_elements (client
->doc
, n
->children
, req
+ 1, &rc
,
1168 NULL
, NULL
, create_elements_cb
, 0, 0, NULL
,
1174 n
= find_text_node (parent
->children
);
1176 xmlNodeSetContent (n
, (xmlChar
*) content
);
1178 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1180 update_element_mtime (parent
);
1185 return send_error (ctx
, rc
);
1189 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1194 int progress
= config_get_integer ("global", "xfer_progress");
1198 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1199 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1200 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1208 if (sent
+ to_send
> total
)
1209 to_send
= total
- sent
;
1211 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1212 flush
? 0 : to_send
);
1215 sent
+= flush
? 0 : to_send
;
1217 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1218 (sent
== total
&& flush
))
1219 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1221 if (!flush
&& !rc
&& sent
== total
)
1228 while (!rc
&& sent
< total
);
1234 do_get (assuan_context_t ctx
, char *line
)
1236 struct client_s
*client
= assuan_get_pointer (ctx
);
1241 req
= str_split (line
, "\t", 0);
1246 return GPG_ERR_SYNTAX
;
1249 n
= find_root_element (client
->doc
, &req
, &rc
, NULL
, 0, 0);
1258 find_elements (client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
1265 if (!n
|| !n
->children
)
1266 return GPG_ERR_NO_DATA
;
1268 n
= find_text_node (n
->children
);
1269 if (!n
|| !n
->content
|| !*n
->content
)
1270 return GPG_ERR_NO_DATA
;
1272 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1277 get_command (assuan_context_t ctx
, char *line
)
1279 struct client_s
*client
= assuan_get_pointer (ctx
);
1281 struct argv_s
*args
[] = {
1282 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1286 rc
= parse_options (&line
, args
, client
);
1288 return send_error (ctx
, rc
);
1290 if (client
->opts
& OPT_INQUIRE
)
1292 unsigned char *result
;
1295 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1297 return send_error (ctx
, rc
);
1299 line
= (char *) result
;
1302 rc
= do_get (ctx
, line
);
1304 if (client
->opts
& OPT_INQUIRE
)
1307 return send_error (ctx
, rc
);
1310 static void list_command_cleanup1 (void *arg
);
1312 realpath_command (assuan_context_t ctx
, char *line
)
1314 struct string_s
*string
= NULL
;
1316 struct client_s
*client
= assuan_get_pointer (ctx
);
1317 struct argv_s
*args
[] = {
1318 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1322 rc
= parse_options (&line
, args
, client
);
1324 return send_error (ctx
, rc
);
1326 if (client
->opts
& OPT_INQUIRE
)
1328 unsigned char *result
;
1331 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1333 return send_error (ctx
, rc
);
1335 line
= (char *) result
;
1338 rc
= build_realpath (client
->doc
, line
, &string
);
1341 pthread_cleanup_push (list_command_cleanup1
, string
);
1342 rc
= xfer_data (ctx
, string
->str
, string
->len
);
1343 pthread_cleanup_pop (1);
1346 if (client
->opts
& OPT_INQUIRE
)
1349 return send_error (ctx
, rc
);
1353 list_command_cleanup1 (void *arg
)
1356 string_free ((struct string_s
*) arg
, 1);
1360 list_command_cleanup2 (void *arg
)
1362 struct element_list_s
*elements
= arg
;
1368 int total
= slist_length (elements
->list
);
1371 for (i
= 0; i
< total
; i
++)
1373 char *tmp
= slist_nth_data (elements
->list
, i
);
1377 slist_free (elements
->list
);
1380 if (elements
->prefix
)
1381 xfree (elements
->prefix
);
1384 strv_free (elements
->req
);
1391 parse_list_opt_norecurse (void *data
, void *value
)
1393 struct client_s
*client
= data
;
1395 client
->opts
&= ~(OPT_LIST_RECURSE
);
1400 parse_list_opt_verbose (void *data
, void *value
)
1402 struct client_s
*client
= data
;
1404 client
->opts
|= OPT_LIST_VERBOSE
;
1409 parse_list_opt_target (void *data
, void *value
)
1411 struct client_s
*client
= data
;
1413 client
->opts
|= OPT_LIST_WITH_TARGET
;
1418 parse_list_opt_all (void *data
, void *value
)
1420 struct client_s
*client
= data
;
1422 client
->opts
|= OPT_LIST_ALL
;
1427 list_path_once (struct client_s
*client
, char *line
,
1428 struct element_list_s
*elements
, struct string_s
*result
)
1432 elements
->req
= str_split (line
, " ", 0);
1434 strv_printf (&elements
->req
, "%s", line
);
1436 rc
= create_path_list (client
->doc
, elements
, *elements
->req
);
1437 if (rc
== GPG_ERR_ELOOP
&& elements
->verbose
)
1444 int total
= slist_length (elements
->list
);
1448 rc
= GPG_ERR_NO_DATA
;
1454 for (i
= 0; i
< total
; i
++)
1456 char *tmp
= slist_nth_data (elements
->list
, i
);
1458 string_append_printf (result
, "%s%s", tmp
,
1459 i
+ 1 == total
? "" : "\n");
1465 rc
= GPG_ERR_NO_DATA
;
1472 has_list_flag (char *path
, char *flags
)
1476 while (*p
&& *++p
!= ' ');
1485 for (f
= flags
; *f
&& *f
!= ' '; f
++)
1496 do_list (assuan_context_t ctx
, char *line
)
1498 struct client_s
*client
= assuan_get_pointer (ctx
);
1500 struct element_list_s
*elements
= NULL
;
1502 elements
= xcalloc (1, sizeof (struct element_list_s
));
1504 return GPG_ERR_ENOMEM
;
1506 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1508 (client
->opts
& OPT_LIST_VERBOSE
) | (client
->opts
& OPT_LIST_WITH_TARGET
);
1509 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1511 if (!line
|| !*line
)
1513 struct string_s
*str
= NULL
;
1515 pthread_cleanup_push (list_command_cleanup2
, elements
);
1516 rc
= list_root_elements (client
->doc
, &str
, elements
->verbose
,
1517 elements
->with_target
);
1518 pthread_cleanup_pop (1);
1519 pthread_cleanup_push (list_command_cleanup1
, str
);
1523 if (client
->opts
& OPT_LIST_ALL
)
1525 char **roots
= str_split (str
->str
, "\n", 0);
1528 pthread_cleanup_push (req_cleanup
, roots
);
1529 string_truncate (str
, 0);
1531 for (p
= roots
; *p
; p
++)
1533 if (strchr (*p
, ' '))
1535 if (has_list_flag (*p
, "EO"))
1537 string_append_printf (str
, "%s%s", *p
,
1538 *(p
+ 1) ? "\n" : "");
1543 elements
= xcalloc (1, sizeof (struct element_list_s
));
1546 rc
= GPG_ERR_ENOMEM
;
1550 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1552 (client
->opts
& OPT_LIST_VERBOSE
) | (client
->opts
&
1553 OPT_LIST_WITH_TARGET
);
1554 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1555 pthread_cleanup_push (list_command_cleanup2
, elements
);
1556 rc
= list_path_once (client
, *p
, elements
, str
);
1557 pthread_cleanup_pop (1);
1562 string_append (str
, "\n");
1565 pthread_cleanup_pop (1);
1569 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1572 pthread_cleanup_pop (1);
1576 pthread_cleanup_push (list_command_cleanup2
, elements
);
1577 struct string_s
*str
= string_new (NULL
);
1578 pthread_cleanup_push (list_command_cleanup1
, str
);
1579 rc
= list_path_once (client
, line
, elements
, str
);
1581 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1583 pthread_cleanup_pop (1);
1584 pthread_cleanup_pop (1);
1589 list_command (assuan_context_t ctx
, char *line
)
1591 struct client_s
*client
= assuan_get_pointer (ctx
);
1593 struct argv_s
*args
[] = {
1594 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
,
1595 parse_list_opt_norecurse
},
1596 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_list_opt_verbose
},
1597 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1598 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
,
1599 parse_list_opt_target
},
1600 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_all
},
1604 if (disable_list_and_dump
== 1)
1605 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1607 client
->opts
|= OPT_LIST_RECURSE
;
1608 rc
= parse_options (&line
, args
, client
);
1610 return send_error (ctx
, rc
);
1612 if (client
->opts
& OPT_LIST_ALL
)
1613 client
->opts
|= OPT_LIST_RECURSE
| OPT_LIST_VERBOSE
;
1615 if (client
->opts
& OPT_INQUIRE
)
1617 unsigned char *result
;
1620 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1622 return send_error (ctx
, rc
);
1624 line
= (char *) result
;
1627 rc
= do_list (ctx
, line
);
1629 if (client
->opts
& OPT_INQUIRE
)
1632 return send_error (ctx
, rc
);
1636 * req[0] - element path
1639 attribute_list (assuan_context_t ctx
, char **req
)
1641 struct client_s
*client
= assuan_get_pointer (ctx
);
1642 char **attrlist
= NULL
;
1650 if (!req
|| !req
[0])
1651 return GPG_ERR_SYNTAX
;
1653 if ((path
= str_split (req
[0], "\t", 0)) == NULL
)
1656 * The first argument may be only a root element.
1658 if ((path
= str_split (req
[0], " ", 0)) == NULL
)
1659 return GPG_ERR_SYNTAX
;
1662 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
1672 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
1673 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1684 for (a
= n
->properties
; a
; a
= a
->next
)
1688 if ((pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *))) == NULL
)
1691 strv_free (attrlist
);
1693 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1694 pwmd_strerror (GPG_ERR_ENOMEM
));
1695 return GPG_ERR_ENOMEM
;
1700 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
,
1702 && an
->content
? (char *) an
->content
: "");
1706 strv_free (attrlist
);
1707 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1708 pwmd_strerror (GPG_ERR_ENOMEM
));
1709 return GPG_ERR_ENOMEM
;
1712 attrlist
[++i
] = NULL
;
1716 return GPG_ERR_NO_DATA
;
1718 line
= strv_join ("\n", attrlist
);
1722 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1723 pwmd_strerror (GPG_ERR_ENOMEM
));
1724 strv_free (attrlist
);
1725 return GPG_ERR_ENOMEM
;
1728 pthread_cleanup_push (xfree
, line
);
1729 pthread_cleanup_push (req_cleanup
, attrlist
);
1730 rc
= xfer_data (ctx
, line
, strlen (line
));
1731 pthread_cleanup_pop (1);
1732 pthread_cleanup_pop (1);
1737 * req[0] - attribute
1738 * req[1] - element path
1741 attribute_delete (struct client_s
*client
, char **req
)
1747 if (!req
|| !req
[0] || !req
[1])
1748 return GPG_ERR_SYNTAX
;
1750 if (!strcmp (req
[0], "_name"))
1751 return GPG_ERR_INV_ATTR
;
1753 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1756 * The first argument may be only a root element.
1758 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1759 return GPG_ERR_SYNTAX
;
1762 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
1768 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
1769 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1774 rc
= delete_attribute (n
, (xmlChar
*) req
[0]);
1782 create_element_path (struct client_s
*client
,
1783 char ***elements
, gpg_error_t
* rc
, xmlNodePtr parent
)
1785 char **req
= *elements
;
1786 char **req_orig
= strv_dup (req
);
1787 xmlNodePtr n
= NULL
;
1793 *rc
= GPG_ERR_ENOMEM
;
1794 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1795 pwmd_strerror (GPG_ERR_ENOMEM
));
1800 n
= find_root_element (client
->doc
, &req
, rc
, NULL
, 0, 0);
1803 if (*rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1806 *rc
= new_root_element (client
->doc
, req
[0]);
1812 else if (n
== parent
)
1814 *rc
= GPG_ERR_CONFLICT
;
1821 n
= create_target_elements_cb (n
, req
+ 1, rc
, NULL
);
1823 n
= find_elements (client
->doc
, n
->children
, req
+ 1, rc
, NULL
, NULL
,
1824 create_target_elements_cb
, 0, 0, parent
, 0);
1830 * Reset the position of the element tree now that the elements
1831 * have been created.
1836 n
= find_root_element (client
->doc
, &req
, rc
, NULL
, 0, 0);
1840 n
= find_elements (client
->doc
, n
->children
, req
+ 1, rc
,
1841 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1848 strv_free (req_orig
);
1855 * Creates a "target" attribute. When other commands encounter an element with
1856 * this attribute, the element path is modified to the target value. If the
1857 * source element path doesn't exist when using 'ATTR SET target', it is
1858 * created, but the destination element path must exist.
1860 * req[0] - source element path
1861 * req[1] - destination element path
1864 target_attribute (struct client_s
*client
, char **req
)
1866 char **src
, **dst
, *line
= NULL
, **odst
= NULL
;
1870 if (!req
|| !req
[0] || !req
[1])
1871 return GPG_ERR_SYNTAX
;
1873 if ((src
= str_split (req
[0], "\t", 0)) == NULL
)
1876 * The first argument may be only a root element.
1878 if ((src
= str_split (req
[0], " ", 0)) == NULL
)
1879 return GPG_ERR_SYNTAX
;
1882 if (!valid_element_path (src
, 0))
1883 return GPG_ERR_INV_VALUE
;
1885 if ((dst
= str_split (req
[1], "\t", 0)) == NULL
)
1888 * The first argument may be only a root element.
1890 if ((dst
= str_split (req
[1], " ", 0)) == NULL
)
1892 rc
= GPG_ERR_SYNTAX
;
1897 odst
= strv_dup (dst
);
1900 rc
= GPG_ERR_ENOMEM
;
1904 n
= find_root_element (client
->doc
, &dst
, &rc
, NULL
, 0, 0);
1906 * Make sure the destination element path exists.
1913 n
= find_elements (client
->doc
, n
->children
, dst
+ 1, &rc
,
1914 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1919 rc
= validate_target_attribute (client
->doc
, req
[0], n
);
1920 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1923 n
= create_element_path (client
, &src
, &rc
, NULL
);
1927 line
= strv_join ("\t", odst
);
1930 rc
= GPG_ERR_ENOMEM
;
1934 rc
= add_attribute (n
, "target", line
);
1945 * req[0] - attribute
1946 * req[1] - element path
1949 attribute_get (assuan_context_t ctx
, char **req
)
1951 struct client_s
*client
= assuan_get_pointer (ctx
);
1957 if (!req
|| !req
[0] || !req
[1])
1958 return GPG_ERR_SYNTAX
;
1960 if (strchr (req
[1], '\t'))
1962 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1963 return GPG_ERR_SYNTAX
;
1967 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1968 return GPG_ERR_SYNTAX
;
1971 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
1978 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
1979 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1987 if ((a
= xmlGetProp (n
, (xmlChar
*) req
[0])) == NULL
)
1988 return GPG_ERR_NOT_FOUND
;
1990 pthread_cleanup_push (xmlFree
, a
);
1993 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
1995 rc
= GPG_ERR_NO_DATA
;
1997 pthread_cleanup_pop (1);
2006 * req[0] - attribute
2007 * req[1] - element path
2011 attribute_set (struct client_s
*client
, char **req
)
2017 if (!req
|| !req
[0] || !req
[1])
2018 return GPG_ERR_SYNTAX
;
2021 * Reserved attribute names.
2023 if (!strcmp (req
[0], "_name"))
2024 return GPG_ERR_INV_ATTR
;
2025 else if (!strcmp (req
[0], "target"))
2026 return target_attribute (client
, req
+ 1);
2028 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2031 * The first argument may be only a root element.
2033 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2034 return GPG_ERR_SYNTAX
;
2037 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
2044 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
2045 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2051 rc
= add_attribute (n
, req
[0], req
[2]);
2060 * req[1] - attribute name or element path if command is LIST
2061 * req[2] - element path
2062 * req[2] - element path or value
2066 do_attr (assuan_context_t ctx
, char *line
)
2068 struct client_s
*client
= assuan_get_pointer (ctx
);
2072 req
= str_split (line
, " ", 4);
2073 if (!req
|| !req
[0] || !req
[1])
2076 return GPG_ERR_SYNTAX
;
2079 pthread_cleanup_push (req_cleanup
, req
);
2081 if (strcasecmp (req
[0], "SET") == 0)
2082 rc
= attribute_set (client
, req
+ 1);
2083 else if (strcasecmp (req
[0], "GET") == 0)
2084 rc
= attribute_get (ctx
, req
+ 1);
2085 else if (strcasecmp (req
[0], "DELETE") == 0)
2086 rc
= attribute_delete (client
, req
+ 1);
2087 else if (strcasecmp (req
[0], "LIST") == 0)
2088 rc
= attribute_list (ctx
, req
+ 1);
2090 rc
= GPG_ERR_SYNTAX
;
2092 pthread_cleanup_pop (1);
2097 attr_command (assuan_context_t ctx
, char *line
)
2099 struct client_s
*client
= assuan_get_pointer (ctx
);
2101 struct argv_s
*args
[] = {
2102 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2106 rc
= parse_options (&line
, args
, client
);
2108 return send_error (ctx
, rc
);
2110 if (client
->opts
& OPT_INQUIRE
)
2112 unsigned char *result
;
2115 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2117 return send_error (ctx
, rc
);
2119 line
= (char *) result
;
2122 rc
= do_attr (ctx
, line
);
2124 if (client
->opts
& OPT_INQUIRE
)
2127 return send_error (ctx
, rc
);
2131 parse_iscached_opt_lock (void *data
, void *value
)
2133 struct client_s
*client
= data
;
2136 client
->opts
|= OPT_LOCK
;
2141 iscached_command (assuan_context_t ctx
, char *line
)
2143 struct client_s
*client
= assuan_get_pointer (ctx
);
2145 unsigned char md5file
[16];
2146 struct argv_s
*args
[] = {
2147 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2151 if (!line
|| !*line
)
2152 return send_error (ctx
, GPG_ERR_SYNTAX
);
2154 rc
= parse_options (&line
, args
, client
);
2156 return send_error (ctx
, rc
);
2157 else if (!valid_filename (line
))
2158 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2160 rc
= cache_iscached (line
, NULL
);
2161 if (client
->opts
& OPT_LOCK
2162 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2164 gpg_error_t trc
= rc
;
2165 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2166 if (memcmp (md5file
, client
->md5file
, 16))
2167 cleanup_client (client
);
2169 memcpy (client
->md5file
, md5file
, 16);
2170 rc
= do_lock (client
, 1);
2175 return send_error (ctx
, rc
);
2179 clearcache_command (assuan_context_t ctx
, char *line
)
2181 gpg_error_t rc
= 0, all_rc
= 0;
2182 unsigned char md5file
[16];
2186 struct client_thread_s
*once
= NULL
;
2189 MUTEX_LOCK (&cn_mutex
);
2190 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
2192 if (!line
|| !*line
)
2195 t
= slist_length (cn_thread_list
);
2197 for (i
= 0; i
< t
; i
++)
2199 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2204 /* Lock each connected clients' file mutex to prevent any other client
2205 * from accessing the cache entry (the file mutex is locked upon
2206 * command startup). The cache for the entry is not cleared if the
2207 * file mutex is locked by another client to prevent this function
2212 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
2213 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2215 if (pthread_equal (pthread_self (), thd
->tid
))
2219 if (!thd
->cl
->filename
||
2220 cache_iscached (thd
->cl
->filename
,
2221 NULL
) == GPG_ERR_NO_DATA
)
2227 cache_defer_clear (thd
->cl
->md5file
);
2230 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2238 rc
= cache_clear (thd
->cl
->md5file
);
2242 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2249 /* A single data filename was specified. Lock only this data file
2250 * mutex and free the cache entry. */
2253 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2255 if (!memcmp (thd
->cl
->md5file
, md5file
, sizeof (md5file
)))
2257 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0,
2259 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2261 if (pthread_equal (pthread_self (), thd
->tid
))
2268 rc
= cache_clear (thd
->cl
->md5file
);
2269 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2273 cache_defer_clear (thd
->cl
->md5file
);
2281 /* Only connected clients' cache entries have been cleared. Now clear any
2282 * remaining cache entries without clients but only if there wasn't an
2283 * error from above since this would defeat the locking check of the
2284 * remaining entries. */
2290 /* No clients are using the specified file. */
2291 else if (!all_rc
&& !rc
&& !once
)
2293 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2294 rc
= cache_clear (md5file
);
2297 /* Release the connection mutex. */
2298 pthread_cleanup_pop (1);
2302 send_status_all (STATUS_CACHE
, NULL
);
2304 /* One or more files were locked while clearing all cache entries. */
2308 return send_error (ctx
, rc
);
2312 cachetimeout_command (assuan_context_t ctx
, char *line
)
2314 unsigned char md5file
[16];
2316 char **req
= str_split (line
, " ", 0);
2320 if (!req
|| !*req
|| !req
[1])
2323 return send_error (ctx
, GPG_ERR_SYNTAX
);
2327 timeout
= (int) strtol (req
[1], &p
, 10);
2328 if (errno
!= 0 || *p
|| timeout
< -1)
2331 return send_error (ctx
, GPG_ERR_SYNTAX
);
2334 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, req
[0], strlen (req
[0]));
2335 rc
= cache_set_timeout (md5file
, timeout
);
2336 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2339 MUTEX_LOCK (&rcfile_mutex
);
2340 config_set_int_param (&global_config
, req
[0], "cache_timeout", req
[1]);
2341 MUTEX_UNLOCK (&rcfile_mutex
);
2345 return send_error (ctx
, rc
);
2349 dump_command (assuan_context_t ctx
, char *line
)
2353 struct client_s
*client
= assuan_get_pointer (ctx
);
2356 if (disable_list_and_dump
== 1)
2357 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2359 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2363 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2364 pwmd_strerror (GPG_ERR_ENOMEM
));
2365 return send_error (ctx
, GPG_ERR_ENOMEM
);
2368 pthread_cleanup_push (xmlFree
, xml
);
2369 rc
= xfer_data (ctx
, (char *) xml
, len
);
2370 pthread_cleanup_pop (1);
2371 return send_error (ctx
, rc
);
2375 getconfig_command (assuan_context_t ctx
, char *line
)
2377 struct client_s
*client
= assuan_get_pointer (ctx
);
2379 char filename
[255] = { 0 }, param
[747] =
2382 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2384 if (!line
|| !*line
)
2385 return send_error (ctx
, GPG_ERR_SYNTAX
);
2387 if (strchr (line
, ' '))
2389 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2394 if (fp
&& !valid_filename (fp
))
2395 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2397 paramp
= str_down (paramp
);
2398 if (!strcmp (paramp
, "cipher") && fp
)
2400 struct crypto_s
*crypto
= NULL
;
2402 rc
= init_client_crypto (&crypto
);
2405 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2409 gcry_cipher_algo_name (cipher_to_gcrypt (crypto
->hdr
.flags
));
2419 UPDATE_AGENT_CTX (client
, crypto
);
2420 cleanup_crypto (&crypto
);
2421 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2422 return send_error (ctx
, rc
);
2427 else if (!strcmp (paramp
, "cipher_iterations") && fp
)
2429 struct crypto_s
*crypto
= NULL
;
2431 rc
= init_client_crypto (&crypto
);
2434 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2437 tmp
= str_asprintf ("%llu",
2438 (unsigned long long) crypto
->hdr
.
2441 rc
= GPG_ERR_ENOMEM
;
2445 UPDATE_AGENT_CTX (client
, crypto
);
2446 cleanup_crypto (&crypto
);
2447 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2448 return send_error (ctx
, rc
);
2453 else if (!strcmp (paramp
, "passphrase"))
2454 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2456 p
= config_get_value (fp
? fp
: "global", paramp
);
2458 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2460 tmp
= expand_homedir (p
);
2464 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2465 pwmd_strerror (GPG_ERR_ENOMEM
));
2466 return send_error (ctx
, GPG_ERR_ENOMEM
);
2471 pthread_cleanup_push (xfree
, p
);
2472 rc
= xfer_data (ctx
, p
, strlen (p
));
2473 pthread_cleanup_pop (1);
2474 return send_error (ctx
, rc
);
2479 xmlXPathContextPtr xp
;
2480 xmlXPathObjectPtr result
;
2486 xpath_command_cleanup (void *arg
)
2488 struct xpath_s
*xpath
= arg
;
2493 req_cleanup (xpath
->req
);
2496 xmlBufferFree (xpath
->buf
);
2499 xmlXPathFreeObject (xpath
->result
);
2502 xmlXPathFreeContext (xpath
->xp
);
2506 do_xpath (assuan_context_t ctx
, char *line
)
2509 struct client_s
*client
= assuan_get_pointer (ctx
);
2510 struct xpath_s _x
= { 0 };
2511 struct xpath_s
*xpath
= &_x
;
2513 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2515 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2516 return GPG_ERR_ENOMEM
;
2519 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2522 rc
= GPG_ERR_BAD_DATA
;
2527 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2530 rc
= GPG_ERR_BAD_DATA
;
2534 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2536 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2540 rc
= recurse_xpath_nodeset (client
->doc
, xpath
->result
->nodesetval
,
2541 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2545 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2547 rc
= GPG_ERR_NO_DATA
;
2550 else if (xpath
->req
[1])
2556 pthread_cleanup_push (xpath_command_cleanup
, &xpath
);
2557 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2558 xmlBufferLength (xpath
->buf
));
2559 pthread_cleanup_pop (0);
2561 xpath_command_cleanup (xpath
);
2566 xpath_command (assuan_context_t ctx
, char *line
)
2568 struct client_s
*client
= assuan_get_pointer (ctx
);
2570 struct argv_s
*args
[] = {
2571 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2575 if (disable_list_and_dump
== 1)
2576 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2578 rc
= parse_options (&line
, args
, client
);
2580 return send_error (ctx
, rc
);
2582 if (client
->opts
& OPT_INQUIRE
)
2584 unsigned char *result
;
2587 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2589 return send_error (ctx
, rc
);
2591 line
= (char *) result
;
2594 if (!line
|| !*line
)
2595 rc
= GPG_ERR_SYNTAX
;
2598 rc
= do_xpath (ctx
, line
);
2600 if (client
->opts
& OPT_INQUIRE
)
2603 return send_error (ctx
, rc
);
2607 do_xpathattr (assuan_context_t ctx
, char *line
)
2609 struct client_s
*client
= assuan_get_pointer (ctx
);
2613 struct xpath_s _x
= { 0 };
2614 struct xpath_s
*xpath
= &_x
;
2616 if ((req
= str_split (line
, " ", 3)) == NULL
)
2617 return GPG_ERR_ENOMEM
;
2621 rc
= GPG_ERR_SYNTAX
;
2625 if (!strcasecmp (req
[0], "SET"))
2627 else if (!strcasecmp (req
[0], "DELETE"))
2631 rc
= GPG_ERR_SYNTAX
;
2635 if (!req
[1] || !req
[2])
2637 rc
= GPG_ERR_SYNTAX
;
2641 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2643 rc
= GPG_ERR_ENOMEM
;
2647 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2649 rc
= GPG_ERR_SYNTAX
;
2653 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2656 rc
= GPG_ERR_BAD_DATA
;
2661 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2664 rc
= GPG_ERR_BAD_DATA
;
2668 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2670 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2674 rc
= recurse_xpath_nodeset (client
->doc
, xpath
->result
->nodesetval
,
2675 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2676 (xmlChar
*) req
[1]);
2679 xpath_command_cleanup (xpath
);
2684 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2686 xpathattr_command (assuan_context_t ctx
, char *line
)
2688 struct client_s
*client
= assuan_get_pointer (ctx
);
2690 struct argv_s
*args
[] = {
2691 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2695 if (disable_list_and_dump
== 1)
2696 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2698 rc
= parse_options (&line
, args
, client
);
2700 return send_error (ctx
, rc
);
2702 if (client
->opts
& OPT_INQUIRE
)
2704 unsigned char *result
;
2707 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2709 return send_error (ctx
, rc
);
2711 line
= (char *) result
;
2714 if (!line
|| !*line
)
2715 rc
= GPG_ERR_SYNTAX
;
2718 rc
= do_xpathattr (ctx
, line
);
2720 if (client
->opts
& OPT_INQUIRE
)
2723 return send_error (ctx
, rc
);
2727 do_import (struct client_s
*client
, unsigned char *line
)
2729 char **req
, **path
= NULL
, **path_orig
= NULL
, *content
;
2730 xmlDocPtr doc
= NULL
;
2731 xmlNodePtr n
, root
, copy
;
2734 req
= str_split ((char *) line
, "\t", 2);
2737 return GPG_ERR_SYNTAX
;
2740 path
= str_split (req
[1], "\t", 0);
2741 if (!content
|| !*content
)
2743 rc
= GPG_ERR_SYNTAX
;
2747 if (path
&& !valid_element_path (path
, 0))
2749 rc
= GPG_ERR_INV_VALUE
;
2753 doc
= xmlReadDoc ((xmlChar
*) content
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2756 rc
= GPG_ERR_BAD_DATA
;
2760 root
= xmlDocGetRootElement (doc
);
2761 rc
= validate_import (root
);
2767 path_orig
= strv_dup (path
);
2770 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2771 pwmd_strerror (GPG_ERR_ENOMEM
));
2772 rc
= GPG_ERR_ENOMEM
;
2776 xmlChar
*a
= xmlGetProp (root
, (xmlChar
*) "_name");
2779 strv_free (path_orig
);
2780 rc
= GPG_ERR_ENOMEM
;
2784 if (strv_printf (&path
, "%s", (char *) a
) == 0)
2787 strv_free (path_orig
);
2788 rc
= GPG_ERR_ENOMEM
;
2793 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
2795 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2797 strv_free (path_orig
);
2804 find_elements (client
->doc
, n
->children
, path
+ 1, &rc
, NULL
,
2805 NULL
, NULL
, 0, 0, NULL
, 1);
2807 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2809 strv_free (path_orig
);
2814 xmlNodePtr parent
= n
->parent
;
2825 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
2827 n
= create_element_path (client
, &path
, &rc
, NULL
);
2833 copy
= xmlCopyNodeList (root
);
2834 n
= xmlAddChildList (n
, copy
);
2836 rc
= GPG_ERR_BAD_DATA
;
2840 /* Check if the content root element can create a DTD root element. */
2841 if (!xmlStrEqual ((xmlChar
*) "element", root
->name
))
2843 rc
= GPG_ERR_SYNTAX
;
2849 if ((a
= xmlGetProp (root
, (xmlChar
*) "_name")) == NULL
)
2851 rc
= GPG_ERR_SYNTAX
;
2855 char *tmp
= str_dup ((char *) a
);
2857 int literal
= is_literal_element (&tmp
);
2859 if (!valid_xml_element ((xmlChar
*) tmp
) || literal
)
2862 rc
= GPG_ERR_INV_VALUE
;
2866 if (strv_printf (&path
, "%s", tmp
) == 0)
2869 rc
= GPG_ERR_ENOMEM
;
2874 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 1);
2876 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2878 rc
= GPG_ERR_BAD_DATA
;
2882 /* Overwriting the existing tree. */
2886 xmlFreeNodeList (n
);
2890 xmlSetProp (root
, (xmlChar
*) "_name", (xmlChar
*) path
[0]);
2891 n
= xmlCopyNode (root
, 1);
2892 n
= xmlAddChildList (xmlDocGetRootElement (client
->doc
), n
);
2896 rc
= update_element_mtime (n
->parent
);
2910 import_command (assuan_context_t ctx
, char *line
)
2913 struct client_s
*client
= assuan_get_pointer (ctx
);
2914 unsigned char *result
;
2917 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2919 return send_error (ctx
, rc
);
2921 rc
= do_import (client
, result
);
2922 return send_error (ctx
, rc
);
2926 do_lock (struct client_s
*client
, int add
)
2928 gpg_error_t rc
= lock_file_mutex (client
, add
);
2931 client
->flags
|= FLAG_LOCK_CMD
;
2937 lock_command (assuan_context_t ctx
, char *line
)
2939 struct client_s
*client
= assuan_get_pointer (ctx
);
2940 gpg_error_t rc
= do_lock (client
, 0);
2942 return send_error (ctx
, rc
);
2946 unlock_command (assuan_context_t ctx
, char *line
)
2948 struct client_s
*client
= assuan_get_pointer (ctx
);
2951 rc
= unlock_file_mutex (client
, 0);
2952 return send_error (ctx
, rc
);
2956 option_command (assuan_context_t ctx
, char *line
)
2958 struct client_s
*client
= assuan_get_pointer (ctx
);
2960 struct pinentry_option_s
*pin_opts
= &client
->pinentry_opts
;
2962 struct agent_s
*agent
= client
->crypto
->agent
;
2964 char namebuf
[255] = { 0 };
2965 char *name
= namebuf
;
2966 char *value
= NULL
, *p
;
2968 p
= strchr (line
, '=');
2970 strncpy (namebuf
, line
, sizeof(namebuf
));
2973 strncpy (namebuf
, line
, strlen (line
)-strlen (p
));
2977 log_write1 ("OPTION name='%s' value='%s'", name
, value
);
2979 if (strcasecmp (name
, (char *) "log_level") == 0)
2985 l
= strtol (value
, NULL
, 10);
2988 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2991 MUTEX_LOCK (&rcfile_mutex
);
2992 config_set_int_param (&global_config
, "global", "log_level", value
);
2993 MUTEX_UNLOCK (&rcfile_mutex
);
2996 else if (strcasecmp (name
, (char *) "lock-timeout") == 0)
3003 n
= strtol (value
, &p
, 10);
3005 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3008 client
->lock_timeout
= n
;
3011 else if (strcasecmp (name
, (char *) "NAME") == 0)
3013 char *tmp
= pthread_getspecific (thread_name_key
);
3018 if (!value
|| !*value
)
3020 pthread_setspecific (thread_name_key
, str_dup (""));
3024 pthread_setspecific (thread_name_key
, str_dup (value
));
3027 else if (strcasecmp (name
, (char *) "lc-messages") == 0)
3029 xfree (pin_opts
->lc_messages
);
3030 pin_opts
->lc_messages
= NULL
;
3031 if (value
&& *value
)
3032 pin_opts
->lc_messages
= str_dup (value
);
3035 rc
= set_agent_option (client
->crypto
->agent
, "lc-messages", value
);
3038 else if (strcasecmp (name
, (char *) "lc-ctype") == 0)
3040 xfree (pin_opts
->lc_ctype
);
3041 pin_opts
->lc_ctype
= NULL
;
3042 if (value
&& *value
)
3043 pin_opts
->lc_ctype
= str_dup (value
);
3046 rc
= set_agent_option (client
->crypto
->agent
, "lc-ctype", value
);
3049 else if (strcasecmp (name
, (char *) "ttyname") == 0)
3051 xfree (pin_opts
->ttyname
);
3052 pin_opts
->ttyname
= NULL
;
3053 if (value
&& *value
)
3054 pin_opts
->ttyname
= str_dup (value
);
3057 rc
= set_agent_option (client
->crypto
->agent
, "ttyname", value
);
3060 else if (strcasecmp (name
, (char *) "ttytype") == 0)
3062 xfree (pin_opts
->ttytype
);
3063 pin_opts
->ttytype
= NULL
;
3064 if (value
&& *value
)
3065 pin_opts
->ttytype
= str_dup (value
);
3068 rc
= set_agent_option (client
->crypto
->agent
, "ttytype", value
);
3071 else if (strcasecmp (name
, (char *) "display") == 0)
3073 xfree (pin_opts
->display
);
3074 pin_opts
->display
= NULL
;
3075 if (value
&& *value
)
3076 pin_opts
->display
= str_dup (value
);
3079 rc
= set_agent_option (client
->crypto
->agent
, "display", value
);
3082 else if (strcasecmp (name
, (char *) "pinentry-desc") == 0)
3084 xfree (pin_opts
->desc
);
3085 pin_opts
->desc
= NULL
;
3086 if (value
&& *value
)
3087 pin_opts
->desc
= str_dup (value
);
3089 else if (strcasecmp (name
, (char *) "pinentry-title") == 0)
3091 xfree (pin_opts
->title
);
3092 pin_opts
->title
= NULL
;
3093 if (value
&& *value
)
3094 pin_opts
->title
= str_dup (value
);
3096 else if (strcasecmp (name
, (char *) "pinentry-prompt") == 0)
3098 xfree (pin_opts
->prompt
);
3099 pin_opts
->prompt
= NULL
;
3100 if (value
&& *value
)
3101 pin_opts
->prompt
= str_dup (value
);
3104 else if (strcasecmp (name
, "pinentry-timeout") == 0)
3112 n
= (int) strtol (value
, &p
, 10);
3115 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3117 pin_opts
->timeout
= n
;
3118 MUTEX_LOCK (&rcfile_mutex
);
3119 config_set_int_param (&global_config
,
3120 client
->filename
? client
->filename
: "global",
3121 "pinentry_timeout", value
);
3122 MUTEX_UNLOCK (&rcfile_mutex
);
3125 else if (strcasecmp (name
, "disable-pinentry") == 0)
3130 if (value
&& *value
)
3132 n
= (int) strtol (value
, &p
, 10);
3133 if (*p
|| n
< 0 || n
> 1)
3134 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3138 client
->flags
|= FLAG_NO_PINENTRY
;
3140 client
->flags
&= ~FLAG_NO_PINENTRY
;
3143 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
3147 if (!rc
&& use_agent
&& agent
)
3149 rc
= pinentry_merge_options (&client
->crypto
->agent
->pinentry_opts
,
3154 return send_error (ctx
, rc
);
3158 do_rename (assuan_context_t ctx
, char *line
)
3160 struct client_s
*client
= assuan_get_pointer (ctx
);
3162 char **req
, **src
, *dst
;
3165 req
= str_split (line
, " ", 0);
3167 if (!req
|| !req
[0] || !req
[1])
3170 return GPG_ERR_SYNTAX
;
3174 is_literal_element (&dst
);
3176 if (!valid_xml_element ((xmlChar
*) dst
))
3179 return GPG_ERR_INV_VALUE
;
3182 if (strchr (req
[0], '\t'))
3183 src
= str_split (req
[0], "\t", 0);
3185 src
= str_split (req
[0], " ", 0);
3189 rc
= GPG_ERR_SYNTAX
;
3193 n
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3195 n
= find_elements (client
->doc
, n
->children
, src
+ 1, &rc
, NULL
, NULL
,
3196 NULL
, 0, 0, NULL
, 0);
3201 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
3204 rc
= GPG_ERR_ENOMEM
;
3208 /* To prevent unwanted effects:
3210 * <root name="a"><b/></root>
3214 if (xmlStrEqual (a
, (xmlChar
*) dst
))
3217 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3227 for (p
= src
; *p
; p
++)
3231 strv_printf (&tmp
, "%s", *p
);
3235 strv_printf (&tmp
, "!%s", dst
);
3236 ndst
= find_root_element (client
->doc
, &tmp
, &rc
, NULL
, 0, 0);
3237 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3244 ndst
= find_elements (client
->doc
, ndst
->children
, tmp
+ 1, &rc
, NULL
,
3245 NULL
, NULL
, 0, 0, NULL
, 0);
3248 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3253 /* Target may exist:
3256 * <root name="b" target="a"/>
3265 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3272 xmlFreeNodeList (ndst
);
3275 rc
= add_attribute (n
, "_name", dst
);
3284 rename_command (assuan_context_t ctx
, char *line
)
3286 struct client_s
*client
= assuan_get_pointer (ctx
);
3288 struct argv_s
*args
[] = {
3289 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3293 rc
= parse_options (&line
, args
, client
);
3295 return send_error (ctx
, rc
);
3297 if (client
->opts
& OPT_INQUIRE
)
3299 unsigned char *result
;
3302 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3304 return send_error (ctx
, rc
);
3306 line
= (char *) result
;
3309 rc
= do_rename (ctx
, line
);
3311 if (client
->opts
& OPT_INQUIRE
)
3314 return send_error (ctx
, rc
);
3318 do_copy (assuan_context_t ctx
, char *line
)
3320 struct client_s
*client
= assuan_get_pointer (ctx
);
3322 char **req
, **src
= NULL
, **dst
= NULL
;
3323 xmlNodePtr nsrc
, ndst
, new = NULL
;
3325 req
= str_split (line
, " ", 0);
3326 if (!req
|| !req
[0] || !req
[1])
3329 return GPG_ERR_SYNTAX
;
3332 if (strchr (req
[0], '\t'))
3333 src
= str_split (req
[0], "\t", 0);
3335 src
= str_split (req
[0], " ", 0);
3339 rc
= GPG_ERR_SYNTAX
;
3343 if (strchr (req
[1], '\t'))
3344 dst
= str_split (req
[1], "\t", 0);
3346 dst
= str_split (req
[1], " ", 0);
3350 rc
= GPG_ERR_SYNTAX
;
3354 if (!valid_element_path (dst
, 0))
3356 rc
= GPG_ERR_INV_VALUE
;
3360 nsrc
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3362 nsrc
= find_elements (client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3363 NULL
, NULL
, 0, 0, NULL
, 0);
3368 new = xmlCopyNodeList (nsrc
);
3371 rc
= GPG_ERR_ENOMEM
;
3376 ndst
= find_root_element (client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3380 ndst
= find_elements (client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3381 NULL
, create_target_elements_cb
, 0, 0, NULL
, 0);
3388 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3392 ndst
= create_element_path (client
, &dst
, &rc
, NULL
);
3397 /* Merge any attributes from the src node to the initial dst node. */
3398 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
)
3400 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3403 xmlAttrPtr a
= xmlHasProp (ndst
, attr
->name
);
3407 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3408 xmlNewProp (ndst
, attr
->name
, tmp
);
3410 rc
= add_attribute (ndst
, NULL
, NULL
);
3413 xmlNodePtr n
= ndst
->children
;
3415 xmlFreeNodeList (n
);
3416 ndst
->children
= NULL
;
3420 n
= xmlCopyNodeList (new->children
);
3423 rc
= GPG_ERR_ENOMEM
;
3427 n
= xmlAddChildList (ndst
, n
);
3430 rc
= GPG_ERR_ENOMEM
;
3434 rc
= update_element_mtime (xmlDocGetRootElement (client
->doc
) ==
3435 ndst
->parent
? ndst
: ndst
->parent
);
3441 xmlUnlinkNode (new);
3442 xmlFreeNodeList (new);
3458 copy_command (assuan_context_t ctx
, char *line
)
3460 struct client_s
*client
= assuan_get_pointer (ctx
);
3462 struct argv_s
*args
[] = {
3463 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3467 rc
= parse_options (&line
, args
, client
);
3469 return send_error (ctx
, rc
);
3471 if (client
->opts
& OPT_INQUIRE
)
3473 unsigned char *result
;
3476 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3478 return send_error (ctx
, rc
);
3480 line
= (char *) result
;
3483 rc
= do_copy (ctx
, line
);
3485 if (client
->opts
& OPT_INQUIRE
)
3488 return send_error (ctx
, rc
);
3492 do_move (assuan_context_t ctx
, char *line
)
3494 struct client_s
*client
= assuan_get_pointer (ctx
);
3496 char **req
, **src
= NULL
, **dst
= NULL
;
3497 xmlNodePtr nsrc
, ndst
= NULL
;
3499 req
= str_split (line
, " ", 0);
3501 if (!req
|| !req
[0] || !req
[1])
3504 return GPG_ERR_SYNTAX
;
3507 if (strchr (req
[0], '\t'))
3508 src
= str_split (req
[0], "\t", 0);
3510 src
= str_split (req
[0], " ", 0);
3514 rc
= GPG_ERR_SYNTAX
;
3518 if (strchr (req
[1], '\t'))
3519 dst
= str_split (req
[1], "\t", 0);
3521 dst
= str_split (req
[1], " ", 0);
3523 nsrc
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3525 nsrc
= find_elements (client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3526 NULL
, NULL
, 0, 0, NULL
, 0);
3533 if (!valid_element_path (dst
, 0))
3535 rc
= GPG_ERR_INV_VALUE
;
3539 ndst
= find_root_element (client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3541 ndst
= find_elements (client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3542 NULL
, NULL
, 0, 0, NULL
, 0);
3545 ndst
= xmlDocGetRootElement (client
->doc
);
3547 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
)
3551 rc
= GPG_ERR_CONFLICT
;
3556 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3563 xmlChar
*a
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3564 xmlNodePtr dup
= find_element (ndst
->children
, (char *) a
, NULL
);
3572 if (ndst
== xmlDocGetRootElement (client
->doc
))
3574 xmlNodePtr n
= nsrc
;
3577 while (n
->parent
&& n
->parent
!= ndst
)
3580 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
3581 xmlChar
*b
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3583 if (xmlStrEqual (a
, b
))
3586 xmlUnlinkNode (nsrc
);
3588 xmlFreeNodeList (n
);
3596 xmlUnlinkNode (dup
);
3597 xmlFreeNodeList (dup
);
3601 xmlUnlinkNode (dup
);
3607 xmlChar
*name
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3609 if (nsrc
->parent
== xmlDocGetRootElement (client
->doc
)
3610 && !strcmp ((char *) name
, *dst
))
3613 rc
= GPG_ERR_CONFLICT
;
3618 ndst
= create_element_path (client
, &dst
, &rc
, nsrc
);
3624 update_element_mtime (nsrc
->parent
);
3625 xmlUnlinkNode (nsrc
);
3626 ndst
= xmlAddChildList (ndst
, nsrc
);
3629 rc
= GPG_ERR_ENOMEM
;
3631 update_element_mtime (ndst
->parent
);
3647 move_command (assuan_context_t ctx
, char *line
)
3649 struct client_s
*client
= assuan_get_pointer (ctx
);
3651 struct argv_s
*args
[] = {
3652 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3656 rc
= parse_options (&line
, args
, client
);
3658 return send_error (ctx
, rc
);
3660 if (client
->opts
& OPT_INQUIRE
)
3662 unsigned char *result
;
3665 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3667 return send_error (ctx
, rc
);
3669 line
= (char *) result
;
3672 rc
= do_move (ctx
, line
);
3674 if (client
->opts
& OPT_INQUIRE
)
3677 return send_error (ctx
, rc
);
3681 ls_command (assuan_context_t ctx
, char *line
)
3684 char *tmp
= str_asprintf ("%s/data", homedir
);
3685 char *dir
= expand_homedir (tmp
);
3686 DIR *d
= opendir (dir
);
3688 rc
= gpg_error_from_syserror ();
3694 return send_error (ctx
, rc
);
3698 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
3699 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
3705 while (!readdir_r (d
, p
, &cur
) && cur
)
3707 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
3709 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.'
3710 && cur
->d_name
[2] == '\0')
3713 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
3720 rc
= GPG_ERR_ENOMEM
;
3732 return send_error (ctx
, rc
);
3735 return send_error (ctx
, GPG_ERR_NO_DATA
);
3737 list
[strlen (list
) - 1] = 0;
3738 rc
= xfer_data (ctx
, list
, strlen (list
));
3740 return send_error (ctx
, rc
);
3744 bye_notify (assuan_context_t ctx
, char *line
)
3746 struct client_s
*cl
= assuan_get_pointer (ctx
);
3749 if (cl
->thd
->remote
)
3755 struct timeval tv
= { 0, 50000 };
3757 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
3758 if (rc
== GNUTLS_E_AGAIN
)
3759 select (0, NULL
, NULL
, NULL
, &tv
);
3761 while (rc
== GNUTLS_E_AGAIN
);
3765 /* This will let assuan_process_next() return. */
3766 fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
);
3767 cl
->last_rc
= 0; // BYE command result
3772 reset_notify (assuan_context_t ctx
, char *line
)
3774 struct client_s
*client
= assuan_get_pointer (ctx
);
3777 cleanup_client (client
);
3783 * This is called before every Assuan command.
3786 command_startup (assuan_context_t ctx
, const char *name
)
3788 struct client_s
*client
= assuan_get_pointer (ctx
);
3790 struct command_table_s
*cmd
= NULL
;
3792 log_write1 ("command='%s'", name
);
3793 client
->last_rc
= client
->opts
= 0;
3795 for (int i
= 0; command_table
[i
]; i
++)
3797 if (!strcasecmp (name
, command_table
[i
]->name
))
3799 if (command_table
[i
]->ignore_startup
)
3801 cmd
= command_table
[i
];
3806 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
3811 * This is called after every Assuan command.
3814 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
3816 struct client_s
*client
= assuan_get_pointer (ctx
);
3818 if (!(client
->flags
& FLAG_LOCK_CMD
))
3819 unlock_file_mutex (client
, 0);
3821 log_write1 (_("command completed: rc=%u"), client
->last_rc
);
3822 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
3826 help_command (assuan_context_t ctx
, char *line
)
3831 if (!line
|| !*line
)
3834 char *help
= str_dup (_("Usage: HELP [<COMMAND>]\n"
3835 "For commands that take an element path as an argument, each element is "
3836 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3839 for (i
= 0; command_table
[i
]; i
++)
3841 if (!command_table
[i
]->help
)
3844 tmp
= str_asprintf ("%s %s", help
, command_table
[i
]->name
);
3849 tmp
= strip_texi_and_wrap (help
);
3851 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3853 return send_error (ctx
, rc
);
3856 for (i
= 0; command_table
[i
]; i
++)
3858 if (!strcasecmp (line
, command_table
[i
]->name
))
3862 if (!command_table
[i
]->help
)
3865 help
= strip_texi_and_wrap (command_table
[i
]->help
);
3866 tmp
= str_asprintf (_("Usage: %s"), help
);
3868 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3870 return send_error (ctx
, rc
);
3874 return send_error (ctx
, GPG_ERR_INV_NAME
);
3878 new_command (const char *name
, int ignore
, int unlock
,
3879 gpg_error_t (*handler
) (assuan_context_t
, char *),
3885 for (i
= 0; command_table
[i
]; i
++);
3888 xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
3889 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
3890 command_table
[i
]->name
= name
;
3891 command_table
[i
]->handler
= handler
;
3892 command_table
[i
]->ignore_startup
= ignore
;
3893 command_table
[i
]->unlock
= unlock
;
3894 command_table
[i
++]->help
= help
;
3895 command_table
[i
] = NULL
;
3903 for (i
= 0; command_table
[i
]; i
++)
3904 xfree (command_table
[i
]);
3906 xfree (command_table
);
3910 sort_commands (const void *arg1
, const void *arg2
)
3912 struct command_table_s
*const *a
= arg1
;
3913 struct command_table_s
*const *b
= arg2
;
3922 return strcmp ((*a
)->name
, (*b
)->name
);
3926 passwd_command (assuan_context_t ctx
, char *line
)
3928 struct client_s
*client
= assuan_get_pointer (ctx
);
3930 struct argv_s
*args
[] = {
3931 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
3932 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
3936 if (client
->flags
& FLAG_NEW
)
3937 return send_error (ctx
, GPG_ERR_INV_STATE
);
3939 client
->crypto
->save
.s2k_count
=
3940 config_get_ulong (client
->filename
, "s2k_count");
3941 rc
= parse_options (&line
, args
, client
);
3943 return send_error (ctx
, rc
);
3945 if (!rc
&& client
->opts
& OPT_RESET
)
3947 rc
= cache_clear (client
->md5file
);
3949 send_status_all (STATUS_CACHE
, NULL
);
3954 if (!IS_PKCS (client
->crypto
))
3956 struct crypto_s
*crypto
;
3958 xfree (client
->crypto
->filename
);
3959 client
->crypto
->filename
= str_dup (client
->filename
);
3960 rc
= change_passwd (ctx
, client
->filename
,
3961 client
->flags
& FLAG_NO_PINENTRY
, &crypto
);
3964 cleanup_crypto (&client
->crypto
);
3965 client
->crypto
= crypto
;
3966 update_checksum (client
);
3967 cleanup_crypto_stage1 (client
->crypto
);
3973 if (client
->crypto
->save
.s2k_count
)
3974 rc
= send_to_agent (client
->crypto
->agent
, NULL
, NULL
,
3975 "OPTION s2k-count=%lu",
3976 client
->crypto
->save
.s2k_count
);
3979 rc
= agent_passwd (client
->crypto
);
3984 return send_error (ctx
, rc
);
3988 parse_keygrip_opt_sign (void *data
, void *value
)
3990 struct client_s
*client
= data
;
3993 client
->opts
|= OPT_SIGN
;
3998 keygrip_command (assuan_context_t ctx
, char *line
)
4000 struct client_s
*client
= assuan_get_pointer (ctx
);
4002 struct crypto_s
*crypto
= NULL
;
4003 struct argv_s
*args
[] = {
4004 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_keygrip_opt_sign
},
4008 if (!line
|| !*line
)
4009 return send_error (ctx
, GPG_ERR_SYNTAX
);
4011 rc
= parse_options (&line
, args
, client
);
4013 return send_error (ctx
, rc
);
4015 if (!valid_filename (line
))
4016 return send_error (ctx
, GPG_ERR_INV_VALUE
);
4018 rc
= init_client_crypto (&crypto
);
4020 return send_error (ctx
, rc
);
4022 rc
= read_data_file (line
, crypto
);
4025 char *hexgrip
= NULL
;
4027 if (!IS_PKCS (crypto
))
4029 cleanup_crypto (&crypto
);
4030 return send_error (ctx
, GPG_ERR_NOT_SUPPORTED
);
4033 if (client
->opts
& OPT_SIGN
)
4035 if (valid_keygrip (crypto
->sign_grip
, sizeof (crypto
->sign_grip
)))
4036 hexgrip
= bin2hex (crypto
->sign_grip
, sizeof (crypto
->sign_grip
));
4040 hexgrip
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
4043 rc
= GPG_ERR_ENOMEM
;
4045 rc
= xfer_data (ctx
, hexgrip
, strlen (hexgrip
));
4050 UPDATE_AGENT_CTX (client
, crypto
);
4051 cleanup_crypto (&crypto
);
4052 return send_error (ctx
, rc
);
4056 parse_opt_data (void *data
, void *value
)
4058 struct client_s
*client
= data
;
4061 client
->opts
|= OPT_DATA
;
4066 getinfo_command (assuan_context_t ctx
, char *line
)
4068 struct client_s
*client
= assuan_get_pointer (ctx
);
4070 char buf
[ASSUAN_LINELENGTH
];
4071 struct argv_s
*args
[] = {
4072 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4076 rc
= parse_options (&line
, args
, client
);
4078 return send_error (ctx
, rc
);
4080 if (!strcasecmp (line
, "clients"))
4082 if (client
->opts
& OPT_DATA
)
4084 MUTEX_LOCK (&cn_mutex
);
4085 snprintf (buf
, sizeof (buf
), "%i", slist_length (cn_thread_list
));
4086 MUTEX_UNLOCK (&cn_mutex
);
4087 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4090 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4092 else if (!strcasecmp (line
, "cache"))
4094 if (client
->opts
& OPT_DATA
)
4096 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4097 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4100 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4102 else if (!strcasecmp (line
, "pid"))
4105 pid_t pid
= getpid ();
4107 snprintf (buf
, sizeof (buf
), "%i", pid
);
4108 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4110 else if (!strcasecmp (line
, "version"))
4112 char *buf
= str_asprintf ("0x%06x %s", VERSION_HEX
,
4126 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4129 else if (!strcasecmp (line
, "last_error"))
4131 if (client
->last_error
)
4132 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4134 rc
= GPG_ERR_NO_DATA
;
4137 rc
= gpg_error (GPG_ERR_SYNTAX
);
4139 return send_error (ctx
, rc
);
4144 send_data_cb (void *user
, const void *buf
, size_t len
)
4146 assuan_context_t ctx
= user
;
4148 return assuan_send_data (ctx
, buf
, len
);
4152 send_status_cb (void *user
, const char *line
)
4154 assuan_context_t ctx
= user
;
4155 char keyword
[200], *k
;
4158 for (p
= line
, k
= keyword
; *p
; p
++)
4170 while (isspace (*p
))
4173 return assuan_write_status (ctx
, keyword
, *p
? p
: NULL
);
4178 agent_command (assuan_context_t ctx
, char *line
)
4183 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
4186 struct client_s
*client
= assuan_get_pointer (ctx
);
4188 if (!line
|| !*line
)
4189 return send_error (ctx
, GPG_ERR_SYNTAX
);
4191 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 1);
4192 rc
= assuan_transact (client
->crypto
->agent
->ctx
, line
, send_data_cb
,
4193 client
->ctx
, agent_loopback_cb
, client
->crypto
,
4194 send_status_cb
, client
->ctx
);
4195 if (gpg_err_code (rc
) == GPG_ERR_ASS_CANCELED
)
4200 rc
= assuan_write_line (client
->crypto
->agent
->ctx
, "CAN");
4203 rc
= assuan_read_line (client
->crypto
->agent
->ctx
, &line
, &len
);
4205 rc
= gpg_error (GPG_ERR_ASS_CANCELED
);
4209 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 0);
4211 return send_error (ctx
, rc
);
4217 /* !BEGIN-HELP-TEXT!
4219 * This comment is used as a marker to generate the offline documentation
4220 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4221 * script to determine where commands begin and end.
4223 new_command("HELP", 1, 1, help_command
, _(
4224 "HELP [<COMMAND>]\n"
4225 "Show available commands or command specific help text."
4228 new_command("AGENT", 1, 1, agent_command
, _(
4230 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4231 "@command{gpg-agent}."
4234 new_command("GETINFO", 1, 1, getinfo_command
, _(
4235 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4236 "Get server and other information: @var{cache} returns the number of cached "
4237 "documents via a status message. @var{clients} returns the number of "
4238 "connected clients via a status message. @var{pid} returns the process ID "
4239 "number of the server via a data response. @var{VERSION} returns the server "
4240 "version number and compile-time features with a data response with each "
4241 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4242 "the last failed command when available. @xref{Status Messages}. "
4244 "When the @option{--data} option is specified then the result will be send "
4245 "via a data response rather than a status message."
4248 new_command("PASSWD", 0, 0, passwd_command
, _(
4249 "PASSWD [--reset] [--s2k-count=N]\n"
4250 "Changes the passphrase of the secret key required to open the current "
4251 "file. When the @option{--reset} option is passed then the cache entry for "
4252 "the current file will be reset and the passphrase, if any, will be required "
4253 "during the next @code{OPEN}. @xref{OPEN}."
4255 "The @option{--s2k-count} option sets number of hash iterations for a "
4256 "passphrase and must be either @code{0} to use the calibrated count of the "
4257 "machine (the default), or a value greater than or equal to @code{65536}. "
4261 new_command("KEYGRIP", 1, 1, keygrip_command
, _(
4262 "KEYGRIP [--sign] <filename>\n"
4263 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4266 "When the @option{--sign} option is specified then the key used for signing "
4267 "of the specified @var{filename} will be returned."
4270 new_command("OPEN", 1, 1, open_command
, _(
4271 "OPEN [--lock] <filename> [<passphrase>]\n"
4272 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4273 "found on the file-system then a new document will be created. If the file "
4274 "is found, it is looked for in the file cache. If cached and no "
4275 "@var{passphrase} was specified then the cached document is opened. When not "
4276 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4277 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4280 "When the @option{--lock} option is passed then the file mutex will be "
4281 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4282 "file has been opened."
4285 new_command("SAVE", 0, 0, save_command
, _(
4286 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring [--sign-keygrip=hexstring]]\n"
4287 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4288 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4289 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4290 "keypair will be generated and a pinentry will be used to prompt for the "
4291 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4292 "passed, in which case the data file will not be passphrase protected."
4294 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4295 "passphrase retrieval and caching for new files and behaves as if the "
4296 "@option{--no-agent} commmand line option to @command{pwmd} was specified."
4298 "The @option{--reset} option will clear the cache entry for the current file "
4301 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4302 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4303 "(@pxref{Configuration}) for available ciphers."
4305 "The @option{--cipher-iterations} option specifies the number of times to "
4306 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4308 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4309 "the client to obtain the key paramaters to use when generating a new "
4310 "keypair. The inquired data is expected to be an S-expression. If not "
4311 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4312 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4313 "that when this option is specified a new keypair will be generated "
4314 "reguardless if the file is a new one or not."
4316 "You can encrypt the data file to a public key other than the one that it "
4317 "was originally encrypted with by passing the @option{--keygrip} option with "
4318 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4319 "be of any key that @command{gpg-agent} knows about. The "
4320 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4321 "secret key. This option may be needed when using a smartcard."
4323 "The @option{--s2k-count} option sets number of hash iterations for a "
4324 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4325 "value which is the default. This setting only affects new files. To change "
4326 "the setting, use the @code{PASSWD} command (@pxref{PASSWD})."
4329 new_command("ISCACHED", 1, 0, iscached_command
, _(
4330 "ISCACHED [--lock] <filename>\n"
4331 "An @emph{OK} response is returned if the specified @var{filename} is found "
4332 "in the file cache. If not found in the cache but exists on the filesystem "
4333 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4336 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4337 "file exists; it does not need to be opened nor cached."
4340 new_command("CLEARCACHE", 1, 1, clearcache_command
, _(
4341 "CLEARCACHE [<filename>]\n"
4342 "Clears a file cache entry for all or the specified @var{filename}."
4345 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command
, _(
4346 "CACHETIMEOUT <filename> <seconds>\n"
4347 "The time in @var{seconds} until @var{filename} will be removed from the "
4348 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4349 "the passphrase for each @code{OPEN} command (@pxref{OPEN}). "
4350 "@xref{Configuration}, and the @code{cache_timeout} parameter."
4353 new_command("LIST", 0, 1, list_command
, _(
4354 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4355 "If no element path is given then a newline separated list of root elements "
4356 "is returned with a data response. If given, then all reachable elements "
4357 "of the specified element path are returned unless the @option{--no-recurse} "
4358 "option is specified. If specified, only the child elements of the element "
4359 "path are returned without recursing into grandchildren. Each resulting "
4360 "element is prefixed with the literal @code{!} character when the element "
4361 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4363 "When the @option{--verbose} option is passed then each element path "
4364 "returned will have zero or more flags appened to it. These flags are "
4365 "delimited from the element path by a single space character. A flag itself "
4366 "is a single character. Flag @code{+} indicates that there are child nodes of "
4367 "the current element path. Flag @code{E} indicates that an element of an "
4368 "element path contained in a @var{target} attribute could not be found. Flag "
4369 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4370 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4371 "of a @var{target} attribute (see below)."
4373 "The @option{--with-target} option implies @option{--verbose} and will append "
4374 "an additional flag @code{T} followed by a single space then an element path. "
4375 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4376 "current element when it contains a @var{target} attribute. When no "
4377 "@var{target} attribute is found then no flag will be appended."
4379 "The @option{--no-recurse} option limits the amount of data returned to only "
4380 "the listing of children of the specified element path and not any "
4383 "The @option{--all} option lists the entire element tree for each root "
4384 "element. This option also implies option @option{--verbose}."
4386 "When the @option{--inquire} option is passed then all remaining non-option "
4387 "arguments are retrieved via a server @emph{INQUIRE}."
4390 new_command("REALPATH", 0, 1, realpath_command
, _(
4391 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4392 "Resolves all @code{target} attributes of the specified element path and "
4393 "returns the result with a data response. @xref{Target Attribute}, for details."
4395 "When the @option{--inquire} option is passed then all remaining non-option "
4396 "arguments are retrieved via a server @emph{INQUIRE}."
4399 new_command("STORE", 0, 1, store_command
, _(
4400 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4401 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4403 "Creates a new element path or modifies the @var{content} of an existing "
4404 "element. If only a single element is specified then a new root element is "
4405 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4406 "set to the final @key{TAB} delimited element. If no @var{content} is "
4407 "specified after the final @key{TAB}, then the content of the element will "
4408 "be removed, or empty when creating a new element."
4410 "The only restriction of an element name is that it not contain whitespace "
4411 "or begin with the literal element character @code{!} unless specifying a "
4412 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4413 "the @key{TAB} delimited elements. It is recommended that the content of an "
4414 "element be base64 encoded when it contains control or @key{TAB} characters "
4415 "to prevent @abbr{XML} and @command{pwmd} parsing errors."
4418 new_command("RENAME", 0, 1, rename_command
, _(
4419 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4420 "Renames the specified @var{element} to the new @var{value}. If an element of "
4421 "the same name as the @var{value} already exists it will be overwritten."
4423 "When the @option{--inquire} option is passed then all remaining non-option "
4424 "arguments are retrieved via a server @emph{INQUIRE}."
4427 new_command("COPY", 0, 1, copy_command
, _(
4428 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4429 "Copies the entire element tree starting from the child node of the source "
4430 "element, to the destination element path. If the destination element path "
4431 "does not exist then it will be created; otherwise it is overwritten."
4433 "Note that attributes from the source element are merged into the "
4434 "destination element when the destination element path exists. When an "
4435 "attribute of the same name exists in both the source and destination "
4436 "elements then the destination attribute will be updated to the source "
4439 "When the @option{--inquire} option is passed then all remaining non-option "
4440 "arguments are retrieved via a server @emph{INQUIRE}."
4443 new_command("MOVE", 0, 1, move_command
, _(
4444 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4445 "Moves the source element path to the destination element path. If the "
4446 "destination is not specified then it will be moved to the root node of the "
4447 "document. If the destination is specified and exists then it will be "
4448 "overwritten; otherwise it will be created."
4450 "When the @option{--inquire} option is passed then all remaining non-option "
4451 "arguments are retrieved via a server @emph{INQUIRE}."
4454 new_command("DELETE", 0, 1, delete_command
, _(
4455 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4456 "Removes the specified element path and all of its children. This may break "
4457 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4458 "refers to this element or any of its children."
4460 "When the @option{--inquire} option is passed then all remaining non-option "
4461 "arguments are retrieved via a server @emph{INQUIRE}."
4464 new_command("GET", 0, 1, get_command
, _(
4465 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4466 "Retrieves the content of the specified element. The content is returned "
4467 "with a data response."
4469 "When the @option{--inquire} option is passed then all remaining non-option "
4470 "arguments are retrieved via a server @emph{INQUIRE}."
4473 new_command("ATTR", 0, 1, attr_command
, _(
4474 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4476 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4478 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4479 "element. When no @var{value} is specified any existing value will be removed."
4481 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4483 " Removes an @var{attribute} from an element."
4485 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4487 " Retrieves a newline separated list of attributes names and values "
4488 "from the specified element. Each attribute name and value is space delimited."
4490 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4492 " Retrieves the value of an @var{attribute} from an element."
4495 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4496 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4499 "The @code{_mtime} attribute is updated each time an element is modified by "
4500 "either storing content, editing attributes or by deleting a child element. "
4501 "The @code{_ctime} attribute is created for each new element in an element "
4504 "When the @option{--inquire} option is passed then all remaining non-option "
4505 "arguments are retrieved via a server @emph{INQUIRE}."
4507 "@xref{Target Attribute}, for details about this special attribute."
4510 new_command("XPATH", 0, 1, xpath_command
, _(
4511 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4512 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4513 "specified, it is assumed the expression is a request to return a result. "
4514 "Otherwise, the result is set to the @var{value} argument and the document is "
4515 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4516 "is assumed to be empty and the document is updated. For example:"
4519 "XPATH //element[@@_name='password']@key{TAB}\n"
4522 "would clear the content of all @code{password} elements in the data file "
4523 "while leaving off the trailing @key{TAB} would return all @code{password} "
4524 "elements in @abbr{XML} format."
4526 "When the @option{--inquire} option is passed then all remaining non-option "
4527 "arguments are retrieved via a server @emph{INQUIRE}."
4529 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4530 "expression syntax."
4533 new_command("XPATHATTR", 0, 1, xpathattr_command
, _(
4534 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4535 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4536 "attributes and does not return a result. For the @var{SET} operation the "
4537 "@var{value} is optional but the field is required. If not specified then "
4538 "the attribute value will be empty. For example:"
4541 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4544 "would create an @code{password} attribute for each @code{password} element "
4545 "found in the document. The attribute value will be empty but still exist."
4547 "When the @option{--inquire} option is passed then all remaining non-option "
4548 "arguments are retrieved via a server @emph{INQUIRE}."
4550 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4551 "expression syntax."
4554 new_command("IMPORT", 0, 1, import_command
, _(
4555 "IMPORT <content>[<TAB>[!]element[<TAB>[!]child[..]]]\n"
4556 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4558 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4559 "argument is raw @abbr{XML} data. The content is created as a child of the "
4560 "specified element path and will overwrite an existing element of the same "
4561 "name. If an element of the element path does not exist then it will be "
4564 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4568 new_command("DUMP", 0, 1, dump_command
, _(
4570 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4571 "dumping a specific node."
4574 new_command("LOCK", 0, 0, lock_command
, _(
4576 "Locks the mutex associated with the opened file. This prevents other clients "
4577 "from sending commands to the same opened file until the client "
4578 "that sent this command either disconnects or sends the @code{UNLOCK} "
4579 "command. @xref{UNLOCK}."
4582 new_command("UNLOCK", 1, 0, unlock_command
, _(
4584 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4585 "a commands' @option{lock} option. @xref{LOCK}."
4588 new_command("GETCONFIG", 1, 1, getconfig_command
, _(
4589 "GETCONFIG [filename] <parameter>\n"
4590 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4591 "data response. If no file has been opened then the value for @var{filename} "
4592 "or the default from the @samp{global} section will be returned. If a file "
4593 "has been opened and no @var{filename} is specified, a value previously "
4594 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4597 new_command("OPTION", 1, 1, option_command
, _(
4598 "OPTION <NAME>=<VALUE>\n"
4599 "Sets a client option @var{name} to @var{value}. The value for an option is "
4600 "kept for the duration of the connection."
4603 "@item DISABLE-PINENTRY\n"
4604 " Disable use of @command{pinentry} for passphrase retrieval. When set, a "
4605 " server inquire is sent to the client to obtain the passphrase. This option "
4606 " may be set as needed before the @pxref{OPEN}, @pxref{PASSWD}, and "
4607 " @pxref{SAVE} commands."
4610 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4613 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4616 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4618 "@item PINENTRY-DESC\n"
4619 " Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4621 "@item PINENTRY-TITLE\n"
4622 " Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
4624 "@item PINENTRY-PROMPT\n"
4625 " Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
4628 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4630 "@item LC-MESSAGES\n"
4631 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4634 " Associates the thread ID of the connection with the specified textual "
4635 "representation. Useful for debugging log messages."
4637 "@item LOCK-TIMEOUT\n"
4638 " When not @code{0}, the duration in tenths of a second to wait for the file "
4639 "mutex which has been locked by another thread to be released before returning "
4640 "an error. When @code{-1}, then an error will be returned immediately."
4644 new_command("LS", 1, 1, ls_command
, _(
4646 "Lists the available data files stored in the data directory "
4647 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4650 new_command("RESET", 1, 1, NULL
, _(
4652 "Closes the currently opened file but keeps any previously set client options."
4655 new_command("NOP", 1, 1, NULL
, _(
4657 "Does nothing. Always returns successfully."
4660 /* !END-HELP-TEXT! */
4661 new_command ("CANCEL", 1, 1, NULL
, NULL
);
4662 new_command ("END", 1, 1, NULL
, NULL
);
4663 new_command ("BYE", 1, 1, NULL
, NULL
);
4666 for (i
= 0; command_table
[i
]; i
++);
4667 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
4672 register_commands (assuan_context_t ctx
)
4676 for (; command_table
[i
]; i
++)
4678 if (!command_table
[i
]->handler
)
4681 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
4682 command_table
[i
]->handler
,
4683 command_table
[i
]->help
);
4688 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
4692 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
4696 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
4700 return assuan_register_post_cmd_notify (ctx
, command_finalize
);