Fix unneeded permission check for MOVE.
[libpwmd.git] / src / commands.c
blob1d912958d8134d240970c5a479ffd1c891d8e7a5
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_LIST_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 && !cache_add_file (client->md5file,
405 (client->flags & FLAG_NEW)
406 ? NULL
407 : client->crypto->grip, cdata, timeout))
409 if (!cached)
411 free_cache_data_once (cdata);
412 xmlFreeDoc (client->doc);
413 client->doc = NULL;
415 rc = GPG_ERR_ENOMEM;
418 if (!rc)
419 client->flags |= FLAG_OPEN;
423 if (!rc)
424 update_checksum (client);
426 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
427 rc = do_lock (client, 0);
429 return rc;
432 static void
433 req_cleanup (void *arg)
435 if (!arg)
436 return;
438 strv_free ((char **) arg);
441 static gpg_error_t
442 parse_open_opt_lock (void *data, void *value)
444 struct client_s *client = data;
446 client->opts |= OPT_LOCK_ON_OPEN;
447 return 0;
450 static gpg_error_t
451 parse_save_opt_inquire (void *data, void *value)
453 struct client_s *client = data;
454 gpg_error_t rc;
456 if (!(client->flags & FLAG_NEW))
458 rc = peer_is_invoker (client);
459 if (rc == GPG_ERR_EACCES)
460 return rc;
463 (void) value;
464 client->opts |= OPT_INQUIRE;
465 return 0;
468 static gpg_error_t
469 parse_opt_inquire (void *data, void *value)
471 struct client_s *client = data;
473 (void) value;
474 client->opts |= OPT_INQUIRE;
475 return 0;
478 static gpg_error_t
479 update_checksum (struct client_s *client)
481 unsigned char *crc;
482 size_t len;
483 struct cache_data_s *cdata;
484 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
486 if (rc)
487 return rc;
489 xfree (client->crc);
490 client->crc = crc;
491 cdata = cache_get_data (client->md5file);
492 if (cdata)
494 xfree (cdata->crc);
495 cdata->crc = xmalloc (len);
496 memcpy (cdata->crc, crc, len);
499 return 0;
502 static gpg_error_t
503 validate_checksum (struct client_s *client, struct cache_data_s *cdata)
505 unsigned char *crc;
506 size_t len;
507 gpg_error_t rc;
508 int n = 0;
510 if (cdata && !cdata->crc)
511 return GPG_ERR_CHECKSUM;
513 rc = get_checksum (client->filename, &crc, &len);
514 if (rc)
515 return rc;
517 if (cdata)
518 n = memcmp (cdata->crc, crc, len);
519 else if (client->crc)
520 n = memcmp (client->crc, crc, len);
522 xfree (crc);
523 return n ? GPG_ERR_CHECKSUM : 0;
526 static gpg_error_t
527 do_open (assuan_context_t ctx, const char *filename, const char *password)
529 struct client_s *client = assuan_get_pointer (ctx);
530 struct cache_data_s *cdata;
531 gpg_error_t rc = 0;
532 int done = 0;
534 if (!valid_filename (filename))
535 return GPG_ERR_INV_VALUE;
537 gcry_md_hash_buffer (GCRY_MD_MD5, client->md5file, filename,
538 strlen (filename));
539 client->filename = str_dup (filename);
540 if (!client->filename)
541 return GPG_ERR_ENOMEM;
543 // Cached document?
544 cdata = cache_get_data (client->md5file);
545 if (cdata && cdata->doc)
547 int defer = 0;
549 /* This will check that the key is cached in the agent which needs to
550 * be determined for files that share a keygrip. */
551 if (!rc)
553 rc = cache_iscached (client->filename, &defer);
554 if ((!rc || rc == GPG_ERR_ENOENT) && defer)
555 rc = GPG_ERR_KEY_EXPIRED;
558 if (!rc && !(client->flags & FLAG_NEW))
559 rc = validate_checksum (client, cdata);
561 #ifdef WITH_GNUTLS
562 if (!rc && client->thd->remote
563 && config_get_boolean (client->filename, "tcp_require_key"))
564 rc = GPG_ERR_KEY_EXPIRED;
565 #endif
567 if (!rc && !password)
569 rc = read_data_header (client->filename, &client->crypto->hdr,
570 NULL, NULL);
571 if (!rc)
573 if (IS_PKI (client->crypto))
575 gcry_sexp_build (&client->crypto->pkey_sexp, NULL, "%S",
576 cdata->pubkey);
577 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
578 client->crypto->grip);
579 gcry_sexp_build (&client->crypto->sigpkey_sexp, NULL, "%S",
580 cdata->sigkey);
581 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
582 client->crypto->sign_grip);
585 if (!rc)
587 rc = open_finalize (ctx, NULL, 0);
588 done = 1;
593 /* There was an error accessing the file so clear the cache entry. The
594 * real error will be returned from read_data_file() since the file
595 * may have only disappeared. */
596 if (!done)
598 log_write ("%s: %s", filename, pwmd_strerror (rc));
599 rc = cache_clear (client->md5file);
600 send_status_all (STATUS_CACHE, NULL);
604 if (done || rc)
605 return rc;
607 rc = read_data_file (client->filename, client->crypto);
608 if (rc)
610 if (gpg_err_source (rc) != GPG_ERR_SOURCE_DEFAULT ||
611 gpg_err_code (rc) != GPG_ERR_ENOENT)
613 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
614 return rc;
617 client->flags |= FLAG_NEW;
618 memset (client->crypto->grip, 0, sizeof (client->crypto->grip));
619 memset (client->crypto->sign_grip, 0,
620 sizeof (client->crypto->sign_grip));
621 rc = open_finalize (ctx, NULL, 0);
622 return rc;
625 if (password && IS_PKI (client->crypto))
627 #ifdef WITH_AGENT
628 rc = set_agent_passphrase (client->crypto, password, strlen (password));
629 if (rc)
630 return rc;
631 #endif
634 rc = open_finalize (ctx, (char *)password, password ? strlen (password) : 0);
635 return rc;
638 static gpg_error_t
639 open_command (assuan_context_t ctx, char *line)
641 gpg_error_t rc;
642 struct client_s *client = assuan_get_pointer (ctx);
643 char **req, *filename;
644 unsigned char md5file[16];
645 int same_file = 0;
646 assuan_peercred_t peer;
647 struct argv_s *args[] = {
648 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
649 NULL
652 rc = parse_options (&line, args, client);
653 if (rc)
654 return send_error (ctx, rc);
656 req = str_split (line, " ", 2);
657 if (!req)
658 return send_error (ctx, GPG_ERR_SYNTAX);
660 rc = do_validate_peer (ctx, req[0], &peer);
661 if (rc)
663 strv_free (req);
664 return send_error (ctx, rc);
667 pthread_cleanup_push (req_cleanup, req);
668 filename = req[0];
669 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, filename, strlen (filename));
670 /* This client may have locked a different file with ISCACHED --lock than
671 * the current filename. This will remove that lock. */
672 same_file = !memcmp (md5file, client->md5file, 16) ? 1 : 0;
673 if (client->flags & FLAG_OPEN ||
674 (client->flags & FLAG_HAS_LOCK && !same_file))
676 typeof (client->opts) opts = client->opts;
677 typeof (client->flags) flags = client->flags;
679 if (same_file)
680 client->flags |= FLAG_KEEP_LOCK;
682 cleanup_client (client);
683 client->opts = opts;
684 client->flags |= flags;
685 client->flags &= ~(FLAG_LOCK_CMD);
686 if (!same_file)
687 client->flags &= ~(FLAG_HAS_LOCK | FLAG_NEW);
690 /* Need to lock the mutex here because file_modified() cannot without
691 * knowing the filename. */
692 memcpy (client->md5file, md5file, 16);
693 rc = lock_file_mutex (client, 1);
694 if (rc)
695 client->flags &= ~FLAG_OPEN;
697 if (!rc)
699 char *password = req[1] && *req[1] ? req[1] : NULL;
701 #ifdef WITH_AGENT
702 if (IS_PKI (client->crypto))
703 rc = set_pinentry_mode (client->crypto->agent,
704 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
705 : "ask");
706 #endif
707 if (!rc)
709 rc = do_open (ctx, filename, password);
710 if (rc)
711 cleanup_client (client);
713 cleanup_crypto_stage1 (client->crypto);
717 pthread_cleanup_pop (1);
719 if (!rc && client->flags & FLAG_NEW)
720 rc = send_status (ctx, STATUS_NEWFILE, NULL);
722 #ifdef WITH_AGENT
723 (void) kill_scd (client->crypto->agent);
724 #endif
726 return send_error (ctx, rc);
729 static gpg_error_t
730 parse_opt_no_passphrase (void *data, void *value)
732 struct client_s *client = data;
734 (void) value;
735 client->opts |= OPT_NO_PASSPHRASE;
736 return 0;
739 static gpg_error_t
740 parse_save_opt_no_agent (void *data, void *value)
742 struct client_s *client = data;
744 client->opts |= OPT_NO_AGENT;
745 return 0;
748 static gpg_error_t
749 parse_save_opt_cipher (void *data, void *value)
751 struct client_s *client = data;
752 int algo = cipher_string_to_gcrypt ((char *) value);
753 file_header_t *hdr = &client->crypto->save.hdr;
754 gpg_error_t rc = 0;
756 if (algo == -1)
757 return GPG_ERR_INV_VALUE;
759 if (!(client->flags & FLAG_NEW))
761 rc = peer_is_invoker (client);
762 if (rc == GPG_ERR_EACCES)
764 uint64_t flags = 0;
766 flags &= ((uint16_t) ((uint32_t) (flags & 0xFFFFFFFF)));
767 if (!(client->crypto->hdr.flags & gcrypt_to_cipher (algo)))
768 return rc;
770 rc = 0;
772 else if (rc)
773 return rc;
776 if (!rc)
777 hdr->flags = set_cipher_flag (hdr->flags, algo);
779 return rc;
782 #ifdef WITH_AGENT
783 static gpg_error_t
784 permitted_to_save (struct client_s *client, const unsigned char *grip,
785 size_t size, const char *value)
787 char *hexgrip;
788 gpg_error_t rc = 0;
790 if (!(client->flags & FLAG_NEW))
792 hexgrip = bin2hex (grip, size);
793 rc = !strcmp (hexgrip, (char *)value) ? 0 : GPG_ERR_EACCES;
794 xfree (hexgrip);
795 if (rc)
796 rc = peer_is_invoker (client);
799 return rc;
801 #endif
803 static gpg_error_t
804 parse_save_opt_keygrip (void *data, void *value)
806 #ifdef WITH_AGENT
807 struct client_s *client = data;
808 gpg_error_t rc = permitted_to_save (client, client->crypto->grip,
809 sizeof (client->crypto->grip),
810 value);
812 if (!IS_PKI (client->crypto))
813 return GPG_ERR_INV_ARG;
815 if (rc)
816 return rc;
818 if (client->crypto->save.pkey)
819 gcry_sexp_release (client->crypto->save.pkey);
821 client->crypto->save.pkey = NULL;
822 return get_pubkey (client->crypto, value, &client->crypto->save.pkey);
823 #else
824 return GPG_ERR_INV_ARG;
825 #endif
828 static gpg_error_t
829 parse_save_opt_sign_keygrip (void *data, void *value)
831 #ifdef WITH_AGENT
832 struct client_s *client = data;
833 gpg_error_t rc = permitted_to_save (client, client->crypto->sign_grip,
834 sizeof (client->crypto->sign_grip),
835 value);
837 if (!IS_PKI (client->crypto))
838 return GPG_ERR_INV_ARG;
840 if (rc)
841 return rc;
843 if (client->crypto->save.sigpkey)
844 gcry_sexp_release (client->crypto->save.sigpkey);
846 client->crypto->save.sigpkey = NULL;
847 return get_pubkey (client->crypto, value, &client->crypto->save.sigpkey);
848 #else
849 return GPG_ERR_INV_ARG;
850 #endif
853 static gpg_error_t
854 parse_opt_s2k_count (void *data, void *value)
856 struct client_s *client = data;
857 char *v = value;
859 if (!v || !*v)
860 return GPG_ERR_INV_VALUE;
862 client->crypto->save.s2k_count = strtoul (v, NULL, 10);
863 return 0;
866 static gpg_error_t
867 parse_save_opt_iterations (void *data, void *value)
869 struct client_s *client = data;
870 char *v = value, *p;
871 uint64_t n;
873 if (!v || !*v)
874 return GPG_ERR_INV_VALUE;
876 errno = 0;
877 n = strtoull (v, &p, 10);
878 if (n == UINT64_MAX && errno)
879 return gpg_error_from_errno (errno);
880 else if (p && *p)
881 return GPG_ERR_INV_VALUE;
883 if (!(client->flags & FLAG_NEW))
885 gpg_error_t rc = peer_is_invoker (client);
887 if (rc == GPG_ERR_EACCES)
889 if (client->crypto->hdr.iterations != n)
890 return rc;
892 rc = 0;
894 else if (rc)
895 return rc;
898 client->crypto->save.hdr.iterations = n;
899 return 0;
902 static gpg_error_t
903 save_finalize (assuan_context_t ctx)
905 struct client_s *client = assuan_get_pointer (ctx);
906 gpg_error_t rc = 0;
907 xmlChar *xmlbuf = NULL;
908 int xmlbuflen;
909 void *key = NULL;
910 size_t keylen = 0;
912 xmlDocDumpFormatMemory (client->doc, &xmlbuf, &xmlbuflen, 0);
913 if (!xmlbuf)
914 return GPG_ERR_ENOMEM;
916 pthread_cleanup_push (xmlFree, xmlbuf);
918 if (!use_agent || ((client->flags & FLAG_NEW)
919 && (client->opts & OPT_NO_AGENT))
920 || !(client->crypto->hdr.flags & PWMD_FLAG_PKI))
922 rc = export_common (ctx, client->flags & FLAG_NO_PINENTRY,
923 client->crypto, xmlbuf, xmlbuflen, client->filename,
924 NULL, &key, &keylen, 1, 1,
925 (client->opts & OPT_NO_PASSPHRASE));
927 #ifdef WITH_AGENT
928 else
930 gcry_sexp_t pubkey = client->crypto->save.pkey;
931 gcry_sexp_t sigkey = client->crypto->save.sigpkey;
933 if (!pubkey)
934 pubkey = client->crypto->pkey_sexp;
936 if (!sigkey)
938 sigkey = client->crypto->sigpkey_sexp;
939 if (!sigkey)
941 gcry_sexp_build (&client->crypto->save.sigpkey, 0, "%S", pubkey);
942 sigkey = client->crypto->save.sigpkey;
946 rc = encrypt_data_file (client->ctx, client->crypto, pubkey, sigkey,
947 client->filename, xmlbuf, xmlbuflen);
948 if (pubkey == client->crypto->save.pkey)
950 if (!rc)
952 gcry_sexp_release (client->crypto->pkey_sexp);
953 client->crypto->pkey_sexp = client->crypto->save.pkey;
954 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
955 client->crypto->grip);
957 else
958 gcry_sexp_release (pubkey);
960 client->crypto->save.pkey = NULL;
963 if (!rc)
964 gcry_pk_get_keygrip (sigkey, client->crypto->sign_grip);
966 if (sigkey == client->crypto->save.sigpkey)
968 if (!rc)
970 if (client->crypto->sigpkey_sexp)
971 gcry_sexp_release (client->crypto->sigpkey_sexp);
973 client->crypto->sigpkey_sexp = client->crypto->save.sigpkey;
974 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
975 client->crypto->sign_grip);
977 else
978 gcry_sexp_release (sigkey);
980 client->crypto->save.sigpkey = NULL;
983 #endif
985 if (!rc)
987 int cached;
989 rc = save_common (client->filename, client->crypto, xmlbuf, xmlbuflen,
990 key, keylen, &cached, client->opts & OPT_NO_AGENT);
991 if (rc)
992 gcry_free (key);
994 if (!rc && (!cached || (client->flags & FLAG_NEW)))
995 send_status_all (STATUS_CACHE, NULL);
997 if (!rc)
999 rc = update_checksum (client);
1000 client->flags &= ~(FLAG_NEW);
1004 pthread_cleanup_pop (1); // xmlFree
1005 return rc;
1008 static gpg_error_t
1009 parse_opt_reset (void *data, void *value)
1011 struct client_s *client = data;
1013 (void) value;
1014 client->opts |= OPT_RESET;
1015 return 0;
1018 static gpg_error_t
1019 save_command (assuan_context_t ctx, char *line)
1021 struct client_s *client = assuan_get_pointer (ctx);
1022 gpg_error_t rc;
1023 struct stat st;
1024 struct argv_s *args[] = {
1025 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG,
1026 parse_opt_no_passphrase},
1027 &(struct argv_s) {"cipher", OPTION_TYPE_ARG, parse_save_opt_cipher},
1028 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
1029 parse_save_opt_inquire},
1030 &(struct argv_s) {"keygrip", OPTION_TYPE_ARG, parse_save_opt_keygrip},
1031 &(struct argv_s) {"sign-keygrip", OPTION_TYPE_ARG,
1032 parse_save_opt_sign_keygrip},
1033 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
1034 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
1035 &(struct argv_s) {"cipher-iterations", OPTION_TYPE_ARG,
1036 parse_save_opt_iterations},
1037 &(struct argv_s) {"no-agent", OPTION_TYPE_NOARG,
1038 parse_save_opt_no_agent},
1039 NULL
1042 cleanup_save (&client->crypto->save);
1043 memcpy (&client->crypto->save.hdr, &client->crypto->hdr,
1044 sizeof (file_header_t));
1045 client->crypto->save.s2k_count =
1046 config_get_ulong (client->filename, "s2k_count");
1048 if (client->flags & FLAG_NEW)
1049 client->crypto->save.hdr.iterations =
1050 config_get_ulonglong (client->filename, "cipher_iterations");
1052 rc = parse_options (&line, args, client);
1053 if (rc)
1054 return send_error (ctx, rc);
1056 if (!(client->flags & FLAG_NEW))
1057 client->opts &= ~OPT_NO_AGENT;
1058 else if (client->opts & OPT_NO_AGENT)
1060 client->crypto->save.hdr.flags &= ~PWMD_FLAG_PKI;
1061 client->crypto->hdr.flags &= ~PWMD_FLAG_PKI;
1064 if ((client->opts & OPT_NO_PASSPHRASE) && !(client->flags & FLAG_NEW)
1065 && !(client->opts & OPT_INQUIRE))
1066 return send_error (ctx, GPG_ERR_WRONG_KEY_USAGE);
1068 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
1069 return send_error (ctx, gpg_error_from_errno (errno));
1071 if (errno != ENOENT && !S_ISREG (st.st_mode))
1073 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
1074 return send_error (ctx, GPG_ERR_ENOANO);
1077 int defer = 0;
1078 rc = cache_iscached (client->filename, &defer);
1079 if (gpg_err_code (rc) == GPG_ERR_ENOENT && client->flags & FLAG_NEW)
1080 rc = 0;
1081 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
1082 rc = 0;
1084 if ((!rc && defer) || config_get_boolean ("global", "require_save_key"))
1085 client->opts |= OPT_RESET;
1087 if (!rc && (client->opts & OPT_RESET))
1089 rc = cache_clear (client->md5file);
1090 if (rc)
1091 return send_error (ctx, rc);
1093 log_write ("%s: %s", client->filename,
1094 pwmd_strerror (GPG_ERR_KEY_EXPIRED));
1095 send_status_all (STATUS_CACHE, NULL);
1098 if (!rc)
1099 rc = update_element_mtime (client, xmlDocGetRootElement (client->doc));
1101 if (rc)
1102 return send_error (ctx, rc);
1104 #ifdef WITH_AGENT
1105 if (!rc && use_agent && !client->crypto->save.pkey &&
1106 (client->flags & FLAG_NEW || client->opts & OPT_INQUIRE)
1107 && !(client->opts & OPT_NO_AGENT))
1109 rc = set_pinentry_mode (client->crypto->agent,
1110 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
1111 : "ask");
1113 if (!(client->flags & FLAG_NEW))
1115 struct crypto_s *crypto;
1116 char *key = NULL;
1117 size_t keylen = 0;
1119 /* Wanting to generate a new key. Require the key to open the
1120 current file before proceeding reguardless of the
1121 require_save_key configuration parameter. */
1122 rc = cache_clear (client->md5file);
1123 if (!rc)
1125 rc = init_client_crypto (&crypto);
1126 if (!rc)
1127 crypto->client_ctx = client->ctx;
1130 if (!rc)
1131 rc = decrypt_common (client->ctx, client->opts&OPT_INQUIRE, crypto,
1132 client->filename, &key, &keylen);
1134 xfree (key);
1135 cleanup_crypto (&crypto);
1138 if (!rc)
1139 rc = send_status (client->ctx, STATUS_GENKEY, NULL);
1141 if (!rc)
1143 struct inquire_data_s idata = { 0 };
1144 char *params = client->opts & OPT_INQUIRE
1145 ? NULL : default_key_params (client->crypto);
1147 pthread_cleanup_push (xfree, params);
1148 idata.crypto = client->crypto;
1150 if (params)
1152 idata.line = params;
1153 idata.len = strlen (params);
1155 else
1156 idata.preset = 1;
1158 client->crypto->agent->inquire_data = &idata;
1159 client->crypto->agent->inquire_cb = NULL;
1160 rc = generate_key (client->crypto, params,
1161 (client->opts & OPT_NO_PASSPHRASE), 1);
1162 pthread_cleanup_pop (1);
1165 #endif
1167 if (!rc)
1168 rc = save_finalize (ctx);
1170 cleanup_crypto_stage1 (client->crypto);
1171 #ifdef WITH_AGENT
1172 (void) kill_scd (client->crypto->agent);
1173 #endif
1174 return send_error (ctx, rc);
1177 static gpg_error_t
1178 do_delete (assuan_context_t ctx, char *line)
1180 struct client_s *client = assuan_get_pointer (ctx);
1181 gpg_error_t rc;
1182 char **req;
1183 xmlNodePtr n;
1185 if (strchr (line, '\t'))
1186 req = str_split (line, "\t", 0);
1187 else
1188 req = str_split (line, " ", 0);
1190 if (!req || !*req)
1191 return GPG_ERR_SYNTAX;
1193 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1194 if (!n)
1196 strv_free (req);
1197 return rc;
1201 * No sub-node defined. Remove the entire node (root element).
1203 if (!req[1])
1205 if (n)
1207 rc = is_element_owner (client, n);
1208 if (!rc)
1210 rc = unlink_node (client, n);
1211 xmlFreeNode (n);
1215 strv_free (req);
1216 return rc;
1219 n = find_elements (client, client->doc, n->children, req + 1, &rc, NULL,
1220 NULL, NULL, 0, 0, NULL, 0);
1221 strv_free (req);
1222 if (!n)
1223 return rc;
1225 rc = is_element_owner (client, n);
1226 if (!rc)
1228 rc = unlink_node (client, n);
1229 xmlFreeNode (n);
1232 return rc;
1235 static gpg_error_t
1236 delete_command (assuan_context_t ctx, char *line)
1238 struct client_s *client = assuan_get_pointer (ctx);
1239 gpg_error_t rc;
1240 struct argv_s *args[] = {
1241 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1242 NULL
1245 rc = parse_options (&line, args, client);
1246 if (rc)
1247 return send_error (ctx, rc);
1249 if (client->opts & OPT_INQUIRE)
1251 unsigned char *result;
1252 size_t len;
1254 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1255 if (rc)
1256 return send_error (ctx, rc);
1258 line = (char *) result;
1261 rc = do_delete (ctx, line);
1263 if (client->opts & OPT_INQUIRE)
1264 xfree (line);
1266 return send_error (ctx, rc);
1269 static gpg_error_t
1270 store_command (assuan_context_t ctx, char *line)
1272 struct client_s *client = assuan_get_pointer (ctx);
1273 gpg_error_t rc;
1274 size_t len;
1275 unsigned char *result;
1276 char **req;
1277 xmlNodePtr n, parent;
1278 int has_content;
1279 char *content = NULL;
1281 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1282 if (rc)
1283 return send_error (ctx, rc);
1285 req = str_split ((char *) result, "\t", 0);
1286 xfree (result);
1288 if (!req || !*req)
1289 return send_error (ctx, GPG_ERR_SYNTAX);
1291 len = strv_length (req);
1292 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1293 if (*(req + 1) && !valid_element_path (req, has_content))
1295 strv_free (req);
1296 return send_error (ctx, GPG_ERR_INV_VALUE);
1299 if (has_content || !*req[len - 1])
1301 has_content = 1;
1302 content = req[len - 1];
1303 req[len - 1] = NULL;
1306 again:
1307 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1308 if (rc && rc == GPG_ERR_ELEMENT_NOT_FOUND)
1310 rc = new_root_element (client, client->doc, *req);
1311 if (rc)
1313 strv_free (req);
1314 return send_error (ctx, rc);
1317 goto again;
1320 if (!n)
1322 strv_free (req);
1323 return send_error (ctx, rc);
1326 parent = n;
1328 if (req[1] && *req[1])
1330 if (!n->children)
1331 parent = create_elements_cb (client, 1, n, req + 1, &rc, NULL);
1332 else
1333 parent = find_elements (client, client->doc, n->children, req + 1, &rc,
1334 NULL, NULL, create_elements_cb, 0, 0, NULL,
1338 if (!rc && len > 1)
1340 rc = is_element_owner (client, parent);
1341 if (!rc)
1343 n = find_text_node (parent->children);
1344 if (n)
1345 xmlNodeSetContent (n, (xmlChar *) content);
1346 else
1347 xmlNodeAddContent (parent, (xmlChar *) content);
1349 update_element_mtime (client, parent);
1353 xfree (content);
1354 strv_free (req);
1355 return send_error (ctx, rc);
1358 static gpg_error_t
1359 xfer_data (assuan_context_t ctx, const char *line, int total)
1361 int to_send;
1362 int sent = 0;
1363 gpg_error_t rc;
1364 int progress = config_get_integer ("global", "xfer_progress");
1365 int flush = 0;
1367 progress =
1368 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1369 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1370 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1372 if (rc)
1373 return rc;
1375 again:
1378 if (sent + to_send > total)
1379 to_send = total - sent;
1381 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1382 flush ? 0 : to_send);
1383 if (!rc)
1385 sent += flush ? 0 : to_send;
1387 if ((progress && !(sent % progress) && sent != total) ||
1388 (sent == total && flush))
1389 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1391 if (!flush && !rc && sent == total)
1393 flush = 1;
1394 goto again;
1398 while (!rc && sent < total);
1400 return rc;
1403 static gpg_error_t
1404 do_get (assuan_context_t ctx, char *line)
1406 struct client_s *client = assuan_get_pointer (ctx);
1407 gpg_error_t rc;
1408 char **req;
1409 xmlNodePtr n;
1411 req = str_split (line, "\t", 0);
1413 if (!req || !*req)
1415 strv_free (req);
1416 return GPG_ERR_SYNTAX;
1419 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1420 if (!n)
1422 strv_free (req);
1423 return rc;
1426 if (req[1])
1428 find_elements (client, client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1429 0, 0, NULL, 0);
1431 strv_free (req);
1432 if (rc)
1433 return rc;
1435 if (!n || !n->children)
1436 return GPG_ERR_NO_DATA;
1438 n = find_text_node (n->children);
1439 if (!n || !n->content || !*n->content)
1440 return GPG_ERR_NO_DATA;
1442 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1443 return rc;
1446 static gpg_error_t
1447 get_command (assuan_context_t ctx, char *line)
1449 struct client_s *client = assuan_get_pointer (ctx);
1450 gpg_error_t rc;
1451 struct argv_s *args[] = {
1452 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1453 NULL
1456 rc = parse_options (&line, args, client);
1457 if (rc)
1458 return send_error (ctx, rc);
1460 if (client->opts & OPT_INQUIRE)
1462 unsigned char *result;
1463 size_t len;
1465 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1466 if (rc)
1467 return send_error (ctx, rc);
1469 line = (char *) result;
1472 rc = do_get (ctx, line);
1474 if (client->opts & OPT_INQUIRE)
1475 xfree (line);
1477 return send_error (ctx, rc);
1480 static void list_command_cleanup1 (void *arg);
1481 static gpg_error_t
1482 realpath_command (assuan_context_t ctx, char *line)
1484 struct string_s *string = NULL;
1485 gpg_error_t rc;
1486 struct client_s *client = assuan_get_pointer (ctx);
1487 struct argv_s *args[] = {
1488 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1489 NULL
1492 rc = parse_options (&line, args, client);
1493 if (rc)
1494 return send_error (ctx, rc);
1496 if (client->opts & OPT_INQUIRE)
1498 unsigned char *result;
1499 size_t len;
1501 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1502 if (rc)
1503 return send_error (ctx, rc);
1505 line = (char *) result;
1508 rc = build_realpath (client, client->doc, line, &string);
1509 if (!rc)
1511 pthread_cleanup_push (list_command_cleanup1, string);
1512 rc = xfer_data (ctx, string->str, string->len);
1513 pthread_cleanup_pop (1);
1516 if (client->opts & OPT_INQUIRE)
1517 xfree (line);
1519 return send_error (ctx, rc);
1522 static void
1523 list_command_cleanup1 (void *arg)
1525 if (arg)
1526 string_free ((struct string_s *) arg, 1);
1529 static void
1530 list_command_cleanup2 (void *arg)
1532 struct element_list_s *elements = arg;
1534 if (elements)
1536 if (elements->list)
1538 int total = slist_length (elements->list);
1539 int i;
1541 for (i = 0; i < total; i++)
1543 char *tmp = slist_nth_data (elements->list, i);
1544 xfree (tmp);
1547 slist_free (elements->list);
1550 if (elements->prefix)
1551 xfree (elements->prefix);
1553 if (elements->req)
1554 strv_free (elements->req);
1556 xfree (elements);
1560 static gpg_error_t
1561 parse_list_opt_norecurse (void *data, void *value)
1563 struct client_s *client = data;
1565 client->opts &= ~(OPT_LIST_RECURSE);
1566 return 0;
1569 static gpg_error_t
1570 parse_list_opt_verbose (void *data, void *value)
1572 struct client_s *client = data;
1574 client->opts |= OPT_LIST_VERBOSE;
1575 return 0;
1578 static gpg_error_t
1579 parse_list_opt_target (void *data, void *value)
1581 struct client_s *client = data;
1583 client->opts |= OPT_LIST_WITH_TARGET;
1584 return 0;
1587 static gpg_error_t
1588 parse_list_opt_all (void *data, void *value)
1590 struct client_s *client = data;
1592 client->opts |= OPT_LIST_ALL;
1593 return 0;
1596 static gpg_error_t
1597 list_path_once (struct client_s *client, char *line,
1598 struct element_list_s *elements, struct string_s *result)
1600 gpg_error_t rc;
1602 elements->req = str_split (line, " ", 0);
1603 if (!elements->req)
1604 strv_printf (&elements->req, "%s", line);
1606 rc = create_path_list (client, client->doc, elements, *elements->req);
1607 if ((rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES) && elements->verbose)
1608 rc = 0;
1610 if (!rc)
1612 int total = slist_length (elements->list);
1614 if (!total)
1615 rc = GPG_ERR_NO_DATA;
1617 if (!rc)
1619 if (!rc)
1621 int i;
1623 for (i = 0; i < total; i++)
1625 char *tmp = slist_nth_data (elements->list, i);
1627 string_append_printf (result, "%s%s", tmp,
1628 i + 1 == total ? "" : "\n");
1632 else
1633 rc = GPG_ERR_NO_DATA;
1636 return rc;
1639 static int
1640 has_list_flag (char *path, char *flags)
1642 char *p = path;
1644 while (*p && *++p != ' ');
1646 if (!*p)
1647 return 0;
1649 for (; *p; p++)
1651 char *f;
1653 for (f = flags; *f && *f != ' '; f++)
1655 if (*p == *f)
1656 return 1;
1660 return 0;
1663 static gpg_error_t
1664 do_list (assuan_context_t ctx, char *line)
1666 struct client_s *client = assuan_get_pointer (ctx);
1667 gpg_error_t rc;
1668 struct element_list_s *elements = NULL;
1670 elements = xcalloc (1, sizeof (struct element_list_s));
1671 if (!elements)
1672 return GPG_ERR_ENOMEM;
1674 elements->recurse = client->opts & OPT_LIST_RECURSE;
1675 elements->verbose =
1676 (client->opts & OPT_LIST_VERBOSE) | (client->opts & OPT_LIST_WITH_TARGET);
1677 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1679 if (!line || !*line)
1681 struct string_s *str = NULL;
1683 pthread_cleanup_push (list_command_cleanup2, elements);
1684 rc = list_root_elements (client, client->doc, &str, elements->verbose,
1685 elements->with_target);
1686 pthread_cleanup_pop (1);
1687 pthread_cleanup_push (list_command_cleanup1, str);
1689 if (!rc)
1691 if (client->opts & OPT_LIST_ALL)
1693 char **roots = str_split (str->str, "\n", 0);
1694 char **p;
1696 pthread_cleanup_push (req_cleanup, roots);
1697 string_truncate (str, 0);
1699 for (p = roots; *p; p++)
1701 if (strchr (*p, ' '))
1703 if (has_list_flag (*p, "EOP"))
1705 string_append_printf (str, "%s%s", *p,
1706 *(p + 1) ? "\n" : "");
1707 continue;
1711 elements = xcalloc (1, sizeof (struct element_list_s));
1712 if (!elements)
1714 rc = GPG_ERR_ENOMEM;
1715 break;
1718 elements->recurse = client->opts & OPT_LIST_RECURSE;
1719 elements->verbose =
1720 (client->opts & OPT_LIST_VERBOSE) | (client->opts &
1721 OPT_LIST_WITH_TARGET);
1722 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1723 pthread_cleanup_push (list_command_cleanup2, elements);
1724 rc = list_path_once (client, *p, elements, str);
1725 pthread_cleanup_pop (1);
1726 if (rc)
1727 break;
1729 if (*(p + 1))
1730 string_append (str, "\n");
1733 pthread_cleanup_pop (1);
1736 if (!rc)
1737 rc = xfer_data (ctx, str->str, str->len);
1740 pthread_cleanup_pop (1);
1741 return rc;
1744 pthread_cleanup_push (list_command_cleanup2, elements);
1745 struct string_s *str = string_new (NULL);
1746 pthread_cleanup_push (list_command_cleanup1, str);
1747 rc = list_path_once (client, line, elements, str);
1748 if (!rc)
1749 rc = xfer_data (ctx, str->str, str->len);
1751 pthread_cleanup_pop (1);
1752 pthread_cleanup_pop (1);
1753 return rc;
1756 static gpg_error_t
1757 list_command (assuan_context_t ctx, char *line)
1759 struct client_s *client = assuan_get_pointer (ctx);
1760 gpg_error_t rc;
1761 struct argv_s *args[] = {
1762 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG,
1763 parse_list_opt_norecurse},
1764 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_list_opt_verbose},
1765 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1766 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG,
1767 parse_list_opt_target},
1768 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_all},
1769 NULL
1772 if (disable_list_and_dump == 1)
1773 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1775 client->opts |= OPT_LIST_RECURSE;
1776 rc = parse_options (&line, args, client);
1777 if (rc)
1778 return send_error (ctx, rc);
1780 if (client->opts & OPT_LIST_ALL)
1781 client->opts |= OPT_LIST_RECURSE | OPT_LIST_VERBOSE;
1783 if (client->opts & OPT_INQUIRE)
1785 unsigned char *result;
1786 size_t len;
1788 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1789 if (rc)
1790 return send_error (ctx, rc);
1792 line = (char *) result;
1795 rc = do_list (ctx, line);
1797 if (client->opts & OPT_INQUIRE)
1798 xfree (line);
1800 return send_error (ctx, rc);
1804 * req[0] - element path
1806 static gpg_error_t
1807 attribute_list (assuan_context_t ctx, char **req)
1809 struct client_s *client = assuan_get_pointer (ctx);
1810 char **attrlist = NULL;
1811 int i = 0;
1812 char **path = NULL;
1813 xmlAttrPtr a;
1814 xmlNodePtr n, an;
1815 char *line;
1816 gpg_error_t rc;
1818 if (!req || !req[0])
1819 return GPG_ERR_SYNTAX;
1821 if ((path = str_split (req[0], "\t", 0)) == NULL)
1824 * The first argument may be only a root element.
1826 if ((path = str_split (req[0], " ", 0)) == NULL)
1827 return GPG_ERR_SYNTAX;
1830 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1832 if (!n)
1834 strv_free (path);
1835 return rc;
1838 if (path[1])
1840 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1841 NULL, NULL, NULL, 0, 0, NULL, 0);
1843 if (!n)
1845 strv_free (path);
1846 return rc;
1850 strv_free (path);
1852 for (a = n->properties; a; a = a->next)
1854 char **pa;
1856 if ((pa = xrealloc (attrlist, (i + 2) * sizeof (char *))) == NULL)
1858 if (attrlist)
1859 strv_free (attrlist);
1861 log_write ("%s(%i): %s", __FILE__, __LINE__,
1862 pwmd_strerror (GPG_ERR_ENOMEM));
1863 return GPG_ERR_ENOMEM;
1866 attrlist = pa;
1867 an = a->children;
1868 attrlist[i] = str_asprintf ("%s %s", (char *) a->name,
1870 && an->content ? (char *) an->content : "");
1872 if (!attrlist[i])
1874 strv_free (attrlist);
1875 log_write ("%s(%i): %s", __FILE__, __LINE__,
1876 pwmd_strerror (GPG_ERR_ENOMEM));
1877 return GPG_ERR_ENOMEM;
1880 attrlist[++i] = NULL;
1883 if (!attrlist)
1884 return GPG_ERR_NO_DATA;
1886 line = strv_join ("\n", attrlist);
1888 if (!line)
1890 log_write ("%s(%i): %s", __FILE__, __LINE__,
1891 pwmd_strerror (GPG_ERR_ENOMEM));
1892 strv_free (attrlist);
1893 return GPG_ERR_ENOMEM;
1896 pthread_cleanup_push (xfree, line);
1897 pthread_cleanup_push (req_cleanup, attrlist);
1898 rc = xfer_data (ctx, line, strlen (line));
1899 pthread_cleanup_pop (1);
1900 pthread_cleanup_pop (1);
1901 return rc;
1905 * req[0] - attribute
1906 * req[1] - element path
1908 static gpg_error_t
1909 attribute_delete (struct client_s *client, char **req)
1911 xmlNodePtr n;
1912 char **path = NULL;
1913 gpg_error_t rc;
1915 if (!req || !req[0] || !req[1])
1916 return GPG_ERR_SYNTAX;
1918 if (!strcmp (req[0], "_name"))
1919 return GPG_ERR_INV_ATTR;
1921 if ((path = str_split (req[1], "\t", 0)) == NULL)
1924 * The first argument may be only a root element.
1926 if ((path = str_split (req[1], " ", 0)) == NULL)
1927 return GPG_ERR_SYNTAX;
1930 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1931 if (!n)
1932 goto fail;
1934 if (path[1])
1936 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1937 NULL, NULL, NULL, 0, 0, NULL, 0);
1938 if (!n)
1939 goto fail;
1942 if (!strcmp (req[0], (char *) "_acl"))
1944 rc = is_element_owner (client, n);
1945 if (rc)
1946 goto fail;
1949 rc = delete_attribute (client, n, (xmlChar *) req[0]);
1951 fail:
1952 strv_free (path);
1953 return rc;
1956 static xmlNodePtr
1957 create_element_path (struct client_s *client,
1958 char ***elements, gpg_error_t * rc, xmlNodePtr parent)
1960 char **req = *elements;
1961 char **req_orig = strv_dup (req);
1962 xmlNodePtr n = NULL;
1964 *rc = 0;
1966 if (!req_orig)
1968 *rc = GPG_ERR_ENOMEM;
1969 log_write ("%s(%i): %s", __FILE__, __LINE__,
1970 pwmd_strerror (GPG_ERR_ENOMEM));
1971 goto fail;
1974 again:
1975 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
1976 if (!n)
1978 if (*rc != GPG_ERR_ELEMENT_NOT_FOUND)
1979 goto fail;
1981 *rc = new_root_element (client, client->doc, req[0]);
1982 if (*rc)
1983 goto fail;
1985 goto again;
1987 else if (n == parent)
1989 *rc = GPG_ERR_CONFLICT;
1990 goto fail;
1993 if (req[1])
1995 if (!n->children)
1996 n = create_target_elements_cb (client, 1, n, req + 1, rc, NULL);
1997 else
1998 n = find_elements (client, client->doc, n->children, req + 1, rc,
1999 NULL, NULL, create_target_elements_cb, 0, 0,
2000 parent, 0);
2002 if (!n)
2003 goto fail;
2006 * Reset the position of the element tree now that the elements
2007 * have been created.
2009 strv_free (req);
2010 req = req_orig;
2011 req_orig = NULL;
2012 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
2013 if (!n)
2014 goto fail;
2016 n = find_elements (client, client->doc, n->children, req + 1, rc,
2017 NULL, NULL, NULL, 0, 0, NULL, 0);
2018 if (!n)
2019 goto fail;
2022 fail:
2023 if (req_orig)
2024 strv_free (req_orig);
2026 *elements = req;
2027 return n;
2031 * Creates a "target" attribute. When other commands encounter an element with
2032 * this attribute, the element path is modified to the target value. If the
2033 * source element path doesn't exist when using 'ATTR SET target', it is
2034 * created, but the destination element path must exist.
2036 * req[0] - source element path
2037 * req[1] - destination element path
2039 static gpg_error_t
2040 target_attribute (struct client_s *client, char **req)
2042 char **src, **dst, *line = NULL, **odst = NULL;
2043 gpg_error_t rc;
2044 xmlNodePtr n;
2046 if (!req || !req[0] || !req[1])
2047 return GPG_ERR_SYNTAX;
2049 if ((src = str_split (req[0], "\t", 0)) == NULL)
2052 * The first argument may be only a root element.
2054 if ((src = str_split (req[0], " ", 0)) == NULL)
2055 return GPG_ERR_SYNTAX;
2058 if (!valid_element_path (src, 0))
2059 return GPG_ERR_INV_VALUE;
2061 if ((dst = str_split (req[1], "\t", 0)) == NULL)
2064 * The first argument may be only a root element.
2066 if ((dst = str_split (req[1], " ", 0)) == NULL)
2068 rc = GPG_ERR_SYNTAX;
2069 goto fail;
2073 odst = strv_dup (dst);
2074 if (!odst)
2076 rc = GPG_ERR_ENOMEM;
2077 goto fail;
2080 n = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
2082 * Make sure the destination element path exists.
2084 if (!n)
2085 goto fail;
2087 if (dst[1])
2089 n = find_elements (client, client->doc, n->children, dst + 1, &rc,
2090 NULL, NULL, NULL, 0, 0, NULL, 0);
2091 if (!n)
2092 goto fail;
2095 rc = validate_target_attribute (client, client->doc, req[0], n);
2096 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2097 goto fail;
2099 n = create_element_path (client, &src, &rc, NULL);
2100 if (rc)
2101 goto fail;
2103 line = strv_join ("\t", odst);
2104 if (!line)
2106 rc = GPG_ERR_ENOMEM;
2107 goto fail;
2110 rc = add_attribute (client, n, "target", line);
2112 fail:
2113 xfree (line);
2114 strv_free (src);
2115 strv_free (dst);
2116 strv_free (odst);
2117 return rc;
2121 * req[0] - attribute
2122 * req[1] - element path
2124 static gpg_error_t
2125 attribute_get (assuan_context_t ctx, char **req)
2127 struct client_s *client = assuan_get_pointer (ctx);
2128 xmlNodePtr n;
2129 xmlChar *a;
2130 char **path = NULL;
2131 gpg_error_t rc;
2133 if (!req || !req[0] || !req[1])
2134 return GPG_ERR_SYNTAX;
2136 if (strchr (req[1], '\t'))
2138 if ((path = str_split (req[1], "\t", 0)) == NULL)
2139 return GPG_ERR_SYNTAX;
2141 else
2143 if ((path = str_split (req[1], " ", 0)) == NULL)
2144 return GPG_ERR_SYNTAX;
2147 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2149 if (!n)
2150 goto fail;
2152 if (path[1])
2154 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2155 NULL, NULL, NULL, 0, 0, NULL, 0);
2157 if (!n)
2158 goto fail;
2161 strv_free (path);
2163 if ((a = xmlGetProp (n, (xmlChar *) req[0])) == NULL)
2164 return GPG_ERR_NOT_FOUND;
2166 pthread_cleanup_push (xmlFree, a);
2168 if (*a)
2169 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
2170 else
2171 rc = GPG_ERR_NO_DATA;
2173 pthread_cleanup_pop (1);
2174 return rc;
2176 fail:
2177 strv_free (path);
2178 return rc;
2182 * req[0] - attribute
2183 * req[1] - element path
2184 * req[2] - value
2186 static gpg_error_t
2187 attribute_set (struct client_s *client, char **req)
2189 char **path = NULL;
2190 gpg_error_t rc;
2191 xmlNodePtr n;
2193 if (!req || !req[0] || !req[1])
2194 return GPG_ERR_SYNTAX;
2197 * Reserved attribute names.
2199 if (!strcmp (req[0], "_name"))
2200 return GPG_ERR_INV_ATTR;
2201 else if (!strcmp (req[0], "target"))
2202 return target_attribute (client, req + 1);
2203 else if (!valid_xml_attribute (req[0]))
2204 return GPG_ERR_INV_VALUE;
2206 if ((path = str_split (req[1], "\t", 0)) == NULL)
2209 * The first argument may be only a root element.
2211 if ((path = str_split (req[1], " ", 0)) == NULL)
2212 return GPG_ERR_SYNTAX;
2215 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2217 if (!n)
2218 goto fail;
2220 if (path[1])
2222 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2223 NULL, NULL, NULL, 0, 0, NULL, 0);
2225 if (!n)
2226 goto fail;
2229 if (!strcmp (req[0], (char *) "_acl"))
2231 rc = is_element_owner (client, n);
2232 if (rc)
2233 goto fail;
2236 rc = add_attribute (client, n, req[0], req[2]);
2238 fail:
2239 strv_free (path);
2240 return rc;
2244 * req[0] - command
2245 * req[1] - attribute name or element path if command is LIST
2246 * req[2] - element path
2247 * req[2] - element path or value
2250 static gpg_error_t
2251 do_attr (assuan_context_t ctx, char *line)
2253 struct client_s *client = assuan_get_pointer (ctx);
2254 gpg_error_t rc = 0;
2255 char **req;
2257 req = str_split (line, " ", 4);
2258 if (!req || !req[0] || !req[1])
2260 strv_free (req);
2261 return GPG_ERR_SYNTAX;
2264 pthread_cleanup_push (req_cleanup, req);
2266 if (strcasecmp (req[0], "SET") == 0)
2267 rc = attribute_set (client, req + 1);
2268 else if (strcasecmp (req[0], "GET") == 0)
2269 rc = attribute_get (ctx, req + 1);
2270 else if (strcasecmp (req[0], "DELETE") == 0)
2271 rc = attribute_delete (client, req + 1);
2272 else if (strcasecmp (req[0], "LIST") == 0)
2273 rc = attribute_list (ctx, req + 1);
2274 else
2275 rc = GPG_ERR_SYNTAX;
2277 pthread_cleanup_pop (1);
2278 return rc;
2281 static gpg_error_t
2282 attr_command (assuan_context_t ctx, char *line)
2284 struct client_s *client = assuan_get_pointer (ctx);
2285 gpg_error_t rc;
2286 struct argv_s *args[] = {
2287 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2288 NULL
2291 rc = parse_options (&line, args, client);
2292 if (rc)
2293 return send_error (ctx, rc);
2295 if (client->opts & OPT_INQUIRE)
2297 unsigned char *result;
2298 size_t len;
2300 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2301 if (rc)
2302 return send_error (ctx, rc);
2304 line = (char *) result;
2307 rc = do_attr (ctx, line);
2309 if (client->opts & OPT_INQUIRE)
2310 xfree (line);
2312 return send_error (ctx, rc);
2315 static gpg_error_t
2316 parse_iscached_opt_lock (void *data, void *value)
2318 struct client_s *client = data;
2320 (void) value;
2321 client->opts |= OPT_LOCK;
2322 return 0;
2325 static gpg_error_t
2326 iscached_command (assuan_context_t ctx, char *line)
2328 struct client_s *client = assuan_get_pointer (ctx);
2329 gpg_error_t rc;
2330 struct argv_s *args[] = {
2331 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2332 NULL
2335 if (!line || !*line)
2336 return send_error (ctx, GPG_ERR_SYNTAX);
2338 rc = parse_options (&line, args, client);
2339 if (rc)
2340 return send_error (ctx, rc);
2341 else if (!valid_filename (line))
2342 return send_error (ctx, GPG_ERR_INV_VALUE);
2344 rc = cache_iscached (line, NULL);
2345 if (client->opts & OPT_LOCK
2346 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2348 unsigned char md5file[16];
2349 gpg_error_t trc = rc;
2351 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2352 if (memcmp (md5file, client->md5file, 16))
2353 cleanup_client (client);
2355 memcpy (client->md5file, md5file, 16);
2356 rc = do_lock (client, 1);
2357 if (!rc)
2358 rc = trc;
2361 return send_error (ctx, rc);
2364 static gpg_error_t
2365 clearcache_command (assuan_context_t ctx, char *line)
2367 gpg_error_t rc = 0, all_rc = 0;
2368 unsigned char md5file[16];
2369 int i;
2370 int t;
2371 int all = 0;
2372 struct client_thread_s *once = NULL;
2374 cache_lock ();
2375 MUTEX_LOCK (&cn_mutex);
2376 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
2378 if (!line || !*line)
2379 all = 1;
2381 t = slist_length (cn_thread_list);
2383 for (i = 0; i < t; i++)
2385 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2386 assuan_peercred_t peer;
2388 if (!thd->cl)
2389 continue;
2391 /* Lock each connected clients' file mutex to prevent any other client
2392 * from accessing the cache entry (the file mutex is locked upon
2393 * command startup). The cache for the entry is not cleared if the
2394 * file mutex is locked by another client to prevent this function
2395 * from blocking.
2397 if (all)
2399 if (thd->cl->filename)
2401 rc = do_validate_peer (ctx, thd->cl->filename, &peer);
2402 all_rc = !all_rc ? rc : all_rc;
2403 if (rc)
2405 rc = 0;
2406 continue;
2410 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
2411 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2413 if (pthread_equal (pthread_self (), thd->tid))
2414 rc = 0;
2415 else
2417 if (!thd->cl->filename ||
2418 cache_iscached (thd->cl->filename,
2419 NULL) == GPG_ERR_NO_DATA)
2421 rc = 0;
2422 continue;
2425 cache_defer_clear (thd->cl->md5file);
2428 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2430 rc = 0;
2431 continue;
2434 if (!rc)
2436 rc = cache_clear (thd->cl->md5file);
2437 cache_unlock_mutex (thd->cl->md5file, 0);
2440 if (rc)
2441 all_rc = rc;
2443 rc = 0;
2445 /* A single data filename was specified. Lock only this data file
2446 * mutex and free the cache entry. */
2447 else
2449 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2450 rc = do_validate_peer (ctx, line, &peer);
2452 if (!rc && !memcmp (thd->cl->md5file, md5file, sizeof (md5file)))
2454 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0,
2455 -1);
2456 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2458 if (pthread_equal (pthread_self (), thd->tid))
2459 rc = 0;
2462 if (!rc)
2464 once = thd;
2465 rc = cache_clear (thd->cl->md5file);
2466 cache_unlock_mutex (thd->cl->md5file, 0);
2468 else
2470 cache_defer_clear (thd->cl->md5file);
2473 break;
2478 /* Only connected clients' cache entries have been cleared. Now clear any
2479 * remaining cache entries without clients but only if there wasn't an
2480 * error from above since this would defeat the locking check of the
2481 * remaining entries. */
2482 if (!all_rc && all)
2484 cache_clear (NULL);
2487 /* No clients are using the specified file. */
2488 else if (!all_rc && !rc && !once)
2490 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2491 rc = cache_clear (md5file);
2494 /* Release the connection mutex. */
2495 pthread_cleanup_pop (1);
2496 cache_unlock ();
2498 if (!rc)
2499 send_status_all (STATUS_CACHE, NULL);
2501 /* One or more files were locked while clearing all cache entries. */
2502 if (all_rc)
2503 rc = all_rc;
2505 return send_error (ctx, rc);
2508 static gpg_error_t
2509 cachetimeout_command (assuan_context_t ctx, char *line)
2511 int timeout;
2512 char **req = str_split (line, " ", 0);
2513 char *p;
2514 gpg_error_t rc = 0;
2515 assuan_peercred_t peer;
2517 if (!req || !*req || !req[1])
2519 strv_free (req);
2520 return send_error (ctx, GPG_ERR_SYNTAX);
2523 errno = 0;
2524 timeout = (int) strtol (req[1], &p, 10);
2525 if (errno != 0 || *p || timeout < -1)
2527 strv_free (req);
2528 return send_error (ctx, GPG_ERR_SYNTAX);
2531 rc = do_validate_peer (ctx, req[0], &peer);
2532 if (!rc)
2534 unsigned char md5file[16];
2536 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, req[0], strlen (req[0]));
2537 rc = cache_set_timeout (md5file, timeout);
2538 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2540 rc = 0;
2541 MUTEX_LOCK (&rcfile_mutex);
2542 config_set_int_param (&global_config, req[0], "cache_timeout",
2543 req[1]);
2544 MUTEX_UNLOCK (&rcfile_mutex);
2548 strv_free (req);
2549 return send_error (ctx, rc);
2552 static gpg_error_t
2553 dump_command (assuan_context_t ctx, char *line)
2555 xmlChar *xml;
2556 int len;
2557 struct client_s *client = assuan_get_pointer (ctx);
2558 gpg_error_t rc;
2560 if (disable_list_and_dump == 1)
2561 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2563 rc = peer_is_invoker(client);
2564 if (rc)
2565 return send_error (ctx, rc);
2567 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2569 if (!xml)
2571 log_write ("%s(%i): %s", __FILE__, __LINE__,
2572 pwmd_strerror (GPG_ERR_ENOMEM));
2573 return send_error (ctx, GPG_ERR_ENOMEM);
2576 pthread_cleanup_push (xmlFree, xml);
2577 rc = xfer_data (ctx, (char *) xml, len);
2578 pthread_cleanup_pop (1);
2579 return send_error (ctx, rc);
2582 static gpg_error_t
2583 getconfig_command (assuan_context_t ctx, char *line)
2585 struct client_s *client = assuan_get_pointer (ctx);
2586 gpg_error_t rc = 0;
2587 char filename[255] = { 0 }, param[747] = { 0 };
2588 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2590 if (!line || !*line)
2591 return send_error (ctx, GPG_ERR_SYNTAX);
2593 if (strchr (line, ' '))
2595 sscanf (line, " %254[^ ] %746c", filename, param);
2596 paramp = param;
2597 fp = filename;
2600 if (fp && !valid_filename (fp))
2601 return send_error (ctx, GPG_ERR_INV_VALUE);
2603 paramp = str_down (paramp);
2604 if (!strcmp (paramp, "cipher") && fp)
2606 struct crypto_s *crypto = NULL;
2608 rc = init_client_crypto (&crypto);
2609 if (!rc)
2611 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2612 if (!rc)
2614 const char *t =
2615 gcry_cipher_algo_name (cipher_to_gcrypt (crypto->hdr.flags));
2616 if (t)
2618 tmp = str_dup (t);
2619 if (tmp)
2620 str_down (tmp);
2625 UPDATE_AGENT_CTX (client, crypto);
2626 cleanup_crypto (&crypto);
2627 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2628 return send_error (ctx, rc);
2630 if (!rc && tmp)
2631 goto done;
2633 else if (!strcmp (paramp, "cipher_iterations") && fp)
2635 struct crypto_s *crypto = NULL;
2637 rc = init_client_crypto (&crypto);
2638 if (!rc)
2640 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2641 if (!rc)
2643 tmp = str_asprintf ("%llu",
2644 (unsigned long long) crypto->hdr.
2645 iterations);
2646 if (!tmp)
2647 rc = GPG_ERR_ENOMEM;
2651 UPDATE_AGENT_CTX (client, crypto);
2652 cleanup_crypto (&crypto);
2653 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2654 return send_error (ctx, rc);
2656 if (!rc && tmp)
2657 goto done;
2659 else if (!strcmp (paramp, "passphrase"))
2660 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2662 p = config_get_value (fp ? fp : "global", paramp);
2663 if (!p)
2664 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2666 tmp = expand_homedir (p);
2667 xfree (p);
2668 if (!tmp)
2670 log_write ("%s(%i): %s", __FILE__, __LINE__,
2671 pwmd_strerror (GPG_ERR_ENOMEM));
2672 return send_error (ctx, GPG_ERR_ENOMEM);
2675 done:
2676 p = tmp;
2677 pthread_cleanup_push (xfree, p);
2678 rc = xfer_data (ctx, p, strlen (p));
2679 pthread_cleanup_pop (1);
2680 return send_error (ctx, rc);
2683 struct xpath_s
2685 xmlXPathContextPtr xp;
2686 xmlXPathObjectPtr result;
2687 xmlBufferPtr buf;
2688 char **req;
2691 static void
2692 xpath_command_cleanup (void *arg)
2694 struct xpath_s *xpath = arg;
2696 if (!xpath)
2697 return;
2699 req_cleanup (xpath->req);
2701 if (xpath->buf)
2702 xmlBufferFree (xpath->buf);
2704 if (xpath->result)
2705 xmlXPathFreeObject (xpath->result);
2707 if (xpath->xp)
2708 xmlXPathFreeContext (xpath->xp);
2711 static gpg_error_t
2712 do_xpath (assuan_context_t ctx, char *line)
2714 gpg_error_t rc;
2715 struct client_s *client = assuan_get_pointer (ctx);
2716 struct xpath_s _x = { 0 };
2717 struct xpath_s *xpath = &_x;
2719 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2721 if (strv_printf (&xpath->req, "%s", line) == 0)
2722 return GPG_ERR_ENOMEM;
2725 xpath->xp = xmlXPathNewContext (client->doc);
2726 if (!xpath->xp)
2728 rc = GPG_ERR_BAD_DATA;
2729 goto fail;
2732 xpath->result =
2733 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2734 if (!xpath->result)
2736 rc = GPG_ERR_BAD_DATA;
2737 goto fail;
2740 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2742 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2743 goto fail;
2746 rc = recurse_xpath_nodeset (client, client->doc, xpath->result->nodesetval,
2747 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2748 NULL);
2749 if (rc)
2750 goto fail;
2751 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2753 rc = GPG_ERR_NO_DATA;
2754 goto fail;
2756 else if (xpath->req[1])
2758 rc = 0;
2759 goto fail;
2762 pthread_cleanup_push (xpath_command_cleanup, &xpath);
2763 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2764 xmlBufferLength (xpath->buf));
2765 pthread_cleanup_pop (0);
2766 fail:
2767 xpath_command_cleanup (xpath);
2768 return rc;
2771 static gpg_error_t
2772 xpath_command (assuan_context_t ctx, char *line)
2774 struct client_s *client = assuan_get_pointer (ctx);
2775 gpg_error_t rc;
2776 struct argv_s *args[] = {
2777 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2778 NULL
2781 if (disable_list_and_dump == 1)
2782 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2784 rc = peer_is_invoker(client);
2785 if (rc)
2786 return send_error (ctx, rc);
2788 rc = parse_options (&line, args, client);
2789 if (rc)
2790 return send_error (ctx, rc);
2792 if (client->opts & OPT_INQUIRE)
2794 unsigned char *result;
2795 size_t len;
2797 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2798 if (rc)
2799 return send_error (ctx, rc);
2801 line = (char *) result;
2804 if (!line || !*line)
2805 rc = GPG_ERR_SYNTAX;
2807 if (!rc)
2808 rc = do_xpath (ctx, line);
2810 if (client->opts & OPT_INQUIRE)
2811 xfree (line);
2813 return send_error (ctx, rc);
2816 static gpg_error_t
2817 do_xpathattr (assuan_context_t ctx, char *line)
2819 struct client_s *client = assuan_get_pointer (ctx);
2820 gpg_error_t rc;
2821 char **req = NULL;
2822 int cmd = 0; //SET
2823 struct xpath_s _x = { 0 };
2824 struct xpath_s *xpath = &_x;
2826 if ((req = str_split (line, " ", 3)) == NULL)
2827 return GPG_ERR_ENOMEM;
2829 if (!req[0])
2831 rc = GPG_ERR_SYNTAX;
2832 goto fail;
2835 if (!strcasecmp (req[0], "SET"))
2836 cmd = 0;
2837 else if (!strcasecmp (req[0], "DELETE"))
2838 cmd = 1;
2839 else
2841 rc = GPG_ERR_SYNTAX;
2842 goto fail;
2845 if (!req[1] || !req[2])
2847 rc = GPG_ERR_SYNTAX;
2848 goto fail;
2851 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2853 rc = GPG_ERR_ENOMEM;
2854 goto fail;
2857 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2859 rc = GPG_ERR_SYNTAX;
2860 goto fail;
2863 xpath->xp = xmlXPathNewContext (client->doc);
2864 if (!xpath->xp)
2866 rc = GPG_ERR_BAD_DATA;
2867 goto fail;
2870 xpath->result = xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2871 if (!xpath->result)
2873 rc = GPG_ERR_BAD_DATA;
2874 goto fail;
2877 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2879 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2880 goto fail;
2883 rc = recurse_xpath_nodeset (client, client->doc, xpath->result->nodesetval,
2884 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2885 (xmlChar *) req[1]);
2887 fail:
2888 xpath_command_cleanup (xpath);
2889 strv_free (req);
2890 return rc;
2893 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2894 static gpg_error_t
2895 xpathattr_command (assuan_context_t ctx, char *line)
2897 struct client_s *client = assuan_get_pointer (ctx);
2898 gpg_error_t rc;
2899 struct argv_s *args[] = {
2900 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2901 NULL
2904 if (disable_list_and_dump == 1)
2905 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2907 rc = peer_is_invoker(client);
2908 if (rc)
2909 return send_error (ctx, rc);
2911 rc = parse_options (&line, args, client);
2912 if (rc)
2913 return send_error (ctx, rc);
2915 if (client->opts & OPT_INQUIRE)
2917 unsigned char *result;
2918 size_t len;
2920 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2921 if (rc)
2922 return send_error (ctx, rc);
2924 line = (char *) result;
2927 if (!line || !*line)
2928 rc = GPG_ERR_SYNTAX;
2930 if (!rc)
2931 rc = do_xpathattr (ctx, line);
2933 if (client->opts & OPT_INQUIRE)
2934 xfree (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 if (!value || !*value)
3274 pthread_setspecific (thread_name_key, str_dup (""));
3275 goto done;
3278 pthread_setspecific (thread_name_key, str_dup (value));
3279 goto done;
3281 else if (strcasecmp (name, (char *) "lc-messages") == 0)
3283 xfree (pin_opts->lc_messages);
3284 pin_opts->lc_messages = NULL;
3285 if (value && *value)
3286 pin_opts->lc_messages = str_dup (value);
3287 #ifdef WITH_AGENT
3288 if (use_agent)
3289 rc = set_agent_option (client->crypto->agent, "lc-messages", value);
3290 #endif
3292 else if (strcasecmp (name, (char *) "lc-ctype") == 0)
3294 xfree (pin_opts->lc_ctype);
3295 pin_opts->lc_ctype = NULL;
3296 if (value && *value)
3297 pin_opts->lc_ctype = str_dup (value);
3298 #ifdef WITH_AGENT
3299 if (use_agent)
3300 rc = set_agent_option (client->crypto->agent, "lc-ctype", value);
3301 #endif
3303 else if (strcasecmp (name, (char *) "ttyname") == 0)
3305 xfree (pin_opts->ttyname);
3306 pin_opts->ttyname = NULL;
3307 if (value && *value)
3308 pin_opts->ttyname = str_dup (value);
3309 #ifdef WITH_AGENT
3310 if (use_agent)
3311 rc = set_agent_option (client->crypto->agent, "ttyname", value);
3312 #endif
3314 else if (strcasecmp (name, (char *) "ttytype") == 0)
3316 xfree (pin_opts->ttytype);
3317 pin_opts->ttytype = NULL;
3318 if (value && *value)
3319 pin_opts->ttytype = str_dup (value);
3320 #ifdef WITH_AGENT
3321 if (use_agent)
3322 rc = set_agent_option (client->crypto->agent, "ttytype", value);
3323 #endif
3325 else if (strcasecmp (name, (char *) "display") == 0)
3327 xfree (pin_opts->display);
3328 pin_opts->display = NULL;
3329 if (value && *value)
3330 pin_opts->display = str_dup (value);
3331 #ifdef WITH_AGENT
3332 if (use_agent)
3333 rc = set_agent_option (client->crypto->agent, "display", value);
3334 #endif
3336 else if (strcasecmp (name, (char *) "pinentry-desc") == 0)
3338 xfree (pin_opts->desc);
3339 pin_opts->desc = NULL;
3340 if (value && *value)
3341 pin_opts->desc = str_dup (value);
3343 else if (strcasecmp (name, (char *) "pinentry-title") == 0)
3345 xfree (pin_opts->title);
3346 pin_opts->title = NULL;
3347 if (value && *value)
3348 pin_opts->title = str_dup (value);
3350 else if (strcasecmp (name, (char *) "pinentry-prompt") == 0)
3352 xfree (pin_opts->prompt);
3353 pin_opts->prompt = NULL;
3354 if (value && *value)
3355 pin_opts->prompt = str_dup (value);
3358 else if (strcasecmp (name, "pinentry-timeout") == 0)
3360 char *p = NULL;
3361 int n;
3363 if (!value)
3364 goto done;
3366 n = (int) strtol (value, &p, 10);
3368 if (*p || n < 0)
3369 return send_error (ctx, GPG_ERR_INV_VALUE);
3371 pin_opts->timeout = n;
3372 MUTEX_LOCK (&rcfile_mutex);
3373 config_set_int_param (&global_config,
3374 client->filename ? client->filename : "global",
3375 "pinentry_timeout", value);
3376 MUTEX_UNLOCK (&rcfile_mutex);
3377 goto done;
3379 else if (strcasecmp (name, "disable-pinentry") == 0)
3381 char *p = NULL;
3382 int n = 1;
3384 if (value && *value)
3386 n = (int) strtol (value, &p, 10);
3387 if (*p || n < 0 || n > 1)
3388 return send_error (ctx, GPG_ERR_INV_VALUE);
3391 if (n)
3392 client->flags |= FLAG_NO_PINENTRY;
3393 else
3394 client->flags &= ~FLAG_NO_PINENTRY;
3396 #ifdef WITH_AGENT
3397 if (use_agent)
3399 if (client->flags & FLAG_NO_PINENTRY)
3400 rc = set_agent_option (client->crypto->agent, "pinentry-mode",
3401 "loopback");
3402 else
3403 rc = set_agent_option (client->crypto->agent, "pinentry-mode",
3404 "ask");
3406 if (rc)
3407 return send_error (ctx, rc);
3409 #endif
3411 else
3412 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
3414 done:
3415 #ifdef WITH_AGENT
3416 if (!rc && use_agent && agent)
3418 rc = pinentry_merge_options (&client->crypto->agent->pinentry_opts,
3419 pin_opts);
3421 #endif
3423 return send_error (ctx, rc);
3426 static gpg_error_t
3427 do_rename (assuan_context_t ctx, char *line)
3429 struct client_s *client = assuan_get_pointer (ctx);
3430 gpg_error_t rc;
3431 char **req, **src, *dst;
3432 xmlNodePtr n, ndst;
3434 req = str_split (line, " ", 0);
3436 if (!req || !req[0] || !req[1])
3438 strv_free (req);
3439 return GPG_ERR_SYNTAX;
3442 dst = req[1];
3443 is_literal_element (&dst);
3445 if (!valid_xml_element ((xmlChar *) dst))
3447 strv_free (req);
3448 return GPG_ERR_INV_VALUE;
3451 if (strchr (req[0], '\t'))
3452 src = str_split (req[0], "\t", 0);
3453 else
3454 src = str_split (req[0], " ", 0);
3456 if (!src || !*src)
3458 rc = GPG_ERR_SYNTAX;
3459 goto fail;
3462 n = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3463 if (src[1] && n)
3464 n = find_elements (client, client->doc, n->children, src + 1, &rc, NULL, NULL,
3465 NULL, 0, 0, NULL, 0);
3467 if (!n)
3468 goto fail;
3470 rc = is_element_owner (client, n);
3471 if (rc)
3472 goto fail;
3474 xmlChar *a = xmlGetProp (n, (xmlChar *) "_name");
3475 if (!a)
3477 rc = GPG_ERR_ENOMEM;
3478 goto fail;
3481 /* To prevent unwanted effects:
3483 * <root name="a"><b/></root>
3485 * RENAME a<TAB>b b
3487 if (xmlStrEqual (a, (xmlChar *) dst))
3489 xmlFree (a);
3490 rc = GPG_ERR_AMBIGUOUS_NAME;
3491 goto fail;
3494 xmlFree (a);
3495 char **tmp = NULL;
3496 if (src[1])
3498 char **p;
3500 for (p = src; *p; p++)
3502 if (!*(p + 1))
3503 break;
3504 strv_printf (&tmp, "%s", *p);
3508 strv_printf (&tmp, "!%s", dst);
3509 ndst = find_root_element (client, client->doc, &tmp, &rc, NULL, 0, 0);
3510 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3512 strv_free (tmp);
3513 goto fail;
3516 if (tmp[1] && ndst)
3517 ndst = find_elements (client, client->doc, ndst->children, tmp + 1, &rc, NULL,
3518 NULL, NULL, 0, 0, NULL, 0);
3520 strv_free (tmp);
3521 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3522 goto fail;
3524 rc = 0;
3526 /* Target may exist:
3528 * <root name="a"/>
3529 * <root name="b" target="a"/>
3531 * RENAME b a
3533 * Would need to do:
3534 * RENAME !b a
3536 if (ndst == n)
3538 rc = GPG_ERR_AMBIGUOUS_NAME;
3539 goto fail;
3542 if (ndst)
3544 rc = is_element_owner (client, ndst);
3545 if (rc)
3546 goto fail;
3548 unlink_node (client, ndst);
3549 xmlFreeNodeList (ndst);
3552 rc = add_attribute (client, n, "_name", dst);
3554 fail:
3555 strv_free (req);
3556 strv_free (src);
3557 return rc;
3560 static gpg_error_t
3561 rename_command (assuan_context_t ctx, char *line)
3563 struct client_s *client = assuan_get_pointer (ctx);
3564 gpg_error_t rc;
3565 struct argv_s *args[] = {
3566 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3567 NULL
3570 rc = parse_options (&line, args, client);
3571 if (rc)
3572 return send_error (ctx, rc);
3574 if (client->opts & OPT_INQUIRE)
3576 unsigned char *result;
3577 size_t len;
3579 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3580 if (rc)
3581 return send_error (ctx, rc);
3583 line = (char *) result;
3586 rc = do_rename (ctx, line);
3588 if (client->opts & OPT_INQUIRE)
3589 xfree (line);
3591 return send_error (ctx, rc);
3594 static gpg_error_t
3595 do_copy (assuan_context_t ctx, char *line)
3597 struct client_s *client = assuan_get_pointer (ctx);
3598 gpg_error_t rc;
3599 char **req, **src = NULL, **dst = NULL;
3600 xmlNodePtr nsrc, ndst, new = NULL;
3602 req = str_split (line, " ", 0);
3603 if (!req || !req[0] || !req[1])
3605 strv_free (req);
3606 return GPG_ERR_SYNTAX;
3609 if (strchr (req[0], '\t'))
3610 src = str_split (req[0], "\t", 0);
3611 else
3612 src = str_split (req[0], " ", 0);
3614 if (!src || !*src)
3616 rc = GPG_ERR_SYNTAX;
3617 goto fail;
3620 if (strchr (req[1], '\t'))
3621 dst = str_split (req[1], "\t", 0);
3622 else
3623 dst = str_split (req[1], " ", 0);
3625 if (!dst || !*dst)
3627 rc = GPG_ERR_SYNTAX;
3628 goto fail;
3631 if (!valid_element_path (dst, 0))
3633 rc = GPG_ERR_INV_VALUE;
3634 goto fail;
3637 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3638 if (nsrc && src[1])
3639 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc, NULL,
3640 NULL, NULL, 0, 0, NULL, 0);
3642 if (!nsrc)
3643 goto fail;
3645 new = xmlCopyNodeList (nsrc);
3646 if (!new)
3648 rc = GPG_ERR_ENOMEM;
3649 goto fail;
3652 int create = 0;
3653 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3654 if (ndst && dst[1])
3656 if (ndst->children)
3657 ndst = find_elements (client, client->doc, ndst->children, dst + 1, &rc, NULL,
3658 NULL, create_target_elements_cb, 0, 0, NULL, 0);
3659 else
3660 create = 1;
3662 else
3663 create = 1;
3665 if (!ndst && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3666 goto fail;
3667 else if (!ndst || create)
3669 ndst = create_element_path (client, &dst, &rc, NULL);
3670 if (!ndst)
3671 goto fail;
3674 rc = is_element_owner (client, ndst);
3675 if (rc)
3676 goto fail;
3678 /* Merge any attributes from the src node to the initial dst node. */
3679 for (xmlAttrPtr attr = new->properties; attr; attr = attr->next)
3681 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3682 continue;
3684 xmlAttrPtr a = xmlHasProp (ndst, attr->name);
3685 if (a)
3686 xmlRemoveProp (a);
3688 xmlChar *tmp = xmlNodeGetContent (attr->children);
3689 xmlNewProp (ndst, attr->name, tmp);
3690 xmlFree (tmp);
3691 rc = add_attribute (client, ndst, NULL, NULL);
3694 xmlNodePtr n = ndst->children;
3695 xmlUnlinkNode (n);
3696 xmlFreeNodeList (n);
3697 ndst->children = NULL;
3699 if (new->children)
3701 n = xmlCopyNodeList (new->children);
3702 if (!n)
3704 rc = GPG_ERR_ENOMEM;
3705 goto fail;
3708 n = xmlAddChildList (ndst, n);
3709 if (!n)
3711 rc = GPG_ERR_ENOMEM;
3712 goto fail;
3715 rc = update_element_mtime (client, xmlDocGetRootElement (client->doc) ==
3716 ndst->parent ? ndst : ndst->parent);
3719 fail:
3720 if (new)
3722 xmlUnlinkNode (new);
3723 xmlFreeNodeList (new);
3726 if (req)
3727 strv_free (req);
3729 if (src)
3730 strv_free (src);
3732 if (dst)
3733 strv_free (dst);
3735 return rc;
3738 static gpg_error_t
3739 copy_command (assuan_context_t ctx, char *line)
3741 struct client_s *client = assuan_get_pointer (ctx);
3742 gpg_error_t rc;
3743 struct argv_s *args[] = {
3744 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3745 NULL
3748 rc = parse_options (&line, args, client);
3749 if (rc)
3750 return send_error (ctx, rc);
3752 if (client->opts & OPT_INQUIRE)
3754 unsigned char *result;
3755 size_t len;
3757 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3758 if (rc)
3759 return send_error (ctx, rc);
3761 line = (char *) result;
3764 rc = do_copy (ctx, line);
3766 if (client->opts & OPT_INQUIRE)
3767 xfree (line);
3769 return send_error (ctx, rc);
3772 static gpg_error_t
3773 do_move (assuan_context_t ctx, char *line)
3775 struct client_s *client = assuan_get_pointer (ctx);
3776 gpg_error_t rc;
3777 char **req, **src = NULL, **dst = NULL;
3778 xmlNodePtr nsrc, ndst = NULL;
3780 req = str_split (line, " ", 0);
3782 if (!req || !req[0] || !req[1])
3784 strv_free (req);
3785 return GPG_ERR_SYNTAX;
3788 if (strchr (req[0], '\t'))
3789 src = str_split (req[0], "\t", 0);
3790 else
3791 src = str_split (req[0], " ", 0);
3793 if (!src || !*src)
3795 rc = GPG_ERR_SYNTAX;
3796 goto fail;
3799 if (strchr (req[1], '\t'))
3800 dst = str_split (req[1], "\t", 0);
3801 else
3802 dst = str_split (req[1], " ", 0);
3804 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3805 if (nsrc && src[1])
3806 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc,
3807 NULL, NULL, NULL, 0, 0, NULL, 0);
3809 if (!nsrc)
3810 goto fail;
3812 rc = is_element_owner (client, nsrc);
3813 if (rc)
3814 goto fail;
3816 if (dst)
3818 if (!valid_element_path (dst, 0))
3820 rc = GPG_ERR_INV_VALUE;
3821 goto fail;
3824 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3825 if (ndst && dst[1])
3826 ndst = find_elements (client, client->doc, ndst->children, dst + 1,
3827 &rc, NULL, NULL, NULL, 0, 0, NULL, 0);
3829 else
3830 ndst = xmlDocGetRootElement (client->doc);
3832 for (xmlNodePtr n = ndst; n; n = n->parent)
3834 if (n == nsrc)
3836 rc = GPG_ERR_CONFLICT;
3837 goto fail;
3841 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3842 goto fail;
3844 rc = 0;
3846 if (ndst)
3848 xmlChar *a = node_has_attribute (nsrc, (xmlChar *) "_name");
3850 xmlNodePtr dup = find_element (client, ndst->children, (char *) a,
3851 NULL, &rc);
3852 xmlFree (a);
3854 if (rc)
3855 goto fail;
3857 if (dup)
3859 if (dup == nsrc)
3860 goto fail;
3862 if (ndst == xmlDocGetRootElement (client->doc))
3864 xmlNodePtr n = nsrc;
3865 int match = 0;
3867 while (n->parent && n->parent != ndst)
3868 n = n->parent;
3870 xmlChar *a = node_has_attribute (n, (xmlChar *) "_name");
3871 xmlChar *b = node_has_attribute (nsrc, (xmlChar *) "_name");
3873 if (xmlStrEqual (a, b))
3875 match = 1;
3876 xmlUnlinkNode (nsrc);
3877 xmlUnlinkNode (n);
3878 xmlFreeNodeList (n);
3881 xmlFree (a);
3882 xmlFree (b);
3884 if (!match)
3886 xmlUnlinkNode (dup);
3887 xmlFreeNodeList (dup);
3890 else
3891 xmlUnlinkNode (dup);
3895 if (!ndst && dst)
3897 xmlChar *name = node_has_attribute (nsrc, (xmlChar *) "_name");
3899 if (nsrc->parent == xmlDocGetRootElement (client->doc)
3900 && !strcmp ((char *) name, *dst))
3902 xmlFree (name);
3903 rc = GPG_ERR_CONFLICT;
3904 goto fail;
3907 xmlFree (name);
3908 ndst = create_element_path (client, &dst, &rc, nsrc);
3911 if (!ndst)
3912 goto fail;
3914 update_element_mtime (client, nsrc->parent);
3915 xmlUnlinkNode (nsrc);
3916 ndst = xmlAddChildList (ndst, nsrc);
3918 if (!ndst)
3919 rc = GPG_ERR_ENOMEM;
3920 else
3921 update_element_mtime (client, ndst->parent);
3923 fail:
3924 if (req)
3925 strv_free (req);
3927 if (src)
3928 strv_free (src);
3930 if (dst)
3931 strv_free (dst);
3933 return rc;
3936 static gpg_error_t
3937 move_command (assuan_context_t ctx, char *line)
3939 struct client_s *client = assuan_get_pointer (ctx);
3940 gpg_error_t rc;
3941 struct argv_s *args[] = {
3942 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3943 NULL
3946 rc = parse_options (&line, args, client);
3947 if (rc)
3948 return send_error (ctx, rc);
3950 if (client->opts & OPT_INQUIRE)
3952 unsigned char *result;
3953 size_t len;
3955 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3956 if (rc)
3957 return send_error (ctx, rc);
3959 line = (char *) result;
3962 rc = do_move (ctx, line);
3964 if (client->opts & OPT_INQUIRE)
3965 xfree (line);
3967 return send_error (ctx, rc);
3970 static gpg_error_t
3971 ls_command (assuan_context_t ctx, char *line)
3973 gpg_error_t rc;
3974 char *tmp = str_asprintf ("%s/data", homedir);
3975 char *dir = expand_homedir (tmp);
3976 DIR *d = opendir (dir);
3978 rc = gpg_error_from_errno (errno);
3979 xfree (tmp);
3981 if (!d)
3983 xfree (dir);
3984 return send_error (ctx, rc);
3987 size_t len =
3988 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3989 struct dirent *p = xmalloc (len), *cur = NULL;
3990 char *list = NULL;
3992 xfree (dir);
3993 rc = 0;
3995 while (!readdir_r (d, p, &cur) && cur)
3997 if (cur->d_name[0] == '.' && cur->d_name[1] == '\0')
3998 continue;
3999 else if (cur->d_name[0] == '.' && cur->d_name[1] == '.'
4000 && cur->d_name[2] == '\0')
4001 continue;
4003 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
4005 if (!tmp)
4007 if (list)
4008 xfree (list);
4010 rc = GPG_ERR_ENOMEM;
4011 break;
4014 xfree (list);
4015 list = tmp;
4018 closedir (d);
4019 xfree (p);
4021 if (rc)
4022 return send_error (ctx, rc);
4024 if (!list)
4025 return send_error (ctx, GPG_ERR_NO_DATA);
4027 list[strlen (list) - 1] = 0;
4028 rc = xfer_data (ctx, list, strlen (list));
4029 xfree (list);
4030 return send_error (ctx, rc);
4033 static gpg_error_t
4034 bye_notify (assuan_context_t ctx, char *line)
4036 struct client_s *cl = assuan_get_pointer (ctx);
4038 #ifdef WITH_GNUTLS
4039 if (cl->thd->remote)
4041 int rc;
4045 struct timeval tv = { 0, 50000 };
4047 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
4048 if (rc == GNUTLS_E_AGAIN)
4049 select (0, NULL, NULL, NULL, &tv);
4051 while (rc == GNUTLS_E_AGAIN);
4053 #endif
4055 /* This will let assuan_process_next() return. */
4056 fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK);
4057 cl->last_rc = 0; // BYE command result
4058 return 0;
4061 static gpg_error_t
4062 reset_notify (assuan_context_t ctx, char *line)
4064 struct client_s *client = assuan_get_pointer (ctx);
4066 if (client)
4067 cleanup_client (client);
4069 return 0;
4073 * This is called before every Assuan command.
4075 static gpg_error_t
4076 command_startup (assuan_context_t ctx, const char *name)
4078 struct client_s *client = assuan_get_pointer (ctx);
4079 gpg_error_t rc;
4080 struct command_table_s *cmd = NULL;
4082 log_write1 ("command='%s'", name);
4083 client->last_rc = client->opts = 0;
4085 for (int i = 0; command_table[i]; i++)
4087 if (!strcasecmp (name, command_table[i]->name))
4089 if (command_table[i]->ignore_startup)
4090 return 0;
4091 cmd = command_table[i];
4092 break;
4096 client->last_rc = rc = gpg_error (file_modified (client, cmd));
4097 return rc;
4101 * This is called after every Assuan command.
4103 static void
4104 command_finalize (assuan_context_t ctx, gpg_error_t rc)
4106 struct client_s *client = assuan_get_pointer (ctx);
4108 if (!(client->flags & FLAG_LOCK_CMD))
4109 unlock_file_mutex (client, 0);
4111 log_write1 (_("command completed: rc=%u"), rc ? rc : client->last_rc);
4112 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
4113 #ifdef WITH_GNUTLS
4114 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
4115 #endif
4118 static gpg_error_t
4119 help_command (assuan_context_t ctx, char *line)
4121 gpg_error_t rc;
4122 int i;
4124 if (!line || !*line)
4126 char *tmp;
4127 char *help = str_dup (_("Usage: HELP [<COMMAND>]\n"
4128 "For commands that take an element path as an argument, each element is "
4129 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
4130 "\n" "COMMANDS:"));
4132 for (i = 0; command_table[i]; i++)
4134 if (!command_table[i]->help)
4135 continue;
4137 tmp = str_asprintf ("%s %s", help, command_table[i]->name);
4138 xfree (help);
4139 help = tmp;
4142 tmp = strip_texi_and_wrap (help);
4143 xfree (help);
4144 rc = xfer_data (ctx, tmp, strlen (tmp));
4145 xfree (tmp);
4146 return send_error (ctx, rc);
4149 for (i = 0; command_table[i]; i++)
4151 if (!strcasecmp (line, command_table[i]->name))
4153 char *help, *tmp;
4155 if (!command_table[i]->help)
4156 break;
4158 help = strip_texi_and_wrap (command_table[i]->help);
4159 tmp = str_asprintf (_("Usage: %s"), help);
4160 xfree (help);
4161 rc = xfer_data (ctx, tmp, strlen (tmp));
4162 xfree (tmp);
4163 return send_error (ctx, rc);
4167 return send_error (ctx, GPG_ERR_INV_NAME);
4170 static void
4171 new_command (const char *name, int ignore, int unlock,
4172 gpg_error_t (*handler) (assuan_context_t, char *),
4173 const char *help)
4175 int i = 0;
4177 if (command_table)
4178 for (i = 0; command_table[i]; i++);
4180 command_table =
4181 xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
4182 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
4183 command_table[i]->name = name;
4184 command_table[i]->handler = handler;
4185 command_table[i]->ignore_startup = ignore;
4186 command_table[i]->unlock = unlock;
4187 command_table[i++]->help = help;
4188 command_table[i] = NULL;
4191 void
4192 deinit_commands ()
4194 int i;
4196 for (i = 0; command_table[i]; i++)
4197 xfree (command_table[i]);
4199 xfree (command_table);
4202 static int
4203 sort_commands (const void *arg1, const void *arg2)
4205 struct command_table_s *const *a = arg1;
4206 struct command_table_s *const *b = arg2;
4208 if (!*a || !*b)
4209 return 0;
4210 else if (*a && !*b)
4211 return 1;
4212 else if (!*a && *b)
4213 return -1;
4215 return strcmp ((*a)->name, (*b)->name);
4218 static gpg_error_t
4219 passwd_command (assuan_context_t ctx, char *line)
4221 struct client_s *client = assuan_get_pointer (ctx);
4222 gpg_error_t rc;
4223 struct argv_s *args[] = {
4224 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
4225 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
4226 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG, parse_opt_no_passphrase},
4227 NULL
4230 rc = peer_is_invoker (client);
4231 if (rc == GPG_ERR_EACCES)
4232 return send_error (ctx, rc);
4234 if (client->flags & FLAG_NEW)
4235 return send_error (ctx, GPG_ERR_INV_STATE);
4237 client->crypto->save.s2k_count =
4238 config_get_ulong (client->filename, "s2k_count");
4239 rc = parse_options (&line, args, client);
4240 if (rc)
4241 return send_error (ctx, rc);
4243 if (!rc && client->opts & OPT_RESET)
4245 rc = cache_clear (client->md5file);
4246 if (!rc)
4247 send_status_all (STATUS_CACHE, NULL);
4250 if (!rc)
4252 if (!IS_PKI (client->crypto))
4254 struct crypto_s *crypto;
4256 xfree (client->crypto->filename);
4257 client->crypto->filename = str_dup (client->filename);
4258 rc = change_passwd (ctx, client->filename,
4259 client->flags & FLAG_NO_PINENTRY, &crypto,
4260 (client->opts & OPT_NO_PASSPHRASE));
4261 if (!rc)
4263 cleanup_crypto (&client->crypto);
4264 client->crypto = crypto;
4265 update_checksum (client);
4266 cleanup_crypto_stage1 (client->crypto);
4269 #ifdef WITH_AGENT
4270 else
4272 if (client->crypto->save.s2k_count)
4273 rc = send_to_agent (client->crypto->agent, NULL, NULL,
4274 "OPTION s2k-count=%lu",
4275 client->crypto->save.s2k_count);
4277 if (!rc)
4278 rc = agent_passwd (client->crypto);
4280 #endif
4283 return send_error (ctx, rc);
4286 static gpg_error_t
4287 parse_keygrip_opt_sign (void *data, void *value)
4289 struct client_s *client = data;
4291 (void) value;
4292 client->opts |= OPT_SIGN;
4293 return 0;
4296 static gpg_error_t
4297 keygrip_command (assuan_context_t ctx, char *line)
4299 struct client_s *client = assuan_get_pointer (ctx);
4300 gpg_error_t rc;
4301 struct crypto_s *crypto = NULL;
4302 struct argv_s *args[] = {
4303 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_keygrip_opt_sign},
4304 NULL
4307 if (!line || !*line)
4308 return send_error (ctx, GPG_ERR_SYNTAX);
4310 rc = parse_options (&line, args, client);
4311 if (rc)
4312 return send_error (ctx, rc);
4314 if (!valid_filename (line))
4315 return send_error (ctx, GPG_ERR_INV_VALUE);
4317 rc = init_client_crypto (&crypto);
4318 if (rc)
4319 return send_error (ctx, rc);
4321 rc = read_data_file (line, crypto);
4322 if (!rc)
4324 char *hexgrip = NULL;
4326 if (!IS_PKI (crypto))
4328 cleanup_crypto (&crypto);
4329 return send_error (ctx, GPG_ERR_NOT_SUPPORTED);
4332 if (client->opts & OPT_SIGN)
4334 if (valid_keygrip (crypto->sign_grip, sizeof (crypto->sign_grip)))
4335 hexgrip = bin2hex (crypto->sign_grip, sizeof (crypto->sign_grip));
4338 if (!hexgrip)
4339 hexgrip = bin2hex (crypto->grip, sizeof (crypto->grip));
4341 if (!hexgrip)
4342 rc = GPG_ERR_ENOMEM;
4343 else
4344 rc = xfer_data (ctx, hexgrip, strlen (hexgrip));
4346 xfree (hexgrip);
4349 UPDATE_AGENT_CTX (client, crypto);
4350 cleanup_crypto (&crypto);
4351 return send_error (ctx, rc);
4354 static gpg_error_t
4355 parse_opt_data (void *data, void *value)
4357 struct client_s *client = data;
4359 (void) value;
4360 client->opts |= OPT_DATA;
4361 return 0;
4364 static gpg_error_t
4365 getinfo_command (assuan_context_t ctx, char *line)
4367 struct client_s *client = assuan_get_pointer (ctx);
4368 gpg_error_t rc;
4369 char buf[ASSUAN_LINELENGTH];
4370 struct argv_s *args[] = {
4371 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4372 NULL
4375 rc = parse_options (&line, args, client);
4376 if (rc)
4377 return send_error (ctx, rc);
4379 if (!strcasecmp (line, "clients"))
4381 if (client->opts & OPT_DATA)
4383 MUTEX_LOCK (&cn_mutex);
4384 snprintf (buf, sizeof (buf), "%i", slist_length (cn_thread_list));
4385 MUTEX_UNLOCK (&cn_mutex);
4386 rc = xfer_data (ctx, buf, strlen (buf));
4388 else
4389 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4391 else if (!strcasecmp (line, "cache"))
4393 if (client->opts & OPT_DATA)
4395 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4396 rc = xfer_data (ctx, buf, strlen (buf));
4398 else
4399 rc = send_status (ctx, STATUS_CACHE, NULL);
4401 else if (!strcasecmp (line, "pid"))
4403 char buf[32];
4404 pid_t pid = getpid ();
4406 snprintf (buf, sizeof (buf), "%i", pid);
4407 rc = xfer_data (ctx, buf, strlen (buf));
4409 else if (!strcasecmp (line, "version"))
4411 char *buf = str_asprintf ("0x%06x %s%s", VERSION_HEX,
4412 #ifdef WITH_GNUTLS
4413 "GNUTLS "
4414 #endif
4415 #ifdef WITH_QUALITY
4416 "QUALITY "
4417 #endif
4418 "", use_agent ? "AGENT" : "");
4419 rc = xfer_data (ctx, buf, strlen (buf));
4420 xfree (buf);
4422 else if (!strcasecmp (line, "last_error"))
4424 if (client->last_error)
4425 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4426 else
4427 rc = GPG_ERR_NO_DATA;
4429 else if (!strcasecmp (line, "user"))
4431 char *user = NULL;
4433 #ifdef WITH_GNUTLS
4434 if (client->thd->remote)
4435 user = str_asprintf ("#%s", client->thd->tls->fp);
4436 else
4437 user = get_username (client->thd->peer->uid);
4438 #else
4439 user = get_username (client->thd->peer->uid);
4440 #endif
4441 if (user)
4442 rc = xfer_data (ctx, user, strlen (user));
4443 else
4444 rc = GPG_ERR_NO_DATA;
4446 xfree (user);
4448 else
4449 rc = gpg_error (GPG_ERR_SYNTAX);
4451 return send_error (ctx, rc);
4454 #ifdef WITH_AGENT
4455 static gpg_error_t
4456 send_data_cb (void *user, const void *buf, size_t len)
4458 assuan_context_t ctx = user;
4460 return assuan_send_data (ctx, buf, len);
4463 static gpg_error_t
4464 send_status_cb (void *user, const char *line)
4466 assuan_context_t ctx = user;
4467 char keyword[200], *k;
4468 const char *p;
4470 for (p = line, k = keyword; *p; p++)
4472 if (isspace (*p))
4473 break;
4475 *k++ = *p;
4478 *k = 0;
4479 if (*p == '#')
4480 p++;
4482 while (isspace (*p))
4483 p++;
4485 return assuan_write_status (ctx, keyword, *p ? p : NULL);
4487 #endif
4489 static gpg_error_t
4490 agent_command (assuan_context_t ctx, char *line)
4492 gpg_error_t rc = 0;
4494 if (!use_agent)
4495 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
4497 #ifdef WITH_AGENT
4498 struct client_s *client = assuan_get_pointer (ctx);
4500 if (!line || !*line)
4501 return send_error (ctx, GPG_ERR_SYNTAX);
4503 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 1);
4504 rc = assuan_transact (client->crypto->agent->ctx, line, send_data_cb,
4505 client->ctx, agent_loopback_cb, client->crypto,
4506 send_status_cb, client->ctx);
4507 if (gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
4509 char *line;
4510 size_t len;
4512 rc = assuan_write_line (client->crypto->agent->ctx, "CAN");
4513 if (!rc)
4515 rc = assuan_read_line (client->crypto->agent->ctx, &line, &len);
4516 if (!rc)
4517 rc = gpg_error (GPG_ERR_ASS_CANCELED);
4521 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 0);
4522 #endif
4523 return send_error (ctx, rc);
4526 void
4527 init_commands ()
4529 /* !BEGIN-HELP-TEXT!
4531 * This comment is used as a marker to generate the offline documentation
4532 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4533 * script to determine where commands begin and end.
4535 new_command("HELP", 1, 1, help_command, _(
4536 "HELP [<COMMAND>]\n"
4537 "Show available commands or command specific help text."
4540 new_command("AGENT", 1, 1, agent_command, _(
4541 "AGENT <command>\n"
4542 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4543 "@command{gpg-agent}."
4546 new_command("GETINFO", 1, 1, getinfo_command, _(
4547 "GETINFO [--data] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4548 "Get server and other information: @var{cache} returns the number of cached "
4549 "documents via a status message. @var{clients} returns the number of "
4550 "connected clients via a status message. @var{pid} returns the process ID "
4551 "number of the server via a data response. @var{VERSION} returns the server "
4552 "version number and compile-time features with a data response with each "
4553 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4554 "the last failed command when available. @var{USER} returns the username or "
4555 "@abbr{TLS} hash of the connected client. @xref{Status Messages}. "
4556 "\n"
4557 "When the @option{--data} option is specified then the result will be sent "
4558 "via a data response rather than a status message."
4561 new_command("PASSWD", 0, 0, passwd_command, _(
4562 "PASSWD [--reset] [--s2k-count=N] [--no-passphrase]\n"
4563 "Changes the passphrase of the secret key required to open the current "
4564 "file or the passphrase of a symmetrically encrypted data file. When the "
4565 "@option{--reset} option is passed then the cache entry for the current "
4566 "file will be reset and the passphrase, if any, will be required during the "
4567 "next @code{OPEN} (@pxref{OPEN})."
4568 "\n"
4569 "The @option{--s2k-count} option sets or changes (@pxref{SAVE}) the number "
4570 "of hash iterations for a passphrase and must be either @code{0} to use "
4571 "the calibrated count of the machine (the default), or a value greater than "
4572 "or equal to @code{65536}. This option has no effect for symmetrically "
4573 "encrypted data files."
4574 "\n"
4575 "The @option{--no-passphrase} option will prevent requiring a passphrase for "
4576 "the data file, although a passphrase may be required when changing it."
4577 "\n"
4578 "This command is not available for non-invoking clients "
4579 "(@pxref{Access Control})."
4582 new_command("KEYGRIP", 1, 1, keygrip_command, _(
4583 "KEYGRIP [--sign] <filename>\n"
4584 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4585 "data response."
4586 "\n"
4587 "When the @option{--sign} option is specified then the key used for signing "
4588 "of the specified @var{filename} will be returned."
4589 "\n"
4590 "For symmetrically encrypted data files this command returns the error "
4591 "GPG_ERR_NOT_SUPPORTED."
4594 new_command("OPEN", 1, 1, open_command, _(
4595 "OPEN [--lock] <filename> [<passphrase>]\n"
4596 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4597 "found on the file-system then a new document will be created. If the file "
4598 "is found, it is looked for in the file cache. If cached and no "
4599 "@var{passphrase} was specified then the cached document is opened. When not "
4600 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4601 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4602 "specified."
4603 "\n"
4604 "When the @option{--lock} option is passed then the file mutex will be "
4605 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4606 "file has been opened."
4609 new_command("SAVE", 0, 0, save_command, _(
4610 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring] [--sign-keygrip=hexstring]\n"
4611 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4612 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4613 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4614 "keypair will be generated and a pinentry will be used to prompt for the "
4615 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4616 "passed in which case the data file will not be passphrase protected. "
4617 "\n"
4618 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4619 "passphrase retrieval and caching of new files when @command{gpg-agent} "
4620 "use is enabled. The datafile will be symmetrically encrypted and will not "
4621 "use or generate any keypair."
4622 "\n"
4623 "The @option{--reset} option will clear the cache entry for the current file "
4624 "and require a passphrase, if needed, before saving."
4625 "\n"
4626 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4627 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4628 "(@pxref{Configuration}) for available ciphers."
4629 "\n"
4630 "The @option{--cipher-iterations} option specifies the number of times to "
4631 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4632 "\n"
4633 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4634 "the client to obtain the key paramaters to use when generating a new "
4635 "keypair. The inquired data is expected to be an S-expression. If not "
4636 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4637 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4638 "that when this option is specified a new keypair will be generated "
4639 "reguardless if the file is a new one and that if the data file is protected "
4640 "the passphrase to open it will be required before generating the new "
4641 "keypair. This option is not available for non-invoking clients "
4642 "(@pxref{Access Control})."
4643 "\n"
4644 "You can encrypt the data file to a public key other than the one that it "
4645 "was originally encrypted with by passing the @option{--keygrip} option with "
4646 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4647 "be of any key that @command{gpg-agent} knows about. The "
4648 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4649 "secret key. Use the @code{KEYGRIP} (@pxref{KEYGRIP}) command to obtain the "
4650 "keygrip of an existing data file. This option may be needed when using a "
4651 "smartcard. This option has no effect with symmetrically encrypted data "
4652 "files. These options are not available for non-invoking clients "
4653 "(@pxref{Access Control})."
4654 "\n"
4655 "The @option{--s2k-count} option sets number of hash iterations for a "
4656 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4657 "value and is the default. This setting only affects new files. To change "
4658 "the setting use the @code{PASSWD} command (@pxref{PASSWD}). This option "
4659 "has no effect with symmetrically encrypted data files."
4662 new_command("ISCACHED", 1, 0, iscached_command, _(
4663 "ISCACHED [--lock] <filename>\n"
4664 "An @emph{OK} response is returned if the specified @var{filename} is found "
4665 "in the file cache. If not found in the cache but exists on the filesystem "
4666 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4667 "returned."
4668 "\n"
4669 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4670 "file exists; it does not need to be opened nor cached. The lock will be "
4671 "released when the client exits or sends the @code{UNLOCK} (@pxref{UNLOCK}) "
4672 "command."
4675 new_command("CLEARCACHE", 1, 1, clearcache_command, _(
4676 "CLEARCACHE [<filename>]\n"
4677 "Clears a file cache entry for all or the specified @var{filename}."
4680 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command, _(
4681 "CACHETIMEOUT <filename> <seconds>\n"
4682 "The time in @var{seconds} until @var{filename} will be removed from the "
4683 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4684 "the passphrase for each @code{OPEN} or @code{SAVE} command (@pxref{OPEN}, "
4685 "@pxref{SAVE}). @xref{Configuration}, and the @code{cache_timeout} "
4686 "parameter."
4689 new_command("LIST", 0, 1, list_command, _(
4690 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4691 "If no element path is given then a newline separated list of root elements "
4692 "is returned with a data response. If given, then all reachable elements "
4693 "of the specified element path are returned unless the @option{--no-recurse} "
4694 "option is specified. If specified, only the child elements of the element "
4695 "path are returned without recursing into grandchildren. Each resulting "
4696 "element is prefixed with the literal @code{!} character when the element "
4697 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4698 "\n"
4699 "When the @option{--verbose} option is passed then each element path "
4700 "returned will have zero or more flags appened to it. These flags are "
4701 "delimited from the element path by a single space character. A flag itself "
4702 "is a single character. Flag @code{P} indicates that access to the element "
4703 "is denied. Flag @code{+} indicates that there are child nodes of "
4704 "the current element path. Flag @code{E} indicates that an element of an "
4705 "element path contained in a @var{target} attribute could not be found. Flag "
4706 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4707 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4708 "of the @var{target} attribute contained in the current element (see below)."
4709 "\n"
4710 "The @option{--with-target} option implies @option{--verbose} and will append "
4711 "an additional flag @code{T} followed by a single space then an element path. "
4712 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4713 "current element when it contains a @var{target} attribute. When no "
4714 "@var{target} attribute is found then no flag will be appended."
4715 "\n"
4716 "The @option{--no-recurse} option limits the amount of data returned to only "
4717 "the listing of children of the specified element path and not any "
4718 "grandchildren."
4719 "\n"
4720 "The @option{--all} option lists the entire element tree for each root "
4721 "element. This option also implies option @option{--verbose}."
4722 "\n"
4723 "When the @option{--inquire} option is passed then all remaining non-option "
4724 "arguments are retrieved via a server @emph{INQUIRE}."
4727 new_command("REALPATH", 0, 1, realpath_command, _(
4728 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4729 "Resolves all @code{target} attributes of the specified element path and "
4730 "returns the result with a data response. @xref{Target Attribute}, for details."
4731 "\n"
4732 "When the @option{--inquire} option is passed then all remaining non-option "
4733 "arguments are retrieved via a server @emph{INQUIRE}."
4736 new_command("STORE", 0, 1, store_command, _(
4737 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4738 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4739 "\n"
4740 "Creates a new element path or modifies the @var{content} of an existing "
4741 "element. If only a single element is specified then a new root element is "
4742 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4743 "set to the final @key{TAB} delimited element. If no @var{content} is "
4744 "specified after the final @key{TAB}, then the content of an existing "
4745 "element will be removed; or empty when creating a new element."
4746 "\n"
4747 "The only restriction of an element name is that it not contain whitespace "
4748 "or begin with the literal element character @code{!} unless specifying a "
4749 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4750 "the @key{TAB} delimited elements. It is recommended that the content of an "
4751 "element be base64 encoded when it contains control or @key{TAB} characters "
4752 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
4755 new_command("RENAME", 0, 1, rename_command, _(
4756 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4757 "Renames the specified @var{element} to the new @var{value}. If an element of "
4758 "the same name as the @var{value} already exists it will be overwritten."
4759 "\n"
4760 "When the @option{--inquire} option is passed then all remaining non-option "
4761 "arguments are retrieved via a server @emph{INQUIRE}."
4764 new_command("COPY", 0, 1, copy_command, _(
4765 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4766 "Copies the entire element tree starting from the child node of the source "
4767 "element, to the destination element path. If the destination element path "
4768 "does not exist then it will be created; otherwise it is overwritten."
4769 "\n"
4770 "Note that attributes from the source element are merged into the "
4771 "destination element when the destination element path exists. When an "
4772 "attribute of the same name exists in both the source and destination "
4773 "elements then the destination attribute will be updated to the source "
4774 "attribute value."
4775 "\n"
4776 "When the @option{--inquire} option is passed then all remaining non-option "
4777 "arguments are retrieved via a server @emph{INQUIRE}."
4780 new_command("MOVE", 0, 1, move_command, _(
4781 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4782 "Moves the source element path to the destination element path. If the "
4783 "destination is not specified then it will be moved to the root node of the "
4784 "document. If the destination is specified and exists then it will be "
4785 "overwritten; otherwise non-existing elements of the destination element "
4786 "path will be created."
4787 "\n"
4788 "When the @option{--inquire} option is passed then all remaining non-option "
4789 "arguments are retrieved via a server @emph{INQUIRE}."
4792 new_command("DELETE", 0, 1, delete_command, _(
4793 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4794 "Removes the specified element path and all of its children. This may break "
4795 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4796 "refers to this element or any of its children."
4797 "\n"
4798 "When the @option{--inquire} option is passed then all remaining non-option "
4799 "arguments are retrieved via a server @emph{INQUIRE}."
4802 new_command("GET", 0, 1, get_command, _(
4803 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4804 "Retrieves the content of the specified element. The content is returned "
4805 "with a data response."
4806 "\n"
4807 "When the @option{--inquire} option is passed then all remaining non-option "
4808 "arguments are retrieved via a server @emph{INQUIRE}."
4811 new_command("ATTR", 0, 1, attr_command, _(
4812 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4813 "@table @asis\n"
4814 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4815 "\n"
4816 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4817 "element. When no @var{value} is specified any existing value will be removed."
4818 "\n"
4819 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4820 "\n"
4821 " Removes an @var{attribute} from an element."
4822 "\n"
4823 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4824 "\n"
4825 " Retrieves a newline separated list of attributes names and values "
4826 "from the specified element. Each attribute name and value is space delimited."
4827 "\n"
4828 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4829 "\n"
4830 " Retrieves the value of an @var{attribute} from an element."
4831 "@end table\n"
4832 "\n"
4833 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4834 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4835 "commands instead."
4836 "\n"
4837 "The @code{_mtime} attribute is updated each time an element is modified by "
4838 "either storing content, editing attributes or by deleting a child element. "
4839 "The @code{_ctime} attribute is created for each new element in an element "
4840 "path."
4841 "\n"
4842 "When the @option{--inquire} option is passed then all remaining non-option "
4843 "arguments are retrieved via a server @emph{INQUIRE}."
4844 "\n"
4845 "@xref{Target Attribute}, for details about this special attribute."
4848 new_command("XPATH", 0, 1, xpath_command, _(
4849 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4850 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4851 "specified it is assumed the expression is a request to return a result. "
4852 "Otherwise, the result is set to the @var{value} argument and the document is "
4853 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4854 "is assumed to be empty and the document is updated. For example:"
4855 "@sp 1\n"
4856 "@example\n"
4857 "XPATH //element[@@_name='password']@key{TAB}\n"
4858 "@end example\n"
4859 "@sp 1"
4860 "would clear the content of all @code{password} elements in the data file "
4861 "while leaving off the trailing @key{TAB} would return all @code{password} "
4862 "elements in @abbr{XML} format."
4863 "\n"
4864 "When the @option{--inquire} option is passed then all remaining non-option "
4865 "arguments are retrieved via a server @emph{INQUIRE}."
4866 "\n"
4867 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4868 "expression syntax."
4871 new_command("XPATHATTR", 0, 1, xpathattr_command, _(
4872 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4873 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4874 "attributes and does not return a result. For the @var{SET} operation the "
4875 "@var{value} is optional but the field is required. If not specified then "
4876 "the attribute value will be empty. For example:"
4877 "@sp 1"
4878 "@example\n"
4879 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4880 "@end example\n"
4881 "@sp 1"
4882 "would create an @code{password} attribute for each @code{password} element "
4883 "found in the document. The attribute value will be empty but still exist."
4884 "\n"
4885 "When the @option{--inquire} option is passed then all remaining non-option "
4886 "arguments are retrieved via a server @emph{INQUIRE}."
4887 "\n"
4888 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4889 "expression syntax."
4892 new_command("IMPORT", 0, 1, import_command, _(
4893 "IMPORT [--root [!]element[<TAB>[!]child[..]]] <content>\n"
4894 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4895 "\n"
4896 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4897 "argument is raw @abbr{XML} data. The content is created as a child of "
4898 "the element path specified with the @option{--root} option or at the "
4899 "document root when not specified. Existing elements of the same name will "
4900 "be overwritten."
4901 "\n"
4902 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4903 "for details."
4906 new_command("DUMP", 0, 1, dump_command, _(
4907 "DUMP\n"
4908 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4909 "dumping a specific node."
4912 new_command("LOCK", 0, 0, lock_command, _(
4913 "LOCK\n"
4914 "Locks the mutex associated with the opened file. This prevents other clients "
4915 "from sending commands to the same opened file until the client "
4916 "that sent this command either disconnects or sends the @code{UNLOCK} "
4917 "command. @xref{UNLOCK}."
4920 new_command("UNLOCK", 1, 0, unlock_command, _(
4921 "UNLOCK\n"
4922 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4923 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4924 "@pxref{ISCACHED})."
4927 new_command("GETCONFIG", 1, 1, getconfig_command, _(
4928 "GETCONFIG [filename] <parameter>\n"
4929 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4930 "data response. If no file has been opened then the value for @var{filename} "
4931 "or the default from the @samp{global} section will be returned. If a file "
4932 "has been opened and no @var{filename} is specified, a value previously "
4933 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4936 new_command("OPTION", 1, 1, option_command, _(
4937 "OPTION <NAME>=<VALUE>\n"
4938 "Sets a client option @var{name} to @var{value}. The value for an option is "
4939 "kept for the duration of the connection."
4940 "\n"
4941 "@table @asis\n"
4942 "@item DISABLE-PINENTRY\n"
4943 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
4944 "server inquire is sent to the client to obtain the passphrase. This option "
4945 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4946 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
4947 "\n"
4948 "@item PINENTRY-TIMEOUT\n"
4949 "Sets the number of seconds before a pinentry prompt will return an error "
4950 "while waiting for user input."
4951 "\n"
4952 "@item TTYNAME\n"
4953 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4954 "\n"
4955 "@item TTYTYPE\n"
4956 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4957 "\n"
4958 "@item DISPLAY\n"
4959 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4960 "\n"
4961 "@item PINENTRY-DESC\n"
4962 "Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4963 "\n"
4964 "@item PINENTRY-TITLE\n"
4965 "Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
4966 "\n"
4967 "@item PINENTRY-PROMPT\n"
4968 "Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
4969 "\n"
4970 "@item LC-CTYPE\n"
4971 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4972 "\n"
4973 "@item LC-MESSAGES\n"
4974 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4975 "\n"
4976 "@item NAME\n"
4977 "Associates the thread ID of the connection with the specified textual "
4978 "representation. Useful for debugging log messages."
4979 "\n"
4980 "@item LOCK-TIMEOUT\n"
4981 "When not @code{0}, the duration in tenths of a second to wait for the file "
4982 "mutex which has been locked by another thread to be released before returning "
4983 "an error. When @code{-1}, then an error will be returned immediately."
4984 "\n"
4985 "@item LOG-LEVEL\n"
4986 "An integer specifiying the logging level."
4987 "@end table\n"
4990 new_command("LS", 1, 1, ls_command, _(
4991 "LS\n"
4992 "Lists the available data files stored in the data directory "
4993 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4996 new_command("RESET", 1, 1, NULL, _(
4997 "RESET\n"
4998 "Closes the currently opened file but keeps any previously set client options."
5001 new_command("NOP", 1, 1, NULL, _(
5002 "NOP\n"
5003 "Does nothing. Always returns successfully."
5006 /* !END-HELP-TEXT! */
5007 new_command ("CANCEL", 1, 1, NULL, NULL);
5008 new_command ("END", 1, 1, NULL, NULL);
5009 new_command ("BYE", 1, 1, NULL, NULL);
5011 int i;
5012 for (i = 0; command_table[i]; i++);
5013 qsort (command_table, i - 1, sizeof (struct command_table_s *),
5014 sort_commands);
5017 gpg_error_t
5018 register_commands (assuan_context_t ctx)
5020 int i = 0, rc;
5022 for (; command_table[i]; i++)
5024 if (!command_table[i]->handler)
5025 continue;
5027 rc = assuan_register_command (ctx, command_table[i]->name,
5028 command_table[i]->handler,
5029 command_table[i]->help);
5030 if (rc)
5031 return rc;
5034 rc = assuan_register_bye_notify (ctx, bye_notify);
5035 if (rc)
5036 return rc;
5038 rc = assuan_register_reset_notify (ctx, reset_notify);
5039 if (rc)
5040 return rc;
5042 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
5043 if (rc)
5044 return rc;
5046 return assuan_register_post_cmd_notify (ctx, command_finalize);