Add KILL command and GETINFO --verbose CLIENTS.
[libpwmd.git] / src / commands.c
blob582986c970185ce02ca515f46afcc90dd5e2ed62
1 /*
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/>.
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <err.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <ctype.h>
33 #include <dirent.h>
34 #include <pthread.h>
35 #include <stdint.h>
37 #include "pwmd-error.h"
38 #include <gcrypt.h>
40 #include "mem.h"
41 #include "xml.h"
42 #include "util-misc.h"
43 #include "common.h"
44 #include "rcfile.h"
45 #include "cache.h"
46 #include "commands.h"
47 #include "mutex.h"
48 #include "crypto.h"
49 #include "pinentry.h"
51 /* Flags needed to be retained for a client across commands. */
52 #define FLAG_NEW 0x0001
53 #define FLAG_HAS_LOCK 0x0002
54 #define FLAG_LOCK_CMD 0x0004
55 #define FLAG_NO_PINENTRY 0x0008
56 #define FLAG_OPEN 0x0010
57 #define FLAG_KEEP_LOCK 0x0020
59 /* These are command option flags. */
60 #define OPT_INQUIRE 0x0001
61 #define OPT_NO_PASSPHRASE 0x0002
62 #define OPT_RESET 0x0004
63 #define OPT_LIST_RECURSE 0x0008
64 #define OPT_VERBOSE 0x0010
65 #define OPT_LOCK 0x0020
66 #define OPT_LOCK_ON_OPEN 0x0040
67 #define OPT_SIGN 0x0080
68 #define OPT_LIST_ALL 0x0100
69 #define OPT_LIST_WITH_TARGET 0x0200
70 #define OPT_DATA 0x0400
71 #define OPT_NO_AGENT 0x0800
73 #ifdef WITH_AGENT
74 /* The GETCONFIG command, for example, may fail because pwmd lost the
75 * gpg-agent connection. Update the recovered agent ctx. */
76 #define UPDATE_AGENT_CTX(client, crypto) do { \
77 if (crypto && crypto->agent && crypto->agent->did_restart \
78 && client->crypto && client->crypto->agent \
79 && client->crypto->agent->ctx && \
80 crypto->agent->ctx != client->crypto->agent->ctx) \
81 { \
82 client->crypto->agent->ctx = crypto->agent->ctx; \
83 crypto->agent->ctx = NULL; \
84 } \
85 } while (0)
86 #else
87 #define UPDATE_AGENT_CTX(client, crypto)
88 #endif
90 struct command_table_s
92 const char *name;
93 gpg_error_t (*handler) (assuan_context_t, char *line);
94 const char *help;
95 int ignore_startup;
96 int unlock; // unlock the file mutex after validating the checksum
99 static struct command_table_s **command_table;
101 static gpg_error_t do_lock (struct client_s *client, int add);
102 static gpg_error_t validate_checksum (struct client_s *,
103 struct cache_data_s *);
104 static gpg_error_t update_checksum (struct client_s *client);
106 static gpg_error_t
107 unlock_file_mutex (struct client_s *client, int remove)
109 gpg_error_t rc = 0;
111 // OPEN: keep the lock for the same file being reopened.
112 if (client->flags & FLAG_KEEP_LOCK && client->flags & FLAG_HAS_LOCK)
113 return 0;
115 if (!(client->flags & FLAG_HAS_LOCK))
116 return GPG_ERR_NOT_LOCKED;
118 rc = cache_unlock_mutex (client->md5file, remove);
119 if (rc)
120 rc = GPG_ERR_INV_STATE;
121 else
122 client->flags &= ~(FLAG_HAS_LOCK | FLAG_LOCK_CMD);
124 return rc;
127 static gpg_error_t
128 lock_file_mutex (struct client_s *client, int add)
130 gpg_error_t rc = 0;
131 int timeout = config_get_integer (client->filename, "cache_timeout");
133 if (client->flags & FLAG_HAS_LOCK)
134 return 0;
136 rc = cache_lock_mutex (client->ctx, client->md5file,
137 client->lock_timeout, add, timeout);
138 if (!rc)
139 client->flags |= FLAG_HAS_LOCK;
141 return rc;
144 static gpg_error_t
145 file_modified (struct client_s *client, struct command_table_s *cmd)
147 gpg_error_t rc = 0;
149 if (!(client->flags & FLAG_OPEN))
150 return GPG_ERR_INV_STATE;
152 rc = lock_file_mutex (client, 0);
153 if (!rc || rc == GPG_ERR_NO_DATA)
155 rc = validate_checksum (client, NULL);
156 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
157 rc = 0;
158 else if (rc)
159 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
162 if ((cmd->unlock && !(client->flags & FLAG_HAS_LOCK)) || rc)
163 unlock_file_mutex (client, 0);
165 return rc;
168 static gpg_error_t
169 parse_xml (assuan_context_t ctx, int new)
171 struct client_s *client = assuan_get_pointer (ctx);
172 int cached = client->doc != NULL;
173 gpg_error_t rc = 0;
175 if (new)
177 client->doc = new_document ();
178 if (client->doc)
180 xmlChar *result;
181 int len;
183 xmlDocDumpFormatMemory (client->doc, &result, &len, 0);
184 client->crypto->plaintext = result;
185 client->crypto->plaintext_len = len;
186 if (!client->crypto->plaintext)
188 xmlFreeDoc (client->doc);
189 client->doc = NULL;
193 else if (!cached)
194 rc = parse_doc ((char *) client->crypto->plaintext,
195 client->crypto->plaintext_len, (xmlDocPtr *)&client->doc);
197 return rc;
200 static void
201 free_client (struct client_s *client)
203 if (client->doc)
204 xmlFreeDoc (client->doc);
206 xfree (client->crc);
207 xfree (client->filename);
208 xfree (client->last_error);
210 if (client->crypto)
212 cleanup_crypto_stage2 (client->crypto);
213 if (client->crypto->pkey_sexp)
214 gcry_sexp_release (client->crypto->pkey_sexp);
216 if (client->crypto->sigpkey_sexp)
217 gcry_sexp_release (client->crypto->sigpkey_sexp);
219 client->crypto->pkey_sexp = NULL;
220 client->crypto->sigpkey_sexp = NULL;
221 memset (client->crypto->sign_grip, 0,
222 sizeof (client->crypto->sign_grip));
223 memset (client->crypto->grip, 0, sizeof(client->crypto->grip));
227 void
228 cleanup_client (struct client_s *client)
230 assuan_context_t ctx = client->ctx;
231 struct client_thread_s *thd = client->thd;
232 struct crypto_s *crypto = client->crypto;
233 long lock_timeout = client->lock_timeout;
234 int no_pinentry = (client->flags & FLAG_NO_PINENTRY);
235 struct pinentry_option_s pin_opts;
236 xmlErrorPtr xml_error = client->xml_error;
237 #ifdef WITH_AGENT
238 struct pinentry_option_s agent_pin_opts;
240 if (crypto && crypto->agent)
241 memcpy (&agent_pin_opts, &crypto->agent->pinentry_opts,
242 sizeof(struct pinentry_option_s));
243 #endif
245 memcpy (&pin_opts, &client->pinentry_opts, sizeof(struct pinentry_option_s));
246 unlock_file_mutex (client, client->flags & FLAG_NEW);
247 free_client (client);
248 memset (client, 0, sizeof (struct client_s));
249 client->xml_error = xml_error;
250 client->crypto = crypto;
251 client->ctx = ctx;
252 client->thd = thd;
253 client->lock_timeout = lock_timeout;
254 memcpy (&client->pinentry_opts, &pin_opts, sizeof(struct pinentry_option_s));
255 client->flags |= no_pinentry ? FLAG_NO_PINENTRY : 0;
256 #ifdef WITH_AGENT
257 if (crypto && crypto->agent)
258 memcpy (&crypto->agent->pinentry_opts, &agent_pin_opts,
259 sizeof(struct pinentry_option_s));
260 #endif
263 static gpg_error_t
264 open_finalize (assuan_context_t ctx, char *key, size_t keylen)
266 struct client_s *client = assuan_get_pointer (ctx);
267 gpg_error_t rc = 0;
268 struct cache_data_s *cdata = cache_get_data (client->md5file);
269 int cached = 0, keyarg = key ? 1 : 0;
270 size_t keysize;
271 unsigned char *salted_key = NULL;
272 int algo;
273 int pin_try = 1;
274 int pin_tries = 3;
275 char *pin_title = client->pinentry_opts.title;
277 client->crypto->filename = str_dup (client->filename);
278 if (cdata || client->flags & FLAG_NEW)
280 if (cdata && !(client->flags & FLAG_NEW))
282 rc = decrypt_cache (client->crypto, cdata->doc, cdata->doclen);
283 if (rc)
284 return rc;
287 cached = cdata != NULL;
288 goto done;
291 if (!key && !IS_PKI (client->crypto)
292 && !(client->crypto->hdr.flags & PWMD_FLAG_NO_PASSPHRASE))
294 if (client->flags & FLAG_NO_PINENTRY)
296 rc = inquire_passphrase (ctx, "PASSPHRASE", (unsigned char **)&key,
297 &keylen);
298 if (rc)
299 return rc;
301 else
303 client->pinentry_opts.timeout = config_get_integer (client->filename,
304 "pinentry_timeout");
305 pin_again:
306 rc = getpin_common (client->ctx, client->filename, PINENTRY_OPEN,
307 &key, &keylen);
308 if (rc)
309 return rc;
313 if (!IS_PKI (client->crypto))
315 if (client->crypto->hdr.flags & PWMD_FLAG_NO_PASSPHRASE)
317 keylen = 1;
318 key = gcry_malloc (keylen);
319 memset (key, 0, keylen);
322 algo = cipher_to_gcrypt (client->crypto->hdr.flags);
323 rc = hash_key (algo, client->crypto->hdr.salt,
324 sizeof(client->crypto->hdr.salt), key, keylen,
325 (void **)&salted_key, &keysize);
326 if (!keyarg)
327 gcry_free (key);
329 if (!rc)
331 rc = decrypt_data (client->ctx, client->crypto, salted_key, keysize);
332 if (gpg_err_code (rc) == GPG_ERR_BAD_PASSPHRASE
333 && ++pin_try <= pin_tries && !(client->flags & FLAG_NO_PINENTRY))
335 gcry_free (salted_key);
336 salted_key = NULL;
337 key = NULL;
338 keylen = 0;
339 client->pinentry_opts.title = str_asprintf (_ ("Bad passphrase (try %i of %i)"), pin_try, pin_tries);
340 goto pin_again;
344 if (client->pinentry_opts.title != pin_title)
345 xfree (client->pinentry_opts.title);
347 client->pinentry_opts.title = pin_title;
348 if (rc)
350 gcry_free (salted_key);
351 return rc;
354 cdata = xcalloc (1, sizeof (struct cache_data_s));
355 if (!cdata)
357 gcry_free (salted_key);
358 return GPG_ERR_ENOMEM;
361 cdata->key = salted_key;
362 cdata->keylen = keysize;
364 else
365 rc = decrypt_data (client->ctx, client->crypto, NULL, 0);
367 done:
368 if (!rc)
370 rc = parse_xml (ctx, client->flags & FLAG_NEW);
371 if (rc && !cached)
372 free_cache_data_once (cdata);
374 if (!rc)
376 int timeout = config_get_integer (client->filename,
377 "cache_timeout");
379 if (!cached)
381 if (!cdata)
382 cdata = xcalloc (1, sizeof (struct cache_data_s));
384 rc = encrypt_xml (NULL, cache_key, cache_keysize,
385 GCRY_CIPHER_AES, client->crypto->plaintext,
386 client->crypto->plaintext_len, &cdata->doc,
387 &cdata->doclen, &cache_iv, &cache_blocksize,
389 if (!rc && !(client->flags & FLAG_NEW) && IS_PKI (client->crypto))
391 rc = gcry_sexp_build ((gcry_sexp_t *) & cdata->pubkey, NULL,
392 "%S", client->crypto->pkey_sexp);
396 if (cdata->sigkey)
397 gcry_sexp_release (cdata->sigkey);
399 cdata->sigkey = NULL;
400 if (!rc && IS_PKI (client->crypto))
401 rc = gcry_sexp_build ((gcry_sexp_t *) &cdata->sigkey, NULL,
402 "%S", client->crypto->sigpkey_sexp);
404 if (!rc)
406 rc = cache_add_file (client->md5file,
407 (client->flags & FLAG_NEW) ? NULL
408 : client->crypto->grip, cdata, timeout);
409 if (rc)
411 if (!cached)
413 free_cache_data_once (cdata);
414 xmlFreeDoc (client->doc);
415 client->doc = NULL;
420 if (!rc)
421 client->flags |= FLAG_OPEN;
425 if (!rc)
426 update_checksum (client);
428 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
429 rc = do_lock (client, 0);
431 return rc;
434 static void
435 req_cleanup (void *arg)
437 if (!arg)
438 return;
440 strv_free ((char **) arg);
443 static gpg_error_t
444 parse_open_opt_lock (void *data, void *value)
446 struct client_s *client = data;
448 client->opts |= OPT_LOCK_ON_OPEN;
449 return 0;
452 static gpg_error_t
453 parse_save_opt_inquire (void *data, void *value)
455 struct client_s *client = data;
456 gpg_error_t rc;
458 if (!(client->flags & FLAG_NEW))
460 rc = peer_is_invoker (client);
461 if (rc == GPG_ERR_EACCES)
462 return rc;
465 (void) value;
466 client->opts |= OPT_INQUIRE;
467 return 0;
470 static gpg_error_t
471 parse_opt_inquire (void *data, void *value)
473 struct client_s *client = data;
475 (void) value;
476 client->opts |= OPT_INQUIRE;
477 return 0;
480 static gpg_error_t
481 update_checksum (struct client_s *client)
483 unsigned char *crc;
484 size_t len;
485 struct cache_data_s *cdata;
486 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
488 if (rc)
489 return rc;
491 xfree (client->crc);
492 client->crc = crc;
493 cdata = cache_get_data (client->md5file);
494 if (cdata)
496 xfree (cdata->crc);
497 cdata->crc = xmalloc (len);
498 memcpy (cdata->crc, crc, len);
501 return 0;
504 static gpg_error_t
505 validate_checksum (struct client_s *client, struct cache_data_s *cdata)
507 unsigned char *crc;
508 size_t len;
509 gpg_error_t rc;
510 int n = 0;
512 if (cdata && !cdata->crc)
513 return GPG_ERR_CHECKSUM;
515 rc = get_checksum (client->filename, &crc, &len);
516 if (rc)
517 return rc;
519 if (cdata)
520 n = memcmp (cdata->crc, crc, len);
521 else if (client->crc)
522 n = memcmp (client->crc, crc, len);
524 xfree (crc);
525 return n ? GPG_ERR_CHECKSUM : 0;
528 static gpg_error_t
529 do_open (assuan_context_t ctx, const char *password)
531 struct client_s *client = assuan_get_pointer (ctx);
532 struct cache_data_s *cdata;
533 gpg_error_t rc = 0;
534 int done = 0;
536 // Cached document?
537 cdata = cache_get_data (client->md5file);
538 if (cdata && cdata->doc)
540 int defer = 0;
542 /* This will check that the key is cached in the agent which needs to
543 * be determined for files that share a keygrip. */
544 if (!rc)
546 rc = cache_iscached (client->filename, &defer);
547 if ((!rc || rc == GPG_ERR_ENOENT) && defer)
548 rc = GPG_ERR_KEY_EXPIRED;
551 if (!rc && !(client->flags & FLAG_NEW))
552 rc = validate_checksum (client, cdata);
554 #ifdef WITH_GNUTLS
555 if (!rc && client->thd->remote
556 && config_get_boolean (client->filename, "tcp_require_key"))
557 rc = GPG_ERR_KEY_EXPIRED;
558 #endif
560 if (!rc && !password)
562 rc = read_data_header (client->filename, &client->crypto->hdr,
563 NULL, NULL);
564 if (!rc)
566 if (IS_PKI (client->crypto))
568 gcry_sexp_build (&client->crypto->pkey_sexp, NULL, "%S",
569 cdata->pubkey);
570 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
571 client->crypto->grip);
572 gcry_sexp_build (&client->crypto->sigpkey_sexp, NULL, "%S",
573 cdata->sigkey);
574 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
575 client->crypto->sign_grip);
578 if (!rc)
580 rc = open_finalize (ctx, NULL, 0);
581 done = 1;
586 /* There was an error accessing the file so clear the cache entry. The
587 * real error will be returned from read_data_file() since the file
588 * may have only disappeared. */
589 if (!done)
591 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
592 rc = cache_clear (client->md5file);
593 send_status_all (STATUS_CACHE, NULL);
597 if (done || rc)
598 return rc;
600 rc = read_data_file (client->filename, client->crypto);
601 if (rc)
603 if (gpg_err_source (rc) != GPG_ERR_SOURCE_DEFAULT ||
604 gpg_err_code (rc) != GPG_ERR_ENOENT)
606 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
607 return rc;
610 client->flags |= FLAG_NEW;
611 memset (client->crypto->grip, 0, sizeof (client->crypto->grip));
612 memset (client->crypto->sign_grip, 0,
613 sizeof (client->crypto->sign_grip));
614 rc = open_finalize (ctx, NULL, 0);
615 return rc;
618 if (password && IS_PKI (client->crypto))
620 #ifdef WITH_AGENT
621 rc = set_agent_passphrase (client->crypto, password, strlen (password));
622 if (rc)
623 return rc;
624 #endif
627 rc = open_finalize (ctx, (char *)password, password ? strlen (password) : 0);
628 return rc;
631 static gpg_error_t
632 open_command (assuan_context_t ctx, char *line)
634 gpg_error_t rc;
635 struct client_s *client = assuan_get_pointer (ctx);
636 char **req, *filename;
637 unsigned char md5file[16];
638 int same_file = 0;
639 assuan_peercred_t peer;
640 struct argv_s *args[] = {
641 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
642 NULL
645 rc = parse_options (&line, args, client);
646 if (rc)
647 return send_error (ctx, rc);
649 req = str_split (line, " ", 2);
650 if (!req)
651 return send_error (ctx, GPG_ERR_SYNTAX);
653 rc = do_validate_peer (ctx, req[0], &peer);
654 if (rc)
656 strv_free (req);
657 return send_error (ctx, rc);
660 filename = req[0];
661 if (!valid_filename (filename))
663 strv_free (req);
664 return send_error (ctx, GPG_ERR_INV_VALUE);
667 pthread_cleanup_push (req_cleanup, req);
668 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, filename, strlen (filename));
669 /* This client may have locked a different file with ISCACHED --lock than
670 * the current filename. This will remove that lock. */
671 same_file = !memcmp (md5file, client->md5file, 16) ? 1 : 0;
672 if (client->flags & FLAG_OPEN ||
673 (client->flags & FLAG_HAS_LOCK && !same_file))
675 uint32_t opts = client->opts;
676 uint32_t flags = client->flags;
678 if (same_file)
679 client->flags |= FLAG_KEEP_LOCK;
680 else if (client->flags & FLAG_NEW)
681 cache_clear (client->md5file);
683 cleanup_client (client);
684 client->opts = opts;
685 client->flags |= flags;
686 client->flags &= ~(FLAG_LOCK_CMD);
687 if (!same_file)
688 client->flags &= ~(FLAG_HAS_LOCK | FLAG_NEW);
691 memcpy (client->md5file, md5file, 16);
692 client->filename = str_dup (filename);
693 if (!client->filename)
695 strv_free (req);
696 return send_error(ctx, GPG_ERR_ENOMEM);
699 /* Need to lock the mutex here because file_modified() cannot without
700 * knowing the filename. */
701 rc = lock_file_mutex (client, 1);
702 if (rc)
703 client->flags &= ~FLAG_OPEN;
705 if (!rc)
707 char *password = req[1] && *req[1] ? req[1] : NULL;
709 #ifdef WITH_AGENT
710 if (IS_PKI (client->crypto))
711 rc = set_pinentry_mode (client->crypto->agent,
712 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
713 : "ask");
714 #endif
715 if (!rc)
717 rc = do_open (ctx, password);
718 if (rc)
719 cleanup_client (client);
721 cleanup_crypto_stage1 (client->crypto);
725 pthread_cleanup_pop (1);
727 if (!rc && client->flags & FLAG_NEW)
728 rc = send_status (ctx, STATUS_NEWFILE, NULL);
730 #ifdef WITH_AGENT
731 (void) kill_scd (client->crypto->agent);
732 #endif
734 return send_error (ctx, rc);
737 static gpg_error_t
738 parse_opt_no_passphrase (void *data, void *value)
740 struct client_s *client = data;
742 (void) value;
743 client->opts |= OPT_NO_PASSPHRASE;
744 return 0;
747 static gpg_error_t
748 parse_save_opt_no_agent (void *data, void *value)
750 struct client_s *client = data;
752 client->opts |= OPT_NO_AGENT;
753 return 0;
756 static gpg_error_t
757 parse_save_opt_cipher (void *data, void *value)
759 struct client_s *client = data;
760 int algo = cipher_string_to_gcrypt ((char *) value);
761 file_header_t *hdr = &client->crypto->save.hdr;
762 gpg_error_t rc = 0;
764 if (algo == -1)
765 return GPG_ERR_INV_VALUE;
767 if (!(client->flags & FLAG_NEW))
769 rc = peer_is_invoker (client);
770 if (rc == GPG_ERR_EACCES)
772 uint64_t flags = 0;
774 flags &= ((uint16_t) ((uint32_t) (flags & 0xFFFFFFFF)));
775 if (!(client->crypto->hdr.flags & gcrypt_to_cipher (algo)))
776 return rc;
778 rc = 0;
780 else if (rc)
781 return rc;
784 if (!rc)
785 hdr->flags = set_cipher_flag (hdr->flags, algo);
787 return rc;
790 #ifdef WITH_AGENT
791 static gpg_error_t
792 permitted_to_save (struct client_s *client, const unsigned char *grip,
793 size_t size, const char *value)
795 char *hexgrip;
796 gpg_error_t rc = 0;
798 if (!(client->flags & FLAG_NEW))
800 hexgrip = bin2hex (grip, size);
801 rc = !strcmp (hexgrip, (char *)value) ? 0 : GPG_ERR_EACCES;
802 xfree (hexgrip);
803 if (rc)
804 rc = peer_is_invoker (client);
807 return rc;
809 #endif
811 static gpg_error_t
812 parse_save_opt_keygrip (void *data, void *value)
814 #ifdef WITH_AGENT
815 struct client_s *client = data;
816 gpg_error_t rc = permitted_to_save (client, client->crypto->grip,
817 sizeof (client->crypto->grip),
818 value);
820 if (!IS_PKI (client->crypto))
821 return GPG_ERR_INV_ARG;
823 if (rc)
824 return rc;
826 if (client->crypto->save.pkey)
827 gcry_sexp_release (client->crypto->save.pkey);
829 client->crypto->save.pkey = NULL;
830 return get_pubkey (client->crypto, value, &client->crypto->save.pkey);
831 #else
832 return GPG_ERR_INV_ARG;
833 #endif
836 static gpg_error_t
837 parse_save_opt_sign_keygrip (void *data, void *value)
839 #ifdef WITH_AGENT
840 struct client_s *client = data;
841 gpg_error_t rc = permitted_to_save (client, client->crypto->sign_grip,
842 sizeof (client->crypto->sign_grip),
843 value);
845 if (!IS_PKI (client->crypto))
846 return GPG_ERR_INV_ARG;
848 if (rc)
849 return rc;
851 if (client->crypto->save.sigpkey)
852 gcry_sexp_release (client->crypto->save.sigpkey);
854 client->crypto->save.sigpkey = NULL;
855 return get_pubkey (client->crypto, value, &client->crypto->save.sigpkey);
856 #else
857 return GPG_ERR_INV_ARG;
858 #endif
861 static gpg_error_t
862 parse_opt_s2k_count (void *data, void *value)
864 struct client_s *client = data;
865 char *v = value;
867 if (!v || !*v)
868 return GPG_ERR_INV_VALUE;
870 client->crypto->save.s2k_count = strtoul (v, NULL, 10);
871 return 0;
874 static gpg_error_t
875 parse_save_opt_iterations (void *data, void *value)
877 struct client_s *client = data;
878 char *v = value, *p;
879 uint64_t n;
881 if (!v || !*v)
882 return GPG_ERR_INV_VALUE;
884 errno = 0;
885 n = strtoull (v, &p, 10);
886 if (n == UINT64_MAX && errno)
887 return gpg_error_from_errno (errno);
888 else if (p && *p)
889 return GPG_ERR_INV_VALUE;
891 if (!(client->flags & FLAG_NEW))
893 gpg_error_t rc = peer_is_invoker (client);
895 if (rc == GPG_ERR_EACCES)
897 if (client->crypto->hdr.iterations != n)
898 return rc;
900 rc = 0;
902 else if (rc)
903 return rc;
906 client->crypto->save.hdr.iterations = n;
907 return 0;
910 static gpg_error_t
911 save_finalize (assuan_context_t ctx)
913 struct client_s *client = assuan_get_pointer (ctx);
914 gpg_error_t rc = 0;
915 xmlChar *xmlbuf = NULL;
916 int xmlbuflen;
917 void *key = NULL;
918 size_t keylen = 0;
920 xmlDocDumpFormatMemory (client->doc, &xmlbuf, &xmlbuflen, 0);
921 if (!xmlbuf)
922 return GPG_ERR_ENOMEM;
924 pthread_cleanup_push (xmlFree, xmlbuf);
926 if (!use_agent || ((client->flags & FLAG_NEW)
927 && (client->opts & OPT_NO_AGENT))
928 || !(client->crypto->hdr.flags & PWMD_FLAG_PKI))
930 rc = export_common (ctx, client->flags & FLAG_NO_PINENTRY,
931 client->crypto, xmlbuf, xmlbuflen, client->filename,
932 NULL, &key, &keylen, 1, 1,
933 (client->opts & OPT_NO_PASSPHRASE));
935 #ifdef WITH_AGENT
936 else
938 gcry_sexp_t pubkey = client->crypto->save.pkey;
939 gcry_sexp_t sigkey = client->crypto->save.sigpkey;
941 if (!pubkey)
942 pubkey = client->crypto->pkey_sexp;
944 if (!sigkey)
946 sigkey = client->crypto->sigpkey_sexp;
947 if (!sigkey)
949 gcry_sexp_build (&client->crypto->save.sigpkey, 0, "%S", pubkey);
950 sigkey = client->crypto->save.sigpkey;
954 rc = encrypt_data_file (client->ctx, client->crypto, pubkey, sigkey,
955 client->filename, xmlbuf, xmlbuflen);
956 if (pubkey == client->crypto->save.pkey)
958 if (!rc)
960 gcry_sexp_release (client->crypto->pkey_sexp);
961 client->crypto->pkey_sexp = client->crypto->save.pkey;
962 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
963 client->crypto->grip);
965 else
966 gcry_sexp_release (pubkey);
968 client->crypto->save.pkey = NULL;
971 if (!rc)
972 gcry_pk_get_keygrip (sigkey, client->crypto->sign_grip);
974 if (sigkey == client->crypto->save.sigpkey)
976 if (!rc)
978 if (client->crypto->sigpkey_sexp)
979 gcry_sexp_release (client->crypto->sigpkey_sexp);
981 client->crypto->sigpkey_sexp = client->crypto->save.sigpkey;
982 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
983 client->crypto->sign_grip);
985 else
986 gcry_sexp_release (sigkey);
988 client->crypto->save.sigpkey = NULL;
991 #endif
993 if (!rc)
995 int cached;
997 rc = save_common (client->filename, client->crypto, xmlbuf, xmlbuflen,
998 key, keylen, &cached, client->opts & OPT_NO_AGENT);
999 if (rc)
1000 gcry_free (key);
1002 if (!rc && (!cached || (client->flags & FLAG_NEW)))
1003 send_status_all (STATUS_CACHE, NULL);
1005 if (!rc)
1007 rc = update_checksum (client);
1008 client->flags &= ~(FLAG_NEW);
1012 pthread_cleanup_pop (1); // xmlFree
1013 return rc;
1016 static gpg_error_t
1017 parse_opt_reset (void *data, void *value)
1019 struct client_s *client = data;
1021 (void) value;
1022 client->opts |= OPT_RESET;
1023 return 0;
1026 static gpg_error_t
1027 save_command (assuan_context_t ctx, char *line)
1029 struct client_s *client = assuan_get_pointer (ctx);
1030 gpg_error_t rc;
1031 struct stat st;
1032 struct argv_s *args[] = {
1033 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG,
1034 parse_opt_no_passphrase},
1035 &(struct argv_s) {"cipher", OPTION_TYPE_ARG, parse_save_opt_cipher},
1036 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
1037 parse_save_opt_inquire},
1038 &(struct argv_s) {"keygrip", OPTION_TYPE_ARG, parse_save_opt_keygrip},
1039 &(struct argv_s) {"sign-keygrip", OPTION_TYPE_ARG,
1040 parse_save_opt_sign_keygrip},
1041 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
1042 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
1043 &(struct argv_s) {"cipher-iterations", OPTION_TYPE_ARG,
1044 parse_save_opt_iterations},
1045 &(struct argv_s) {"no-agent", OPTION_TYPE_NOARG,
1046 parse_save_opt_no_agent},
1047 NULL
1050 cleanup_save (&client->crypto->save);
1051 memcpy (&client->crypto->save.hdr, &client->crypto->hdr,
1052 sizeof (file_header_t));
1053 client->crypto->save.s2k_count =
1054 config_get_ulong (client->filename, "s2k_count");
1056 if (client->flags & FLAG_NEW)
1057 client->crypto->save.hdr.iterations =
1058 config_get_ulonglong (client->filename, "cipher_iterations");
1060 rc = parse_options (&line, args, client);
1061 if (rc)
1062 return send_error (ctx, rc);
1064 if (!(client->flags & FLAG_NEW))
1065 client->opts &= ~OPT_NO_AGENT;
1066 else if (client->opts & OPT_NO_AGENT)
1068 client->crypto->save.hdr.flags &= ~PWMD_FLAG_PKI;
1069 client->crypto->hdr.flags &= ~PWMD_FLAG_PKI;
1072 if ((client->opts & OPT_NO_PASSPHRASE) && !(client->flags & FLAG_NEW)
1073 && !(client->opts & OPT_INQUIRE))
1074 return send_error (ctx, GPG_ERR_WRONG_KEY_USAGE);
1076 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
1077 return send_error (ctx, gpg_error_from_errno (errno));
1079 if (errno != ENOENT && !S_ISREG (st.st_mode))
1081 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
1082 return send_error (ctx, GPG_ERR_ENOANO);
1085 int defer = 0;
1086 rc = cache_iscached (client->filename, &defer);
1087 if (gpg_err_code (rc) == GPG_ERR_ENOENT && client->flags & FLAG_NEW)
1088 rc = 0;
1089 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
1090 rc = 0;
1092 if ((!rc && defer) || config_get_boolean ("global", "require_save_key"))
1093 client->opts |= OPT_RESET;
1095 if (!rc && (client->opts & OPT_RESET))
1097 rc = cache_clear (client->md5file);
1098 if (rc)
1099 return send_error (ctx, rc);
1101 log_write ("%s: %s", client->filename,
1102 pwmd_strerror (GPG_ERR_KEY_EXPIRED));
1103 send_status_all (STATUS_CACHE, NULL);
1106 if (!rc)
1107 rc = update_element_mtime (client, xmlDocGetRootElement (client->doc));
1109 if (rc)
1110 return send_error (ctx, rc);
1112 #ifdef WITH_AGENT
1113 if (!rc && use_agent && !client->crypto->save.pkey &&
1114 (client->flags & FLAG_NEW || client->opts & OPT_INQUIRE)
1115 && !(client->opts & OPT_NO_AGENT))
1117 rc = set_pinentry_mode (client->crypto->agent,
1118 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
1119 : "ask");
1121 if (!(client->flags & FLAG_NEW))
1123 struct crypto_s *crypto;
1124 char *key = NULL;
1125 size_t keylen = 0;
1127 /* Wanting to generate a new key. Require the key to open the
1128 current file before proceeding reguardless of the
1129 require_save_key configuration parameter. */
1130 rc = cache_clear (client->md5file);
1131 if (!rc)
1133 rc = init_client_crypto (&crypto);
1134 if (!rc)
1135 crypto->client_ctx = client->ctx;
1138 if (!rc)
1139 rc = decrypt_common (client->ctx, client->opts&OPT_INQUIRE, crypto,
1140 client->filename, &key, &keylen);
1142 xfree (key);
1143 cleanup_crypto (&crypto);
1146 if (!rc)
1147 rc = send_status (client->ctx, STATUS_GENKEY, NULL);
1149 if (!rc)
1151 struct inquire_data_s idata = { 0 };
1152 char *params = client->opts & OPT_INQUIRE
1153 ? NULL : default_key_params (client->crypto);
1155 pthread_cleanup_push (xfree, params);
1156 idata.crypto = client->crypto;
1158 if (params)
1160 idata.line = params;
1161 idata.len = strlen (params);
1163 else
1164 idata.preset = 1;
1166 client->crypto->agent->inquire_data = &idata;
1167 client->crypto->agent->inquire_cb = NULL;
1168 rc = generate_key (client->crypto, params,
1169 (client->opts & OPT_NO_PASSPHRASE), 1);
1170 pthread_cleanup_pop (1);
1173 #endif
1175 if (!rc)
1176 rc = save_finalize (ctx);
1178 cleanup_crypto_stage1 (client->crypto);
1179 #ifdef WITH_AGENT
1180 (void) kill_scd (client->crypto->agent);
1181 #endif
1182 return send_error (ctx, rc);
1185 static gpg_error_t
1186 do_delete (assuan_context_t ctx, char *line)
1188 struct client_s *client = assuan_get_pointer (ctx);
1189 gpg_error_t rc;
1190 char **req;
1191 xmlNodePtr n;
1193 if (strchr (line, '\t'))
1194 req = str_split (line, "\t", 0);
1195 else
1196 req = str_split (line, " ", 0);
1198 if (!req || !*req)
1199 return GPG_ERR_SYNTAX;
1201 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1202 if (!n)
1204 strv_free (req);
1205 return rc;
1209 * No sub-node defined. Remove the entire node (root element).
1211 if (!req[1])
1213 if (n)
1215 rc = is_element_owner (client, n);
1216 if (!rc)
1218 rc = unlink_node (client, n);
1219 xmlFreeNode (n);
1223 strv_free (req);
1224 return rc;
1227 n = find_elements (client, client->doc, n->children, req + 1, &rc, NULL,
1228 NULL, NULL, 0, 0, NULL, 0);
1229 strv_free (req);
1230 if (!n)
1231 return rc;
1233 rc = is_element_owner (client, n);
1234 if (!rc)
1236 rc = unlink_node (client, n);
1237 xmlFreeNode (n);
1240 return rc;
1243 static gpg_error_t
1244 delete_command (assuan_context_t ctx, char *line)
1246 struct client_s *client = assuan_get_pointer (ctx);
1247 gpg_error_t rc;
1248 struct argv_s *args[] = {
1249 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1250 NULL
1253 rc = parse_options (&line, args, client);
1254 if (rc)
1255 return send_error (ctx, rc);
1257 if (client->opts & OPT_INQUIRE)
1259 unsigned char *result;
1260 size_t len;
1262 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1263 if (rc)
1264 return send_error (ctx, rc);
1266 pthread_cleanup_push (xfree, result);
1267 rc = do_delete (ctx, (char *)result);
1268 pthread_cleanup_pop (1);
1270 else
1271 rc = do_delete (ctx, line);
1273 return send_error (ctx, rc);
1276 static gpg_error_t
1277 store_command (assuan_context_t ctx, char *line)
1279 struct client_s *client = assuan_get_pointer (ctx);
1280 gpg_error_t rc;
1281 size_t len;
1282 unsigned char *result;
1283 char **req;
1284 xmlNodePtr n, parent;
1285 int has_content;
1286 char *content = NULL;
1288 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1289 if (rc)
1290 return send_error (ctx, rc);
1292 req = str_split ((char *) result, "\t", 0);
1293 xfree (result);
1295 if (!req || !*req)
1296 return send_error (ctx, GPG_ERR_SYNTAX);
1298 len = strv_length (req);
1299 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1300 if (*(req + 1) && !valid_element_path (req, has_content))
1302 strv_free (req);
1303 return send_error (ctx, GPG_ERR_INV_VALUE);
1306 if (has_content || !*req[len - 1])
1308 has_content = 1;
1309 content = req[len - 1];
1310 req[len - 1] = NULL;
1313 again:
1314 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1315 if (rc && rc == GPG_ERR_ELEMENT_NOT_FOUND)
1317 rc = new_root_element (client, client->doc, *req);
1318 if (rc)
1320 strv_free (req);
1321 return send_error (ctx, rc);
1324 goto again;
1327 if (!n)
1329 strv_free (req);
1330 return send_error (ctx, rc);
1333 parent = n;
1335 if (req[1] && *req[1])
1337 if (!n->children)
1338 parent = create_elements_cb (client, 1, n, req + 1, &rc, NULL);
1339 else
1340 parent = find_elements (client, client->doc, n->children, req + 1, &rc,
1341 NULL, NULL, create_elements_cb, 0, 0, NULL,
1345 if (!rc && len > 1)
1347 rc = is_element_owner (client, parent);
1348 if (!rc)
1350 n = find_text_node (parent->children);
1351 if (n)
1352 xmlNodeSetContent (n, (xmlChar *) content);
1353 else
1354 xmlNodeAddContent (parent, (xmlChar *) content);
1356 update_element_mtime (client, parent);
1360 xfree (content);
1361 strv_free (req);
1362 return send_error (ctx, rc);
1365 static gpg_error_t
1366 xfer_data (assuan_context_t ctx, const char *line, int total)
1368 int to_send;
1369 int sent = 0;
1370 gpg_error_t rc;
1371 int progress = config_get_integer ("global", "xfer_progress");
1372 int flush = 0;
1374 progress =
1375 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1376 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1377 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1379 if (rc)
1380 return rc;
1382 again:
1385 if (sent + to_send > total)
1386 to_send = total - sent;
1388 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1389 flush ? 0 : to_send);
1390 if (!rc)
1392 sent += flush ? 0 : to_send;
1394 if ((progress && !(sent % progress) && sent != total) ||
1395 (sent == total && flush))
1396 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1398 if (!flush && !rc && sent == total)
1400 flush = 1;
1401 goto again;
1405 while (!rc && sent < total);
1407 return rc;
1410 static gpg_error_t
1411 do_get (assuan_context_t ctx, char *line)
1413 struct client_s *client = assuan_get_pointer (ctx);
1414 gpg_error_t rc;
1415 char **req;
1416 xmlNodePtr n;
1418 req = str_split (line, "\t", 0);
1420 if (!req || !*req)
1422 strv_free (req);
1423 return GPG_ERR_SYNTAX;
1426 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1427 if (!n)
1429 strv_free (req);
1430 return rc;
1433 if (req[1])
1435 find_elements (client, client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1436 0, 0, NULL, 0);
1438 strv_free (req);
1439 if (rc)
1440 return rc;
1442 if (!n || !n->children)
1443 return GPG_ERR_NO_DATA;
1445 n = find_text_node (n->children);
1446 if (!n || !n->content || !*n->content)
1447 return GPG_ERR_NO_DATA;
1449 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1450 return rc;
1453 static gpg_error_t
1454 get_command (assuan_context_t ctx, char *line)
1456 struct client_s *client = assuan_get_pointer (ctx);
1457 gpg_error_t rc;
1458 struct argv_s *args[] = {
1459 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1460 NULL
1463 rc = parse_options (&line, args, client);
1464 if (rc)
1465 return send_error (ctx, rc);
1467 if (client->opts & OPT_INQUIRE)
1469 unsigned char *result;
1470 size_t len;
1472 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1473 if (rc)
1474 return send_error (ctx, rc);
1476 pthread_cleanup_push (xfree, result);
1477 rc = do_get (ctx, (char *)result);
1478 pthread_cleanup_pop (1);
1480 else
1481 rc = do_get (ctx, line);
1483 return send_error (ctx, rc);
1486 static void list_command_cleanup1 (void *arg);
1487 static gpg_error_t
1488 realpath_command (assuan_context_t ctx, char *line)
1490 struct string_s *string = NULL;
1491 gpg_error_t rc;
1492 struct client_s *client = assuan_get_pointer (ctx);
1493 struct argv_s *args[] = {
1494 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1495 NULL
1498 rc = parse_options (&line, args, client);
1499 if (rc)
1500 return send_error (ctx, rc);
1502 if (client->opts & OPT_INQUIRE)
1504 unsigned char *result;
1505 size_t len;
1507 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1508 if (rc)
1509 return send_error (ctx, rc);
1511 pthread_cleanup_push (xfree, result);
1512 rc = build_realpath (client, client->doc, (char *)result, &string);
1513 pthread_cleanup_pop (1);
1515 else
1516 rc = build_realpath (client, client->doc, line, &string);
1518 if (!rc)
1520 pthread_cleanup_push (list_command_cleanup1, string);
1521 rc = xfer_data (ctx, string->str, string->len);
1522 pthread_cleanup_pop (1);
1525 return send_error (ctx, rc);
1528 static void
1529 list_command_cleanup1 (void *arg)
1531 if (arg)
1532 string_free ((struct string_s *) arg, 1);
1535 static void
1536 list_command_cleanup2 (void *arg)
1538 struct element_list_s *elements = arg;
1540 if (elements)
1542 if (elements->list)
1544 int total = slist_length (elements->list);
1545 int i;
1547 for (i = 0; i < total; i++)
1549 char *tmp = slist_nth_data (elements->list, i);
1550 xfree (tmp);
1553 slist_free (elements->list);
1556 if (elements->prefix)
1557 xfree (elements->prefix);
1559 if (elements->req)
1560 strv_free (elements->req);
1562 xfree (elements);
1566 static gpg_error_t
1567 parse_list_opt_norecurse (void *data, void *value)
1569 struct client_s *client = data;
1571 client->opts &= ~(OPT_LIST_RECURSE);
1572 return 0;
1575 static gpg_error_t
1576 parse_opt_verbose (void *data, void *value)
1578 struct client_s *client = data;
1580 client->opts |= OPT_VERBOSE;
1581 return 0;
1584 static gpg_error_t
1585 parse_list_opt_target (void *data, void *value)
1587 struct client_s *client = data;
1589 client->opts |= OPT_LIST_WITH_TARGET;
1590 return 0;
1593 static gpg_error_t
1594 parse_list_opt_all (void *data, void *value)
1596 struct client_s *client = data;
1598 client->opts |= OPT_LIST_ALL;
1599 return 0;
1602 static gpg_error_t
1603 list_path_once (struct client_s *client, char *line,
1604 struct element_list_s *elements, struct string_s *result)
1606 gpg_error_t rc;
1608 elements->req = str_split (line, " ", 0);
1609 if (!elements->req)
1610 strv_printf (&elements->req, "%s", line);
1612 rc = create_path_list (client, client->doc, elements, *elements->req);
1613 if ((rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES) && elements->verbose)
1614 rc = 0;
1616 if (!rc)
1618 int total = slist_length (elements->list);
1620 if (!total)
1621 rc = GPG_ERR_NO_DATA;
1623 if (!rc)
1625 if (!rc)
1627 int i;
1629 for (i = 0; i < total; i++)
1631 char *tmp = slist_nth_data (elements->list, i);
1633 string_append_printf (result, "%s%s", tmp,
1634 i + 1 == total ? "" : "\n");
1638 else
1639 rc = GPG_ERR_NO_DATA;
1642 return rc;
1645 static int
1646 has_list_flag (char *path, char *flags)
1648 char *p = path;
1650 while (*p && *++p != ' ');
1652 if (!*p)
1653 return 0;
1655 for (; *p; p++)
1657 char *f;
1659 for (f = flags; *f && *f != ' '; f++)
1661 if (*p == *f)
1662 return 1;
1666 return 0;
1669 static gpg_error_t
1670 do_list (assuan_context_t ctx, char *line)
1672 struct client_s *client = assuan_get_pointer (ctx);
1673 gpg_error_t rc;
1674 struct element_list_s *elements = NULL;
1676 elements = xcalloc (1, sizeof (struct element_list_s));
1677 if (!elements)
1678 return GPG_ERR_ENOMEM;
1680 elements->recurse = client->opts & OPT_LIST_RECURSE;
1681 elements->verbose =
1682 (client->opts & OPT_VERBOSE) | (client->opts & OPT_LIST_WITH_TARGET);
1683 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1685 if (!line || !*line)
1687 struct string_s *str = NULL;
1689 pthread_cleanup_push (list_command_cleanup2, elements);
1690 rc = list_root_elements (client, client->doc, &str, elements->verbose,
1691 elements->with_target);
1692 pthread_cleanup_pop (1);
1693 pthread_cleanup_push (list_command_cleanup1, str);
1695 if (!rc)
1697 if (client->opts & OPT_LIST_ALL)
1699 char **roots = str_split (str->str, "\n", 0);
1700 char **p;
1702 pthread_cleanup_push (req_cleanup, roots);
1703 string_truncate (str, 0);
1705 for (p = roots; *p; p++)
1707 if (strchr (*p, ' '))
1709 if (has_list_flag (*p, "EOP"))
1711 string_append_printf (str, "%s%s", *p,
1712 *(p + 1) ? "\n" : "");
1713 continue;
1717 elements = xcalloc (1, sizeof (struct element_list_s));
1718 if (!elements)
1720 rc = GPG_ERR_ENOMEM;
1721 break;
1724 elements->recurse = client->opts & OPT_LIST_RECURSE;
1725 elements->verbose =
1726 (client->opts & OPT_VERBOSE) | (client->opts &
1727 OPT_LIST_WITH_TARGET);
1728 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1729 pthread_cleanup_push (list_command_cleanup2, elements);
1730 rc = list_path_once (client, *p, elements, str);
1731 pthread_cleanup_pop (1);
1732 if (rc)
1733 break;
1735 if (*(p + 1))
1736 string_append (str, "\n");
1739 pthread_cleanup_pop (1);
1742 if (!rc)
1743 rc = xfer_data (ctx, str->str, str->len);
1746 pthread_cleanup_pop (1);
1747 return rc;
1750 pthread_cleanup_push (list_command_cleanup2, elements);
1751 struct string_s *str = string_new (NULL);
1752 pthread_cleanup_push (list_command_cleanup1, str);
1753 rc = list_path_once (client, line, elements, str);
1754 if (!rc)
1755 rc = xfer_data (ctx, str->str, str->len);
1757 pthread_cleanup_pop (1);
1758 pthread_cleanup_pop (1);
1759 return rc;
1762 static gpg_error_t
1763 list_command (assuan_context_t ctx, char *line)
1765 struct client_s *client = assuan_get_pointer (ctx);
1766 gpg_error_t rc;
1767 struct argv_s *args[] = {
1768 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG,
1769 parse_list_opt_norecurse},
1770 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_opt_verbose},
1771 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1772 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG,
1773 parse_list_opt_target},
1774 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_all},
1775 NULL
1778 if (disable_list_and_dump == 1)
1779 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1781 client->opts |= OPT_LIST_RECURSE;
1782 rc = parse_options (&line, args, client);
1783 if (rc)
1784 return send_error (ctx, rc);
1786 if (client->opts & OPT_LIST_ALL)
1787 client->opts |= OPT_LIST_RECURSE | OPT_VERBOSE;
1789 if (client->opts & OPT_INQUIRE)
1791 unsigned char *result;
1792 size_t len;
1794 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1795 if (rc)
1796 return send_error (ctx, rc);
1798 pthread_cleanup_push (xfree, result);
1799 rc = do_list (ctx, (char *)result);
1800 pthread_cleanup_pop (1);
1802 else
1803 rc = do_list (ctx, line);
1805 return send_error (ctx, rc);
1809 * req[0] - element path
1811 static gpg_error_t
1812 attribute_list (assuan_context_t ctx, char **req)
1814 struct client_s *client = assuan_get_pointer (ctx);
1815 char **attrlist = NULL;
1816 int i = 0;
1817 char **path = NULL;
1818 xmlAttrPtr a;
1819 xmlNodePtr n, an;
1820 char *line;
1821 gpg_error_t rc;
1823 if (!req || !req[0])
1824 return GPG_ERR_SYNTAX;
1826 if ((path = str_split (req[0], "\t", 0)) == NULL)
1829 * The first argument may be only a root element.
1831 if ((path = str_split (req[0], " ", 0)) == NULL)
1832 return GPG_ERR_SYNTAX;
1835 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1837 if (!n)
1839 strv_free (path);
1840 return rc;
1843 if (path[1])
1845 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1846 NULL, NULL, NULL, 0, 0, NULL, 0);
1848 if (!n)
1850 strv_free (path);
1851 return rc;
1855 strv_free (path);
1857 for (a = n->properties; a; a = a->next)
1859 char **pa;
1861 if ((pa = xrealloc (attrlist, (i + 2) * sizeof (char *))) == NULL)
1863 if (attrlist)
1864 strv_free (attrlist);
1866 log_write ("%s(%i): %s", __FILE__, __LINE__,
1867 pwmd_strerror (GPG_ERR_ENOMEM));
1868 return GPG_ERR_ENOMEM;
1871 attrlist = pa;
1872 an = a->children;
1873 attrlist[i] = str_asprintf ("%s %s", (char *) a->name,
1875 && an->content ? (char *) an->content : "");
1877 if (!attrlist[i])
1879 strv_free (attrlist);
1880 log_write ("%s(%i): %s", __FILE__, __LINE__,
1881 pwmd_strerror (GPG_ERR_ENOMEM));
1882 return GPG_ERR_ENOMEM;
1885 attrlist[++i] = NULL;
1888 if (!attrlist)
1889 return GPG_ERR_NO_DATA;
1891 line = strv_join ("\n", attrlist);
1893 if (!line)
1895 log_write ("%s(%i): %s", __FILE__, __LINE__,
1896 pwmd_strerror (GPG_ERR_ENOMEM));
1897 strv_free (attrlist);
1898 return GPG_ERR_ENOMEM;
1901 pthread_cleanup_push (xfree, line);
1902 pthread_cleanup_push (req_cleanup, attrlist);
1903 rc = xfer_data (ctx, line, strlen (line));
1904 pthread_cleanup_pop (1);
1905 pthread_cleanup_pop (1);
1906 return rc;
1910 * req[0] - attribute
1911 * req[1] - element path
1913 static gpg_error_t
1914 attribute_delete (struct client_s *client, char **req)
1916 xmlNodePtr n;
1917 char **path = NULL;
1918 gpg_error_t rc;
1920 if (!req || !req[0] || !req[1])
1921 return GPG_ERR_SYNTAX;
1923 if (!strcmp (req[0], "_name"))
1924 return GPG_ERR_INV_ATTR;
1926 if ((path = str_split (req[1], "\t", 0)) == NULL)
1929 * The first argument may be only a root element.
1931 if ((path = str_split (req[1], " ", 0)) == NULL)
1932 return GPG_ERR_SYNTAX;
1935 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1936 if (!n)
1937 goto fail;
1939 if (path[1])
1941 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1942 NULL, NULL, NULL, 0, 0, NULL, 0);
1943 if (!n)
1944 goto fail;
1947 if (!strcmp (req[0], (char *) "_acl"))
1949 rc = is_element_owner (client, n);
1950 if (rc)
1951 goto fail;
1954 rc = delete_attribute (client, n, (xmlChar *) req[0]);
1956 fail:
1957 strv_free (path);
1958 return rc;
1961 static xmlNodePtr
1962 create_element_path (struct client_s *client,
1963 char ***elements, gpg_error_t * rc, xmlNodePtr parent)
1965 char **req = *elements;
1966 char **req_orig = strv_dup (req);
1967 xmlNodePtr n = NULL;
1969 *rc = 0;
1971 if (!req_orig)
1973 *rc = GPG_ERR_ENOMEM;
1974 log_write ("%s(%i): %s", __FILE__, __LINE__,
1975 pwmd_strerror (GPG_ERR_ENOMEM));
1976 goto fail;
1979 again:
1980 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
1981 if (!n)
1983 if (*rc != GPG_ERR_ELEMENT_NOT_FOUND)
1984 goto fail;
1986 *rc = new_root_element (client, client->doc, req[0]);
1987 if (*rc)
1988 goto fail;
1990 goto again;
1992 else if (n == parent)
1994 *rc = GPG_ERR_CONFLICT;
1995 goto fail;
1998 if (req[1])
2000 if (!n->children)
2001 n = create_target_elements_cb (client, 1, n, req + 1, rc, NULL);
2002 else
2003 n = find_elements (client, client->doc, n->children, req + 1, rc,
2004 NULL, NULL, create_target_elements_cb, 0, 0,
2005 parent, 0);
2007 if (!n)
2008 goto fail;
2011 * Reset the position of the element tree now that the elements
2012 * have been created.
2014 strv_free (req);
2015 req = req_orig;
2016 req_orig = NULL;
2017 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
2018 if (!n)
2019 goto fail;
2021 n = find_elements (client, client->doc, n->children, req + 1, rc,
2022 NULL, NULL, NULL, 0, 0, NULL, 0);
2023 if (!n)
2024 goto fail;
2027 fail:
2028 if (req_orig)
2029 strv_free (req_orig);
2031 *elements = req;
2032 return n;
2036 * Creates a "target" attribute. When other commands encounter an element with
2037 * this attribute, the element path is modified to the target value. If the
2038 * source element path doesn't exist when using 'ATTR SET target', it is
2039 * created, but the destination element path must exist.
2041 * req[0] - source element path
2042 * req[1] - destination element path
2044 static gpg_error_t
2045 target_attribute (struct client_s *client, char **req)
2047 char **src, **dst, *line = NULL, **odst = NULL;
2048 gpg_error_t rc;
2049 xmlNodePtr n;
2051 if (!req || !req[0] || !req[1])
2052 return GPG_ERR_SYNTAX;
2054 if ((src = str_split (req[0], "\t", 0)) == NULL)
2057 * The first argument may be only a root element.
2059 if ((src = str_split (req[0], " ", 0)) == NULL)
2060 return GPG_ERR_SYNTAX;
2063 if (!valid_element_path (src, 0))
2064 return GPG_ERR_INV_VALUE;
2066 if ((dst = str_split (req[1], "\t", 0)) == NULL)
2069 * The first argument may be only a root element.
2071 if ((dst = str_split (req[1], " ", 0)) == NULL)
2073 rc = GPG_ERR_SYNTAX;
2074 goto fail;
2078 odst = strv_dup (dst);
2079 if (!odst)
2081 rc = GPG_ERR_ENOMEM;
2082 goto fail;
2085 n = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
2087 * Make sure the destination element path exists.
2089 if (!n)
2090 goto fail;
2092 if (dst[1])
2094 n = find_elements (client, client->doc, n->children, dst + 1, &rc,
2095 NULL, NULL, NULL, 0, 0, NULL, 0);
2096 if (!n)
2097 goto fail;
2100 rc = validate_target_attribute (client, client->doc, req[0], n);
2101 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2102 goto fail;
2104 n = create_element_path (client, &src, &rc, NULL);
2105 if (rc)
2106 goto fail;
2108 line = strv_join ("\t", odst);
2109 if (!line)
2111 rc = GPG_ERR_ENOMEM;
2112 goto fail;
2115 rc = add_attribute (client, n, "target", line);
2117 fail:
2118 xfree (line);
2119 strv_free (src);
2120 strv_free (dst);
2121 strv_free (odst);
2122 return rc;
2126 * req[0] - attribute
2127 * req[1] - element path
2129 static gpg_error_t
2130 attribute_get (assuan_context_t ctx, char **req)
2132 struct client_s *client = assuan_get_pointer (ctx);
2133 xmlNodePtr n;
2134 xmlChar *a;
2135 char **path = NULL;
2136 gpg_error_t rc;
2138 if (!req || !req[0] || !req[1])
2139 return GPG_ERR_SYNTAX;
2141 if (strchr (req[1], '\t'))
2143 if ((path = str_split (req[1], "\t", 0)) == NULL)
2144 return GPG_ERR_SYNTAX;
2146 else
2148 if ((path = str_split (req[1], " ", 0)) == NULL)
2149 return GPG_ERR_SYNTAX;
2152 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2154 if (!n)
2155 goto fail;
2157 if (path[1])
2159 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2160 NULL, NULL, NULL, 0, 0, NULL, 0);
2162 if (!n)
2163 goto fail;
2166 strv_free (path);
2168 if ((a = xmlGetProp (n, (xmlChar *) req[0])) == NULL)
2169 return GPG_ERR_NOT_FOUND;
2171 pthread_cleanup_push (xmlFree, a);
2173 if (*a)
2174 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
2175 else
2176 rc = GPG_ERR_NO_DATA;
2178 pthread_cleanup_pop (1);
2179 return rc;
2181 fail:
2182 strv_free (path);
2183 return rc;
2187 * req[0] - attribute
2188 * req[1] - element path
2189 * req[2] - value
2191 static gpg_error_t
2192 attribute_set (struct client_s *client, char **req)
2194 char **path = NULL;
2195 gpg_error_t rc;
2196 xmlNodePtr n;
2198 if (!req || !req[0] || !req[1])
2199 return GPG_ERR_SYNTAX;
2202 * Reserved attribute names.
2204 if (!strcmp (req[0], "_name"))
2205 return GPG_ERR_INV_ATTR;
2206 else if (!strcmp (req[0], "target"))
2207 return target_attribute (client, req + 1);
2208 else if (!valid_xml_attribute (req[0]))
2209 return GPG_ERR_INV_VALUE;
2211 if ((path = str_split (req[1], "\t", 0)) == NULL)
2214 * The first argument may be only a root element.
2216 if ((path = str_split (req[1], " ", 0)) == NULL)
2217 return GPG_ERR_SYNTAX;
2220 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2222 if (!n)
2223 goto fail;
2225 if (path[1])
2227 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2228 NULL, NULL, NULL, 0, 0, NULL, 0);
2230 if (!n)
2231 goto fail;
2234 if (!strcmp (req[0], (char *) "_acl"))
2236 rc = is_element_owner (client, n);
2237 if (rc)
2238 goto fail;
2241 rc = add_attribute (client, n, req[0], req[2]);
2243 fail:
2244 strv_free (path);
2245 return rc;
2249 * req[0] - command
2250 * req[1] - attribute name or element path if command is LIST
2251 * req[2] - element path
2252 * req[2] - element path or value
2255 static gpg_error_t
2256 do_attr (assuan_context_t ctx, char *line)
2258 struct client_s *client = assuan_get_pointer (ctx);
2259 gpg_error_t rc = 0;
2260 char **req;
2262 req = str_split (line, " ", 4);
2263 if (!req || !req[0] || !req[1])
2265 strv_free (req);
2266 return GPG_ERR_SYNTAX;
2269 pthread_cleanup_push (req_cleanup, req);
2271 if (strcasecmp (req[0], "SET") == 0)
2272 rc = attribute_set (client, req + 1);
2273 else if (strcasecmp (req[0], "GET") == 0)
2274 rc = attribute_get (ctx, req + 1);
2275 else if (strcasecmp (req[0], "DELETE") == 0)
2276 rc = attribute_delete (client, req + 1);
2277 else if (strcasecmp (req[0], "LIST") == 0)
2278 rc = attribute_list (ctx, req + 1);
2279 else
2280 rc = GPG_ERR_SYNTAX;
2282 pthread_cleanup_pop (1);
2283 return rc;
2286 static gpg_error_t
2287 attr_command (assuan_context_t ctx, char *line)
2289 struct client_s *client = assuan_get_pointer (ctx);
2290 gpg_error_t rc;
2291 struct argv_s *args[] = {
2292 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2293 NULL
2296 rc = parse_options (&line, args, client);
2297 if (rc)
2298 return send_error (ctx, rc);
2300 if (client->opts & OPT_INQUIRE)
2302 unsigned char *result;
2303 size_t len;
2305 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2306 if (rc)
2307 return send_error (ctx, rc);
2309 pthread_cleanup_push (xfree, result);
2310 rc = do_attr (ctx, (char *)result);
2311 pthread_cleanup_pop (1);
2313 else
2314 rc = do_attr (ctx, line);
2316 return send_error (ctx, rc);
2319 static gpg_error_t
2320 parse_iscached_opt_lock (void *data, void *value)
2322 struct client_s *client = data;
2324 (void) value;
2325 client->opts |= OPT_LOCK;
2326 return 0;
2329 static gpg_error_t
2330 iscached_command (assuan_context_t ctx, char *line)
2332 struct client_s *client = assuan_get_pointer (ctx);
2333 gpg_error_t rc;
2334 struct argv_s *args[] = {
2335 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2336 NULL
2339 if (!line || !*line)
2340 return send_error (ctx, GPG_ERR_SYNTAX);
2342 rc = parse_options (&line, args, client);
2343 if (rc)
2344 return send_error (ctx, rc);
2345 else if (!valid_filename (line))
2346 return send_error (ctx, GPG_ERR_INV_VALUE);
2348 rc = cache_iscached (line, NULL);
2349 if (client->opts & OPT_LOCK
2350 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2352 unsigned char md5file[16];
2353 gpg_error_t trc = rc;
2355 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2356 if (memcmp (md5file, client->md5file, 16))
2357 cleanup_client (client);
2359 memcpy (client->md5file, md5file, 16);
2360 rc = do_lock (client, 1);
2361 if (!rc)
2362 rc = trc;
2365 return send_error (ctx, rc);
2368 static gpg_error_t
2369 clearcache_command (assuan_context_t ctx, char *line)
2371 gpg_error_t rc = 0, all_rc = 0;
2372 unsigned char md5file[16];
2373 int i;
2374 int t;
2375 int all = 0;
2376 struct client_thread_s *once = NULL;
2378 cache_lock ();
2379 MUTEX_LOCK (&cn_mutex);
2380 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
2382 if (!line || !*line)
2383 all = 1;
2385 t = slist_length (cn_thread_list);
2387 for (i = 0; i < t; i++)
2389 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2390 assuan_peercred_t peer;
2392 if (!thd->cl)
2393 continue;
2395 /* Lock each connected clients' file mutex to prevent any other client
2396 * from accessing the cache entry (the file mutex is locked upon
2397 * command startup). The cache for the entry is not cleared if the
2398 * file mutex is locked by another client to prevent this function
2399 * from blocking.
2401 if (all)
2403 if (thd->cl->filename)
2405 rc = do_validate_peer (ctx, thd->cl->filename, &peer);
2406 all_rc = !all_rc ? rc : all_rc;
2407 if (rc)
2409 rc = 0;
2410 continue;
2414 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
2415 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2417 if (pthread_equal (pthread_self (), thd->tid))
2418 rc = 0;
2419 else
2421 if (!thd->cl->filename ||
2422 cache_iscached (thd->cl->filename,
2423 NULL) == GPG_ERR_NO_DATA)
2425 rc = 0;
2426 continue;
2429 cache_defer_clear (thd->cl->md5file);
2432 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2434 rc = 0;
2435 continue;
2438 if (!rc)
2440 rc = cache_clear (thd->cl->md5file);
2441 cache_unlock_mutex (thd->cl->md5file, 0);
2444 if (rc)
2445 all_rc = rc;
2447 rc = 0;
2449 /* A single data filename was specified. Lock only this data file
2450 * mutex and free the cache entry. */
2451 else
2453 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2454 rc = do_validate_peer (ctx, line, &peer);
2456 if (!rc && !memcmp (thd->cl->md5file, md5file, sizeof (md5file)))
2458 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0,
2459 -1);
2460 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2462 if (pthread_equal (pthread_self (), thd->tid))
2463 rc = 0;
2466 if (!rc)
2468 once = thd;
2469 rc = cache_clear (thd->cl->md5file);
2470 cache_unlock_mutex (thd->cl->md5file, 0);
2472 else
2474 cache_defer_clear (thd->cl->md5file);
2477 break;
2482 /* Only connected clients' cache entries have been cleared. Now clear any
2483 * remaining cache entries without clients but only if there wasn't an
2484 * error from above since this would defeat the locking check of the
2485 * remaining entries. */
2486 if (!all_rc && all)
2488 cache_clear (NULL);
2491 /* No clients are using the specified file. */
2492 else if (!all_rc && !rc && !once)
2494 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2495 rc = cache_clear (md5file);
2498 /* Release the connection mutex. */
2499 pthread_cleanup_pop (1);
2500 cache_unlock ();
2502 if (!rc)
2503 send_status_all (STATUS_CACHE, NULL);
2505 /* One or more files were locked while clearing all cache entries. */
2506 if (all_rc)
2507 rc = all_rc;
2509 return send_error (ctx, rc);
2512 static gpg_error_t
2513 cachetimeout_command (assuan_context_t ctx, char *line)
2515 int timeout;
2516 char **req = str_split (line, " ", 0);
2517 char *p;
2518 gpg_error_t rc = 0;
2519 assuan_peercred_t peer;
2521 if (!req || !*req || !req[1])
2523 strv_free (req);
2524 return send_error (ctx, GPG_ERR_SYNTAX);
2527 errno = 0;
2528 timeout = (int) strtol (req[1], &p, 10);
2529 if (errno != 0 || *p || timeout < -1)
2531 strv_free (req);
2532 return send_error (ctx, GPG_ERR_SYNTAX);
2535 rc = do_validate_peer (ctx, req[0], &peer);
2536 if (!rc)
2538 unsigned char md5file[16];
2540 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, req[0], strlen (req[0]));
2541 rc = cache_set_timeout (md5file, timeout);
2542 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2544 rc = 0;
2545 MUTEX_LOCK (&rcfile_mutex);
2546 config_set_int_param (&global_config, req[0], "cache_timeout",
2547 req[1]);
2548 MUTEX_UNLOCK (&rcfile_mutex);
2552 strv_free (req);
2553 return send_error (ctx, rc);
2556 static gpg_error_t
2557 dump_command (assuan_context_t ctx, char *line)
2559 xmlChar *xml;
2560 int len;
2561 struct client_s *client = assuan_get_pointer (ctx);
2562 gpg_error_t rc;
2564 if (disable_list_and_dump == 1)
2565 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2567 rc = peer_is_invoker(client);
2568 if (rc)
2569 return send_error (ctx, rc);
2571 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2573 if (!xml)
2575 log_write ("%s(%i): %s", __FILE__, __LINE__,
2576 pwmd_strerror (GPG_ERR_ENOMEM));
2577 return send_error (ctx, GPG_ERR_ENOMEM);
2580 pthread_cleanup_push (xmlFree, xml);
2581 rc = xfer_data (ctx, (char *) xml, len);
2582 pthread_cleanup_pop (1);
2583 return send_error (ctx, rc);
2586 static gpg_error_t
2587 getconfig_command (assuan_context_t ctx, char *line)
2589 struct client_s *client = assuan_get_pointer (ctx);
2590 gpg_error_t rc = 0;
2591 char filename[255] = { 0 }, param[747] = { 0 };
2592 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2594 if (!line || !*line)
2595 return send_error (ctx, GPG_ERR_SYNTAX);
2597 if (strchr (line, ' '))
2599 sscanf (line, " %254[^ ] %746c", filename, param);
2600 paramp = param;
2601 fp = filename;
2604 if (fp && !valid_filename (fp))
2605 return send_error (ctx, GPG_ERR_INV_VALUE);
2607 paramp = str_down (paramp);
2608 if (!strcmp (paramp, "cipher") && fp)
2610 struct crypto_s *crypto = NULL;
2612 rc = init_client_crypto (&crypto);
2613 if (!rc)
2615 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2616 if (!rc)
2618 const char *t =
2619 gcry_cipher_algo_name (cipher_to_gcrypt (crypto->hdr.flags));
2620 if (t)
2622 tmp = str_dup (t);
2623 if (tmp)
2624 str_down (tmp);
2629 UPDATE_AGENT_CTX (client, crypto);
2630 cleanup_crypto (&crypto);
2631 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2632 return send_error (ctx, rc);
2634 if (!rc && tmp)
2635 goto done;
2637 else if (!strcmp (paramp, "cipher_iterations") && fp)
2639 struct crypto_s *crypto = NULL;
2641 rc = init_client_crypto (&crypto);
2642 if (!rc)
2644 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2645 if (!rc)
2647 tmp = str_asprintf ("%llu",
2648 (unsigned long long) crypto->hdr.
2649 iterations);
2650 if (!tmp)
2651 rc = GPG_ERR_ENOMEM;
2655 UPDATE_AGENT_CTX (client, crypto);
2656 cleanup_crypto (&crypto);
2657 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2658 return send_error (ctx, rc);
2660 if (!rc && tmp)
2661 goto done;
2663 else if (!strcmp (paramp, "passphrase"))
2664 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2666 p = config_get_value (fp ? fp : "global", paramp);
2667 if (!p)
2668 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2670 tmp = expand_homedir (p);
2671 xfree (p);
2672 if (!tmp)
2674 log_write ("%s(%i): %s", __FILE__, __LINE__,
2675 pwmd_strerror (GPG_ERR_ENOMEM));
2676 return send_error (ctx, GPG_ERR_ENOMEM);
2679 done:
2680 p = tmp;
2681 pthread_cleanup_push (xfree, p);
2682 rc = xfer_data (ctx, p, strlen (p));
2683 pthread_cleanup_pop (1);
2684 return send_error (ctx, rc);
2687 struct xpath_s
2689 xmlXPathContextPtr xp;
2690 xmlXPathObjectPtr result;
2691 xmlBufferPtr buf;
2692 char **req;
2695 static void
2696 xpath_command_cleanup (void *arg)
2698 struct xpath_s *xpath = arg;
2700 if (!xpath)
2701 return;
2703 req_cleanup (xpath->req);
2705 if (xpath->buf)
2706 xmlBufferFree (xpath->buf);
2708 if (xpath->result)
2709 xmlXPathFreeObject (xpath->result);
2711 if (xpath->xp)
2712 xmlXPathFreeContext (xpath->xp);
2715 static gpg_error_t
2716 do_xpath (assuan_context_t ctx, char *line)
2718 gpg_error_t rc;
2719 struct client_s *client = assuan_get_pointer (ctx);
2720 struct xpath_s _x = { 0 };
2721 struct xpath_s *xpath = &_x;
2723 if (!line || !*line)
2724 return GPG_ERR_SYNTAX;
2726 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2728 if (strv_printf (&xpath->req, "%s", line) == 0)
2729 return GPG_ERR_ENOMEM;
2732 xpath->xp = xmlXPathNewContext (client->doc);
2733 if (!xpath->xp)
2735 rc = GPG_ERR_BAD_DATA;
2736 goto fail;
2739 xpath->result =
2740 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2741 if (!xpath->result)
2743 rc = GPG_ERR_BAD_DATA;
2744 goto fail;
2747 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2749 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2750 goto fail;
2753 rc = recurse_xpath_nodeset (client, client->doc, xpath->result->nodesetval,
2754 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2755 NULL);
2756 if (rc)
2757 goto fail;
2758 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2760 rc = GPG_ERR_NO_DATA;
2761 goto fail;
2763 else if (xpath->req[1])
2765 rc = 0;
2766 goto fail;
2769 pthread_cleanup_push (xpath_command_cleanup, &xpath);
2770 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2771 xmlBufferLength (xpath->buf));
2772 pthread_cleanup_pop (0);
2773 fail:
2774 xpath_command_cleanup (xpath);
2775 return rc;
2778 static gpg_error_t
2779 xpath_command (assuan_context_t ctx, char *line)
2781 struct client_s *client = assuan_get_pointer (ctx);
2782 gpg_error_t rc;
2783 struct argv_s *args[] = {
2784 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2785 NULL
2788 if (disable_list_and_dump == 1)
2789 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2791 rc = peer_is_invoker(client);
2792 if (rc)
2793 return send_error (ctx, rc);
2795 rc = parse_options (&line, args, client);
2796 if (rc)
2797 return send_error (ctx, rc);
2799 if (client->opts & OPT_INQUIRE)
2801 unsigned char *result;
2802 size_t len;
2804 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2805 if (rc)
2806 return send_error (ctx, rc);
2808 pthread_cleanup_push (xfree, result);
2809 rc = do_xpath (ctx, (char *)result);
2810 pthread_cleanup_pop (1);
2812 else
2813 rc = do_xpath (ctx, line);
2815 return send_error (ctx, rc);
2818 static gpg_error_t
2819 do_xpathattr (assuan_context_t ctx, char *line)
2821 struct client_s *client = assuan_get_pointer (ctx);
2822 gpg_error_t rc;
2823 char **req = NULL;
2824 int cmd = 0; //SET
2825 struct xpath_s _x = { 0 };
2826 struct xpath_s *xpath = &_x;
2828 if (!line || !*line)
2829 return GPG_ERR_SYNTAX;
2831 if ((req = str_split (line, " ", 3)) == NULL)
2832 return GPG_ERR_ENOMEM;
2834 if (!req[0])
2836 rc = GPG_ERR_SYNTAX;
2837 goto fail;
2840 if (!strcasecmp (req[0], "SET"))
2841 cmd = 0;
2842 else if (!strcasecmp (req[0], "DELETE"))
2843 cmd = 1;
2844 else
2846 rc = GPG_ERR_SYNTAX;
2847 goto fail;
2850 if (!req[1] || !req[2])
2852 rc = GPG_ERR_SYNTAX;
2853 goto fail;
2856 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2858 rc = GPG_ERR_ENOMEM;
2859 goto fail;
2862 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2864 rc = GPG_ERR_SYNTAX;
2865 goto fail;
2868 xpath->xp = xmlXPathNewContext (client->doc);
2869 if (!xpath->xp)
2871 rc = GPG_ERR_BAD_DATA;
2872 goto fail;
2875 xpath->result = xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2876 if (!xpath->result)
2878 rc = GPG_ERR_BAD_DATA;
2879 goto fail;
2882 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2884 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2885 goto fail;
2888 rc = recurse_xpath_nodeset (client, client->doc, xpath->result->nodesetval,
2889 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2890 (xmlChar *) req[1]);
2892 fail:
2893 xpath_command_cleanup (xpath);
2894 strv_free (req);
2895 return rc;
2898 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2899 static gpg_error_t
2900 xpathattr_command (assuan_context_t ctx, char *line)
2902 struct client_s *client = assuan_get_pointer (ctx);
2903 gpg_error_t rc;
2904 struct argv_s *args[] = {
2905 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2906 NULL
2909 if (disable_list_and_dump == 1)
2910 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2912 rc = peer_is_invoker(client);
2913 if (rc)
2914 return send_error (ctx, rc);
2916 rc = parse_options (&line, args, client);
2917 if (rc)
2918 return send_error (ctx, rc);
2920 if (client->opts & OPT_INQUIRE)
2922 unsigned char *result;
2923 size_t len;
2925 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2926 if (rc)
2927 return send_error (ctx, rc);
2929 pthread_cleanup_push (xfree, result);
2930 rc = do_xpathattr (ctx, (char *)result);
2931 pthread_cleanup_pop (1);
2933 else
2934 rc = do_xpathattr (ctx, line);
2936 return send_error (ctx, rc);
2939 static gpg_error_t
2940 do_import (struct client_s *client, const char *root_element,
2941 unsigned char *content)
2943 char **dst_path = NULL;
2944 xmlDocPtr doc = NULL;
2945 xmlNodePtr n, root, copy;
2946 gpg_error_t rc;
2948 if (!content || !*content)
2950 xfree (content);
2951 return GPG_ERR_SYNTAX;
2954 if (root_element)
2955 dst_path = str_split (root_element, "\t", 0);
2957 if (dst_path && !valid_element_path (dst_path, 0))
2959 if (dst_path)
2960 strv_free (dst_path);
2962 return GPG_ERR_INV_VALUE;
2965 struct string_s *str = string_new_content ((char *)content);
2966 str = string_prepend (str, "<pwmd>");
2967 str = string_append (str, "</pwmd>");
2968 doc = xmlReadDoc ((xmlChar *) str->str, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2969 string_free (str, 1);
2970 if (!doc)
2972 rc = GPG_ERR_BAD_DATA;
2973 goto fail;
2976 root = xmlDocGetRootElement (doc);
2977 xmlNodePtr root_orig = root->children;
2978 root = root->children;
2979 rc = validate_import (client, root);
2980 if (rc)
2981 goto fail;
2985 again:
2986 if (dst_path)
2988 char **path = strv_dup (dst_path);
2989 if (!path)
2991 log_write ("%s(%i): %s", __FILE__, __LINE__,
2992 pwmd_strerror (GPG_ERR_ENOMEM));
2993 rc = GPG_ERR_ENOMEM;
2994 goto fail;
2997 xmlChar *a = xmlGetProp (root, (xmlChar *) "_name");
2998 if (!a)
3000 strv_free (path);
3001 rc = GPG_ERR_INV_VALUE;
3002 goto fail;
3005 if (strv_printf (&path, "%s", (char *) a) == 0)
3007 xmlFree (a);
3008 rc = GPG_ERR_ENOMEM;
3009 goto fail;
3012 xmlFree (a);
3013 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
3014 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3016 strv_free (path);
3017 goto fail;
3020 if (!rc)
3022 n = find_elements (client, client->doc, n->children, path + 1, &rc,
3023 NULL, NULL, NULL, 0, 0, NULL, 1);
3024 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3026 strv_free (path);
3027 goto fail;
3029 else if (!rc)
3031 xmlUnlinkNode (n);
3032 xmlFreeNode (n);
3033 strv_free (path);
3034 path = NULL;
3035 goto again;
3039 if (rc == GPG_ERR_ELEMENT_NOT_FOUND)
3041 n = create_element_path (client, &path, &rc, NULL);
3042 if (rc)
3043 goto fail;
3046 if (root->children)
3048 copy = xmlCopyNodeList (root->children);
3049 n = xmlAddChildList (n, copy);
3050 if (!n)
3051 rc = GPG_ERR_ENOMEM;
3054 strv_free (path);
3056 else
3058 char **path = NULL;
3060 /* Check if the content root element can create a DTD root element. */
3061 if (!xmlStrEqual ((xmlChar *) "element", root->name))
3063 rc = GPG_ERR_SYNTAX;
3064 goto fail;
3067 xmlChar *a;
3069 if ((a = xmlGetProp (root, (xmlChar *) "_name")) == NULL)
3071 rc = GPG_ERR_SYNTAX;
3072 goto fail;
3075 char *tmp = str_dup ((char *) a);
3076 xmlFree (a);
3077 int literal = is_literal_element (&tmp);
3079 if (!valid_xml_element ((xmlChar *) tmp) || literal)
3081 xfree (tmp);
3082 rc = GPG_ERR_INV_VALUE;
3083 goto fail;
3086 if (strv_printf (&path, "%s", tmp) == 0)
3088 xfree (tmp);
3089 rc = GPG_ERR_ENOMEM;
3090 goto fail;
3093 xfree (tmp);
3094 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 1);
3095 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3097 rc = GPG_ERR_BAD_DATA;
3098 goto fail;
3101 /* Overwriting the existing tree. */
3102 if (!rc)
3104 xmlUnlinkNode (n);
3105 xmlFreeNodeList (n);
3108 rc = 0;
3109 xmlSetProp (root, (xmlChar *) "_name", (xmlChar *) path[0]);
3110 n = xmlCopyNode (root, 1);
3111 n = xmlAddChildList (xmlDocGetRootElement (client->doc), n);
3112 strv_free (path);
3115 if (n && !rc)
3116 rc = update_element_mtime (client, n->parent);
3118 for (root = root_orig->next; root; root = root->next)
3120 if (root->type == XML_ELEMENT_NODE)
3121 break;
3124 root_orig = root;
3126 while (root);
3128 fail:
3129 if (doc)
3130 xmlFreeDoc (doc);
3132 if (dst_path)
3133 strv_free (dst_path);
3135 return rc;
3138 static gpg_error_t
3139 parse_import_opt_root (void *data, void *value)
3141 struct client_s *client = data;
3143 client->import_root = str_dup (value);
3144 return client->import_root ? 0 : GPG_ERR_ENOMEM;
3147 static gpg_error_t
3148 import_command (assuan_context_t ctx, char *line)
3150 gpg_error_t rc;
3151 struct client_s *client = assuan_get_pointer (ctx);
3152 unsigned char *result;
3153 size_t len;
3154 struct argv_s *args[] = {
3155 &(struct argv_s) {"root", OPTION_TYPE_ARG, parse_import_opt_root},
3156 NULL
3159 xfree (client->import_root);
3160 client->import_root = NULL;
3161 rc = parse_options (&line, args, client);
3162 if (rc)
3163 return send_error (ctx, rc);
3165 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3166 if (rc)
3168 xfree (client->import_root);
3169 client->import_root = NULL;
3170 return send_error (ctx, rc);
3173 rc = do_import (client, client->import_root, result);
3174 xfree (client->import_root);
3175 client->import_root = NULL;
3176 return send_error (ctx, rc);
3179 static gpg_error_t
3180 do_lock (struct client_s *client, int add)
3182 gpg_error_t rc = lock_file_mutex (client, add);
3184 if (!rc)
3185 client->flags |= FLAG_LOCK_CMD;
3187 return rc;
3190 static gpg_error_t
3191 lock_command (assuan_context_t ctx, char *line)
3193 struct client_s *client = assuan_get_pointer (ctx);
3194 gpg_error_t rc = do_lock (client, 0);
3196 return send_error (ctx, rc);
3199 static gpg_error_t
3200 unlock_command (assuan_context_t ctx, char *line)
3202 struct client_s *client = assuan_get_pointer (ctx);
3203 gpg_error_t rc;
3205 rc = unlock_file_mutex (client, 0);
3206 return send_error (ctx, rc);
3209 static gpg_error_t
3210 option_command (assuan_context_t ctx, char *line)
3212 struct client_s *client = assuan_get_pointer (ctx);
3213 gpg_error_t rc = 0;
3214 struct pinentry_option_s *pin_opts = &client->pinentry_opts;
3215 #ifdef WITH_AGENT
3216 struct agent_s *agent = client->crypto->agent;
3217 #endif
3218 char namebuf[255] = { 0 };
3219 char *name = namebuf;
3220 char *value = NULL, *p;
3222 p = strchr (line, '=');
3223 if (!p)
3224 strncpy (namebuf, line, sizeof(namebuf));
3225 else
3227 strncpy (namebuf, line, strlen (line)-strlen (p));
3228 value = p+1;
3231 log_write1 ("OPTION name='%s' value='%s'", name, value);
3233 if (strcasecmp (name, (char *) "log_level") == 0)
3235 long l = 0;
3237 if (value)
3239 l = strtol (value, NULL, 10);
3241 if (l < 0 || l > 2)
3242 return send_error (ctx, GPG_ERR_INV_VALUE);
3245 MUTEX_LOCK (&rcfile_mutex);
3246 config_set_int_param (&global_config, "global", "log_level", value);
3247 MUTEX_UNLOCK (&rcfile_mutex);
3248 goto done;
3250 else if (strcasecmp (name, (char *) "lock-timeout") == 0)
3252 long n = 0;
3253 char *p = NULL;
3255 if (value)
3257 n = strtol (value, &p, 10);
3258 if (p && *p)
3259 return send_error (ctx, GPG_ERR_INV_VALUE);
3262 client->lock_timeout = n;
3263 goto done;
3265 else if (strcasecmp (name, (char *) "NAME") == 0)
3267 char *tmp = pthread_getspecific (thread_name_key);
3269 if (tmp)
3270 xfree (tmp);
3272 MUTEX_LOCK (&cn_mutex);
3273 xfree (client->thd->name);
3274 client->thd->name = NULL;
3276 if (!value || !*value)
3277 pthread_setspecific (thread_name_key, str_dup (""));
3278 else
3280 client->thd->name = str_dup (value);
3281 pthread_setspecific (thread_name_key, str_dup (value));
3284 MUTEX_UNLOCK (&cn_mutex);
3285 goto done;
3287 else if (strcasecmp (name, (char *) "lc-messages") == 0)
3289 xfree (pin_opts->lc_messages);
3290 pin_opts->lc_messages = NULL;
3291 if (value && *value)
3292 pin_opts->lc_messages = str_dup (value);
3293 #ifdef WITH_AGENT
3294 if (use_agent)
3295 rc = set_agent_option (client->crypto->agent, "lc-messages", value);
3296 #endif
3298 else if (strcasecmp (name, (char *) "lc-ctype") == 0)
3300 xfree (pin_opts->lc_ctype);
3301 pin_opts->lc_ctype = NULL;
3302 if (value && *value)
3303 pin_opts->lc_ctype = str_dup (value);
3304 #ifdef WITH_AGENT
3305 if (use_agent)
3306 rc = set_agent_option (client->crypto->agent, "lc-ctype", value);
3307 #endif
3309 else if (strcasecmp (name, (char *) "ttyname") == 0)
3311 xfree (pin_opts->ttyname);
3312 pin_opts->ttyname = NULL;
3313 if (value && *value)
3314 pin_opts->ttyname = str_dup (value);
3315 #ifdef WITH_AGENT
3316 if (use_agent)
3317 rc = set_agent_option (client->crypto->agent, "ttyname", value);
3318 #endif
3320 else if (strcasecmp (name, (char *) "ttytype") == 0)
3322 xfree (pin_opts->ttytype);
3323 pin_opts->ttytype = NULL;
3324 if (value && *value)
3325 pin_opts->ttytype = str_dup (value);
3326 #ifdef WITH_AGENT
3327 if (use_agent)
3328 rc = set_agent_option (client->crypto->agent, "ttytype", value);
3329 #endif
3331 else if (strcasecmp (name, (char *) "display") == 0)
3333 xfree (pin_opts->display);
3334 pin_opts->display = NULL;
3335 if (value && *value)
3336 pin_opts->display = str_dup (value);
3337 #ifdef WITH_AGENT
3338 if (use_agent)
3339 rc = set_agent_option (client->crypto->agent, "display", value);
3340 #endif
3342 else if (strcasecmp (name, (char *) "pinentry-desc") == 0)
3344 xfree (pin_opts->desc);
3345 pin_opts->desc = NULL;
3346 if (value && *value)
3347 pin_opts->desc = str_dup (value);
3349 else if (strcasecmp (name, (char *) "pinentry-title") == 0)
3351 xfree (pin_opts->title);
3352 pin_opts->title = NULL;
3353 if (value && *value)
3354 pin_opts->title = str_dup (value);
3356 else if (strcasecmp (name, (char *) "pinentry-prompt") == 0)
3358 xfree (pin_opts->prompt);
3359 pin_opts->prompt = NULL;
3360 if (value && *value)
3361 pin_opts->prompt = str_dup (value);
3364 else if (strcasecmp (name, "pinentry-timeout") == 0)
3366 char *p = NULL;
3367 int n;
3369 if (!value)
3370 goto done;
3372 n = (int) strtol (value, &p, 10);
3374 if (*p || n < 0)
3375 return send_error (ctx, GPG_ERR_INV_VALUE);
3377 pin_opts->timeout = n;
3378 MUTEX_LOCK (&rcfile_mutex);
3379 config_set_int_param (&global_config,
3380 client->filename ? client->filename : "global",
3381 "pinentry_timeout", value);
3382 MUTEX_UNLOCK (&rcfile_mutex);
3383 goto done;
3385 else if (strcasecmp (name, "disable-pinentry") == 0)
3387 char *p = NULL;
3388 int n = 1;
3390 if (value && *value)
3392 n = (int) strtol (value, &p, 10);
3393 if (*p || n < 0 || n > 1)
3394 return send_error (ctx, GPG_ERR_INV_VALUE);
3397 if (n)
3398 client->flags |= FLAG_NO_PINENTRY;
3399 else
3400 client->flags &= ~FLAG_NO_PINENTRY;
3402 #ifdef WITH_AGENT
3403 if (use_agent)
3405 if (client->flags & FLAG_NO_PINENTRY)
3406 rc = set_agent_option (client->crypto->agent, "pinentry-mode",
3407 "loopback");
3408 else
3409 rc = set_agent_option (client->crypto->agent, "pinentry-mode",
3410 "ask");
3412 if (rc)
3413 return send_error (ctx, rc);
3415 #endif
3417 else
3418 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
3420 done:
3421 #ifdef WITH_AGENT
3422 if (!rc && use_agent && agent)
3424 rc = pinentry_merge_options (&client->crypto->agent->pinentry_opts,
3425 pin_opts);
3427 #endif
3429 return send_error (ctx, rc);
3432 static gpg_error_t
3433 do_rename (assuan_context_t ctx, char *line)
3435 struct client_s *client = assuan_get_pointer (ctx);
3436 gpg_error_t rc;
3437 char **req, **src, *dst;
3438 xmlNodePtr n, ndst;
3440 req = str_split (line, " ", 0);
3442 if (!req || !req[0] || !req[1])
3444 strv_free (req);
3445 return GPG_ERR_SYNTAX;
3448 dst = req[1];
3449 is_literal_element (&dst);
3451 if (!valid_xml_element ((xmlChar *) dst))
3453 strv_free (req);
3454 return GPG_ERR_INV_VALUE;
3457 if (strchr (req[0], '\t'))
3458 src = str_split (req[0], "\t", 0);
3459 else
3460 src = str_split (req[0], " ", 0);
3462 if (!src || !*src)
3464 rc = GPG_ERR_SYNTAX;
3465 goto fail;
3468 n = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3469 if (src[1] && n)
3470 n = find_elements (client, client->doc, n->children, src + 1, &rc, NULL, NULL,
3471 NULL, 0, 0, NULL, 0);
3473 if (!n)
3474 goto fail;
3476 rc = is_element_owner (client, n);
3477 if (rc)
3478 goto fail;
3480 xmlChar *a = xmlGetProp (n, (xmlChar *) "_name");
3481 if (!a)
3483 rc = GPG_ERR_ENOMEM;
3484 goto fail;
3487 /* To prevent unwanted effects:
3489 * <root name="a"><b/></root>
3491 * RENAME a<TAB>b b
3493 if (xmlStrEqual (a, (xmlChar *) dst))
3495 xmlFree (a);
3496 rc = GPG_ERR_AMBIGUOUS_NAME;
3497 goto fail;
3500 xmlFree (a);
3501 char **tmp = NULL;
3502 if (src[1])
3504 char **p;
3506 for (p = src; *p; p++)
3508 if (!*(p + 1))
3509 break;
3510 strv_printf (&tmp, "%s", *p);
3514 strv_printf (&tmp, "!%s", dst);
3515 ndst = find_root_element (client, client->doc, &tmp, &rc, NULL, 0, 0);
3516 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3518 strv_free (tmp);
3519 goto fail;
3522 if (tmp[1] && ndst)
3523 ndst = find_elements (client, client->doc, ndst->children, tmp + 1, &rc, NULL,
3524 NULL, NULL, 0, 0, NULL, 0);
3526 strv_free (tmp);
3527 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3528 goto fail;
3530 rc = 0;
3532 /* Target may exist:
3534 * <root name="a"/>
3535 * <root name="b" target="a"/>
3537 * RENAME b a
3539 * Would need to do:
3540 * RENAME !b a
3542 if (ndst == n)
3544 rc = GPG_ERR_AMBIGUOUS_NAME;
3545 goto fail;
3548 if (ndst)
3550 rc = is_element_owner (client, ndst);
3551 if (rc)
3552 goto fail;
3554 unlink_node (client, ndst);
3555 xmlFreeNodeList (ndst);
3558 rc = add_attribute (client, n, "_name", dst);
3560 fail:
3561 strv_free (req);
3562 strv_free (src);
3563 return rc;
3566 static gpg_error_t
3567 rename_command (assuan_context_t ctx, char *line)
3569 struct client_s *client = assuan_get_pointer (ctx);
3570 gpg_error_t rc;
3571 struct argv_s *args[] = {
3572 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3573 NULL
3576 rc = parse_options (&line, args, client);
3577 if (rc)
3578 return send_error (ctx, rc);
3580 if (client->opts & OPT_INQUIRE)
3582 unsigned char *result;
3583 size_t len;
3585 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3586 if (rc)
3587 return send_error (ctx, rc);
3589 pthread_cleanup_push (xfree, result);
3590 rc = do_rename (ctx, (char *)result);
3591 pthread_cleanup_pop (1);
3593 else
3594 rc = do_rename (ctx, line);
3596 return send_error (ctx, rc);
3599 static gpg_error_t
3600 do_copy (assuan_context_t ctx, char *line)
3602 struct client_s *client = assuan_get_pointer (ctx);
3603 gpg_error_t rc;
3604 char **req, **src = NULL, **dst = NULL;
3605 xmlNodePtr nsrc, ndst, new = NULL;
3607 req = str_split (line, " ", 0);
3608 if (!req || !req[0] || !req[1])
3610 strv_free (req);
3611 return GPG_ERR_SYNTAX;
3614 if (strchr (req[0], '\t'))
3615 src = str_split (req[0], "\t", 0);
3616 else
3617 src = str_split (req[0], " ", 0);
3619 if (!src || !*src)
3621 rc = GPG_ERR_SYNTAX;
3622 goto fail;
3625 if (strchr (req[1], '\t'))
3626 dst = str_split (req[1], "\t", 0);
3627 else
3628 dst = str_split (req[1], " ", 0);
3630 if (!dst || !*dst)
3632 rc = GPG_ERR_SYNTAX;
3633 goto fail;
3636 if (!valid_element_path (dst, 0))
3638 rc = GPG_ERR_INV_VALUE;
3639 goto fail;
3642 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3643 if (nsrc && src[1])
3644 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc, NULL,
3645 NULL, NULL, 0, 0, NULL, 0);
3647 if (!nsrc)
3648 goto fail;
3650 new = xmlCopyNodeList (nsrc);
3651 if (!new)
3653 rc = GPG_ERR_ENOMEM;
3654 goto fail;
3657 int create = 0;
3658 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3659 if (ndst && dst[1])
3661 if (ndst->children)
3662 ndst = find_elements (client, client->doc, ndst->children, dst + 1, &rc, NULL,
3663 NULL, create_target_elements_cb, 0, 0, NULL, 0);
3664 else
3665 create = 1;
3667 else
3668 create = 1;
3670 if (!ndst && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3671 goto fail;
3672 else if (!ndst || create)
3674 ndst = create_element_path (client, &dst, &rc, NULL);
3675 if (!ndst)
3676 goto fail;
3679 rc = is_element_owner (client, ndst);
3680 if (rc)
3681 goto fail;
3683 /* Merge any attributes from the src node to the initial dst node. */
3684 for (xmlAttrPtr attr = new->properties; attr; attr = attr->next)
3686 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3687 continue;
3689 xmlAttrPtr a = xmlHasProp (ndst, attr->name);
3690 if (a)
3691 xmlRemoveProp (a);
3693 xmlChar *tmp = xmlNodeGetContent (attr->children);
3694 xmlNewProp (ndst, attr->name, tmp);
3695 xmlFree (tmp);
3696 rc = add_attribute (client, ndst, NULL, NULL);
3699 xmlNodePtr n = ndst->children;
3700 xmlUnlinkNode (n);
3701 xmlFreeNodeList (n);
3702 ndst->children = NULL;
3704 if (new->children)
3706 n = xmlCopyNodeList (new->children);
3707 if (!n)
3709 rc = GPG_ERR_ENOMEM;
3710 goto fail;
3713 n = xmlAddChildList (ndst, n);
3714 if (!n)
3716 rc = GPG_ERR_ENOMEM;
3717 goto fail;
3720 rc = update_element_mtime (client, xmlDocGetRootElement (client->doc) ==
3721 ndst->parent ? ndst : ndst->parent);
3724 fail:
3725 if (new)
3727 xmlUnlinkNode (new);
3728 xmlFreeNodeList (new);
3731 if (req)
3732 strv_free (req);
3734 if (src)
3735 strv_free (src);
3737 if (dst)
3738 strv_free (dst);
3740 return rc;
3743 static gpg_error_t
3744 copy_command (assuan_context_t ctx, char *line)
3746 struct client_s *client = assuan_get_pointer (ctx);
3747 gpg_error_t rc;
3748 struct argv_s *args[] = {
3749 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3750 NULL
3753 rc = parse_options (&line, args, client);
3754 if (rc)
3755 return send_error (ctx, rc);
3757 if (client->opts & OPT_INQUIRE)
3759 unsigned char *result;
3760 size_t len;
3762 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3763 if (rc)
3764 return send_error (ctx, rc);
3766 pthread_cleanup_push (xfree, result);
3767 rc = do_copy (ctx, (char *)result);
3768 pthread_cleanup_pop (1);
3770 else
3771 rc = do_copy (ctx, line);
3773 return send_error (ctx, rc);
3776 static gpg_error_t
3777 do_move (assuan_context_t ctx, char *line)
3779 struct client_s *client = assuan_get_pointer (ctx);
3780 gpg_error_t rc;
3781 char **req, **src = NULL, **dst = NULL;
3782 xmlNodePtr nsrc, ndst = NULL;
3784 req = str_split (line, " ", 0);
3786 if (!req || !req[0] || !req[1])
3788 strv_free (req);
3789 return GPG_ERR_SYNTAX;
3792 if (strchr (req[0], '\t'))
3793 src = str_split (req[0], "\t", 0);
3794 else
3795 src = str_split (req[0], " ", 0);
3797 if (!src || !*src)
3799 rc = GPG_ERR_SYNTAX;
3800 goto fail;
3803 if (strchr (req[1], '\t'))
3804 dst = str_split (req[1], "\t", 0);
3805 else
3806 dst = str_split (req[1], " ", 0);
3808 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3809 if (nsrc && src[1])
3810 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc,
3811 NULL, NULL, NULL, 0, 0, NULL, 0);
3813 if (!nsrc)
3814 goto fail;
3816 rc = is_element_owner (client, nsrc);
3817 if (rc)
3818 goto fail;
3820 if (dst)
3822 if (!valid_element_path (dst, 0))
3824 rc = GPG_ERR_INV_VALUE;
3825 goto fail;
3828 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3829 if (ndst && dst[1])
3830 ndst = find_elements (client, client->doc, ndst->children, dst + 1,
3831 &rc, NULL, NULL, NULL, 0, 0, NULL, 0);
3833 else
3834 ndst = xmlDocGetRootElement (client->doc);
3836 for (xmlNodePtr n = ndst; n; n = n->parent)
3838 if (n == nsrc)
3840 rc = GPG_ERR_CONFLICT;
3841 goto fail;
3845 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3846 goto fail;
3848 rc = 0;
3850 if (ndst)
3852 xmlChar *a = node_has_attribute (nsrc, (xmlChar *) "_name");
3854 xmlNodePtr dup = find_element (client, ndst->children, (char *) a,
3855 NULL, &rc);
3856 xmlFree (a);
3858 if (rc)
3859 goto fail;
3861 if (dup)
3863 if (dup == nsrc)
3864 goto fail;
3866 if (ndst == xmlDocGetRootElement (client->doc))
3868 xmlNodePtr n = nsrc;
3869 int match = 0;
3871 while (n->parent && n->parent != ndst)
3872 n = n->parent;
3874 xmlChar *a = node_has_attribute (n, (xmlChar *) "_name");
3875 xmlChar *b = node_has_attribute (nsrc, (xmlChar *) "_name");
3877 if (xmlStrEqual (a, b))
3879 match = 1;
3880 xmlUnlinkNode (nsrc);
3881 xmlUnlinkNode (n);
3882 xmlFreeNodeList (n);
3885 xmlFree (a);
3886 xmlFree (b);
3888 if (!match)
3890 xmlUnlinkNode (dup);
3891 xmlFreeNodeList (dup);
3894 else
3895 xmlUnlinkNode (dup);
3899 if (!ndst && dst)
3901 xmlChar *name = node_has_attribute (nsrc, (xmlChar *) "_name");
3903 if (nsrc->parent == xmlDocGetRootElement (client->doc)
3904 && !strcmp ((char *) name, *dst))
3906 xmlFree (name);
3907 rc = GPG_ERR_CONFLICT;
3908 goto fail;
3911 xmlFree (name);
3912 ndst = create_element_path (client, &dst, &rc, nsrc);
3915 if (!ndst)
3916 goto fail;
3918 update_element_mtime (client, nsrc->parent);
3919 xmlUnlinkNode (nsrc);
3920 ndst = xmlAddChildList (ndst, nsrc);
3922 if (!ndst)
3923 rc = GPG_ERR_ENOMEM;
3924 else
3925 update_element_mtime (client, ndst->parent);
3927 fail:
3928 if (req)
3929 strv_free (req);
3931 if (src)
3932 strv_free (src);
3934 if (dst)
3935 strv_free (dst);
3937 return rc;
3940 static gpg_error_t
3941 move_command (assuan_context_t ctx, char *line)
3943 struct client_s *client = assuan_get_pointer (ctx);
3944 gpg_error_t rc;
3945 struct argv_s *args[] = {
3946 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3947 NULL
3950 rc = parse_options (&line, args, client);
3951 if (rc)
3952 return send_error (ctx, rc);
3954 if (client->opts & OPT_INQUIRE)
3956 unsigned char *result;
3957 size_t len;
3959 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3960 if (rc)
3961 return send_error (ctx, rc);
3963 pthread_cleanup_push (xfree, result);
3964 rc = do_move (ctx, (char *)result);
3965 pthread_cleanup_pop (1);
3967 else
3968 rc = do_move (ctx, line);
3970 return send_error (ctx, rc);
3973 static gpg_error_t
3974 ls_command (assuan_context_t ctx, char *line)
3976 gpg_error_t rc;
3977 char *tmp = str_asprintf ("%s/data", homedir);
3978 char *dir = expand_homedir (tmp);
3979 DIR *d = opendir (dir);
3981 rc = gpg_error_from_errno (errno);
3982 xfree (tmp);
3984 if (!d)
3986 xfree (dir);
3987 return send_error (ctx, rc);
3990 size_t len =
3991 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3992 struct dirent *p = xmalloc (len), *cur = NULL;
3993 char *list = NULL;
3995 xfree (dir);
3996 pthread_cleanup_push (xfree, p);
3997 pthread_cleanup_push (closedir, d);
3998 rc = 0;
4000 while (!readdir_r (d, p, &cur) && cur)
4002 if (cur->d_name[0] == '.' && cur->d_name[1] == '\0')
4003 continue;
4004 else if (cur->d_name[0] == '.' && cur->d_name[1] == '.'
4005 && cur->d_name[2] == '\0')
4006 continue;
4008 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
4010 if (!tmp)
4012 if (list)
4013 xfree (list);
4015 rc = GPG_ERR_ENOMEM;
4016 break;
4019 xfree (list);
4020 list = tmp;
4023 pthread_cleanup_pop (1); // closedir (d)
4024 pthread_cleanup_pop (1); // xfree (p)
4026 if (rc)
4027 return send_error (ctx, rc);
4029 if (!list)
4030 return send_error (ctx, GPG_ERR_NO_DATA);
4032 list[strlen (list) - 1] = 0;
4033 pthread_cleanup_push (xfree, list);
4034 rc = xfer_data (ctx, list, strlen (list));
4035 pthread_cleanup_pop (1);
4036 return send_error (ctx, rc);
4039 static gpg_error_t
4040 bye_notify (assuan_context_t ctx, char *line)
4042 struct client_s *cl = assuan_get_pointer (ctx);
4044 #ifdef WITH_GNUTLS
4045 if (cl->thd->remote)
4047 int rc;
4051 struct timeval tv = { 0, 50000 };
4053 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
4054 if (rc == GNUTLS_E_AGAIN)
4055 select (0, NULL, NULL, NULL, &tv);
4057 while (rc == GNUTLS_E_AGAIN);
4059 #endif
4061 /* This will let assuan_process_next() return. */
4062 fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK);
4063 cl->last_rc = 0; // BYE command result
4064 return 0;
4067 static gpg_error_t
4068 reset_notify (assuan_context_t ctx, char *line)
4070 struct client_s *client = assuan_get_pointer (ctx);
4072 if (client)
4073 cleanup_client (client);
4075 return 0;
4079 * This is called before every Assuan command.
4081 static gpg_error_t
4082 command_startup (assuan_context_t ctx, const char *name)
4084 struct client_s *client = assuan_get_pointer (ctx);
4085 gpg_error_t rc;
4086 struct command_table_s *cmd = NULL;
4088 log_write1 ("command='%s'", name);
4089 client->last_rc = client->opts = 0;
4091 for (int i = 0; command_table[i]; i++)
4093 if (!strcasecmp (name, command_table[i]->name))
4095 if (command_table[i]->ignore_startup)
4096 return 0;
4097 cmd = command_table[i];
4098 break;
4102 client->last_rc = rc = gpg_error (file_modified (client, cmd));
4103 return rc;
4107 * This is called after every Assuan command.
4109 static void
4110 command_finalize (assuan_context_t ctx, gpg_error_t rc)
4112 struct client_s *client = assuan_get_pointer (ctx);
4114 if (!(client->flags & FLAG_LOCK_CMD))
4115 unlock_file_mutex (client, 0);
4117 log_write1 (_("command completed: rc=%u"), rc ? rc : client->last_rc);
4118 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
4119 #ifdef WITH_GNUTLS
4120 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
4121 #endif
4124 static gpg_error_t
4125 help_command (assuan_context_t ctx, char *line)
4127 gpg_error_t rc;
4128 int i;
4130 if (!line || !*line)
4132 char *tmp;
4133 char *help = str_dup (_("Usage: HELP [<COMMAND>]\n"
4134 "For commands that take an element path as an argument, each element is "
4135 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
4136 "\n" "COMMANDS:"));
4138 for (i = 0; command_table[i]; i++)
4140 if (!command_table[i]->help)
4141 continue;
4143 tmp = str_asprintf ("%s %s", help, command_table[i]->name);
4144 xfree (help);
4145 help = tmp;
4148 tmp = strip_texi_and_wrap (help);
4149 xfree (help);
4150 pthread_cleanup_push (xfree, tmp);
4151 rc = xfer_data (ctx, tmp, strlen (tmp));
4152 pthread_cleanup_pop (1);
4153 return send_error (ctx, rc);
4156 for (i = 0; command_table[i]; i++)
4158 if (!strcasecmp (line, command_table[i]->name))
4160 char *help, *tmp;
4162 if (!command_table[i]->help)
4163 break;
4165 help = strip_texi_and_wrap (command_table[i]->help);
4166 tmp = str_asprintf (_("Usage: %s"), help);
4167 xfree (help);
4168 pthread_cleanup_push (xfree, tmp);
4169 rc = xfer_data (ctx, tmp, strlen (tmp));
4170 pthread_cleanup_pop (1);
4171 return send_error (ctx, rc);
4175 return send_error (ctx, GPG_ERR_INV_NAME);
4178 static void
4179 new_command (const char *name, int ignore, int unlock,
4180 gpg_error_t (*handler) (assuan_context_t, char *),
4181 const char *help)
4183 int i = 0;
4185 if (command_table)
4186 for (i = 0; command_table[i]; i++);
4188 command_table =
4189 xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
4190 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
4191 command_table[i]->name = name;
4192 command_table[i]->handler = handler;
4193 command_table[i]->ignore_startup = ignore;
4194 command_table[i]->unlock = unlock;
4195 command_table[i++]->help = help;
4196 command_table[i] = NULL;
4199 void
4200 deinit_commands ()
4202 int i;
4204 for (i = 0; command_table[i]; i++)
4205 xfree (command_table[i]);
4207 xfree (command_table);
4210 static int
4211 sort_commands (const void *arg1, const void *arg2)
4213 struct command_table_s *const *a = arg1;
4214 struct command_table_s *const *b = arg2;
4216 if (!*a || !*b)
4217 return 0;
4218 else if (*a && !*b)
4219 return 1;
4220 else if (!*a && *b)
4221 return -1;
4223 return strcmp ((*a)->name, (*b)->name);
4226 // FIXME cleanup/implement options
4227 static gpg_error_t
4228 passwd_command (assuan_context_t ctx, char *line)
4230 struct client_s *client = assuan_get_pointer (ctx);
4231 gpg_error_t rc;
4232 struct argv_s *args[] = {
4233 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
4234 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
4235 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG, parse_opt_no_passphrase},
4236 NULL
4239 rc = peer_is_invoker (client);
4240 if (rc == GPG_ERR_EACCES)
4241 return send_error (ctx, rc);
4243 if (client->flags & FLAG_NEW)
4244 return send_error (ctx, GPG_ERR_INV_STATE);
4246 client->crypto->save.s2k_count =
4247 config_get_ulong (client->filename, "s2k_count");
4248 rc = parse_options (&line, args, client);
4249 if (rc)
4250 return send_error (ctx, rc);
4252 if (!rc && client->opts & OPT_RESET)
4254 rc = cache_clear (client->md5file);
4255 if (!rc)
4256 send_status_all (STATUS_CACHE, NULL);
4259 if (!rc)
4261 if (!IS_PKI (client->crypto))
4263 struct crypto_s *crypto;
4265 xfree (client->crypto->filename);
4266 client->crypto->filename = str_dup (client->filename);
4267 rc = change_passwd (ctx, client->filename,
4268 client->flags & FLAG_NO_PINENTRY, &crypto,
4269 (client->opts & OPT_NO_PASSPHRASE));
4270 if (!rc)
4272 cleanup_crypto (&client->crypto);
4273 client->crypto = crypto;
4274 update_checksum (client);
4275 cleanup_crypto_stage1 (client->crypto);
4278 #ifdef WITH_AGENT
4279 else
4281 if (client->crypto->save.s2k_count)
4282 rc = send_to_agent (client->crypto->agent, NULL, NULL,
4283 "OPTION s2k-count=%lu",
4284 client->crypto->save.s2k_count);
4286 if (!rc)
4287 rc = agent_passwd (client->crypto);
4289 #endif
4292 return send_error (ctx, rc);
4295 static gpg_error_t
4296 parse_keygrip_opt_sign (void *data, void *value)
4298 struct client_s *client = data;
4300 (void) value;
4301 client->opts |= OPT_SIGN;
4302 return 0;
4305 static gpg_error_t
4306 keygrip_command (assuan_context_t ctx, char *line)
4308 struct client_s *client = assuan_get_pointer (ctx);
4309 gpg_error_t rc;
4310 struct crypto_s *crypto = NULL;
4311 struct argv_s *args[] = {
4312 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_keygrip_opt_sign},
4313 NULL
4316 if (!line || !*line)
4317 return send_error (ctx, GPG_ERR_SYNTAX);
4319 rc = parse_options (&line, args, client);
4320 if (rc)
4321 return send_error (ctx, rc);
4323 if (!valid_filename (line))
4324 return send_error (ctx, GPG_ERR_INV_VALUE);
4326 rc = init_client_crypto (&crypto);
4327 if (rc)
4328 return send_error (ctx, rc);
4330 rc = read_data_file (line, crypto);
4331 if (!rc)
4333 char *hexgrip = NULL;
4335 if (!IS_PKI (crypto))
4337 cleanup_crypto (&crypto);
4338 return send_error (ctx, GPG_ERR_NOT_SUPPORTED);
4341 if (client->opts & OPT_SIGN)
4343 if (valid_keygrip (crypto->sign_grip, sizeof (crypto->sign_grip)))
4344 hexgrip = bin2hex (crypto->sign_grip, sizeof (crypto->sign_grip));
4347 if (!hexgrip)
4348 hexgrip = bin2hex (crypto->grip, sizeof (crypto->grip));
4350 if (!hexgrip)
4351 rc = GPG_ERR_ENOMEM;
4352 else
4353 rc = xfer_data (ctx, hexgrip, strlen (hexgrip));
4355 xfree (hexgrip);
4358 UPDATE_AGENT_CTX (client, crypto);
4359 cleanup_crypto (&crypto);
4360 return send_error (ctx, rc);
4363 static gpg_error_t
4364 parse_opt_data (void *data, void *value)
4366 struct client_s *client = data;
4368 (void) value;
4369 client->opts |= OPT_DATA;
4370 return 0;
4373 static gpg_error_t
4374 send_client_list (assuan_context_t ctx)
4376 struct client_s *client = assuan_get_pointer (ctx);
4377 gpg_error_t rc = 0;
4378 char buf[ASSUAN_LINELENGTH];
4380 if (client->opts & OPT_VERBOSE)
4382 unsigned i, t;
4383 char **list = NULL;
4384 char *line;
4386 MUTEX_LOCK (&cn_mutex);
4387 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
4388 t = slist_length (cn_thread_list);
4390 for (i = 0; i < t; i++)
4392 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
4394 if (!strv_printf (&list, "%p %s %s", thd->tid,
4395 thd->name ? thd->name : "-",
4396 thd->cl && thd->cl->filename
4397 ? thd->cl->filename : "-"))
4399 strv_free (list);
4400 rc = GPG_ERR_ENOMEM;
4401 break;
4405 pthread_cleanup_pop (1);
4406 if (rc)
4407 return rc;
4409 line = strv_join ("\n", list);
4410 strv_free (list);
4411 pthread_cleanup_push (xfree, line);
4412 rc = xfer_data (ctx, line, strlen (line));
4413 pthread_cleanup_pop (1);
4414 return rc;
4417 if (client->opts & OPT_DATA)
4419 MUTEX_LOCK (&cn_mutex);
4420 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
4421 snprintf (buf, sizeof (buf), "%i", slist_length (cn_thread_list));
4422 pthread_cleanup_pop (1);
4423 rc = xfer_data (ctx, buf, strlen (buf));
4425 else
4426 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4428 return rc;
4431 static gpg_error_t
4432 getinfo_command (assuan_context_t ctx, char *line)
4434 struct client_s *client = assuan_get_pointer (ctx);
4435 gpg_error_t rc;
4436 char buf[ASSUAN_LINELENGTH];
4437 struct argv_s *args[] = {
4438 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4439 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_opt_verbose},
4440 NULL
4443 rc = parse_options (&line, args, client);
4444 if (rc)
4445 return send_error (ctx, rc);
4447 if (!strcasecmp (line, "clients"))
4449 rc = send_client_list (ctx);
4451 else if (!strcasecmp (line, "cache"))
4453 if (client->opts & OPT_DATA)
4455 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4456 rc = xfer_data (ctx, buf, strlen (buf));
4458 else
4459 rc = send_status (ctx, STATUS_CACHE, NULL);
4461 else if (!strcasecmp (line, "pid"))
4463 char buf[32];
4464 pid_t pid = getpid ();
4466 snprintf (buf, sizeof (buf), "%i", pid);
4467 rc = xfer_data (ctx, buf, strlen (buf));
4469 else if (!strcasecmp (line, "version"))
4471 char *buf = str_asprintf ("0x%06x %s%s", VERSION_HEX,
4472 #ifdef WITH_GNUTLS
4473 "GNUTLS "
4474 #endif
4475 #ifdef WITH_QUALITY
4476 "QUALITY "
4477 #endif
4478 "", use_agent ? "AGENT" : "");
4479 rc = xfer_data (ctx, buf, strlen (buf));
4480 xfree (buf);
4482 else if (!strcasecmp (line, "last_error"))
4484 if (client->last_error)
4485 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4486 else
4487 rc = GPG_ERR_NO_DATA;
4489 else if (!strcasecmp (line, "user"))
4491 char *user = NULL;
4493 #ifdef WITH_GNUTLS
4494 if (client->thd->remote)
4495 user = str_asprintf ("#%s", client->thd->tls->fp);
4496 else
4497 user = get_username (client->thd->peer->uid);
4498 #else
4499 user = get_username (client->thd->peer->uid);
4500 #endif
4501 if (user)
4503 pthread_cleanup_push (xfree, user);
4504 rc = xfer_data (ctx, user, strlen (user));
4505 pthread_cleanup_pop (1);
4507 else
4508 rc = GPG_ERR_NO_DATA;
4510 else
4511 rc = gpg_error (GPG_ERR_SYNTAX);
4513 return send_error (ctx, rc);
4516 #ifdef WITH_AGENT
4517 static gpg_error_t
4518 send_data_cb (void *user, const void *buf, size_t len)
4520 assuan_context_t ctx = user;
4522 return assuan_send_data (ctx, buf, len);
4525 static gpg_error_t
4526 send_status_cb (void *user, const char *line)
4528 assuan_context_t ctx = user;
4529 char keyword[200], *k;
4530 const char *p;
4532 for (p = line, k = keyword; *p; p++)
4534 if (isspace (*p))
4535 break;
4537 *k++ = *p;
4540 *k = 0;
4541 if (*p == '#')
4542 p++;
4544 while (isspace (*p))
4545 p++;
4547 return assuan_write_status (ctx, keyword, *p ? p : NULL);
4549 #endif
4551 static gpg_error_t
4552 kill_command (assuan_context_t ctx, char *line)
4554 #ifdef HAVE_PTHREAD_CANCEL
4555 struct client_s *client = assuan_get_pointer (ctx);
4556 gpg_error_t rc;
4558 if (!line || !*line)
4559 return send_error (ctx, GPG_ERR_SYNTAX);
4561 rc = peer_is_invoker (client);
4562 if (!rc)
4564 unsigned i, t;
4566 MUTEX_LOCK (&cn_mutex);
4567 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
4568 t = slist_length (cn_thread_list);
4570 for (i = 0; i < t; i++)
4572 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
4573 char *tmp = str_asprintf ("%p", thd->tid);
4575 if (strcmp (line, tmp))
4577 xfree (tmp);
4578 continue;
4581 xfree (tmp);
4582 rc = pthread_cancel (thd->tid);
4583 break;
4586 pthread_cleanup_pop (1);
4589 return send_error (ctx, rc);
4590 #else
4591 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
4592 #endif
4595 static gpg_error_t
4596 agent_command (assuan_context_t ctx, char *line)
4598 gpg_error_t rc = 0;
4600 if (!use_agent)
4601 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
4603 #ifdef WITH_AGENT
4604 struct client_s *client = assuan_get_pointer (ctx);
4606 if (!line || !*line)
4607 return send_error (ctx, GPG_ERR_SYNTAX);
4609 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 1);
4610 rc = assuan_transact (client->crypto->agent->ctx, line, send_data_cb,
4611 client->ctx, agent_loopback_cb, client->crypto,
4612 send_status_cb, client->ctx);
4613 if (gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
4615 char *line;
4616 size_t len;
4618 rc = assuan_write_line (client->crypto->agent->ctx, "CAN");
4619 if (!rc)
4621 rc = assuan_read_line (client->crypto->agent->ctx, &line, &len);
4622 if (!rc)
4623 rc = gpg_error (GPG_ERR_ASS_CANCELED);
4627 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 0);
4628 #endif
4629 return send_error (ctx, rc);
4632 void
4633 init_commands ()
4635 /* !BEGIN-HELP-TEXT!
4637 * This comment is used as a marker to generate the offline documentation
4638 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4639 * script to determine where commands begin and end.
4641 new_command("HELP", 1, 1, help_command, _(
4642 "HELP [<COMMAND>]\n"
4643 "Show available commands or command specific help text."
4646 new_command("AGENT", 1, 1, agent_command, _(
4647 "AGENT <command>\n"
4648 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4649 "@command{gpg-agent}."
4652 new_command("KILL", 1, 0, kill_command, _(
4653 "KILL <thread_id>\n"
4654 "Terminates the client identified by @var{thread_id} and releases any file "
4655 "lock or other resources it has held. @xref{GETINFO} for details about listing "
4656 "connected clients.\n"
4659 new_command("GETINFO", 1, 1, getinfo_command, _(
4660 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4661 "Get server and other information: @var{CACHE} returns the number of cached "
4662 "documents via a status message. @var{CLIENTS} returns the number of "
4663 "connected clients via a status message or a list of connected clients when "
4664 "the @option{--verbose} parameter is used. The list contains space delimited "
4665 "fields specifying the thread ID, client name and opened file and is always "
4666 "returned with a data response. @var{PID} returns the process ID "
4667 "number of the server via a data response. @var{VERSION} returns the server "
4668 "version number and compile-time features with a data response with each "
4669 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4670 "the last failed command when available. @var{USER} returns the username or "
4671 "@abbr{TLS} hash of the connected client. @xref{Status Messages}. "
4672 "\n"
4673 "When the @option{--data} option is specified then the result will be sent "
4674 "via a data response rather than a status message."
4677 new_command("PASSWD", 0, 0, passwd_command, _(
4678 "PASSWD [--reset] [--s2k-count=N] [--no-passphrase]\n"
4679 "Changes the passphrase of the secret key required to open the current "
4680 "file or the passphrase of a symmetrically encrypted data file. When the "
4681 "@option{--reset} option is passed then the cache entry for the current "
4682 "file will be reset and the passphrase, if any, will be required during the "
4683 "next @code{OPEN} (@pxref{OPEN})."
4684 "\n"
4685 "The @option{--s2k-count} option sets or changes (@pxref{SAVE}) the number "
4686 "of hash iterations for a passphrase and must be either @code{0} to use "
4687 "the calibrated count of the machine (the default), or a value greater than "
4688 "or equal to @code{65536}. This option has no effect for symmetrically "
4689 "encrypted data files."
4690 "\n"
4691 "The @option{--no-passphrase} option will prevent requiring a passphrase for "
4692 "the data file, although a passphrase may be required when changing it."
4693 "\n"
4694 "This command is not available for non-invoking clients "
4695 "(@pxref{Access Control})."
4698 new_command("KEYGRIP", 1, 1, keygrip_command, _(
4699 "KEYGRIP [--sign] <filename>\n"
4700 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4701 "data response."
4702 "\n"
4703 "When the @option{--sign} option is specified then the key used for signing "
4704 "of the specified @var{filename} will be returned."
4705 "\n"
4706 "For symmetrically encrypted data files this command returns the error "
4707 "GPG_ERR_NOT_SUPPORTED."
4710 new_command("OPEN", 1, 1, open_command, _(
4711 "OPEN [--lock] <filename> [<passphrase>]\n"
4712 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4713 "found on the file-system then a new document will be created. If the file "
4714 "is found, it is looked for in the file cache. If cached and no "
4715 "@var{passphrase} was specified then the cached document is opened. When not "
4716 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4717 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4718 "specified."
4719 "\n"
4720 "When the @option{--lock} option is passed then the file mutex will be "
4721 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4722 "file has been opened."
4725 new_command("SAVE", 0, 0, save_command, _(
4726 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring] [--sign-keygrip=hexstring]\n"
4727 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4728 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4729 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4730 "keypair will be generated and a pinentry will be used to prompt for the "
4731 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4732 "passed in which case the data file will not be passphrase protected. "
4733 "\n"
4734 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4735 "passphrase retrieval and caching of new files when @command{gpg-agent} "
4736 "use is enabled. The datafile will be symmetrically encrypted and will not "
4737 "use or generate any keypair."
4738 "\n"
4739 "The @option{--reset} option will clear the cache entry for the current file "
4740 "and require a passphrase, if needed, before saving."
4741 "\n"
4742 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4743 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4744 "(@pxref{Configuration}) for available ciphers."
4745 "\n"
4746 "The @option{--cipher-iterations} option specifies the number of times to "
4747 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4748 "\n"
4749 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4750 "the client to obtain the key paramaters to use when generating a new "
4751 "keypair. The inquired data is expected to be an S-expression. If not "
4752 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4753 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4754 "that when this option is specified a new keypair will be generated "
4755 "reguardless if the file is a new one and that if the data file is protected "
4756 "the passphrase to open it will be required before generating the new "
4757 "keypair. This option is not available for non-invoking clients "
4758 "(@pxref{Access Control})."
4759 "\n"
4760 "You can encrypt the data file to a public key other than the one that it "
4761 "was originally encrypted with by passing the @option{--keygrip} option with "
4762 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4763 "be of any key that @command{gpg-agent} knows about. The "
4764 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4765 "secret key. Use the @code{KEYGRIP} (@pxref{KEYGRIP}) command to obtain the "
4766 "keygrip of an existing data file. This option may be needed when using a "
4767 "smartcard. This option has no effect with symmetrically encrypted data "
4768 "files. These options are not available for non-invoking clients "
4769 "(@pxref{Access Control})."
4770 "\n"
4771 "The @option{--s2k-count} option sets number of hash iterations for a "
4772 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4773 "value and is the default. This setting only affects new files. To change "
4774 "the setting use the @code{PASSWD} command (@pxref{PASSWD}). This option "
4775 "has no effect with symmetrically encrypted data files."
4778 new_command("ISCACHED", 1, 0, iscached_command, _(
4779 "ISCACHED [--lock] <filename>\n"
4780 "An @emph{OK} response is returned if the specified @var{filename} is found "
4781 "in the file cache. If not found in the cache but exists on the filesystem "
4782 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4783 "returned."
4784 "\n"
4785 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4786 "file exists; it does not need to be opened nor cached. The lock will be "
4787 "released when the client exits or sends the @code{UNLOCK} (@pxref{UNLOCK}) "
4788 "command."
4791 new_command("CLEARCACHE", 1, 1, clearcache_command, _(
4792 "CLEARCACHE [<filename>]\n"
4793 "Clears a file cache entry for all or the specified @var{filename}."
4796 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command, _(
4797 "CACHETIMEOUT <filename> <seconds>\n"
4798 "The time in @var{seconds} until @var{filename} will be removed from the "
4799 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4800 "the passphrase for each @code{OPEN} or @code{SAVE} command (@pxref{OPEN}, "
4801 "@pxref{SAVE}). @xref{Configuration}, and the @code{cache_timeout} "
4802 "parameter."
4805 new_command("LIST", 0, 1, list_command, _(
4806 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4807 "If no element path is given then a newline separated list of root elements "
4808 "is returned with a data response. If given, then all reachable elements "
4809 "of the specified element path are returned unless the @option{--no-recurse} "
4810 "option is specified. If specified, only the child elements of the element "
4811 "path are returned without recursing into grandchildren. Each resulting "
4812 "element is prefixed with the literal @code{!} character when the element "
4813 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4814 "\n"
4815 "When the @option{--verbose} option is passed then each element path "
4816 "returned will have zero or more flags appened to it. These flags are "
4817 "delimited from the element path by a single space character. A flag itself "
4818 "is a single character. Flag @code{P} indicates that access to the element "
4819 "is denied. Flag @code{+} indicates that there are child nodes of "
4820 "the current element path. Flag @code{E} indicates that an element of an "
4821 "element path contained in a @var{target} attribute could not be found. Flag "
4822 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4823 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4824 "of the @var{target} attribute contained in the current element (see below)."
4825 "\n"
4826 "The @option{--with-target} option implies @option{--verbose} and will append "
4827 "an additional flag @code{T} followed by a single space then an element path. "
4828 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4829 "current element when it contains a @var{target} attribute. When no "
4830 "@var{target} attribute is found then no flag will be appended."
4831 "\n"
4832 "The @option{--no-recurse} option limits the amount of data returned to only "
4833 "the listing of children of the specified element path and not any "
4834 "grandchildren."
4835 "\n"
4836 "The @option{--all} option lists the entire element tree for each root "
4837 "element. This option also implies option @option{--verbose}."
4838 "\n"
4839 "When the @option{--inquire} option is passed then all remaining non-option "
4840 "arguments are retrieved via a server @emph{INQUIRE}."
4843 new_command("REALPATH", 0, 1, realpath_command, _(
4844 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4845 "Resolves all @code{target} attributes of the specified element path and "
4846 "returns the result with a data response. @xref{Target Attribute}, for details."
4847 "\n"
4848 "When the @option{--inquire} option is passed then all remaining non-option "
4849 "arguments are retrieved via a server @emph{INQUIRE}."
4852 new_command("STORE", 0, 1, store_command, _(
4853 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4854 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4855 "\n"
4856 "Creates a new element path or modifies the @var{content} of an existing "
4857 "element. If only a single element is specified then a new root element is "
4858 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4859 "set to the final @key{TAB} delimited element. If no @var{content} is "
4860 "specified after the final @key{TAB}, then the content of an existing "
4861 "element will be removed; or empty when creating a new element."
4862 "\n"
4863 "The only restriction of an element name is that it not contain whitespace "
4864 "or begin with the literal element character @code{!} unless specifying a "
4865 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4866 "the @key{TAB} delimited elements. It is recommended that the content of an "
4867 "element be base64 encoded when it contains control or @key{TAB} characters "
4868 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
4871 new_command("RENAME", 0, 1, rename_command, _(
4872 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4873 "Renames the specified @var{element} to the new @var{value}. If an element of "
4874 "the same name as the @var{value} already exists it will be overwritten."
4875 "\n"
4876 "When the @option{--inquire} option is passed then all remaining non-option "
4877 "arguments are retrieved via a server @emph{INQUIRE}."
4880 new_command("COPY", 0, 1, copy_command, _(
4881 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4882 "Copies the entire element tree starting from the child node of the source "
4883 "element, to the destination element path. If the destination element path "
4884 "does not exist then it will be created; otherwise it is overwritten."
4885 "\n"
4886 "Note that attributes from the source element are merged into the "
4887 "destination element when the destination element path exists. When an "
4888 "attribute of the same name exists in both the source and destination "
4889 "elements then the destination attribute will be updated to the source "
4890 "attribute value."
4891 "\n"
4892 "When the @option{--inquire} option is passed then all remaining non-option "
4893 "arguments are retrieved via a server @emph{INQUIRE}."
4896 new_command("MOVE", 0, 1, move_command, _(
4897 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4898 "Moves the source element path to the destination element path. If the "
4899 "destination is not specified then it will be moved to the root node of the "
4900 "document. If the destination is specified and exists then it will be "
4901 "overwritten; otherwise non-existing elements of the destination element "
4902 "path will be created."
4903 "\n"
4904 "When the @option{--inquire} option is passed then all remaining non-option "
4905 "arguments are retrieved via a server @emph{INQUIRE}."
4908 new_command("DELETE", 0, 1, delete_command, _(
4909 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4910 "Removes the specified element path and all of its children. This may break "
4911 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4912 "refers to this element or any of its children."
4913 "\n"
4914 "When the @option{--inquire} option is passed then all remaining non-option "
4915 "arguments are retrieved via a server @emph{INQUIRE}."
4918 new_command("GET", 0, 1, get_command, _(
4919 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4920 "Retrieves the content of the specified element. The content is returned "
4921 "with a data response."
4922 "\n"
4923 "When the @option{--inquire} option is passed then all remaining non-option "
4924 "arguments are retrieved via a server @emph{INQUIRE}."
4927 new_command("ATTR", 0, 1, attr_command, _(
4928 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4929 "@table @asis\n"
4930 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4931 "\n"
4932 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4933 "element. When no @var{value} is specified any existing value will be removed."
4934 "\n"
4935 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4936 "\n"
4937 " Removes an @var{attribute} from an element."
4938 "\n"
4939 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4940 "\n"
4941 " Retrieves a newline separated list of attributes names and values "
4942 "from the specified element. Each attribute name and value is space delimited."
4943 "\n"
4944 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4945 "\n"
4946 " Retrieves the value of an @var{attribute} from an element."
4947 "@end table\n"
4948 "\n"
4949 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4950 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4951 "commands instead."
4952 "\n"
4953 "The @code{_mtime} attribute is updated each time an element is modified by "
4954 "either storing content, editing attributes or by deleting a child element. "
4955 "The @code{_ctime} attribute is created for each new element in an element "
4956 "path."
4957 "\n"
4958 "When the @option{--inquire} option is passed then all remaining non-option "
4959 "arguments are retrieved via a server @emph{INQUIRE}."
4960 "\n"
4961 "@xref{Target Attribute}, for details about this special attribute."
4964 new_command("XPATH", 0, 1, xpath_command, _(
4965 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4966 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4967 "specified it is assumed the expression is a request to return a result. "
4968 "Otherwise, the result is set to the @var{value} argument and the document is "
4969 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4970 "is assumed to be empty and the document is updated. For example:"
4971 "@sp 1\n"
4972 "@example\n"
4973 "XPATH //element[@@_name='password']@key{TAB}\n"
4974 "@end example\n"
4975 "@sp 1"
4976 "would clear the content of all @code{password} elements in the data file "
4977 "while leaving off the trailing @key{TAB} would return all @code{password} "
4978 "elements in @abbr{XML} format."
4979 "\n"
4980 "When the @option{--inquire} option is passed then all remaining non-option "
4981 "arguments are retrieved via a server @emph{INQUIRE}."
4982 "\n"
4983 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4984 "expression syntax."
4987 new_command("XPATHATTR", 0, 1, xpathattr_command, _(
4988 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4989 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4990 "attributes and does not return a result. For the @var{SET} operation the "
4991 "@var{value} is optional but the field is required. If not specified then "
4992 "the attribute value will be empty. For example:"
4993 "@sp 1"
4994 "@example\n"
4995 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4996 "@end example\n"
4997 "@sp 1"
4998 "would create an @code{password} attribute for each @code{password} element "
4999 "found in the document. The attribute value will be empty but still exist."
5000 "\n"
5001 "When the @option{--inquire} option is passed then all remaining non-option "
5002 "arguments are retrieved via a server @emph{INQUIRE}."
5003 "\n"
5004 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
5005 "expression syntax."
5008 new_command("IMPORT", 0, 1, import_command, _(
5009 "IMPORT [--root [!]element[<TAB>[!]child[..]]] <content>\n"
5010 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
5011 "\n"
5012 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
5013 "argument is raw @abbr{XML} data. The content is created as a child of "
5014 "the element path specified with the @option{--root} option or at the "
5015 "document root when not specified. Existing elements of the same name will "
5016 "be overwritten."
5017 "\n"
5018 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
5019 "for details."
5022 new_command("DUMP", 0, 1, dump_command, _(
5023 "DUMP\n"
5024 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
5025 "dumping a specific node."
5028 new_command("LOCK", 0, 0, lock_command, _(
5029 "LOCK\n"
5030 "Locks the mutex associated with the opened file. This prevents other clients "
5031 "from sending commands to the same opened file until the client "
5032 "that sent this command either disconnects or sends the @code{UNLOCK} "
5033 "command. @xref{UNLOCK}."
5036 new_command("UNLOCK", 1, 0, unlock_command, _(
5037 "UNLOCK\n"
5038 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
5039 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
5040 "@pxref{ISCACHED})."
5043 new_command("GETCONFIG", 1, 1, getconfig_command, _(
5044 "GETCONFIG [filename] <parameter>\n"
5045 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
5046 "data response. If no file has been opened then the value for @var{filename} "
5047 "or the default from the @samp{global} section will be returned. If a file "
5048 "has been opened and no @var{filename} is specified, a value previously "
5049 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
5052 new_command("OPTION", 1, 1, option_command, _(
5053 "OPTION <NAME>=<VALUE>\n"
5054 "Sets a client option @var{name} to @var{value}. The value for an option is "
5055 "kept for the duration of the connection."
5056 "\n"
5057 "@table @asis\n"
5058 "@item DISABLE-PINENTRY\n"
5059 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
5060 "server inquire is sent to the client to obtain the passphrase. This option "
5061 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
5062 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
5063 "\n"
5064 "@item PINENTRY-TIMEOUT\n"
5065 "Sets the number of seconds before a pinentry prompt will return an error "
5066 "while waiting for user input."
5067 "\n"
5068 "@item TTYNAME\n"
5069 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5070 "\n"
5071 "@item TTYTYPE\n"
5072 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5073 "\n"
5074 "@item DISPLAY\n"
5075 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5076 "\n"
5077 "@item PINENTRY-DESC\n"
5078 "Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
5079 "\n"
5080 "@item PINENTRY-TITLE\n"
5081 "Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
5082 "\n"
5083 "@item PINENTRY-PROMPT\n"
5084 "Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
5085 "\n"
5086 "@item LC-CTYPE\n"
5087 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5088 "\n"
5089 "@item LC-MESSAGES\n"
5090 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5091 "\n"
5092 "@item NAME\n"
5093 "Associates the thread ID of the connection with the specified textual "
5094 "representation. Useful for debugging log messages."
5095 "\n"
5096 "@item LOCK-TIMEOUT\n"
5097 "When not @code{0}, the duration in tenths of a second to wait for the file "
5098 "mutex which has been locked by another thread to be released before returning "
5099 "an error. When @code{-1}, then an error will be returned immediately."
5100 "\n"
5101 "@item LOG-LEVEL\n"
5102 "An integer specifiying the logging level."
5103 "@end table\n"
5106 new_command("LS", 1, 1, ls_command, _(
5107 "LS\n"
5108 "Lists the available data files stored in the data directory "
5109 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
5112 new_command("RESET", 1, 1, NULL, _(
5113 "RESET\n"
5114 "Closes the currently opened file but keeps any previously set client options."
5117 new_command("NOP", 1, 1, NULL, _(
5118 "NOP\n"
5119 "Does nothing. Always returns successfully."
5122 /* !END-HELP-TEXT! */
5123 new_command ("CANCEL", 1, 1, NULL, NULL);
5124 new_command ("END", 1, 1, NULL, NULL);
5125 new_command ("BYE", 1, 1, NULL, NULL);
5127 int i;
5128 for (i = 0; command_table[i]; i++);
5129 qsort (command_table, i - 1, sizeof (struct command_table_s *),
5130 sort_commands);
5133 gpg_error_t
5134 register_commands (assuan_context_t ctx)
5136 int i = 0, rc;
5138 for (; command_table[i]; i++)
5140 if (!command_table[i]->handler)
5141 continue;
5143 rc = assuan_register_command (ctx, command_table[i]->name,
5144 command_table[i]->handler,
5145 command_table[i]->help);
5146 if (rc)
5147 return rc;
5150 rc = assuan_register_bye_notify (ctx, bye_notify);
5151 if (rc)
5152 return rc;
5154 rc = assuan_register_reset_notify (ctx, reset_notify);
5155 if (rc)
5156 return rc;
5158 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
5159 if (rc)
5160 return rc;
5162 return assuan_register_post_cmd_notify (ctx, command_finalize);