2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
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>
37 #include "pwmd-error.h"
42 #include "util-misc.h"
50 /* Flags needed to be retained for a client across commands. */
51 #define FLAG_NEW 0x0001
52 #define FLAG_HAS_LOCK 0x0002
53 #define FLAG_LOCK_CMD 0x0004
54 #define FLAG_NO_PINENTRY 0x0008
55 #define FLAG_OPEN 0x0010
56 #define FLAG_KEEP_LOCK 0x0020
58 /* These are command option flags. */
59 #define OPT_INQUIRE 0x0001
60 #define OPT_NO_PASSPHRASE 0x0002
61 #define OPT_RESET 0x0004
62 #define OPT_LIST_RECURSE 0x0008
63 #define OPT_VERBOSE 0x0010
64 #define OPT_LOCK 0x0020
65 #define OPT_LOCK_ON_OPEN 0x0040
66 #define OPT_SIGN 0x0080
67 #define OPT_LIST_ALL 0x0100
68 #define OPT_LIST_WITH_TARGET 0x0200
69 #define OPT_DATA 0x0400
70 #define OPT_NO_AGENT 0x0800
71 #define OPT_SECRET_ONLY 0x1000
72 #define OPT_INQUIRE_KEYID 0x2000
73 #define OPT_INQUIRE_SIGN_KEYID 0x4000
75 struct command_table_s
78 gpg_error_t (*handler
) (assuan_context_t
, char *line
);
81 int unlock
; // unlock the file mutex after validating the checksum
84 static struct command_table_s
**command_table
;
86 static gpg_error_t
do_lock (struct client_s
*client
, int add
);
87 static gpg_error_t
validate_checksum (struct client_s
*,
88 struct cache_data_s
*);
89 static gpg_error_t
update_checksum (struct client_s
*client
);
92 update_client_state (struct client_s
*client
, unsigned s
)
94 client
->thd
->state
= s
;
96 if (config_get_boolean ("global", "send_state"))
97 send_status_all_not_self (STATUS_STATE
, "%p %u", client
->thd
->tid
,
102 unlock_file_mutex (struct client_s
*client
, int remove
)
106 // OPEN: keep the lock for the same file being reopened.
107 if (client
->flags
& FLAG_KEEP_LOCK
&& client
->flags
& FLAG_HAS_LOCK
)
110 if (!(client
->flags
& FLAG_HAS_LOCK
))
111 return GPG_ERR_NOT_LOCKED
;
113 rc
= cache_unlock_mutex (client
->md5file
, remove
);
115 rc
= GPG_ERR_INV_STATE
;
117 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_LOCK_CMD
);
123 lock_file_mutex (struct client_s
*client
, int add
)
126 int timeout
= config_get_integer (client
->filename
, "cache_timeout");
128 if (client
->flags
& FLAG_HAS_LOCK
)
131 rc
= cache_lock_mutex (client
->ctx
, client
->md5file
,
132 client
->lock_timeout
, add
, timeout
);
134 client
->flags
|= FLAG_HAS_LOCK
;
140 file_modified (struct client_s
*client
, struct command_table_s
*cmd
)
144 if (!(client
->flags
& FLAG_OPEN
))
145 return GPG_ERR_INV_STATE
;
147 rc
= lock_file_mutex (client
, 0);
148 if (!rc
|| rc
== GPG_ERR_NO_DATA
)
150 rc
= validate_checksum (client
, NULL
);
151 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
154 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
157 if ((cmd
->unlock
&& !(client
->flags
& FLAG_HAS_LOCK
)) || rc
)
158 unlock_file_mutex (client
, 0);
164 parse_xml (assuan_context_t ctx
, int new)
166 struct client_s
*client
= assuan_get_pointer (ctx
);
167 int cached
= client
->doc
!= NULL
;
172 client
->doc
= new_document ();
178 xmlDocDumpFormatMemory (client
->doc
, &result
, &len
, 0);
179 client
->crypto
->plaintext
= result
;
180 client
->crypto
->plaintext_size
= len
;
181 if (!client
->crypto
->plaintext
)
183 xmlFreeDoc (client
->doc
);
190 rc
= parse_doc ((char *) client
->crypto
->plaintext
,
191 client
->crypto
->plaintext_size
, (xmlDocPtr
*)&client
->doc
);
197 free_client (struct client_s
*client
)
200 xmlFreeDoc (client
->doc
);
203 xfree (client
->filename
);
204 xfree (client
->last_error
);
205 crypto_cleanup (client
->crypto
);
206 client
->crypto
= NULL
;
210 cleanup_client (struct client_s
*client
)
212 assuan_context_t ctx
= client
->ctx
;
213 struct client_thread_s
*thd
= client
->thd
;
214 long lock_timeout
= client
->lock_timeout
;
215 xmlErrorPtr xml_error
= client
->xml_error
;
216 int no_pinentry
= (client
->flags
& FLAG_NO_PINENTRY
);
218 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
219 free_client (client
);
220 memset (client
, 0, sizeof (struct client_s
));
221 client
->xml_error
= xml_error
;
224 client
->lock_timeout
= lock_timeout
;
225 client
->flags
|= no_pinentry
? FLAG_NO_PINENTRY
: 0;
229 req_cleanup (void *arg
)
234 strv_free ((char **) arg
);
238 parse_open_opt_lock (void *data
, void *value
)
240 struct client_s
*client
= data
;
242 client
->opts
|= OPT_LOCK_ON_OPEN
;
247 parse_opt_inquire (void *data
, void *value
)
249 struct client_s
*client
= data
;
252 client
->opts
|= OPT_INQUIRE
;
257 update_checksum (struct client_s
*client
)
261 struct cache_data_s
*cdata
;
262 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
269 cdata
= cache_get_data (client
->md5file
);
273 cdata
->crc
= xmalloc (len
);
274 memcpy (cdata
->crc
, crc
, len
);
281 validate_checksum (struct client_s
*client
, struct cache_data_s
*cdata
)
288 if (cdata
&& !cdata
->crc
)
289 return GPG_ERR_CHECKSUM
;
291 rc
= get_checksum (client
->filename
, &crc
, &len
);
296 n
= memcmp (cdata
->crc
, crc
, len
);
297 else if (client
->crc
)
298 n
= memcmp (client
->crc
, crc
, len
);
301 return n
? GPG_ERR_CHECKSUM
: 0;
305 open_command (assuan_context_t ctx
, char *line
)
308 struct client_s
*client
= assuan_get_pointer (ctx
);
309 char **req
, *filename
;
310 unsigned char md5file
[16];
312 assuan_peercred_t peer
;
313 struct cache_data_s
*cdata
= NULL
;
315 struct argv_s
*args
[] = {
316 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
320 rc
= parse_options (&line
, args
, client
);
322 return send_error (ctx
, rc
);
324 req
= str_split (line
, " ", 2);
326 return send_error (ctx
, GPG_ERR_SYNTAX
);
328 rc
= do_validate_peer (ctx
, req
[0], &peer
);
332 return send_error (ctx
, rc
);
336 if (!valid_filename (filename
))
339 return send_error (ctx
, GPG_ERR_INV_VALUE
);
342 pthread_cleanup_push (req_cleanup
, req
);
343 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
344 /* This client may have locked a different file with ISCACHED --lock than
345 * the current filename. This will remove that lock. */
346 same_file
= !memcmp (md5file
, client
->md5file
, 16) ? 1 : 0;
347 if (client
->flags
& FLAG_OPEN
||
348 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
350 uint32_t opts
= client
->opts
;
351 uint32_t flags
= client
->flags
;
354 client
->flags
|= FLAG_KEEP_LOCK
;
355 else if (client
->flags
& FLAG_NEW
)
356 cache_clear (client
->md5file
);
358 cleanup_client (client
);
360 client
->flags
|= flags
;
361 client
->flags
&= ~(FLAG_LOCK_CMD
);
363 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_NEW
);
366 memcpy (client
->md5file
, md5file
, 16);
367 client
->filename
= str_dup (filename
);
368 if (!client
->filename
)
371 return send_error(ctx
, GPG_ERR_ENOMEM
);
374 /* Need to lock the mutex here because file_modified() cannot without
375 * knowing the filename. */
376 rc
= lock_file_mutex (client
, 1);
378 client
->flags
&= ~FLAG_OPEN
;
384 rc
= crypto_init (&client
->crypto
, client
->ctx
, client
->filename
,
385 client
->flags
& FLAG_NO_PINENTRY
);
386 if (!rc
&& stat (client
->filename
, &st
) == -1)
387 rc
= gpg_error_from_errno (errno
);
390 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_ENOENT
)
392 cdata
= cache_get_data (client
->md5file
);
397 client
->flags
|= FLAG_NEW
;
398 // data file disappeared. clear the cache entry.
399 cache_clear (client
->md5file
);
402 else if (cdata
&& cdata
->doc
) // cached document
406 if (!rc
&& !(client
->flags
& FLAG_NEW
))
408 rc
= validate_checksum (client
, cdata
);
409 if (rc
== GPG_ERR_CHECKSUM
)
417 if (!rc
&& client
->thd
->remote
418 && config_get_boolean (client
->filename
, "tcp_require_key")
419 && !(client
->flags
& FLAG_NEW
))
421 rc
= GPG_ERR_KEY_EXPIRED
;
425 if (reload
) // GPG_ERR_CHECKSUM or GPG_ERR_KEY_EXPIRED
427 log_write ("%s: %s", client
->filename
,
428 pwmd_strerror (rc
? rc
: GPG_ERR_CHECKSUM
));
429 cache_clear (client
->md5file
);
431 rc
= crypto_decrypt (client
, client
->crypto
);
436 client
->crypto
->plaintext
= cdata
->doc
;
437 client
->crypto
->plaintext_size
= cdata
->size
;
438 rc
= cache_decrypt (client
->crypto
);
443 strv_free (client
->crypto
->pubkey
);
444 strv_free (client
->crypto
->sigkey
);
445 client
->crypto
->pubkey
= strv_dup (cdata
->pubkey
);
446 client
->crypto
->sigkey
= strv_dup (cdata
->sigkey
);
451 client
->crypto
->plaintext
= NULL
;
452 client
->crypto
->plaintext_size
= 0;
456 else // existing file
458 cached
= cdata
!= NULL
;
459 rc
= crypto_decrypt (client
, client
->crypto
);
465 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
468 rc
= cache_encrypt (client
->crypto
);
470 cache_clear (client
->md5file
);
473 int timeout
= config_get_integer (client
->filename
,
476 free_cache_data_once (cdata
);
477 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
478 cdata
->doc
= client
->crypto
->plaintext
;
479 cdata
->size
= client
->crypto
->plaintext_size
;
480 cdata
->pubkey
= strv_dup(client
->crypto
->pubkey
);
481 cdata
->sigkey
= strv_dup(client
->crypto
->sigkey
);
482 client
->crypto
->plaintext
= NULL
;
483 client
->crypto
->plaintext_size
= 0;
485 if (cached
) // wont increment the refcount
487 rc
= cache_set_data (client
->md5file
, cdata
);
488 /* The cache entry may have been removed and cache_set_data()
489 * already sent STATUS_CACHE. */
490 if (!cache_iscached (client
->filename
, NULL
))
491 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
494 rc
= cache_add_file (client
->md5file
, cdata
, timeout
);
499 pthread_cleanup_pop (1);
503 client
->flags
|= FLAG_OPEN
;
504 update_checksum (client
);
506 if (client
->flags
& FLAG_NEW
)
507 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
509 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
510 rc
= do_lock (client
, 0);
514 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
516 if (client
->flags
& FLAG_NEW
)
517 cache_clear (client
->md5file
);
519 crypto_cleanup (client
->crypto
);
520 client
->crypto
= NULL
;
521 client
->flags
&= ~FLAG_OPEN
;
524 crypto_cleanup_non_keys (client
->crypto
);
525 return send_error (ctx
, rc
);
529 parse_opt_no_passphrase (void *data
, void *value
)
531 struct client_s
*client
= data
;
534 client
->opts
|= OPT_NO_PASSPHRASE
;
538 /* If not the invoking_user or new file, check that the list of user supplied
539 * keyid's are in the list of current keyid's obtained when decrypting the data
540 * file. Both short and long form keyid fingerprints are valid:
542 * 8 byte with optional "0x" prefix
546 * Only key fingerprints are compared and not user id's when not the
550 permitted_to_save (struct client_s
*client
, const char **keys
,
556 if (client
->flags
& FLAG_NEW
)
559 rc
= peer_is_invoker (client
);
563 for (v
= value
; v
&& *v
; v
++)
565 const char **k
, *pv
= *v
;
568 if (*pv
== '0' && *(pv
+1) == 'x')
571 for (k
= keys
; k
&& *k
; k
++)
575 if (*pk
== '0' && *(pk
+1) == 'x')
578 rc
= !strcmp (pv
, pk
) ? 0 : GPG_ERR_EACCES
;
581 size_t len
= strlen (*k
);
584 p
+= len
== 16 ? 8 : 32;
585 rc
= !strcmp (pv
, p
) ? 0 : GPG_ERR_EACCES
;
596 return GPG_ERR_EACCES
;
603 parse_save_opt_keyid_common (struct client_s
*client
, const char **list
,
604 const char *value
, char ***dst
)
609 if (!value
|| !*value
)
610 return GPG_ERR_INV_VALUE
;
612 p
= str_split (value
, ",", 0);
614 return GPG_ERR_ENOMEM
;
616 rc
= permitted_to_save (client
, list
, (const char **)p
);
626 parse_save_opt_keyid (void *data
, void *value
)
628 struct client_s
*client
= data
;
629 const char *str
= value
;
633 rc
= parse_save_opt_keyid_common (client
,
634 (const char **)client
->crypto
->pubkey
,
639 client
->crypto
->save
.pubkey
= dst
;
644 parse_save_opt_sign_keyid (void *data
, void *value
)
646 struct client_s
*client
= data
;
647 const char *str
= value
;
651 rc
= parse_save_opt_keyid_common (client
,
652 (const char **)client
->crypto
->sigkey
,
657 client
->crypto
->save
.sigkey
= dst
;
662 parse_save_opt_inquire (struct client_s
*client
, uint32_t opt
)
664 if (opt
== OPT_INQUIRE
&& !(client
->flags
& FLAG_NEW
))
666 gpg_error_t rc
= peer_is_invoker (client
);
677 parse_save_opt_inquire_keyparam (void *data
, void *value
)
679 return parse_save_opt_inquire (data
, OPT_INQUIRE
);
683 parse_save_opt_inquire_keyid (void *data
, void *value
)
685 return parse_save_opt_inquire (data
, OPT_INQUIRE_KEYID
);
689 parse_save_opt_inquire_sign_keyid (void *data
, void *value
)
691 return parse_save_opt_inquire (data
, OPT_INQUIRE_SIGN_KEYID
);
694 /* Tests that the keys in new_keys are also in old_keys. */
696 compare_keys (char **new_keys
, char **old_keys
)
700 if (!new_keys
|| !*new_keys
)
703 if (!old_keys
|| !*old_keys
)
706 for (o
= old_keys
; *o
; o
++)
710 for (n
= new_keys
; *n
; n
++)
712 if (!strcmp (*n
, *o
))
717 return GPG_ERR_NOT_FOUND
;
724 inquire_keyid (struct client_s
*client
, uint32_t opt
)
727 unsigned char *result
= NULL
;
733 if (opt
== OPT_INQUIRE_KEYID
)
736 orig
= &client
->crypto
->pubkey
;
737 save
= &client
->crypto
->save
.pubkey
;
741 s
= "INQUIRE_SIGN_KEYID";
742 orig
= &client
->crypto
->sigkey
;
743 save
= &client
->crypto
->save
.sigkey
;
746 rc
= assuan_inquire (client
->ctx
, s
, &result
, &len
, 0);
751 rc
= parse_save_opt_keyid_common (client
, (const char **)*orig
,
752 (char *)result
, &dst
);
762 save_command (assuan_context_t ctx
, char *line
)
764 struct client_s
*client
= assuan_get_pointer (ctx
);
765 struct cache_data_s
*cdata
= NULL
;
766 gpg_error_t rc
, cached
= 0;
769 struct argv_s
*args
[] = {
770 &(struct argv_s
) {"keyid", OPTION_TYPE_ARG
, parse_save_opt_keyid
},
771 &(struct argv_s
) {"sign-keyid", OPTION_TYPE_ARG
, parse_save_opt_sign_keyid
},
772 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
773 parse_save_opt_inquire_keyparam
},
774 &(struct argv_s
) {"inquire-keyid", OPTION_TYPE_NOARG
,
775 parse_save_opt_inquire_keyid
},
776 &(struct argv_s
) {"inquire-sign-keyid", OPTION_TYPE_NOARG
,
777 parse_save_opt_inquire_sign_keyid
},
781 rc
= parse_options (&line
, args
, client
);
783 return send_error (ctx
, rc
);
785 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
786 return send_error (ctx
, gpg_error_from_errno (errno
));
788 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
790 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
791 return send_error (ctx
, GPG_ERR_ENOANO
);
794 cached
= rc
= cache_iscached (client
->filename
, &defer
);
795 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
797 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
801 rc
= update_element_mtime (client
, xmlDocGetRootElement (client
->doc
));
804 return send_error (ctx
, rc
);
806 rc
= crypto_init_ctx (client
->crypto
, client
->flags
& FLAG_NO_PINENTRY
);
808 return send_error (ctx
, rc
);
810 if (((client
->opts
& OPT_INQUIRE_KEYID
)
811 || (client
->opts
& OPT_INQUIRE_SIGN_KEYID
)))
813 if (client
->opts
& OPT_INQUIRE
)
814 return send_error (ctx
, GPG_ERR_CONFLICT
);
816 if (client
->opts
& OPT_INQUIRE_KEYID
)
817 rc
= inquire_keyid (client
, OPT_INQUIRE_KEYID
);
819 if (!rc
&& (client
->opts
& OPT_INQUIRE_SIGN_KEYID
))
820 rc
= inquire_keyid (client
, OPT_INQUIRE_SIGN_KEYID
);
822 else if ((client
->crypto
->save
.pubkey
|| client
->crypto
->save
.sigkey
)
823 && client
->opts
& OPT_INQUIRE
)
824 return send_error (ctx
, GPG_ERR_CONFLICT
);
826 if (!rc
&& (client
->opts
& OPT_INQUIRE
))
828 /* Require a passphrase when generating a new key pair for an existing
831 if (!(client
->flags
& FLAG_NEW
))
832 rc
= crypto_try_decrypt (client
, client
->flags
& FLAG_NO_PINENTRY
);
836 unsigned char *params
= NULL
;
839 rc
= assuan_inquire (client
->ctx
, "KEYPARAM", ¶ms
, &len
, 0);
842 pthread_cleanup_push (xfree
, params
);
843 rc
= crypto_genkey (client
, client
->crypto
, params
);
844 pthread_cleanup_pop (1);
848 else if (!rc
&& (client
->flags
& FLAG_NEW
))
850 if (!client
->crypto
->save
.pubkey
&& !client
->crypto
->save
.sigkey
)
852 char *params
= crypto_default_key_params ();
859 pthread_cleanup_push (xfree
, params
);
860 rc
= crypto_genkey (client
, client
->crypto
,
861 (unsigned char *)params
);
862 pthread_cleanup_pop (1);
867 if (!client
->crypto
->save
.pubkey
&& client
->crypto
->save
.sigkey
)
868 client
->crypto
->save
.pubkey
= strv_dup (client
->crypto
->save
.sigkey
);
870 if (client
->crypto
->save
.pubkey
&& !client
->crypto
->save
.sigkey
)
871 client
->crypto
->save
.sigkey
= strv_dup (client
->crypto
->save
.pubkey
);
876 cdata
= cache_get_data (client
->md5file
);
879 rc
= compare_keys (client
->crypto
->save
.pubkey
, cdata
->pubkey
);
881 rc
= compare_keys (client
->crypto
->save
.sigkey
, cdata
->sigkey
);
883 /* Prevent saving to a recipient who is not in the original recipient
884 * list without a passphrase. */
886 rc
= crypto_try_decrypt (client
, client
->flags
& FLAG_NO_PINENTRY
);
890 if (!client
->crypto
->save
.pubkey
)
891 client
->crypto
->save
.pubkey
= strv_dup (cdata
->pubkey
);
893 if (!client
->crypto
->save
.sigkey
&& cdata
->sigkey
)
894 client
->crypto
->save
.sigkey
= strv_dup (cdata
->sigkey
);
895 else if (!client
->crypto
->save
.sigkey
)
896 client
->crypto
->save
.sigkey
= strv_dup (cdata
->pubkey
);
901 client
->crypto
->save
.pubkey
= strv_dup (client
->crypto
->pubkey
);
902 client
->crypto
->save
.sigkey
= strv_dup (client
->crypto
->sigkey
);
910 xmlDocDumpFormatMemory (client
->doc
, &client
->crypto
->plaintext
, &size
,
913 client
->crypto
->plaintext_size
= (size_t) size
;
917 rc
= crypto_encrypt (client
, client
->crypto
);
920 rc
= crypto_write_file (client
->crypto
);
923 if (!cached
) // no error
924 cdata
= cache_get_data (client
->md5file
);
929 rc
= cache_encrypt (client
->crypto
);
932 cache_clear (client
->md5file
);
933 strv_free (client
->crypto
->pubkey
);
934 client
->crypto
->pubkey
= strv_dup (client
->crypto
->save
.pubkey
);
935 strv_free (client
->crypto
->sigkey
);
936 client
->crypto
->sigkey
= strv_dup (client
->crypto
->save
.sigkey
);
937 client
->flags
&= ~(FLAG_NEW
);
943 int timeout
= config_get_integer (client
->filename
,
946 free_cache_data_once (cdata
);
947 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
948 cdata
->doc
= client
->crypto
->plaintext
;
949 client
->crypto
->plaintext
= NULL
;
950 cdata
->size
= client
->crypto
->plaintext_size
;
951 client
->crypto
->plaintext_size
= 0;
952 cdata
->pubkey
= client
->crypto
->save
.pubkey
;
953 client
->crypto
->save
.pubkey
= NULL
;
954 cdata
->sigkey
= client
->crypto
->save
.sigkey
;
955 client
->crypto
->save
.sigkey
= NULL
;
957 /* Update in case the cache entry expires the next SAVE may not
958 * have any known keys. */
959 strv_free (client
->crypto
->pubkey
);
960 client
->crypto
->pubkey
= strv_dup (cdata
->pubkey
);
961 strv_free (client
->crypto
->sigkey
);
962 client
->crypto
->sigkey
= strv_dup (cdata
->sigkey
);
964 if (!cached
) // no error and wont increment refcount
965 rc
= cache_set_data (client
->md5file
, cdata
);
967 rc
= cache_add_file (client
->md5file
, cdata
, timeout
);
969 if (!rc
&& (cached
|| (client
->flags
& FLAG_NEW
)))
970 send_status_all (STATUS_CACHE
, NULL
);
974 rc
= update_checksum (client
);
975 client
->flags
&= ~(FLAG_NEW
);
981 crypto_cleanup_non_keys (client
->crypto
);
982 return send_error (ctx
, rc
);
986 do_delete (assuan_context_t ctx
, char *line
)
988 struct client_s
*client
= assuan_get_pointer (ctx
);
993 if (strchr (line
, '\t'))
994 req
= str_split (line
, "\t", 0);
996 req
= str_split (line
, " ", 0);
999 return GPG_ERR_SYNTAX
;
1001 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1009 * No sub-node defined. Remove the entire node (root element).
1015 rc
= is_element_owner (client
, n
);
1018 rc
= unlink_node (client
, n
);
1027 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
, NULL
,
1028 NULL
, NULL
, 0, 0, NULL
, 0);
1033 rc
= is_element_owner (client
, n
);
1036 rc
= unlink_node (client
, n
);
1044 delete_command (assuan_context_t ctx
, char *line
)
1046 struct client_s
*client
= assuan_get_pointer (ctx
);
1048 struct argv_s
*args
[] = {
1049 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1053 rc
= parse_options (&line
, args
, client
);
1055 return send_error (ctx
, rc
);
1057 if (client
->opts
& OPT_INQUIRE
)
1059 unsigned char *result
;
1062 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1064 return send_error (ctx
, rc
);
1066 pthread_cleanup_push (xfree
, result
);
1067 rc
= do_delete (ctx
, (char *)result
);
1068 pthread_cleanup_pop (1);
1071 rc
= do_delete (ctx
, line
);
1073 return send_error (ctx
, rc
);
1077 store_command (assuan_context_t ctx
, char *line
)
1079 struct client_s
*client
= assuan_get_pointer (ctx
);
1082 unsigned char *result
;
1084 xmlNodePtr n
, parent
;
1086 char *content
= NULL
;
1088 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1090 return send_error (ctx
, rc
);
1092 req
= str_split ((char *) result
, "\t", 0);
1096 return send_error (ctx
, GPG_ERR_SYNTAX
);
1098 len
= strv_length (req
);
1099 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1100 if (*(req
+ 1) && !valid_element_path (req
, has_content
))
1103 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1106 if (has_content
|| !*req
[len
- 1])
1109 content
= req
[len
- 1];
1110 req
[len
- 1] = NULL
;
1114 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1115 if (rc
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1117 rc
= new_root_element (client
, client
->doc
, *req
);
1121 return send_error (ctx
, rc
);
1130 return send_error (ctx
, rc
);
1135 if (req
[1] && *req
[1])
1138 parent
= create_elements_cb (client
, 1, n
, req
+ 1, &rc
, NULL
);
1140 parent
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
,
1141 NULL
, NULL
, create_elements_cb
, 0, 0, NULL
,
1147 rc
= is_element_owner (client
, parent
);
1150 n
= find_text_node (parent
->children
);
1152 xmlNodeSetContent (n
, (xmlChar
*) content
);
1154 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1156 update_element_mtime (client
, parent
);
1162 return send_error (ctx
, rc
);
1166 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1171 int progress
= config_get_integer ("global", "xfer_progress");
1175 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1176 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1177 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1185 if (sent
+ to_send
> total
)
1186 to_send
= total
- sent
;
1188 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1189 flush
? 0 : to_send
);
1192 sent
+= flush
? 0 : to_send
;
1194 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1195 (sent
== total
&& flush
))
1196 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1198 if (!flush
&& !rc
&& sent
== total
)
1205 while (!rc
&& sent
< total
);
1211 do_get (assuan_context_t ctx
, char *line
)
1213 struct client_s
*client
= assuan_get_pointer (ctx
);
1218 req
= str_split (line
, "\t", 0);
1223 return GPG_ERR_SYNTAX
;
1226 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1235 find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
1242 if (!n
|| !n
->children
)
1243 return GPG_ERR_NO_DATA
;
1245 n
= find_text_node (n
->children
);
1246 if (!n
|| !n
->content
|| !*n
->content
)
1247 return GPG_ERR_NO_DATA
;
1249 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1254 get_command (assuan_context_t ctx
, char *line
)
1256 struct client_s
*client
= assuan_get_pointer (ctx
);
1258 struct argv_s
*args
[] = {
1259 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1263 rc
= parse_options (&line
, args
, client
);
1265 return send_error (ctx
, rc
);
1267 if (client
->opts
& OPT_INQUIRE
)
1269 unsigned char *result
;
1272 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1274 return send_error (ctx
, rc
);
1276 pthread_cleanup_push (xfree
, result
);
1277 rc
= do_get (ctx
, (char *)result
);
1278 pthread_cleanup_pop (1);
1281 rc
= do_get (ctx
, line
);
1283 return send_error (ctx
, rc
);
1286 static void list_command_cleanup1 (void *arg
);
1288 realpath_command (assuan_context_t ctx
, char *line
)
1290 struct string_s
*string
= NULL
;
1292 struct client_s
*client
= assuan_get_pointer (ctx
);
1293 struct argv_s
*args
[] = {
1294 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1298 rc
= parse_options (&line
, args
, client
);
1300 return send_error (ctx
, rc
);
1302 if (client
->opts
& OPT_INQUIRE
)
1304 unsigned char *result
;
1307 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1309 return send_error (ctx
, rc
);
1311 pthread_cleanup_push (xfree
, result
);
1312 rc
= build_realpath (client
, client
->doc
, (char *)result
, &string
);
1313 pthread_cleanup_pop (1);
1316 rc
= build_realpath (client
, client
->doc
, line
, &string
);
1320 pthread_cleanup_push (list_command_cleanup1
, string
);
1321 rc
= xfer_data (ctx
, string
->str
, string
->len
);
1322 pthread_cleanup_pop (1);
1325 return send_error (ctx
, rc
);
1329 list_command_cleanup1 (void *arg
)
1332 string_free ((struct string_s
*) arg
, 1);
1336 list_command_cleanup2 (void *arg
)
1338 struct element_list_s
*elements
= arg
;
1344 int total
= slist_length (elements
->list
);
1347 for (i
= 0; i
< total
; i
++)
1349 char *tmp
= slist_nth_data (elements
->list
, i
);
1353 slist_free (elements
->list
);
1356 if (elements
->prefix
)
1357 xfree (elements
->prefix
);
1360 strv_free (elements
->req
);
1367 parse_list_opt_norecurse (void *data
, void *value
)
1369 struct client_s
*client
= data
;
1371 client
->opts
&= ~(OPT_LIST_RECURSE
);
1376 parse_opt_verbose (void *data
, void *value
)
1378 struct client_s
*client
= data
;
1380 client
->opts
|= OPT_VERBOSE
;
1385 parse_list_opt_target (void *data
, void *value
)
1387 struct client_s
*client
= data
;
1389 client
->opts
|= OPT_LIST_WITH_TARGET
;
1394 parse_list_opt_all (void *data
, void *value
)
1396 struct client_s
*client
= data
;
1398 client
->opts
|= OPT_LIST_ALL
;
1403 list_path_once (struct client_s
*client
, char *line
,
1404 struct element_list_s
*elements
, struct string_s
*result
)
1408 elements
->req
= str_split (line
, " ", 0);
1410 strv_printf (&elements
->req
, "%s", line
);
1412 rc
= create_path_list (client
, client
->doc
, elements
, *elements
->req
);
1413 if ((rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
) && elements
->verbose
)
1418 int total
= slist_length (elements
->list
);
1421 rc
= GPG_ERR_NO_DATA
;
1429 for (i
= 0; i
< total
; i
++)
1431 char *tmp
= slist_nth_data (elements
->list
, i
);
1433 string_append_printf (result
, "%s%s", tmp
,
1434 i
+ 1 == total
? "" : "\n");
1439 rc
= GPG_ERR_NO_DATA
;
1446 has_list_flag (char *path
, char *flags
)
1450 while (*p
&& *++p
!= ' ');
1459 for (f
= flags
; *f
&& *f
!= ' '; f
++)
1470 do_list (assuan_context_t ctx
, char *line
)
1472 struct client_s
*client
= assuan_get_pointer (ctx
);
1474 struct element_list_s
*elements
= NULL
;
1476 elements
= xcalloc (1, sizeof (struct element_list_s
));
1478 return GPG_ERR_ENOMEM
;
1480 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1482 (client
->opts
& OPT_VERBOSE
) | (client
->opts
& OPT_LIST_WITH_TARGET
);
1483 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1485 if (!line
|| !*line
)
1487 struct string_s
*str
= NULL
;
1489 pthread_cleanup_push (list_command_cleanup2
, elements
);
1490 rc
= list_root_elements (client
, client
->doc
, &str
, elements
->verbose
,
1491 elements
->with_target
);
1492 pthread_cleanup_pop (1);
1493 pthread_cleanup_push (list_command_cleanup1
, str
);
1497 if (client
->opts
& OPT_LIST_ALL
)
1499 char **roots
= str_split (str
->str
, "\n", 0);
1502 pthread_cleanup_push (req_cleanup
, roots
);
1503 string_truncate (str
, 0);
1505 for (p
= roots
; *p
; p
++)
1507 if (strchr (*p
, ' '))
1509 if (has_list_flag (*p
, "EOP"))
1511 string_append_printf (str
, "%s%s", *p
,
1512 *(p
+ 1) ? "\n" : "");
1517 elements
= xcalloc (1, sizeof (struct element_list_s
));
1520 rc
= GPG_ERR_ENOMEM
;
1524 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1526 (client
->opts
& OPT_VERBOSE
) | (client
->opts
&
1527 OPT_LIST_WITH_TARGET
);
1528 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1529 pthread_cleanup_push (list_command_cleanup2
, elements
);
1530 rc
= list_path_once (client
, *p
, elements
, str
);
1531 pthread_cleanup_pop (1);
1536 string_append (str
, "\n");
1539 pthread_cleanup_pop (1);
1543 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1546 pthread_cleanup_pop (1);
1550 pthread_cleanup_push (list_command_cleanup2
, elements
);
1551 struct string_s
*str
= string_new (NULL
);
1552 pthread_cleanup_push (list_command_cleanup1
, str
);
1553 rc
= list_path_once (client
, line
, elements
, str
);
1555 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1557 pthread_cleanup_pop (1);
1558 pthread_cleanup_pop (1);
1563 list_command (assuan_context_t ctx
, char *line
)
1565 struct client_s
*client
= assuan_get_pointer (ctx
);
1567 struct argv_s
*args
[] = {
1568 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
,
1569 parse_list_opt_norecurse
},
1570 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
1571 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1572 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
,
1573 parse_list_opt_target
},
1574 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_all
},
1578 if (disable_list_and_dump
== 1)
1579 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1581 client
->opts
|= OPT_LIST_RECURSE
;
1582 rc
= parse_options (&line
, args
, client
);
1584 return send_error (ctx
, rc
);
1586 if (client
->opts
& OPT_LIST_ALL
)
1587 client
->opts
|= OPT_LIST_RECURSE
| OPT_VERBOSE
;
1589 if (client
->opts
& OPT_INQUIRE
)
1591 unsigned char *result
;
1594 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1596 return send_error (ctx
, rc
);
1598 pthread_cleanup_push (xfree
, result
);
1599 rc
= do_list (ctx
, (char *)result
);
1600 pthread_cleanup_pop (1);
1603 rc
= do_list (ctx
, line
);
1605 return send_error (ctx
, rc
);
1609 * req[0] - element path
1612 attribute_list (assuan_context_t ctx
, char **req
)
1614 struct client_s
*client
= assuan_get_pointer (ctx
);
1615 char **attrlist
= NULL
;
1623 if (!req
|| !req
[0])
1624 return GPG_ERR_SYNTAX
;
1626 if ((path
= str_split (req
[0], "\t", 0)) == NULL
)
1629 * The first argument may be only a root element.
1631 if ((path
= str_split (req
[0], " ", 0)) == NULL
)
1632 return GPG_ERR_SYNTAX
;
1635 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
1645 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
1646 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1657 for (a
= n
->properties
; a
; a
= a
->next
)
1661 if ((pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *))) == NULL
)
1664 strv_free (attrlist
);
1666 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1667 pwmd_strerror (GPG_ERR_ENOMEM
));
1668 return GPG_ERR_ENOMEM
;
1673 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
,
1675 && an
->content
? (char *) an
->content
: "");
1679 strv_free (attrlist
);
1680 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1681 pwmd_strerror (GPG_ERR_ENOMEM
));
1682 return GPG_ERR_ENOMEM
;
1685 attrlist
[++i
] = NULL
;
1689 return GPG_ERR_NO_DATA
;
1691 line
= strv_join ("\n", attrlist
);
1695 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1696 pwmd_strerror (GPG_ERR_ENOMEM
));
1697 strv_free (attrlist
);
1698 return GPG_ERR_ENOMEM
;
1701 pthread_cleanup_push (xfree
, line
);
1702 pthread_cleanup_push (req_cleanup
, attrlist
);
1703 rc
= xfer_data (ctx
, line
, strlen (line
));
1704 pthread_cleanup_pop (1);
1705 pthread_cleanup_pop (1);
1710 * req[0] - attribute
1711 * req[1] - element path
1714 attribute_delete (struct client_s
*client
, char **req
)
1720 if (!req
|| !req
[0] || !req
[1])
1721 return GPG_ERR_SYNTAX
;
1723 if (!strcmp (req
[0], "_name"))
1724 return GPG_ERR_INV_ATTR
;
1726 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1729 * The first argument may be only a root element.
1731 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1732 return GPG_ERR_SYNTAX
;
1735 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
1741 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
1742 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1747 if (!strcmp (req
[0], (char *) "_acl"))
1749 rc
= is_element_owner (client
, n
);
1754 rc
= delete_attribute (client
, n
, (xmlChar
*) req
[0]);
1762 create_element_path (struct client_s
*client
,
1763 char ***elements
, gpg_error_t
* rc
, xmlNodePtr parent
)
1765 char **req
= *elements
;
1766 char **req_orig
= strv_dup (req
);
1767 xmlNodePtr n
= NULL
;
1773 *rc
= GPG_ERR_ENOMEM
;
1774 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1775 pwmd_strerror (GPG_ERR_ENOMEM
));
1780 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
1783 if (*rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1786 *rc
= new_root_element (client
, client
->doc
, req
[0]);
1792 else if (n
== parent
)
1794 *rc
= GPG_ERR_CONFLICT
;
1801 n
= create_target_elements_cb (client
, 1, n
, req
+ 1, rc
, NULL
);
1803 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
1804 NULL
, NULL
, create_target_elements_cb
, 0, 0,
1811 * Reset the position of the element tree now that the elements
1812 * have been created.
1817 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
1821 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
1822 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1829 strv_free (req_orig
);
1836 * Creates a "target" attribute. When other commands encounter an element with
1837 * this attribute, the element path is modified to the target value. If the
1838 * source element path doesn't exist when using 'ATTR SET target', it is
1839 * created, but the destination element path must exist.
1841 * req[0] - source element path
1842 * req[1] - destination element path
1845 target_attribute (struct client_s
*client
, char **req
)
1847 char **src
, **dst
, *line
= NULL
, **odst
= NULL
;
1851 if (!req
|| !req
[0] || !req
[1])
1852 return GPG_ERR_SYNTAX
;
1854 if ((src
= str_split (req
[0], "\t", 0)) == NULL
)
1857 * The first argument may be only a root element.
1859 if ((src
= str_split (req
[0], " ", 0)) == NULL
)
1860 return GPG_ERR_SYNTAX
;
1863 if (!valid_element_path (src
, 0))
1864 return GPG_ERR_INV_VALUE
;
1866 if ((dst
= str_split (req
[1], "\t", 0)) == NULL
)
1869 * The first argument may be only a root element.
1871 if ((dst
= str_split (req
[1], " ", 0)) == NULL
)
1873 rc
= GPG_ERR_SYNTAX
;
1878 odst
= strv_dup (dst
);
1881 rc
= GPG_ERR_ENOMEM
;
1885 n
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
1887 * Make sure the destination element path exists.
1894 n
= find_elements (client
, client
->doc
, n
->children
, dst
+ 1, &rc
,
1895 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1900 rc
= validate_target_attribute (client
, client
->doc
, req
[0], n
);
1901 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1904 n
= create_element_path (client
, &src
, &rc
, NULL
);
1908 line
= strv_join ("\t", odst
);
1911 rc
= GPG_ERR_ENOMEM
;
1915 rc
= add_attribute (client
, n
, "target", line
);
1926 * req[0] - attribute
1927 * req[1] - element path
1930 attribute_get (assuan_context_t ctx
, char **req
)
1932 struct client_s
*client
= assuan_get_pointer (ctx
);
1938 if (!req
|| !req
[0] || !req
[1])
1939 return GPG_ERR_SYNTAX
;
1941 if (strchr (req
[1], '\t'))
1943 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1944 return GPG_ERR_SYNTAX
;
1948 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1949 return GPG_ERR_SYNTAX
;
1952 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
1959 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
1960 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1968 if ((a
= xmlGetProp (n
, (xmlChar
*) req
[0])) == NULL
)
1969 return GPG_ERR_NOT_FOUND
;
1971 pthread_cleanup_push (xmlFree
, a
);
1974 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
1976 rc
= GPG_ERR_NO_DATA
;
1978 pthread_cleanup_pop (1);
1987 * req[0] - attribute
1988 * req[1] - element path
1992 attribute_set (struct client_s
*client
, char **req
)
1998 if (!req
|| !req
[0] || !req
[1])
1999 return GPG_ERR_SYNTAX
;
2002 * Reserved attribute names.
2004 if (!strcmp (req
[0], "_name"))
2005 return GPG_ERR_INV_ATTR
;
2006 else if (!strcmp (req
[0], "target"))
2007 return target_attribute (client
, req
+ 1);
2008 else if (!valid_xml_attribute (req
[0]))
2009 return GPG_ERR_INV_VALUE
;
2011 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2014 * The first argument may be only a root element.
2016 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2017 return GPG_ERR_SYNTAX
;
2020 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2027 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2028 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2034 if (!strcmp (req
[0], (char *) "_acl"))
2036 rc
= is_element_owner (client
, n
);
2041 rc
= add_attribute (client
, n
, req
[0], req
[2]);
2050 * req[1] - attribute name or element path if command is LIST
2051 * req[2] - element path
2052 * req[2] - element path or value
2056 do_attr (assuan_context_t ctx
, char *line
)
2058 struct client_s
*client
= assuan_get_pointer (ctx
);
2062 req
= str_split (line
, " ", 4);
2063 if (!req
|| !req
[0] || !req
[1])
2066 return GPG_ERR_SYNTAX
;
2069 pthread_cleanup_push (req_cleanup
, req
);
2071 if (strcasecmp (req
[0], "SET") == 0)
2072 rc
= attribute_set (client
, req
+ 1);
2073 else if (strcasecmp (req
[0], "GET") == 0)
2074 rc
= attribute_get (ctx
, req
+ 1);
2075 else if (strcasecmp (req
[0], "DELETE") == 0)
2076 rc
= attribute_delete (client
, req
+ 1);
2077 else if (strcasecmp (req
[0], "LIST") == 0)
2078 rc
= attribute_list (ctx
, req
+ 1);
2080 rc
= GPG_ERR_SYNTAX
;
2082 pthread_cleanup_pop (1);
2087 attr_command (assuan_context_t ctx
, char *line
)
2089 struct client_s
*client
= assuan_get_pointer (ctx
);
2091 struct argv_s
*args
[] = {
2092 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2096 rc
= parse_options (&line
, args
, client
);
2098 return send_error (ctx
, rc
);
2100 if (client
->opts
& OPT_INQUIRE
)
2102 unsigned char *result
;
2105 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2107 return send_error (ctx
, rc
);
2109 pthread_cleanup_push (xfree
, result
);
2110 rc
= do_attr (ctx
, (char *)result
);
2111 pthread_cleanup_pop (1);
2114 rc
= do_attr (ctx
, line
);
2116 return send_error (ctx
, rc
);
2120 parse_iscached_opt_lock (void *data
, void *value
)
2122 struct client_s
*client
= data
;
2125 client
->opts
|= OPT_LOCK
;
2130 iscached_command (assuan_context_t ctx
, char *line
)
2132 struct client_s
*client
= assuan_get_pointer (ctx
);
2134 struct argv_s
*args
[] = {
2135 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2139 if (!line
|| !*line
)
2140 return send_error (ctx
, GPG_ERR_SYNTAX
);
2142 rc
= parse_options (&line
, args
, client
);
2144 return send_error (ctx
, rc
);
2145 else if (!valid_filename (line
))
2146 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2148 rc
= cache_iscached (line
, NULL
);
2149 if (client
->opts
& OPT_LOCK
2150 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2152 unsigned char md5file
[16];
2153 gpg_error_t trc
= rc
;
2155 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2156 if (memcmp (md5file
, client
->md5file
, 16))
2157 cleanup_client (client
);
2159 memcpy (client
->md5file
, md5file
, 16);
2160 rc
= do_lock (client
, 1);
2165 return send_error (ctx
, rc
);
2169 clearcache_command (assuan_context_t ctx
, char *line
)
2171 gpg_error_t rc
= 0, all_rc
= 0;
2172 unsigned char md5file
[16];
2176 struct client_thread_s
*once
= NULL
;
2179 MUTEX_LOCK (&cn_mutex
);
2180 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
2182 if (!line
|| !*line
)
2185 t
= slist_length (cn_thread_list
);
2187 for (i
= 0; i
< t
; i
++)
2189 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2190 assuan_peercred_t peer
;
2195 /* Lock each connected clients' file mutex to prevent any other client
2196 * from accessing the cache entry (the file mutex is locked upon
2197 * command startup). The cache for the entry is not cleared if the
2198 * file mutex is locked by another client to prevent this function
2203 if (thd
->cl
->filename
)
2205 rc
= do_validate_peer (ctx
, thd
->cl
->filename
, &peer
);
2206 all_rc
= !all_rc
? rc
: all_rc
;
2214 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
2215 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2217 if (pthread_equal (pthread_self (), thd
->tid
))
2221 if (!thd
->cl
->filename
||
2222 cache_iscached (thd
->cl
->filename
,
2223 NULL
) == GPG_ERR_NO_DATA
)
2229 cache_defer_clear (thd
->cl
->md5file
);
2232 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2240 rc
= cache_clear (thd
->cl
->md5file
);
2241 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
));
2254 rc
= do_validate_peer (ctx
, line
, &peer
);
2256 if (!rc
&& !memcmp (thd
->cl
->md5file
, md5file
, sizeof (md5file
)))
2258 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0,
2260 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2262 if (pthread_equal (pthread_self (), thd
->tid
))
2269 rc
= cache_clear (thd
->cl
->md5file
);
2270 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2274 cache_defer_clear (thd
->cl
->md5file
);
2282 /* Only connected clients' cache entries have been cleared. Now clear any
2283 * remaining cache entries without clients but only if there wasn't an
2284 * error from above since this would defeat the locking check of the
2285 * remaining entries. */
2291 /* No clients are using the specified file. */
2292 else if (!all_rc
&& !rc
&& !once
)
2294 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2295 rc
= cache_clear (md5file
);
2298 /* Release the connection mutex. */
2299 pthread_cleanup_pop (1);
2303 send_status_all (STATUS_CACHE
, NULL
);
2305 /* One or more files were locked while clearing all cache entries. */
2309 return send_error (ctx
, rc
);
2313 cachetimeout_command (assuan_context_t ctx
, char *line
)
2316 char **req
= str_split (line
, " ", 0);
2319 assuan_peercred_t peer
;
2321 if (!req
|| !*req
|| !req
[1])
2324 return send_error (ctx
, GPG_ERR_SYNTAX
);
2328 timeout
= (int) strtol (req
[1], &p
, 10);
2329 if (errno
!= 0 || *p
|| timeout
< -1)
2332 return send_error (ctx
, GPG_ERR_SYNTAX
);
2335 rc
= do_validate_peer (ctx
, req
[0], &peer
);
2338 unsigned char md5file
[16];
2340 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, req
[0], strlen (req
[0]));
2341 rc
= cache_set_timeout (md5file
, timeout
);
2342 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2345 MUTEX_LOCK (&rcfile_mutex
);
2346 config_set_int_param (&global_config
, req
[0], "cache_timeout",
2348 MUTEX_UNLOCK (&rcfile_mutex
);
2353 return send_error (ctx
, rc
);
2357 dump_command (assuan_context_t ctx
, char *line
)
2361 struct client_s
*client
= assuan_get_pointer (ctx
);
2364 if (disable_list_and_dump
== 1)
2365 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2367 rc
= peer_is_invoker(client
);
2369 return send_error (ctx
, rc
);
2371 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2375 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2376 pwmd_strerror (GPG_ERR_ENOMEM
));
2377 return send_error (ctx
, GPG_ERR_ENOMEM
);
2380 pthread_cleanup_push (xmlFree
, xml
);
2381 rc
= xfer_data (ctx
, (char *) xml
, len
);
2382 pthread_cleanup_pop (1);
2383 return send_error (ctx
, rc
);
2387 getconfig_command (assuan_context_t ctx
, char *line
)
2389 struct client_s
*client
= assuan_get_pointer (ctx
);
2391 char filename
[255] = { 0 }, param
[747] = { 0 };
2392 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2394 if (!line
|| !*line
)
2395 return send_error (ctx
, GPG_ERR_SYNTAX
);
2397 if (strchr (line
, ' '))
2399 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2404 if (fp
&& !valid_filename (fp
))
2405 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2407 paramp
= str_down (paramp
);
2408 if (!strcmp (paramp
, "passphrase"))
2409 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2411 p
= config_get_value (fp
? fp
: "global", paramp
);
2413 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2415 tmp
= expand_homedir (p
);
2419 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2420 pwmd_strerror (GPG_ERR_ENOMEM
));
2421 return send_error (ctx
, GPG_ERR_ENOMEM
);
2425 pthread_cleanup_push (xfree
, p
);
2426 rc
= xfer_data (ctx
, p
, strlen (p
));
2427 pthread_cleanup_pop (1);
2428 return send_error (ctx
, rc
);
2433 xmlXPathContextPtr xp
;
2434 xmlXPathObjectPtr result
;
2440 xpath_command_cleanup (void *arg
)
2442 struct xpath_s
*xpath
= arg
;
2447 req_cleanup (xpath
->req
);
2450 xmlBufferFree (xpath
->buf
);
2453 xmlXPathFreeObject (xpath
->result
);
2456 xmlXPathFreeContext (xpath
->xp
);
2460 do_xpath (assuan_context_t ctx
, char *line
)
2463 struct client_s
*client
= assuan_get_pointer (ctx
);
2464 struct xpath_s _x
= { 0 };
2465 struct xpath_s
*xpath
= &_x
;
2467 if (!line
|| !*line
)
2468 return GPG_ERR_SYNTAX
;
2470 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2472 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2473 return GPG_ERR_ENOMEM
;
2476 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2479 rc
= GPG_ERR_BAD_DATA
;
2484 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2487 rc
= GPG_ERR_BAD_DATA
;
2491 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2493 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2497 rc
= recurse_xpath_nodeset (client
, client
->doc
, xpath
->result
->nodesetval
,
2498 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2502 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2504 rc
= GPG_ERR_NO_DATA
;
2507 else if (xpath
->req
[1])
2513 pthread_cleanup_push (xpath_command_cleanup
, &xpath
);
2514 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2515 xmlBufferLength (xpath
->buf
));
2516 pthread_cleanup_pop (0);
2518 xpath_command_cleanup (xpath
);
2523 xpath_command (assuan_context_t ctx
, char *line
)
2525 struct client_s
*client
= assuan_get_pointer (ctx
);
2527 struct argv_s
*args
[] = {
2528 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2532 if (disable_list_and_dump
== 1)
2533 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2535 rc
= peer_is_invoker(client
);
2537 return send_error (ctx
, rc
);
2539 rc
= parse_options (&line
, args
, client
);
2541 return send_error (ctx
, rc
);
2543 if (client
->opts
& OPT_INQUIRE
)
2545 unsigned char *result
;
2548 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2550 return send_error (ctx
, rc
);
2552 pthread_cleanup_push (xfree
, result
);
2553 rc
= do_xpath (ctx
, (char *)result
);
2554 pthread_cleanup_pop (1);
2557 rc
= do_xpath (ctx
, line
);
2559 return send_error (ctx
, rc
);
2563 do_xpathattr (assuan_context_t ctx
, char *line
)
2565 struct client_s
*client
= assuan_get_pointer (ctx
);
2569 struct xpath_s _x
= { 0 };
2570 struct xpath_s
*xpath
= &_x
;
2572 if (!line
|| !*line
)
2573 return GPG_ERR_SYNTAX
;
2575 if ((req
= str_split (line
, " ", 3)) == NULL
)
2576 return GPG_ERR_ENOMEM
;
2580 rc
= GPG_ERR_SYNTAX
;
2584 if (!strcasecmp (req
[0], "SET"))
2586 else if (!strcasecmp (req
[0], "DELETE"))
2590 rc
= GPG_ERR_SYNTAX
;
2594 if (!req
[1] || !req
[2])
2596 rc
= GPG_ERR_SYNTAX
;
2600 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2602 rc
= GPG_ERR_ENOMEM
;
2606 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2608 rc
= GPG_ERR_SYNTAX
;
2612 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2615 rc
= GPG_ERR_BAD_DATA
;
2619 xpath
->result
= xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2622 rc
= GPG_ERR_BAD_DATA
;
2626 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2628 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2632 rc
= recurse_xpath_nodeset (client
, client
->doc
, xpath
->result
->nodesetval
,
2633 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2634 (xmlChar
*) req
[1]);
2637 xpath_command_cleanup (xpath
);
2642 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2644 xpathattr_command (assuan_context_t ctx
, char *line
)
2646 struct client_s
*client
= assuan_get_pointer (ctx
);
2648 struct argv_s
*args
[] = {
2649 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2653 if (disable_list_and_dump
== 1)
2654 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2656 rc
= peer_is_invoker(client
);
2658 return send_error (ctx
, rc
);
2660 rc
= parse_options (&line
, args
, client
);
2662 return send_error (ctx
, rc
);
2664 if (client
->opts
& OPT_INQUIRE
)
2666 unsigned char *result
;
2669 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2671 return send_error (ctx
, rc
);
2673 pthread_cleanup_push (xfree
, result
);
2674 rc
= do_xpathattr (ctx
, (char *)result
);
2675 pthread_cleanup_pop (1);
2678 rc
= do_xpathattr (ctx
, line
);
2680 return send_error (ctx
, rc
);
2684 do_import (struct client_s
*client
, const char *root_element
,
2685 unsigned char *content
)
2687 char **dst_path
= NULL
;
2688 xmlDocPtr doc
= NULL
;
2689 xmlNodePtr n
, root
, copy
;
2692 if (!content
|| !*content
)
2695 return GPG_ERR_SYNTAX
;
2699 dst_path
= str_split (root_element
, "\t", 0);
2701 if (dst_path
&& !valid_element_path (dst_path
, 0))
2704 strv_free (dst_path
);
2706 return GPG_ERR_INV_VALUE
;
2709 struct string_s
*str
= string_new_content ((char *)content
);
2710 str
= string_prepend (str
, "<pwmd>");
2711 str
= string_append (str
, "</pwmd>");
2712 doc
= xmlReadDoc ((xmlChar
*) str
->str
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2713 string_free (str
, 1);
2716 rc
= GPG_ERR_BAD_DATA
;
2720 root
= xmlDocGetRootElement (doc
);
2721 xmlNodePtr root_orig
= root
->children
;
2722 root
= root
->children
;
2723 rc
= validate_import (client
, root
);
2732 char **path
= strv_dup (dst_path
);
2735 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2736 pwmd_strerror (GPG_ERR_ENOMEM
));
2737 rc
= GPG_ERR_ENOMEM
;
2741 xmlChar
*a
= xmlGetProp (root
, (xmlChar
*) "_name");
2745 rc
= GPG_ERR_INV_VALUE
;
2749 if (strv_printf (&path
, "%s", (char *) a
) == 0)
2752 rc
= GPG_ERR_ENOMEM
;
2757 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2758 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2766 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2767 NULL
, NULL
, NULL
, 0, 0, NULL
, 1);
2768 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2783 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
2785 n
= create_element_path (client
, &path
, &rc
, NULL
);
2792 copy
= xmlCopyNodeList (root
->children
);
2793 n
= xmlAddChildList (n
, copy
);
2795 rc
= GPG_ERR_ENOMEM
;
2804 /* Check if the content root element can create a DTD root element. */
2805 if (!xmlStrEqual ((xmlChar
*) "element", root
->name
))
2807 rc
= GPG_ERR_SYNTAX
;
2813 if ((a
= xmlGetProp (root
, (xmlChar
*) "_name")) == NULL
)
2815 rc
= GPG_ERR_SYNTAX
;
2819 char *tmp
= str_dup ((char *) a
);
2821 int literal
= is_literal_element (&tmp
);
2823 if (!valid_xml_element ((xmlChar
*) tmp
) || literal
)
2826 rc
= GPG_ERR_INV_VALUE
;
2830 if (strv_printf (&path
, "%s", tmp
) == 0)
2833 rc
= GPG_ERR_ENOMEM
;
2838 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 1);
2839 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2841 rc
= GPG_ERR_BAD_DATA
;
2845 /* Overwriting the existing tree. */
2849 xmlFreeNodeList (n
);
2853 xmlSetProp (root
, (xmlChar
*) "_name", (xmlChar
*) path
[0]);
2854 n
= xmlCopyNode (root
, 1);
2855 n
= xmlAddChildList (xmlDocGetRootElement (client
->doc
), n
);
2860 rc
= update_element_mtime (client
, n
->parent
);
2862 for (root
= root_orig
->next
; root
; root
= root
->next
)
2864 if (root
->type
== XML_ELEMENT_NODE
)
2877 strv_free (dst_path
);
2883 parse_import_opt_root (void *data
, void *value
)
2885 struct client_s
*client
= data
;
2887 client
->import_root
= str_dup (value
);
2888 return client
->import_root
? 0 : GPG_ERR_ENOMEM
;
2892 import_command (assuan_context_t ctx
, char *line
)
2895 struct client_s
*client
= assuan_get_pointer (ctx
);
2896 unsigned char *result
;
2898 struct argv_s
*args
[] = {
2899 &(struct argv_s
) {"root", OPTION_TYPE_ARG
, parse_import_opt_root
},
2903 xfree (client
->import_root
);
2904 client
->import_root
= NULL
;
2905 rc
= parse_options (&line
, args
, client
);
2907 return send_error (ctx
, rc
);
2909 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2912 xfree (client
->import_root
);
2913 client
->import_root
= NULL
;
2914 return send_error (ctx
, rc
);
2917 rc
= do_import (client
, client
->import_root
, result
);
2918 xfree (client
->import_root
);
2919 client
->import_root
= NULL
;
2920 return send_error (ctx
, rc
);
2924 do_lock (struct client_s
*client
, int add
)
2926 gpg_error_t rc
= lock_file_mutex (client
, add
);
2929 client
->flags
|= FLAG_LOCK_CMD
;
2935 lock_command (assuan_context_t ctx
, char *line
)
2937 struct client_s
*client
= assuan_get_pointer (ctx
);
2938 gpg_error_t rc
= do_lock (client
, 0);
2940 return send_error (ctx
, rc
);
2944 unlock_command (assuan_context_t ctx
, char *line
)
2946 struct client_s
*client
= assuan_get_pointer (ctx
);
2949 rc
= unlock_file_mutex (client
, 0);
2950 return send_error (ctx
, rc
);
2954 option_command (assuan_context_t ctx
, char *line
)
2956 struct client_s
*client
= assuan_get_pointer (ctx
);
2958 char namebuf
[255] = { 0 };
2959 char *name
= namebuf
;
2960 char *value
= NULL
, *p
, *tmp
= NULL
;
2962 p
= strchr (line
, '=');
2964 strncpy (namebuf
, line
, sizeof(namebuf
));
2967 strncpy (namebuf
, line
, strlen (line
)-strlen (p
));
2971 log_write2 ("OPTION name='%s' value='%s'", name
, value
);
2973 if (strcasecmp (name
, (char *) "log_level") == 0)
2979 l
= strtol (value
, NULL
, 10);
2982 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2985 MUTEX_LOCK (&rcfile_mutex
);
2986 config_set_int_param (&global_config
, "global", "log_level", value
);
2987 MUTEX_UNLOCK (&rcfile_mutex
);
2989 else if (strcasecmp (name
, (char *) "lock-timeout") == 0)
2995 n
= strtol (value
, &tmp
, 10);
2997 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3000 client
->lock_timeout
= n
;
3002 else if (strcasecmp (name
, (char *) "NAME") == 0)
3004 if (value
&& strchr (value
, ' '))
3005 rc
= GPG_ERR_INV_VALUE
;
3008 tmp
= pthread_getspecific (thread_name_key
);
3010 MUTEX_LOCK (&cn_mutex
);
3011 xfree (client
->thd
->name
);
3012 client
->thd
->name
= NULL
;
3014 if (!value
|| !*value
)
3015 pthread_setspecific (thread_name_key
, str_dup (""));
3018 client
->thd
->name
= str_dup (value
);
3019 pthread_setspecific (thread_name_key
, str_dup (value
));
3022 MUTEX_UNLOCK (&cn_mutex
);
3025 else if (strcasecmp (name
, "disable-pinentry") == 0)
3029 if (value
&& *value
)
3031 n
= (int) strtol (value
, &tmp
, 10);
3032 if (*tmp
|| n
< 0 || n
> 1)
3033 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3037 client
->flags
|= FLAG_NO_PINENTRY
;
3039 client
->flags
&= ~FLAG_NO_PINENTRY
;
3042 rc
= GPG_ERR_UNKNOWN_OPTION
;
3044 return send_error (ctx
, rc
);
3048 do_rename (assuan_context_t ctx
, char *line
)
3050 struct client_s
*client
= assuan_get_pointer (ctx
);
3052 char **req
, **src
, *dst
;
3055 req
= str_split (line
, " ", 0);
3057 if (!req
|| !req
[0] || !req
[1])
3060 return GPG_ERR_SYNTAX
;
3064 is_literal_element (&dst
);
3066 if (!valid_xml_element ((xmlChar
*) dst
))
3069 return GPG_ERR_INV_VALUE
;
3072 if (strchr (req
[0], '\t'))
3073 src
= str_split (req
[0], "\t", 0);
3075 src
= str_split (req
[0], " ", 0);
3079 rc
= GPG_ERR_SYNTAX
;
3083 n
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3085 n
= find_elements (client
, client
->doc
, n
->children
, src
+ 1, &rc
, NULL
, NULL
,
3086 NULL
, 0, 0, NULL
, 0);
3091 rc
= is_element_owner (client
, n
);
3095 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
3098 rc
= GPG_ERR_ENOMEM
;
3102 /* To prevent unwanted effects:
3104 * <root name="a"><b/></root>
3108 if (xmlStrEqual (a
, (xmlChar
*) dst
))
3111 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3121 for (p
= src
; *p
; p
++)
3125 strv_printf (&tmp
, "%s", *p
);
3129 strv_printf (&tmp
, "!%s", dst
);
3130 ndst
= find_root_element (client
, client
->doc
, &tmp
, &rc
, NULL
, 0, 0);
3131 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3138 ndst
= find_elements (client
, client
->doc
, ndst
->children
, tmp
+ 1, &rc
, NULL
,
3139 NULL
, NULL
, 0, 0, NULL
, 0);
3142 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3147 /* Target may exist:
3150 * <root name="b" target="a"/>
3159 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3165 rc
= is_element_owner (client
, ndst
);
3169 unlink_node (client
, ndst
);
3170 xmlFreeNodeList (ndst
);
3173 rc
= add_attribute (client
, n
, "_name", dst
);
3182 rename_command (assuan_context_t ctx
, char *line
)
3184 struct client_s
*client
= assuan_get_pointer (ctx
);
3186 struct argv_s
*args
[] = {
3187 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3191 rc
= parse_options (&line
, args
, client
);
3193 return send_error (ctx
, rc
);
3195 if (client
->opts
& OPT_INQUIRE
)
3197 unsigned char *result
;
3200 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3202 return send_error (ctx
, rc
);
3204 pthread_cleanup_push (xfree
, result
);
3205 rc
= do_rename (ctx
, (char *)result
);
3206 pthread_cleanup_pop (1);
3209 rc
= do_rename (ctx
, line
);
3211 return send_error (ctx
, rc
);
3215 do_copy (assuan_context_t ctx
, char *line
)
3217 struct client_s
*client
= assuan_get_pointer (ctx
);
3219 char **req
, **src
= NULL
, **dst
= NULL
;
3220 xmlNodePtr nsrc
, ndst
, new = NULL
;
3222 req
= str_split (line
, " ", 0);
3223 if (!req
|| !req
[0] || !req
[1])
3226 return GPG_ERR_SYNTAX
;
3229 if (strchr (req
[0], '\t'))
3230 src
= str_split (req
[0], "\t", 0);
3232 src
= str_split (req
[0], " ", 0);
3236 rc
= GPG_ERR_SYNTAX
;
3240 if (strchr (req
[1], '\t'))
3241 dst
= str_split (req
[1], "\t", 0);
3243 dst
= str_split (req
[1], " ", 0);
3247 rc
= GPG_ERR_SYNTAX
;
3251 if (!valid_element_path (dst
, 0))
3253 rc
= GPG_ERR_INV_VALUE
;
3257 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3259 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3260 NULL
, NULL
, 0, 0, NULL
, 0);
3265 new = xmlCopyNodeList (nsrc
);
3268 rc
= GPG_ERR_ENOMEM
;
3273 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3277 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3278 NULL
, create_target_elements_cb
, 0, 0, NULL
, 0);
3285 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3287 else if (!ndst
|| create
)
3289 ndst
= create_element_path (client
, &dst
, &rc
, NULL
);
3294 rc
= is_element_owner (client
, ndst
);
3298 /* Merge any attributes from the src node to the initial dst node. */
3299 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
)
3301 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3304 xmlAttrPtr a
= xmlHasProp (ndst
, attr
->name
);
3308 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3309 xmlNewProp (ndst
, attr
->name
, tmp
);
3311 rc
= add_attribute (client
, ndst
, NULL
, NULL
);
3314 xmlNodePtr n
= ndst
->children
;
3316 xmlFreeNodeList (n
);
3317 ndst
->children
= NULL
;
3321 n
= xmlCopyNodeList (new->children
);
3324 rc
= GPG_ERR_ENOMEM
;
3328 n
= xmlAddChildList (ndst
, n
);
3331 rc
= GPG_ERR_ENOMEM
;
3335 rc
= update_element_mtime (client
, xmlDocGetRootElement (client
->doc
) ==
3336 ndst
->parent
? ndst
: ndst
->parent
);
3342 xmlUnlinkNode (new);
3343 xmlFreeNodeList (new);
3359 copy_command (assuan_context_t ctx
, char *line
)
3361 struct client_s
*client
= assuan_get_pointer (ctx
);
3363 struct argv_s
*args
[] = {
3364 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3368 rc
= parse_options (&line
, args
, client
);
3370 return send_error (ctx
, rc
);
3372 if (client
->opts
& OPT_INQUIRE
)
3374 unsigned char *result
;
3377 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3379 return send_error (ctx
, rc
);
3381 pthread_cleanup_push (xfree
, result
);
3382 rc
= do_copy (ctx
, (char *)result
);
3383 pthread_cleanup_pop (1);
3386 rc
= do_copy (ctx
, line
);
3388 return send_error (ctx
, rc
);
3392 do_move (assuan_context_t ctx
, char *line
)
3394 struct client_s
*client
= assuan_get_pointer (ctx
);
3396 char **req
, **src
= NULL
, **dst
= NULL
;
3397 xmlNodePtr nsrc
, ndst
= NULL
;
3399 req
= str_split (line
, " ", 0);
3401 if (!req
|| !req
[0] || !req
[1])
3404 return GPG_ERR_SYNTAX
;
3407 if (strchr (req
[0], '\t'))
3408 src
= str_split (req
[0], "\t", 0);
3410 src
= str_split (req
[0], " ", 0);
3414 rc
= GPG_ERR_SYNTAX
;
3418 if (strchr (req
[1], '\t'))
3419 dst
= str_split (req
[1], "\t", 0);
3421 dst
= str_split (req
[1], " ", 0);
3423 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3425 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
,
3426 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
3431 rc
= is_element_owner (client
, nsrc
);
3437 if (!valid_element_path (dst
, 0))
3439 rc
= GPG_ERR_INV_VALUE
;
3443 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3445 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1,
3446 &rc
, NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
3449 ndst
= xmlDocGetRootElement (client
->doc
);
3451 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
)
3455 rc
= GPG_ERR_CONFLICT
;
3460 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3467 xmlChar
*a
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3469 xmlNodePtr dup
= find_element (client
, ndst
->children
, (char *) a
,
3481 if (ndst
== xmlDocGetRootElement (client
->doc
))
3483 xmlNodePtr n
= nsrc
;
3486 while (n
->parent
&& n
->parent
!= ndst
)
3489 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
3490 xmlChar
*b
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3492 if (xmlStrEqual (a
, b
))
3495 xmlUnlinkNode (nsrc
);
3497 xmlFreeNodeList (n
);
3505 xmlUnlinkNode (dup
);
3506 xmlFreeNodeList (dup
);
3510 xmlUnlinkNode (dup
);
3516 xmlChar
*name
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3518 if (nsrc
->parent
== xmlDocGetRootElement (client
->doc
)
3519 && !strcmp ((char *) name
, *dst
))
3522 rc
= GPG_ERR_CONFLICT
;
3527 ndst
= create_element_path (client
, &dst
, &rc
, nsrc
);
3533 update_element_mtime (client
, nsrc
->parent
);
3534 xmlUnlinkNode (nsrc
);
3535 ndst
= xmlAddChildList (ndst
, nsrc
);
3538 rc
= GPG_ERR_ENOMEM
;
3540 update_element_mtime (client
, ndst
->parent
);
3556 move_command (assuan_context_t ctx
, char *line
)
3558 struct client_s
*client
= assuan_get_pointer (ctx
);
3560 struct argv_s
*args
[] = {
3561 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3565 rc
= parse_options (&line
, args
, client
);
3567 return send_error (ctx
, rc
);
3569 if (client
->opts
& OPT_INQUIRE
)
3571 unsigned char *result
;
3574 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3576 return send_error (ctx
, rc
);
3578 pthread_cleanup_push (xfree
, result
);
3579 rc
= do_move (ctx
, (char *)result
);
3580 pthread_cleanup_pop (1);
3583 rc
= do_move (ctx
, line
);
3585 return send_error (ctx
, rc
);
3589 ls_command (assuan_context_t ctx
, char *line
)
3592 char *tmp
= str_asprintf ("%s/data", homedir
);
3593 char *dir
= expand_homedir (tmp
);
3594 DIR *d
= opendir (dir
);
3596 rc
= gpg_error_from_errno (errno
);
3602 return send_error (ctx
, rc
);
3606 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
3607 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
3611 pthread_cleanup_push (xfree
, p
);
3612 pthread_cleanup_push (closedir
, d
);
3615 while (!readdir_r (d
, p
, &cur
) && cur
)
3617 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
3619 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.'
3620 && cur
->d_name
[2] == '\0')
3623 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
3630 rc
= GPG_ERR_ENOMEM
;
3638 pthread_cleanup_pop (1); // closedir (d)
3639 pthread_cleanup_pop (1); // xfree (p)
3642 return send_error (ctx
, rc
);
3645 return send_error (ctx
, GPG_ERR_NO_DATA
);
3647 list
[strlen (list
) - 1] = 0;
3648 pthread_cleanup_push (xfree
, list
);
3649 rc
= xfer_data (ctx
, list
, strlen (list
));
3650 pthread_cleanup_pop (1);
3651 return send_error (ctx
, rc
);
3655 bye_notify (assuan_context_t ctx
, char *line
)
3657 struct client_s
*cl
= assuan_get_pointer (ctx
);
3659 cl
->thd
->state
= CLIENT_STATE_DISCON
;
3662 if (cl
->thd
->remote
)
3668 struct timeval tv
= { 0, 50000 };
3670 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
3671 if (rc
== GNUTLS_E_AGAIN
)
3672 select (0, NULL
, NULL
, NULL
, &tv
);
3674 while (rc
== GNUTLS_E_AGAIN
);
3678 /* This will let assuan_process_next() return. */
3679 fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
);
3680 cl
->last_rc
= 0; // BYE command result
3685 reset_notify (assuan_context_t ctx
, char *line
)
3687 struct client_s
*client
= assuan_get_pointer (ctx
);
3690 cleanup_client (client
);
3696 * This is called before every Assuan command.
3699 command_startup (assuan_context_t ctx
, const char *name
)
3701 struct client_s
*client
= assuan_get_pointer (ctx
);
3703 struct command_table_s
*cmd
= NULL
;
3705 log_write2 ("command='%s'", name
);
3706 client
->last_rc
= client
->opts
= 0;
3708 for (int i
= 0; command_table
[i
]; i
++)
3710 if (!strcasecmp (name
, command_table
[i
]->name
))
3712 if (command_table
[i
]->ignore_startup
)
3714 cmd
= command_table
[i
];
3719 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
3721 update_client_state (client
, CLIENT_STATE_COMMAND
);
3727 * This is called after every Assuan command.
3730 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
3732 struct client_s
*client
= assuan_get_pointer (ctx
);
3734 if (!(client
->flags
& FLAG_LOCK_CMD
))
3735 unlock_file_mutex (client
, 0);
3737 log_write2 (_("command completed: rc=%u"), rc
? rc
: client
->last_rc
);
3738 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
3740 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
3742 update_client_state (client
, CLIENT_STATE_IDLE
);
3746 help_command (assuan_context_t ctx
, char *line
)
3751 if (!line
|| !*line
)
3754 char *help
= str_dup (_("Usage: HELP [<COMMAND>]\n"
3755 "For commands that take an element path as an argument, each element is "
3756 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3759 for (i
= 0; command_table
[i
]; i
++)
3761 if (!command_table
[i
]->help
)
3764 tmp
= str_asprintf ("%s %s", help
, command_table
[i
]->name
);
3769 tmp
= strip_texi_and_wrap (help
);
3771 pthread_cleanup_push (xfree
, tmp
);
3772 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3773 pthread_cleanup_pop (1);
3774 return send_error (ctx
, rc
);
3777 for (i
= 0; command_table
[i
]; i
++)
3779 if (!strcasecmp (line
, command_table
[i
]->name
))
3783 if (!command_table
[i
]->help
)
3786 help
= strip_texi_and_wrap (command_table
[i
]->help
);
3787 tmp
= str_asprintf (_("Usage: %s"), help
);
3789 pthread_cleanup_push (xfree
, tmp
);
3790 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3791 pthread_cleanup_pop (1);
3792 return send_error (ctx
, rc
);
3796 return send_error (ctx
, GPG_ERR_INV_NAME
);
3800 new_command (const char *name
, int ignore
, int unlock
,
3801 gpg_error_t (*handler
) (assuan_context_t
, char *),
3807 for (i
= 0; command_table
[i
]; i
++);
3810 xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
3811 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
3812 command_table
[i
]->name
= name
;
3813 command_table
[i
]->handler
= handler
;
3814 command_table
[i
]->ignore_startup
= ignore
;
3815 command_table
[i
]->unlock
= unlock
;
3816 command_table
[i
++]->help
= help
;
3817 command_table
[i
] = NULL
;
3825 for (i
= 0; command_table
[i
]; i
++)
3826 xfree (command_table
[i
]);
3828 xfree (command_table
);
3832 sort_commands (const void *arg1
, const void *arg2
)
3834 struct command_table_s
*const *a
= arg1
;
3835 struct command_table_s
*const *b
= arg2
;
3844 return strcmp ((*a
)->name
, (*b
)->name
);
3847 // FIXME cleanup/implement options
3849 passwd_command (assuan_context_t ctx
, char *line
)
3851 struct client_s
*client
= assuan_get_pointer (ctx
);
3853 struct argv_s
*args
[] = {
3854 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
, parse_opt_no_passphrase
},
3858 rc
= peer_is_invoker (client
);
3859 if (rc
== GPG_ERR_EACCES
)
3860 return send_error (ctx
, rc
);
3862 if (client
->flags
& FLAG_NEW
)
3863 return send_error (ctx
, GPG_ERR_INV_STATE
);
3865 rc
= parse_options (&line
, args
, client
);
3867 return send_error (ctx
, rc
);
3869 rc
= crypto_init_ctx (client
->crypto
, client
->flags
& FLAG_NO_PINENTRY
);
3871 return send_error (ctx
, rc
);
3873 if (!rc
&& client
->opts
& OPT_RESET
)
3875 rc
= cache_clear (client
->md5file
);
3877 send_status_all (STATUS_CACHE
, NULL
);
3881 rc
= crypto_passwd (client
, client
->crypto
);
3883 crypto_cleanup_non_keys (client
->crypto
);
3884 return send_error (ctx
, rc
);
3888 parse_opt_data (void *data
, void *value
)
3890 struct client_s
*client
= data
;
3893 client
->opts
|= OPT_DATA
;
3898 send_client_list (assuan_context_t ctx
)
3900 struct client_s
*client
= assuan_get_pointer (ctx
);
3902 char buf
[ASSUAN_LINELENGTH
];
3904 if (client
->opts
& OPT_VERBOSE
)
3909 int with_state
= config_get_boolean ("global", "send_state");
3911 MUTEX_LOCK (&cn_mutex
);
3912 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
3913 t
= slist_length (cn_thread_list
);
3915 for (i
= 0; i
< t
; i
++)
3917 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
3918 pthread_t self
= pthread_self ();
3920 if (!strv_printf (&list
, "%p %s %s %s %u %u %u",
3922 thd
->name
? thd
->name
: "-",
3923 thd
->cl
&& thd
->cl
->filename
3924 ? thd
->cl
->filename
: "/",
3926 thd
->remote
? thd
->peeraddr
: "-",
3930 thd
->cl
&& thd
->cl
->flags
& FLAG_HAS_LOCK
? 1 : 0,
3931 self
== thd
->tid
? 1 : 0,
3932 with_state
? thd
->state
: CLIENT_STATE_UNKNOWN
3936 rc
= GPG_ERR_ENOMEM
;
3941 pthread_cleanup_pop (1);
3945 line
= strv_join ("\n", list
);
3947 pthread_cleanup_push (xfree
, line
);
3948 rc
= xfer_data (ctx
, line
, strlen (line
));
3949 pthread_cleanup_pop (1);
3953 if (client
->opts
& OPT_DATA
)
3955 MUTEX_LOCK (&cn_mutex
);
3956 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
3957 snprintf (buf
, sizeof (buf
), "%i", slist_length (cn_thread_list
));
3958 pthread_cleanup_pop (1);
3959 rc
= xfer_data (ctx
, buf
, strlen (buf
));
3962 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
3968 getinfo_command (assuan_context_t ctx
, char *line
)
3970 struct client_s
*client
= assuan_get_pointer (ctx
);
3972 char buf
[ASSUAN_LINELENGTH
];
3973 struct argv_s
*args
[] = {
3974 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
3975 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
3979 rc
= parse_options (&line
, args
, client
);
3981 return send_error (ctx
, rc
);
3983 if (!strcasecmp (line
, "clients"))
3985 rc
= send_client_list (ctx
);
3987 else if (!strcasecmp (line
, "cache"))
3989 if (client
->opts
& OPT_DATA
)
3991 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
3992 rc
= xfer_data (ctx
, buf
, strlen (buf
));
3995 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
3997 else if (!strcasecmp (line
, "pid"))
4000 pid_t pid
= getpid ();
4002 snprintf (buf
, sizeof (buf
), "%i", pid
);
4003 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4005 else if (!strcasecmp (line
, "version"))
4007 char *buf
= str_asprintf ("0x%06x %s", VERSION_HEX
,
4012 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4015 else if (!strcasecmp (line
, "last_error"))
4017 if (client
->last_error
)
4018 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4020 rc
= GPG_ERR_NO_DATA
;
4022 else if (!strcasecmp (line
, "user"))
4027 if (client
->thd
->remote
)
4028 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
4030 user
= get_username (client
->thd
->peer
->uid
);
4032 user
= get_username (client
->thd
->peer
->uid
);
4036 pthread_cleanup_push (xfree
, user
);
4037 rc
= xfer_data (ctx
, user
, strlen (user
));
4038 pthread_cleanup_pop (1);
4041 rc
= GPG_ERR_NO_DATA
;
4044 rc
= gpg_error (GPG_ERR_SYNTAX
);
4046 return send_error (ctx
, rc
);
4050 parse_listkeys_opt_secret_only (void *data
, void *value
)
4052 struct client_s
*client
= data
;
4055 client
->opts
|= OPT_SECRET_ONLY
;
4060 keyinfo_command (assuan_context_t ctx
, char *line
)
4062 struct client_s
*client
= assuan_get_pointer (ctx
);
4064 char **keys
= NULL
, **p
;
4066 if (!(client
->flags
& FLAG_OPEN
))
4067 return send_error (ctx
, GPG_ERR_INV_STATE
);
4069 if (client
->flags
& FLAG_NEW
)
4070 return send_error (ctx
, GPG_ERR_NO_DATA
);
4072 p
= strv_catv(keys
, client
->crypto
->pubkey
);
4076 for (p
= client
->crypto
->sigkey
; p
&& *p
; p
++)
4078 if (!strv_printf(&keys
, "S%s", *p
))
4081 return send_error (ctx
, GPG_ERR_ENOMEM
);
4086 rc
= GPG_ERR_ENOMEM
;
4090 line
= strv_join ("\n", keys
);
4092 pthread_cleanup_push (xfree
, line
);
4094 rc
= xfer_data (ctx
, line
, strlen (line
));
4096 rc
= GPG_ERR_ENOMEM
;
4098 pthread_cleanup_pop (1);
4103 return send_error (ctx
, rc
);
4107 kill_command (assuan_context_t ctx
, char *line
)
4109 #ifdef HAVE_PTHREAD_CANCEL
4110 struct client_s
*client
= assuan_get_pointer (ctx
);
4113 if (!line
|| !*line
)
4114 return send_error (ctx
, GPG_ERR_SYNTAX
);
4116 rc
= peer_is_invoker (client
);
4121 MUTEX_LOCK (&cn_mutex
);
4122 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
4123 t
= slist_length (cn_thread_list
);
4126 for (i
= 0; i
< t
; i
++)
4128 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4129 char *tmp
= str_asprintf ("%p", thd
->tid
);
4131 if (strcmp (line
, tmp
))
4138 rc
= pthread_cancel (thd
->tid
);
4142 pthread_cleanup_pop (1);
4145 return send_error (ctx
, rc
);
4147 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
4152 listkeys_command (assuan_context_t ctx
, char *line
)
4154 struct client_s
*client
= assuan_get_pointer (ctx
);
4155 struct crypto_s
*crypto
= NULL
;
4156 char **pattern
= NULL
;
4157 gpgme_key_t
*keys
= NULL
;
4158 char **result
= NULL
;
4160 struct argv_s
*args
[] = {
4161 &(struct argv_s
) {"secret-only", OPTION_TYPE_NOARG
,
4162 parse_listkeys_opt_secret_only
},
4166 rc
= peer_is_invoker(client
);
4168 return send_error (ctx
, rc
);
4170 rc
= parse_options (&line
, args
, client
);
4172 return send_error (ctx
, rc
);
4174 rc
= crypto_init (&crypto
, client
->ctx
, client
->filename
,
4175 client
->flags
& FLAG_NO_PINENTRY
);
4177 return send_error (ctx
, rc
);
4179 pthread_cleanup_push (crypto_cleanup
, crypto
);
4180 pattern
= str_split (line
, ",", 0);
4181 pthread_cleanup_push (strv_free
, pattern
);
4182 rc
= crypto_list_keys (crypto
, pattern
, client
->opts
& OPT_SECRET_ONLY
,
4184 pthread_cleanup_pop (1);
4185 pthread_cleanup_pop (1);
4190 for (i
= 0; keys
[i
]; i
++)
4192 char *p
= crypto_key_info (keys
[i
]);
4197 rc
= GPG_ERR_ENOMEM
;
4201 r
= strv_cat (result
, p
);
4204 rc
= GPG_ERR_ENOMEM
;
4212 rc
= GPG_ERR_NO_DATA
;
4214 crypto_free_key_list (keys
);
4219 line
= strv_join ("\n", result
);
4221 pthread_cleanup_push (xfree
, line
);
4223 rc
= GPG_ERR_ENOMEM
;
4225 rc
= xfer_data (ctx
, line
, strlen (line
));
4227 pthread_cleanup_pop (1);
4232 return send_error (ctx
, rc
);
4238 /* !BEGIN-HELP-TEXT!
4240 * This comment is used as a marker to generate the offline documentation
4241 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4242 * script to determine where commands begin and end.
4244 new_command("HELP", 1, 1, help_command
, _(
4245 "HELP [<COMMAND>]\n"
4246 "Show available commands or command specific help text."
4249 new_command("KILL", 1, 0, kill_command
, _(
4250 "KILL <thread_id>\n"
4251 "Terminates the client identified by @var{thread_id} and releases any file "
4252 "lock or other resources it has held. @xref{GETINFO} for details about listing "
4253 "connected clients.\n"
4256 new_command("LISTKEYS", 1, 0, listkeys_command
, _(
4257 "LISTKEYS [--secret-only] [pattern[,<pattern>]]\n"
4258 "Returns a new line separated list of key information matching a comma "
4259 "separated list of @var{pattern}'s. When option @option{--secret-only} is "
4260 "specified, only keys matching @var{pattern} that also have a secret "
4261 "available key will be returned."
4264 new_command("KEYINFO", 1, 1, keyinfo_command
, _(
4266 "Returns a new line separated list of key ID's that the currently opened "
4267 "data file has recipients and signers for. If the key is a signing key it "
4268 "will be prefixed with an @key{S}. If the file is a new one the error code "
4269 "GPG_ERR_NO_DATA is returned."
4272 new_command("GETINFO", 1, 1, getinfo_command
, _(
4273 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4274 "Get server and other information: @var{CACHE} returns the number of cached "
4275 "documents via a status message. @var{CLIENTS} returns the number of "
4276 "connected clients via a status message or a list of connected clients when "
4277 "the @option{--verbose} parameter is used. The list contains space delimited "
4278 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4279 "file lock status, whether the current client is self and a client state. "
4280 "Client state @code{1} indicates that the client is idle, @code{2} means the "
4281 "client is in a command and @code{3} means the client is disconnecting. This "
4282 "line is always returned with a data response. @var{PID} returns the process "
4283 "ID number of the server via a data response. @var{VERSION} returns the server "
4284 "version number and compile-time features with a data response with each "
4285 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4286 "the last failed command when available. @var{USER} returns the username or "
4287 "@abbr{TLS} hash of the connected client. @xref{Status Messages}. "
4289 "When the @option{--data} option is specified then the result will be sent "
4290 "via a data response rather than a status message."
4293 new_command("PASSWD", 0, 0, passwd_command
, _(
4295 "Changes the passphrase of the secret key required to open the current "
4298 "This command is not available for non-invoking clients "
4299 "(@pxref{Access Control})."
4302 new_command("OPEN", 1, 1, open_command
, _(
4303 "OPEN [--lock] <filename>\n"
4304 "Opens @var{filename}. When the @var{filename} is not found on the "
4305 "file-system then a new document will be created. If the file is found, it "
4306 "is looked for in the file cache and when found no passphrase will be "
4307 "required to open it. When not cached, @cite{pinentry(1)} will be used to "
4308 "retrieve the passphrase for decryption unless @option{disable-pinentry} "
4309 "(@pxref{OPTION}) was specified in which case @command{pwmd} will "
4310 "@emph{INQUIRE} the client for the passphrase."
4312 "When the @option{--lock} option is passed then the file mutex will be "
4313 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4314 "file had been opened."
4317 new_command("SAVE", 0, 0, save_command
, _(
4318 "SAVE [--inquire-keyparam] [--keyid=<keyid>[,...] | [--inquire-keyid]] [--sign-keyid=<fingerprint>[,...] | [--inquire-sign-keyid]]\n"
4319 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4320 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4321 "new one or the option @option{--inquire-keyparam} was passed, a new keypair "
4322 "will be generated and @cite{pinentry(1)} will be used to prompt for the "
4323 "passphrase to encrypt with."
4325 "The @option{--inquire-keyparam} option will send an "
4326 "@emph{INQUIRE} to the client to obtain the key parameters to use for "
4327 "generating the new keypair. The inquired data is expected to be in "
4328 "@cite{gnupg} @abbr{XML} format. See the @cite{gnupg} documentation for "
4329 "details. Note that when this option is specified a new keypair will be "
4330 "generated reguardless if the file is a new one and that the passphrase for "
4331 "the current file will be required before generating the new keypair. This "
4332 "option is not available for non-invoking clients (@pxref{Access Control})."
4334 "You can encrypt the data file to a recipient other than the one that it "
4335 "was encrypted with by passing the @option{--keyid} or "
4336 "@option{--inquire-keyid} option with "
4337 "the key ID of a public encryption key as its argument. Use the "
4338 "@command{LISTKEYS} command (@pxref{LISTKEYS}) to show key information by "
4339 "pattern. The @option{--sign-keyid} or @option{--inquire-sign-keyid} option "
4340 "may also be used to sign the data "
4341 "file with an alternate key by specifying the fingerprint of a secret key. "
4342 "A passphrase to decrypt the data file "
4343 "will be required if one or more of the original encryption or signing keys "
4344 "are not found in either of these two options' arguments. The original "
4345 "encryption or signing keys will be used when either of these options are "
4348 "Note that the @option{--keyid} option requires a key ID and not a "
4349 "fingerprint and the @option{--sign-keyid} option requires a fingerprint do "
4350 "to the " "way recipients and signers are obtained when @command{OPEN}'ing a "
4351 "file. These options are not available for non-invoking clients "
4352 "(@pxref{Access Control}) when the recipients or signers do not match those "
4353 "that were used when the file was @code{OPEN}'ed."
4356 new_command("ISCACHED", 1, 0, iscached_command
, _(
4357 "ISCACHED [--lock] <filename>\n"
4358 "An @emph{OK} response is returned if the specified @var{filename} is found "
4359 "in the file cache. If not found in the cache but exists on the filesystem "
4360 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4363 "The @option{--lock} option will lock the file mutex of @var{filename} when "
4364 "the file exists; it does not need to be opened nor cached. The lock will be "
4365 "released when the client exits or sends the @code{UNLOCK} command "
4369 new_command("CLEARCACHE", 1, 1, clearcache_command
, _(
4370 "CLEARCACHE [<filename>]\n"
4371 "Clears a file cache entry for all or the specified @var{filename}."
4374 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command
, _(
4375 "CACHETIMEOUT <filename> <seconds>\n"
4376 "The time in @var{seconds} until @var{filename} will be removed from the "
4377 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4378 "the passphrase for each @code{OPEN} command (@pxref{OPEN}) or @code{SAVE} "
4379 "(@pxref{SAVE}) command. @xref{Configuration}, and the @code{cache_timeout} "
4383 new_command("LIST", 0, 1, list_command
, _(
4384 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4385 "If no element path is given then a newline separated list of root elements "
4386 "is returned with a data response. If given, then all reachable elements "
4387 "of the specified element path are returned unless the @option{--no-recurse} "
4388 "option is specified. If specified, only the child elements of the element "
4389 "path are returned without recursing into grandchildren. Each resulting "
4390 "element is prefixed with the literal @code{!} character when the element "
4391 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4393 "When the @option{--verbose} option is passed then each element path "
4394 "returned will have zero or more flags appened to it. These flags are "
4395 "delimited from the element path by a single space character. A flag itself "
4396 "is a single character. Flag @code{P} indicates that access to the element "
4397 "is denied. Flag @code{+} indicates that there are child nodes of "
4398 "the current element path. Flag @code{E} indicates that an element of an "
4399 "element path contained in a @var{target} attribute could not be found. Flag "
4400 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4401 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4402 "of the @var{target} attribute contained in the current element (see below)."
4404 "The @option{--with-target} option implies @option{--verbose} and will append "
4405 "an additional flag @code{T} followed by a single space then an element path. "
4406 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4407 "current element when it contains a @var{target} attribute. When no "
4408 "@var{target} attribute is found then no @code{T} flag will be appended."
4410 "The @option{--no-recurse} option limits the amount of data returned to only "
4411 "the listing of children of the specified element path and not any "
4414 "The @option{--all} option lists the entire element tree for each root "
4415 "element. This option also implies option @option{--verbose}."
4417 "When the @option{--inquire} option is passed then all remaining non-option "
4418 "arguments are retrieved via a server @emph{INQUIRE}."
4421 new_command("REALPATH", 0, 1, realpath_command
, _(
4422 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4423 "Resolves all @code{target} attributes of the specified element path and "
4424 "returns the result with a data response. @xref{Target Attribute}, for details."
4426 "When the @option{--inquire} option is passed then all remaining non-option "
4427 "arguments are retrieved via a server @emph{INQUIRE}."
4430 new_command("STORE", 0, 1, store_command
, _(
4431 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4432 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4434 "Creates a new element path or modifies the @var{content} of an existing "
4435 "element. If only a single element is specified then a new root element is "
4436 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4437 "set to the final @key{TAB} delimited element. If no @var{content} is "
4438 "specified after the final @key{TAB}, then the content of an existing "
4439 "element will be removed; or empty if creating a new element."
4441 "The only restriction of an element name is that it not contain whitespace "
4442 "or begin with the literal element character @code{!} unless specifying a "
4443 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4444 "the @key{TAB} delimited elements. It is recommended that the content of an "
4445 "element be base64 encoded when it contains control or @key{TAB} characters "
4446 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
4449 new_command("RENAME", 0, 1, rename_command
, _(
4450 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4451 "Renames the specified @var{element} to the new @var{value}. If an element of "
4452 "the same name as the @var{value} already exists it will be overwritten."
4454 "When the @option{--inquire} option is passed then all remaining non-option "
4455 "arguments are retrieved via a server @emph{INQUIRE}."
4458 new_command("COPY", 0, 1, copy_command
, _(
4459 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4460 "Copies the entire element tree starting from the child node of the source "
4461 "element, to the destination element path. If the destination element path "
4462 "does not exist then it will be created; otherwise it is overwritten."
4464 "Note that attributes from the source element are merged into the "
4465 "destination element when the destination element path exists. When an "
4466 "attribute of the same name exists in both the source and destination "
4467 "elements then the destination attribute will be updated to the source "
4470 "When the @option{--inquire} option is passed then all remaining non-option "
4471 "arguments are retrieved via a server @emph{INQUIRE}."
4474 new_command("MOVE", 0, 1, move_command
, _(
4475 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4476 "Moves the source element path to the destination element path. If the "
4477 "destination is not specified then it will be moved to the root node of the "
4478 "document. If the destination is specified and exists then it will be "
4479 "overwritten; otherwise non-existing elements of the destination element "
4480 "path will be created."
4482 "When the @option{--inquire} option is passed then all remaining non-option "
4483 "arguments are retrieved via a server @emph{INQUIRE}."
4486 new_command("DELETE", 0, 1, delete_command
, _(
4487 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4488 "Removes the specified element path and all of its children. This may break "
4489 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4490 "refers to this element or any of its children."
4492 "When the @option{--inquire} option is passed then all remaining non-option "
4493 "arguments are retrieved via a server @emph{INQUIRE}."
4496 new_command("GET", 0, 1, get_command
, _(
4497 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4498 "Retrieves the content of the specified element. The content is returned "
4499 "with a data response."
4501 "When the @option{--inquire} option is passed then all remaining non-option "
4502 "arguments are retrieved via a server @emph{INQUIRE}."
4505 new_command("ATTR", 0, 1, attr_command
, _(
4506 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4508 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4510 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4511 "element. When no @var{value} is specified any existing value will be removed."
4513 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4515 " Removes an @var{attribute} from an element."
4517 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4519 " Retrieves a newline separated list of attributes names and values "
4520 "from the specified element. Each attribute name and value is space delimited."
4522 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4524 " Retrieves the value of an @var{attribute} from an element."
4527 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4528 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4531 "The @code{_mtime} attribute is updated each time an element is modified by "
4532 "either storing content, editing attributes or by deleting a child element. "
4533 "The @code{_ctime} attribute is created for each new element in an element "
4536 "When the @option{--inquire} option is passed then all remaining non-option "
4537 "arguments are retrieved via a server @emph{INQUIRE}."
4539 "@xref{Target Attribute}, for details about this special attribute."
4542 new_command("XPATH", 0, 1, xpath_command
, _(
4543 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4544 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4545 "specified it is assumed the expression is a request to return a result. "
4546 "Otherwise, the result is set to the @var{value} argument and the document is "
4547 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4548 "is assumed to be empty and the document is updated. For example:"
4551 "XPATH //element[@@_name='password']@key{TAB}\n"
4554 "would clear the content of all @code{password} elements in the data file "
4555 "while leaving off the trailing @key{TAB} would return all @code{password} "
4556 "elements in @abbr{XML} format."
4558 "When the @option{--inquire} option is passed then all remaining non-option "
4559 "arguments are retrieved via a server @emph{INQUIRE}."
4561 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4562 "expression syntax."
4565 new_command("XPATHATTR", 0, 1, xpathattr_command
, _(
4566 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4567 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4568 "attributes and does not return a result. For the @var{SET} operation the "
4569 "@var{value} is optional but the field is required. If not specified then "
4570 "the attribute value will be empty. For example:"
4573 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4576 "would create an @code{password} attribute for each @code{password} element "
4577 "found in the document. The attribute value will be empty but still exist."
4579 "When the @option{--inquire} option is passed then all remaining non-option "
4580 "arguments are retrieved via a server @emph{INQUIRE}."
4582 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4583 "expression syntax."
4586 new_command("IMPORT", 0, 1, import_command
, _(
4587 "IMPORT [--root [!]element[<TAB>[!]child[..]]] <content>\n"
4588 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4590 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4591 "argument is raw @abbr{XML} data. The content is created as a child of "
4592 "the element path specified with the @option{--root} option or at the "
4593 "document root when not specified. Existing elements of the same name will "
4596 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4600 new_command("DUMP", 0, 1, dump_command
, _(
4602 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4603 "dumping a specific node."
4606 new_command("LOCK", 0, 0, lock_command
, _(
4608 "Locks the mutex associated with the opened file. This prevents other clients "
4609 "from sending commands to the same opened file until the client "
4610 "that sent this command either disconnects or sends the @code{UNLOCK} "
4611 "command. @xref{UNLOCK}."
4614 new_command("UNLOCK", 1, 0, unlock_command
, _(
4616 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4617 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4618 "@pxref{ISCACHED})."
4621 new_command("GETCONFIG", 1, 1, getconfig_command
, _(
4622 "GETCONFIG [filename] <parameter>\n"
4623 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4624 "data response. If no file has been opened then the value for @var{filename} "
4625 "or the default from the @samp{global} section will be returned. If a file "
4626 "has been opened and no @var{filename} is specified, a value previously "
4627 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4630 new_command("OPTION", 1, 1, option_command
, _(
4631 "OPTION <NAME>=<VALUE>\n"
4632 "Sets a client option @var{name} to @var{value}. The value for an option is "
4633 "kept for the duration of the connection."
4636 "@item DISABLE-PINENTRY\n"
4637 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
4638 "server inquire is sent to the client to obtain the passphrase. This option "
4639 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4640 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
4643 "Associates the thread ID of the connection with the specified textual "
4644 "representation. Useful for debugging log messages. May not contain whitespace."
4646 "@item LOCK-TIMEOUT\n"
4647 "When not @code{0}, the duration in tenths of a second to wait for the file "
4648 "mutex which has been locked by another thread to be released before returning "
4649 "an error. When @code{-1}, then an error will be returned immediately."
4653 new_command("LS", 1, 1, ls_command
, _(
4655 "Returns a newline separated list of data files stored in the data directory "
4656 "@file{HOMEDIR/data} (@pxref{Invoking}) with a data response."
4659 new_command("RESET", 1, 1, NULL
, _(
4661 "Closes the currently opened file but keeps any previously set client options."
4664 new_command("NOP", 1, 1, NULL
, _(
4666 "Does nothing. Always returns successfully."
4669 /* !END-HELP-TEXT! */
4670 new_command ("CANCEL", 1, 1, NULL
, NULL
);
4671 new_command ("END", 1, 1, NULL
, NULL
);
4672 new_command ("BYE", 1, 1, NULL
, NULL
);
4675 for (i
= 0; command_table
[i
]; i
++);
4676 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
4681 register_commands (assuan_context_t ctx
)
4685 for (; command_table
[i
]; i
++)
4687 if (!command_table
[i
]->handler
)
4690 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
4691 command_table
[i
]->handler
,
4692 command_table
[i
]->help
);
4697 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
4701 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
4705 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
4709 return assuan_register_post_cmd_notify (ctx
, command_finalize
);