Check permissions before DELETE.
[pwmd.git] / src / commands.c
blobd0deed79c641533dd5c0f7529cbb20d1f1a35f9f
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 = peer_is_self (client);
1114 if (rc == GPG_ERR_EPERM)
1115 rc = is_element_owner (client, n);
1117 if (!rc)
1119 rc = unlink_node (n);
1120 xmlFreeNode (n);
1124 strv_free (req);
1125 return rc;
1128 n = find_elements (client, client->doc, n->children, req + 1, &rc, NULL,
1129 NULL, NULL, 0, 0, NULL, 0);
1130 strv_free (req);
1131 if (!n)
1132 return rc;
1134 rc = peer_is_self (client);
1135 if (rc == GPG_ERR_EPERM)
1136 rc = is_element_owner (client, n);
1138 if (!rc)
1140 rc = unlink_node (n);
1141 xmlFreeNode (n);
1144 return rc;
1147 static gpg_error_t
1148 delete_command (assuan_context_t ctx, char *line)
1150 struct client_s *client = assuan_get_pointer (ctx);
1151 gpg_error_t rc;
1152 struct argv_s *args[] = {
1153 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1154 NULL
1157 rc = parse_options (&line, args, client);
1158 if (rc)
1159 return send_error (ctx, rc);
1161 if (client->opts & OPT_INQUIRE)
1163 unsigned char *result;
1164 size_t len;
1166 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1167 if (rc)
1168 return send_error (ctx, rc);
1170 line = (char *) result;
1173 rc = do_delete (ctx, line);
1175 if (client->opts & OPT_INQUIRE)
1176 xfree (line);
1178 return send_error (ctx, rc);
1181 static gpg_error_t
1182 store_command (assuan_context_t ctx, char *line)
1184 struct client_s *client = assuan_get_pointer (ctx);
1185 gpg_error_t rc;
1186 size_t len;
1187 unsigned char *result;
1188 char **req;
1189 xmlNodePtr n, parent;
1190 int has_content;
1191 char *content = NULL;
1193 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1194 if (rc)
1195 return send_error (ctx, rc);
1197 req = str_split ((char *) result, "\t", 0);
1198 xfree (result);
1200 if (!req || !*req)
1201 return send_error (ctx, GPG_ERR_SYNTAX);
1203 len = strv_length (req);
1204 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1205 if (*(req + 1) && !valid_element_path (req, has_content))
1207 strv_free (req);
1208 return send_error (ctx, GPG_ERR_INV_VALUE);
1211 if (has_content || !*req[len - 1])
1213 has_content = 1;
1214 content = req[len - 1];
1215 req[len - 1] = NULL;
1218 again:
1219 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1220 if (rc && rc == GPG_ERR_ELEMENT_NOT_FOUND)
1222 rc = new_root_element (client, client->doc, *req);
1223 if (rc)
1225 strv_free (req);
1226 return send_error (ctx, rc);
1229 goto again;
1232 if (!n)
1234 strv_free (req);
1235 return send_error (ctx, rc);
1238 parent = n;
1240 if (req[1] && *req[1])
1242 if (!n->children)
1243 parent = create_elements_cb (client, n, req + 1, &rc, NULL);
1244 else
1245 parent = find_elements (client, client->doc, n->children, req + 1, &rc,
1246 NULL, NULL, create_elements_cb, 0, 0, NULL,
1250 if (!rc && len > 1)
1252 n = find_text_node (parent->children);
1253 if (n)
1254 xmlNodeSetContent (n, (xmlChar *) content);
1255 else
1256 xmlNodeAddContent (parent, (xmlChar *) content);
1258 update_element_mtime (parent);
1261 xfree (content);
1262 strv_free (req);
1263 return send_error (ctx, rc);
1266 static gpg_error_t
1267 xfer_data (assuan_context_t ctx, const char *line, int total)
1269 int to_send;
1270 int sent = 0;
1271 gpg_error_t rc;
1272 int progress = config_get_integer ("global", "xfer_progress");
1273 int flush = 0;
1275 progress =
1276 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1277 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1278 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1280 if (rc)
1281 return rc;
1283 again:
1286 if (sent + to_send > total)
1287 to_send = total - sent;
1289 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1290 flush ? 0 : to_send);
1291 if (!rc)
1293 sent += flush ? 0 : to_send;
1295 if ((progress && !(sent % progress) && sent != total) ||
1296 (sent == total && flush))
1297 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1299 if (!flush && !rc && sent == total)
1301 flush = 1;
1302 goto again;
1306 while (!rc && sent < total);
1308 return rc;
1311 static gpg_error_t
1312 do_get (assuan_context_t ctx, char *line)
1314 struct client_s *client = assuan_get_pointer (ctx);
1315 gpg_error_t rc;
1316 char **req;
1317 xmlNodePtr n;
1319 req = str_split (line, "\t", 0);
1321 if (!req || !*req)
1323 strv_free (req);
1324 return GPG_ERR_SYNTAX;
1327 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1328 if (!n)
1330 strv_free (req);
1331 return rc;
1334 if (req[1])
1336 find_elements (client, client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1337 0, 0, NULL, 0);
1339 strv_free (req);
1340 if (rc)
1341 return rc;
1343 if (!n || !n->children)
1344 return GPG_ERR_NO_DATA;
1346 n = find_text_node (n->children);
1347 if (!n || !n->content || !*n->content)
1348 return GPG_ERR_NO_DATA;
1350 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1351 return rc;
1354 static gpg_error_t
1355 get_command (assuan_context_t ctx, char *line)
1357 struct client_s *client = assuan_get_pointer (ctx);
1358 gpg_error_t rc;
1359 struct argv_s *args[] = {
1360 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1361 NULL
1364 rc = parse_options (&line, args, client);
1365 if (rc)
1366 return send_error (ctx, rc);
1368 if (client->opts & OPT_INQUIRE)
1370 unsigned char *result;
1371 size_t len;
1373 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1374 if (rc)
1375 return send_error (ctx, rc);
1377 line = (char *) result;
1380 rc = do_get (ctx, line);
1382 if (client->opts & OPT_INQUIRE)
1383 xfree (line);
1385 return send_error (ctx, rc);
1388 static void list_command_cleanup1 (void *arg);
1389 static gpg_error_t
1390 realpath_command (assuan_context_t ctx, char *line)
1392 struct string_s *string = NULL;
1393 gpg_error_t rc;
1394 struct client_s *client = assuan_get_pointer (ctx);
1395 struct argv_s *args[] = {
1396 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1397 NULL
1400 rc = parse_options (&line, args, client);
1401 if (rc)
1402 return send_error (ctx, rc);
1404 if (client->opts & OPT_INQUIRE)
1406 unsigned char *result;
1407 size_t len;
1409 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1410 if (rc)
1411 return send_error (ctx, rc);
1413 line = (char *) result;
1416 rc = build_realpath (client, client->doc, line, &string);
1417 if (!rc)
1419 pthread_cleanup_push (list_command_cleanup1, string);
1420 rc = xfer_data (ctx, string->str, string->len);
1421 pthread_cleanup_pop (1);
1424 if (client->opts & OPT_INQUIRE)
1425 xfree (line);
1427 return send_error (ctx, rc);
1430 static void
1431 list_command_cleanup1 (void *arg)
1433 if (arg)
1434 string_free ((struct string_s *) arg, 1);
1437 static void
1438 list_command_cleanup2 (void *arg)
1440 struct element_list_s *elements = arg;
1442 if (elements)
1444 if (elements->list)
1446 int total = slist_length (elements->list);
1447 int i;
1449 for (i = 0; i < total; i++)
1451 char *tmp = slist_nth_data (elements->list, i);
1452 xfree (tmp);
1455 slist_free (elements->list);
1458 if (elements->prefix)
1459 xfree (elements->prefix);
1461 if (elements->req)
1462 strv_free (elements->req);
1464 xfree (elements);
1468 static gpg_error_t
1469 parse_list_opt_norecurse (void *data, void *value)
1471 struct client_s *client = data;
1473 client->opts &= ~(OPT_LIST_RECURSE);
1474 return 0;
1477 static gpg_error_t
1478 parse_list_opt_verbose (void *data, void *value)
1480 struct client_s *client = data;
1482 client->opts |= OPT_LIST_VERBOSE;
1483 return 0;
1486 static gpg_error_t
1487 parse_list_opt_target (void *data, void *value)
1489 struct client_s *client = data;
1491 client->opts |= OPT_LIST_WITH_TARGET;
1492 return 0;
1495 static gpg_error_t
1496 parse_list_opt_all (void *data, void *value)
1498 struct client_s *client = data;
1500 client->opts |= OPT_LIST_ALL;
1501 return 0;
1504 static gpg_error_t
1505 list_path_once (struct client_s *client, char *line,
1506 struct element_list_s *elements, struct string_s *result)
1508 gpg_error_t rc;
1510 elements->req = str_split (line, " ", 0);
1511 if (!elements->req)
1512 strv_printf (&elements->req, "%s", line);
1514 rc = create_path_list (client, client->doc, elements, *elements->req);
1515 if ((rc == GPG_ERR_ELOOP || rc == GPG_ERR_EPERM) && elements->verbose)
1516 rc = 0;
1518 if (!rc)
1520 int total = slist_length (elements->list);
1522 if (!total)
1523 rc = GPG_ERR_NO_DATA;
1525 if (!rc)
1527 if (!rc)
1529 int i;
1531 for (i = 0; i < total; i++)
1533 char *tmp = slist_nth_data (elements->list, i);
1535 string_append_printf (result, "%s%s", tmp,
1536 i + 1 == total ? "" : "\n");
1540 else
1541 rc = GPG_ERR_NO_DATA;
1544 return rc;
1547 static int
1548 has_list_flag (char *path, char *flags)
1550 char *p = path;
1552 while (*p && *++p != ' ');
1554 if (!*p)
1555 return 0;
1557 for (; *p; p++)
1559 char *f;
1561 for (f = flags; *f && *f != ' '; f++)
1563 if (*p == *f)
1564 return 1;
1568 return 0;
1571 static gpg_error_t
1572 do_list (assuan_context_t ctx, char *line)
1574 struct client_s *client = assuan_get_pointer (ctx);
1575 gpg_error_t rc;
1576 struct element_list_s *elements = NULL;
1578 elements = xcalloc (1, sizeof (struct element_list_s));
1579 if (!elements)
1580 return GPG_ERR_ENOMEM;
1582 elements->recurse = client->opts & OPT_LIST_RECURSE;
1583 elements->verbose =
1584 (client->opts & OPT_LIST_VERBOSE) | (client->opts & OPT_LIST_WITH_TARGET);
1585 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1587 if (!line || !*line)
1589 struct string_s *str = NULL;
1591 pthread_cleanup_push (list_command_cleanup2, elements);
1592 rc = list_root_elements (client, client->doc, &str, elements->verbose,
1593 elements->with_target);
1594 pthread_cleanup_pop (1);
1595 pthread_cleanup_push (list_command_cleanup1, str);
1597 if (!rc)
1599 if (client->opts & OPT_LIST_ALL)
1601 char **roots = str_split (str->str, "\n", 0);
1602 char **p;
1604 pthread_cleanup_push (req_cleanup, roots);
1605 string_truncate (str, 0);
1607 for (p = roots; *p; p++)
1609 if (strchr (*p, ' '))
1611 if (has_list_flag (*p, "EOP"))
1613 string_append_printf (str, "%s%s", *p,
1614 *(p + 1) ? "\n" : "");
1615 continue;
1619 elements = xcalloc (1, sizeof (struct element_list_s));
1620 if (!elements)
1622 rc = GPG_ERR_ENOMEM;
1623 break;
1626 elements->recurse = client->opts & OPT_LIST_RECURSE;
1627 elements->verbose =
1628 (client->opts & OPT_LIST_VERBOSE) | (client->opts &
1629 OPT_LIST_WITH_TARGET);
1630 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1631 pthread_cleanup_push (list_command_cleanup2, elements);
1632 rc = list_path_once (client, *p, elements, str);
1633 pthread_cleanup_pop (1);
1634 if (rc)
1635 break;
1637 if (*(p + 1))
1638 string_append (str, "\n");
1641 pthread_cleanup_pop (1);
1644 if (!rc)
1645 rc = xfer_data (ctx, str->str, str->len);
1648 pthread_cleanup_pop (1);
1649 return rc;
1652 pthread_cleanup_push (list_command_cleanup2, elements);
1653 struct string_s *str = string_new (NULL);
1654 pthread_cleanup_push (list_command_cleanup1, str);
1655 rc = list_path_once (client, line, elements, str);
1656 if (!rc)
1657 rc = xfer_data (ctx, str->str, str->len);
1659 pthread_cleanup_pop (1);
1660 pthread_cleanup_pop (1);
1661 return rc;
1664 static gpg_error_t
1665 list_command (assuan_context_t ctx, char *line)
1667 struct client_s *client = assuan_get_pointer (ctx);
1668 gpg_error_t rc;
1669 struct argv_s *args[] = {
1670 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG,
1671 parse_list_opt_norecurse},
1672 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_list_opt_verbose},
1673 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1674 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG,
1675 parse_list_opt_target},
1676 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_all},
1677 NULL
1680 if (disable_list_and_dump == 1)
1681 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1683 client->opts |= OPT_LIST_RECURSE;
1684 rc = parse_options (&line, args, client);
1685 if (rc)
1686 return send_error (ctx, rc);
1688 if (client->opts & OPT_LIST_ALL)
1689 client->opts |= OPT_LIST_RECURSE | OPT_LIST_VERBOSE;
1691 if (client->opts & OPT_INQUIRE)
1693 unsigned char *result;
1694 size_t len;
1696 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1697 if (rc)
1698 return send_error (ctx, rc);
1700 line = (char *) result;
1703 rc = do_list (ctx, line);
1705 if (client->opts & OPT_INQUIRE)
1706 xfree (line);
1708 return send_error (ctx, rc);
1712 * req[0] - element path
1714 static gpg_error_t
1715 attribute_list (assuan_context_t ctx, char **req)
1717 struct client_s *client = assuan_get_pointer (ctx);
1718 char **attrlist = NULL;
1719 int i = 0;
1720 char **path = NULL;
1721 xmlAttrPtr a;
1722 xmlNodePtr n, an;
1723 char *line;
1724 gpg_error_t rc;
1726 if (!req || !req[0])
1727 return GPG_ERR_SYNTAX;
1729 if ((path = str_split (req[0], "\t", 0)) == NULL)
1732 * The first argument may be only a root element.
1734 if ((path = str_split (req[0], " ", 0)) == NULL)
1735 return GPG_ERR_SYNTAX;
1738 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1740 if (!n)
1742 strv_free (path);
1743 return rc;
1746 if (path[1])
1748 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1749 NULL, NULL, NULL, 0, 0, NULL, 0);
1751 if (!n)
1753 strv_free (path);
1754 return rc;
1758 strv_free (path);
1760 for (a = n->properties; a; a = a->next)
1762 char **pa;
1764 if ((pa = xrealloc (attrlist, (i + 2) * sizeof (char *))) == NULL)
1766 if (attrlist)
1767 strv_free (attrlist);
1769 log_write ("%s(%i): %s", __FILE__, __LINE__,
1770 pwmd_strerror (GPG_ERR_ENOMEM));
1771 return GPG_ERR_ENOMEM;
1774 attrlist = pa;
1775 an = a->children;
1776 attrlist[i] = str_asprintf ("%s %s", (char *) a->name,
1778 && an->content ? (char *) an->content : "");
1780 if (!attrlist[i])
1782 strv_free (attrlist);
1783 log_write ("%s(%i): %s", __FILE__, __LINE__,
1784 pwmd_strerror (GPG_ERR_ENOMEM));
1785 return GPG_ERR_ENOMEM;
1788 attrlist[++i] = NULL;
1791 if (!attrlist)
1792 return GPG_ERR_NO_DATA;
1794 line = strv_join ("\n", attrlist);
1796 if (!line)
1798 log_write ("%s(%i): %s", __FILE__, __LINE__,
1799 pwmd_strerror (GPG_ERR_ENOMEM));
1800 strv_free (attrlist);
1801 return GPG_ERR_ENOMEM;
1804 pthread_cleanup_push (xfree, line);
1805 pthread_cleanup_push (req_cleanup, attrlist);
1806 rc = xfer_data (ctx, line, strlen (line));
1807 pthread_cleanup_pop (1);
1808 pthread_cleanup_pop (1);
1809 return rc;
1813 * req[0] - attribute
1814 * req[1] - element path
1816 static gpg_error_t
1817 attribute_delete (struct client_s *client, char **req)
1819 xmlNodePtr n;
1820 char **path = NULL;
1821 gpg_error_t rc;
1823 if (!req || !req[0] || !req[1])
1824 return GPG_ERR_SYNTAX;
1826 if (!strcmp (req[0], "_name"))
1827 return GPG_ERR_INV_ATTR;
1829 if ((path = str_split (req[1], "\t", 0)) == NULL)
1832 * The first argument may be only a root element.
1834 if ((path = str_split (req[1], " ", 0)) == NULL)
1835 return GPG_ERR_SYNTAX;
1838 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1839 if (!n)
1840 goto fail;
1842 if (path[1])
1844 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1845 NULL, NULL, NULL, 0, 0, NULL, 0);
1846 if (!n)
1847 goto fail;
1850 if (!strcmp (req[0], (char *) "_acl"))
1852 rc = peer_is_self (client);
1853 if (rc == GPG_ERR_EPERM)
1854 rc = is_element_owner (client, n);
1856 if (rc)
1857 goto fail;
1860 rc = delete_attribute (client, n, (xmlChar *) req[0]);
1862 fail:
1863 strv_free (path);
1864 return rc;
1867 static xmlNodePtr
1868 create_element_path (struct client_s *client,
1869 char ***elements, gpg_error_t * rc, xmlNodePtr parent)
1871 char **req = *elements;
1872 char **req_orig = strv_dup (req);
1873 xmlNodePtr n = NULL;
1875 *rc = 0;
1877 if (!req_orig)
1879 *rc = GPG_ERR_ENOMEM;
1880 log_write ("%s(%i): %s", __FILE__, __LINE__,
1881 pwmd_strerror (GPG_ERR_ENOMEM));
1882 goto fail;
1885 again:
1886 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
1887 if (!n)
1889 if (*rc != GPG_ERR_ELEMENT_NOT_FOUND)
1890 goto fail;
1892 *rc = new_root_element (client, client->doc, req[0]);
1893 if (*rc)
1894 goto fail;
1896 goto again;
1898 else if (n == parent)
1900 *rc = GPG_ERR_CONFLICT;
1901 goto fail;
1904 if (req[1])
1906 if (!n->children)
1907 n = create_target_elements_cb (client, n, req + 1, rc, NULL);
1908 else
1909 n = find_elements (client, client->doc, n->children, req + 1, rc,
1910 NULL, NULL, create_target_elements_cb, 0, 0,
1911 parent, 0);
1913 if (!n)
1914 goto fail;
1917 * Reset the position of the element tree now that the elements
1918 * have been created.
1920 strv_free (req);
1921 req = req_orig;
1922 req_orig = NULL;
1923 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
1924 if (!n)
1925 goto fail;
1927 n = find_elements (client, client->doc, n->children, req + 1, rc,
1928 NULL, NULL, NULL, 0, 0, NULL, 0);
1929 if (!n)
1930 goto fail;
1933 fail:
1934 if (req_orig)
1935 strv_free (req_orig);
1937 *elements = req;
1938 return n;
1942 * Creates a "target" attribute. When other commands encounter an element with
1943 * this attribute, the element path is modified to the target value. If the
1944 * source element path doesn't exist when using 'ATTR SET target', it is
1945 * created, but the destination element path must exist.
1947 * req[0] - source element path
1948 * req[1] - destination element path
1950 static gpg_error_t
1951 target_attribute (struct client_s *client, char **req)
1953 char **src, **dst, *line = NULL, **odst = NULL;
1954 gpg_error_t rc;
1955 xmlNodePtr n;
1957 if (!req || !req[0] || !req[1])
1958 return GPG_ERR_SYNTAX;
1960 if ((src = str_split (req[0], "\t", 0)) == NULL)
1963 * The first argument may be only a root element.
1965 if ((src = str_split (req[0], " ", 0)) == NULL)
1966 return GPG_ERR_SYNTAX;
1969 if (!valid_element_path (src, 0))
1970 return GPG_ERR_INV_VALUE;
1972 if ((dst = str_split (req[1], "\t", 0)) == NULL)
1975 * The first argument may be only a root element.
1977 if ((dst = str_split (req[1], " ", 0)) == NULL)
1979 rc = GPG_ERR_SYNTAX;
1980 goto fail;
1984 odst = strv_dup (dst);
1985 if (!odst)
1987 rc = GPG_ERR_ENOMEM;
1988 goto fail;
1991 n = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
1993 * Make sure the destination element path exists.
1995 if (!n)
1996 goto fail;
1998 if (dst[1])
2000 n = find_elements (client, client->doc, n->children, dst + 1, &rc,
2001 NULL, NULL, NULL, 0, 0, NULL, 0);
2002 if (!n)
2003 goto fail;
2006 rc = validate_target_attribute (client, client->doc, req[0], n);
2007 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2008 goto fail;
2010 n = create_element_path (client, &src, &rc, NULL);
2011 if (rc)
2012 goto fail;
2014 line = strv_join ("\t", odst);
2015 if (!line)
2017 rc = GPG_ERR_ENOMEM;
2018 goto fail;
2021 rc = add_attribute (n, "target", line);
2023 fail:
2024 xfree (line);
2025 strv_free (src);
2026 strv_free (dst);
2027 strv_free (odst);
2028 return rc;
2032 * req[0] - attribute
2033 * req[1] - element path
2035 static gpg_error_t
2036 attribute_get (assuan_context_t ctx, char **req)
2038 struct client_s *client = assuan_get_pointer (ctx);
2039 xmlNodePtr n;
2040 xmlChar *a;
2041 char **path = NULL;
2042 gpg_error_t rc;
2044 if (!req || !req[0] || !req[1])
2045 return GPG_ERR_SYNTAX;
2047 if (strchr (req[1], '\t'))
2049 if ((path = str_split (req[1], "\t", 0)) == NULL)
2050 return GPG_ERR_SYNTAX;
2052 else
2054 if ((path = str_split (req[1], " ", 0)) == NULL)
2055 return GPG_ERR_SYNTAX;
2058 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2060 if (!n)
2061 goto fail;
2063 if (path[1])
2065 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2066 NULL, NULL, NULL, 0, 0, NULL, 0);
2068 if (!n)
2069 goto fail;
2072 strv_free (path);
2074 if ((a = xmlGetProp (n, (xmlChar *) req[0])) == NULL)
2075 return GPG_ERR_NOT_FOUND;
2077 pthread_cleanup_push (xmlFree, a);
2079 if (*a)
2080 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
2081 else
2082 rc = GPG_ERR_NO_DATA;
2084 pthread_cleanup_pop (1);
2085 return rc;
2087 fail:
2088 strv_free (path);
2089 return rc;
2093 * req[0] - attribute
2094 * req[1] - element path
2095 * req[2] - value
2097 static gpg_error_t
2098 attribute_set (struct client_s *client, char **req)
2100 char **path = NULL;
2101 gpg_error_t rc;
2102 xmlNodePtr n;
2104 if (!req || !req[0] || !req[1])
2105 return GPG_ERR_SYNTAX;
2108 * Reserved attribute names.
2110 if (!strcmp (req[0], "_name"))
2111 return GPG_ERR_INV_ATTR;
2112 else if (!strcmp (req[0], "target"))
2113 return target_attribute (client, req + 1);
2114 else if (!valid_xml_attribute (req[0]))
2115 return GPG_ERR_INV_VALUE;
2117 if ((path = str_split (req[1], "\t", 0)) == NULL)
2120 * The first argument may be only a root element.
2122 if ((path = str_split (req[1], " ", 0)) == NULL)
2123 return GPG_ERR_SYNTAX;
2126 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2128 if (!n)
2129 goto fail;
2131 if (path[1])
2133 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2134 NULL, NULL, NULL, 0, 0, NULL, 0);
2136 if (!n)
2137 goto fail;
2140 if (!strcmp (req[0], (char *) "_acl"))
2142 rc = peer_is_self (client);
2143 if (rc == GPG_ERR_EPERM)
2144 rc = is_element_owner (client, n);
2146 if (rc)
2147 goto fail;
2150 rc = add_attribute (n, req[0], req[2]);
2152 fail:
2153 strv_free (path);
2154 return rc;
2158 * req[0] - command
2159 * req[1] - attribute name or element path if command is LIST
2160 * req[2] - element path
2161 * req[2] - element path or value
2164 static gpg_error_t
2165 do_attr (assuan_context_t ctx, char *line)
2167 struct client_s *client = assuan_get_pointer (ctx);
2168 gpg_error_t rc = 0;
2169 char **req;
2171 req = str_split (line, " ", 4);
2172 if (!req || !req[0] || !req[1])
2174 strv_free (req);
2175 return GPG_ERR_SYNTAX;
2178 pthread_cleanup_push (req_cleanup, req);
2180 if (strcasecmp (req[0], "SET") == 0)
2181 rc = attribute_set (client, req + 1);
2182 else if (strcasecmp (req[0], "GET") == 0)
2183 rc = attribute_get (ctx, req + 1);
2184 else if (strcasecmp (req[0], "DELETE") == 0)
2185 rc = attribute_delete (client, req + 1);
2186 else if (strcasecmp (req[0], "LIST") == 0)
2187 rc = attribute_list (ctx, req + 1);
2188 else
2189 rc = GPG_ERR_SYNTAX;
2191 pthread_cleanup_pop (1);
2192 return rc;
2195 static gpg_error_t
2196 attr_command (assuan_context_t ctx, char *line)
2198 struct client_s *client = assuan_get_pointer (ctx);
2199 gpg_error_t rc;
2200 struct argv_s *args[] = {
2201 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2202 NULL
2205 rc = parse_options (&line, args, client);
2206 if (rc)
2207 return send_error (ctx, rc);
2209 if (client->opts & OPT_INQUIRE)
2211 unsigned char *result;
2212 size_t len;
2214 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2215 if (rc)
2216 return send_error (ctx, rc);
2218 line = (char *) result;
2221 rc = do_attr (ctx, line);
2223 if (client->opts & OPT_INQUIRE)
2224 xfree (line);
2226 return send_error (ctx, rc);
2229 static gpg_error_t
2230 parse_iscached_opt_lock (void *data, void *value)
2232 struct client_s *client = data;
2234 (void) value;
2235 client->opts |= OPT_LOCK;
2236 return 0;
2239 static gpg_error_t
2240 iscached_command (assuan_context_t ctx, char *line)
2242 struct client_s *client = assuan_get_pointer (ctx);
2243 gpg_error_t rc;
2244 struct argv_s *args[] = {
2245 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2246 NULL
2249 if (!line || !*line)
2250 return send_error (ctx, GPG_ERR_SYNTAX);
2252 rc = parse_options (&line, args, client);
2253 if (rc)
2254 return send_error (ctx, rc);
2255 else if (!valid_filename (line))
2256 return send_error (ctx, GPG_ERR_INV_VALUE);
2258 rc = cache_iscached (line, NULL);
2259 if (client->opts & OPT_LOCK
2260 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2262 unsigned char md5file[16];
2263 gpg_error_t trc = rc;
2265 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2266 if (memcmp (md5file, client->md5file, 16))
2267 cleanup_client (client);
2269 memcpy (client->md5file, md5file, 16);
2270 rc = do_lock (client, 1);
2271 if (!rc)
2272 rc = trc;
2275 return send_error (ctx, rc);
2278 static gpg_error_t
2279 clearcache_command (assuan_context_t ctx, char *line)
2281 gpg_error_t rc = 0, all_rc = 0;
2282 unsigned char md5file[16];
2283 int i;
2284 int t;
2285 int all = 0;
2286 struct client_thread_s *once = NULL;
2288 cache_lock ();
2289 MUTEX_LOCK (&cn_mutex);
2290 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
2292 if (!line || !*line)
2293 all = 1;
2295 t = slist_length (cn_thread_list);
2297 for (i = 0; i < t; i++)
2299 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2300 assuan_peercred_t peer;
2302 if (!thd->cl)
2303 continue;
2305 /* Lock each connected clients' file mutex to prevent any other client
2306 * from accessing the cache entry (the file mutex is locked upon
2307 * command startup). The cache for the entry is not cleared if the
2308 * file mutex is locked by another client to prevent this function
2309 * from blocking.
2311 if (all)
2313 if (thd->cl->filename)
2315 rc = do_validate_peer (ctx, thd->cl->filename, &peer);
2316 all_rc = !all_rc ? rc : all_rc;
2317 if (rc)
2319 rc = 0;
2320 continue;
2324 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
2325 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2327 if (pthread_equal (pthread_self (), thd->tid))
2328 rc = 0;
2329 else
2331 if (!thd->cl->filename ||
2332 cache_iscached (thd->cl->filename,
2333 NULL) == GPG_ERR_NO_DATA)
2335 rc = 0;
2336 continue;
2339 cache_defer_clear (thd->cl->md5file);
2342 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2344 rc = 0;
2345 continue;
2348 if (!rc)
2350 rc = cache_clear (thd->cl->md5file);
2351 cache_unlock_mutex (thd->cl->md5file, 0);
2354 if (rc)
2355 all_rc = rc;
2357 rc = 0;
2359 /* A single data filename was specified. Lock only this data file
2360 * mutex and free the cache entry. */
2361 else
2363 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2364 rc = do_validate_peer (ctx, line, &peer);
2366 if (!rc && !memcmp (thd->cl->md5file, md5file, sizeof (md5file)))
2368 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0,
2369 -1);
2370 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2372 if (pthread_equal (pthread_self (), thd->tid))
2373 rc = 0;
2376 if (!rc)
2378 once = thd;
2379 rc = cache_clear (thd->cl->md5file);
2380 cache_unlock_mutex (thd->cl->md5file, 0);
2382 else
2384 cache_defer_clear (thd->cl->md5file);
2387 break;
2392 /* Only connected clients' cache entries have been cleared. Now clear any
2393 * remaining cache entries without clients but only if there wasn't an
2394 * error from above since this would defeat the locking check of the
2395 * remaining entries. */
2396 if (!all_rc && all)
2398 cache_clear (NULL);
2401 /* No clients are using the specified file. */
2402 else if (!all_rc && !rc && !once)
2404 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2405 rc = cache_clear (md5file);
2408 /* Release the connection mutex. */
2409 pthread_cleanup_pop (1);
2410 cache_unlock ();
2412 if (!rc)
2413 send_status_all (STATUS_CACHE, NULL);
2415 /* One or more files were locked while clearing all cache entries. */
2416 if (all_rc)
2417 rc = all_rc;
2419 return send_error (ctx, rc);
2422 static gpg_error_t
2423 cachetimeout_command (assuan_context_t ctx, char *line)
2425 int timeout;
2426 char **req = str_split (line, " ", 0);
2427 char *p;
2428 gpg_error_t rc = 0;
2429 assuan_peercred_t peer;
2431 if (!req || !*req || !req[1])
2433 strv_free (req);
2434 return send_error (ctx, GPG_ERR_SYNTAX);
2437 errno = 0;
2438 timeout = (int) strtol (req[1], &p, 10);
2439 if (errno != 0 || *p || timeout < -1)
2441 strv_free (req);
2442 return send_error (ctx, GPG_ERR_SYNTAX);
2445 rc = do_validate_peer (ctx, req[0], &peer);
2446 if (!rc)
2448 unsigned char md5file[16];
2450 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, req[0], strlen (req[0]));
2451 rc = cache_set_timeout (md5file, timeout);
2452 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2454 rc = 0;
2455 MUTEX_LOCK (&rcfile_mutex);
2456 config_set_int_param (&global_config, req[0], "cache_timeout",
2457 req[1]);
2458 MUTEX_UNLOCK (&rcfile_mutex);
2462 strv_free (req);
2463 return send_error (ctx, rc);
2466 static gpg_error_t
2467 dump_command (assuan_context_t ctx, char *line)
2469 xmlChar *xml;
2470 int len;
2471 struct client_s *client = assuan_get_pointer (ctx);
2472 gpg_error_t rc;
2474 if (disable_list_and_dump == 1)
2475 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2477 rc = peer_is_self(client);
2478 if (rc)
2479 return send_error (ctx, rc);
2481 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2483 if (!xml)
2485 log_write ("%s(%i): %s", __FILE__, __LINE__,
2486 pwmd_strerror (GPG_ERR_ENOMEM));
2487 return send_error (ctx, GPG_ERR_ENOMEM);
2490 pthread_cleanup_push (xmlFree, xml);
2491 rc = xfer_data (ctx, (char *) xml, len);
2492 pthread_cleanup_pop (1);
2493 return send_error (ctx, rc);
2496 static gpg_error_t
2497 getconfig_command (assuan_context_t ctx, char *line)
2499 struct client_s *client = assuan_get_pointer (ctx);
2500 gpg_error_t rc = 0;
2501 char filename[255] = { 0 }, param[747] = { 0 };
2502 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2504 if (!line || !*line)
2505 return send_error (ctx, GPG_ERR_SYNTAX);
2507 if (strchr (line, ' '))
2509 sscanf (line, " %254[^ ] %746c", filename, param);
2510 paramp = param;
2511 fp = filename;
2514 if (fp && !valid_filename (fp))
2515 return send_error (ctx, GPG_ERR_INV_VALUE);
2517 paramp = str_down (paramp);
2518 if (!strcmp (paramp, "cipher") && fp)
2520 struct crypto_s *crypto = NULL;
2522 rc = init_client_crypto (&crypto);
2523 if (!rc)
2525 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2526 if (!rc)
2528 const char *t =
2529 gcry_cipher_algo_name (cipher_to_gcrypt (crypto->hdr.flags));
2530 if (t)
2532 tmp = str_dup (t);
2533 if (tmp)
2534 str_down (tmp);
2539 UPDATE_AGENT_CTX (client, crypto);
2540 cleanup_crypto (&crypto);
2541 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2542 return send_error (ctx, rc);
2544 if (!rc && tmp)
2545 goto done;
2547 else if (!strcmp (paramp, "cipher_iterations") && fp)
2549 struct crypto_s *crypto = NULL;
2551 rc = init_client_crypto (&crypto);
2552 if (!rc)
2554 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2555 if (!rc)
2557 tmp = str_asprintf ("%llu",
2558 (unsigned long long) crypto->hdr.
2559 iterations);
2560 if (!tmp)
2561 rc = GPG_ERR_ENOMEM;
2565 UPDATE_AGENT_CTX (client, crypto);
2566 cleanup_crypto (&crypto);
2567 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2568 return send_error (ctx, rc);
2570 if (!rc && tmp)
2571 goto done;
2573 else if (!strcmp (paramp, "passphrase"))
2574 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2576 p = config_get_value (fp ? fp : "global", paramp);
2577 if (!p)
2578 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2580 tmp = expand_homedir (p);
2581 xfree (p);
2582 if (!tmp)
2584 log_write ("%s(%i): %s", __FILE__, __LINE__,
2585 pwmd_strerror (GPG_ERR_ENOMEM));
2586 return send_error (ctx, GPG_ERR_ENOMEM);
2589 done:
2590 p = tmp;
2591 pthread_cleanup_push (xfree, p);
2592 rc = xfer_data (ctx, p, strlen (p));
2593 pthread_cleanup_pop (1);
2594 return send_error (ctx, rc);
2597 struct xpath_s
2599 xmlXPathContextPtr xp;
2600 xmlXPathObjectPtr result;
2601 xmlBufferPtr buf;
2602 char **req;
2605 static void
2606 xpath_command_cleanup (void *arg)
2608 struct xpath_s *xpath = arg;
2610 if (!xpath)
2611 return;
2613 req_cleanup (xpath->req);
2615 if (xpath->buf)
2616 xmlBufferFree (xpath->buf);
2618 if (xpath->result)
2619 xmlXPathFreeObject (xpath->result);
2621 if (xpath->xp)
2622 xmlXPathFreeContext (xpath->xp);
2625 static gpg_error_t
2626 do_xpath (assuan_context_t ctx, char *line)
2628 gpg_error_t rc;
2629 struct client_s *client = assuan_get_pointer (ctx);
2630 struct xpath_s _x = { 0 };
2631 struct xpath_s *xpath = &_x;
2633 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2635 if (strv_printf (&xpath->req, "%s", line) == 0)
2636 return GPG_ERR_ENOMEM;
2639 xpath->xp = xmlXPathNewContext (client->doc);
2640 if (!xpath->xp)
2642 rc = GPG_ERR_BAD_DATA;
2643 goto fail;
2646 xpath->result =
2647 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2648 if (!xpath->result)
2650 rc = GPG_ERR_BAD_DATA;
2651 goto fail;
2654 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2656 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2657 goto fail;
2660 rc = recurse_xpath_nodeset (client, client->doc, xpath->result->nodesetval,
2661 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2662 NULL);
2663 if (rc)
2664 goto fail;
2665 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2667 rc = GPG_ERR_NO_DATA;
2668 goto fail;
2670 else if (xpath->req[1])
2672 rc = 0;
2673 goto fail;
2676 pthread_cleanup_push (xpath_command_cleanup, &xpath);
2677 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2678 xmlBufferLength (xpath->buf));
2679 pthread_cleanup_pop (0);
2680 fail:
2681 xpath_command_cleanup (xpath);
2682 return rc;
2685 static gpg_error_t
2686 xpath_command (assuan_context_t ctx, char *line)
2688 struct client_s *client = assuan_get_pointer (ctx);
2689 gpg_error_t rc;
2690 struct argv_s *args[] = {
2691 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2692 NULL
2695 if (disable_list_and_dump == 1)
2696 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2698 rc = peer_is_self(client);
2699 if (rc)
2700 return send_error (ctx, rc);
2702 rc = parse_options (&line, args, client);
2703 if (rc)
2704 return send_error (ctx, rc);
2706 if (client->opts & OPT_INQUIRE)
2708 unsigned char *result;
2709 size_t len;
2711 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2712 if (rc)
2713 return send_error (ctx, rc);
2715 line = (char *) result;
2718 if (!line || !*line)
2719 rc = GPG_ERR_SYNTAX;
2721 if (!rc)
2722 rc = do_xpath (ctx, line);
2724 if (client->opts & OPT_INQUIRE)
2725 xfree (line);
2727 return send_error (ctx, rc);
2730 static gpg_error_t
2731 do_xpathattr (assuan_context_t ctx, char *line)
2733 struct client_s *client = assuan_get_pointer (ctx);
2734 gpg_error_t rc;
2735 char **req = NULL;
2736 int cmd = 0; //SET
2737 struct xpath_s _x = { 0 };
2738 struct xpath_s *xpath = &_x;
2740 if ((req = str_split (line, " ", 3)) == NULL)
2741 return GPG_ERR_ENOMEM;
2743 if (!req[0])
2745 rc = GPG_ERR_SYNTAX;
2746 goto fail;
2749 if (!strcasecmp (req[0], "SET"))
2750 cmd = 0;
2751 else if (!strcasecmp (req[0], "DELETE"))
2752 cmd = 1;
2753 else
2755 rc = GPG_ERR_SYNTAX;
2756 goto fail;
2759 if (!req[1] || !req[2])
2761 rc = GPG_ERR_SYNTAX;
2762 goto fail;
2765 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2767 rc = GPG_ERR_ENOMEM;
2768 goto fail;
2771 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2773 rc = GPG_ERR_SYNTAX;
2774 goto fail;
2777 xpath->xp = xmlXPathNewContext (client->doc);
2778 if (!xpath->xp)
2780 rc = GPG_ERR_BAD_DATA;
2781 goto fail;
2784 xpath->result = xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2785 if (!xpath->result)
2787 rc = GPG_ERR_BAD_DATA;
2788 goto fail;
2791 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2793 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2794 goto fail;
2797 rc = recurse_xpath_nodeset (client, client->doc, xpath->result->nodesetval,
2798 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2799 (xmlChar *) req[1]);
2801 fail:
2802 xpath_command_cleanup (xpath);
2803 strv_free (req);
2804 return rc;
2807 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2808 static gpg_error_t
2809 xpathattr_command (assuan_context_t ctx, char *line)
2811 struct client_s *client = assuan_get_pointer (ctx);
2812 gpg_error_t rc;
2813 struct argv_s *args[] = {
2814 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2815 NULL
2818 if (disable_list_and_dump == 1)
2819 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2821 rc = peer_is_self(client);
2822 if (rc)
2823 return send_error (ctx, rc);
2825 rc = parse_options (&line, args, client);
2826 if (rc)
2827 return send_error (ctx, rc);
2829 if (client->opts & OPT_INQUIRE)
2831 unsigned char *result;
2832 size_t len;
2834 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2835 if (rc)
2836 return send_error (ctx, rc);
2838 line = (char *) result;
2841 if (!line || !*line)
2842 rc = GPG_ERR_SYNTAX;
2844 if (!rc)
2845 rc = do_xpathattr (ctx, line);
2847 if (client->opts & OPT_INQUIRE)
2848 xfree (line);
2850 return send_error (ctx, rc);
2853 static gpg_error_t
2854 do_import (struct client_s *client, const char *root_element,
2855 unsigned char *content)
2857 char **dst_path = NULL;
2858 xmlDocPtr doc = NULL;
2859 xmlNodePtr n, root, copy;
2860 gpg_error_t rc;
2862 if (!content || !*content)
2864 xfree (content);
2865 return GPG_ERR_SYNTAX;
2868 if (root_element)
2869 dst_path = str_split (root_element, "\t", 0);
2871 if (dst_path && !valid_element_path (dst_path, 0))
2873 if (dst_path)
2874 strv_free (dst_path);
2876 return GPG_ERR_INV_VALUE;
2879 struct string_s *str = string_new_content ((char *)content);
2880 str = string_prepend (str, "<pwmd>");
2881 str = string_append (str, "</pwmd>");
2882 doc = xmlReadDoc ((xmlChar *) str->str, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2883 string_free (str, 1);
2884 if (!doc)
2886 rc = GPG_ERR_BAD_DATA;
2887 goto fail;
2890 root = xmlDocGetRootElement (doc);
2891 xmlNodePtr root_orig = root->children;
2892 root = root->children;
2893 rc = validate_import (root);
2894 if (rc)
2895 goto fail;
2899 again:
2900 if (dst_path)
2902 char **path = strv_dup (dst_path);
2903 if (!path)
2905 log_write ("%s(%i): %s", __FILE__, __LINE__,
2906 pwmd_strerror (GPG_ERR_ENOMEM));
2907 rc = GPG_ERR_ENOMEM;
2908 goto fail;
2911 xmlChar *a = xmlGetProp (root, (xmlChar *) "_name");
2912 if (!a)
2914 strv_free (path);
2915 rc = GPG_ERR_INV_VALUE;
2916 goto fail;
2919 if (strv_printf (&path, "%s", (char *) a) == 0)
2921 xmlFree (a);
2922 rc = GPG_ERR_ENOMEM;
2923 goto fail;
2926 xmlFree (a);
2927 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2928 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2930 strv_free (path);
2931 goto fail;
2934 if (!rc)
2936 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2937 NULL, NULL, NULL, 0, 0, NULL, 1);
2938 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2940 strv_free (path);
2941 goto fail;
2943 else if (!rc)
2945 xmlUnlinkNode (n);
2946 xmlFreeNode (n);
2947 strv_free (path);
2948 path = NULL;
2949 goto again;
2953 if (rc == GPG_ERR_ELEMENT_NOT_FOUND)
2955 n = create_element_path (client, &path, &rc, NULL);
2956 if (rc)
2957 goto fail;
2960 if (root->children)
2962 copy = xmlCopyNodeList (root->children);
2963 n = xmlAddChildList (n, copy);
2964 if (!n)
2965 rc = GPG_ERR_ENOMEM;
2968 strv_free (path);
2970 else
2972 char **path = NULL;
2974 /* Check if the content root element can create a DTD root element. */
2975 if (!xmlStrEqual ((xmlChar *) "element", root->name))
2977 rc = GPG_ERR_SYNTAX;
2978 goto fail;
2981 xmlChar *a;
2983 if ((a = xmlGetProp (root, (xmlChar *) "_name")) == NULL)
2985 rc = GPG_ERR_SYNTAX;
2986 goto fail;
2989 char *tmp = str_dup ((char *) a);
2990 xmlFree (a);
2991 int literal = is_literal_element (&tmp);
2993 if (!valid_xml_element ((xmlChar *) tmp) || literal)
2995 xfree (tmp);
2996 rc = GPG_ERR_INV_VALUE;
2997 goto fail;
3000 if (strv_printf (&path, "%s", tmp) == 0)
3002 xfree (tmp);
3003 rc = GPG_ERR_ENOMEM;
3004 goto fail;
3007 xfree (tmp);
3008 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 1);
3009 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3011 rc = GPG_ERR_BAD_DATA;
3012 goto fail;
3015 /* Overwriting the existing tree. */
3016 if (!rc)
3018 xmlUnlinkNode (n);
3019 xmlFreeNodeList (n);
3022 rc = 0;
3023 xmlSetProp (root, (xmlChar *) "_name", (xmlChar *) path[0]);
3024 n = xmlCopyNode (root, 1);
3025 n = xmlAddChildList (xmlDocGetRootElement (client->doc), n);
3026 strv_free (path);
3029 if (n && !rc)
3030 rc = update_element_mtime (n->parent);
3032 for (root = root_orig->next; root; root = root->next)
3034 if (root->type == XML_ELEMENT_NODE)
3035 break;
3038 root_orig = root;
3040 while (root);
3042 fail:
3043 if (doc)
3044 xmlFreeDoc (doc);
3046 if (dst_path)
3047 strv_free (dst_path);
3049 return rc;
3052 static gpg_error_t
3053 parse_import_opt_root (void *data, void *value)
3055 struct client_s *client = data;
3057 client->import_root = str_dup (value);
3058 return client->import_root ? 0 : GPG_ERR_ENOMEM;
3061 static gpg_error_t
3062 import_command (assuan_context_t ctx, char *line)
3064 gpg_error_t rc;
3065 struct client_s *client = assuan_get_pointer (ctx);
3066 unsigned char *result;
3067 size_t len;
3068 struct argv_s *args[] = {
3069 &(struct argv_s) {"root", OPTION_TYPE_ARG, parse_import_opt_root},
3070 NULL
3073 xfree (client->import_root);
3074 client->import_root = NULL;
3075 rc = parse_options (&line, args, client);
3076 if (rc)
3077 return send_error (ctx, rc);
3079 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3080 if (rc)
3082 xfree (client->import_root);
3083 client->import_root = NULL;
3084 return send_error (ctx, rc);
3087 rc = do_import (client, client->import_root, result);
3088 xfree (client->import_root);
3089 client->import_root = NULL;
3090 return send_error (ctx, rc);
3093 static gpg_error_t
3094 do_lock (struct client_s *client, int add)
3096 gpg_error_t rc = lock_file_mutex (client, add);
3098 if (!rc)
3099 client->flags |= FLAG_LOCK_CMD;
3101 return rc;
3104 static gpg_error_t
3105 lock_command (assuan_context_t ctx, char *line)
3107 struct client_s *client = assuan_get_pointer (ctx);
3108 gpg_error_t rc = do_lock (client, 0);
3110 return send_error (ctx, rc);
3113 static gpg_error_t
3114 unlock_command (assuan_context_t ctx, char *line)
3116 struct client_s *client = assuan_get_pointer (ctx);
3117 gpg_error_t rc;
3119 rc = unlock_file_mutex (client, 0);
3120 return send_error (ctx, rc);
3123 static gpg_error_t
3124 option_command (assuan_context_t ctx, char *line)
3126 struct client_s *client = assuan_get_pointer (ctx);
3127 gpg_error_t rc = 0;
3128 struct pinentry_option_s *pin_opts = &client->pinentry_opts;
3129 #ifdef WITH_AGENT
3130 struct agent_s *agent = client->crypto->agent;
3131 #endif
3132 char namebuf[255] = { 0 };
3133 char *name = namebuf;
3134 char *value = NULL, *p;
3136 p = strchr (line, '=');
3137 if (!p)
3138 strncpy (namebuf, line, sizeof(namebuf));
3139 else
3141 strncpy (namebuf, line, strlen (line)-strlen (p));
3142 value = p+1;
3145 log_write1 ("OPTION name='%s' value='%s'", name, value);
3147 if (strcasecmp (name, (char *) "log_level") == 0)
3149 long l = 0;
3151 if (value)
3153 l = strtol (value, NULL, 10);
3155 if (l < 0 || l > 2)
3156 return send_error (ctx, GPG_ERR_INV_VALUE);
3159 MUTEX_LOCK (&rcfile_mutex);
3160 config_set_int_param (&global_config, "global", "log_level", value);
3161 MUTEX_UNLOCK (&rcfile_mutex);
3162 goto done;
3164 else if (strcasecmp (name, (char *) "lock-timeout") == 0)
3166 long n = 0;
3167 char *p = NULL;
3169 if (value)
3171 n = strtol (value, &p, 10);
3172 if (p && *p)
3173 return send_error (ctx, GPG_ERR_INV_VALUE);
3176 client->lock_timeout = n;
3177 goto done;
3179 else if (strcasecmp (name, (char *) "NAME") == 0)
3181 char *tmp = pthread_getspecific (thread_name_key);
3183 if (tmp)
3184 xfree (tmp);
3186 if (!value || !*value)
3188 pthread_setspecific (thread_name_key, str_dup (""));
3189 goto done;
3192 pthread_setspecific (thread_name_key, str_dup (value));
3193 goto done;
3195 else if (strcasecmp (name, (char *) "lc-messages") == 0)
3197 xfree (pin_opts->lc_messages);
3198 pin_opts->lc_messages = NULL;
3199 if (value && *value)
3200 pin_opts->lc_messages = str_dup (value);
3201 #ifdef WITH_AGENT
3202 if (use_agent)
3203 rc = set_agent_option (client->crypto->agent, "lc-messages", value);
3204 #endif
3206 else if (strcasecmp (name, (char *) "lc-ctype") == 0)
3208 xfree (pin_opts->lc_ctype);
3209 pin_opts->lc_ctype = NULL;
3210 if (value && *value)
3211 pin_opts->lc_ctype = str_dup (value);
3212 #ifdef WITH_AGENT
3213 if (use_agent)
3214 rc = set_agent_option (client->crypto->agent, "lc-ctype", value);
3215 #endif
3217 else if (strcasecmp (name, (char *) "ttyname") == 0)
3219 xfree (pin_opts->ttyname);
3220 pin_opts->ttyname = NULL;
3221 if (value && *value)
3222 pin_opts->ttyname = str_dup (value);
3223 #ifdef WITH_AGENT
3224 if (use_agent)
3225 rc = set_agent_option (client->crypto->agent, "ttyname", value);
3226 #endif
3228 else if (strcasecmp (name, (char *) "ttytype") == 0)
3230 xfree (pin_opts->ttytype);
3231 pin_opts->ttytype = NULL;
3232 if (value && *value)
3233 pin_opts->ttytype = str_dup (value);
3234 #ifdef WITH_AGENT
3235 if (use_agent)
3236 rc = set_agent_option (client->crypto->agent, "ttytype", value);
3237 #endif
3239 else if (strcasecmp (name, (char *) "display") == 0)
3241 xfree (pin_opts->display);
3242 pin_opts->display = NULL;
3243 if (value && *value)
3244 pin_opts->display = str_dup (value);
3245 #ifdef WITH_AGENT
3246 if (use_agent)
3247 rc = set_agent_option (client->crypto->agent, "display", value);
3248 #endif
3250 else if (strcasecmp (name, (char *) "pinentry-desc") == 0)
3252 xfree (pin_opts->desc);
3253 pin_opts->desc = NULL;
3254 if (value && *value)
3255 pin_opts->desc = str_dup (value);
3257 else if (strcasecmp (name, (char *) "pinentry-title") == 0)
3259 xfree (pin_opts->title);
3260 pin_opts->title = NULL;
3261 if (value && *value)
3262 pin_opts->title = str_dup (value);
3264 else if (strcasecmp (name, (char *) "pinentry-prompt") == 0)
3266 xfree (pin_opts->prompt);
3267 pin_opts->prompt = NULL;
3268 if (value && *value)
3269 pin_opts->prompt = str_dup (value);
3272 else if (strcasecmp (name, "pinentry-timeout") == 0)
3274 char *p = NULL;
3275 int n;
3277 if (!value)
3278 goto done;
3280 n = (int) strtol (value, &p, 10);
3282 if (*p || n < 0)
3283 return send_error (ctx, GPG_ERR_INV_VALUE);
3285 pin_opts->timeout = n;
3286 MUTEX_LOCK (&rcfile_mutex);
3287 config_set_int_param (&global_config,
3288 client->filename ? client->filename : "global",
3289 "pinentry_timeout", value);
3290 MUTEX_UNLOCK (&rcfile_mutex);
3291 goto done;
3293 else if (strcasecmp (name, "disable-pinentry") == 0)
3295 char *p = NULL;
3296 int n = 1;
3298 if (value && *value)
3300 n = (int) strtol (value, &p, 10);
3301 if (*p || n < 0 || n > 1)
3302 return send_error (ctx, GPG_ERR_INV_VALUE);
3305 if (n)
3306 client->flags |= FLAG_NO_PINENTRY;
3307 else
3308 client->flags &= ~FLAG_NO_PINENTRY;
3310 #ifdef WITH_AGENT
3311 if (use_agent)
3313 if (client->flags & FLAG_NO_PINENTRY)
3314 rc = set_agent_option (client->crypto->agent, "pinentry-mode",
3315 "loopback");
3316 else
3317 rc = set_agent_option (client->crypto->agent, "pinentry-mode",
3318 "ask");
3320 if (rc)
3321 return send_error (ctx, rc);
3323 #endif
3325 else
3326 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
3328 done:
3329 #ifdef WITH_AGENT
3330 if (!rc && use_agent && agent)
3332 rc = pinentry_merge_options (&client->crypto->agent->pinentry_opts,
3333 pin_opts);
3335 #endif
3337 return send_error (ctx, rc);
3340 static gpg_error_t
3341 do_rename (assuan_context_t ctx, char *line)
3343 struct client_s *client = assuan_get_pointer (ctx);
3344 gpg_error_t rc;
3345 char **req, **src, *dst;
3346 xmlNodePtr n, ndst;
3348 req = str_split (line, " ", 0);
3350 if (!req || !req[0] || !req[1])
3352 strv_free (req);
3353 return GPG_ERR_SYNTAX;
3356 dst = req[1];
3357 is_literal_element (&dst);
3359 if (!valid_xml_element ((xmlChar *) dst))
3361 strv_free (req);
3362 return GPG_ERR_INV_VALUE;
3365 if (strchr (req[0], '\t'))
3366 src = str_split (req[0], "\t", 0);
3367 else
3368 src = str_split (req[0], " ", 0);
3370 if (!src || !*src)
3372 rc = GPG_ERR_SYNTAX;
3373 goto fail;
3376 n = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3377 if (src[1] && n)
3378 n = find_elements (client, client->doc, n->children, src + 1, &rc, NULL, NULL,
3379 NULL, 0, 0, NULL, 0);
3381 if (!n)
3382 goto fail;
3384 xmlChar *a = xmlGetProp (n, (xmlChar *) "_name");
3385 if (!a)
3387 rc = GPG_ERR_ENOMEM;
3388 goto fail;
3391 /* To prevent unwanted effects:
3393 * <root name="a"><b/></root>
3395 * RENAME a<TAB>b b
3397 if (xmlStrEqual (a, (xmlChar *) dst))
3399 xmlFree (a);
3400 rc = GPG_ERR_AMBIGUOUS_NAME;
3401 goto fail;
3404 xmlFree (a);
3405 char **tmp = NULL;
3406 if (src[1])
3408 char **p;
3410 for (p = src; *p; p++)
3412 if (!*(p + 1))
3413 break;
3414 strv_printf (&tmp, "%s", *p);
3418 strv_printf (&tmp, "!%s", dst);
3419 ndst = find_root_element (client, client->doc, &tmp, &rc, NULL, 0, 0);
3420 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3422 strv_free (tmp);
3423 goto fail;
3426 if (tmp[1] && ndst)
3427 ndst = find_elements (client, client->doc, ndst->children, tmp + 1, &rc, NULL,
3428 NULL, NULL, 0, 0, NULL, 0);
3430 strv_free (tmp);
3431 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3432 goto fail;
3434 rc = 0;
3436 /* Target may exist:
3438 * <root name="a"/>
3439 * <root name="b" target="a"/>
3441 * RENAME b a
3443 * Would need to do:
3444 * RENAME !b a
3446 if (ndst == n)
3448 rc = GPG_ERR_AMBIGUOUS_NAME;
3449 goto fail;
3452 if (ndst)
3454 unlink_node (ndst);
3455 xmlFreeNodeList (ndst);
3458 rc = add_attribute (n, "_name", dst);
3460 fail:
3461 strv_free (req);
3462 strv_free (src);
3463 return rc;
3466 static gpg_error_t
3467 rename_command (assuan_context_t ctx, char *line)
3469 struct client_s *client = assuan_get_pointer (ctx);
3470 gpg_error_t rc;
3471 struct argv_s *args[] = {
3472 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3473 NULL
3476 rc = parse_options (&line, args, client);
3477 if (rc)
3478 return send_error (ctx, rc);
3480 if (client->opts & OPT_INQUIRE)
3482 unsigned char *result;
3483 size_t len;
3485 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3486 if (rc)
3487 return send_error (ctx, rc);
3489 line = (char *) result;
3492 rc = do_rename (ctx, line);
3494 if (client->opts & OPT_INQUIRE)
3495 xfree (line);
3497 return send_error (ctx, rc);
3500 static gpg_error_t
3501 do_copy (assuan_context_t ctx, char *line)
3503 struct client_s *client = assuan_get_pointer (ctx);
3504 gpg_error_t rc;
3505 char **req, **src = NULL, **dst = NULL;
3506 xmlNodePtr nsrc, ndst, new = NULL;
3508 req = str_split (line, " ", 0);
3509 if (!req || !req[0] || !req[1])
3511 strv_free (req);
3512 return GPG_ERR_SYNTAX;
3515 if (strchr (req[0], '\t'))
3516 src = str_split (req[0], "\t", 0);
3517 else
3518 src = str_split (req[0], " ", 0);
3520 if (!src || !*src)
3522 rc = GPG_ERR_SYNTAX;
3523 goto fail;
3526 if (strchr (req[1], '\t'))
3527 dst = str_split (req[1], "\t", 0);
3528 else
3529 dst = str_split (req[1], " ", 0);
3531 if (!dst || !*dst)
3533 rc = GPG_ERR_SYNTAX;
3534 goto fail;
3537 if (!valid_element_path (dst, 0))
3539 rc = GPG_ERR_INV_VALUE;
3540 goto fail;
3543 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3544 if (nsrc && src[1])
3545 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc, NULL,
3546 NULL, NULL, 0, 0, NULL, 0);
3548 if (!nsrc)
3549 goto fail;
3551 new = xmlCopyNodeList (nsrc);
3552 if (!new)
3554 rc = GPG_ERR_ENOMEM;
3555 goto fail;
3558 int create = 0;
3559 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3560 if (ndst && dst[1])
3562 if (ndst->children)
3563 ndst = find_elements (client, client->doc, ndst->children, dst + 1, &rc, NULL,
3564 NULL, create_target_elements_cb, 0, 0, NULL, 0);
3565 else
3566 create = 1;
3568 else
3569 create = 1;
3571 if (!ndst && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3572 goto fail;
3573 else if (!ndst || create)
3575 ndst = create_element_path (client, &dst, &rc, NULL);
3576 if (!ndst)
3577 goto fail;
3580 /* Merge any attributes from the src node to the initial dst node. */
3581 for (xmlAttrPtr attr = new->properties; attr; attr = attr->next)
3583 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3584 continue;
3586 xmlAttrPtr a = xmlHasProp (ndst, attr->name);
3587 if (a)
3588 xmlRemoveProp (a);
3590 xmlChar *tmp = xmlNodeGetContent (attr->children);
3591 xmlNewProp (ndst, attr->name, tmp);
3592 xmlFree (tmp);
3593 rc = add_attribute (ndst, NULL, NULL);
3596 xmlNodePtr n = ndst->children;
3597 xmlUnlinkNode (n);
3598 xmlFreeNodeList (n);
3599 ndst->children = NULL;
3601 if (new->children)
3603 n = xmlCopyNodeList (new->children);
3604 if (!n)
3606 rc = GPG_ERR_ENOMEM;
3607 goto fail;
3610 n = xmlAddChildList (ndst, n);
3611 if (!n)
3613 rc = GPG_ERR_ENOMEM;
3614 goto fail;
3617 rc = update_element_mtime (xmlDocGetRootElement (client->doc) ==
3618 ndst->parent ? ndst : ndst->parent);
3621 fail:
3622 if (new)
3624 xmlUnlinkNode (new);
3625 xmlFreeNodeList (new);
3628 if (req)
3629 strv_free (req);
3631 if (src)
3632 strv_free (src);
3634 if (dst)
3635 strv_free (dst);
3637 return rc;
3640 static gpg_error_t
3641 copy_command (assuan_context_t ctx, char *line)
3643 struct client_s *client = assuan_get_pointer (ctx);
3644 gpg_error_t rc;
3645 struct argv_s *args[] = {
3646 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3647 NULL
3650 rc = parse_options (&line, args, client);
3651 if (rc)
3652 return send_error (ctx, rc);
3654 if (client->opts & OPT_INQUIRE)
3656 unsigned char *result;
3657 size_t len;
3659 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3660 if (rc)
3661 return send_error (ctx, rc);
3663 line = (char *) result;
3666 rc = do_copy (ctx, line);
3668 if (client->opts & OPT_INQUIRE)
3669 xfree (line);
3671 return send_error (ctx, rc);
3674 static gpg_error_t
3675 do_move (assuan_context_t ctx, char *line)
3677 struct client_s *client = assuan_get_pointer (ctx);
3678 gpg_error_t rc;
3679 char **req, **src = NULL, **dst = NULL;
3680 xmlNodePtr nsrc, ndst = NULL;
3682 req = str_split (line, " ", 0);
3684 if (!req || !req[0] || !req[1])
3686 strv_free (req);
3687 return GPG_ERR_SYNTAX;
3690 if (strchr (req[0], '\t'))
3691 src = str_split (req[0], "\t", 0);
3692 else
3693 src = str_split (req[0], " ", 0);
3695 if (!src || !*src)
3697 rc = GPG_ERR_SYNTAX;
3698 goto fail;
3701 if (strchr (req[1], '\t'))
3702 dst = str_split (req[1], "\t", 0);
3703 else
3704 dst = str_split (req[1], " ", 0);
3706 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3707 if (nsrc && src[1])
3708 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc, NULL,
3709 NULL, NULL, 0, 0, NULL, 0);
3711 if (!nsrc)
3712 goto fail;
3714 if (dst)
3716 if (!valid_element_path (dst, 0))
3718 rc = GPG_ERR_INV_VALUE;
3719 goto fail;
3722 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3723 if (ndst && dst[1])
3724 ndst = find_elements (client, client->doc, ndst->children, dst + 1, &rc, NULL,
3725 NULL, NULL, 0, 0, NULL, 0);
3727 else
3728 ndst = xmlDocGetRootElement (client->doc);
3730 for (xmlNodePtr n = ndst; n; n = n->parent)
3732 if (n == nsrc)
3734 rc = GPG_ERR_CONFLICT;
3735 goto fail;
3739 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3740 goto fail;
3742 rc = 0;
3744 if (ndst)
3746 xmlChar *a = node_has_attribute (nsrc, (xmlChar *) "_name");
3748 xmlNodePtr dup = find_element (client, ndst->children, (char *) a,
3749 NULL, &rc);
3750 xmlFree (a);
3752 if (rc)
3753 goto fail;
3755 if (dup)
3757 if (dup == nsrc)
3758 goto fail;
3760 if (ndst == xmlDocGetRootElement (client->doc))
3762 xmlNodePtr n = nsrc;
3763 int match = 0;
3765 while (n->parent && n->parent != ndst)
3766 n = n->parent;
3768 xmlChar *a = node_has_attribute (n, (xmlChar *) "_name");
3769 xmlChar *b = node_has_attribute (nsrc, (xmlChar *) "_name");
3771 if (xmlStrEqual (a, b))
3773 match = 1;
3774 xmlUnlinkNode (nsrc);
3775 xmlUnlinkNode (n);
3776 xmlFreeNodeList (n);
3779 xmlFree (a);
3780 xmlFree (b);
3782 if (!match)
3784 xmlUnlinkNode (dup);
3785 xmlFreeNodeList (dup);
3788 else
3789 xmlUnlinkNode (dup);
3793 if (!ndst && dst)
3795 xmlChar *name = node_has_attribute (nsrc, (xmlChar *) "_name");
3797 if (nsrc->parent == xmlDocGetRootElement (client->doc)
3798 && !strcmp ((char *) name, *dst))
3800 xmlFree (name);
3801 rc = GPG_ERR_CONFLICT;
3802 goto fail;
3805 xmlFree (name);
3806 ndst = create_element_path (client, &dst, &rc, nsrc);
3809 if (!ndst)
3810 goto fail;
3812 update_element_mtime (nsrc->parent);
3813 xmlUnlinkNode (nsrc);
3814 ndst = xmlAddChildList (ndst, nsrc);
3816 if (!ndst)
3817 rc = GPG_ERR_ENOMEM;
3818 else
3819 update_element_mtime (ndst->parent);
3821 fail:
3822 if (req)
3823 strv_free (req);
3825 if (src)
3826 strv_free (src);
3828 if (dst)
3829 strv_free (dst);
3831 return rc;
3834 static gpg_error_t
3835 move_command (assuan_context_t ctx, char *line)
3837 struct client_s *client = assuan_get_pointer (ctx);
3838 gpg_error_t rc;
3839 struct argv_s *args[] = {
3840 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3841 NULL
3844 rc = parse_options (&line, args, client);
3845 if (rc)
3846 return send_error (ctx, rc);
3848 if (client->opts & OPT_INQUIRE)
3850 unsigned char *result;
3851 size_t len;
3853 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3854 if (rc)
3855 return send_error (ctx, rc);
3857 line = (char *) result;
3860 rc = do_move (ctx, line);
3862 if (client->opts & OPT_INQUIRE)
3863 xfree (line);
3865 return send_error (ctx, rc);
3868 static gpg_error_t
3869 ls_command (assuan_context_t ctx, char *line)
3871 gpg_error_t rc;
3872 char *tmp = str_asprintf ("%s/data", homedir);
3873 char *dir = expand_homedir (tmp);
3874 DIR *d = opendir (dir);
3876 rc = gpg_error_from_errno (errno);
3877 xfree (tmp);
3879 if (!d)
3881 xfree (dir);
3882 return send_error (ctx, rc);
3885 size_t len =
3886 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3887 struct dirent *p = xmalloc (len), *cur = NULL;
3888 char *list = NULL;
3890 xfree (dir);
3891 rc = 0;
3893 while (!readdir_r (d, p, &cur) && cur)
3895 if (cur->d_name[0] == '.' && cur->d_name[1] == '\0')
3896 continue;
3897 else if (cur->d_name[0] == '.' && cur->d_name[1] == '.'
3898 && cur->d_name[2] == '\0')
3899 continue;
3901 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3903 if (!tmp)
3905 if (list)
3906 xfree (list);
3908 rc = GPG_ERR_ENOMEM;
3909 break;
3912 xfree (list);
3913 list = tmp;
3916 closedir (d);
3917 xfree (p);
3919 if (rc)
3920 return send_error (ctx, rc);
3922 if (!list)
3923 return send_error (ctx, GPG_ERR_NO_DATA);
3925 list[strlen (list) - 1] = 0;
3926 rc = xfer_data (ctx, list, strlen (list));
3927 xfree (list);
3928 return send_error (ctx, rc);
3931 static gpg_error_t
3932 bye_notify (assuan_context_t ctx, char *line)
3934 struct client_s *cl = assuan_get_pointer (ctx);
3936 #ifdef WITH_GNUTLS
3937 if (cl->thd->remote)
3939 int rc;
3943 struct timeval tv = { 0, 50000 };
3945 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3946 if (rc == GNUTLS_E_AGAIN)
3947 select (0, NULL, NULL, NULL, &tv);
3949 while (rc == GNUTLS_E_AGAIN);
3951 #endif
3953 /* This will let assuan_process_next() return. */
3954 fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK);
3955 cl->last_rc = 0; // BYE command result
3956 return 0;
3959 static gpg_error_t
3960 reset_notify (assuan_context_t ctx, char *line)
3962 struct client_s *client = assuan_get_pointer (ctx);
3964 if (client)
3965 cleanup_client (client);
3967 return 0;
3971 * This is called before every Assuan command.
3973 static gpg_error_t
3974 command_startup (assuan_context_t ctx, const char *name)
3976 struct client_s *client = assuan_get_pointer (ctx);
3977 gpg_error_t rc;
3978 struct command_table_s *cmd = NULL;
3980 log_write1 ("command='%s'", name);
3981 client->last_rc = client->opts = 0;
3983 for (int i = 0; command_table[i]; i++)
3985 if (!strcasecmp (name, command_table[i]->name))
3987 if (command_table[i]->ignore_startup)
3988 return 0;
3989 cmd = command_table[i];
3990 break;
3994 client->last_rc = rc = gpg_error (file_modified (client, cmd));
3995 return rc;
3999 * This is called after every Assuan command.
4001 static void
4002 command_finalize (assuan_context_t ctx, gpg_error_t rc)
4004 struct client_s *client = assuan_get_pointer (ctx);
4006 if (!(client->flags & FLAG_LOCK_CMD))
4007 unlock_file_mutex (client, 0);
4009 log_write1 (_("command completed: rc=%u"), client->last_rc);
4010 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
4011 #ifdef WITH_GNUTLS
4012 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
4013 #endif
4016 static gpg_error_t
4017 help_command (assuan_context_t ctx, char *line)
4019 gpg_error_t rc;
4020 int i;
4022 if (!line || !*line)
4024 char *tmp;
4025 char *help = str_dup (_("Usage: HELP [<COMMAND>]\n"
4026 "For commands that take an element path as an argument, each element is "
4027 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
4028 "\n" "COMMANDS:"));
4030 for (i = 0; command_table[i]; i++)
4032 if (!command_table[i]->help)
4033 continue;
4035 tmp = str_asprintf ("%s %s", help, command_table[i]->name);
4036 xfree (help);
4037 help = tmp;
4040 tmp = strip_texi_and_wrap (help);
4041 xfree (help);
4042 rc = xfer_data (ctx, tmp, strlen (tmp));
4043 xfree (tmp);
4044 return send_error (ctx, rc);
4047 for (i = 0; command_table[i]; i++)
4049 if (!strcasecmp (line, command_table[i]->name))
4051 char *help, *tmp;
4053 if (!command_table[i]->help)
4054 break;
4056 help = strip_texi_and_wrap (command_table[i]->help);
4057 tmp = str_asprintf (_("Usage: %s"), help);
4058 xfree (help);
4059 rc = xfer_data (ctx, tmp, strlen (tmp));
4060 xfree (tmp);
4061 return send_error (ctx, rc);
4065 return send_error (ctx, GPG_ERR_INV_NAME);
4068 static void
4069 new_command (const char *name, int ignore, int unlock,
4070 gpg_error_t (*handler) (assuan_context_t, char *),
4071 const char *help)
4073 int i = 0;
4075 if (command_table)
4076 for (i = 0; command_table[i]; i++);
4078 command_table =
4079 xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
4080 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
4081 command_table[i]->name = name;
4082 command_table[i]->handler = handler;
4083 command_table[i]->ignore_startup = ignore;
4084 command_table[i]->unlock = unlock;
4085 command_table[i++]->help = help;
4086 command_table[i] = NULL;
4089 void
4090 deinit_commands ()
4092 int i;
4094 for (i = 0; command_table[i]; i++)
4095 xfree (command_table[i]);
4097 xfree (command_table);
4100 static int
4101 sort_commands (const void *arg1, const void *arg2)
4103 struct command_table_s *const *a = arg1;
4104 struct command_table_s *const *b = arg2;
4106 if (!*a || !*b)
4107 return 0;
4108 else if (*a && !*b)
4109 return 1;
4110 else if (!*a && *b)
4111 return -1;
4113 return strcmp ((*a)->name, (*b)->name);
4116 static gpg_error_t
4117 passwd_command (assuan_context_t ctx, char *line)
4119 struct client_s *client = assuan_get_pointer (ctx);
4120 gpg_error_t rc;
4121 struct argv_s *args[] = {
4122 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
4123 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
4124 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG, parse_opt_no_passphrase},
4125 NULL
4128 if (client->flags & FLAG_NEW)
4129 return send_error (ctx, GPG_ERR_INV_STATE);
4131 client->crypto->save.s2k_count =
4132 config_get_ulong (client->filename, "s2k_count");
4133 rc = parse_options (&line, args, client);
4134 if (rc)
4135 return send_error (ctx, rc);
4137 if (!rc && client->opts & OPT_RESET)
4139 rc = cache_clear (client->md5file);
4140 if (!rc)
4141 send_status_all (STATUS_CACHE, NULL);
4144 if (!rc)
4146 if (!IS_PKI (client->crypto))
4148 struct crypto_s *crypto;
4150 xfree (client->crypto->filename);
4151 client->crypto->filename = str_dup (client->filename);
4152 rc = change_passwd (ctx, client->filename,
4153 client->flags & FLAG_NO_PINENTRY, &crypto,
4154 (client->opts & OPT_NO_PASSPHRASE));
4155 if (!rc)
4157 cleanup_crypto (&client->crypto);
4158 client->crypto = crypto;
4159 update_checksum (client);
4160 cleanup_crypto_stage1 (client->crypto);
4163 #ifdef WITH_AGENT
4164 else
4166 if (client->crypto->save.s2k_count)
4167 rc = send_to_agent (client->crypto->agent, NULL, NULL,
4168 "OPTION s2k-count=%lu",
4169 client->crypto->save.s2k_count);
4171 if (!rc)
4172 rc = agent_passwd (client->crypto);
4174 #endif
4177 return send_error (ctx, rc);
4180 static gpg_error_t
4181 parse_keygrip_opt_sign (void *data, void *value)
4183 struct client_s *client = data;
4185 (void) value;
4186 client->opts |= OPT_SIGN;
4187 return 0;
4190 static gpg_error_t
4191 keygrip_command (assuan_context_t ctx, char *line)
4193 struct client_s *client = assuan_get_pointer (ctx);
4194 gpg_error_t rc;
4195 struct crypto_s *crypto = NULL;
4196 struct argv_s *args[] = {
4197 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_keygrip_opt_sign},
4198 NULL
4201 if (!line || !*line)
4202 return send_error (ctx, GPG_ERR_SYNTAX);
4204 rc = parse_options (&line, args, client);
4205 if (rc)
4206 return send_error (ctx, rc);
4208 if (!valid_filename (line))
4209 return send_error (ctx, GPG_ERR_INV_VALUE);
4211 rc = init_client_crypto (&crypto);
4212 if (rc)
4213 return send_error (ctx, rc);
4215 rc = read_data_file (line, crypto);
4216 if (!rc)
4218 char *hexgrip = NULL;
4220 if (!IS_PKI (crypto))
4222 cleanup_crypto (&crypto);
4223 return send_error (ctx, GPG_ERR_NOT_SUPPORTED);
4226 if (client->opts & OPT_SIGN)
4228 if (valid_keygrip (crypto->sign_grip, sizeof (crypto->sign_grip)))
4229 hexgrip = bin2hex (crypto->sign_grip, sizeof (crypto->sign_grip));
4232 if (!hexgrip)
4233 hexgrip = bin2hex (crypto->grip, sizeof (crypto->grip));
4235 if (!hexgrip)
4236 rc = GPG_ERR_ENOMEM;
4237 else
4238 rc = xfer_data (ctx, hexgrip, strlen (hexgrip));
4240 xfree (hexgrip);
4243 UPDATE_AGENT_CTX (client, crypto);
4244 cleanup_crypto (&crypto);
4245 return send_error (ctx, rc);
4248 static gpg_error_t
4249 parse_opt_data (void *data, void *value)
4251 struct client_s *client = data;
4253 (void) value;
4254 client->opts |= OPT_DATA;
4255 return 0;
4258 static gpg_error_t
4259 getinfo_command (assuan_context_t ctx, char *line)
4261 struct client_s *client = assuan_get_pointer (ctx);
4262 gpg_error_t rc;
4263 char buf[ASSUAN_LINELENGTH];
4264 struct argv_s *args[] = {
4265 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4266 NULL
4269 rc = parse_options (&line, args, client);
4270 if (rc)
4271 return send_error (ctx, rc);
4273 if (!strcasecmp (line, "clients"))
4275 if (client->opts & OPT_DATA)
4277 MUTEX_LOCK (&cn_mutex);
4278 snprintf (buf, sizeof (buf), "%i", slist_length (cn_thread_list));
4279 MUTEX_UNLOCK (&cn_mutex);
4280 rc = xfer_data (ctx, buf, strlen (buf));
4282 else
4283 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4285 else if (!strcasecmp (line, "cache"))
4287 if (client->opts & OPT_DATA)
4289 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4290 rc = xfer_data (ctx, buf, strlen (buf));
4292 else
4293 rc = send_status (ctx, STATUS_CACHE, NULL);
4295 else if (!strcasecmp (line, "pid"))
4297 char buf[32];
4298 pid_t pid = getpid ();
4300 snprintf (buf, sizeof (buf), "%i", pid);
4301 rc = xfer_data (ctx, buf, strlen (buf));
4303 else if (!strcasecmp (line, "version"))
4305 char *buf = str_asprintf ("0x%06x %s%s", VERSION_HEX,
4306 #ifdef WITH_GNUTLS
4307 "GNUTLS "
4308 #endif
4309 #ifdef WITH_QUALITY
4310 "QUALITY "
4311 #endif
4312 "", use_agent ? "AGENT" : "");
4313 rc = xfer_data (ctx, buf, strlen (buf));
4314 xfree (buf);
4316 else if (!strcasecmp (line, "last_error"))
4318 if (client->last_error)
4319 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4320 else
4321 rc = GPG_ERR_NO_DATA;
4323 else
4324 rc = gpg_error (GPG_ERR_SYNTAX);
4326 return send_error (ctx, rc);
4329 #ifdef WITH_AGENT
4330 static gpg_error_t
4331 send_data_cb (void *user, const void *buf, size_t len)
4333 assuan_context_t ctx = user;
4335 return assuan_send_data (ctx, buf, len);
4338 static gpg_error_t
4339 send_status_cb (void *user, const char *line)
4341 assuan_context_t ctx = user;
4342 char keyword[200], *k;
4343 const char *p;
4345 for (p = line, k = keyword; *p; p++)
4347 if (isspace (*p))
4348 break;
4350 *k++ = *p;
4353 *k = 0;
4354 if (*p == '#')
4355 p++;
4357 while (isspace (*p))
4358 p++;
4360 return assuan_write_status (ctx, keyword, *p ? p : NULL);
4362 #endif
4364 static gpg_error_t
4365 agent_command (assuan_context_t ctx, char *line)
4367 gpg_error_t rc = 0;
4369 if (!use_agent)
4370 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
4372 #ifdef WITH_AGENT
4373 struct client_s *client = assuan_get_pointer (ctx);
4375 if (!line || !*line)
4376 return send_error (ctx, GPG_ERR_SYNTAX);
4378 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 1);
4379 rc = assuan_transact (client->crypto->agent->ctx, line, send_data_cb,
4380 client->ctx, agent_loopback_cb, client->crypto,
4381 send_status_cb, client->ctx);
4382 if (gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
4384 char *line;
4385 size_t len;
4387 rc = assuan_write_line (client->crypto->agent->ctx, "CAN");
4388 if (!rc)
4390 rc = assuan_read_line (client->crypto->agent->ctx, &line, &len);
4391 if (!rc)
4392 rc = gpg_error (GPG_ERR_ASS_CANCELED);
4396 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 0);
4397 #endif
4398 return send_error (ctx, rc);
4401 void
4402 init_commands ()
4404 /* !BEGIN-HELP-TEXT!
4406 * This comment is used as a marker to generate the offline documentation
4407 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4408 * script to determine where commands begin and end.
4410 new_command("HELP", 1, 1, help_command, _(
4411 "HELP [<COMMAND>]\n"
4412 "Show available commands or command specific help text."
4415 new_command("AGENT", 1, 1, agent_command, _(
4416 "AGENT <command>\n"
4417 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4418 "@command{gpg-agent}."
4421 new_command("GETINFO", 1, 1, getinfo_command, _(
4422 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4423 "Get server and other information: @var{cache} returns the number of cached "
4424 "documents via a status message. @var{clients} returns the number of "
4425 "connected clients via a status message. @var{pid} returns the process ID "
4426 "number of the server via a data response. @var{VERSION} returns the server "
4427 "version number and compile-time features with a data response with each "
4428 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4429 "the last failed command when available. @xref{Status Messages}. "
4430 "\n"
4431 "When the @option{--data} option is specified then the result will be sent "
4432 "via a data response rather than a status message."
4435 new_command("PASSWD", 0, 0, passwd_command, _(
4436 "PASSWD [--reset] [--s2k-count=N] [--no-passphrase]\n"
4437 "Changes the passphrase of the secret key required to open the current "
4438 "file or the passphrase of a symmetrically encrypted data file. When the "
4439 "@option{--reset} option is passed then the cache entry for the current "
4440 "file will be reset and the passphrase, if any, will be required during the "
4441 "next @code{OPEN} (@pxref{OPEN})."
4442 "\n"
4443 "The @option{--s2k-count} option sets or changes (@pxref{SAVE}) the number "
4444 "of hash iterations for a passphrase and must be either @code{0} to use "
4445 "the calibrated count of the machine (the default), or a value greater than "
4446 "or equal to @code{65536}. This option has no effect for symmetrically "
4447 "encrypted data files."
4448 "\n"
4449 "The @option{--no-passphrase} option will prevent requiring a passphrase for "
4450 "the data file, although a passphrase may be required when changing it."
4453 new_command("KEYGRIP", 1, 1, keygrip_command, _(
4454 "KEYGRIP [--sign] <filename>\n"
4455 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4456 "data response."
4457 "\n"
4458 "When the @option{--sign} option is specified then the key used for signing "
4459 "of the specified @var{filename} will be returned."
4460 "\n"
4461 "For symmetrically encrypted data files this command returns the error "
4462 "GPG_ERR_NOT_SUPPORTED."
4465 new_command("OPEN", 1, 1, open_command, _(
4466 "OPEN [--lock] <filename> [<passphrase>]\n"
4467 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4468 "found on the file-system then a new document will be created. If the file "
4469 "is found, it is looked for in the file cache. If cached and no "
4470 "@var{passphrase} was specified then the cached document is opened. When not "
4471 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4472 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4473 "specified."
4474 "\n"
4475 "When the @option{--lock} option is passed then the file mutex will be "
4476 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4477 "file has been opened."
4480 new_command("SAVE", 0, 0, save_command, _(
4481 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring] [--sign-keygrip=hexstring]\n"
4482 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4483 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4484 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4485 "keypair will be generated and a pinentry will be used to prompt for the "
4486 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4487 "passed in which case the data file will not be passphrase protected. "
4488 "\n"
4489 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4490 "passphrase retrieval and caching of new files when @command{gpg-agent} "
4491 "use is enabled. The datafile will be symmetrically encrypted and will not "
4492 "use or generate any keypair."
4493 "\n"
4494 "The @option{--reset} option will clear the cache entry for the current file "
4495 "and require a passphrase, if needed, before saving."
4496 "\n"
4497 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4498 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4499 "(@pxref{Configuration}) for available ciphers."
4500 "\n"
4501 "The @option{--cipher-iterations} option specifies the number of times to "
4502 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4503 "\n"
4504 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4505 "the client to obtain the key paramaters to use when generating a new "
4506 "keypair. The inquired data is expected to be an S-expression. If not "
4507 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4508 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4509 "that when this option is specified a new keypair will be generated "
4510 "reguardless if the file is a new one and that if the data file is protected "
4511 "the passphrase to open it will be required before generating the new keypair."
4512 "\n"
4513 "You can encrypt the data file to a public key other than the one that it "
4514 "was originally encrypted with by passing the @option{--keygrip} option with "
4515 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4516 "be of any key that @command{gpg-agent} knows about. The "
4517 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4518 "secret key. Use the @code{KEYGRIP} (@pxref{KEYGRIP}) command to obtain the "
4519 "keygrip of an existing data file. This option may be needed when using a "
4520 "smartcard. This option has no effect with symmetrically encrypted data files."
4521 "\n"
4522 "The @option{--s2k-count} option sets number of hash iterations for a "
4523 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4524 "value and is the default. This setting only affects new files. To change "
4525 "the setting use the @code{PASSWD} command (@pxref{PASSWD}). This option "
4526 "has no effect with symmetrically encrypted data files."
4529 new_command("ISCACHED", 1, 0, iscached_command, _(
4530 "ISCACHED [--lock] <filename>\n"
4531 "An @emph{OK} response is returned if the specified @var{filename} is found "
4532 "in the file cache. If not found in the cache but exists on the filesystem "
4533 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4534 "returned."
4535 "\n"
4536 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4537 "file exists; it does not need to be opened nor cached. The lock will be "
4538 "released when the client exits or sends the @code{UNLOCK} (@pxref{UNLOCK}) "
4539 "command."
4542 new_command("CLEARCACHE", 1, 1, clearcache_command, _(
4543 "CLEARCACHE [<filename>]\n"
4544 "Clears a file cache entry for all or the specified @var{filename}."
4547 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command, _(
4548 "CACHETIMEOUT <filename> <seconds>\n"
4549 "The time in @var{seconds} until @var{filename} will be removed from the "
4550 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4551 "the passphrase for each @code{OPEN} or @code{SAVE} command (@pxref{OPEN}, "
4552 "@pxref{SAVE}). @xref{Configuration}, and the @code{cache_timeout} "
4553 "parameter."
4556 new_command("LIST", 0, 1, list_command, _(
4557 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4558 "If no element path is given then a newline separated list of root elements "
4559 "is returned with a data response. If given, then all reachable elements "
4560 "of the specified element path are returned unless the @option{--no-recurse} "
4561 "option is specified. If specified, only the child elements of the element "
4562 "path are returned without recursing into grandchildren. Each resulting "
4563 "element is prefixed with the literal @code{!} character when the element "
4564 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4565 "\n"
4566 "When the @option{--verbose} option is passed then each element path "
4567 "returned will have zero or more flags appened to it. These flags are "
4568 "delimited from the element path by a single space character. A flag itself "
4569 "is a single character. Flag @code{P} indicates that access to the element "
4570 "is denied. Flag @code{+} indicates that there are child nodes of "
4571 "the current element path. Flag @code{E} indicates that an element of an "
4572 "element path contained in a @var{target} attribute could not be found. Flag "
4573 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4574 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4575 "of the @var{target} attribute contained in the current element (see below)."
4576 "\n"
4577 "The @option{--with-target} option implies @option{--verbose} and will append "
4578 "an additional flag @code{T} followed by a single space then an element path. "
4579 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4580 "current element when it contains a @var{target} attribute. When no "
4581 "@var{target} attribute is found then no flag will be appended."
4582 "\n"
4583 "The @option{--no-recurse} option limits the amount of data returned to only "
4584 "the listing of children of the specified element path and not any "
4585 "grandchildren."
4586 "\n"
4587 "The @option{--all} option lists the entire element tree for each root "
4588 "element. This option also implies option @option{--verbose}."
4589 "\n"
4590 "When the @option{--inquire} option is passed then all remaining non-option "
4591 "arguments are retrieved via a server @emph{INQUIRE}."
4594 new_command("REALPATH", 0, 1, realpath_command, _(
4595 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4596 "Resolves all @code{target} attributes of the specified element path and "
4597 "returns the result with a data response. @xref{Target Attribute}, for details."
4598 "\n"
4599 "When the @option{--inquire} option is passed then all remaining non-option "
4600 "arguments are retrieved via a server @emph{INQUIRE}."
4603 new_command("STORE", 0, 1, store_command, _(
4604 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4605 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4606 "\n"
4607 "Creates a new element path or modifies the @var{content} of an existing "
4608 "element. If only a single element is specified then a new root element is "
4609 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4610 "set to the final @key{TAB} delimited element. If no @var{content} is "
4611 "specified after the final @key{TAB}, then the content of an existing "
4612 "element will be removed; or empty when creating a new element."
4613 "\n"
4614 "The only restriction of an element name is that it not contain whitespace "
4615 "or begin with the literal element character @code{!} unless specifying a "
4616 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4617 "the @key{TAB} delimited elements. It is recommended that the content of an "
4618 "element be base64 encoded when it contains control or @key{TAB} characters "
4619 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
4622 new_command("RENAME", 0, 1, rename_command, _(
4623 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4624 "Renames the specified @var{element} to the new @var{value}. If an element of "
4625 "the same name as the @var{value} already exists it will be overwritten."
4626 "\n"
4627 "When the @option{--inquire} option is passed then all remaining non-option "
4628 "arguments are retrieved via a server @emph{INQUIRE}."
4631 new_command("COPY", 0, 1, copy_command, _(
4632 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4633 "Copies the entire element tree starting from the child node of the source "
4634 "element, to the destination element path. If the destination element path "
4635 "does not exist then it will be created; otherwise it is overwritten."
4636 "\n"
4637 "Note that attributes from the source element are merged into the "
4638 "destination element when the destination element path exists. When an "
4639 "attribute of the same name exists in both the source and destination "
4640 "elements then the destination attribute will be updated to the source "
4641 "attribute value."
4642 "\n"
4643 "When the @option{--inquire} option is passed then all remaining non-option "
4644 "arguments are retrieved via a server @emph{INQUIRE}."
4647 new_command("MOVE", 0, 1, move_command, _(
4648 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4649 "Moves the source element path to the destination element path. If the "
4650 "destination is not specified then it will be moved to the root node of the "
4651 "document. If the destination is specified and exists then it will be "
4652 "overwritten; otherwise non-existing elements of the destination element "
4653 "path will be created."
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("DELETE", 0, 1, delete_command, _(
4660 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4661 "Removes the specified element path and all of its children. This may break "
4662 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4663 "refers to this element or any of its children."
4664 "\n"
4665 "When the @option{--inquire} option is passed then all remaining non-option "
4666 "arguments are retrieved via a server @emph{INQUIRE}."
4669 new_command("GET", 0, 1, get_command, _(
4670 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4671 "Retrieves the content of the specified element. The content is returned "
4672 "with a data response."
4673 "\n"
4674 "When the @option{--inquire} option is passed then all remaining non-option "
4675 "arguments are retrieved via a server @emph{INQUIRE}."
4678 new_command("ATTR", 0, 1, attr_command, _(
4679 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4680 "@table @asis\n"
4681 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4682 "\n"
4683 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4684 "element. When no @var{value} is specified any existing value will be removed."
4685 "\n"
4686 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4687 "\n"
4688 " Removes an @var{attribute} from an element."
4689 "\n"
4690 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4691 "\n"
4692 " Retrieves a newline separated list of attributes names and values "
4693 "from the specified element. Each attribute name and value is space delimited."
4694 "\n"
4695 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4696 "\n"
4697 " Retrieves the value of an @var{attribute} from an element."
4698 "@end table\n"
4699 "\n"
4700 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4701 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4702 "commands instead."
4703 "\n"
4704 "The @code{_mtime} attribute is updated each time an element is modified by "
4705 "either storing content, editing attributes or by deleting a child element. "
4706 "The @code{_ctime} attribute is created for each new element in an element "
4707 "path."
4708 "\n"
4709 "When the @option{--inquire} option is passed then all remaining non-option "
4710 "arguments are retrieved via a server @emph{INQUIRE}."
4711 "\n"
4712 "@xref{Target Attribute}, for details about this special attribute."
4715 new_command("XPATH", 0, 1, xpath_command, _(
4716 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4717 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4718 "specified it is assumed the expression is a request to return a result. "
4719 "Otherwise, the result is set to the @var{value} argument and the document is "
4720 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4721 "is assumed to be empty and the document is updated. For example:"
4722 "@sp 1\n"
4723 "@example\n"
4724 "XPATH //element[@@_name='password']@key{TAB}\n"
4725 "@end example\n"
4726 "@sp 1"
4727 "would clear the content of all @code{password} elements in the data file "
4728 "while leaving off the trailing @key{TAB} would return all @code{password} "
4729 "elements in @abbr{XML} format."
4730 "\n"
4731 "When the @option{--inquire} option is passed then all remaining non-option "
4732 "arguments are retrieved via a server @emph{INQUIRE}."
4733 "\n"
4734 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4735 "expression syntax."
4738 new_command("XPATHATTR", 0, 1, xpathattr_command, _(
4739 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4740 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4741 "attributes and does not return a result. For the @var{SET} operation the "
4742 "@var{value} is optional but the field is required. If not specified then "
4743 "the attribute value will be empty. For example:"
4744 "@sp 1"
4745 "@example\n"
4746 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4747 "@end example\n"
4748 "@sp 1"
4749 "would create an @code{password} attribute for each @code{password} element "
4750 "found in the document. The attribute value will be empty but still exist."
4751 "\n"
4752 "When the @option{--inquire} option is passed then all remaining non-option "
4753 "arguments are retrieved via a server @emph{INQUIRE}."
4754 "\n"
4755 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4756 "expression syntax."
4759 new_command("IMPORT", 0, 1, import_command, _(
4760 "IMPORT [--root [!]element[<TAB>[!]child[..]]] <content>\n"
4761 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4762 "\n"
4763 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4764 "argument is raw @abbr{XML} data. The content is created as a child of "
4765 "the element path specified with the @option{--root} option or at the "
4766 "document root when not specified. Existing elements of the same name will "
4767 "be overwritten."
4768 "\n"
4769 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4770 "for details."
4773 new_command("DUMP", 0, 1, dump_command, _(
4774 "DUMP\n"
4775 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4776 "dumping a specific node."
4779 new_command("LOCK", 0, 0, lock_command, _(
4780 "LOCK\n"
4781 "Locks the mutex associated with the opened file. This prevents other clients "
4782 "from sending commands to the same opened file until the client "
4783 "that sent this command either disconnects or sends the @code{UNLOCK} "
4784 "command. @xref{UNLOCK}."
4787 new_command("UNLOCK", 1, 0, unlock_command, _(
4788 "UNLOCK\n"
4789 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4790 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4791 "@pxref{ISCACHED})."
4794 new_command("GETCONFIG", 1, 1, getconfig_command, _(
4795 "GETCONFIG [filename] <parameter>\n"
4796 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4797 "data response. If no file has been opened then the value for @var{filename} "
4798 "or the default from the @samp{global} section will be returned. If a file "
4799 "has been opened and no @var{filename} is specified, a value previously "
4800 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4803 new_command("OPTION", 1, 1, option_command, _(
4804 "OPTION <NAME>=<VALUE>\n"
4805 "Sets a client option @var{name} to @var{value}. The value for an option is "
4806 "kept for the duration of the connection."
4807 "\n"
4808 "@table @asis\n"
4809 "@item DISABLE-PINENTRY\n"
4810 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
4811 "server inquire is sent to the client to obtain the passphrase. This option "
4812 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4813 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
4814 "\n"
4815 "@item PINENTRY-TIMEOUT\n"
4816 "Sets the number of seconds before a pinentry prompt will return an error "
4817 "while waiting for user input."
4818 "\n"
4819 "@item TTYNAME\n"
4820 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4821 "\n"
4822 "@item TTYTYPE\n"
4823 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4824 "\n"
4825 "@item DISPLAY\n"
4826 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4827 "\n"
4828 "@item PINENTRY-DESC\n"
4829 "Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4830 "\n"
4831 "@item PINENTRY-TITLE\n"
4832 "Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
4833 "\n"
4834 "@item PINENTRY-PROMPT\n"
4835 "Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
4836 "\n"
4837 "@item LC-CTYPE\n"
4838 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4839 "\n"
4840 "@item LC-MESSAGES\n"
4841 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4842 "\n"
4843 "@item NAME\n"
4844 "Associates the thread ID of the connection with the specified textual "
4845 "representation. Useful for debugging log messages."
4846 "\n"
4847 "@item LOCK-TIMEOUT\n"
4848 "When not @code{0}, the duration in tenths of a second to wait for the file "
4849 "mutex which has been locked by another thread to be released before returning "
4850 "an error. When @code{-1}, then an error will be returned immediately."
4851 "\n"
4852 "@item LOG-LEVEL\n"
4853 "An integer specifiying the logging level."
4854 "@end table\n"
4857 new_command("LS", 1, 1, ls_command, _(
4858 "LS\n"
4859 "Lists the available data files stored in the data directory "
4860 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4863 new_command("RESET", 1, 1, NULL, _(
4864 "RESET\n"
4865 "Closes the currently opened file but keeps any previously set client options."
4868 new_command("NOP", 1, 1, NULL, _(
4869 "NOP\n"
4870 "Does nothing. Always returns successfully."
4873 /* !END-HELP-TEXT! */
4874 new_command ("CANCEL", 1, 1, NULL, NULL);
4875 new_command ("END", 1, 1, NULL, NULL);
4876 new_command ("BYE", 1, 1, NULL, NULL);
4878 int i;
4879 for (i = 0; command_table[i]; i++);
4880 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4881 sort_commands);
4884 gpg_error_t
4885 register_commands (assuan_context_t ctx)
4887 int i = 0, rc;
4889 for (; command_table[i]; i++)
4891 if (!command_table[i]->handler)
4892 continue;
4894 rc = assuan_register_command (ctx, command_table[i]->name,
4895 command_table[i]->handler,
4896 command_table[i]->help);
4897 if (rc)
4898 return rc;
4901 rc = assuan_register_bye_notify (ctx, bye_notify);
4902 if (rc)
4903 return rc;
4905 rc = assuan_register_reset_notify (ctx, reset_notify);
4906 if (rc)
4907 return rc;
4909 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4910 if (rc)
4911 return rc;
4913 return assuan_register_post_cmd_notify (ctx, command_finalize);