Access control for the _acl attribute.
[pwmd.git] / src / commands.c
blob40fe76f89274bd0b3c66199a8b768743e982c829
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 client->crypto->pkey_sexp = NULL;
217 memset (client->crypto->sign_grip, 0,
218 sizeof (client->crypto->sign_grip));
219 memset (client->crypto->grip, 0, sizeof(client->crypto->grip));
223 void
224 cleanup_client (struct client_s *client)
226 assuan_context_t ctx = client->ctx;
227 struct client_thread_s *thd = client->thd;
228 struct crypto_s *crypto = client->crypto;
229 long lock_timeout = client->lock_timeout;
230 int no_pinentry = (client->flags & FLAG_NO_PINENTRY);
231 struct pinentry_option_s pin_opts;
232 xmlErrorPtr xml_error = client->xml_error;
233 #ifdef WITH_AGENT
234 struct pinentry_option_s agent_pin_opts;
236 if (crypto && crypto->agent)
237 memcpy (&agent_pin_opts, &crypto->agent->pinentry_opts,
238 sizeof(struct pinentry_option_s));
239 #endif
241 memcpy (&pin_opts, &client->pinentry_opts, sizeof(struct pinentry_option_s));
242 unlock_file_mutex (client, client->flags & FLAG_NEW);
243 free_client (client);
244 memset (client, 0, sizeof (struct client_s));
245 client->xml_error = xml_error;
246 client->crypto = crypto;
247 client->ctx = ctx;
248 client->thd = thd;
249 client->lock_timeout = lock_timeout;
250 memcpy (&client->pinentry_opts, &pin_opts, sizeof(struct pinentry_option_s));
251 client->flags |= no_pinentry ? FLAG_NO_PINENTRY : 0;
252 #ifdef WITH_AGENT
253 if (crypto && crypto->agent)
254 memcpy (&crypto->agent->pinentry_opts, &agent_pin_opts,
255 sizeof(struct pinentry_option_s));
256 #endif
259 static gpg_error_t
260 open_finalize (assuan_context_t ctx, char *key, size_t keylen)
262 struct client_s *client = assuan_get_pointer (ctx);
263 gpg_error_t rc = 0;
264 struct cache_data_s *cdata = cache_get_data (client->md5file);
265 int cached = 0, keyarg = key ? 1 : 0;
266 size_t keysize;
267 unsigned char *salted_key = NULL;
268 int algo;
269 int pin_try = 1;
270 int pin_tries = 3;
271 char *pin_title = client->pinentry_opts.title;
273 client->crypto->filename = str_dup (client->filename);
274 if (cdata || client->flags & FLAG_NEW)
276 if (cdata && !(client->flags & FLAG_NEW))
278 rc = decrypt_cache (client->crypto, cdata->doc, cdata->doclen);
279 if (rc)
280 return rc;
283 cached = cdata != NULL;
284 goto done;
287 if (!key && !IS_PKI (client->crypto)
288 && !(client->crypto->hdr.flags & PWMD_FLAG_NO_PASSPHRASE))
290 if (client->flags & FLAG_NO_PINENTRY)
292 rc = inquire_passphrase (ctx, "PASSPHRASE", (unsigned char **)&key,
293 &keylen);
294 if (rc)
295 return rc;
297 else
299 client->pinentry_opts.timeout = config_get_integer (client->filename,
300 "pinentry_timeout");
301 pin_again:
302 rc = getpin_common (client->ctx, client->filename, PINENTRY_OPEN,
303 &key, &keylen);
304 if (rc)
305 return rc;
309 if (!IS_PKI (client->crypto))
311 if (client->crypto->hdr.flags & PWMD_FLAG_NO_PASSPHRASE)
313 keylen = 1;
314 key = gcry_malloc (keylen);
315 memset (key, 0, keylen);
318 algo = cipher_to_gcrypt (client->crypto->hdr.flags);
319 rc = hash_key (algo, client->crypto->hdr.salt,
320 sizeof(client->crypto->hdr.salt), key, keylen,
321 (void **)&salted_key, &keysize);
322 if (!keyarg)
323 gcry_free (key);
325 if (!rc)
327 rc = decrypt_data (client->ctx, client->crypto, salted_key, keysize);
328 if (gpg_err_code (rc) == GPG_ERR_BAD_PASSPHRASE
329 && ++pin_try <= pin_tries && !(client->flags & FLAG_NO_PINENTRY))
331 gcry_free (salted_key);
332 salted_key = NULL;
333 key = NULL;
334 keylen = 0;
335 client->pinentry_opts.title = str_asprintf (_ ("Bad passphrase (try %i of %i)"), pin_try, pin_tries);
336 goto pin_again;
340 if (client->pinentry_opts.title != pin_title)
341 xfree (client->pinentry_opts.title);
343 client->pinentry_opts.title = pin_title;
344 if (rc)
346 gcry_free (salted_key);
347 return rc;
350 cdata = xcalloc (1, sizeof (struct cache_data_s));
351 if (!cdata)
353 gcry_free (salted_key);
354 return GPG_ERR_ENOMEM;
357 cdata->key = salted_key;
358 cdata->keylen = keysize;
360 else
361 rc = decrypt_data (client->ctx, client->crypto, NULL, 0);
363 done:
364 if (!rc)
366 rc = parse_xml (ctx, client->flags & FLAG_NEW);
367 if (rc && !cached)
368 free_cache_data_once (cdata);
370 if (!rc)
372 int timeout = config_get_integer (client->filename,
373 "cache_timeout");
375 if (!cached)
377 if (!cdata)
378 cdata = xcalloc (1, sizeof (struct cache_data_s));
380 rc = encrypt_xml (NULL, cache_key, cache_keysize,
381 GCRY_CIPHER_AES, client->crypto->plaintext,
382 client->crypto->plaintext_len, &cdata->doc,
383 &cdata->doclen, &cache_iv, &cache_blocksize,
385 if (!rc && !(client->flags & FLAG_NEW) && IS_PKI (client->crypto))
387 rc = gcry_sexp_build ((gcry_sexp_t *) & cdata->pubkey, NULL,
388 "%S", client->crypto->pkey_sexp);
392 if (cdata->sigkey)
393 gcry_sexp_release (cdata->sigkey);
395 cdata->sigkey = NULL;
396 if (!rc && IS_PKI (client->crypto))
397 rc = gcry_sexp_build ((gcry_sexp_t *) &cdata->sigkey, NULL,
398 "%S", client->crypto->sigpkey_sexp);
400 if (!rc && !cache_add_file (client->md5file,
401 (client->flags & FLAG_NEW)
402 ? NULL
403 : client->crypto->grip, cdata, timeout))
405 if (!cached)
407 free_cache_data_once (cdata);
408 xmlFreeDoc (client->doc);
409 client->doc = NULL;
411 rc = GPG_ERR_ENOMEM;
414 if (!rc)
415 client->flags |= FLAG_OPEN;
419 if (!rc)
420 update_checksum (client);
422 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
423 rc = do_lock (client, 0);
425 return rc;
428 static void
429 req_cleanup (void *arg)
431 if (!arg)
432 return;
434 strv_free ((char **) arg);
437 static gpg_error_t
438 parse_open_opt_lock (void *data, void *value)
440 struct client_s *client = data;
442 client->opts |= OPT_LOCK_ON_OPEN;
443 return 0;
446 static gpg_error_t
447 parse_opt_inquire (void *data, void *value)
449 struct client_s *client = data;
451 (void) value;
452 client->opts |= OPT_INQUIRE;
453 return 0;
456 static gpg_error_t
457 update_checksum (struct client_s *client)
459 unsigned char *crc;
460 size_t len;
461 struct cache_data_s *cdata;
462 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
464 if (rc)
465 return rc;
467 xfree (client->crc);
468 client->crc = crc;
469 cdata = cache_get_data (client->md5file);
470 if (cdata)
472 xfree (cdata->crc);
473 cdata->crc = xmalloc (len);
474 memcpy (cdata->crc, crc, len);
477 return 0;
480 static gpg_error_t
481 validate_checksum (struct client_s *client, struct cache_data_s *cdata)
483 unsigned char *crc;
484 size_t len;
485 gpg_error_t rc;
486 int n = 0;
488 if (cdata && !cdata->crc)
489 return GPG_ERR_CHECKSUM;
491 rc = get_checksum (client->filename, &crc, &len);
492 if (rc)
493 return rc;
495 if (cdata)
496 n = memcmp (cdata->crc, crc, len);
497 else if (client->crc)
498 n = memcmp (client->crc, crc, len);
500 xfree (crc);
501 return n ? GPG_ERR_CHECKSUM : 0;
504 static gpg_error_t
505 do_open (assuan_context_t ctx, const char *filename, const char *password)
507 struct client_s *client = assuan_get_pointer (ctx);
508 struct cache_data_s *cdata;
509 gpg_error_t rc = 0;
510 int done = 0;
512 if (!valid_filename (filename))
513 return GPG_ERR_INV_VALUE;
515 gcry_md_hash_buffer (GCRY_MD_MD5, client->md5file, filename,
516 strlen (filename));
517 client->filename = str_dup (filename);
518 if (!client->filename)
519 return GPG_ERR_ENOMEM;
521 // Cached document?
522 cdata = cache_get_data (client->md5file);
523 if (cdata && cdata->doc)
525 int defer = 0;
527 /* This will check that the key is cached in the agent which needs to
528 * be determined for files that share a keygrip. */
529 if (!rc)
531 rc = cache_iscached (client->filename, &defer);
532 if ((!rc || rc == GPG_ERR_ENOENT) && defer)
533 rc = GPG_ERR_KEY_EXPIRED;
536 if (!rc && !(client->flags & FLAG_NEW))
537 rc = validate_checksum (client, cdata);
539 #ifdef WITH_GNUTLS
540 if (!rc && client->thd->remote
541 && config_get_boolean (client->filename, "tcp_require_key"))
542 rc = GPG_ERR_KEY_EXPIRED;
543 #endif
545 if (!rc && !password)
547 rc = read_data_header (client->filename, &client->crypto->hdr,
548 NULL, NULL);
549 if (!rc)
551 if (IS_PKI (client->crypto))
553 gcry_sexp_build (&client->crypto->pkey_sexp, NULL, "%S",
554 cdata->pubkey);
555 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
556 client->crypto->grip);
557 gcry_sexp_build (&client->crypto->sigpkey_sexp, NULL, "%S",
558 cdata->sigkey);
559 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
560 client->crypto->sign_grip);
563 if (!rc)
565 rc = open_finalize (ctx, NULL, 0);
566 done = 1;
571 /* There was an error accessing the file so clear the cache entry. The
572 * real error will be returned from read_data_file() since the file
573 * may have only disappeared. */
574 if (!done)
576 log_write ("%s: %s", filename, pwmd_strerror (rc));
577 rc = cache_clear (client->md5file);
578 send_status_all (STATUS_CACHE, NULL);
582 if (done || rc)
583 return rc;
585 rc = read_data_file (client->filename, client->crypto);
586 if (rc)
588 if (gpg_err_source (rc) != GPG_ERR_SOURCE_DEFAULT ||
589 gpg_err_code (rc) != GPG_ERR_ENOENT)
591 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
592 return rc;
595 client->flags |= FLAG_NEW;
596 memset (client->crypto->grip, 0, sizeof (client->crypto->grip));
597 memset (client->crypto->sign_grip, 0,
598 sizeof (client->crypto->sign_grip));
599 rc = open_finalize (ctx, NULL, 0);
600 return rc;
603 if (password && IS_PKI (client->crypto))
605 #ifdef WITH_AGENT
606 rc = set_agent_passphrase (client->crypto, password, strlen (password));
607 if (rc)
608 return rc;
609 #endif
612 rc = open_finalize (ctx, (char *)password, password ? strlen (password) : 0);
613 return rc;
616 static gpg_error_t
617 open_command (assuan_context_t ctx, char *line)
619 gpg_error_t rc;
620 struct client_s *client = assuan_get_pointer (ctx);
621 char **req, *filename;
622 unsigned char md5file[16];
623 int same_file = 0;
624 assuan_peercred_t peer;
625 struct argv_s *args[] = {
626 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
627 NULL
630 rc = parse_options (&line, args, client);
631 if (rc)
632 return send_error (ctx, rc);
634 req = str_split (line, " ", 2);
635 if (!req)
636 return send_error (ctx, GPG_ERR_SYNTAX);
638 rc = do_validate_peer (ctx, req[0], &peer);
639 if (rc)
641 strv_free (req);
642 return send_error (ctx, rc);
645 pthread_cleanup_push (req_cleanup, req);
646 filename = req[0];
647 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, filename, strlen (filename));
648 /* This client may have locked a different file with ISCACHED --lock than
649 * the current filename. This will remove that lock. */
650 same_file = !memcmp (md5file, client->md5file, 16) ? 1 : 0;
651 if (client->flags & FLAG_OPEN ||
652 (client->flags & FLAG_HAS_LOCK && !same_file))
654 typeof (client->opts) opts = client->opts;
655 typeof (client->flags) flags = client->flags;
657 if (same_file)
658 client->flags |= FLAG_KEEP_LOCK;
660 cleanup_client (client);
661 client->opts = opts;
662 client->flags |= flags;
663 client->flags &= ~(FLAG_LOCK_CMD);
664 if (!same_file)
665 client->flags &= ~(FLAG_HAS_LOCK | FLAG_NEW);
668 /* Need to lock the mutex here because file_modified() cannot without
669 * knowing the filename. */
670 memcpy (client->md5file, md5file, 16);
671 rc = lock_file_mutex (client, 1);
672 if (!rc)
674 char *password = req[1] && *req[1] ? req[1] : NULL;
676 #ifdef WITH_AGENT
677 if (IS_PKI (client->crypto))
678 rc = set_pinentry_mode (client->crypto->agent,
679 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
680 : "ask");
681 #endif
682 if (!rc)
684 rc = do_open (ctx, filename, password);
685 if (rc)
686 cleanup_client (client);
688 cleanup_crypto_stage1 (client->crypto);
692 pthread_cleanup_pop (1);
694 if (!rc && client->flags & FLAG_NEW)
695 rc = send_status (ctx, STATUS_NEWFILE, NULL);
697 #ifdef WITH_AGENT
698 (void) kill_scd (client->crypto->agent);
699 #endif
700 return send_error (ctx, rc);
703 static gpg_error_t
704 parse_opt_no_passphrase (void *data, void *value)
706 struct client_s *client = data;
708 (void) value;
709 client->opts |= OPT_NO_PASSPHRASE;
710 return 0;
713 static gpg_error_t
714 parse_save_opt_no_agent (void *data, void *value)
716 struct client_s *client = data;
718 client->opts |= OPT_NO_AGENT;
719 return 0;
722 static gpg_error_t
723 parse_save_opt_cipher (void *data, void *value)
725 struct client_s *client = data;
726 int algo = cipher_string_to_gcrypt ((char *) value);
727 file_header_t *hdr = &client->crypto->save.hdr;
729 if (algo == -1)
730 return GPG_ERR_INV_VALUE;
732 hdr->flags = set_cipher_flag (hdr->flags, algo);
733 return 0;
736 static gpg_error_t
737 parse_save_opt_keygrip (void *data, void *value)
739 struct client_s *client = data;
741 if (!IS_PKI (client->crypto))
742 return GPG_ERR_INV_ARG;
744 #ifdef WITH_AGENT
745 if (client->crypto->save.pkey)
746 gcry_sexp_release (client->crypto->save.pkey);
748 client->crypto->save.pkey = NULL;
749 return get_pubkey (client->crypto, value, &client->crypto->save.pkey);
750 #else
751 return GPG_ERR_INV_ARG;
752 #endif
755 static gpg_error_t
756 parse_save_opt_sign_keygrip (void *data, void *value)
758 struct client_s *client = data;
760 if (!IS_PKI (client->crypto))
761 return GPG_ERR_INV_ARG;
763 #ifdef WITH_AGENT
764 if (client->crypto->save.sigpkey)
765 gcry_sexp_release (client->crypto->save.sigpkey);
767 client->crypto->save.sigpkey = NULL;
768 return get_pubkey (client->crypto, value, &client->crypto->save.sigpkey);
769 #else
770 return GPG_ERR_INV_ARG;
771 #endif
774 static gpg_error_t
775 parse_opt_s2k_count (void *data, void *value)
777 struct client_s *client = data;
778 char *v = value;
780 if (!v || !*v)
781 return GPG_ERR_INV_VALUE;
783 client->crypto->save.s2k_count = strtoul (v, NULL, 10);
784 return 0;
787 static gpg_error_t
788 parse_save_opt_iterations (void *data, void *value)
790 struct client_s *client = data;
791 char *v = value, *p;
792 uint64_t n;
794 if (!v || !*v)
795 return GPG_ERR_INV_VALUE;
797 errno = 0;
798 n = strtoull (v, &p, 10);
799 if (n == UINT64_MAX && errno)
800 return gpg_error_from_errno (errno);
801 else if (p && *p)
802 return GPG_ERR_INV_VALUE;
804 client->crypto->save.hdr.iterations = n;
805 return 0;
808 static gpg_error_t
809 save_finalize (assuan_context_t ctx)
811 struct client_s *client = assuan_get_pointer (ctx);
812 gpg_error_t rc = 0;
813 xmlChar *xmlbuf = NULL;
814 int xmlbuflen;
815 void *key = NULL;
816 size_t keylen = 0;
818 xmlDocDumpFormatMemory (client->doc, &xmlbuf, &xmlbuflen, 0);
819 if (!xmlbuf)
820 return GPG_ERR_ENOMEM;
822 pthread_cleanup_push (xmlFree, xmlbuf);
824 if (!use_agent || ((client->flags & FLAG_NEW)
825 && (client->opts & OPT_NO_AGENT))
826 || !(client->crypto->hdr.flags & PWMD_FLAG_PKI))
828 rc = export_common (ctx, client->flags & FLAG_NO_PINENTRY,
829 client->crypto, xmlbuf, xmlbuflen, client->filename,
830 NULL, &key, &keylen, 1, 1,
831 (client->opts & OPT_NO_PASSPHRASE));
833 #ifdef WITH_AGENT
834 else
836 gcry_sexp_t pubkey = client->crypto->save.pkey;
837 gcry_sexp_t sigkey = client->crypto->save.sigpkey;
839 if (!pubkey)
840 pubkey = client->crypto->pkey_sexp;
842 if (!sigkey)
844 sigkey = client->crypto->sigpkey_sexp;
845 if (!sigkey)
847 gcry_sexp_build (&client->crypto->save.sigpkey, 0, "%S", pubkey);
848 sigkey = client->crypto->save.sigpkey;
852 rc = encrypt_data_file (client->ctx, client->crypto, pubkey, sigkey,
853 client->filename, xmlbuf, xmlbuflen);
854 if (pubkey == client->crypto->save.pkey)
856 if (!rc)
858 gcry_sexp_release (client->crypto->pkey_sexp);
859 client->crypto->pkey_sexp = client->crypto->save.pkey;
860 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
861 client->crypto->grip);
863 else
864 gcry_sexp_release (pubkey);
866 client->crypto->save.pkey = NULL;
869 if (!rc)
870 gcry_pk_get_keygrip (sigkey, client->crypto->sign_grip);
872 if (sigkey == client->crypto->save.sigpkey)
874 if (!rc)
876 if (client->crypto->sigpkey_sexp)
877 gcry_sexp_release (client->crypto->sigpkey_sexp);
879 client->crypto->sigpkey_sexp = client->crypto->save.sigpkey;
880 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
881 client->crypto->sign_grip);
883 else
884 gcry_sexp_release (sigkey);
886 client->crypto->save.sigpkey = NULL;
889 #endif
891 if (!rc)
893 int cached;
895 rc = save_common (client->filename, client->crypto, xmlbuf, xmlbuflen,
896 key, keylen, &cached, client->opts & OPT_NO_AGENT);
897 if (rc)
898 gcry_free (key);
900 if (!rc && (!cached || (client->flags & FLAG_NEW)))
901 send_status_all (STATUS_CACHE, NULL);
903 if (!rc)
905 rc = update_checksum (client);
906 client->flags &= ~(FLAG_NEW);
910 pthread_cleanup_pop (1); // xmlFree
911 return rc;
914 static gpg_error_t
915 parse_opt_reset (void *data, void *value)
917 struct client_s *client = data;
919 (void) value;
920 client->opts |= OPT_RESET;
921 return 0;
924 static gpg_error_t
925 save_command (assuan_context_t ctx, char *line)
927 struct client_s *client = assuan_get_pointer (ctx);
928 gpg_error_t rc;
929 struct stat st;
930 struct argv_s *args[] = {
931 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG,
932 parse_opt_no_passphrase},
933 &(struct argv_s) {"cipher", OPTION_TYPE_ARG, parse_save_opt_cipher},
934 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
935 parse_opt_inquire},
936 &(struct argv_s) {"keygrip", OPTION_TYPE_ARG, parse_save_opt_keygrip},
937 &(struct argv_s) {"sign-keygrip", OPTION_TYPE_ARG,
938 parse_save_opt_sign_keygrip},
939 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
940 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
941 &(struct argv_s) {"cipher-iterations", OPTION_TYPE_ARG,
942 parse_save_opt_iterations},
943 &(struct argv_s) {"no-agent", OPTION_TYPE_NOARG,
944 parse_save_opt_no_agent},
945 NULL
948 cleanup_save (&client->crypto->save);
949 memcpy (&client->crypto->save.hdr, &client->crypto->hdr,
950 sizeof (file_header_t));
951 client->crypto->save.s2k_count =
952 config_get_ulong (client->filename, "s2k_count");
954 if (client->flags & FLAG_NEW)
955 client->crypto->save.hdr.iterations =
956 config_get_ulonglong (client->filename, "cipher_iterations");
958 rc = parse_options (&line, args, client);
959 if (rc)
960 return send_error (ctx, rc);
962 if (!(client->flags & FLAG_NEW))
963 client->opts &= ~OPT_NO_AGENT;
964 else if (client->opts & OPT_NO_AGENT)
966 client->crypto->save.hdr.flags &= ~PWMD_FLAG_PKI;
967 client->crypto->hdr.flags &= ~PWMD_FLAG_PKI;
970 if ((client->opts & OPT_NO_PASSPHRASE) && !(client->flags & FLAG_NEW)
971 && !(client->opts & OPT_INQUIRE))
972 return send_error (ctx, GPG_ERR_WRONG_KEY_USAGE);
974 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
975 return send_error (ctx, gpg_error_from_errno (errno));
977 if (errno != ENOENT && !S_ISREG (st.st_mode))
979 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
980 return send_error (ctx, GPG_ERR_ENOANO);
983 int defer = 0;
984 rc = cache_iscached (client->filename, &defer);
985 if (gpg_err_code (rc) == GPG_ERR_ENOENT && client->flags & FLAG_NEW)
986 rc = 0;
987 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
988 rc = 0;
990 if ((!rc && defer) || config_get_boolean ("global", "require_save_key"))
991 client->opts |= OPT_RESET;
993 if (!rc && (client->opts & OPT_RESET))
995 rc = cache_clear (client->md5file);
996 if (rc)
997 return send_error (ctx, rc);
999 log_write ("%s: %s", client->filename,
1000 pwmd_strerror (GPG_ERR_KEY_EXPIRED));
1001 send_status_all (STATUS_CACHE, NULL);
1004 if (!rc)
1005 rc = update_element_mtime (xmlDocGetRootElement (client->doc));
1007 if (rc)
1008 return send_error (ctx, rc);
1010 #ifdef WITH_AGENT
1011 if (!rc && use_agent && !client->crypto->save.pkey &&
1012 (client->flags & FLAG_NEW || client->opts & OPT_INQUIRE)
1013 && !(client->opts & OPT_NO_AGENT))
1015 rc = set_pinentry_mode (client->crypto->agent,
1016 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
1017 : "ask");
1019 if (!(client->flags & FLAG_NEW))
1021 struct crypto_s *crypto;
1022 char *key = NULL;
1023 size_t keylen = 0;
1025 /* Wanting to generate a new key. Require the key to open the
1026 current file before proceeding reguardless of the
1027 require_save_key configuration parameter. */
1028 rc = cache_clear (client->md5file);
1029 if (!rc)
1031 rc = init_client_crypto (&crypto);
1032 if (!rc)
1033 crypto->client_ctx = client->ctx;
1036 if (!rc)
1037 rc = decrypt_common (client->ctx, client->opts&OPT_INQUIRE, crypto,
1038 client->filename, &key, &keylen);
1040 xfree (key);
1041 cleanup_crypto (&crypto);
1044 if (!rc)
1045 rc = send_status (client->ctx, STATUS_GENKEY, NULL);
1047 if (!rc)
1049 struct inquire_data_s idata = { 0 };
1050 char *params = client->opts & OPT_INQUIRE
1051 ? NULL : default_key_params (client->crypto);
1053 pthread_cleanup_push (xfree, params);
1054 idata.crypto = client->crypto;
1056 if (params)
1058 idata.line = params;
1059 idata.len = strlen (params);
1061 else
1062 idata.preset = 1;
1064 client->crypto->agent->inquire_data = &idata;
1065 client->crypto->agent->inquire_cb = NULL;
1066 rc = generate_key (client->crypto, params,
1067 (client->opts & OPT_NO_PASSPHRASE), 1);
1068 pthread_cleanup_pop (1);
1071 #endif
1073 if (!rc)
1074 rc = save_finalize (ctx);
1076 cleanup_crypto_stage1 (client->crypto);
1077 #ifdef WITH_AGENT
1078 (void) kill_scd (client->crypto->agent);
1079 #endif
1080 return send_error (ctx, rc);
1083 static gpg_error_t
1084 do_delete (assuan_context_t ctx, char *line)
1086 struct client_s *client = assuan_get_pointer (ctx);
1087 gpg_error_t rc;
1088 char **req;
1089 xmlNodePtr n;
1091 if (strchr (line, '\t'))
1092 req = str_split (line, "\t", 0);
1093 else
1094 req = str_split (line, " ", 0);
1096 if (!req || !*req)
1097 return GPG_ERR_SYNTAX;
1099 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1100 if (!n)
1102 strv_free (req);
1103 return rc;
1107 * No sub-node defined. Remove the entire node (root element).
1109 if (!req[1])
1111 if (n)
1113 rc = unlink_node (n);
1114 xmlFreeNode (n);
1117 strv_free (req);
1118 return rc;
1122 find_elements (client, client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1123 0, 0, NULL, 0);
1124 strv_free (req);
1125 if (!n)
1126 return rc;
1128 if (n)
1130 rc = unlink_node (n);
1131 xmlFreeNode (n);
1134 return rc;
1137 static gpg_error_t
1138 delete_command (assuan_context_t ctx, char *line)
1140 struct client_s *client = assuan_get_pointer (ctx);
1141 gpg_error_t rc;
1142 struct argv_s *args[] = {
1143 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1144 NULL
1147 rc = parse_options (&line, args, client);
1148 if (rc)
1149 return send_error (ctx, rc);
1151 if (client->opts & OPT_INQUIRE)
1153 unsigned char *result;
1154 size_t len;
1156 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1157 if (rc)
1158 return send_error (ctx, rc);
1160 line = (char *) result;
1163 rc = do_delete (ctx, line);
1165 if (client->opts & OPT_INQUIRE)
1166 xfree (line);
1168 return send_error (ctx, rc);
1171 static gpg_error_t
1172 store_command (assuan_context_t ctx, char *line)
1174 struct client_s *client = assuan_get_pointer (ctx);
1175 gpg_error_t rc;
1176 size_t len;
1177 unsigned char *result;
1178 char **req;
1179 xmlNodePtr n, parent;
1180 int has_content;
1181 char *content = NULL;
1183 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1184 if (rc)
1185 return send_error (ctx, rc);
1187 req = str_split ((char *) result, "\t", 0);
1188 xfree (result);
1190 if (!req || !*req)
1191 return send_error (ctx, GPG_ERR_SYNTAX);
1193 len = strv_length (req);
1194 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1195 if (*(req + 1) && !valid_element_path (req, has_content))
1197 strv_free (req);
1198 return send_error (ctx, GPG_ERR_INV_VALUE);
1201 if (has_content || !*req[len - 1])
1203 has_content = 1;
1204 content = req[len - 1];
1205 req[len - 1] = NULL;
1208 again:
1209 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1210 if (rc && rc == GPG_ERR_ELEMENT_NOT_FOUND)
1212 rc = new_root_element (client, client->doc, *req);
1213 if (rc)
1215 strv_free (req);
1216 return send_error (ctx, rc);
1219 goto again;
1222 if (!n)
1224 strv_free (req);
1225 return send_error (ctx, rc);
1228 parent = n;
1230 if (req[1] && *req[1])
1232 if (!n->children)
1233 parent = create_elements_cb (client, n, req + 1, &rc, NULL);
1234 else
1235 parent = find_elements (client, client->doc, n->children, req + 1, &rc,
1236 NULL, NULL, create_elements_cb, 0, 0, NULL,
1240 if (!rc && len > 1)
1242 n = find_text_node (parent->children);
1243 if (n)
1244 xmlNodeSetContent (n, (xmlChar *) content);
1245 else
1246 xmlNodeAddContent (parent, (xmlChar *) content);
1248 update_element_mtime (parent);
1251 xfree (content);
1252 strv_free (req);
1253 return send_error (ctx, rc);
1256 static gpg_error_t
1257 xfer_data (assuan_context_t ctx, const char *line, int total)
1259 int to_send;
1260 int sent = 0;
1261 gpg_error_t rc;
1262 int progress = config_get_integer ("global", "xfer_progress");
1263 int flush = 0;
1265 progress =
1266 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1267 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1268 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1270 if (rc)
1271 return rc;
1273 again:
1276 if (sent + to_send > total)
1277 to_send = total - sent;
1279 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1280 flush ? 0 : to_send);
1281 if (!rc)
1283 sent += flush ? 0 : to_send;
1285 if ((progress && !(sent % progress) && sent != total) ||
1286 (sent == total && flush))
1287 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1289 if (!flush && !rc && sent == total)
1291 flush = 1;
1292 goto again;
1296 while (!rc && sent < total);
1298 return rc;
1301 static gpg_error_t
1302 do_get (assuan_context_t ctx, char *line)
1304 struct client_s *client = assuan_get_pointer (ctx);
1305 gpg_error_t rc;
1306 char **req;
1307 xmlNodePtr n;
1309 req = str_split (line, "\t", 0);
1311 if (!req || !*req)
1313 strv_free (req);
1314 return GPG_ERR_SYNTAX;
1317 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1318 if (!n)
1320 strv_free (req);
1321 return rc;
1324 if (req[1])
1326 find_elements (client, client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1327 0, 0, NULL, 0);
1329 strv_free (req);
1330 if (rc)
1331 return rc;
1333 if (!n || !n->children)
1334 return GPG_ERR_NO_DATA;
1336 n = find_text_node (n->children);
1337 if (!n || !n->content || !*n->content)
1338 return GPG_ERR_NO_DATA;
1340 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1341 return rc;
1344 static gpg_error_t
1345 get_command (assuan_context_t ctx, char *line)
1347 struct client_s *client = assuan_get_pointer (ctx);
1348 gpg_error_t rc;
1349 struct argv_s *args[] = {
1350 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1351 NULL
1354 rc = parse_options (&line, args, client);
1355 if (rc)
1356 return send_error (ctx, rc);
1358 if (client->opts & OPT_INQUIRE)
1360 unsigned char *result;
1361 size_t len;
1363 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1364 if (rc)
1365 return send_error (ctx, rc);
1367 line = (char *) result;
1370 rc = do_get (ctx, line);
1372 if (client->opts & OPT_INQUIRE)
1373 xfree (line);
1375 return send_error (ctx, rc);
1378 static void list_command_cleanup1 (void *arg);
1379 static gpg_error_t
1380 realpath_command (assuan_context_t ctx, char *line)
1382 struct string_s *string = NULL;
1383 gpg_error_t rc;
1384 struct client_s *client = assuan_get_pointer (ctx);
1385 struct argv_s *args[] = {
1386 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1387 NULL
1390 rc = parse_options (&line, args, client);
1391 if (rc)
1392 return send_error (ctx, rc);
1394 if (client->opts & OPT_INQUIRE)
1396 unsigned char *result;
1397 size_t len;
1399 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1400 if (rc)
1401 return send_error (ctx, rc);
1403 line = (char *) result;
1406 rc = build_realpath (client, client->doc, line, &string);
1407 if (!rc)
1409 pthread_cleanup_push (list_command_cleanup1, string);
1410 rc = xfer_data (ctx, string->str, string->len);
1411 pthread_cleanup_pop (1);
1414 if (client->opts & OPT_INQUIRE)
1415 xfree (line);
1417 return send_error (ctx, rc);
1420 static void
1421 list_command_cleanup1 (void *arg)
1423 if (arg)
1424 string_free ((struct string_s *) arg, 1);
1427 static void
1428 list_command_cleanup2 (void *arg)
1430 struct element_list_s *elements = arg;
1432 if (elements)
1434 if (elements->list)
1436 int total = slist_length (elements->list);
1437 int i;
1439 for (i = 0; i < total; i++)
1441 char *tmp = slist_nth_data (elements->list, i);
1442 xfree (tmp);
1445 slist_free (elements->list);
1448 if (elements->prefix)
1449 xfree (elements->prefix);
1451 if (elements->req)
1452 strv_free (elements->req);
1454 xfree (elements);
1458 static gpg_error_t
1459 parse_list_opt_norecurse (void *data, void *value)
1461 struct client_s *client = data;
1463 client->opts &= ~(OPT_LIST_RECURSE);
1464 return 0;
1467 static gpg_error_t
1468 parse_list_opt_verbose (void *data, void *value)
1470 struct client_s *client = data;
1472 client->opts |= OPT_LIST_VERBOSE;
1473 return 0;
1476 static gpg_error_t
1477 parse_list_opt_target (void *data, void *value)
1479 struct client_s *client = data;
1481 client->opts |= OPT_LIST_WITH_TARGET;
1482 return 0;
1485 static gpg_error_t
1486 parse_list_opt_all (void *data, void *value)
1488 struct client_s *client = data;
1490 client->opts |= OPT_LIST_ALL;
1491 return 0;
1494 static gpg_error_t
1495 list_path_once (struct client_s *client, char *line,
1496 struct element_list_s *elements, struct string_s *result)
1498 gpg_error_t rc;
1500 elements->req = str_split (line, " ", 0);
1501 if (!elements->req)
1502 strv_printf (&elements->req, "%s", line);
1504 rc = create_path_list (client, client->doc, elements, *elements->req);
1505 if ((rc == GPG_ERR_ELOOP || rc == GPG_ERR_EPERM) && elements->verbose)
1506 rc = 0;
1508 if (!rc)
1510 int total = slist_length (elements->list);
1512 if (!total)
1513 rc = GPG_ERR_NO_DATA;
1515 if (!rc)
1517 if (!rc)
1519 int i;
1521 for (i = 0; i < total; i++)
1523 char *tmp = slist_nth_data (elements->list, i);
1525 string_append_printf (result, "%s%s", tmp,
1526 i + 1 == total ? "" : "\n");
1530 else
1531 rc = GPG_ERR_NO_DATA;
1534 return rc;
1537 static int
1538 has_list_flag (char *path, char *flags)
1540 char *p = path;
1542 while (*p && *++p != ' ');
1544 if (!*p)
1545 return 0;
1547 for (; *p; p++)
1549 char *f;
1551 for (f = flags; *f && *f != ' '; f++)
1553 if (*p == *f)
1554 return 1;
1558 return 0;
1561 static gpg_error_t
1562 do_list (assuan_context_t ctx, char *line)
1564 struct client_s *client = assuan_get_pointer (ctx);
1565 gpg_error_t rc;
1566 struct element_list_s *elements = NULL;
1568 elements = xcalloc (1, sizeof (struct element_list_s));
1569 if (!elements)
1570 return GPG_ERR_ENOMEM;
1572 elements->recurse = client->opts & OPT_LIST_RECURSE;
1573 elements->verbose =
1574 (client->opts & OPT_LIST_VERBOSE) | (client->opts & OPT_LIST_WITH_TARGET);
1575 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1577 if (!line || !*line)
1579 struct string_s *str = NULL;
1581 pthread_cleanup_push (list_command_cleanup2, elements);
1582 rc = list_root_elements (client, client->doc, &str, elements->verbose,
1583 elements->with_target);
1584 pthread_cleanup_pop (1);
1585 pthread_cleanup_push (list_command_cleanup1, str);
1587 if (!rc)
1589 if (client->opts & OPT_LIST_ALL)
1591 char **roots = str_split (str->str, "\n", 0);
1592 char **p;
1594 pthread_cleanup_push (req_cleanup, roots);
1595 string_truncate (str, 0);
1597 for (p = roots; *p; p++)
1599 if (strchr (*p, ' '))
1601 if (has_list_flag (*p, "EOP"))
1603 string_append_printf (str, "%s%s", *p,
1604 *(p + 1) ? "\n" : "");
1605 continue;
1609 elements = xcalloc (1, sizeof (struct element_list_s));
1610 if (!elements)
1612 rc = GPG_ERR_ENOMEM;
1613 break;
1616 elements->recurse = client->opts & OPT_LIST_RECURSE;
1617 elements->verbose =
1618 (client->opts & OPT_LIST_VERBOSE) | (client->opts &
1619 OPT_LIST_WITH_TARGET);
1620 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1621 pthread_cleanup_push (list_command_cleanup2, elements);
1622 rc = list_path_once (client, *p, elements, str);
1623 pthread_cleanup_pop (1);
1624 if (rc)
1625 break;
1627 if (*(p + 1))
1628 string_append (str, "\n");
1631 pthread_cleanup_pop (1);
1634 if (!rc)
1635 rc = xfer_data (ctx, str->str, str->len);
1638 pthread_cleanup_pop (1);
1639 return rc;
1642 pthread_cleanup_push (list_command_cleanup2, elements);
1643 struct string_s *str = string_new (NULL);
1644 pthread_cleanup_push (list_command_cleanup1, str);
1645 rc = list_path_once (client, line, elements, str);
1646 if (!rc)
1647 rc = xfer_data (ctx, str->str, str->len);
1649 pthread_cleanup_pop (1);
1650 pthread_cleanup_pop (1);
1651 return rc;
1654 static gpg_error_t
1655 list_command (assuan_context_t ctx, char *line)
1657 struct client_s *client = assuan_get_pointer (ctx);
1658 gpg_error_t rc;
1659 struct argv_s *args[] = {
1660 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG,
1661 parse_list_opt_norecurse},
1662 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_list_opt_verbose},
1663 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1664 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG,
1665 parse_list_opt_target},
1666 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_all},
1667 NULL
1670 if (disable_list_and_dump == 1)
1671 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1673 client->opts |= OPT_LIST_RECURSE;
1674 rc = parse_options (&line, args, client);
1675 if (rc)
1676 return send_error (ctx, rc);
1678 if (client->opts & OPT_LIST_ALL)
1679 client->opts |= OPT_LIST_RECURSE | OPT_LIST_VERBOSE;
1681 if (client->opts & OPT_INQUIRE)
1683 unsigned char *result;
1684 size_t len;
1686 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1687 if (rc)
1688 return send_error (ctx, rc);
1690 line = (char *) result;
1693 rc = do_list (ctx, line);
1695 if (client->opts & OPT_INQUIRE)
1696 xfree (line);
1698 return send_error (ctx, rc);
1702 * req[0] - element path
1704 static gpg_error_t
1705 attribute_list (assuan_context_t ctx, char **req)
1707 struct client_s *client = assuan_get_pointer (ctx);
1708 char **attrlist = NULL;
1709 int i = 0;
1710 char **path = NULL;
1711 xmlAttrPtr a;
1712 xmlNodePtr n, an;
1713 char *line;
1714 gpg_error_t rc;
1716 if (!req || !req[0])
1717 return GPG_ERR_SYNTAX;
1719 if ((path = str_split (req[0], "\t", 0)) == NULL)
1722 * The first argument may be only a root element.
1724 if ((path = str_split (req[0], " ", 0)) == NULL)
1725 return GPG_ERR_SYNTAX;
1728 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1730 if (!n)
1732 strv_free (path);
1733 return rc;
1736 if (path[1])
1738 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1739 NULL, NULL, NULL, 0, 0, NULL, 0);
1741 if (!n)
1743 strv_free (path);
1744 return rc;
1748 strv_free (path);
1750 for (a = n->properties; a; a = a->next)
1752 char **pa;
1754 if ((pa = xrealloc (attrlist, (i + 2) * sizeof (char *))) == NULL)
1756 if (attrlist)
1757 strv_free (attrlist);
1759 log_write ("%s(%i): %s", __FILE__, __LINE__,
1760 pwmd_strerror (GPG_ERR_ENOMEM));
1761 return GPG_ERR_ENOMEM;
1764 attrlist = pa;
1765 an = a->children;
1766 attrlist[i] = str_asprintf ("%s %s", (char *) a->name,
1768 && an->content ? (char *) an->content : "");
1770 if (!attrlist[i])
1772 strv_free (attrlist);
1773 log_write ("%s(%i): %s", __FILE__, __LINE__,
1774 pwmd_strerror (GPG_ERR_ENOMEM));
1775 return GPG_ERR_ENOMEM;
1778 attrlist[++i] = NULL;
1781 if (!attrlist)
1782 return GPG_ERR_NO_DATA;
1784 line = strv_join ("\n", attrlist);
1786 if (!line)
1788 log_write ("%s(%i): %s", __FILE__, __LINE__,
1789 pwmd_strerror (GPG_ERR_ENOMEM));
1790 strv_free (attrlist);
1791 return GPG_ERR_ENOMEM;
1794 pthread_cleanup_push (xfree, line);
1795 pthread_cleanup_push (req_cleanup, attrlist);
1796 rc = xfer_data (ctx, line, strlen (line));
1797 pthread_cleanup_pop (1);
1798 pthread_cleanup_pop (1);
1799 return rc;
1803 * req[0] - attribute
1804 * req[1] - element path
1806 static gpg_error_t
1807 attribute_delete (struct client_s *client, char **req)
1809 xmlNodePtr n;
1810 char **path = NULL;
1811 gpg_error_t rc;
1813 if (!req || !req[0] || !req[1])
1814 return GPG_ERR_SYNTAX;
1816 if (!strcmp (req[0], "_name"))
1817 return GPG_ERR_INV_ATTR;
1819 if ((path = str_split (req[1], "\t", 0)) == NULL)
1822 * The first argument may be only a root element.
1824 if ((path = str_split (req[1], " ", 0)) == NULL)
1825 return GPG_ERR_SYNTAX;
1828 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1829 if (!n)
1830 goto fail;
1832 if (path[1])
1834 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1835 NULL, NULL, NULL, 0, 0, NULL, 0);
1836 if (!n)
1837 goto fail;
1840 if (!strcmp (req[0], (char *) "_acl"))
1842 rc = peer_is_self (client);
1843 if (rc == GPG_ERR_EPERM)
1844 rc = is_element_owner (client, n);
1846 if (rc)
1847 goto fail;
1850 rc = delete_attribute (client, n, (xmlChar *) req[0]);
1852 fail:
1853 strv_free (path);
1854 return rc;
1857 static xmlNodePtr
1858 create_element_path (struct client_s *client,
1859 char ***elements, gpg_error_t * rc, xmlNodePtr parent)
1861 char **req = *elements;
1862 char **req_orig = strv_dup (req);
1863 xmlNodePtr n = NULL;
1865 *rc = 0;
1867 if (!req_orig)
1869 *rc = GPG_ERR_ENOMEM;
1870 log_write ("%s(%i): %s", __FILE__, __LINE__,
1871 pwmd_strerror (GPG_ERR_ENOMEM));
1872 goto fail;
1875 again:
1876 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
1877 if (!n)
1879 if (*rc != GPG_ERR_ELEMENT_NOT_FOUND)
1880 goto fail;
1882 *rc = new_root_element (client, client->doc, req[0]);
1883 if (*rc)
1884 goto fail;
1886 goto again;
1888 else if (n == parent)
1890 *rc = GPG_ERR_CONFLICT;
1891 goto fail;
1894 if (req[1])
1896 if (!n->children)
1897 n = create_target_elements_cb (client, n, req + 1, rc, NULL);
1898 else
1899 n = find_elements (client, client->doc, n->children, req + 1, rc,
1900 NULL, NULL, create_target_elements_cb, 0, 0,
1901 parent, 0);
1903 if (!n)
1904 goto fail;
1907 * Reset the position of the element tree now that the elements
1908 * have been created.
1910 strv_free (req);
1911 req = req_orig;
1912 req_orig = NULL;
1913 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
1914 if (!n)
1915 goto fail;
1917 n = find_elements (client, client->doc, n->children, req + 1, rc,
1918 NULL, NULL, NULL, 0, 0, NULL, 0);
1919 if (!n)
1920 goto fail;
1923 fail:
1924 if (req_orig)
1925 strv_free (req_orig);
1927 *elements = req;
1928 return n;
1932 * Creates a "target" attribute. When other commands encounter an element with
1933 * this attribute, the element path is modified to the target value. If the
1934 * source element path doesn't exist when using 'ATTR SET target', it is
1935 * created, but the destination element path must exist.
1937 * req[0] - source element path
1938 * req[1] - destination element path
1940 static gpg_error_t
1941 target_attribute (struct client_s *client, char **req)
1943 char **src, **dst, *line = NULL, **odst = NULL;
1944 gpg_error_t rc;
1945 xmlNodePtr n;
1947 if (!req || !req[0] || !req[1])
1948 return GPG_ERR_SYNTAX;
1950 if ((src = str_split (req[0], "\t", 0)) == NULL)
1953 * The first argument may be only a root element.
1955 if ((src = str_split (req[0], " ", 0)) == NULL)
1956 return GPG_ERR_SYNTAX;
1959 if (!valid_element_path (src, 0))
1960 return GPG_ERR_INV_VALUE;
1962 if ((dst = str_split (req[1], "\t", 0)) == NULL)
1965 * The first argument may be only a root element.
1967 if ((dst = str_split (req[1], " ", 0)) == NULL)
1969 rc = GPG_ERR_SYNTAX;
1970 goto fail;
1974 odst = strv_dup (dst);
1975 if (!odst)
1977 rc = GPG_ERR_ENOMEM;
1978 goto fail;
1981 n = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
1983 * Make sure the destination element path exists.
1985 if (!n)
1986 goto fail;
1988 if (dst[1])
1990 n = find_elements (client, client->doc, n->children, dst + 1, &rc,
1991 NULL, NULL, NULL, 0, 0, NULL, 0);
1992 if (!n)
1993 goto fail;
1996 rc = validate_target_attribute (client, client->doc, req[0], n);
1997 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
1998 goto fail;
2000 n = create_element_path (client, &src, &rc, NULL);
2001 if (rc)
2002 goto fail;
2004 line = strv_join ("\t", odst);
2005 if (!line)
2007 rc = GPG_ERR_ENOMEM;
2008 goto fail;
2011 rc = add_attribute (n, "target", line);
2013 fail:
2014 xfree (line);
2015 strv_free (src);
2016 strv_free (dst);
2017 strv_free (odst);
2018 return rc;
2022 * req[0] - attribute
2023 * req[1] - element path
2025 static gpg_error_t
2026 attribute_get (assuan_context_t ctx, char **req)
2028 struct client_s *client = assuan_get_pointer (ctx);
2029 xmlNodePtr n;
2030 xmlChar *a;
2031 char **path = NULL;
2032 gpg_error_t rc;
2034 if (!req || !req[0] || !req[1])
2035 return GPG_ERR_SYNTAX;
2037 if (strchr (req[1], '\t'))
2039 if ((path = str_split (req[1], "\t", 0)) == NULL)
2040 return GPG_ERR_SYNTAX;
2042 else
2044 if ((path = str_split (req[1], " ", 0)) == NULL)
2045 return GPG_ERR_SYNTAX;
2048 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2050 if (!n)
2051 goto fail;
2053 if (path[1])
2055 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2056 NULL, NULL, NULL, 0, 0, NULL, 0);
2058 if (!n)
2059 goto fail;
2062 strv_free (path);
2064 if ((a = xmlGetProp (n, (xmlChar *) req[0])) == NULL)
2065 return GPG_ERR_NOT_FOUND;
2067 pthread_cleanup_push (xmlFree, a);
2069 if (*a)
2070 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
2071 else
2072 rc = GPG_ERR_NO_DATA;
2074 pthread_cleanup_pop (1);
2075 return rc;
2077 fail:
2078 strv_free (path);
2079 return rc;
2083 * req[0] - attribute
2084 * req[1] - element path
2085 * req[2] - value
2087 static gpg_error_t
2088 attribute_set (struct client_s *client, char **req)
2090 char **path = NULL;
2091 gpg_error_t rc;
2092 xmlNodePtr n;
2094 if (!req || !req[0] || !req[1])
2095 return GPG_ERR_SYNTAX;
2098 * Reserved attribute names.
2100 if (!strcmp (req[0], "_name"))
2101 return GPG_ERR_INV_ATTR;
2102 else if (!strcmp (req[0], "target"))
2103 return target_attribute (client, req + 1);
2104 else if (!valid_xml_attribute (req[0]))
2105 return GPG_ERR_INV_VALUE;
2107 if ((path = str_split (req[1], "\t", 0)) == NULL)
2110 * The first argument may be only a root element.
2112 if ((path = str_split (req[1], " ", 0)) == NULL)
2113 return GPG_ERR_SYNTAX;
2116 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2118 if (!n)
2119 goto fail;
2121 if (path[1])
2123 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2124 NULL, NULL, NULL, 0, 0, NULL, 0);
2126 if (!n)
2127 goto fail;
2130 if (!strcmp (req[0], (char *) "_acl"))
2132 rc = peer_is_self (client);
2133 if (rc == GPG_ERR_EPERM)
2134 rc = is_element_owner (client, n);
2136 if (rc)
2137 goto fail;
2140 rc = add_attribute (n, req[0], req[2]);
2142 fail:
2143 strv_free (path);
2144 return rc;
2148 * req[0] - command
2149 * req[1] - attribute name or element path if command is LIST
2150 * req[2] - element path
2151 * req[2] - element path or value
2154 static gpg_error_t
2155 do_attr (assuan_context_t ctx, char *line)
2157 struct client_s *client = assuan_get_pointer (ctx);
2158 gpg_error_t rc = 0;
2159 char **req;
2161 req = str_split (line, " ", 4);
2162 if (!req || !req[0] || !req[1])
2164 strv_free (req);
2165 return GPG_ERR_SYNTAX;
2168 pthread_cleanup_push (req_cleanup, req);
2170 if (strcasecmp (req[0], "SET") == 0)
2171 rc = attribute_set (client, req + 1);
2172 else if (strcasecmp (req[0], "GET") == 0)
2173 rc = attribute_get (ctx, req + 1);
2174 else if (strcasecmp (req[0], "DELETE") == 0)
2175 rc = attribute_delete (client, req + 1);
2176 else if (strcasecmp (req[0], "LIST") == 0)
2177 rc = attribute_list (ctx, req + 1);
2178 else
2179 rc = GPG_ERR_SYNTAX;
2181 pthread_cleanup_pop (1);
2182 return rc;
2185 static gpg_error_t
2186 attr_command (assuan_context_t ctx, char *line)
2188 struct client_s *client = assuan_get_pointer (ctx);
2189 gpg_error_t rc;
2190 struct argv_s *args[] = {
2191 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2192 NULL
2195 rc = parse_options (&line, args, client);
2196 if (rc)
2197 return send_error (ctx, rc);
2199 if (client->opts & OPT_INQUIRE)
2201 unsigned char *result;
2202 size_t len;
2204 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2205 if (rc)
2206 return send_error (ctx, rc);
2208 line = (char *) result;
2211 rc = do_attr (ctx, line);
2213 if (client->opts & OPT_INQUIRE)
2214 xfree (line);
2216 return send_error (ctx, rc);
2219 static gpg_error_t
2220 parse_iscached_opt_lock (void *data, void *value)
2222 struct client_s *client = data;
2224 (void) value;
2225 client->opts |= OPT_LOCK;
2226 return 0;
2229 static gpg_error_t
2230 iscached_command (assuan_context_t ctx, char *line)
2232 struct client_s *client = assuan_get_pointer (ctx);
2233 gpg_error_t rc;
2234 struct argv_s *args[] = {
2235 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2236 NULL
2239 if (!line || !*line)
2240 return send_error (ctx, GPG_ERR_SYNTAX);
2242 rc = parse_options (&line, args, client);
2243 if (rc)
2244 return send_error (ctx, rc);
2245 else if (!valid_filename (line))
2246 return send_error (ctx, GPG_ERR_INV_VALUE);
2248 rc = cache_iscached (line, NULL);
2249 if (client->opts & OPT_LOCK
2250 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2252 unsigned char md5file[16];
2253 gpg_error_t trc = rc;
2255 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2256 if (memcmp (md5file, client->md5file, 16))
2257 cleanup_client (client);
2259 memcpy (client->md5file, md5file, 16);
2260 rc = do_lock (client, 1);
2261 if (!rc)
2262 rc = trc;
2265 return send_error (ctx, rc);
2268 static gpg_error_t
2269 clearcache_command (assuan_context_t ctx, char *line)
2271 gpg_error_t rc = 0, all_rc = 0;
2272 unsigned char md5file[16];
2273 int i;
2274 int t;
2275 int all = 0;
2276 struct client_thread_s *once = NULL;
2278 cache_lock ();
2279 MUTEX_LOCK (&cn_mutex);
2280 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
2282 if (!line || !*line)
2283 all = 1;
2285 t = slist_length (cn_thread_list);
2287 for (i = 0; i < t; i++)
2289 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2290 assuan_peercred_t peer;
2292 if (!thd->cl)
2293 continue;
2295 /* Lock each connected clients' file mutex to prevent any other client
2296 * from accessing the cache entry (the file mutex is locked upon
2297 * command startup). The cache for the entry is not cleared if the
2298 * file mutex is locked by another client to prevent this function
2299 * from blocking.
2301 if (all)
2303 if (thd->cl->filename)
2305 rc = do_validate_peer (ctx, thd->cl->filename, &peer);
2306 all_rc = !all_rc ? rc : all_rc;
2307 if (rc)
2309 rc = 0;
2310 continue;
2314 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
2315 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2317 if (pthread_equal (pthread_self (), thd->tid))
2318 rc = 0;
2319 else
2321 if (!thd->cl->filename ||
2322 cache_iscached (thd->cl->filename,
2323 NULL) == GPG_ERR_NO_DATA)
2325 rc = 0;
2326 continue;
2329 cache_defer_clear (thd->cl->md5file);
2332 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2334 rc = 0;
2335 continue;
2338 if (!rc)
2340 rc = cache_clear (thd->cl->md5file);
2341 cache_unlock_mutex (thd->cl->md5file, 0);
2344 if (rc)
2345 all_rc = rc;
2347 rc = 0;
2349 /* A single data filename was specified. Lock only this data file
2350 * mutex and free the cache entry. */
2351 else
2353 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2354 rc = do_validate_peer (ctx, line, &peer);
2356 if (!rc && !memcmp (thd->cl->md5file, md5file, sizeof (md5file)))
2358 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0,
2359 -1);
2360 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2362 if (pthread_equal (pthread_self (), thd->tid))
2363 rc = 0;
2366 if (!rc)
2368 once = thd;
2369 rc = cache_clear (thd->cl->md5file);
2370 cache_unlock_mutex (thd->cl->md5file, 0);
2372 else
2374 cache_defer_clear (thd->cl->md5file);
2377 break;
2382 /* Only connected clients' cache entries have been cleared. Now clear any
2383 * remaining cache entries without clients but only if there wasn't an
2384 * error from above since this would defeat the locking check of the
2385 * remaining entries. */
2386 if (!all_rc && all)
2388 cache_clear (NULL);
2391 /* No clients are using the specified file. */
2392 else if (!all_rc && !rc && !once)
2394 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2395 rc = cache_clear (md5file);
2398 /* Release the connection mutex. */
2399 pthread_cleanup_pop (1);
2400 cache_unlock ();
2402 if (!rc)
2403 send_status_all (STATUS_CACHE, NULL);
2405 /* One or more files were locked while clearing all cache entries. */
2406 if (all_rc)
2407 rc = all_rc;
2409 return send_error (ctx, rc);
2412 static gpg_error_t
2413 cachetimeout_command (assuan_context_t ctx, char *line)
2415 int timeout;
2416 char **req = str_split (line, " ", 0);
2417 char *p;
2418 gpg_error_t rc = 0;
2419 assuan_peercred_t peer;
2421 if (!req || !*req || !req[1])
2423 strv_free (req);
2424 return send_error (ctx, GPG_ERR_SYNTAX);
2427 errno = 0;
2428 timeout = (int) strtol (req[1], &p, 10);
2429 if (errno != 0 || *p || timeout < -1)
2431 strv_free (req);
2432 return send_error (ctx, GPG_ERR_SYNTAX);
2435 rc = do_validate_peer (ctx, req[0], &peer);
2436 if (!rc)
2438 unsigned char md5file[16];
2440 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, req[0], strlen (req[0]));
2441 rc = cache_set_timeout (md5file, timeout);
2442 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2444 rc = 0;
2445 MUTEX_LOCK (&rcfile_mutex);
2446 config_set_int_param (&global_config, req[0], "cache_timeout",
2447 req[1]);
2448 MUTEX_UNLOCK (&rcfile_mutex);
2452 strv_free (req);
2453 return send_error (ctx, rc);
2456 static gpg_error_t
2457 dump_command (assuan_context_t ctx, char *line)
2459 xmlChar *xml;
2460 int len;
2461 struct client_s *client = assuan_get_pointer (ctx);
2462 gpg_error_t rc;
2464 if (disable_list_and_dump == 1)
2465 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2467 rc = peer_is_self(client);
2468 if (rc)
2469 return send_error (ctx, rc);
2471 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2473 if (!xml)
2475 log_write ("%s(%i): %s", __FILE__, __LINE__,
2476 pwmd_strerror (GPG_ERR_ENOMEM));
2477 return send_error (ctx, GPG_ERR_ENOMEM);
2480 pthread_cleanup_push (xmlFree, xml);
2481 rc = xfer_data (ctx, (char *) xml, len);
2482 pthread_cleanup_pop (1);
2483 return send_error (ctx, rc);
2486 static gpg_error_t
2487 getconfig_command (assuan_context_t ctx, char *line)
2489 struct client_s *client = assuan_get_pointer (ctx);
2490 gpg_error_t rc = 0;
2491 char filename[255] = { 0 }, param[747] = { 0 };
2492 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2494 if (!line || !*line)
2495 return send_error (ctx, GPG_ERR_SYNTAX);
2497 if (strchr (line, ' '))
2499 sscanf (line, " %254[^ ] %746c", filename, param);
2500 paramp = param;
2501 fp = filename;
2504 if (fp && !valid_filename (fp))
2505 return send_error (ctx, GPG_ERR_INV_VALUE);
2507 paramp = str_down (paramp);
2508 if (!strcmp (paramp, "cipher") && fp)
2510 struct crypto_s *crypto = NULL;
2512 rc = init_client_crypto (&crypto);
2513 if (!rc)
2515 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2516 if (!rc)
2518 const char *t =
2519 gcry_cipher_algo_name (cipher_to_gcrypt (crypto->hdr.flags));
2520 if (t)
2522 tmp = str_dup (t);
2523 if (tmp)
2524 str_down (tmp);
2529 UPDATE_AGENT_CTX (client, crypto);
2530 cleanup_crypto (&crypto);
2531 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2532 return send_error (ctx, rc);
2534 if (!rc && tmp)
2535 goto done;
2537 else if (!strcmp (paramp, "cipher_iterations") && fp)
2539 struct crypto_s *crypto = NULL;
2541 rc = init_client_crypto (&crypto);
2542 if (!rc)
2544 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2545 if (!rc)
2547 tmp = str_asprintf ("%llu",
2548 (unsigned long long) crypto->hdr.
2549 iterations);
2550 if (!tmp)
2551 rc = GPG_ERR_ENOMEM;
2555 UPDATE_AGENT_CTX (client, crypto);
2556 cleanup_crypto (&crypto);
2557 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2558 return send_error (ctx, rc);
2560 if (!rc && tmp)
2561 goto done;
2563 else if (!strcmp (paramp, "passphrase"))
2564 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2566 p = config_get_value (fp ? fp : "global", paramp);
2567 if (!p)
2568 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2570 tmp = expand_homedir (p);
2571 xfree (p);
2572 if (!tmp)
2574 log_write ("%s(%i): %s", __FILE__, __LINE__,
2575 pwmd_strerror (GPG_ERR_ENOMEM));
2576 return send_error (ctx, GPG_ERR_ENOMEM);
2579 done:
2580 p = tmp;
2581 pthread_cleanup_push (xfree, p);
2582 rc = xfer_data (ctx, p, strlen (p));
2583 pthread_cleanup_pop (1);
2584 return send_error (ctx, rc);
2587 struct xpath_s
2589 xmlXPathContextPtr xp;
2590 xmlXPathObjectPtr result;
2591 xmlBufferPtr buf;
2592 char **req;
2595 static void
2596 xpath_command_cleanup (void *arg)
2598 struct xpath_s *xpath = arg;
2600 if (!xpath)
2601 return;
2603 req_cleanup (xpath->req);
2605 if (xpath->buf)
2606 xmlBufferFree (xpath->buf);
2608 if (xpath->result)
2609 xmlXPathFreeObject (xpath->result);
2611 if (xpath->xp)
2612 xmlXPathFreeContext (xpath->xp);
2615 static gpg_error_t
2616 do_xpath (assuan_context_t ctx, char *line)
2618 gpg_error_t rc;
2619 struct client_s *client = assuan_get_pointer (ctx);
2620 struct xpath_s _x = { 0 };
2621 struct xpath_s *xpath = &_x;
2623 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2625 if (strv_printf (&xpath->req, "%s", line) == 0)
2626 return GPG_ERR_ENOMEM;
2629 xpath->xp = xmlXPathNewContext (client->doc);
2630 if (!xpath->xp)
2632 rc = GPG_ERR_BAD_DATA;
2633 goto fail;
2636 xpath->result =
2637 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2638 if (!xpath->result)
2640 rc = GPG_ERR_BAD_DATA;
2641 goto fail;
2644 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2646 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2647 goto fail;
2650 rc = recurse_xpath_nodeset (client, client->doc, xpath->result->nodesetval,
2651 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2652 NULL);
2653 if (rc)
2654 goto fail;
2655 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2657 rc = GPG_ERR_NO_DATA;
2658 goto fail;
2660 else if (xpath->req[1])
2662 rc = 0;
2663 goto fail;
2666 pthread_cleanup_push (xpath_command_cleanup, &xpath);
2667 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2668 xmlBufferLength (xpath->buf));
2669 pthread_cleanup_pop (0);
2670 fail:
2671 xpath_command_cleanup (xpath);
2672 return rc;
2675 static gpg_error_t
2676 xpath_command (assuan_context_t ctx, char *line)
2678 struct client_s *client = assuan_get_pointer (ctx);
2679 gpg_error_t rc;
2680 struct argv_s *args[] = {
2681 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2682 NULL
2685 if (disable_list_and_dump == 1)
2686 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2688 rc = peer_is_self(client);
2689 if (rc)
2690 return send_error (ctx, rc);
2692 rc = parse_options (&line, args, client);
2693 if (rc)
2694 return send_error (ctx, rc);
2696 if (client->opts & OPT_INQUIRE)
2698 unsigned char *result;
2699 size_t len;
2701 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2702 if (rc)
2703 return send_error (ctx, rc);
2705 line = (char *) result;
2708 if (!line || !*line)
2709 rc = GPG_ERR_SYNTAX;
2711 if (!rc)
2712 rc = do_xpath (ctx, line);
2714 if (client->opts & OPT_INQUIRE)
2715 xfree (line);
2717 return send_error (ctx, rc);
2720 static gpg_error_t
2721 do_xpathattr (assuan_context_t ctx, char *line)
2723 struct client_s *client = assuan_get_pointer (ctx);
2724 gpg_error_t rc;
2725 char **req = NULL;
2726 int cmd = 0; //SET
2727 struct xpath_s _x = { 0 };
2728 struct xpath_s *xpath = &_x;
2730 if ((req = str_split (line, " ", 3)) == NULL)
2731 return GPG_ERR_ENOMEM;
2733 if (!req[0])
2735 rc = GPG_ERR_SYNTAX;
2736 goto fail;
2739 if (!strcasecmp (req[0], "SET"))
2740 cmd = 0;
2741 else if (!strcasecmp (req[0], "DELETE"))
2742 cmd = 1;
2743 else
2745 rc = GPG_ERR_SYNTAX;
2746 goto fail;
2749 if (!req[1] || !req[2])
2751 rc = GPG_ERR_SYNTAX;
2752 goto fail;
2755 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2757 rc = GPG_ERR_ENOMEM;
2758 goto fail;
2761 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2763 rc = GPG_ERR_SYNTAX;
2764 goto fail;
2767 xpath->xp = xmlXPathNewContext (client->doc);
2768 if (!xpath->xp)
2770 rc = GPG_ERR_BAD_DATA;
2771 goto fail;
2774 xpath->result = xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2775 if (!xpath->result)
2777 rc = GPG_ERR_BAD_DATA;
2778 goto fail;
2781 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2783 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2784 goto fail;
2787 rc = recurse_xpath_nodeset (client, client->doc, xpath->result->nodesetval,
2788 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2789 (xmlChar *) req[1]);
2791 fail:
2792 xpath_command_cleanup (xpath);
2793 strv_free (req);
2794 return rc;
2797 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2798 static gpg_error_t
2799 xpathattr_command (assuan_context_t ctx, char *line)
2801 struct client_s *client = assuan_get_pointer (ctx);
2802 gpg_error_t rc;
2803 struct argv_s *args[] = {
2804 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2805 NULL
2808 if (disable_list_and_dump == 1)
2809 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2811 rc = peer_is_self(client);
2812 if (rc)
2813 return send_error (ctx, rc);
2815 rc = parse_options (&line, args, client);
2816 if (rc)
2817 return send_error (ctx, rc);
2819 if (client->opts & OPT_INQUIRE)
2821 unsigned char *result;
2822 size_t len;
2824 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2825 if (rc)
2826 return send_error (ctx, rc);
2828 line = (char *) result;
2831 if (!line || !*line)
2832 rc = GPG_ERR_SYNTAX;
2834 if (!rc)
2835 rc = do_xpathattr (ctx, line);
2837 if (client->opts & OPT_INQUIRE)
2838 xfree (line);
2840 return send_error (ctx, rc);
2843 static gpg_error_t
2844 do_import (struct client_s *client, const char *root_element,
2845 unsigned char *content)
2847 char **dst_path = NULL;
2848 xmlDocPtr doc = NULL;
2849 xmlNodePtr n, root, copy;
2850 gpg_error_t rc;
2852 if (!content || !*content)
2854 xfree (content);
2855 return GPG_ERR_SYNTAX;
2858 if (root_element)
2859 dst_path = str_split (root_element, "\t", 0);
2861 if (dst_path && !valid_element_path (dst_path, 0))
2863 if (dst_path)
2864 strv_free (dst_path);
2866 return GPG_ERR_INV_VALUE;
2869 struct string_s *str = string_new_content ((char *)content);
2870 str = string_prepend (str, "<pwmd>");
2871 str = string_append (str, "</pwmd>");
2872 doc = xmlReadDoc ((xmlChar *) str->str, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2873 string_free (str, 1);
2874 if (!doc)
2876 rc = GPG_ERR_BAD_DATA;
2877 goto fail;
2880 root = xmlDocGetRootElement (doc);
2881 xmlNodePtr root_orig = root->children;
2882 root = root->children;
2883 rc = validate_import (root);
2884 if (rc)
2885 goto fail;
2889 again:
2890 if (dst_path)
2892 char **path = strv_dup (dst_path);
2893 if (!path)
2895 log_write ("%s(%i): %s", __FILE__, __LINE__,
2896 pwmd_strerror (GPG_ERR_ENOMEM));
2897 rc = GPG_ERR_ENOMEM;
2898 goto fail;
2901 xmlChar *a = xmlGetProp (root, (xmlChar *) "_name");
2902 if (!a)
2904 strv_free (path);
2905 rc = GPG_ERR_INV_VALUE;
2906 goto fail;
2909 if (strv_printf (&path, "%s", (char *) a) == 0)
2911 xmlFree (a);
2912 rc = GPG_ERR_ENOMEM;
2913 goto fail;
2916 xmlFree (a);
2917 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2918 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2920 strv_free (path);
2921 goto fail;
2924 if (!rc)
2926 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2927 NULL, NULL, NULL, 0, 0, NULL, 1);
2928 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2930 strv_free (path);
2931 goto fail;
2933 else if (!rc)
2935 xmlUnlinkNode (n);
2936 xmlFreeNode (n);
2937 strv_free (path);
2938 path = NULL;
2939 goto again;
2943 if (rc == GPG_ERR_ELEMENT_NOT_FOUND)
2945 n = create_element_path (client, &path, &rc, NULL);
2946 if (rc)
2947 goto fail;
2950 if (root->children)
2952 copy = xmlCopyNodeList (root->children);
2953 n = xmlAddChildList (n, copy);
2954 if (!n)
2955 rc = GPG_ERR_ENOMEM;
2958 strv_free (path);
2960 else
2962 char **path = NULL;
2964 /* Check if the content root element can create a DTD root element. */
2965 if (!xmlStrEqual ((xmlChar *) "element", root->name))
2967 rc = GPG_ERR_SYNTAX;
2968 goto fail;
2971 xmlChar *a;
2973 if ((a = xmlGetProp (root, (xmlChar *) "_name")) == NULL)
2975 rc = GPG_ERR_SYNTAX;
2976 goto fail;
2979 char *tmp = str_dup ((char *) a);
2980 xmlFree (a);
2981 int literal = is_literal_element (&tmp);
2983 if (!valid_xml_element ((xmlChar *) tmp) || literal)
2985 xfree (tmp);
2986 rc = GPG_ERR_INV_VALUE;
2987 goto fail;
2990 if (strv_printf (&path, "%s", tmp) == 0)
2992 xfree (tmp);
2993 rc = GPG_ERR_ENOMEM;
2994 goto fail;
2997 xfree (tmp);
2998 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 1);
2999 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3001 rc = GPG_ERR_BAD_DATA;
3002 goto fail;
3005 /* Overwriting the existing tree. */
3006 if (!rc)
3008 xmlUnlinkNode (n);
3009 xmlFreeNodeList (n);
3012 rc = 0;
3013 xmlSetProp (root, (xmlChar *) "_name", (xmlChar *) path[0]);
3014 n = xmlCopyNode (root, 1);
3015 n = xmlAddChildList (xmlDocGetRootElement (client->doc), n);
3016 strv_free (path);
3019 if (n && !rc)
3020 rc = update_element_mtime (n->parent);
3022 for (root = root_orig->next; root; root = root->next)
3024 if (root->type == XML_ELEMENT_NODE)
3025 break;
3028 root_orig = root;
3030 while (root);
3032 fail:
3033 if (doc)
3034 xmlFreeDoc (doc);
3036 if (dst_path)
3037 strv_free (dst_path);
3039 return rc;
3042 static gpg_error_t
3043 parse_import_opt_root (void *data, void *value)
3045 struct client_s *client = data;
3047 client->import_root = str_dup (value);
3048 return client->import_root ? 0 : GPG_ERR_ENOMEM;
3051 static gpg_error_t
3052 import_command (assuan_context_t ctx, char *line)
3054 gpg_error_t rc;
3055 struct client_s *client = assuan_get_pointer (ctx);
3056 unsigned char *result;
3057 size_t len;
3058 struct argv_s *args[] = {
3059 &(struct argv_s) {"root", OPTION_TYPE_ARG, parse_import_opt_root},
3060 NULL
3063 xfree (client->import_root);
3064 client->import_root = NULL;
3065 rc = parse_options (&line, args, client);
3066 if (rc)
3067 return send_error (ctx, rc);
3069 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3070 if (rc)
3072 xfree (client->import_root);
3073 client->import_root = NULL;
3074 return send_error (ctx, rc);
3077 rc = do_import (client, client->import_root, result);
3078 xfree (client->import_root);
3079 client->import_root = NULL;
3080 return send_error (ctx, rc);
3083 static gpg_error_t
3084 do_lock (struct client_s *client, int add)
3086 gpg_error_t rc = lock_file_mutex (client, add);
3088 if (!rc)
3089 client->flags |= FLAG_LOCK_CMD;
3091 return rc;
3094 static gpg_error_t
3095 lock_command (assuan_context_t ctx, char *line)
3097 struct client_s *client = assuan_get_pointer (ctx);
3098 gpg_error_t rc = do_lock (client, 0);
3100 return send_error (ctx, rc);
3103 static gpg_error_t
3104 unlock_command (assuan_context_t ctx, char *line)
3106 struct client_s *client = assuan_get_pointer (ctx);
3107 gpg_error_t rc;
3109 rc = unlock_file_mutex (client, 0);
3110 return send_error (ctx, rc);
3113 static gpg_error_t
3114 option_command (assuan_context_t ctx, char *line)
3116 struct client_s *client = assuan_get_pointer (ctx);
3117 gpg_error_t rc = 0;
3118 struct pinentry_option_s *pin_opts = &client->pinentry_opts;
3119 #ifdef WITH_AGENT
3120 struct agent_s *agent = client->crypto->agent;
3121 #endif
3122 char namebuf[255] = { 0 };
3123 char *name = namebuf;
3124 char *value = NULL, *p;
3126 p = strchr (line, '=');
3127 if (!p)
3128 strncpy (namebuf, line, sizeof(namebuf));
3129 else
3131 strncpy (namebuf, line, strlen (line)-strlen (p));
3132 value = p+1;
3135 log_write1 ("OPTION name='%s' value='%s'", name, value);
3137 if (strcasecmp (name, (char *) "log_level") == 0)
3139 long l = 0;
3141 if (value)
3143 l = strtol (value, NULL, 10);
3145 if (l < 0 || l > 2)
3146 return send_error (ctx, GPG_ERR_INV_VALUE);
3149 MUTEX_LOCK (&rcfile_mutex);
3150 config_set_int_param (&global_config, "global", "log_level", value);
3151 MUTEX_UNLOCK (&rcfile_mutex);
3152 goto done;
3154 else if (strcasecmp (name, (char *) "lock-timeout") == 0)
3156 long n = 0;
3157 char *p = NULL;
3159 if (value)
3161 n = strtol (value, &p, 10);
3162 if (p && *p)
3163 return send_error (ctx, GPG_ERR_INV_VALUE);
3166 client->lock_timeout = n;
3167 goto done;
3169 else if (strcasecmp (name, (char *) "NAME") == 0)
3171 char *tmp = pthread_getspecific (thread_name_key);
3173 if (tmp)
3174 xfree (tmp);
3176 if (!value || !*value)
3178 pthread_setspecific (thread_name_key, str_dup (""));
3179 goto done;
3182 pthread_setspecific (thread_name_key, str_dup (value));
3183 goto done;
3185 else if (strcasecmp (name, (char *) "lc-messages") == 0)
3187 xfree (pin_opts->lc_messages);
3188 pin_opts->lc_messages = NULL;
3189 if (value && *value)
3190 pin_opts->lc_messages = str_dup (value);
3191 #ifdef WITH_AGENT
3192 if (use_agent)
3193 rc = set_agent_option (client->crypto->agent, "lc-messages", value);
3194 #endif
3196 else if (strcasecmp (name, (char *) "lc-ctype") == 0)
3198 xfree (pin_opts->lc_ctype);
3199 pin_opts->lc_ctype = NULL;
3200 if (value && *value)
3201 pin_opts->lc_ctype = str_dup (value);
3202 #ifdef WITH_AGENT
3203 if (use_agent)
3204 rc = set_agent_option (client->crypto->agent, "lc-ctype", value);
3205 #endif
3207 else if (strcasecmp (name, (char *) "ttyname") == 0)
3209 xfree (pin_opts->ttyname);
3210 pin_opts->ttyname = NULL;
3211 if (value && *value)
3212 pin_opts->ttyname = str_dup (value);
3213 #ifdef WITH_AGENT
3214 if (use_agent)
3215 rc = set_agent_option (client->crypto->agent, "ttyname", value);
3216 #endif
3218 else if (strcasecmp (name, (char *) "ttytype") == 0)
3220 xfree (pin_opts->ttytype);
3221 pin_opts->ttytype = NULL;
3222 if (value && *value)
3223 pin_opts->ttytype = str_dup (value);
3224 #ifdef WITH_AGENT
3225 if (use_agent)
3226 rc = set_agent_option (client->crypto->agent, "ttytype", value);
3227 #endif
3229 else if (strcasecmp (name, (char *) "display") == 0)
3231 xfree (pin_opts->display);
3232 pin_opts->display = NULL;
3233 if (value && *value)
3234 pin_opts->display = str_dup (value);
3235 #ifdef WITH_AGENT
3236 if (use_agent)
3237 rc = set_agent_option (client->crypto->agent, "display", value);
3238 #endif
3240 else if (strcasecmp (name, (char *) "pinentry-desc") == 0)
3242 xfree (pin_opts->desc);
3243 pin_opts->desc = NULL;
3244 if (value && *value)
3245 pin_opts->desc = str_dup (value);
3247 else if (strcasecmp (name, (char *) "pinentry-title") == 0)
3249 xfree (pin_opts->title);
3250 pin_opts->title = NULL;
3251 if (value && *value)
3252 pin_opts->title = str_dup (value);
3254 else if (strcasecmp (name, (char *) "pinentry-prompt") == 0)
3256 xfree (pin_opts->prompt);
3257 pin_opts->prompt = NULL;
3258 if (value && *value)
3259 pin_opts->prompt = str_dup (value);
3262 else if (strcasecmp (name, "pinentry-timeout") == 0)
3264 char *p = NULL;
3265 int n;
3267 if (!value)
3268 goto done;
3270 n = (int) strtol (value, &p, 10);
3272 if (*p || n < 0)
3273 return send_error (ctx, GPG_ERR_INV_VALUE);
3275 pin_opts->timeout = n;
3276 MUTEX_LOCK (&rcfile_mutex);
3277 config_set_int_param (&global_config,
3278 client->filename ? client->filename : "global",
3279 "pinentry_timeout", value);
3280 MUTEX_UNLOCK (&rcfile_mutex);
3281 goto done;
3283 else if (strcasecmp (name, "disable-pinentry") == 0)
3285 char *p = NULL;
3286 int n = 1;
3288 if (value && *value)
3290 n = (int) strtol (value, &p, 10);
3291 if (*p || n < 0 || n > 1)
3292 return send_error (ctx, GPG_ERR_INV_VALUE);
3295 if (n)
3296 client->flags |= FLAG_NO_PINENTRY;
3297 else
3298 client->flags &= ~FLAG_NO_PINENTRY;
3300 #ifdef WITH_AGENT
3301 if (use_agent)
3303 if (client->flags & FLAG_NO_PINENTRY)
3304 rc = set_agent_option (client->crypto->agent, "pinentry-mode",
3305 "loopback");
3306 else
3307 rc = set_agent_option (client->crypto->agent, "pinentry-mode",
3308 "ask");
3310 if (rc)
3311 return send_error (ctx, rc);
3313 #endif
3315 else
3316 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
3318 done:
3319 #ifdef WITH_AGENT
3320 if (!rc && use_agent && agent)
3322 rc = pinentry_merge_options (&client->crypto->agent->pinentry_opts,
3323 pin_opts);
3325 #endif
3327 return send_error (ctx, rc);
3330 static gpg_error_t
3331 do_rename (assuan_context_t ctx, char *line)
3333 struct client_s *client = assuan_get_pointer (ctx);
3334 gpg_error_t rc;
3335 char **req, **src, *dst;
3336 xmlNodePtr n, ndst;
3338 req = str_split (line, " ", 0);
3340 if (!req || !req[0] || !req[1])
3342 strv_free (req);
3343 return GPG_ERR_SYNTAX;
3346 dst = req[1];
3347 is_literal_element (&dst);
3349 if (!valid_xml_element ((xmlChar *) dst))
3351 strv_free (req);
3352 return GPG_ERR_INV_VALUE;
3355 if (strchr (req[0], '\t'))
3356 src = str_split (req[0], "\t", 0);
3357 else
3358 src = str_split (req[0], " ", 0);
3360 if (!src || !*src)
3362 rc = GPG_ERR_SYNTAX;
3363 goto fail;
3366 n = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3367 if (src[1] && n)
3368 n = find_elements (client, client->doc, n->children, src + 1, &rc, NULL, NULL,
3369 NULL, 0, 0, NULL, 0);
3371 if (!n)
3372 goto fail;
3374 xmlChar *a = xmlGetProp (n, (xmlChar *) "_name");
3375 if (!a)
3377 rc = GPG_ERR_ENOMEM;
3378 goto fail;
3381 /* To prevent unwanted effects:
3383 * <root name="a"><b/></root>
3385 * RENAME a<TAB>b b
3387 if (xmlStrEqual (a, (xmlChar *) dst))
3389 xmlFree (a);
3390 rc = GPG_ERR_AMBIGUOUS_NAME;
3391 goto fail;
3394 xmlFree (a);
3395 char **tmp = NULL;
3396 if (src[1])
3398 char **p;
3400 for (p = src; *p; p++)
3402 if (!*(p + 1))
3403 break;
3404 strv_printf (&tmp, "%s", *p);
3408 strv_printf (&tmp, "!%s", dst);
3409 ndst = find_root_element (client, client->doc, &tmp, &rc, NULL, 0, 0);
3410 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3412 strv_free (tmp);
3413 goto fail;
3416 if (tmp[1] && ndst)
3417 ndst = find_elements (client, client->doc, ndst->children, tmp + 1, &rc, NULL,
3418 NULL, NULL, 0, 0, NULL, 0);
3420 strv_free (tmp);
3421 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3422 goto fail;
3424 rc = 0;
3426 /* Target may exist:
3428 * <root name="a"/>
3429 * <root name="b" target="a"/>
3431 * RENAME b a
3433 * Would need to do:
3434 * RENAME !b a
3436 if (ndst == n)
3438 rc = GPG_ERR_AMBIGUOUS_NAME;
3439 goto fail;
3442 if (ndst)
3444 unlink_node (ndst);
3445 xmlFreeNodeList (ndst);
3448 rc = add_attribute (n, "_name", dst);
3450 fail:
3451 strv_free (req);
3452 strv_free (src);
3453 return rc;
3456 static gpg_error_t
3457 rename_command (assuan_context_t ctx, char *line)
3459 struct client_s *client = assuan_get_pointer (ctx);
3460 gpg_error_t rc;
3461 struct argv_s *args[] = {
3462 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3463 NULL
3466 rc = parse_options (&line, args, client);
3467 if (rc)
3468 return send_error (ctx, rc);
3470 if (client->opts & OPT_INQUIRE)
3472 unsigned char *result;
3473 size_t len;
3475 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3476 if (rc)
3477 return send_error (ctx, rc);
3479 line = (char *) result;
3482 rc = do_rename (ctx, line);
3484 if (client->opts & OPT_INQUIRE)
3485 xfree (line);
3487 return send_error (ctx, rc);
3490 static gpg_error_t
3491 do_copy (assuan_context_t ctx, char *line)
3493 struct client_s *client = assuan_get_pointer (ctx);
3494 gpg_error_t rc;
3495 char **req, **src = NULL, **dst = NULL;
3496 xmlNodePtr nsrc, ndst, new = NULL;
3498 req = str_split (line, " ", 0);
3499 if (!req || !req[0] || !req[1])
3501 strv_free (req);
3502 return GPG_ERR_SYNTAX;
3505 if (strchr (req[0], '\t'))
3506 src = str_split (req[0], "\t", 0);
3507 else
3508 src = str_split (req[0], " ", 0);
3510 if (!src || !*src)
3512 rc = GPG_ERR_SYNTAX;
3513 goto fail;
3516 if (strchr (req[1], '\t'))
3517 dst = str_split (req[1], "\t", 0);
3518 else
3519 dst = str_split (req[1], " ", 0);
3521 if (!dst || !*dst)
3523 rc = GPG_ERR_SYNTAX;
3524 goto fail;
3527 if (!valid_element_path (dst, 0))
3529 rc = GPG_ERR_INV_VALUE;
3530 goto fail;
3533 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3534 if (nsrc && src[1])
3535 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc, NULL,
3536 NULL, NULL, 0, 0, NULL, 0);
3538 if (!nsrc)
3539 goto fail;
3541 new = xmlCopyNodeList (nsrc);
3542 if (!new)
3544 rc = GPG_ERR_ENOMEM;
3545 goto fail;
3548 int create = 0;
3549 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3550 if (ndst && dst[1])
3552 if (ndst->children)
3553 ndst = find_elements (client, client->doc, ndst->children, dst + 1, &rc, NULL,
3554 NULL, create_target_elements_cb, 0, 0, NULL, 0);
3555 else
3556 create = 1;
3558 else
3559 create = 1;
3561 if (!ndst && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3562 goto fail;
3563 else if (!ndst || create)
3565 ndst = create_element_path (client, &dst, &rc, NULL);
3566 if (!ndst)
3567 goto fail;
3570 /* Merge any attributes from the src node to the initial dst node. */
3571 for (xmlAttrPtr attr = new->properties; attr; attr = attr->next)
3573 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3574 continue;
3576 xmlAttrPtr a = xmlHasProp (ndst, attr->name);
3577 if (a)
3578 xmlRemoveProp (a);
3580 xmlChar *tmp = xmlNodeGetContent (attr->children);
3581 xmlNewProp (ndst, attr->name, tmp);
3582 xmlFree (tmp);
3583 rc = add_attribute (ndst, NULL, NULL);
3586 xmlNodePtr n = ndst->children;
3587 xmlUnlinkNode (n);
3588 xmlFreeNodeList (n);
3589 ndst->children = NULL;
3591 if (new->children)
3593 n = xmlCopyNodeList (new->children);
3594 if (!n)
3596 rc = GPG_ERR_ENOMEM;
3597 goto fail;
3600 n = xmlAddChildList (ndst, n);
3601 if (!n)
3603 rc = GPG_ERR_ENOMEM;
3604 goto fail;
3607 rc = update_element_mtime (xmlDocGetRootElement (client->doc) ==
3608 ndst->parent ? ndst : ndst->parent);
3611 fail:
3612 if (new)
3614 xmlUnlinkNode (new);
3615 xmlFreeNodeList (new);
3618 if (req)
3619 strv_free (req);
3621 if (src)
3622 strv_free (src);
3624 if (dst)
3625 strv_free (dst);
3627 return rc;
3630 static gpg_error_t
3631 copy_command (assuan_context_t ctx, char *line)
3633 struct client_s *client = assuan_get_pointer (ctx);
3634 gpg_error_t rc;
3635 struct argv_s *args[] = {
3636 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3637 NULL
3640 rc = parse_options (&line, args, client);
3641 if (rc)
3642 return send_error (ctx, rc);
3644 if (client->opts & OPT_INQUIRE)
3646 unsigned char *result;
3647 size_t len;
3649 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3650 if (rc)
3651 return send_error (ctx, rc);
3653 line = (char *) result;
3656 rc = do_copy (ctx, line);
3658 if (client->opts & OPT_INQUIRE)
3659 xfree (line);
3661 return send_error (ctx, rc);
3664 static gpg_error_t
3665 do_move (assuan_context_t ctx, char *line)
3667 struct client_s *client = assuan_get_pointer (ctx);
3668 gpg_error_t rc;
3669 char **req, **src = NULL, **dst = NULL;
3670 xmlNodePtr nsrc, ndst = NULL;
3672 req = str_split (line, " ", 0);
3674 if (!req || !req[0] || !req[1])
3676 strv_free (req);
3677 return GPG_ERR_SYNTAX;
3680 if (strchr (req[0], '\t'))
3681 src = str_split (req[0], "\t", 0);
3682 else
3683 src = str_split (req[0], " ", 0);
3685 if (!src || !*src)
3687 rc = GPG_ERR_SYNTAX;
3688 goto fail;
3691 if (strchr (req[1], '\t'))
3692 dst = str_split (req[1], "\t", 0);
3693 else
3694 dst = str_split (req[1], " ", 0);
3696 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3697 if (nsrc && src[1])
3698 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc, NULL,
3699 NULL, NULL, 0, 0, NULL, 0);
3701 if (!nsrc)
3702 goto fail;
3704 if (dst)
3706 if (!valid_element_path (dst, 0))
3708 rc = GPG_ERR_INV_VALUE;
3709 goto fail;
3712 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3713 if (ndst && dst[1])
3714 ndst = find_elements (client, client->doc, ndst->children, dst + 1, &rc, NULL,
3715 NULL, NULL, 0, 0, NULL, 0);
3717 else
3718 ndst = xmlDocGetRootElement (client->doc);
3720 for (xmlNodePtr n = ndst; n; n = n->parent)
3722 if (n == nsrc)
3724 rc = GPG_ERR_CONFLICT;
3725 goto fail;
3729 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3730 goto fail;
3732 rc = 0;
3734 if (ndst)
3736 xmlChar *a = node_has_attribute (nsrc, (xmlChar *) "_name");
3738 xmlNodePtr dup = find_element (client, ndst->children, (char *) a,
3739 NULL, &rc);
3740 xmlFree (a);
3742 if (rc)
3743 goto fail;
3745 if (dup)
3747 if (dup == nsrc)
3748 goto fail;
3750 if (ndst == xmlDocGetRootElement (client->doc))
3752 xmlNodePtr n = nsrc;
3753 int match = 0;
3755 while (n->parent && n->parent != ndst)
3756 n = n->parent;
3758 xmlChar *a = node_has_attribute (n, (xmlChar *) "_name");
3759 xmlChar *b = node_has_attribute (nsrc, (xmlChar *) "_name");
3761 if (xmlStrEqual (a, b))
3763 match = 1;
3764 xmlUnlinkNode (nsrc);
3765 xmlUnlinkNode (n);
3766 xmlFreeNodeList (n);
3769 xmlFree (a);
3770 xmlFree (b);
3772 if (!match)
3774 xmlUnlinkNode (dup);
3775 xmlFreeNodeList (dup);
3778 else
3779 xmlUnlinkNode (dup);
3783 if (!ndst && dst)
3785 xmlChar *name = node_has_attribute (nsrc, (xmlChar *) "_name");
3787 if (nsrc->parent == xmlDocGetRootElement (client->doc)
3788 && !strcmp ((char *) name, *dst))
3790 xmlFree (name);
3791 rc = GPG_ERR_CONFLICT;
3792 goto fail;
3795 xmlFree (name);
3796 ndst = create_element_path (client, &dst, &rc, nsrc);
3799 if (!ndst)
3800 goto fail;
3802 update_element_mtime (nsrc->parent);
3803 xmlUnlinkNode (nsrc);
3804 ndst = xmlAddChildList (ndst, nsrc);
3806 if (!ndst)
3807 rc = GPG_ERR_ENOMEM;
3808 else
3809 update_element_mtime (ndst->parent);
3811 fail:
3812 if (req)
3813 strv_free (req);
3815 if (src)
3816 strv_free (src);
3818 if (dst)
3819 strv_free (dst);
3821 return rc;
3824 static gpg_error_t
3825 move_command (assuan_context_t ctx, char *line)
3827 struct client_s *client = assuan_get_pointer (ctx);
3828 gpg_error_t rc;
3829 struct argv_s *args[] = {
3830 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3831 NULL
3834 rc = parse_options (&line, args, client);
3835 if (rc)
3836 return send_error (ctx, rc);
3838 if (client->opts & OPT_INQUIRE)
3840 unsigned char *result;
3841 size_t len;
3843 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3844 if (rc)
3845 return send_error (ctx, rc);
3847 line = (char *) result;
3850 rc = do_move (ctx, line);
3852 if (client->opts & OPT_INQUIRE)
3853 xfree (line);
3855 return send_error (ctx, rc);
3858 static gpg_error_t
3859 ls_command (assuan_context_t ctx, char *line)
3861 gpg_error_t rc;
3862 char *tmp = str_asprintf ("%s/data", homedir);
3863 char *dir = expand_homedir (tmp);
3864 DIR *d = opendir (dir);
3866 rc = gpg_error_from_errno (errno);
3867 xfree (tmp);
3869 if (!d)
3871 xfree (dir);
3872 return send_error (ctx, rc);
3875 size_t len =
3876 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3877 struct dirent *p = xmalloc (len), *cur = NULL;
3878 char *list = NULL;
3880 xfree (dir);
3881 rc = 0;
3883 while (!readdir_r (d, p, &cur) && cur)
3885 if (cur->d_name[0] == '.' && cur->d_name[1] == '\0')
3886 continue;
3887 else if (cur->d_name[0] == '.' && cur->d_name[1] == '.'
3888 && cur->d_name[2] == '\0')
3889 continue;
3891 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3893 if (!tmp)
3895 if (list)
3896 xfree (list);
3898 rc = GPG_ERR_ENOMEM;
3899 break;
3902 xfree (list);
3903 list = tmp;
3906 closedir (d);
3907 xfree (p);
3909 if (rc)
3910 return send_error (ctx, rc);
3912 if (!list)
3913 return send_error (ctx, GPG_ERR_NO_DATA);
3915 list[strlen (list) - 1] = 0;
3916 rc = xfer_data (ctx, list, strlen (list));
3917 xfree (list);
3918 return send_error (ctx, rc);
3921 static gpg_error_t
3922 bye_notify (assuan_context_t ctx, char *line)
3924 struct client_s *cl = assuan_get_pointer (ctx);
3926 #ifdef WITH_GNUTLS
3927 if (cl->thd->remote)
3929 int rc;
3933 struct timeval tv = { 0, 50000 };
3935 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3936 if (rc == GNUTLS_E_AGAIN)
3937 select (0, NULL, NULL, NULL, &tv);
3939 while (rc == GNUTLS_E_AGAIN);
3941 #endif
3943 /* This will let assuan_process_next() return. */
3944 fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK);
3945 cl->last_rc = 0; // BYE command result
3946 return 0;
3949 static gpg_error_t
3950 reset_notify (assuan_context_t ctx, char *line)
3952 struct client_s *client = assuan_get_pointer (ctx);
3954 if (client)
3955 cleanup_client (client);
3957 return 0;
3961 * This is called before every Assuan command.
3963 static gpg_error_t
3964 command_startup (assuan_context_t ctx, const char *name)
3966 struct client_s *client = assuan_get_pointer (ctx);
3967 gpg_error_t rc;
3968 struct command_table_s *cmd = NULL;
3970 log_write1 ("command='%s'", name);
3971 client->last_rc = client->opts = 0;
3973 for (int i = 0; command_table[i]; i++)
3975 if (!strcasecmp (name, command_table[i]->name))
3977 if (command_table[i]->ignore_startup)
3978 return 0;
3979 cmd = command_table[i];
3980 break;
3984 client->last_rc = rc = gpg_error (file_modified (client, cmd));
3985 return rc;
3989 * This is called after every Assuan command.
3991 static void
3992 command_finalize (assuan_context_t ctx, gpg_error_t rc)
3994 struct client_s *client = assuan_get_pointer (ctx);
3996 if (!(client->flags & FLAG_LOCK_CMD))
3997 unlock_file_mutex (client, 0);
3999 log_write1 (_("command completed: rc=%u"), client->last_rc);
4000 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
4001 #ifdef WITH_GNUTLS
4002 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
4003 #endif
4006 static gpg_error_t
4007 help_command (assuan_context_t ctx, char *line)
4009 gpg_error_t rc;
4010 int i;
4012 if (!line || !*line)
4014 char *tmp;
4015 char *help = str_dup (_("Usage: HELP [<COMMAND>]\n"
4016 "For commands that take an element path as an argument, each element is "
4017 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
4018 "\n" "COMMANDS:"));
4020 for (i = 0; command_table[i]; i++)
4022 if (!command_table[i]->help)
4023 continue;
4025 tmp = str_asprintf ("%s %s", help, command_table[i]->name);
4026 xfree (help);
4027 help = tmp;
4030 tmp = strip_texi_and_wrap (help);
4031 xfree (help);
4032 rc = xfer_data (ctx, tmp, strlen (tmp));
4033 xfree (tmp);
4034 return send_error (ctx, rc);
4037 for (i = 0; command_table[i]; i++)
4039 if (!strcasecmp (line, command_table[i]->name))
4041 char *help, *tmp;
4043 if (!command_table[i]->help)
4044 break;
4046 help = strip_texi_and_wrap (command_table[i]->help);
4047 tmp = str_asprintf (_("Usage: %s"), help);
4048 xfree (help);
4049 rc = xfer_data (ctx, tmp, strlen (tmp));
4050 xfree (tmp);
4051 return send_error (ctx, rc);
4055 return send_error (ctx, GPG_ERR_INV_NAME);
4058 static void
4059 new_command (const char *name, int ignore, int unlock,
4060 gpg_error_t (*handler) (assuan_context_t, char *),
4061 const char *help)
4063 int i = 0;
4065 if (command_table)
4066 for (i = 0; command_table[i]; i++);
4068 command_table =
4069 xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
4070 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
4071 command_table[i]->name = name;
4072 command_table[i]->handler = handler;
4073 command_table[i]->ignore_startup = ignore;
4074 command_table[i]->unlock = unlock;
4075 command_table[i++]->help = help;
4076 command_table[i] = NULL;
4079 void
4080 deinit_commands ()
4082 int i;
4084 for (i = 0; command_table[i]; i++)
4085 xfree (command_table[i]);
4087 xfree (command_table);
4090 static int
4091 sort_commands (const void *arg1, const void *arg2)
4093 struct command_table_s *const *a = arg1;
4094 struct command_table_s *const *b = arg2;
4096 if (!*a || !*b)
4097 return 0;
4098 else if (*a && !*b)
4099 return 1;
4100 else if (!*a && *b)
4101 return -1;
4103 return strcmp ((*a)->name, (*b)->name);
4106 static gpg_error_t
4107 passwd_command (assuan_context_t ctx, char *line)
4109 struct client_s *client = assuan_get_pointer (ctx);
4110 gpg_error_t rc;
4111 struct argv_s *args[] = {
4112 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
4113 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
4114 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG, parse_opt_no_passphrase},
4115 NULL
4118 if (client->flags & FLAG_NEW)
4119 return send_error (ctx, GPG_ERR_INV_STATE);
4121 client->crypto->save.s2k_count =
4122 config_get_ulong (client->filename, "s2k_count");
4123 rc = parse_options (&line, args, client);
4124 if (rc)
4125 return send_error (ctx, rc);
4127 if (!rc && client->opts & OPT_RESET)
4129 rc = cache_clear (client->md5file);
4130 if (!rc)
4131 send_status_all (STATUS_CACHE, NULL);
4134 if (!rc)
4136 if (!IS_PKI (client->crypto))
4138 struct crypto_s *crypto;
4140 xfree (client->crypto->filename);
4141 client->crypto->filename = str_dup (client->filename);
4142 rc = change_passwd (ctx, client->filename,
4143 client->flags & FLAG_NO_PINENTRY, &crypto,
4144 (client->opts & OPT_NO_PASSPHRASE));
4145 if (!rc)
4147 cleanup_crypto (&client->crypto);
4148 client->crypto = crypto;
4149 update_checksum (client);
4150 cleanup_crypto_stage1 (client->crypto);
4153 #ifdef WITH_AGENT
4154 else
4156 if (client->crypto->save.s2k_count)
4157 rc = send_to_agent (client->crypto->agent, NULL, NULL,
4158 "OPTION s2k-count=%lu",
4159 client->crypto->save.s2k_count);
4161 if (!rc)
4162 rc = agent_passwd (client->crypto);
4164 #endif
4167 return send_error (ctx, rc);
4170 static gpg_error_t
4171 parse_keygrip_opt_sign (void *data, void *value)
4173 struct client_s *client = data;
4175 (void) value;
4176 client->opts |= OPT_SIGN;
4177 return 0;
4180 static gpg_error_t
4181 keygrip_command (assuan_context_t ctx, char *line)
4183 struct client_s *client = assuan_get_pointer (ctx);
4184 gpg_error_t rc;
4185 struct crypto_s *crypto = NULL;
4186 struct argv_s *args[] = {
4187 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_keygrip_opt_sign},
4188 NULL
4191 if (!line || !*line)
4192 return send_error (ctx, GPG_ERR_SYNTAX);
4194 rc = parse_options (&line, args, client);
4195 if (rc)
4196 return send_error (ctx, rc);
4198 if (!valid_filename (line))
4199 return send_error (ctx, GPG_ERR_INV_VALUE);
4201 rc = init_client_crypto (&crypto);
4202 if (rc)
4203 return send_error (ctx, rc);
4205 rc = read_data_file (line, crypto);
4206 if (!rc)
4208 char *hexgrip = NULL;
4210 if (!IS_PKI (crypto))
4212 cleanup_crypto (&crypto);
4213 return send_error (ctx, GPG_ERR_NOT_SUPPORTED);
4216 if (client->opts & OPT_SIGN)
4218 if (valid_keygrip (crypto->sign_grip, sizeof (crypto->sign_grip)))
4219 hexgrip = bin2hex (crypto->sign_grip, sizeof (crypto->sign_grip));
4222 if (!hexgrip)
4223 hexgrip = bin2hex (crypto->grip, sizeof (crypto->grip));
4225 if (!hexgrip)
4226 rc = GPG_ERR_ENOMEM;
4227 else
4228 rc = xfer_data (ctx, hexgrip, strlen (hexgrip));
4230 xfree (hexgrip);
4233 UPDATE_AGENT_CTX (client, crypto);
4234 cleanup_crypto (&crypto);
4235 return send_error (ctx, rc);
4238 static gpg_error_t
4239 parse_opt_data (void *data, void *value)
4241 struct client_s *client = data;
4243 (void) value;
4244 client->opts |= OPT_DATA;
4245 return 0;
4248 static gpg_error_t
4249 getinfo_command (assuan_context_t ctx, char *line)
4251 struct client_s *client = assuan_get_pointer (ctx);
4252 gpg_error_t rc;
4253 char buf[ASSUAN_LINELENGTH];
4254 struct argv_s *args[] = {
4255 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4256 NULL
4259 rc = parse_options (&line, args, client);
4260 if (rc)
4261 return send_error (ctx, rc);
4263 if (!strcasecmp (line, "clients"))
4265 if (client->opts & OPT_DATA)
4267 MUTEX_LOCK (&cn_mutex);
4268 snprintf (buf, sizeof (buf), "%i", slist_length (cn_thread_list));
4269 MUTEX_UNLOCK (&cn_mutex);
4270 rc = xfer_data (ctx, buf, strlen (buf));
4272 else
4273 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4275 else if (!strcasecmp (line, "cache"))
4277 if (client->opts & OPT_DATA)
4279 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4280 rc = xfer_data (ctx, buf, strlen (buf));
4282 else
4283 rc = send_status (ctx, STATUS_CACHE, NULL);
4285 else if (!strcasecmp (line, "pid"))
4287 char buf[32];
4288 pid_t pid = getpid ();
4290 snprintf (buf, sizeof (buf), "%i", pid);
4291 rc = xfer_data (ctx, buf, strlen (buf));
4293 else if (!strcasecmp (line, "version"))
4295 char *buf = str_asprintf ("0x%06x %s%s", VERSION_HEX,
4296 #ifdef WITH_GNUTLS
4297 "GNUTLS "
4298 #endif
4299 #ifdef WITH_QUALITY
4300 "QUALITY "
4301 #endif
4302 "", use_agent ? "AGENT" : "");
4303 rc = xfer_data (ctx, buf, strlen (buf));
4304 xfree (buf);
4306 else if (!strcasecmp (line, "last_error"))
4308 if (client->last_error)
4309 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4310 else
4311 rc = GPG_ERR_NO_DATA;
4313 else
4314 rc = gpg_error (GPG_ERR_SYNTAX);
4316 return send_error (ctx, rc);
4319 #ifdef WITH_AGENT
4320 static gpg_error_t
4321 send_data_cb (void *user, const void *buf, size_t len)
4323 assuan_context_t ctx = user;
4325 return assuan_send_data (ctx, buf, len);
4328 static gpg_error_t
4329 send_status_cb (void *user, const char *line)
4331 assuan_context_t ctx = user;
4332 char keyword[200], *k;
4333 const char *p;
4335 for (p = line, k = keyword; *p; p++)
4337 if (isspace (*p))
4338 break;
4340 *k++ = *p;
4343 *k = 0;
4344 if (*p == '#')
4345 p++;
4347 while (isspace (*p))
4348 p++;
4350 return assuan_write_status (ctx, keyword, *p ? p : NULL);
4352 #endif
4354 static gpg_error_t
4355 agent_command (assuan_context_t ctx, char *line)
4357 gpg_error_t rc = 0;
4359 if (!use_agent)
4360 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
4362 #ifdef WITH_AGENT
4363 struct client_s *client = assuan_get_pointer (ctx);
4365 if (!line || !*line)
4366 return send_error (ctx, GPG_ERR_SYNTAX);
4368 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 1);
4369 rc = assuan_transact (client->crypto->agent->ctx, line, send_data_cb,
4370 client->ctx, agent_loopback_cb, client->crypto,
4371 send_status_cb, client->ctx);
4372 if (gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
4374 char *line;
4375 size_t len;
4377 rc = assuan_write_line (client->crypto->agent->ctx, "CAN");
4378 if (!rc)
4380 rc = assuan_read_line (client->crypto->agent->ctx, &line, &len);
4381 if (!rc)
4382 rc = gpg_error (GPG_ERR_ASS_CANCELED);
4386 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 0);
4387 #endif
4388 return send_error (ctx, rc);
4391 void
4392 init_commands ()
4394 /* !BEGIN-HELP-TEXT!
4396 * This comment is used as a marker to generate the offline documentation
4397 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4398 * script to determine where commands begin and end.
4400 new_command("HELP", 1, 1, help_command, _(
4401 "HELP [<COMMAND>]\n"
4402 "Show available commands or command specific help text."
4405 new_command("AGENT", 1, 1, agent_command, _(
4406 "AGENT <command>\n"
4407 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4408 "@command{gpg-agent}."
4411 new_command("GETINFO", 1, 1, getinfo_command, _(
4412 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4413 "Get server and other information: @var{cache} returns the number of cached "
4414 "documents via a status message. @var{clients} returns the number of "
4415 "connected clients via a status message. @var{pid} returns the process ID "
4416 "number of the server via a data response. @var{VERSION} returns the server "
4417 "version number and compile-time features with a data response with each "
4418 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4419 "the last failed command when available. @xref{Status Messages}. "
4420 "\n"
4421 "When the @option{--data} option is specified then the result will be sent "
4422 "via a data response rather than a status message."
4425 new_command("PASSWD", 0, 0, passwd_command, _(
4426 "PASSWD [--reset] [--s2k-count=N] [--no-passphrase]\n"
4427 "Changes the passphrase of the secret key required to open the current "
4428 "file or the passphrase of a symmetrically encrypted data file. When the "
4429 "@option{--reset} option is passed then the cache entry for the current "
4430 "file will be reset and the passphrase, if any, will be required during the "
4431 "next @code{OPEN} (@pxref{OPEN})."
4432 "\n"
4433 "The @option{--s2k-count} option sets or changes (@pxref{SAVE}) the number "
4434 "of hash iterations for a passphrase and must be either @code{0} to use "
4435 "the calibrated count of the machine (the default), or a value greater than "
4436 "or equal to @code{65536}. This option has no effect for symmetrically "
4437 "encrypted data files."
4438 "\n"
4439 "The @option{--no-passphrase} option will prevent requiring a passphrase for "
4440 "the data file, although a passphrase may be required when changing it."
4443 new_command("KEYGRIP", 1, 1, keygrip_command, _(
4444 "KEYGRIP [--sign] <filename>\n"
4445 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4446 "data response."
4447 "\n"
4448 "When the @option{--sign} option is specified then the key used for signing "
4449 "of the specified @var{filename} will be returned."
4450 "\n"
4451 "For symmetrically encrypted data files this command returns the error "
4452 "GPG_ERR_NOT_SUPPORTED."
4455 new_command("OPEN", 1, 1, open_command, _(
4456 "OPEN [--lock] <filename> [<passphrase>]\n"
4457 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4458 "found on the file-system then a new document will be created. If the file "
4459 "is found, it is looked for in the file cache. If cached and no "
4460 "@var{passphrase} was specified then the cached document is opened. When not "
4461 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4462 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4463 "specified."
4464 "\n"
4465 "When the @option{--lock} option is passed then the file mutex will be "
4466 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4467 "file has been opened."
4470 new_command("SAVE", 0, 0, save_command, _(
4471 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring] [--sign-keygrip=hexstring]\n"
4472 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4473 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4474 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4475 "keypair will be generated and a pinentry will be used to prompt for the "
4476 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4477 "passed in which case the data file will not be passphrase protected. "
4478 "\n"
4479 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4480 "passphrase retrieval and caching of new files when @command{gpg-agent} "
4481 "use is enabled. The datafile will be symmetrically encrypted and will not "
4482 "use or generate any keypair."
4483 "\n"
4484 "The @option{--reset} option will clear the cache entry for the current file "
4485 "and require a passphrase, if needed, before saving."
4486 "\n"
4487 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4488 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4489 "(@pxref{Configuration}) for available ciphers."
4490 "\n"
4491 "The @option{--cipher-iterations} option specifies the number of times to "
4492 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4493 "\n"
4494 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4495 "the client to obtain the key paramaters to use when generating a new "
4496 "keypair. The inquired data is expected to be an S-expression. If not "
4497 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4498 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4499 "that when this option is specified a new keypair will be generated "
4500 "reguardless if the file is a new one and that if the data file is protected "
4501 "the passphrase to open it will be required before generating the new keypair."
4502 "\n"
4503 "You can encrypt the data file to a public key other than the one that it "
4504 "was originally encrypted with by passing the @option{--keygrip} option with "
4505 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4506 "be of any key that @command{gpg-agent} knows about. The "
4507 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4508 "secret key. Use the @code{KEYGRIP} (@pxref{KEYGRIP}) command to obtain the "
4509 "keygrip of an existing data file. This option may be needed when using a "
4510 "smartcard. This option has no effect with symmetrically encrypted data files."
4511 "\n"
4512 "The @option{--s2k-count} option sets number of hash iterations for a "
4513 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4514 "value and is the default. This setting only affects new files. To change "
4515 "the setting use the @code{PASSWD} command (@pxref{PASSWD}). This option "
4516 "has no effect with symmetrically encrypted data files."
4519 new_command("ISCACHED", 1, 0, iscached_command, _(
4520 "ISCACHED [--lock] <filename>\n"
4521 "An @emph{OK} response is returned if the specified @var{filename} is found "
4522 "in the file cache. If not found in the cache but exists on the filesystem "
4523 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4524 "returned."
4525 "\n"
4526 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4527 "file exists; it does not need to be opened nor cached. The lock will be "
4528 "released when the client exits or sends the @code{UNLOCK} (@pxref{UNLOCK}) "
4529 "command."
4532 new_command("CLEARCACHE", 1, 1, clearcache_command, _(
4533 "CLEARCACHE [<filename>]\n"
4534 "Clears a file cache entry for all or the specified @var{filename}."
4537 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command, _(
4538 "CACHETIMEOUT <filename> <seconds>\n"
4539 "The time in @var{seconds} until @var{filename} will be removed from the "
4540 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4541 "the passphrase for each @code{OPEN} or @code{SAVE} command (@pxref{OPEN}, "
4542 "@pxref{SAVE}). @xref{Configuration}, and the @code{cache_timeout} "
4543 "parameter."
4546 new_command("LIST", 0, 1, list_command, _(
4547 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4548 "If no element path is given then a newline separated list of root elements "
4549 "is returned with a data response. If given, then all reachable elements "
4550 "of the specified element path are returned unless the @option{--no-recurse} "
4551 "option is specified. If specified, only the child elements of the element "
4552 "path are returned without recursing into grandchildren. Each resulting "
4553 "element is prefixed with the literal @code{!} character when the element "
4554 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4555 "\n"
4556 "When the @option{--verbose} option is passed then each element path "
4557 "returned will have zero or more flags appened to it. These flags are "
4558 "delimited from the element path by a single space character. A flag itself "
4559 "is a single character. Flag @code{P} indicates that access to the element "
4560 "is denied. Flag @code{+} indicates that there are child nodes of "
4561 "the current element path. Flag @code{E} indicates that an element of an "
4562 "element path contained in a @var{target} attribute could not be found. Flag "
4563 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4564 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4565 "of the @var{target} attribute contained in the current element (see below)."
4566 "\n"
4567 "The @option{--with-target} option implies @option{--verbose} and will append "
4568 "an additional flag @code{T} followed by a single space then an element path. "
4569 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4570 "current element when it contains a @var{target} attribute. When no "
4571 "@var{target} attribute is found then no flag will be appended."
4572 "\n"
4573 "The @option{--no-recurse} option limits the amount of data returned to only "
4574 "the listing of children of the specified element path and not any "
4575 "grandchildren."
4576 "\n"
4577 "The @option{--all} option lists the entire element tree for each root "
4578 "element. This option also implies option @option{--verbose}."
4579 "\n"
4580 "When the @option{--inquire} option is passed then all remaining non-option "
4581 "arguments are retrieved via a server @emph{INQUIRE}."
4584 new_command("REALPATH", 0, 1, realpath_command, _(
4585 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4586 "Resolves all @code{target} attributes of the specified element path and "
4587 "returns the result with a data response. @xref{Target Attribute}, for details."
4588 "\n"
4589 "When the @option{--inquire} option is passed then all remaining non-option "
4590 "arguments are retrieved via a server @emph{INQUIRE}."
4593 new_command("STORE", 0, 1, store_command, _(
4594 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4595 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4596 "\n"
4597 "Creates a new element path or modifies the @var{content} of an existing "
4598 "element. If only a single element is specified then a new root element is "
4599 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4600 "set to the final @key{TAB} delimited element. If no @var{content} is "
4601 "specified after the final @key{TAB}, then the content of an existing "
4602 "element will be removed; or empty when creating a new element."
4603 "\n"
4604 "The only restriction of an element name is that it not contain whitespace "
4605 "or begin with the literal element character @code{!} unless specifying a "
4606 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4607 "the @key{TAB} delimited elements. It is recommended that the content of an "
4608 "element be base64 encoded when it contains control or @key{TAB} characters "
4609 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
4612 new_command("RENAME", 0, 1, rename_command, _(
4613 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4614 "Renames the specified @var{element} to the new @var{value}. If an element of "
4615 "the same name as the @var{value} already exists it will be overwritten."
4616 "\n"
4617 "When the @option{--inquire} option is passed then all remaining non-option "
4618 "arguments are retrieved via a server @emph{INQUIRE}."
4621 new_command("COPY", 0, 1, copy_command, _(
4622 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4623 "Copies the entire element tree starting from the child node of the source "
4624 "element, to the destination element path. If the destination element path "
4625 "does not exist then it will be created; otherwise it is overwritten."
4626 "\n"
4627 "Note that attributes from the source element are merged into the "
4628 "destination element when the destination element path exists. When an "
4629 "attribute of the same name exists in both the source and destination "
4630 "elements then the destination attribute will be updated to the source "
4631 "attribute value."
4632 "\n"
4633 "When the @option{--inquire} option is passed then all remaining non-option "
4634 "arguments are retrieved via a server @emph{INQUIRE}."
4637 new_command("MOVE", 0, 1, move_command, _(
4638 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4639 "Moves the source element path to the destination element path. If the "
4640 "destination is not specified then it will be moved to the root node of the "
4641 "document. If the destination is specified and exists then it will be "
4642 "overwritten; otherwise non-existing elements of the destination element "
4643 "path will be created."
4644 "\n"
4645 "When the @option{--inquire} option is passed then all remaining non-option "
4646 "arguments are retrieved via a server @emph{INQUIRE}."
4649 new_command("DELETE", 0, 1, delete_command, _(
4650 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4651 "Removes the specified element path and all of its children. This may break "
4652 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4653 "refers to this element or any of its children."
4654 "\n"
4655 "When the @option{--inquire} option is passed then all remaining non-option "
4656 "arguments are retrieved via a server @emph{INQUIRE}."
4659 new_command("GET", 0, 1, get_command, _(
4660 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4661 "Retrieves the content of the specified element. The content is returned "
4662 "with a data response."
4663 "\n"
4664 "When the @option{--inquire} option is passed then all remaining non-option "
4665 "arguments are retrieved via a server @emph{INQUIRE}."
4668 new_command("ATTR", 0, 1, attr_command, _(
4669 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4670 "@table @asis\n"
4671 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4672 "\n"
4673 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4674 "element. When no @var{value} is specified any existing value will be removed."
4675 "\n"
4676 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4677 "\n"
4678 " Removes an @var{attribute} from an element."
4679 "\n"
4680 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4681 "\n"
4682 " Retrieves a newline separated list of attributes names and values "
4683 "from the specified element. Each attribute name and value is space delimited."
4684 "\n"
4685 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4686 "\n"
4687 " Retrieves the value of an @var{attribute} from an element."
4688 "@end table\n"
4689 "\n"
4690 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4691 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4692 "commands instead."
4693 "\n"
4694 "The @code{_mtime} attribute is updated each time an element is modified by "
4695 "either storing content, editing attributes or by deleting a child element. "
4696 "The @code{_ctime} attribute is created for each new element in an element "
4697 "path."
4698 "\n"
4699 "When the @option{--inquire} option is passed then all remaining non-option "
4700 "arguments are retrieved via a server @emph{INQUIRE}."
4701 "\n"
4702 "@xref{Target Attribute}, for details about this special attribute."
4705 new_command("XPATH", 0, 1, xpath_command, _(
4706 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4707 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4708 "specified it is assumed the expression is a request to return a result. "
4709 "Otherwise, the result is set to the @var{value} argument and the document is "
4710 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4711 "is assumed to be empty and the document is updated. For example:"
4712 "@sp 1\n"
4713 "@example\n"
4714 "XPATH //element[@@_name='password']@key{TAB}\n"
4715 "@end example\n"
4716 "@sp 1"
4717 "would clear the content of all @code{password} elements in the data file "
4718 "while leaving off the trailing @key{TAB} would return all @code{password} "
4719 "elements in @abbr{XML} format."
4720 "\n"
4721 "When the @option{--inquire} option is passed then all remaining non-option "
4722 "arguments are retrieved via a server @emph{INQUIRE}."
4723 "\n"
4724 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4725 "expression syntax."
4728 new_command("XPATHATTR", 0, 1, xpathattr_command, _(
4729 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4730 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4731 "attributes and does not return a result. For the @var{SET} operation the "
4732 "@var{value} is optional but the field is required. If not specified then "
4733 "the attribute value will be empty. For example:"
4734 "@sp 1"
4735 "@example\n"
4736 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4737 "@end example\n"
4738 "@sp 1"
4739 "would create an @code{password} attribute for each @code{password} element "
4740 "found in the document. The attribute value will be empty but still exist."
4741 "\n"
4742 "When the @option{--inquire} option is passed then all remaining non-option "
4743 "arguments are retrieved via a server @emph{INQUIRE}."
4744 "\n"
4745 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4746 "expression syntax."
4749 new_command("IMPORT", 0, 1, import_command, _(
4750 "IMPORT [--root [!]element[<TAB>[!]child[..]]] <content>\n"
4751 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4752 "\n"
4753 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4754 "argument is raw @abbr{XML} data. The content is created as a child of "
4755 "the element path specified with the @option{--root} option or at the "
4756 "document root when not specified. Existing elements of the same name will "
4757 "be overwritten."
4758 "\n"
4759 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4760 "for details."
4763 new_command("DUMP", 0, 1, dump_command, _(
4764 "DUMP\n"
4765 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4766 "dumping a specific node."
4769 new_command("LOCK", 0, 0, lock_command, _(
4770 "LOCK\n"
4771 "Locks the mutex associated with the opened file. This prevents other clients "
4772 "from sending commands to the same opened file until the client "
4773 "that sent this command either disconnects or sends the @code{UNLOCK} "
4774 "command. @xref{UNLOCK}."
4777 new_command("UNLOCK", 1, 0, unlock_command, _(
4778 "UNLOCK\n"
4779 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4780 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4781 "@pxref{ISCACHED})."
4784 new_command("GETCONFIG", 1, 1, getconfig_command, _(
4785 "GETCONFIG [filename] <parameter>\n"
4786 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4787 "data response. If no file has been opened then the value for @var{filename} "
4788 "or the default from the @samp{global} section will be returned. If a file "
4789 "has been opened and no @var{filename} is specified, a value previously "
4790 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4793 new_command("OPTION", 1, 1, option_command, _(
4794 "OPTION <NAME>=<VALUE>\n"
4795 "Sets a client option @var{name} to @var{value}. The value for an option is "
4796 "kept for the duration of the connection."
4797 "\n"
4798 "@table @asis\n"
4799 "@item DISABLE-PINENTRY\n"
4800 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
4801 "server inquire is sent to the client to obtain the passphrase. This option "
4802 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4803 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
4804 "\n"
4805 "@item PINENTRY-TIMEOUT\n"
4806 "Sets the number of seconds before a pinentry prompt will return an error "
4807 "while waiting for user input."
4808 "\n"
4809 "@item TTYNAME\n"
4810 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4811 "\n"
4812 "@item TTYTYPE\n"
4813 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4814 "\n"
4815 "@item DISPLAY\n"
4816 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4817 "\n"
4818 "@item PINENTRY-DESC\n"
4819 "Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4820 "\n"
4821 "@item PINENTRY-TITLE\n"
4822 "Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
4823 "\n"
4824 "@item PINENTRY-PROMPT\n"
4825 "Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
4826 "\n"
4827 "@item LC-CTYPE\n"
4828 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4829 "\n"
4830 "@item LC-MESSAGES\n"
4831 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4832 "\n"
4833 "@item NAME\n"
4834 "Associates the thread ID of the connection with the specified textual "
4835 "representation. Useful for debugging log messages."
4836 "\n"
4837 "@item LOCK-TIMEOUT\n"
4838 "When not @code{0}, the duration in tenths of a second to wait for the file "
4839 "mutex which has been locked by another thread to be released before returning "
4840 "an error. When @code{-1}, then an error will be returned immediately."
4841 "\n"
4842 "@item LOG-LEVEL\n"
4843 "An integer specifiying the logging level."
4844 "@end table\n"
4847 new_command("LS", 1, 1, ls_command, _(
4848 "LS\n"
4849 "Lists the available data files stored in the data directory "
4850 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4853 new_command("RESET", 1, 1, NULL, _(
4854 "RESET\n"
4855 "Closes the currently opened file but keeps any previously set client options."
4858 new_command("NOP", 1, 1, NULL, _(
4859 "NOP\n"
4860 "Does nothing. Always returns successfully."
4863 /* !END-HELP-TEXT! */
4864 new_command ("CANCEL", 1, 1, NULL, NULL);
4865 new_command ("END", 1, 1, NULL, NULL);
4866 new_command ("BYE", 1, 1, NULL, NULL);
4868 int i;
4869 for (i = 0; command_table[i]; i++);
4870 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4871 sort_commands);
4874 gpg_error_t
4875 register_commands (assuan_context_t ctx)
4877 int i = 0, rc;
4879 for (; command_table[i]; i++)
4881 if (!command_table[i]->handler)
4882 continue;
4884 rc = assuan_register_command (ctx, command_table[i]->name,
4885 command_table[i]->handler,
4886 command_table[i]->help);
4887 if (rc)
4888 return rc;
4891 rc = assuan_register_bye_notify (ctx, bye_notify);
4892 if (rc)
4893 return rc;
4895 rc = assuan_register_reset_notify (ctx, reset_notify);
4896 if (rc)
4897 return rc;
4899 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4900 if (rc)
4901 return rc;
4903 return assuan_register_post_cmd_notify (ctx, command_finalize);