Version 3.0.12.
[pwmd.git] / src / commands.c
blobb3ca83bc5a4a9ba07e81b8d9830382fd4529b671
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)
406 rc = cache_add_file (client->md5file,
407 (client->flags & FLAG_NEW) ? NULL
408 : client->crypto->grip, cdata, timeout);
409 if (rc)
411 if (!cached)
413 free_cache_data_once (cdata);
414 xmlFreeDoc (client->doc);
415 client->doc = NULL;
420 if (!rc)
421 client->flags |= FLAG_OPEN;
425 if (!rc)
426 update_checksum (client);
428 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
429 rc = do_lock (client, 0);
431 return rc;
434 static void
435 req_cleanup (void *arg)
437 if (!arg)
438 return;
440 strv_free ((char **) arg);
443 static gpg_error_t
444 parse_open_opt_lock (void *data, void *value)
446 struct client_s *client = data;
448 client->opts |= OPT_LOCK_ON_OPEN;
449 return 0;
452 static gpg_error_t
453 parse_save_opt_inquire (void *data, void *value)
455 struct client_s *client = data;
456 gpg_error_t rc;
458 if (!(client->flags & FLAG_NEW))
460 rc = peer_is_invoker (client);
461 if (rc == GPG_ERR_EACCES)
462 return rc;
465 (void) value;
466 client->opts |= OPT_INQUIRE;
467 return 0;
470 static gpg_error_t
471 parse_opt_inquire (void *data, void *value)
473 struct client_s *client = data;
475 (void) value;
476 client->opts |= OPT_INQUIRE;
477 return 0;
480 static gpg_error_t
481 update_checksum (struct client_s *client)
483 unsigned char *crc;
484 size_t len;
485 struct cache_data_s *cdata;
486 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
488 if (rc)
489 return rc;
491 xfree (client->crc);
492 client->crc = crc;
493 cdata = cache_get_data (client->md5file);
494 if (cdata)
496 xfree (cdata->crc);
497 cdata->crc = xmalloc (len);
498 memcpy (cdata->crc, crc, len);
501 return 0;
504 static gpg_error_t
505 validate_checksum (struct client_s *client, struct cache_data_s *cdata)
507 unsigned char *crc;
508 size_t len;
509 gpg_error_t rc;
510 int n = 0;
512 if (cdata && !cdata->crc)
513 return GPG_ERR_CHECKSUM;
515 rc = get_checksum (client->filename, &crc, &len);
516 if (rc)
517 return rc;
519 if (cdata)
520 n = memcmp (cdata->crc, crc, len);
521 else if (client->crc)
522 n = memcmp (client->crc, crc, len);
524 xfree (crc);
525 return n ? GPG_ERR_CHECKSUM : 0;
528 static gpg_error_t
529 do_open (assuan_context_t ctx, const char *password)
531 struct client_s *client = assuan_get_pointer (ctx);
532 struct cache_data_s *cdata;
533 gpg_error_t rc = 0;
534 int done = 0;
536 // Cached document?
537 cdata = cache_get_data (client->md5file);
538 if (cdata && cdata->doc)
540 int defer = 0;
542 /* This will check that the key is cached in the agent which needs to
543 * be determined for files that share a keygrip. */
544 if (!rc)
546 rc = cache_iscached (client->filename, &defer);
547 if ((!rc || rc == GPG_ERR_ENOENT) && defer)
548 rc = GPG_ERR_KEY_EXPIRED;
551 if (!rc && !(client->flags & FLAG_NEW))
552 rc = validate_checksum (client, cdata);
554 #ifdef WITH_GNUTLS
555 if (!rc && client->thd->remote
556 && config_get_boolean (client->filename, "tcp_require_key"))
557 rc = GPG_ERR_KEY_EXPIRED;
558 #endif
560 if (!rc && !password)
562 rc = read_data_header (client->filename, &client->crypto->hdr,
563 NULL, NULL);
564 if (!rc)
566 if (IS_PKI (client->crypto))
568 gcry_sexp_build (&client->crypto->pkey_sexp, NULL, "%S",
569 cdata->pubkey);
570 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
571 client->crypto->grip);
572 gcry_sexp_build (&client->crypto->sigpkey_sexp, NULL, "%S",
573 cdata->sigkey);
574 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
575 client->crypto->sign_grip);
578 if (!rc)
580 rc = open_finalize (ctx, NULL, 0);
581 done = 1;
586 /* There was an error accessing the file so clear the cache entry. The
587 * real error will be returned from read_data_file() since the file
588 * may have only disappeared. */
589 if (!done)
591 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
592 rc = cache_clear (client->md5file);
593 send_status_all (STATUS_CACHE, NULL);
597 if (done || rc)
598 return rc;
600 rc = read_data_file (client->filename, client->crypto);
601 if (rc)
603 if (gpg_err_source (rc) != GPG_ERR_SOURCE_DEFAULT ||
604 gpg_err_code (rc) != GPG_ERR_ENOENT)
606 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
607 return rc;
610 client->flags |= FLAG_NEW;
611 memset (client->crypto->grip, 0, sizeof (client->crypto->grip));
612 memset (client->crypto->sign_grip, 0,
613 sizeof (client->crypto->sign_grip));
614 rc = open_finalize (ctx, NULL, 0);
615 return rc;
618 if (password && IS_PKI (client->crypto))
620 #ifdef WITH_AGENT
621 rc = set_agent_passphrase (client->crypto, password, strlen (password));
622 if (rc)
623 return rc;
624 #endif
627 rc = open_finalize (ctx, (char *)password, password ? strlen (password) : 0);
628 return rc;
631 static gpg_error_t
632 open_command (assuan_context_t ctx, char *line)
634 gpg_error_t rc;
635 struct client_s *client = assuan_get_pointer (ctx);
636 char **req, *filename;
637 unsigned char md5file[16];
638 int same_file = 0;
639 assuan_peercred_t peer;
640 struct argv_s *args[] = {
641 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
642 NULL
645 rc = parse_options (&line, args, client);
646 if (rc)
647 return send_error (ctx, rc);
649 req = str_split (line, " ", 2);
650 if (!req)
651 return send_error (ctx, GPG_ERR_SYNTAX);
653 rc = do_validate_peer (ctx, req[0], &peer);
654 if (rc)
656 strv_free (req);
657 return send_error (ctx, rc);
660 filename = req[0];
661 if (!valid_filename (filename))
663 strv_free (req);
664 return send_error (ctx, GPG_ERR_INV_VALUE);
667 pthread_cleanup_push (req_cleanup, req);
668 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, filename, strlen (filename));
669 /* This client may have locked a different file with ISCACHED --lock than
670 * the current filename. This will remove that lock. */
671 same_file = !memcmp (md5file, client->md5file, 16) ? 1 : 0;
672 if (client->flags & FLAG_OPEN ||
673 (client->flags & FLAG_HAS_LOCK && !same_file))
675 uint32_t opts = client->opts;
676 uint32_t flags = client->flags;
678 if (same_file)
679 client->flags |= FLAG_KEEP_LOCK;
680 else if (client->flags & FLAG_NEW)
681 cache_clear (client->md5file);
683 cleanup_client (client);
684 client->opts = opts;
685 client->flags |= flags;
686 client->flags &= ~(FLAG_LOCK_CMD);
687 if (!same_file)
688 client->flags &= ~(FLAG_HAS_LOCK | FLAG_NEW);
691 memcpy (client->md5file, md5file, 16);
692 client->filename = str_dup (filename);
693 if (!client->filename)
695 strv_free (req);
696 return send_error(ctx, GPG_ERR_ENOMEM);
699 /* Need to lock the mutex here because file_modified() cannot without
700 * knowing the filename. */
701 rc = lock_file_mutex (client, 1);
702 if (rc)
703 client->flags &= ~FLAG_OPEN;
705 if (!rc)
707 char *password = req[1] && *req[1] ? req[1] : NULL;
709 #ifdef WITH_AGENT
710 if (IS_PKI (client->crypto))
711 rc = set_pinentry_mode (client->crypto->agent,
712 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
713 : "ask");
714 #endif
715 if (!rc)
717 rc = do_open (ctx, password);
718 if (rc)
719 cleanup_client (client);
721 cleanup_crypto_stage1 (client->crypto);
725 pthread_cleanup_pop (1);
727 if (!rc && client->flags & FLAG_NEW)
728 rc = send_status (ctx, STATUS_NEWFILE, NULL);
730 #ifdef WITH_AGENT
731 (void) kill_scd (client->crypto->agent);
732 #endif
734 return send_error (ctx, rc);
737 static gpg_error_t
738 parse_opt_no_passphrase (void *data, void *value)
740 struct client_s *client = data;
742 (void) value;
743 client->opts |= OPT_NO_PASSPHRASE;
744 return 0;
747 static gpg_error_t
748 parse_save_opt_no_agent (void *data, void *value)
750 struct client_s *client = data;
752 client->opts |= OPT_NO_AGENT;
753 return 0;
756 static gpg_error_t
757 parse_save_opt_cipher (void *data, void *value)
759 struct client_s *client = data;
760 int algo = cipher_string_to_gcrypt ((char *) value);
761 file_header_t *hdr = &client->crypto->save.hdr;
762 gpg_error_t rc = 0;
764 if (algo == -1)
765 return GPG_ERR_INV_VALUE;
767 if (!(client->flags & FLAG_NEW))
769 rc = peer_is_invoker (client);
770 if (rc == GPG_ERR_EACCES)
772 uint64_t flags = 0;
774 flags &= ((uint16_t) ((uint32_t) (flags & 0xFFFFFFFF)));
775 if (!(client->crypto->hdr.flags & gcrypt_to_cipher (algo)))
776 return rc;
778 rc = 0;
780 else if (rc)
781 return rc;
784 if (!rc)
785 hdr->flags = set_cipher_flag (hdr->flags, algo);
787 return rc;
790 #ifdef WITH_AGENT
791 static gpg_error_t
792 permitted_to_save (struct client_s *client, const unsigned char *grip,
793 size_t size, const char *value)
795 char *hexgrip;
796 gpg_error_t rc = 0;
798 if (!(client->flags & FLAG_NEW))
800 hexgrip = bin2hex (grip, size);
801 rc = !strcmp (hexgrip, (char *)value) ? 0 : GPG_ERR_EACCES;
802 xfree (hexgrip);
803 if (rc)
804 rc = peer_is_invoker (client);
807 return rc;
809 #endif
811 static gpg_error_t
812 parse_save_opt_keygrip (void *data, void *value)
814 #ifdef WITH_AGENT
815 struct client_s *client = data;
816 gpg_error_t rc = permitted_to_save (client, client->crypto->grip,
817 sizeof (client->crypto->grip),
818 value);
820 if (!IS_PKI (client->crypto))
821 return GPG_ERR_INV_ARG;
823 if (rc)
824 return rc;
826 if (client->crypto->save.pkey)
827 gcry_sexp_release (client->crypto->save.pkey);
829 client->crypto->save.pkey = NULL;
830 return get_pubkey (client->crypto, value, &client->crypto->save.pkey);
831 #else
832 return GPG_ERR_INV_ARG;
833 #endif
836 static gpg_error_t
837 parse_save_opt_sign_keygrip (void *data, void *value)
839 #ifdef WITH_AGENT
840 struct client_s *client = data;
841 gpg_error_t rc = permitted_to_save (client, client->crypto->sign_grip,
842 sizeof (client->crypto->sign_grip),
843 value);
845 if (!IS_PKI (client->crypto))
846 return GPG_ERR_INV_ARG;
848 if (rc)
849 return rc;
851 if (client->crypto->save.sigpkey)
852 gcry_sexp_release (client->crypto->save.sigpkey);
854 client->crypto->save.sigpkey = NULL;
855 return get_pubkey (client->crypto, value, &client->crypto->save.sigpkey);
856 #else
857 return GPG_ERR_INV_ARG;
858 #endif
861 static gpg_error_t
862 parse_opt_s2k_count (void *data, void *value)
864 struct client_s *client = data;
865 char *v = value;
867 if (!v || !*v)
868 return GPG_ERR_INV_VALUE;
870 client->crypto->save.s2k_count = strtoul (v, NULL, 10);
871 return 0;
874 static gpg_error_t
875 parse_save_opt_iterations (void *data, void *value)
877 struct client_s *client = data;
878 char *v = value, *p;
879 uint64_t n;
881 if (!v || !*v)
882 return GPG_ERR_INV_VALUE;
884 errno = 0;
885 n = strtoull (v, &p, 10);
886 if (n == UINT64_MAX && errno)
887 return gpg_error_from_errno (errno);
888 else if (p && *p)
889 return GPG_ERR_INV_VALUE;
891 if (!(client->flags & FLAG_NEW))
893 gpg_error_t rc = peer_is_invoker (client);
895 if (rc == GPG_ERR_EACCES)
897 if (client->crypto->hdr.iterations != n)
898 return rc;
900 rc = 0;
902 else if (rc)
903 return rc;
906 client->crypto->save.hdr.iterations = n;
907 return 0;
910 static gpg_error_t
911 save_finalize (assuan_context_t ctx)
913 struct client_s *client = assuan_get_pointer (ctx);
914 gpg_error_t rc = 0;
915 xmlChar *xmlbuf = NULL;
916 int xmlbuflen;
917 void *key = NULL;
918 size_t keylen = 0;
920 xmlDocDumpFormatMemory (client->doc, &xmlbuf, &xmlbuflen, 0);
921 if (!xmlbuf)
922 return GPG_ERR_ENOMEM;
924 pthread_cleanup_push (xmlFree, xmlbuf);
926 if (!use_agent || ((client->flags & FLAG_NEW)
927 && (client->opts & OPT_NO_AGENT))
928 || !(client->crypto->hdr.flags & PWMD_FLAG_PKI))
930 rc = export_common (ctx, client->flags & FLAG_NO_PINENTRY,
931 client->crypto, xmlbuf, xmlbuflen, client->filename,
932 NULL, &key, &keylen, 1, 1,
933 (client->opts & OPT_NO_PASSPHRASE));
935 #ifdef WITH_AGENT
936 else
938 gcry_sexp_t pubkey = client->crypto->save.pkey;
939 gcry_sexp_t sigkey = client->crypto->save.sigpkey;
941 if (!pubkey)
942 pubkey = client->crypto->pkey_sexp;
944 if (!sigkey)
946 sigkey = client->crypto->sigpkey_sexp;
947 if (!sigkey)
949 gcry_sexp_build (&client->crypto->save.sigpkey, 0, "%S", pubkey);
950 sigkey = client->crypto->save.sigpkey;
954 rc = encrypt_data_file (client->ctx, client->crypto, pubkey, sigkey,
955 client->filename, xmlbuf, xmlbuflen);
956 if (pubkey == client->crypto->save.pkey)
958 if (!rc)
960 gcry_sexp_release (client->crypto->pkey_sexp);
961 client->crypto->pkey_sexp = client->crypto->save.pkey;
962 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
963 client->crypto->grip);
965 else
966 gcry_sexp_release (pubkey);
968 client->crypto->save.pkey = NULL;
971 if (!rc)
972 gcry_pk_get_keygrip (sigkey, client->crypto->sign_grip);
974 if (sigkey == client->crypto->save.sigpkey)
976 if (!rc)
978 if (client->crypto->sigpkey_sexp)
979 gcry_sexp_release (client->crypto->sigpkey_sexp);
981 client->crypto->sigpkey_sexp = client->crypto->save.sigpkey;
982 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
983 client->crypto->sign_grip);
985 else
986 gcry_sexp_release (sigkey);
988 client->crypto->save.sigpkey = NULL;
991 #endif
993 if (!rc)
995 int cached;
997 rc = save_common (client->filename, client->crypto, xmlbuf, xmlbuflen,
998 key, keylen, &cached, client->opts & OPT_NO_AGENT);
999 if (rc)
1000 gcry_free (key);
1002 if (!rc && (!cached || (client->flags & FLAG_NEW)))
1003 send_status_all (STATUS_CACHE, NULL);
1005 if (!rc)
1007 rc = update_checksum (client);
1008 client->flags &= ~(FLAG_NEW);
1012 pthread_cleanup_pop (1); // xmlFree
1013 return rc;
1016 static gpg_error_t
1017 parse_opt_reset (void *data, void *value)
1019 struct client_s *client = data;
1021 (void) value;
1022 client->opts |= OPT_RESET;
1023 return 0;
1026 static gpg_error_t
1027 save_command (assuan_context_t ctx, char *line)
1029 struct client_s *client = assuan_get_pointer (ctx);
1030 gpg_error_t rc;
1031 struct stat st;
1032 struct argv_s *args[] = {
1033 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG,
1034 parse_opt_no_passphrase},
1035 &(struct argv_s) {"cipher", OPTION_TYPE_ARG, parse_save_opt_cipher},
1036 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
1037 parse_save_opt_inquire},
1038 &(struct argv_s) {"keygrip", OPTION_TYPE_ARG, parse_save_opt_keygrip},
1039 &(struct argv_s) {"sign-keygrip", OPTION_TYPE_ARG,
1040 parse_save_opt_sign_keygrip},
1041 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
1042 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
1043 &(struct argv_s) {"cipher-iterations", OPTION_TYPE_ARG,
1044 parse_save_opt_iterations},
1045 &(struct argv_s) {"no-agent", OPTION_TYPE_NOARG,
1046 parse_save_opt_no_agent},
1047 NULL
1050 cleanup_save (&client->crypto->save);
1051 memcpy (&client->crypto->save.hdr, &client->crypto->hdr,
1052 sizeof (file_header_t));
1053 client->crypto->save.s2k_count =
1054 config_get_ulong (client->filename, "s2k_count");
1056 if (client->flags & FLAG_NEW)
1057 client->crypto->save.hdr.iterations =
1058 config_get_ulonglong (client->filename, "cipher_iterations");
1060 rc = parse_options (&line, args, client);
1061 if (rc)
1062 return send_error (ctx, rc);
1064 if (!(client->flags & FLAG_NEW))
1065 client->opts &= ~OPT_NO_AGENT;
1066 else if (client->opts & OPT_NO_AGENT)
1068 client->crypto->save.hdr.flags &= ~PWMD_FLAG_PKI;
1069 client->crypto->hdr.flags &= ~PWMD_FLAG_PKI;
1072 if ((client->opts & OPT_NO_PASSPHRASE) && !(client->flags & FLAG_NEW)
1073 && !(client->opts & OPT_INQUIRE))
1074 return send_error (ctx, GPG_ERR_WRONG_KEY_USAGE);
1076 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
1077 return send_error (ctx, gpg_error_from_errno (errno));
1079 if (errno != ENOENT && !S_ISREG (st.st_mode))
1081 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
1082 return send_error (ctx, GPG_ERR_ENOANO);
1085 int defer = 0;
1086 rc = cache_iscached (client->filename, &defer);
1087 if (gpg_err_code (rc) == GPG_ERR_ENOENT && client->flags & FLAG_NEW)
1088 rc = 0;
1089 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
1090 rc = 0;
1092 if ((!rc && defer) || config_get_boolean ("global", "require_save_key"))
1093 client->opts |= OPT_RESET;
1095 if (!rc && (client->opts & OPT_RESET))
1097 rc = cache_clear (client->md5file);
1098 if (rc)
1099 return send_error (ctx, rc);
1101 log_write ("%s: %s", client->filename,
1102 pwmd_strerror (GPG_ERR_KEY_EXPIRED));
1103 send_status_all (STATUS_CACHE, NULL);
1106 if (!rc)
1107 rc = update_element_mtime (client, xmlDocGetRootElement (client->doc));
1109 if (rc)
1110 return send_error (ctx, rc);
1112 #ifdef WITH_AGENT
1113 if (!rc && use_agent && !client->crypto->save.pkey &&
1114 (client->flags & FLAG_NEW || client->opts & OPT_INQUIRE)
1115 && !(client->opts & OPT_NO_AGENT))
1117 rc = set_pinentry_mode (client->crypto->agent,
1118 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
1119 : "ask");
1121 if (!(client->flags & FLAG_NEW))
1123 struct crypto_s *crypto;
1124 char *key = NULL;
1125 size_t keylen = 0;
1127 /* Wanting to generate a new key. Require the key to open the
1128 current file before proceeding reguardless of the
1129 require_save_key configuration parameter. */
1130 rc = cache_clear (client->md5file);
1131 if (!rc)
1133 rc = init_client_crypto (&crypto);
1134 if (!rc)
1135 crypto->client_ctx = client->ctx;
1138 if (!rc)
1139 rc = decrypt_common (client->ctx, client->opts&OPT_INQUIRE, crypto,
1140 client->filename, &key, &keylen);
1142 xfree (key);
1143 cleanup_crypto (&crypto);
1146 if (!rc)
1147 rc = send_status (client->ctx, STATUS_GENKEY, NULL);
1149 if (!rc)
1151 struct inquire_data_s idata = { 0 };
1152 char *params = client->opts & OPT_INQUIRE
1153 ? NULL : default_key_params (client->crypto);
1155 pthread_cleanup_push (xfree, params);
1156 idata.crypto = client->crypto;
1158 if (params)
1160 idata.line = params;
1161 idata.len = strlen (params);
1163 else
1164 idata.preset = 1;
1166 client->crypto->agent->inquire_data = &idata;
1167 client->crypto->agent->inquire_cb = NULL;
1168 rc = generate_key (client->crypto, params,
1169 (client->opts & OPT_NO_PASSPHRASE), 1);
1170 pthread_cleanup_pop (1);
1173 #endif
1175 if (!rc)
1176 rc = save_finalize (ctx);
1178 cleanup_crypto_stage1 (client->crypto);
1179 #ifdef WITH_AGENT
1180 (void) kill_scd (client->crypto->agent);
1181 #endif
1182 return send_error (ctx, rc);
1185 static gpg_error_t
1186 do_delete (assuan_context_t ctx, char *line)
1188 struct client_s *client = assuan_get_pointer (ctx);
1189 gpg_error_t rc;
1190 char **req;
1191 xmlNodePtr n;
1193 if (strchr (line, '\t'))
1194 req = str_split (line, "\t", 0);
1195 else
1196 req = str_split (line, " ", 0);
1198 if (!req || !*req)
1199 return GPG_ERR_SYNTAX;
1201 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1202 if (!n)
1204 strv_free (req);
1205 return rc;
1209 * No sub-node defined. Remove the entire node (root element).
1211 if (!req[1])
1213 if (n)
1215 rc = is_element_owner (client, n);
1216 if (!rc)
1218 rc = unlink_node (client, n);
1219 xmlFreeNode (n);
1223 strv_free (req);
1224 return rc;
1227 n = find_elements (client, client->doc, n->children, req + 1, &rc, NULL,
1228 NULL, NULL, 0, 0, NULL, 0);
1229 strv_free (req);
1230 if (!n)
1231 return rc;
1233 rc = is_element_owner (client, n);
1234 if (!rc)
1236 rc = unlink_node (client, n);
1237 xmlFreeNode (n);
1240 return rc;
1243 static gpg_error_t
1244 delete_command (assuan_context_t ctx, char *line)
1246 struct client_s *client = assuan_get_pointer (ctx);
1247 gpg_error_t rc;
1248 struct argv_s *args[] = {
1249 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1250 NULL
1253 rc = parse_options (&line, args, client);
1254 if (rc)
1255 return send_error (ctx, rc);
1257 if (client->opts & OPT_INQUIRE)
1259 unsigned char *result;
1260 size_t len;
1262 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1263 if (rc)
1264 return send_error (ctx, rc);
1266 line = (char *) result;
1269 rc = do_delete (ctx, line);
1271 if (client->opts & OPT_INQUIRE)
1272 xfree (line);
1274 return send_error (ctx, rc);
1277 static gpg_error_t
1278 store_command (assuan_context_t ctx, char *line)
1280 struct client_s *client = assuan_get_pointer (ctx);
1281 gpg_error_t rc;
1282 size_t len;
1283 unsigned char *result;
1284 char **req;
1285 xmlNodePtr n, parent;
1286 int has_content;
1287 char *content = NULL;
1289 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1290 if (rc)
1291 return send_error (ctx, rc);
1293 req = str_split ((char *) result, "\t", 0);
1294 xfree (result);
1296 if (!req || !*req)
1297 return send_error (ctx, GPG_ERR_SYNTAX);
1299 len = strv_length (req);
1300 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1301 if (*(req + 1) && !valid_element_path (req, has_content))
1303 strv_free (req);
1304 return send_error (ctx, GPG_ERR_INV_VALUE);
1307 if (has_content || !*req[len - 1])
1309 has_content = 1;
1310 content = req[len - 1];
1311 req[len - 1] = NULL;
1314 again:
1315 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1316 if (rc && rc == GPG_ERR_ELEMENT_NOT_FOUND)
1318 rc = new_root_element (client, client->doc, *req);
1319 if (rc)
1321 strv_free (req);
1322 return send_error (ctx, rc);
1325 goto again;
1328 if (!n)
1330 strv_free (req);
1331 return send_error (ctx, rc);
1334 parent = n;
1336 if (req[1] && *req[1])
1338 if (!n->children)
1339 parent = create_elements_cb (client, 1, n, req + 1, &rc, NULL);
1340 else
1341 parent = find_elements (client, client->doc, n->children, req + 1, &rc,
1342 NULL, NULL, create_elements_cb, 0, 0, NULL,
1346 if (!rc && len > 1)
1348 rc = is_element_owner (client, parent);
1349 if (!rc)
1351 n = find_text_node (parent->children);
1352 if (n)
1353 xmlNodeSetContent (n, (xmlChar *) content);
1354 else
1355 xmlNodeAddContent (parent, (xmlChar *) content);
1357 update_element_mtime (client, parent);
1361 xfree (content);
1362 strv_free (req);
1363 return send_error (ctx, rc);
1366 static gpg_error_t
1367 xfer_data (assuan_context_t ctx, const char *line, int total)
1369 int to_send;
1370 int sent = 0;
1371 gpg_error_t rc;
1372 int progress = config_get_integer ("global", "xfer_progress");
1373 int flush = 0;
1375 progress =
1376 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1377 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1378 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1380 if (rc)
1381 return rc;
1383 again:
1386 if (sent + to_send > total)
1387 to_send = total - sent;
1389 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1390 flush ? 0 : to_send);
1391 if (!rc)
1393 sent += flush ? 0 : to_send;
1395 if ((progress && !(sent % progress) && sent != total) ||
1396 (sent == total && flush))
1397 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1399 if (!flush && !rc && sent == total)
1401 flush = 1;
1402 goto again;
1406 while (!rc && sent < total);
1408 return rc;
1411 static gpg_error_t
1412 do_get (assuan_context_t ctx, char *line)
1414 struct client_s *client = assuan_get_pointer (ctx);
1415 gpg_error_t rc;
1416 char **req;
1417 xmlNodePtr n;
1419 req = str_split (line, "\t", 0);
1421 if (!req || !*req)
1423 strv_free (req);
1424 return GPG_ERR_SYNTAX;
1427 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1428 if (!n)
1430 strv_free (req);
1431 return rc;
1434 if (req[1])
1436 find_elements (client, client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1437 0, 0, NULL, 0);
1439 strv_free (req);
1440 if (rc)
1441 return rc;
1443 if (!n || !n->children)
1444 return GPG_ERR_NO_DATA;
1446 n = find_text_node (n->children);
1447 if (!n || !n->content || !*n->content)
1448 return GPG_ERR_NO_DATA;
1450 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1451 return rc;
1454 static gpg_error_t
1455 get_command (assuan_context_t ctx, char *line)
1457 struct client_s *client = assuan_get_pointer (ctx);
1458 gpg_error_t rc;
1459 struct argv_s *args[] = {
1460 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1461 NULL
1464 rc = parse_options (&line, args, client);
1465 if (rc)
1466 return send_error (ctx, rc);
1468 if (client->opts & OPT_INQUIRE)
1470 unsigned char *result;
1471 size_t len;
1473 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1474 if (rc)
1475 return send_error (ctx, rc);
1477 line = (char *) result;
1480 rc = do_get (ctx, line);
1482 if (client->opts & OPT_INQUIRE)
1483 xfree (line);
1485 return send_error (ctx, rc);
1488 static void list_command_cleanup1 (void *arg);
1489 static gpg_error_t
1490 realpath_command (assuan_context_t ctx, char *line)
1492 struct string_s *string = NULL;
1493 gpg_error_t rc;
1494 struct client_s *client = assuan_get_pointer (ctx);
1495 struct argv_s *args[] = {
1496 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1497 NULL
1500 rc = parse_options (&line, args, client);
1501 if (rc)
1502 return send_error (ctx, rc);
1504 if (client->opts & OPT_INQUIRE)
1506 unsigned char *result;
1507 size_t len;
1509 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1510 if (rc)
1511 return send_error (ctx, rc);
1513 line = (char *) result;
1516 rc = build_realpath (client, client->doc, line, &string);
1517 if (!rc)
1519 pthread_cleanup_push (list_command_cleanup1, string);
1520 rc = xfer_data (ctx, string->str, string->len);
1521 pthread_cleanup_pop (1);
1524 if (client->opts & OPT_INQUIRE)
1525 xfree (line);
1527 return send_error (ctx, rc);
1530 static void
1531 list_command_cleanup1 (void *arg)
1533 if (arg)
1534 string_free ((struct string_s *) arg, 1);
1537 static void
1538 list_command_cleanup2 (void *arg)
1540 struct element_list_s *elements = arg;
1542 if (elements)
1544 if (elements->list)
1546 int total = slist_length (elements->list);
1547 int i;
1549 for (i = 0; i < total; i++)
1551 char *tmp = slist_nth_data (elements->list, i);
1552 xfree (tmp);
1555 slist_free (elements->list);
1558 if (elements->prefix)
1559 xfree (elements->prefix);
1561 if (elements->req)
1562 strv_free (elements->req);
1564 xfree (elements);
1568 static gpg_error_t
1569 parse_list_opt_norecurse (void *data, void *value)
1571 struct client_s *client = data;
1573 client->opts &= ~(OPT_LIST_RECURSE);
1574 return 0;
1577 static gpg_error_t
1578 parse_list_opt_verbose (void *data, void *value)
1580 struct client_s *client = data;
1582 client->opts |= OPT_LIST_VERBOSE;
1583 return 0;
1586 static gpg_error_t
1587 parse_list_opt_target (void *data, void *value)
1589 struct client_s *client = data;
1591 client->opts |= OPT_LIST_WITH_TARGET;
1592 return 0;
1595 static gpg_error_t
1596 parse_list_opt_all (void *data, void *value)
1598 struct client_s *client = data;
1600 client->opts |= OPT_LIST_ALL;
1601 return 0;
1604 static gpg_error_t
1605 list_path_once (struct client_s *client, char *line,
1606 struct element_list_s *elements, struct string_s *result)
1608 gpg_error_t rc;
1610 elements->req = str_split (line, " ", 0);
1611 if (!elements->req)
1612 strv_printf (&elements->req, "%s", line);
1614 rc = create_path_list (client, client->doc, elements, *elements->req);
1615 if ((rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES) && elements->verbose)
1616 rc = 0;
1618 if (!rc)
1620 int total = slist_length (elements->list);
1622 if (!total)
1623 rc = GPG_ERR_NO_DATA;
1625 if (!rc)
1627 if (!rc)
1629 int i;
1631 for (i = 0; i < total; i++)
1633 char *tmp = slist_nth_data (elements->list, i);
1635 string_append_printf (result, "%s%s", tmp,
1636 i + 1 == total ? "" : "\n");
1640 else
1641 rc = GPG_ERR_NO_DATA;
1644 return rc;
1647 static int
1648 has_list_flag (char *path, char *flags)
1650 char *p = path;
1652 while (*p && *++p != ' ');
1654 if (!*p)
1655 return 0;
1657 for (; *p; p++)
1659 char *f;
1661 for (f = flags; *f && *f != ' '; f++)
1663 if (*p == *f)
1664 return 1;
1668 return 0;
1671 static gpg_error_t
1672 do_list (assuan_context_t ctx, char *line)
1674 struct client_s *client = assuan_get_pointer (ctx);
1675 gpg_error_t rc;
1676 struct element_list_s *elements = NULL;
1678 elements = xcalloc (1, sizeof (struct element_list_s));
1679 if (!elements)
1680 return GPG_ERR_ENOMEM;
1682 elements->recurse = client->opts & OPT_LIST_RECURSE;
1683 elements->verbose =
1684 (client->opts & OPT_LIST_VERBOSE) | (client->opts & OPT_LIST_WITH_TARGET);
1685 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1687 if (!line || !*line)
1689 struct string_s *str = NULL;
1691 pthread_cleanup_push (list_command_cleanup2, elements);
1692 rc = list_root_elements (client, client->doc, &str, elements->verbose,
1693 elements->with_target);
1694 pthread_cleanup_pop (1);
1695 pthread_cleanup_push (list_command_cleanup1, str);
1697 if (!rc)
1699 if (client->opts & OPT_LIST_ALL)
1701 char **roots = str_split (str->str, "\n", 0);
1702 char **p;
1704 pthread_cleanup_push (req_cleanup, roots);
1705 string_truncate (str, 0);
1707 for (p = roots; *p; p++)
1709 if (strchr (*p, ' '))
1711 if (has_list_flag (*p, "EOP"))
1713 string_append_printf (str, "%s%s", *p,
1714 *(p + 1) ? "\n" : "");
1715 continue;
1719 elements = xcalloc (1, sizeof (struct element_list_s));
1720 if (!elements)
1722 rc = GPG_ERR_ENOMEM;
1723 break;
1726 elements->recurse = client->opts & OPT_LIST_RECURSE;
1727 elements->verbose =
1728 (client->opts & OPT_LIST_VERBOSE) | (client->opts &
1729 OPT_LIST_WITH_TARGET);
1730 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1731 pthread_cleanup_push (list_command_cleanup2, elements);
1732 rc = list_path_once (client, *p, elements, str);
1733 pthread_cleanup_pop (1);
1734 if (rc)
1735 break;
1737 if (*(p + 1))
1738 string_append (str, "\n");
1741 pthread_cleanup_pop (1);
1744 if (!rc)
1745 rc = xfer_data (ctx, str->str, str->len);
1748 pthread_cleanup_pop (1);
1749 return rc;
1752 pthread_cleanup_push (list_command_cleanup2, elements);
1753 struct string_s *str = string_new (NULL);
1754 pthread_cleanup_push (list_command_cleanup1, str);
1755 rc = list_path_once (client, line, elements, str);
1756 if (!rc)
1757 rc = xfer_data (ctx, str->str, str->len);
1759 pthread_cleanup_pop (1);
1760 pthread_cleanup_pop (1);
1761 return rc;
1764 static gpg_error_t
1765 list_command (assuan_context_t ctx, char *line)
1767 struct client_s *client = assuan_get_pointer (ctx);
1768 gpg_error_t rc;
1769 struct argv_s *args[] = {
1770 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG,
1771 parse_list_opt_norecurse},
1772 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_list_opt_verbose},
1773 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1774 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG,
1775 parse_list_opt_target},
1776 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_all},
1777 NULL
1780 if (disable_list_and_dump == 1)
1781 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1783 client->opts |= OPT_LIST_RECURSE;
1784 rc = parse_options (&line, args, client);
1785 if (rc)
1786 return send_error (ctx, rc);
1788 if (client->opts & OPT_LIST_ALL)
1789 client->opts |= OPT_LIST_RECURSE | OPT_LIST_VERBOSE;
1791 if (client->opts & OPT_INQUIRE)
1793 unsigned char *result;
1794 size_t len;
1796 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1797 if (rc)
1798 return send_error (ctx, rc);
1800 line = (char *) result;
1803 rc = do_list (ctx, line);
1805 if (client->opts & OPT_INQUIRE)
1806 xfree (line);
1808 return send_error (ctx, rc);
1812 * req[0] - element path
1814 static gpg_error_t
1815 attribute_list (assuan_context_t ctx, char **req)
1817 struct client_s *client = assuan_get_pointer (ctx);
1818 char **attrlist = NULL;
1819 int i = 0;
1820 char **path = NULL;
1821 xmlAttrPtr a;
1822 xmlNodePtr n, an;
1823 char *line;
1824 gpg_error_t rc;
1826 if (!req || !req[0])
1827 return GPG_ERR_SYNTAX;
1829 if ((path = str_split (req[0], "\t", 0)) == NULL)
1832 * The first argument may be only a root element.
1834 if ((path = str_split (req[0], " ", 0)) == NULL)
1835 return GPG_ERR_SYNTAX;
1838 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1840 if (!n)
1842 strv_free (path);
1843 return rc;
1846 if (path[1])
1848 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1849 NULL, NULL, NULL, 0, 0, NULL, 0);
1851 if (!n)
1853 strv_free (path);
1854 return rc;
1858 strv_free (path);
1860 for (a = n->properties; a; a = a->next)
1862 char **pa;
1864 if ((pa = xrealloc (attrlist, (i + 2) * sizeof (char *))) == NULL)
1866 if (attrlist)
1867 strv_free (attrlist);
1869 log_write ("%s(%i): %s", __FILE__, __LINE__,
1870 pwmd_strerror (GPG_ERR_ENOMEM));
1871 return GPG_ERR_ENOMEM;
1874 attrlist = pa;
1875 an = a->children;
1876 attrlist[i] = str_asprintf ("%s %s", (char *) a->name,
1878 && an->content ? (char *) an->content : "");
1880 if (!attrlist[i])
1882 strv_free (attrlist);
1883 log_write ("%s(%i): %s", __FILE__, __LINE__,
1884 pwmd_strerror (GPG_ERR_ENOMEM));
1885 return GPG_ERR_ENOMEM;
1888 attrlist[++i] = NULL;
1891 if (!attrlist)
1892 return GPG_ERR_NO_DATA;
1894 line = strv_join ("\n", attrlist);
1896 if (!line)
1898 log_write ("%s(%i): %s", __FILE__, __LINE__,
1899 pwmd_strerror (GPG_ERR_ENOMEM));
1900 strv_free (attrlist);
1901 return GPG_ERR_ENOMEM;
1904 pthread_cleanup_push (xfree, line);
1905 pthread_cleanup_push (req_cleanup, attrlist);
1906 rc = xfer_data (ctx, line, strlen (line));
1907 pthread_cleanup_pop (1);
1908 pthread_cleanup_pop (1);
1909 return rc;
1913 * req[0] - attribute
1914 * req[1] - element path
1916 static gpg_error_t
1917 attribute_delete (struct client_s *client, char **req)
1919 xmlNodePtr n;
1920 char **path = NULL;
1921 gpg_error_t rc;
1923 if (!req || !req[0] || !req[1])
1924 return GPG_ERR_SYNTAX;
1926 if (!strcmp (req[0], "_name"))
1927 return GPG_ERR_INV_ATTR;
1929 if ((path = str_split (req[1], "\t", 0)) == NULL)
1932 * The first argument may be only a root element.
1934 if ((path = str_split (req[1], " ", 0)) == NULL)
1935 return GPG_ERR_SYNTAX;
1938 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1939 if (!n)
1940 goto fail;
1942 if (path[1])
1944 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1945 NULL, NULL, NULL, 0, 0, NULL, 0);
1946 if (!n)
1947 goto fail;
1950 if (!strcmp (req[0], (char *) "_acl"))
1952 rc = is_element_owner (client, n);
1953 if (rc)
1954 goto fail;
1957 rc = delete_attribute (client, n, (xmlChar *) req[0]);
1959 fail:
1960 strv_free (path);
1961 return rc;
1964 static xmlNodePtr
1965 create_element_path (struct client_s *client,
1966 char ***elements, gpg_error_t * rc, xmlNodePtr parent)
1968 char **req = *elements;
1969 char **req_orig = strv_dup (req);
1970 xmlNodePtr n = NULL;
1972 *rc = 0;
1974 if (!req_orig)
1976 *rc = GPG_ERR_ENOMEM;
1977 log_write ("%s(%i): %s", __FILE__, __LINE__,
1978 pwmd_strerror (GPG_ERR_ENOMEM));
1979 goto fail;
1982 again:
1983 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
1984 if (!n)
1986 if (*rc != GPG_ERR_ELEMENT_NOT_FOUND)
1987 goto fail;
1989 *rc = new_root_element (client, client->doc, req[0]);
1990 if (*rc)
1991 goto fail;
1993 goto again;
1995 else if (n == parent)
1997 *rc = GPG_ERR_CONFLICT;
1998 goto fail;
2001 if (req[1])
2003 if (!n->children)
2004 n = create_target_elements_cb (client, 1, n, req + 1, rc, NULL);
2005 else
2006 n = find_elements (client, client->doc, n->children, req + 1, rc,
2007 NULL, NULL, create_target_elements_cb, 0, 0,
2008 parent, 0);
2010 if (!n)
2011 goto fail;
2014 * Reset the position of the element tree now that the elements
2015 * have been created.
2017 strv_free (req);
2018 req = req_orig;
2019 req_orig = NULL;
2020 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
2021 if (!n)
2022 goto fail;
2024 n = find_elements (client, client->doc, n->children, req + 1, rc,
2025 NULL, NULL, NULL, 0, 0, NULL, 0);
2026 if (!n)
2027 goto fail;
2030 fail:
2031 if (req_orig)
2032 strv_free (req_orig);
2034 *elements = req;
2035 return n;
2039 * Creates a "target" attribute. When other commands encounter an element with
2040 * this attribute, the element path is modified to the target value. If the
2041 * source element path doesn't exist when using 'ATTR SET target', it is
2042 * created, but the destination element path must exist.
2044 * req[0] - source element path
2045 * req[1] - destination element path
2047 static gpg_error_t
2048 target_attribute (struct client_s *client, char **req)
2050 char **src, **dst, *line = NULL, **odst = NULL;
2051 gpg_error_t rc;
2052 xmlNodePtr n;
2054 if (!req || !req[0] || !req[1])
2055 return GPG_ERR_SYNTAX;
2057 if ((src = str_split (req[0], "\t", 0)) == NULL)
2060 * The first argument may be only a root element.
2062 if ((src = str_split (req[0], " ", 0)) == NULL)
2063 return GPG_ERR_SYNTAX;
2066 if (!valid_element_path (src, 0))
2067 return GPG_ERR_INV_VALUE;
2069 if ((dst = str_split (req[1], "\t", 0)) == NULL)
2072 * The first argument may be only a root element.
2074 if ((dst = str_split (req[1], " ", 0)) == NULL)
2076 rc = GPG_ERR_SYNTAX;
2077 goto fail;
2081 odst = strv_dup (dst);
2082 if (!odst)
2084 rc = GPG_ERR_ENOMEM;
2085 goto fail;
2088 n = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
2090 * Make sure the destination element path exists.
2092 if (!n)
2093 goto fail;
2095 if (dst[1])
2097 n = find_elements (client, client->doc, n->children, dst + 1, &rc,
2098 NULL, NULL, NULL, 0, 0, NULL, 0);
2099 if (!n)
2100 goto fail;
2103 rc = validate_target_attribute (client, client->doc, req[0], n);
2104 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2105 goto fail;
2107 n = create_element_path (client, &src, &rc, NULL);
2108 if (rc)
2109 goto fail;
2111 line = strv_join ("\t", odst);
2112 if (!line)
2114 rc = GPG_ERR_ENOMEM;
2115 goto fail;
2118 rc = add_attribute (client, n, "target", line);
2120 fail:
2121 xfree (line);
2122 strv_free (src);
2123 strv_free (dst);
2124 strv_free (odst);
2125 return rc;
2129 * req[0] - attribute
2130 * req[1] - element path
2132 static gpg_error_t
2133 attribute_get (assuan_context_t ctx, char **req)
2135 struct client_s *client = assuan_get_pointer (ctx);
2136 xmlNodePtr n;
2137 xmlChar *a;
2138 char **path = NULL;
2139 gpg_error_t rc;
2141 if (!req || !req[0] || !req[1])
2142 return GPG_ERR_SYNTAX;
2144 if (strchr (req[1], '\t'))
2146 if ((path = str_split (req[1], "\t", 0)) == NULL)
2147 return GPG_ERR_SYNTAX;
2149 else
2151 if ((path = str_split (req[1], " ", 0)) == NULL)
2152 return GPG_ERR_SYNTAX;
2155 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2157 if (!n)
2158 goto fail;
2160 if (path[1])
2162 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2163 NULL, NULL, NULL, 0, 0, NULL, 0);
2165 if (!n)
2166 goto fail;
2169 strv_free (path);
2171 if ((a = xmlGetProp (n, (xmlChar *) req[0])) == NULL)
2172 return GPG_ERR_NOT_FOUND;
2174 pthread_cleanup_push (xmlFree, a);
2176 if (*a)
2177 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
2178 else
2179 rc = GPG_ERR_NO_DATA;
2181 pthread_cleanup_pop (1);
2182 return rc;
2184 fail:
2185 strv_free (path);
2186 return rc;
2190 * req[0] - attribute
2191 * req[1] - element path
2192 * req[2] - value
2194 static gpg_error_t
2195 attribute_set (struct client_s *client, char **req)
2197 char **path = NULL;
2198 gpg_error_t rc;
2199 xmlNodePtr n;
2201 if (!req || !req[0] || !req[1])
2202 return GPG_ERR_SYNTAX;
2205 * Reserved attribute names.
2207 if (!strcmp (req[0], "_name"))
2208 return GPG_ERR_INV_ATTR;
2209 else if (!strcmp (req[0], "target"))
2210 return target_attribute (client, req + 1);
2211 else if (!valid_xml_attribute (req[0]))
2212 return GPG_ERR_INV_VALUE;
2214 if ((path = str_split (req[1], "\t", 0)) == NULL)
2217 * The first argument may be only a root element.
2219 if ((path = str_split (req[1], " ", 0)) == NULL)
2220 return GPG_ERR_SYNTAX;
2223 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2225 if (!n)
2226 goto fail;
2228 if (path[1])
2230 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2231 NULL, NULL, NULL, 0, 0, NULL, 0);
2233 if (!n)
2234 goto fail;
2237 if (!strcmp (req[0], (char *) "_acl"))
2239 rc = is_element_owner (client, n);
2240 if (rc)
2241 goto fail;
2244 rc = add_attribute (client, n, req[0], req[2]);
2246 fail:
2247 strv_free (path);
2248 return rc;
2252 * req[0] - command
2253 * req[1] - attribute name or element path if command is LIST
2254 * req[2] - element path
2255 * req[2] - element path or value
2258 static gpg_error_t
2259 do_attr (assuan_context_t ctx, char *line)
2261 struct client_s *client = assuan_get_pointer (ctx);
2262 gpg_error_t rc = 0;
2263 char **req;
2265 req = str_split (line, " ", 4);
2266 if (!req || !req[0] || !req[1])
2268 strv_free (req);
2269 return GPG_ERR_SYNTAX;
2272 pthread_cleanup_push (req_cleanup, req);
2274 if (strcasecmp (req[0], "SET") == 0)
2275 rc = attribute_set (client, req + 1);
2276 else if (strcasecmp (req[0], "GET") == 0)
2277 rc = attribute_get (ctx, req + 1);
2278 else if (strcasecmp (req[0], "DELETE") == 0)
2279 rc = attribute_delete (client, req + 1);
2280 else if (strcasecmp (req[0], "LIST") == 0)
2281 rc = attribute_list (ctx, req + 1);
2282 else
2283 rc = GPG_ERR_SYNTAX;
2285 pthread_cleanup_pop (1);
2286 return rc;
2289 static gpg_error_t
2290 attr_command (assuan_context_t ctx, char *line)
2292 struct client_s *client = assuan_get_pointer (ctx);
2293 gpg_error_t rc;
2294 struct argv_s *args[] = {
2295 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2296 NULL
2299 rc = parse_options (&line, args, client);
2300 if (rc)
2301 return send_error (ctx, rc);
2303 if (client->opts & OPT_INQUIRE)
2305 unsigned char *result;
2306 size_t len;
2308 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2309 if (rc)
2310 return send_error (ctx, rc);
2312 line = (char *) result;
2315 rc = do_attr (ctx, line);
2317 if (client->opts & OPT_INQUIRE)
2318 xfree (line);
2320 return send_error (ctx, rc);
2323 static gpg_error_t
2324 parse_iscached_opt_lock (void *data, void *value)
2326 struct client_s *client = data;
2328 (void) value;
2329 client->opts |= OPT_LOCK;
2330 return 0;
2333 static gpg_error_t
2334 iscached_command (assuan_context_t ctx, char *line)
2336 struct client_s *client = assuan_get_pointer (ctx);
2337 gpg_error_t rc;
2338 struct argv_s *args[] = {
2339 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2340 NULL
2343 if (!line || !*line)
2344 return send_error (ctx, GPG_ERR_SYNTAX);
2346 rc = parse_options (&line, args, client);
2347 if (rc)
2348 return send_error (ctx, rc);
2349 else if (!valid_filename (line))
2350 return send_error (ctx, GPG_ERR_INV_VALUE);
2352 rc = cache_iscached (line, NULL);
2353 if (client->opts & OPT_LOCK
2354 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2356 unsigned char md5file[16];
2357 gpg_error_t trc = rc;
2359 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2360 if (memcmp (md5file, client->md5file, 16))
2361 cleanup_client (client);
2363 memcpy (client->md5file, md5file, 16);
2364 rc = do_lock (client, 1);
2365 if (!rc)
2366 rc = trc;
2369 return send_error (ctx, rc);
2372 static gpg_error_t
2373 clearcache_command (assuan_context_t ctx, char *line)
2375 gpg_error_t rc = 0, all_rc = 0;
2376 unsigned char md5file[16];
2377 int i;
2378 int t;
2379 int all = 0;
2380 struct client_thread_s *once = NULL;
2382 cache_lock ();
2383 MUTEX_LOCK (&cn_mutex);
2384 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
2386 if (!line || !*line)
2387 all = 1;
2389 t = slist_length (cn_thread_list);
2391 for (i = 0; i < t; i++)
2393 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2394 assuan_peercred_t peer;
2396 if (!thd->cl)
2397 continue;
2399 /* Lock each connected clients' file mutex to prevent any other client
2400 * from accessing the cache entry (the file mutex is locked upon
2401 * command startup). The cache for the entry is not cleared if the
2402 * file mutex is locked by another client to prevent this function
2403 * from blocking.
2405 if (all)
2407 if (thd->cl->filename)
2409 rc = do_validate_peer (ctx, thd->cl->filename, &peer);
2410 all_rc = !all_rc ? rc : all_rc;
2411 if (rc)
2413 rc = 0;
2414 continue;
2418 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
2419 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2421 if (pthread_equal (pthread_self (), thd->tid))
2422 rc = 0;
2423 else
2425 if (!thd->cl->filename ||
2426 cache_iscached (thd->cl->filename,
2427 NULL) == GPG_ERR_NO_DATA)
2429 rc = 0;
2430 continue;
2433 cache_defer_clear (thd->cl->md5file);
2436 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2438 rc = 0;
2439 continue;
2442 if (!rc)
2444 rc = cache_clear (thd->cl->md5file);
2445 cache_unlock_mutex (thd->cl->md5file, 0);
2448 if (rc)
2449 all_rc = rc;
2451 rc = 0;
2453 /* A single data filename was specified. Lock only this data file
2454 * mutex and free the cache entry. */
2455 else
2457 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2458 rc = do_validate_peer (ctx, line, &peer);
2460 if (!rc && !memcmp (thd->cl->md5file, md5file, sizeof (md5file)))
2462 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0,
2463 -1);
2464 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2466 if (pthread_equal (pthread_self (), thd->tid))
2467 rc = 0;
2470 if (!rc)
2472 once = thd;
2473 rc = cache_clear (thd->cl->md5file);
2474 cache_unlock_mutex (thd->cl->md5file, 0);
2476 else
2478 cache_defer_clear (thd->cl->md5file);
2481 break;
2486 /* Only connected clients' cache entries have been cleared. Now clear any
2487 * remaining cache entries without clients but only if there wasn't an
2488 * error from above since this would defeat the locking check of the
2489 * remaining entries. */
2490 if (!all_rc && all)
2492 cache_clear (NULL);
2495 /* No clients are using the specified file. */
2496 else if (!all_rc && !rc && !once)
2498 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2499 rc = cache_clear (md5file);
2502 /* Release the connection mutex. */
2503 pthread_cleanup_pop (1);
2504 cache_unlock ();
2506 if (!rc)
2507 send_status_all (STATUS_CACHE, NULL);
2509 /* One or more files were locked while clearing all cache entries. */
2510 if (all_rc)
2511 rc = all_rc;
2513 return send_error (ctx, rc);
2516 static gpg_error_t
2517 cachetimeout_command (assuan_context_t ctx, char *line)
2519 int timeout;
2520 char **req = str_split (line, " ", 0);
2521 char *p;
2522 gpg_error_t rc = 0;
2523 assuan_peercred_t peer;
2525 if (!req || !*req || !req[1])
2527 strv_free (req);
2528 return send_error (ctx, GPG_ERR_SYNTAX);
2531 errno = 0;
2532 timeout = (int) strtol (req[1], &p, 10);
2533 if (errno != 0 || *p || timeout < -1)
2535 strv_free (req);
2536 return send_error (ctx, GPG_ERR_SYNTAX);
2539 rc = do_validate_peer (ctx, req[0], &peer);
2540 if (!rc)
2542 unsigned char md5file[16];
2544 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, req[0], strlen (req[0]));
2545 rc = cache_set_timeout (md5file, timeout);
2546 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2548 rc = 0;
2549 MUTEX_LOCK (&rcfile_mutex);
2550 config_set_int_param (&global_config, req[0], "cache_timeout",
2551 req[1]);
2552 MUTEX_UNLOCK (&rcfile_mutex);
2556 strv_free (req);
2557 return send_error (ctx, rc);
2560 static gpg_error_t
2561 dump_command (assuan_context_t ctx, char *line)
2563 xmlChar *xml;
2564 int len;
2565 struct client_s *client = assuan_get_pointer (ctx);
2566 gpg_error_t rc;
2568 if (disable_list_and_dump == 1)
2569 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2571 rc = peer_is_invoker(client);
2572 if (rc)
2573 return send_error (ctx, rc);
2575 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2577 if (!xml)
2579 log_write ("%s(%i): %s", __FILE__, __LINE__,
2580 pwmd_strerror (GPG_ERR_ENOMEM));
2581 return send_error (ctx, GPG_ERR_ENOMEM);
2584 pthread_cleanup_push (xmlFree, xml);
2585 rc = xfer_data (ctx, (char *) xml, len);
2586 pthread_cleanup_pop (1);
2587 return send_error (ctx, rc);
2590 static gpg_error_t
2591 getconfig_command (assuan_context_t ctx, char *line)
2593 struct client_s *client = assuan_get_pointer (ctx);
2594 gpg_error_t rc = 0;
2595 char filename[255] = { 0 }, param[747] = { 0 };
2596 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2598 if (!line || !*line)
2599 return send_error (ctx, GPG_ERR_SYNTAX);
2601 if (strchr (line, ' '))
2603 sscanf (line, " %254[^ ] %746c", filename, param);
2604 paramp = param;
2605 fp = filename;
2608 if (fp && !valid_filename (fp))
2609 return send_error (ctx, GPG_ERR_INV_VALUE);
2611 paramp = str_down (paramp);
2612 if (!strcmp (paramp, "cipher") && fp)
2614 struct crypto_s *crypto = NULL;
2616 rc = init_client_crypto (&crypto);
2617 if (!rc)
2619 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2620 if (!rc)
2622 const char *t =
2623 gcry_cipher_algo_name (cipher_to_gcrypt (crypto->hdr.flags));
2624 if (t)
2626 tmp = str_dup (t);
2627 if (tmp)
2628 str_down (tmp);
2633 UPDATE_AGENT_CTX (client, crypto);
2634 cleanup_crypto (&crypto);
2635 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2636 return send_error (ctx, rc);
2638 if (!rc && tmp)
2639 goto done;
2641 else if (!strcmp (paramp, "cipher_iterations") && fp)
2643 struct crypto_s *crypto = NULL;
2645 rc = init_client_crypto (&crypto);
2646 if (!rc)
2648 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2649 if (!rc)
2651 tmp = str_asprintf ("%llu",
2652 (unsigned long long) crypto->hdr.
2653 iterations);
2654 if (!tmp)
2655 rc = GPG_ERR_ENOMEM;
2659 UPDATE_AGENT_CTX (client, crypto);
2660 cleanup_crypto (&crypto);
2661 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2662 return send_error (ctx, rc);
2664 if (!rc && tmp)
2665 goto done;
2667 else if (!strcmp (paramp, "passphrase"))
2668 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2670 p = config_get_value (fp ? fp : "global", paramp);
2671 if (!p)
2672 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2674 tmp = expand_homedir (p);
2675 xfree (p);
2676 if (!tmp)
2678 log_write ("%s(%i): %s", __FILE__, __LINE__,
2679 pwmd_strerror (GPG_ERR_ENOMEM));
2680 return send_error (ctx, GPG_ERR_ENOMEM);
2683 done:
2684 p = tmp;
2685 pthread_cleanup_push (xfree, p);
2686 rc = xfer_data (ctx, p, strlen (p));
2687 pthread_cleanup_pop (1);
2688 return send_error (ctx, rc);
2691 struct xpath_s
2693 xmlXPathContextPtr xp;
2694 xmlXPathObjectPtr result;
2695 xmlBufferPtr buf;
2696 char **req;
2699 static void
2700 xpath_command_cleanup (void *arg)
2702 struct xpath_s *xpath = arg;
2704 if (!xpath)
2705 return;
2707 req_cleanup (xpath->req);
2709 if (xpath->buf)
2710 xmlBufferFree (xpath->buf);
2712 if (xpath->result)
2713 xmlXPathFreeObject (xpath->result);
2715 if (xpath->xp)
2716 xmlXPathFreeContext (xpath->xp);
2719 static gpg_error_t
2720 do_xpath (assuan_context_t ctx, char *line)
2722 gpg_error_t rc;
2723 struct client_s *client = assuan_get_pointer (ctx);
2724 struct xpath_s _x = { 0 };
2725 struct xpath_s *xpath = &_x;
2727 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2729 if (strv_printf (&xpath->req, "%s", line) == 0)
2730 return GPG_ERR_ENOMEM;
2733 xpath->xp = xmlXPathNewContext (client->doc);
2734 if (!xpath->xp)
2736 rc = GPG_ERR_BAD_DATA;
2737 goto fail;
2740 xpath->result =
2741 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2742 if (!xpath->result)
2744 rc = GPG_ERR_BAD_DATA;
2745 goto fail;
2748 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2750 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2751 goto fail;
2754 rc = recurse_xpath_nodeset (client, client->doc, xpath->result->nodesetval,
2755 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2756 NULL);
2757 if (rc)
2758 goto fail;
2759 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2761 rc = GPG_ERR_NO_DATA;
2762 goto fail;
2764 else if (xpath->req[1])
2766 rc = 0;
2767 goto fail;
2770 pthread_cleanup_push (xpath_command_cleanup, &xpath);
2771 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2772 xmlBufferLength (xpath->buf));
2773 pthread_cleanup_pop (0);
2774 fail:
2775 xpath_command_cleanup (xpath);
2776 return rc;
2779 static gpg_error_t
2780 xpath_command (assuan_context_t ctx, char *line)
2782 struct client_s *client = assuan_get_pointer (ctx);
2783 gpg_error_t rc;
2784 struct argv_s *args[] = {
2785 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2786 NULL
2789 if (disable_list_and_dump == 1)
2790 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2792 rc = peer_is_invoker(client);
2793 if (rc)
2794 return send_error (ctx, rc);
2796 rc = parse_options (&line, args, client);
2797 if (rc)
2798 return send_error (ctx, rc);
2800 if (client->opts & OPT_INQUIRE)
2802 unsigned char *result;
2803 size_t len;
2805 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2806 if (rc)
2807 return send_error (ctx, rc);
2809 line = (char *) result;
2812 if (!line || !*line)
2813 rc = GPG_ERR_SYNTAX;
2815 if (!rc)
2816 rc = do_xpath (ctx, line);
2818 if (client->opts & OPT_INQUIRE)
2819 xfree (line);
2821 return send_error (ctx, rc);
2824 static gpg_error_t
2825 do_xpathattr (assuan_context_t ctx, char *line)
2827 struct client_s *client = assuan_get_pointer (ctx);
2828 gpg_error_t rc;
2829 char **req = NULL;
2830 int cmd = 0; //SET
2831 struct xpath_s _x = { 0 };
2832 struct xpath_s *xpath = &_x;
2834 if ((req = str_split (line, " ", 3)) == NULL)
2835 return GPG_ERR_ENOMEM;
2837 if (!req[0])
2839 rc = GPG_ERR_SYNTAX;
2840 goto fail;
2843 if (!strcasecmp (req[0], "SET"))
2844 cmd = 0;
2845 else if (!strcasecmp (req[0], "DELETE"))
2846 cmd = 1;
2847 else
2849 rc = GPG_ERR_SYNTAX;
2850 goto fail;
2853 if (!req[1] || !req[2])
2855 rc = GPG_ERR_SYNTAX;
2856 goto fail;
2859 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2861 rc = GPG_ERR_ENOMEM;
2862 goto fail;
2865 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2867 rc = GPG_ERR_SYNTAX;
2868 goto fail;
2871 xpath->xp = xmlXPathNewContext (client->doc);
2872 if (!xpath->xp)
2874 rc = GPG_ERR_BAD_DATA;
2875 goto fail;
2878 xpath->result = xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2879 if (!xpath->result)
2881 rc = GPG_ERR_BAD_DATA;
2882 goto fail;
2885 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2887 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2888 goto fail;
2891 rc = recurse_xpath_nodeset (client, client->doc, xpath->result->nodesetval,
2892 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2893 (xmlChar *) req[1]);
2895 fail:
2896 xpath_command_cleanup (xpath);
2897 strv_free (req);
2898 return rc;
2901 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2902 static gpg_error_t
2903 xpathattr_command (assuan_context_t ctx, char *line)
2905 struct client_s *client = assuan_get_pointer (ctx);
2906 gpg_error_t rc;
2907 struct argv_s *args[] = {
2908 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2909 NULL
2912 if (disable_list_and_dump == 1)
2913 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2915 rc = peer_is_invoker(client);
2916 if (rc)
2917 return send_error (ctx, rc);
2919 rc = parse_options (&line, args, client);
2920 if (rc)
2921 return send_error (ctx, rc);
2923 if (client->opts & OPT_INQUIRE)
2925 unsigned char *result;
2926 size_t len;
2928 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2929 if (rc)
2930 return send_error (ctx, rc);
2932 line = (char *) result;
2935 if (!line || !*line)
2936 rc = GPG_ERR_SYNTAX;
2938 if (!rc)
2939 rc = do_xpathattr (ctx, line);
2941 if (client->opts & OPT_INQUIRE)
2942 xfree (line);
2944 return send_error (ctx, rc);
2947 static gpg_error_t
2948 do_import (struct client_s *client, const char *root_element,
2949 unsigned char *content)
2951 char **dst_path = NULL;
2952 xmlDocPtr doc = NULL;
2953 xmlNodePtr n, root, copy;
2954 gpg_error_t rc;
2956 if (!content || !*content)
2958 xfree (content);
2959 return GPG_ERR_SYNTAX;
2962 if (root_element)
2963 dst_path = str_split (root_element, "\t", 0);
2965 if (dst_path && !valid_element_path (dst_path, 0))
2967 if (dst_path)
2968 strv_free (dst_path);
2970 return GPG_ERR_INV_VALUE;
2973 struct string_s *str = string_new_content ((char *)content);
2974 str = string_prepend (str, "<pwmd>");
2975 str = string_append (str, "</pwmd>");
2976 doc = xmlReadDoc ((xmlChar *) str->str, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2977 string_free (str, 1);
2978 if (!doc)
2980 rc = GPG_ERR_BAD_DATA;
2981 goto fail;
2984 root = xmlDocGetRootElement (doc);
2985 xmlNodePtr root_orig = root->children;
2986 root = root->children;
2987 rc = validate_import (client, root);
2988 if (rc)
2989 goto fail;
2993 again:
2994 if (dst_path)
2996 char **path = strv_dup (dst_path);
2997 if (!path)
2999 log_write ("%s(%i): %s", __FILE__, __LINE__,
3000 pwmd_strerror (GPG_ERR_ENOMEM));
3001 rc = GPG_ERR_ENOMEM;
3002 goto fail;
3005 xmlChar *a = xmlGetProp (root, (xmlChar *) "_name");
3006 if (!a)
3008 strv_free (path);
3009 rc = GPG_ERR_INV_VALUE;
3010 goto fail;
3013 if (strv_printf (&path, "%s", (char *) a) == 0)
3015 xmlFree (a);
3016 rc = GPG_ERR_ENOMEM;
3017 goto fail;
3020 xmlFree (a);
3021 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
3022 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3024 strv_free (path);
3025 goto fail;
3028 if (!rc)
3030 n = find_elements (client, client->doc, n->children, path + 1, &rc,
3031 NULL, NULL, NULL, 0, 0, NULL, 1);
3032 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3034 strv_free (path);
3035 goto fail;
3037 else if (!rc)
3039 xmlUnlinkNode (n);
3040 xmlFreeNode (n);
3041 strv_free (path);
3042 path = NULL;
3043 goto again;
3047 if (rc == GPG_ERR_ELEMENT_NOT_FOUND)
3049 n = create_element_path (client, &path, &rc, NULL);
3050 if (rc)
3051 goto fail;
3054 if (root->children)
3056 copy = xmlCopyNodeList (root->children);
3057 n = xmlAddChildList (n, copy);
3058 if (!n)
3059 rc = GPG_ERR_ENOMEM;
3062 strv_free (path);
3064 else
3066 char **path = NULL;
3068 /* Check if the content root element can create a DTD root element. */
3069 if (!xmlStrEqual ((xmlChar *) "element", root->name))
3071 rc = GPG_ERR_SYNTAX;
3072 goto fail;
3075 xmlChar *a;
3077 if ((a = xmlGetProp (root, (xmlChar *) "_name")) == NULL)
3079 rc = GPG_ERR_SYNTAX;
3080 goto fail;
3083 char *tmp = str_dup ((char *) a);
3084 xmlFree (a);
3085 int literal = is_literal_element (&tmp);
3087 if (!valid_xml_element ((xmlChar *) tmp) || literal)
3089 xfree (tmp);
3090 rc = GPG_ERR_INV_VALUE;
3091 goto fail;
3094 if (strv_printf (&path, "%s", tmp) == 0)
3096 xfree (tmp);
3097 rc = GPG_ERR_ENOMEM;
3098 goto fail;
3101 xfree (tmp);
3102 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 1);
3103 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3105 rc = GPG_ERR_BAD_DATA;
3106 goto fail;
3109 /* Overwriting the existing tree. */
3110 if (!rc)
3112 xmlUnlinkNode (n);
3113 xmlFreeNodeList (n);
3116 rc = 0;
3117 xmlSetProp (root, (xmlChar *) "_name", (xmlChar *) path[0]);
3118 n = xmlCopyNode (root, 1);
3119 n = xmlAddChildList (xmlDocGetRootElement (client->doc), n);
3120 strv_free (path);
3123 if (n && !rc)
3124 rc = update_element_mtime (client, n->parent);
3126 for (root = root_orig->next; root; root = root->next)
3128 if (root->type == XML_ELEMENT_NODE)
3129 break;
3132 root_orig = root;
3134 while (root);
3136 fail:
3137 if (doc)
3138 xmlFreeDoc (doc);
3140 if (dst_path)
3141 strv_free (dst_path);
3143 return rc;
3146 static gpg_error_t
3147 parse_import_opt_root (void *data, void *value)
3149 struct client_s *client = data;
3151 client->import_root = str_dup (value);
3152 return client->import_root ? 0 : GPG_ERR_ENOMEM;
3155 static gpg_error_t
3156 import_command (assuan_context_t ctx, char *line)
3158 gpg_error_t rc;
3159 struct client_s *client = assuan_get_pointer (ctx);
3160 unsigned char *result;
3161 size_t len;
3162 struct argv_s *args[] = {
3163 &(struct argv_s) {"root", OPTION_TYPE_ARG, parse_import_opt_root},
3164 NULL
3167 xfree (client->import_root);
3168 client->import_root = NULL;
3169 rc = parse_options (&line, args, client);
3170 if (rc)
3171 return send_error (ctx, rc);
3173 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3174 if (rc)
3176 xfree (client->import_root);
3177 client->import_root = NULL;
3178 return send_error (ctx, rc);
3181 rc = do_import (client, client->import_root, result);
3182 xfree (client->import_root);
3183 client->import_root = NULL;
3184 return send_error (ctx, rc);
3187 static gpg_error_t
3188 do_lock (struct client_s *client, int add)
3190 gpg_error_t rc = lock_file_mutex (client, add);
3192 if (!rc)
3193 client->flags |= FLAG_LOCK_CMD;
3195 return rc;
3198 static gpg_error_t
3199 lock_command (assuan_context_t ctx, char *line)
3201 struct client_s *client = assuan_get_pointer (ctx);
3202 gpg_error_t rc = do_lock (client, 0);
3204 return send_error (ctx, rc);
3207 static gpg_error_t
3208 unlock_command (assuan_context_t ctx, char *line)
3210 struct client_s *client = assuan_get_pointer (ctx);
3211 gpg_error_t rc;
3213 rc = unlock_file_mutex (client, 0);
3214 return send_error (ctx, rc);
3217 static gpg_error_t
3218 option_command (assuan_context_t ctx, char *line)
3220 struct client_s *client = assuan_get_pointer (ctx);
3221 gpg_error_t rc = 0;
3222 struct pinentry_option_s *pin_opts = &client->pinentry_opts;
3223 #ifdef WITH_AGENT
3224 struct agent_s *agent = client->crypto->agent;
3225 #endif
3226 char namebuf[255] = { 0 };
3227 char *name = namebuf;
3228 char *value = NULL, *p;
3230 p = strchr (line, '=');
3231 if (!p)
3232 strncpy (namebuf, line, sizeof(namebuf));
3233 else
3235 strncpy (namebuf, line, strlen (line)-strlen (p));
3236 value = p+1;
3239 log_write1 ("OPTION name='%s' value='%s'", name, value);
3241 if (strcasecmp (name, (char *) "log_level") == 0)
3243 long l = 0;
3245 if (value)
3247 l = strtol (value, NULL, 10);
3249 if (l < 0 || l > 2)
3250 return send_error (ctx, GPG_ERR_INV_VALUE);
3253 MUTEX_LOCK (&rcfile_mutex);
3254 config_set_int_param (&global_config, "global", "log_level", value);
3255 MUTEX_UNLOCK (&rcfile_mutex);
3256 goto done;
3258 else if (strcasecmp (name, (char *) "lock-timeout") == 0)
3260 long n = 0;
3261 char *p = NULL;
3263 if (value)
3265 n = strtol (value, &p, 10);
3266 if (p && *p)
3267 return send_error (ctx, GPG_ERR_INV_VALUE);
3270 client->lock_timeout = n;
3271 goto done;
3273 else if (strcasecmp (name, (char *) "NAME") == 0)
3275 char *tmp = pthread_getspecific (thread_name_key);
3277 if (tmp)
3278 xfree (tmp);
3280 if (!value || !*value)
3282 pthread_setspecific (thread_name_key, str_dup (""));
3283 goto done;
3286 pthread_setspecific (thread_name_key, str_dup (value));
3287 goto done;
3289 else if (strcasecmp (name, (char *) "lc-messages") == 0)
3291 xfree (pin_opts->lc_messages);
3292 pin_opts->lc_messages = NULL;
3293 if (value && *value)
3294 pin_opts->lc_messages = str_dup (value);
3295 #ifdef WITH_AGENT
3296 if (use_agent)
3297 rc = set_agent_option (client->crypto->agent, "lc-messages", value);
3298 #endif
3300 else if (strcasecmp (name, (char *) "lc-ctype") == 0)
3302 xfree (pin_opts->lc_ctype);
3303 pin_opts->lc_ctype = NULL;
3304 if (value && *value)
3305 pin_opts->lc_ctype = str_dup (value);
3306 #ifdef WITH_AGENT
3307 if (use_agent)
3308 rc = set_agent_option (client->crypto->agent, "lc-ctype", value);
3309 #endif
3311 else if (strcasecmp (name, (char *) "ttyname") == 0)
3313 xfree (pin_opts->ttyname);
3314 pin_opts->ttyname = NULL;
3315 if (value && *value)
3316 pin_opts->ttyname = str_dup (value);
3317 #ifdef WITH_AGENT
3318 if (use_agent)
3319 rc = set_agent_option (client->crypto->agent, "ttyname", value);
3320 #endif
3322 else if (strcasecmp (name, (char *) "ttytype") == 0)
3324 xfree (pin_opts->ttytype);
3325 pin_opts->ttytype = NULL;
3326 if (value && *value)
3327 pin_opts->ttytype = str_dup (value);
3328 #ifdef WITH_AGENT
3329 if (use_agent)
3330 rc = set_agent_option (client->crypto->agent, "ttytype", value);
3331 #endif
3333 else if (strcasecmp (name, (char *) "display") == 0)
3335 xfree (pin_opts->display);
3336 pin_opts->display = NULL;
3337 if (value && *value)
3338 pin_opts->display = str_dup (value);
3339 #ifdef WITH_AGENT
3340 if (use_agent)
3341 rc = set_agent_option (client->crypto->agent, "display", value);
3342 #endif
3344 else if (strcasecmp (name, (char *) "pinentry-desc") == 0)
3346 xfree (pin_opts->desc);
3347 pin_opts->desc = NULL;
3348 if (value && *value)
3349 pin_opts->desc = str_dup (value);
3351 else if (strcasecmp (name, (char *) "pinentry-title") == 0)
3353 xfree (pin_opts->title);
3354 pin_opts->title = NULL;
3355 if (value && *value)
3356 pin_opts->title = str_dup (value);
3358 else if (strcasecmp (name, (char *) "pinentry-prompt") == 0)
3360 xfree (pin_opts->prompt);
3361 pin_opts->prompt = NULL;
3362 if (value && *value)
3363 pin_opts->prompt = str_dup (value);
3366 else if (strcasecmp (name, "pinentry-timeout") == 0)
3368 char *p = NULL;
3369 int n;
3371 if (!value)
3372 goto done;
3374 n = (int) strtol (value, &p, 10);
3376 if (*p || n < 0)
3377 return send_error (ctx, GPG_ERR_INV_VALUE);
3379 pin_opts->timeout = n;
3380 MUTEX_LOCK (&rcfile_mutex);
3381 config_set_int_param (&global_config,
3382 client->filename ? client->filename : "global",
3383 "pinentry_timeout", value);
3384 MUTEX_UNLOCK (&rcfile_mutex);
3385 goto done;
3387 else if (strcasecmp (name, "disable-pinentry") == 0)
3389 char *p = NULL;
3390 int n = 1;
3392 if (value && *value)
3394 n = (int) strtol (value, &p, 10);
3395 if (*p || n < 0 || n > 1)
3396 return send_error (ctx, GPG_ERR_INV_VALUE);
3399 if (n)
3400 client->flags |= FLAG_NO_PINENTRY;
3401 else
3402 client->flags &= ~FLAG_NO_PINENTRY;
3404 #ifdef WITH_AGENT
3405 if (use_agent)
3407 if (client->flags & FLAG_NO_PINENTRY)
3408 rc = set_agent_option (client->crypto->agent, "pinentry-mode",
3409 "loopback");
3410 else
3411 rc = set_agent_option (client->crypto->agent, "pinentry-mode",
3412 "ask");
3414 if (rc)
3415 return send_error (ctx, rc);
3417 #endif
3419 else
3420 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
3422 done:
3423 #ifdef WITH_AGENT
3424 if (!rc && use_agent && agent)
3426 rc = pinentry_merge_options (&client->crypto->agent->pinentry_opts,
3427 pin_opts);
3429 #endif
3431 return send_error (ctx, rc);
3434 static gpg_error_t
3435 do_rename (assuan_context_t ctx, char *line)
3437 struct client_s *client = assuan_get_pointer (ctx);
3438 gpg_error_t rc;
3439 char **req, **src, *dst;
3440 xmlNodePtr n, ndst;
3442 req = str_split (line, " ", 0);
3444 if (!req || !req[0] || !req[1])
3446 strv_free (req);
3447 return GPG_ERR_SYNTAX;
3450 dst = req[1];
3451 is_literal_element (&dst);
3453 if (!valid_xml_element ((xmlChar *) dst))
3455 strv_free (req);
3456 return GPG_ERR_INV_VALUE;
3459 if (strchr (req[0], '\t'))
3460 src = str_split (req[0], "\t", 0);
3461 else
3462 src = str_split (req[0], " ", 0);
3464 if (!src || !*src)
3466 rc = GPG_ERR_SYNTAX;
3467 goto fail;
3470 n = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3471 if (src[1] && n)
3472 n = find_elements (client, client->doc, n->children, src + 1, &rc, NULL, NULL,
3473 NULL, 0, 0, NULL, 0);
3475 if (!n)
3476 goto fail;
3478 rc = is_element_owner (client, n);
3479 if (rc)
3480 goto fail;
3482 xmlChar *a = xmlGetProp (n, (xmlChar *) "_name");
3483 if (!a)
3485 rc = GPG_ERR_ENOMEM;
3486 goto fail;
3489 /* To prevent unwanted effects:
3491 * <root name="a"><b/></root>
3493 * RENAME a<TAB>b b
3495 if (xmlStrEqual (a, (xmlChar *) dst))
3497 xmlFree (a);
3498 rc = GPG_ERR_AMBIGUOUS_NAME;
3499 goto fail;
3502 xmlFree (a);
3503 char **tmp = NULL;
3504 if (src[1])
3506 char **p;
3508 for (p = src; *p; p++)
3510 if (!*(p + 1))
3511 break;
3512 strv_printf (&tmp, "%s", *p);
3516 strv_printf (&tmp, "!%s", dst);
3517 ndst = find_root_element (client, client->doc, &tmp, &rc, NULL, 0, 0);
3518 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3520 strv_free (tmp);
3521 goto fail;
3524 if (tmp[1] && ndst)
3525 ndst = find_elements (client, client->doc, ndst->children, tmp + 1, &rc, NULL,
3526 NULL, NULL, 0, 0, NULL, 0);
3528 strv_free (tmp);
3529 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3530 goto fail;
3532 rc = 0;
3534 /* Target may exist:
3536 * <root name="a"/>
3537 * <root name="b" target="a"/>
3539 * RENAME b a
3541 * Would need to do:
3542 * RENAME !b a
3544 if (ndst == n)
3546 rc = GPG_ERR_AMBIGUOUS_NAME;
3547 goto fail;
3550 if (ndst)
3552 rc = is_element_owner (client, ndst);
3553 if (rc)
3554 goto fail;
3556 unlink_node (client, ndst);
3557 xmlFreeNodeList (ndst);
3560 rc = add_attribute (client, n, "_name", dst);
3562 fail:
3563 strv_free (req);
3564 strv_free (src);
3565 return rc;
3568 static gpg_error_t
3569 rename_command (assuan_context_t ctx, char *line)
3571 struct client_s *client = assuan_get_pointer (ctx);
3572 gpg_error_t rc;
3573 struct argv_s *args[] = {
3574 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3575 NULL
3578 rc = parse_options (&line, args, client);
3579 if (rc)
3580 return send_error (ctx, rc);
3582 if (client->opts & OPT_INQUIRE)
3584 unsigned char *result;
3585 size_t len;
3587 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3588 if (rc)
3589 return send_error (ctx, rc);
3591 line = (char *) result;
3594 rc = do_rename (ctx, line);
3596 if (client->opts & OPT_INQUIRE)
3597 xfree (line);
3599 return send_error (ctx, rc);
3602 static gpg_error_t
3603 do_copy (assuan_context_t ctx, char *line)
3605 struct client_s *client = assuan_get_pointer (ctx);
3606 gpg_error_t rc;
3607 char **req, **src = NULL, **dst = NULL;
3608 xmlNodePtr nsrc, ndst, new = NULL;
3610 req = str_split (line, " ", 0);
3611 if (!req || !req[0] || !req[1])
3613 strv_free (req);
3614 return GPG_ERR_SYNTAX;
3617 if (strchr (req[0], '\t'))
3618 src = str_split (req[0], "\t", 0);
3619 else
3620 src = str_split (req[0], " ", 0);
3622 if (!src || !*src)
3624 rc = GPG_ERR_SYNTAX;
3625 goto fail;
3628 if (strchr (req[1], '\t'))
3629 dst = str_split (req[1], "\t", 0);
3630 else
3631 dst = str_split (req[1], " ", 0);
3633 if (!dst || !*dst)
3635 rc = GPG_ERR_SYNTAX;
3636 goto fail;
3639 if (!valid_element_path (dst, 0))
3641 rc = GPG_ERR_INV_VALUE;
3642 goto fail;
3645 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3646 if (nsrc && src[1])
3647 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc, NULL,
3648 NULL, NULL, 0, 0, NULL, 0);
3650 if (!nsrc)
3651 goto fail;
3653 new = xmlCopyNodeList (nsrc);
3654 if (!new)
3656 rc = GPG_ERR_ENOMEM;
3657 goto fail;
3660 int create = 0;
3661 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3662 if (ndst && dst[1])
3664 if (ndst->children)
3665 ndst = find_elements (client, client->doc, ndst->children, dst + 1, &rc, NULL,
3666 NULL, create_target_elements_cb, 0, 0, NULL, 0);
3667 else
3668 create = 1;
3670 else
3671 create = 1;
3673 if (!ndst && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3674 goto fail;
3675 else if (!ndst || create)
3677 ndst = create_element_path (client, &dst, &rc, NULL);
3678 if (!ndst)
3679 goto fail;
3682 rc = is_element_owner (client, ndst);
3683 if (rc)
3684 goto fail;
3686 /* Merge any attributes from the src node to the initial dst node. */
3687 for (xmlAttrPtr attr = new->properties; attr; attr = attr->next)
3689 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3690 continue;
3692 xmlAttrPtr a = xmlHasProp (ndst, attr->name);
3693 if (a)
3694 xmlRemoveProp (a);
3696 xmlChar *tmp = xmlNodeGetContent (attr->children);
3697 xmlNewProp (ndst, attr->name, tmp);
3698 xmlFree (tmp);
3699 rc = add_attribute (client, ndst, NULL, NULL);
3702 xmlNodePtr n = ndst->children;
3703 xmlUnlinkNode (n);
3704 xmlFreeNodeList (n);
3705 ndst->children = NULL;
3707 if (new->children)
3709 n = xmlCopyNodeList (new->children);
3710 if (!n)
3712 rc = GPG_ERR_ENOMEM;
3713 goto fail;
3716 n = xmlAddChildList (ndst, n);
3717 if (!n)
3719 rc = GPG_ERR_ENOMEM;
3720 goto fail;
3723 rc = update_element_mtime (client, xmlDocGetRootElement (client->doc) ==
3724 ndst->parent ? ndst : ndst->parent);
3727 fail:
3728 if (new)
3730 xmlUnlinkNode (new);
3731 xmlFreeNodeList (new);
3734 if (req)
3735 strv_free (req);
3737 if (src)
3738 strv_free (src);
3740 if (dst)
3741 strv_free (dst);
3743 return rc;
3746 static gpg_error_t
3747 copy_command (assuan_context_t ctx, char *line)
3749 struct client_s *client = assuan_get_pointer (ctx);
3750 gpg_error_t rc;
3751 struct argv_s *args[] = {
3752 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3753 NULL
3756 rc = parse_options (&line, args, client);
3757 if (rc)
3758 return send_error (ctx, rc);
3760 if (client->opts & OPT_INQUIRE)
3762 unsigned char *result;
3763 size_t len;
3765 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3766 if (rc)
3767 return send_error (ctx, rc);
3769 line = (char *) result;
3772 rc = do_copy (ctx, line);
3774 if (client->opts & OPT_INQUIRE)
3775 xfree (line);
3777 return send_error (ctx, rc);
3780 static gpg_error_t
3781 do_move (assuan_context_t ctx, char *line)
3783 struct client_s *client = assuan_get_pointer (ctx);
3784 gpg_error_t rc;
3785 char **req, **src = NULL, **dst = NULL;
3786 xmlNodePtr nsrc, ndst = NULL;
3788 req = str_split (line, " ", 0);
3790 if (!req || !req[0] || !req[1])
3792 strv_free (req);
3793 return GPG_ERR_SYNTAX;
3796 if (strchr (req[0], '\t'))
3797 src = str_split (req[0], "\t", 0);
3798 else
3799 src = str_split (req[0], " ", 0);
3801 if (!src || !*src)
3803 rc = GPG_ERR_SYNTAX;
3804 goto fail;
3807 if (strchr (req[1], '\t'))
3808 dst = str_split (req[1], "\t", 0);
3809 else
3810 dst = str_split (req[1], " ", 0);
3812 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3813 if (nsrc && src[1])
3814 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc,
3815 NULL, NULL, NULL, 0, 0, NULL, 0);
3817 if (!nsrc)
3818 goto fail;
3820 rc = is_element_owner (client, nsrc);
3821 if (rc)
3822 goto fail;
3824 if (dst)
3826 if (!valid_element_path (dst, 0))
3828 rc = GPG_ERR_INV_VALUE;
3829 goto fail;
3832 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3833 if (ndst && dst[1])
3834 ndst = find_elements (client, client->doc, ndst->children, dst + 1,
3835 &rc, NULL, NULL, NULL, 0, 0, NULL, 0);
3837 else
3838 ndst = xmlDocGetRootElement (client->doc);
3840 for (xmlNodePtr n = ndst; n; n = n->parent)
3842 if (n == nsrc)
3844 rc = GPG_ERR_CONFLICT;
3845 goto fail;
3849 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3850 goto fail;
3852 rc = 0;
3854 if (ndst)
3856 xmlChar *a = node_has_attribute (nsrc, (xmlChar *) "_name");
3858 xmlNodePtr dup = find_element (client, ndst->children, (char *) a,
3859 NULL, &rc);
3860 xmlFree (a);
3862 if (rc)
3863 goto fail;
3865 if (dup)
3867 if (dup == nsrc)
3868 goto fail;
3870 if (ndst == xmlDocGetRootElement (client->doc))
3872 xmlNodePtr n = nsrc;
3873 int match = 0;
3875 while (n->parent && n->parent != ndst)
3876 n = n->parent;
3878 xmlChar *a = node_has_attribute (n, (xmlChar *) "_name");
3879 xmlChar *b = node_has_attribute (nsrc, (xmlChar *) "_name");
3881 if (xmlStrEqual (a, b))
3883 match = 1;
3884 xmlUnlinkNode (nsrc);
3885 xmlUnlinkNode (n);
3886 xmlFreeNodeList (n);
3889 xmlFree (a);
3890 xmlFree (b);
3892 if (!match)
3894 xmlUnlinkNode (dup);
3895 xmlFreeNodeList (dup);
3898 else
3899 xmlUnlinkNode (dup);
3903 if (!ndst && dst)
3905 xmlChar *name = node_has_attribute (nsrc, (xmlChar *) "_name");
3907 if (nsrc->parent == xmlDocGetRootElement (client->doc)
3908 && !strcmp ((char *) name, *dst))
3910 xmlFree (name);
3911 rc = GPG_ERR_CONFLICT;
3912 goto fail;
3915 xmlFree (name);
3916 ndst = create_element_path (client, &dst, &rc, nsrc);
3919 if (!ndst)
3920 goto fail;
3922 update_element_mtime (client, nsrc->parent);
3923 xmlUnlinkNode (nsrc);
3924 ndst = xmlAddChildList (ndst, nsrc);
3926 if (!ndst)
3927 rc = GPG_ERR_ENOMEM;
3928 else
3929 update_element_mtime (client, ndst->parent);
3931 fail:
3932 if (req)
3933 strv_free (req);
3935 if (src)
3936 strv_free (src);
3938 if (dst)
3939 strv_free (dst);
3941 return rc;
3944 static gpg_error_t
3945 move_command (assuan_context_t ctx, char *line)
3947 struct client_s *client = assuan_get_pointer (ctx);
3948 gpg_error_t rc;
3949 struct argv_s *args[] = {
3950 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3951 NULL
3954 rc = parse_options (&line, args, client);
3955 if (rc)
3956 return send_error (ctx, rc);
3958 if (client->opts & OPT_INQUIRE)
3960 unsigned char *result;
3961 size_t len;
3963 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3964 if (rc)
3965 return send_error (ctx, rc);
3967 line = (char *) result;
3970 rc = do_move (ctx, line);
3972 if (client->opts & OPT_INQUIRE)
3973 xfree (line);
3975 return send_error (ctx, rc);
3978 static gpg_error_t
3979 ls_command (assuan_context_t ctx, char *line)
3981 gpg_error_t rc;
3982 char *tmp = str_asprintf ("%s/data", homedir);
3983 char *dir = expand_homedir (tmp);
3984 DIR *d = opendir (dir);
3986 rc = gpg_error_from_errno (errno);
3987 xfree (tmp);
3989 if (!d)
3991 xfree (dir);
3992 return send_error (ctx, rc);
3995 size_t len =
3996 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3997 struct dirent *p = xmalloc (len), *cur = NULL;
3998 char *list = NULL;
4000 xfree (dir);
4001 rc = 0;
4003 while (!readdir_r (d, p, &cur) && cur)
4005 if (cur->d_name[0] == '.' && cur->d_name[1] == '\0')
4006 continue;
4007 else if (cur->d_name[0] == '.' && cur->d_name[1] == '.'
4008 && cur->d_name[2] == '\0')
4009 continue;
4011 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
4013 if (!tmp)
4015 if (list)
4016 xfree (list);
4018 rc = GPG_ERR_ENOMEM;
4019 break;
4022 xfree (list);
4023 list = tmp;
4026 closedir (d);
4027 xfree (p);
4029 if (rc)
4030 return send_error (ctx, rc);
4032 if (!list)
4033 return send_error (ctx, GPG_ERR_NO_DATA);
4035 list[strlen (list) - 1] = 0;
4036 rc = xfer_data (ctx, list, strlen (list));
4037 xfree (list);
4038 return send_error (ctx, rc);
4041 static gpg_error_t
4042 bye_notify (assuan_context_t ctx, char *line)
4044 struct client_s *cl = assuan_get_pointer (ctx);
4046 #ifdef WITH_GNUTLS
4047 if (cl->thd->remote)
4049 int rc;
4053 struct timeval tv = { 0, 50000 };
4055 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
4056 if (rc == GNUTLS_E_AGAIN)
4057 select (0, NULL, NULL, NULL, &tv);
4059 while (rc == GNUTLS_E_AGAIN);
4061 #endif
4063 /* This will let assuan_process_next() return. */
4064 fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK);
4065 cl->last_rc = 0; // BYE command result
4066 return 0;
4069 static gpg_error_t
4070 reset_notify (assuan_context_t ctx, char *line)
4072 struct client_s *client = assuan_get_pointer (ctx);
4074 if (client)
4075 cleanup_client (client);
4077 return 0;
4081 * This is called before every Assuan command.
4083 static gpg_error_t
4084 command_startup (assuan_context_t ctx, const char *name)
4086 struct client_s *client = assuan_get_pointer (ctx);
4087 gpg_error_t rc;
4088 struct command_table_s *cmd = NULL;
4090 log_write1 ("command='%s'", name);
4091 client->last_rc = client->opts = 0;
4093 for (int i = 0; command_table[i]; i++)
4095 if (!strcasecmp (name, command_table[i]->name))
4097 if (command_table[i]->ignore_startup)
4098 return 0;
4099 cmd = command_table[i];
4100 break;
4104 client->last_rc = rc = gpg_error (file_modified (client, cmd));
4105 return rc;
4109 * This is called after every Assuan command.
4111 static void
4112 command_finalize (assuan_context_t ctx, gpg_error_t rc)
4114 struct client_s *client = assuan_get_pointer (ctx);
4116 if (!(client->flags & FLAG_LOCK_CMD))
4117 unlock_file_mutex (client, 0);
4119 log_write1 (_("command completed: rc=%u"), rc ? rc : client->last_rc);
4120 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
4121 #ifdef WITH_GNUTLS
4122 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
4123 #endif
4126 static gpg_error_t
4127 help_command (assuan_context_t ctx, char *line)
4129 gpg_error_t rc;
4130 int i;
4132 if (!line || !*line)
4134 char *tmp;
4135 char *help = str_dup (_("Usage: HELP [<COMMAND>]\n"
4136 "For commands that take an element path as an argument, each element is "
4137 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
4138 "\n" "COMMANDS:"));
4140 for (i = 0; command_table[i]; i++)
4142 if (!command_table[i]->help)
4143 continue;
4145 tmp = str_asprintf ("%s %s", help, command_table[i]->name);
4146 xfree (help);
4147 help = tmp;
4150 tmp = strip_texi_and_wrap (help);
4151 xfree (help);
4152 rc = xfer_data (ctx, tmp, strlen (tmp));
4153 xfree (tmp);
4154 return send_error (ctx, rc);
4157 for (i = 0; command_table[i]; i++)
4159 if (!strcasecmp (line, command_table[i]->name))
4161 char *help, *tmp;
4163 if (!command_table[i]->help)
4164 break;
4166 help = strip_texi_and_wrap (command_table[i]->help);
4167 tmp = str_asprintf (_("Usage: %s"), help);
4168 xfree (help);
4169 rc = xfer_data (ctx, tmp, strlen (tmp));
4170 xfree (tmp);
4171 return send_error (ctx, rc);
4175 return send_error (ctx, GPG_ERR_INV_NAME);
4178 static void
4179 new_command (const char *name, int ignore, int unlock,
4180 gpg_error_t (*handler) (assuan_context_t, char *),
4181 const char *help)
4183 int i = 0;
4185 if (command_table)
4186 for (i = 0; command_table[i]; i++);
4188 command_table =
4189 xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
4190 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
4191 command_table[i]->name = name;
4192 command_table[i]->handler = handler;
4193 command_table[i]->ignore_startup = ignore;
4194 command_table[i]->unlock = unlock;
4195 command_table[i++]->help = help;
4196 command_table[i] = NULL;
4199 void
4200 deinit_commands ()
4202 int i;
4204 for (i = 0; command_table[i]; i++)
4205 xfree (command_table[i]);
4207 xfree (command_table);
4210 static int
4211 sort_commands (const void *arg1, const void *arg2)
4213 struct command_table_s *const *a = arg1;
4214 struct command_table_s *const *b = arg2;
4216 if (!*a || !*b)
4217 return 0;
4218 else if (*a && !*b)
4219 return 1;
4220 else if (!*a && *b)
4221 return -1;
4223 return strcmp ((*a)->name, (*b)->name);
4226 static gpg_error_t
4227 passwd_command (assuan_context_t ctx, char *line)
4229 struct client_s *client = assuan_get_pointer (ctx);
4230 gpg_error_t rc;
4231 struct argv_s *args[] = {
4232 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
4233 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
4234 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG, parse_opt_no_passphrase},
4235 NULL
4238 rc = peer_is_invoker (client);
4239 if (rc == GPG_ERR_EACCES)
4240 return send_error (ctx, rc);
4242 if (client->flags & FLAG_NEW)
4243 return send_error (ctx, GPG_ERR_INV_STATE);
4245 client->crypto->save.s2k_count =
4246 config_get_ulong (client->filename, "s2k_count");
4247 rc = parse_options (&line, args, client);
4248 if (rc)
4249 return send_error (ctx, rc);
4251 if (!rc && client->opts & OPT_RESET)
4253 rc = cache_clear (client->md5file);
4254 if (!rc)
4255 send_status_all (STATUS_CACHE, NULL);
4258 if (!rc)
4260 if (!IS_PKI (client->crypto))
4262 struct crypto_s *crypto;
4264 xfree (client->crypto->filename);
4265 client->crypto->filename = str_dup (client->filename);
4266 rc = change_passwd (ctx, client->filename,
4267 client->flags & FLAG_NO_PINENTRY, &crypto,
4268 (client->opts & OPT_NO_PASSPHRASE));
4269 if (!rc)
4271 cleanup_crypto (&client->crypto);
4272 client->crypto = crypto;
4273 update_checksum (client);
4274 cleanup_crypto_stage1 (client->crypto);
4277 #ifdef WITH_AGENT
4278 else
4280 if (client->crypto->save.s2k_count)
4281 rc = send_to_agent (client->crypto->agent, NULL, NULL,
4282 "OPTION s2k-count=%lu",
4283 client->crypto->save.s2k_count);
4285 if (!rc)
4286 rc = agent_passwd (client->crypto);
4288 #endif
4291 return send_error (ctx, rc);
4294 static gpg_error_t
4295 parse_keygrip_opt_sign (void *data, void *value)
4297 struct client_s *client = data;
4299 (void) value;
4300 client->opts |= OPT_SIGN;
4301 return 0;
4304 static gpg_error_t
4305 keygrip_command (assuan_context_t ctx, char *line)
4307 struct client_s *client = assuan_get_pointer (ctx);
4308 gpg_error_t rc;
4309 struct crypto_s *crypto = NULL;
4310 struct argv_s *args[] = {
4311 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_keygrip_opt_sign},
4312 NULL
4315 if (!line || !*line)
4316 return send_error (ctx, GPG_ERR_SYNTAX);
4318 rc = parse_options (&line, args, client);
4319 if (rc)
4320 return send_error (ctx, rc);
4322 if (!valid_filename (line))
4323 return send_error (ctx, GPG_ERR_INV_VALUE);
4325 rc = init_client_crypto (&crypto);
4326 if (rc)
4327 return send_error (ctx, rc);
4329 rc = read_data_file (line, crypto);
4330 if (!rc)
4332 char *hexgrip = NULL;
4334 if (!IS_PKI (crypto))
4336 cleanup_crypto (&crypto);
4337 return send_error (ctx, GPG_ERR_NOT_SUPPORTED);
4340 if (client->opts & OPT_SIGN)
4342 if (valid_keygrip (crypto->sign_grip, sizeof (crypto->sign_grip)))
4343 hexgrip = bin2hex (crypto->sign_grip, sizeof (crypto->sign_grip));
4346 if (!hexgrip)
4347 hexgrip = bin2hex (crypto->grip, sizeof (crypto->grip));
4349 if (!hexgrip)
4350 rc = GPG_ERR_ENOMEM;
4351 else
4352 rc = xfer_data (ctx, hexgrip, strlen (hexgrip));
4354 xfree (hexgrip);
4357 UPDATE_AGENT_CTX (client, crypto);
4358 cleanup_crypto (&crypto);
4359 return send_error (ctx, rc);
4362 static gpg_error_t
4363 parse_opt_data (void *data, void *value)
4365 struct client_s *client = data;
4367 (void) value;
4368 client->opts |= OPT_DATA;
4369 return 0;
4372 static gpg_error_t
4373 getinfo_command (assuan_context_t ctx, char *line)
4375 struct client_s *client = assuan_get_pointer (ctx);
4376 gpg_error_t rc;
4377 char buf[ASSUAN_LINELENGTH];
4378 struct argv_s *args[] = {
4379 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4380 NULL
4383 rc = parse_options (&line, args, client);
4384 if (rc)
4385 return send_error (ctx, rc);
4387 if (!strcasecmp (line, "clients"))
4389 if (client->opts & OPT_DATA)
4391 MUTEX_LOCK (&cn_mutex);
4392 snprintf (buf, sizeof (buf), "%i", slist_length (cn_thread_list));
4393 MUTEX_UNLOCK (&cn_mutex);
4394 rc = xfer_data (ctx, buf, strlen (buf));
4396 else
4397 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4399 else if (!strcasecmp (line, "cache"))
4401 if (client->opts & OPT_DATA)
4403 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4404 rc = xfer_data (ctx, buf, strlen (buf));
4406 else
4407 rc = send_status (ctx, STATUS_CACHE, NULL);
4409 else if (!strcasecmp (line, "pid"))
4411 char buf[32];
4412 pid_t pid = getpid ();
4414 snprintf (buf, sizeof (buf), "%i", pid);
4415 rc = xfer_data (ctx, buf, strlen (buf));
4417 else if (!strcasecmp (line, "version"))
4419 char *buf = str_asprintf ("0x%06x %s%s", VERSION_HEX,
4420 #ifdef WITH_GNUTLS
4421 "GNUTLS "
4422 #endif
4423 #ifdef WITH_QUALITY
4424 "QUALITY "
4425 #endif
4426 "", use_agent ? "AGENT" : "");
4427 rc = xfer_data (ctx, buf, strlen (buf));
4428 xfree (buf);
4430 else if (!strcasecmp (line, "last_error"))
4432 if (client->last_error)
4433 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4434 else
4435 rc = GPG_ERR_NO_DATA;
4437 else if (!strcasecmp (line, "user"))
4439 char *user = NULL;
4441 #ifdef WITH_GNUTLS
4442 if (client->thd->remote)
4443 user = str_asprintf ("#%s", client->thd->tls->fp);
4444 else
4445 user = get_username (client->thd->peer->uid);
4446 #else
4447 user = get_username (client->thd->peer->uid);
4448 #endif
4449 if (user)
4450 rc = xfer_data (ctx, user, strlen (user));
4451 else
4452 rc = GPG_ERR_NO_DATA;
4454 xfree (user);
4456 else
4457 rc = gpg_error (GPG_ERR_SYNTAX);
4459 return send_error (ctx, rc);
4462 #ifdef WITH_AGENT
4463 static gpg_error_t
4464 send_data_cb (void *user, const void *buf, size_t len)
4466 assuan_context_t ctx = user;
4468 return assuan_send_data (ctx, buf, len);
4471 static gpg_error_t
4472 send_status_cb (void *user, const char *line)
4474 assuan_context_t ctx = user;
4475 char keyword[200], *k;
4476 const char *p;
4478 for (p = line, k = keyword; *p; p++)
4480 if (isspace (*p))
4481 break;
4483 *k++ = *p;
4486 *k = 0;
4487 if (*p == '#')
4488 p++;
4490 while (isspace (*p))
4491 p++;
4493 return assuan_write_status (ctx, keyword, *p ? p : NULL);
4495 #endif
4497 static gpg_error_t
4498 agent_command (assuan_context_t ctx, char *line)
4500 gpg_error_t rc = 0;
4502 if (!use_agent)
4503 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
4505 #ifdef WITH_AGENT
4506 struct client_s *client = assuan_get_pointer (ctx);
4508 if (!line || !*line)
4509 return send_error (ctx, GPG_ERR_SYNTAX);
4511 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 1);
4512 rc = assuan_transact (client->crypto->agent->ctx, line, send_data_cb,
4513 client->ctx, agent_loopback_cb, client->crypto,
4514 send_status_cb, client->ctx);
4515 if (gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
4517 char *line;
4518 size_t len;
4520 rc = assuan_write_line (client->crypto->agent->ctx, "CAN");
4521 if (!rc)
4523 rc = assuan_read_line (client->crypto->agent->ctx, &line, &len);
4524 if (!rc)
4525 rc = gpg_error (GPG_ERR_ASS_CANCELED);
4529 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 0);
4530 #endif
4531 return send_error (ctx, rc);
4534 void
4535 init_commands ()
4537 /* !BEGIN-HELP-TEXT!
4539 * This comment is used as a marker to generate the offline documentation
4540 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4541 * script to determine where commands begin and end.
4543 new_command("HELP", 1, 1, help_command, _(
4544 "HELP [<COMMAND>]\n"
4545 "Show available commands or command specific help text."
4548 new_command("AGENT", 1, 1, agent_command, _(
4549 "AGENT <command>\n"
4550 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4551 "@command{gpg-agent}."
4554 new_command("GETINFO", 1, 1, getinfo_command, _(
4555 "GETINFO [--data] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4556 "Get server and other information: @var{cache} returns the number of cached "
4557 "documents via a status message. @var{clients} returns the number of "
4558 "connected clients via a status message. @var{pid} returns the process ID "
4559 "number of the server via a data response. @var{VERSION} returns the server "
4560 "version number and compile-time features with a data response with each "
4561 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4562 "the last failed command when available. @var{USER} returns the username or "
4563 "@abbr{TLS} hash of the connected client. @xref{Status Messages}. "
4564 "\n"
4565 "When the @option{--data} option is specified then the result will be sent "
4566 "via a data response rather than a status message."
4569 new_command("PASSWD", 0, 0, passwd_command, _(
4570 "PASSWD [--reset] [--s2k-count=N] [--no-passphrase]\n"
4571 "Changes the passphrase of the secret key required to open the current "
4572 "file or the passphrase of a symmetrically encrypted data file. When the "
4573 "@option{--reset} option is passed then the cache entry for the current "
4574 "file will be reset and the passphrase, if any, will be required during the "
4575 "next @code{OPEN} (@pxref{OPEN})."
4576 "\n"
4577 "The @option{--s2k-count} option sets or changes (@pxref{SAVE}) the number "
4578 "of hash iterations for a passphrase and must be either @code{0} to use "
4579 "the calibrated count of the machine (the default), or a value greater than "
4580 "or equal to @code{65536}. This option has no effect for symmetrically "
4581 "encrypted data files."
4582 "\n"
4583 "The @option{--no-passphrase} option will prevent requiring a passphrase for "
4584 "the data file, although a passphrase may be required when changing it."
4585 "\n"
4586 "This command is not available for non-invoking clients "
4587 "(@pxref{Access Control})."
4590 new_command("KEYGRIP", 1, 1, keygrip_command, _(
4591 "KEYGRIP [--sign] <filename>\n"
4592 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4593 "data response."
4594 "\n"
4595 "When the @option{--sign} option is specified then the key used for signing "
4596 "of the specified @var{filename} will be returned."
4597 "\n"
4598 "For symmetrically encrypted data files this command returns the error "
4599 "GPG_ERR_NOT_SUPPORTED."
4602 new_command("OPEN", 1, 1, open_command, _(
4603 "OPEN [--lock] <filename> [<passphrase>]\n"
4604 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4605 "found on the file-system then a new document will be created. If the file "
4606 "is found, it is looked for in the file cache. If cached and no "
4607 "@var{passphrase} was specified then the cached document is opened. When not "
4608 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4609 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4610 "specified."
4611 "\n"
4612 "When the @option{--lock} option is passed then the file mutex will be "
4613 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4614 "file has been opened."
4617 new_command("SAVE", 0, 0, save_command, _(
4618 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring] [--sign-keygrip=hexstring]\n"
4619 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4620 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4621 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4622 "keypair will be generated and a pinentry will be used to prompt for the "
4623 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4624 "passed in which case the data file will not be passphrase protected. "
4625 "\n"
4626 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4627 "passphrase retrieval and caching of new files when @command{gpg-agent} "
4628 "use is enabled. The datafile will be symmetrically encrypted and will not "
4629 "use or generate any keypair."
4630 "\n"
4631 "The @option{--reset} option will clear the cache entry for the current file "
4632 "and require a passphrase, if needed, before saving."
4633 "\n"
4634 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4635 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4636 "(@pxref{Configuration}) for available ciphers."
4637 "\n"
4638 "The @option{--cipher-iterations} option specifies the number of times to "
4639 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4640 "\n"
4641 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4642 "the client to obtain the key paramaters to use when generating a new "
4643 "keypair. The inquired data is expected to be an S-expression. If not "
4644 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4645 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4646 "that when this option is specified a new keypair will be generated "
4647 "reguardless if the file is a new one and that if the data file is protected "
4648 "the passphrase to open it will be required before generating the new "
4649 "keypair. This option is not available for non-invoking clients "
4650 "(@pxref{Access Control})."
4651 "\n"
4652 "You can encrypt the data file to a public key other than the one that it "
4653 "was originally encrypted with by passing the @option{--keygrip} option with "
4654 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4655 "be of any key that @command{gpg-agent} knows about. The "
4656 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4657 "secret key. Use the @code{KEYGRIP} (@pxref{KEYGRIP}) command to obtain the "
4658 "keygrip of an existing data file. This option may be needed when using a "
4659 "smartcard. This option has no effect with symmetrically encrypted data "
4660 "files. These options are not available for non-invoking clients "
4661 "(@pxref{Access Control})."
4662 "\n"
4663 "The @option{--s2k-count} option sets number of hash iterations for a "
4664 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4665 "value and is the default. This setting only affects new files. To change "
4666 "the setting use the @code{PASSWD} command (@pxref{PASSWD}). This option "
4667 "has no effect with symmetrically encrypted data files."
4670 new_command("ISCACHED", 1, 0, iscached_command, _(
4671 "ISCACHED [--lock] <filename>\n"
4672 "An @emph{OK} response is returned if the specified @var{filename} is found "
4673 "in the file cache. If not found in the cache but exists on the filesystem "
4674 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4675 "returned."
4676 "\n"
4677 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4678 "file exists; it does not need to be opened nor cached. The lock will be "
4679 "released when the client exits or sends the @code{UNLOCK} (@pxref{UNLOCK}) "
4680 "command."
4683 new_command("CLEARCACHE", 1, 1, clearcache_command, _(
4684 "CLEARCACHE [<filename>]\n"
4685 "Clears a file cache entry for all or the specified @var{filename}."
4688 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command, _(
4689 "CACHETIMEOUT <filename> <seconds>\n"
4690 "The time in @var{seconds} until @var{filename} will be removed from the "
4691 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4692 "the passphrase for each @code{OPEN} or @code{SAVE} command (@pxref{OPEN}, "
4693 "@pxref{SAVE}). @xref{Configuration}, and the @code{cache_timeout} "
4694 "parameter."
4697 new_command("LIST", 0, 1, list_command, _(
4698 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4699 "If no element path is given then a newline separated list of root elements "
4700 "is returned with a data response. If given, then all reachable elements "
4701 "of the specified element path are returned unless the @option{--no-recurse} "
4702 "option is specified. If specified, only the child elements of the element "
4703 "path are returned without recursing into grandchildren. Each resulting "
4704 "element is prefixed with the literal @code{!} character when the element "
4705 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4706 "\n"
4707 "When the @option{--verbose} option is passed then each element path "
4708 "returned will have zero or more flags appened to it. These flags are "
4709 "delimited from the element path by a single space character. A flag itself "
4710 "is a single character. Flag @code{P} indicates that access to the element "
4711 "is denied. Flag @code{+} indicates that there are child nodes of "
4712 "the current element path. Flag @code{E} indicates that an element of an "
4713 "element path contained in a @var{target} attribute could not be found. Flag "
4714 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4715 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4716 "of the @var{target} attribute contained in the current element (see below)."
4717 "\n"
4718 "The @option{--with-target} option implies @option{--verbose} and will append "
4719 "an additional flag @code{T} followed by a single space then an element path. "
4720 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4721 "current element when it contains a @var{target} attribute. When no "
4722 "@var{target} attribute is found then no flag will be appended."
4723 "\n"
4724 "The @option{--no-recurse} option limits the amount of data returned to only "
4725 "the listing of children of the specified element path and not any "
4726 "grandchildren."
4727 "\n"
4728 "The @option{--all} option lists the entire element tree for each root "
4729 "element. This option also implies option @option{--verbose}."
4730 "\n"
4731 "When the @option{--inquire} option is passed then all remaining non-option "
4732 "arguments are retrieved via a server @emph{INQUIRE}."
4735 new_command("REALPATH", 0, 1, realpath_command, _(
4736 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4737 "Resolves all @code{target} attributes of the specified element path and "
4738 "returns the result with a data response. @xref{Target Attribute}, for details."
4739 "\n"
4740 "When the @option{--inquire} option is passed then all remaining non-option "
4741 "arguments are retrieved via a server @emph{INQUIRE}."
4744 new_command("STORE", 0, 1, store_command, _(
4745 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4746 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4747 "\n"
4748 "Creates a new element path or modifies the @var{content} of an existing "
4749 "element. If only a single element is specified then a new root element is "
4750 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4751 "set to the final @key{TAB} delimited element. If no @var{content} is "
4752 "specified after the final @key{TAB}, then the content of an existing "
4753 "element will be removed; or empty when creating a new element."
4754 "\n"
4755 "The only restriction of an element name is that it not contain whitespace "
4756 "or begin with the literal element character @code{!} unless specifying a "
4757 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4758 "the @key{TAB} delimited elements. It is recommended that the content of an "
4759 "element be base64 encoded when it contains control or @key{TAB} characters "
4760 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
4763 new_command("RENAME", 0, 1, rename_command, _(
4764 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4765 "Renames the specified @var{element} to the new @var{value}. If an element of "
4766 "the same name as the @var{value} already exists it will be overwritten."
4767 "\n"
4768 "When the @option{--inquire} option is passed then all remaining non-option "
4769 "arguments are retrieved via a server @emph{INQUIRE}."
4772 new_command("COPY", 0, 1, copy_command, _(
4773 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4774 "Copies the entire element tree starting from the child node of the source "
4775 "element, to the destination element path. If the destination element path "
4776 "does not exist then it will be created; otherwise it is overwritten."
4777 "\n"
4778 "Note that attributes from the source element are merged into the "
4779 "destination element when the destination element path exists. When an "
4780 "attribute of the same name exists in both the source and destination "
4781 "elements then the destination attribute will be updated to the source "
4782 "attribute value."
4783 "\n"
4784 "When the @option{--inquire} option is passed then all remaining non-option "
4785 "arguments are retrieved via a server @emph{INQUIRE}."
4788 new_command("MOVE", 0, 1, move_command, _(
4789 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4790 "Moves the source element path to the destination element path. If the "
4791 "destination is not specified then it will be moved to the root node of the "
4792 "document. If the destination is specified and exists then it will be "
4793 "overwritten; otherwise non-existing elements of the destination element "
4794 "path will be created."
4795 "\n"
4796 "When the @option{--inquire} option is passed then all remaining non-option "
4797 "arguments are retrieved via a server @emph{INQUIRE}."
4800 new_command("DELETE", 0, 1, delete_command, _(
4801 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4802 "Removes the specified element path and all of its children. This may break "
4803 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4804 "refers to this element or any of its children."
4805 "\n"
4806 "When the @option{--inquire} option is passed then all remaining non-option "
4807 "arguments are retrieved via a server @emph{INQUIRE}."
4810 new_command("GET", 0, 1, get_command, _(
4811 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4812 "Retrieves the content of the specified element. The content is returned "
4813 "with a data response."
4814 "\n"
4815 "When the @option{--inquire} option is passed then all remaining non-option "
4816 "arguments are retrieved via a server @emph{INQUIRE}."
4819 new_command("ATTR", 0, 1, attr_command, _(
4820 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4821 "@table @asis\n"
4822 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4823 "\n"
4824 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4825 "element. When no @var{value} is specified any existing value will be removed."
4826 "\n"
4827 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4828 "\n"
4829 " Removes an @var{attribute} from an element."
4830 "\n"
4831 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4832 "\n"
4833 " Retrieves a newline separated list of attributes names and values "
4834 "from the specified element. Each attribute name and value is space delimited."
4835 "\n"
4836 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4837 "\n"
4838 " Retrieves the value of an @var{attribute} from an element."
4839 "@end table\n"
4840 "\n"
4841 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4842 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4843 "commands instead."
4844 "\n"
4845 "The @code{_mtime} attribute is updated each time an element is modified by "
4846 "either storing content, editing attributes or by deleting a child element. "
4847 "The @code{_ctime} attribute is created for each new element in an element "
4848 "path."
4849 "\n"
4850 "When the @option{--inquire} option is passed then all remaining non-option "
4851 "arguments are retrieved via a server @emph{INQUIRE}."
4852 "\n"
4853 "@xref{Target Attribute}, for details about this special attribute."
4856 new_command("XPATH", 0, 1, xpath_command, _(
4857 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4858 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4859 "specified it is assumed the expression is a request to return a result. "
4860 "Otherwise, the result is set to the @var{value} argument and the document is "
4861 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4862 "is assumed to be empty and the document is updated. For example:"
4863 "@sp 1\n"
4864 "@example\n"
4865 "XPATH //element[@@_name='password']@key{TAB}\n"
4866 "@end example\n"
4867 "@sp 1"
4868 "would clear the content of all @code{password} elements in the data file "
4869 "while leaving off the trailing @key{TAB} would return all @code{password} "
4870 "elements in @abbr{XML} format."
4871 "\n"
4872 "When the @option{--inquire} option is passed then all remaining non-option "
4873 "arguments are retrieved via a server @emph{INQUIRE}."
4874 "\n"
4875 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4876 "expression syntax."
4879 new_command("XPATHATTR", 0, 1, xpathattr_command, _(
4880 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4881 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4882 "attributes and does not return a result. For the @var{SET} operation the "
4883 "@var{value} is optional but the field is required. If not specified then "
4884 "the attribute value will be empty. For example:"
4885 "@sp 1"
4886 "@example\n"
4887 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4888 "@end example\n"
4889 "@sp 1"
4890 "would create an @code{password} attribute for each @code{password} element "
4891 "found in the document. The attribute value will be empty but still exist."
4892 "\n"
4893 "When the @option{--inquire} option is passed then all remaining non-option "
4894 "arguments are retrieved via a server @emph{INQUIRE}."
4895 "\n"
4896 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4897 "expression syntax."
4900 new_command("IMPORT", 0, 1, import_command, _(
4901 "IMPORT [--root [!]element[<TAB>[!]child[..]]] <content>\n"
4902 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4903 "\n"
4904 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4905 "argument is raw @abbr{XML} data. The content is created as a child of "
4906 "the element path specified with the @option{--root} option or at the "
4907 "document root when not specified. Existing elements of the same name will "
4908 "be overwritten."
4909 "\n"
4910 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4911 "for details."
4914 new_command("DUMP", 0, 1, dump_command, _(
4915 "DUMP\n"
4916 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4917 "dumping a specific node."
4920 new_command("LOCK", 0, 0, lock_command, _(
4921 "LOCK\n"
4922 "Locks the mutex associated with the opened file. This prevents other clients "
4923 "from sending commands to the same opened file until the client "
4924 "that sent this command either disconnects or sends the @code{UNLOCK} "
4925 "command. @xref{UNLOCK}."
4928 new_command("UNLOCK", 1, 0, unlock_command, _(
4929 "UNLOCK\n"
4930 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4931 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4932 "@pxref{ISCACHED})."
4935 new_command("GETCONFIG", 1, 1, getconfig_command, _(
4936 "GETCONFIG [filename] <parameter>\n"
4937 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4938 "data response. If no file has been opened then the value for @var{filename} "
4939 "or the default from the @samp{global} section will be returned. If a file "
4940 "has been opened and no @var{filename} is specified, a value previously "
4941 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4944 new_command("OPTION", 1, 1, option_command, _(
4945 "OPTION <NAME>=<VALUE>\n"
4946 "Sets a client option @var{name} to @var{value}. The value for an option is "
4947 "kept for the duration of the connection."
4948 "\n"
4949 "@table @asis\n"
4950 "@item DISABLE-PINENTRY\n"
4951 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
4952 "server inquire is sent to the client to obtain the passphrase. This option "
4953 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4954 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
4955 "\n"
4956 "@item PINENTRY-TIMEOUT\n"
4957 "Sets the number of seconds before a pinentry prompt will return an error "
4958 "while waiting for user input."
4959 "\n"
4960 "@item TTYNAME\n"
4961 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4962 "\n"
4963 "@item TTYTYPE\n"
4964 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4965 "\n"
4966 "@item DISPLAY\n"
4967 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4968 "\n"
4969 "@item PINENTRY-DESC\n"
4970 "Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4971 "\n"
4972 "@item PINENTRY-TITLE\n"
4973 "Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
4974 "\n"
4975 "@item PINENTRY-PROMPT\n"
4976 "Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
4977 "\n"
4978 "@item LC-CTYPE\n"
4979 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4980 "\n"
4981 "@item LC-MESSAGES\n"
4982 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4983 "\n"
4984 "@item NAME\n"
4985 "Associates the thread ID of the connection with the specified textual "
4986 "representation. Useful for debugging log messages."
4987 "\n"
4988 "@item LOCK-TIMEOUT\n"
4989 "When not @code{0}, the duration in tenths of a second to wait for the file "
4990 "mutex which has been locked by another thread to be released before returning "
4991 "an error. When @code{-1}, then an error will be returned immediately."
4992 "\n"
4993 "@item LOG-LEVEL\n"
4994 "An integer specifiying the logging level."
4995 "@end table\n"
4998 new_command("LS", 1, 1, ls_command, _(
4999 "LS\n"
5000 "Lists the available data files stored in the data directory "
5001 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
5004 new_command("RESET", 1, 1, NULL, _(
5005 "RESET\n"
5006 "Closes the currently opened file but keeps any previously set client options."
5009 new_command("NOP", 1, 1, NULL, _(
5010 "NOP\n"
5011 "Does nothing. Always returns successfully."
5014 /* !END-HELP-TEXT! */
5015 new_command ("CANCEL", 1, 1, NULL, NULL);
5016 new_command ("END", 1, 1, NULL, NULL);
5017 new_command ("BYE", 1, 1, NULL, NULL);
5019 int i;
5020 for (i = 0; command_table[i]; i++);
5021 qsort (command_table, i - 1, sizeof (struct command_table_s *),
5022 sort_commands);
5025 gpg_error_t
5026 register_commands (assuan_context_t ctx)
5028 int i = 0, rc;
5030 for (; command_table[i]; i++)
5032 if (!command_table[i]->handler)
5033 continue;
5035 rc = assuan_register_command (ctx, command_table[i]->name,
5036 command_table[i]->handler,
5037 command_table[i]->help);
5038 if (rc)
5039 return rc;
5042 rc = assuan_register_bye_notify (ctx, bye_notify);
5043 if (rc)
5044 return rc;
5046 rc = assuan_register_reset_notify (ctx, reset_notify);
5047 if (rc)
5048 return rc;
5050 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
5051 if (rc)
5052 return rc;
5054 return assuan_register_post_cmd_notify (ctx, command_finalize);