Fix return code of IMPORT.
[pwmd.git] / src / commands.c
blobdf7a6c6146e2f33fee6f356f5883eea8417289c5
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 #ifdef WITH_LIBACL
38 #include <sys/acl.h>
39 #endif
41 #include "pwmd-error.h"
42 #include <gcrypt.h>
44 #include "mem.h"
45 #include "xml.h"
46 #include "util-misc.h"
47 #include "common.h"
48 #include "rcfile.h"
49 #include "cache.h"
50 #include "commands.h"
51 #include "mutex.h"
52 #include "crypto.h"
53 #include "pinentry.h"
55 /* Flags needed to be retained for a client across commands. */
56 #define FLAG_NEW 0x0001
57 #define FLAG_HAS_LOCK 0x0002
58 #define FLAG_LOCK_CMD 0x0004
59 #define FLAG_NO_PINENTRY 0x0008
60 #define FLAG_OPEN 0x0010
61 #define FLAG_KEEP_LOCK 0x0020
63 /* These are command option flags. */
64 #define OPT_INQUIRE 0x0001
65 #define OPT_NO_PASSPHRASE 0x0002
66 #define OPT_RESET 0x0004
67 #define OPT_LIST_RECURSE 0x0008
68 #define OPT_LIST_VERBOSE 0x0010
69 #define OPT_LOCK 0x0020
70 #define OPT_LOCK_ON_OPEN 0x0040
71 #define OPT_SIGN 0x0080
72 #define OPT_LIST_ALL 0x0100
73 #define OPT_LIST_WITH_TARGET 0x0200
74 #define OPT_DATA 0x0400
75 #define OPT_NO_AGENT 0x0800
77 #ifdef WITH_AGENT
78 /* The GETCONFIG command, for example, may fail because pwmd lost the
79 * gpg-agent connection. Update the recovered agent ctx. */
80 #define UPDATE_AGENT_CTX(client, crypto) do { \
81 if (crypto && crypto->agent && crypto->agent->did_restart \
82 && client->crypto && client->crypto->agent \
83 && client->crypto->agent->ctx && \
84 crypto->agent->ctx != client->crypto->agent->ctx) \
85 { \
86 client->crypto->agent->ctx = crypto->agent->ctx; \
87 crypto->agent->ctx = NULL; \
88 } \
89 } while (0)
90 #else
91 #define UPDATE_AGENT_CTX(client, crypto)
92 #endif
94 struct command_table_s
96 const char *name;
97 gpg_error_t (*handler) (assuan_context_t, char *line);
98 const char *help;
99 int ignore_startup;
100 int unlock; // unlock the file mutex after validating the checksum
103 static struct command_table_s **command_table;
105 static gpg_error_t do_lock (struct client_s *client, int add);
106 static gpg_error_t validate_checksum (struct client_s *,
107 struct cache_data_s *);
108 static gpg_error_t update_checksum (struct client_s *client);
110 static gpg_error_t
111 unlock_file_mutex (struct client_s *client, int remove)
113 gpg_error_t rc = 0;
115 // OPEN: keep the lock for the same file being reopened.
116 if (client->flags & FLAG_KEEP_LOCK && client->flags & FLAG_HAS_LOCK)
117 return 0;
119 if (!(client->flags & FLAG_HAS_LOCK))
120 return GPG_ERR_NOT_LOCKED;
122 rc = cache_unlock_mutex (client->md5file, remove);
123 if (rc)
124 rc = GPG_ERR_INV_STATE;
125 else
126 client->flags &= ~(FLAG_HAS_LOCK | FLAG_LOCK_CMD);
128 return rc;
131 static gpg_error_t
132 lock_file_mutex (struct client_s *client, int add)
134 gpg_error_t rc = 0;
135 int timeout = config_get_integer (client->filename, "cache_timeout");
137 if (client->flags & FLAG_HAS_LOCK)
138 return 0;
140 rc = cache_lock_mutex (client->ctx, client->md5file,
141 client->lock_timeout, add, timeout);
142 if (!rc)
143 client->flags |= FLAG_HAS_LOCK;
145 return rc;
148 static gpg_error_t
149 file_modified (struct client_s *client, struct command_table_s *cmd)
151 gpg_error_t rc = 0;
153 if (!(client->flags & FLAG_OPEN))
154 return GPG_ERR_INV_STATE;
156 rc = lock_file_mutex (client, 0);
157 if (!rc || rc == GPG_ERR_NO_DATA)
159 rc = validate_checksum (client, NULL);
160 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
161 rc = 0;
162 else if (rc)
163 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
166 if ((cmd->unlock && !(client->flags & FLAG_HAS_LOCK)) || rc)
167 unlock_file_mutex (client, 0);
169 return rc;
172 static gpg_error_t
173 parse_xml (assuan_context_t ctx, int new)
175 struct client_s *client = assuan_get_pointer (ctx);
176 int cached = client->doc != NULL;
177 gpg_error_t rc = 0;
179 if (new)
181 client->doc = new_document ();
182 if (client->doc)
184 xmlChar *result;
185 int len;
187 xmlDocDumpFormatMemory (client->doc, &result, &len, 0);
188 client->crypto->plaintext = result;
189 client->crypto->plaintext_len = len;
190 if (!client->crypto->plaintext)
192 xmlFreeDoc (client->doc);
193 client->doc = NULL;
197 else if (!cached)
198 rc = parse_doc ((char *) client->crypto->plaintext,
199 client->crypto->plaintext_len, (xmlDocPtr *)&client->doc);
201 return rc;
204 static void
205 free_client (struct client_s *client)
207 if (client->doc)
208 xmlFreeDoc (client->doc);
210 xfree (client->crc);
211 xfree (client->filename);
212 xfree (client->last_error);
214 if (client->crypto)
216 cleanup_crypto_stage2 (client->crypto);
217 if (client->crypto->pkey_sexp)
218 gcry_sexp_release (client->crypto->pkey_sexp);
220 client->crypto->pkey_sexp = NULL;
221 memset (client->crypto->sign_grip, 0,
222 sizeof (client->crypto->sign_grip));
223 memset (client->crypto->grip, 0, sizeof(client->crypto->grip));
227 void
228 cleanup_client (struct client_s *client)
230 assuan_context_t ctx = client->ctx;
231 struct client_thread_s *thd = client->thd;
232 struct crypto_s *crypto = client->crypto;
233 long lock_timeout = client->lock_timeout;
234 int no_pinentry = (client->flags & FLAG_NO_PINENTRY);
235 struct pinentry_option_s pin_opts;
236 xmlErrorPtr xml_error = client->xml_error;
237 #ifdef WITH_AGENT
238 struct pinentry_option_s agent_pin_opts;
240 if (crypto && crypto->agent)
241 memcpy (&agent_pin_opts, &crypto->agent->pinentry_opts,
242 sizeof(struct pinentry_option_s));
243 #endif
245 memcpy (&pin_opts, &client->pinentry_opts, sizeof(struct pinentry_option_s));
246 unlock_file_mutex (client, client->flags & FLAG_NEW);
247 free_client (client);
248 memset (client, 0, sizeof (struct client_s));
249 client->xml_error = xml_error;
250 client->crypto = crypto;
251 client->ctx = ctx;
252 client->thd = thd;
253 client->lock_timeout = lock_timeout;
254 memcpy (&client->pinentry_opts, &pin_opts, sizeof(struct pinentry_option_s));
255 client->flags |= no_pinentry ? FLAG_NO_PINENTRY : 0;
256 #ifdef WITH_AGENT
257 if (crypto && crypto->agent)
258 memcpy (&crypto->agent->pinentry_opts, &agent_pin_opts,
259 sizeof(struct pinentry_option_s));
260 #endif
263 static gpg_error_t
264 open_finalize (assuan_context_t ctx, char *key, size_t keylen)
266 struct client_s *client = assuan_get_pointer (ctx);
267 gpg_error_t rc = 0;
268 struct cache_data_s *cdata = cache_get_data (client->md5file);
269 int cached = 0, keyarg = key ? 1 : 0;
270 size_t keysize;
271 unsigned char *salted_key = NULL;
272 int algo;
273 int pin_try = 1;
274 int pin_tries = 3;
275 char *pin_title = client->pinentry_opts.title;
277 client->crypto->filename = str_dup (client->filename);
278 if (cdata || client->flags & FLAG_NEW)
280 if (cdata && !(client->flags & FLAG_NEW))
282 rc = decrypt_cache (client->crypto, cdata->doc, cdata->doclen);
283 if (rc)
284 return rc;
287 cached = cdata != NULL;
288 goto done;
291 if (!key && !IS_PKI (client->crypto)
292 && !(client->crypto->hdr.flags & PWMD_FLAG_NO_PASSPHRASE))
294 if (client->flags & FLAG_NO_PINENTRY)
296 rc = inquire_passphrase (ctx, "PASSPHRASE", (unsigned char **)&key,
297 &keylen);
298 if (rc)
299 return rc;
301 else
303 client->pinentry_opts.timeout = config_get_integer (client->filename,
304 "pinentry_timeout");
305 pin_again:
306 rc = getpin_common (client->ctx, client->filename, PINENTRY_OPEN,
307 &key, &keylen);
308 if (rc)
309 return rc;
313 if (!IS_PKI (client->crypto))
315 if (client->crypto->hdr.flags & PWMD_FLAG_NO_PASSPHRASE)
317 keylen = 1;
318 key = gcry_malloc (keylen);
319 memset (key, 0, keylen);
322 algo = cipher_to_gcrypt (client->crypto->hdr.flags);
323 rc = hash_key (algo, client->crypto->hdr.salt,
324 sizeof(client->crypto->hdr.salt), key, keylen,
325 (void **)&salted_key, &keysize);
326 if (!keyarg)
327 gcry_free (key);
329 if (!rc)
331 rc = decrypt_data (client->ctx, client->crypto, salted_key, keysize);
332 if (gpg_err_code (rc) == GPG_ERR_BAD_PASSPHRASE
333 && ++pin_try <= pin_tries && !(client->flags & FLAG_NO_PINENTRY))
335 gcry_free (salted_key);
336 salted_key = NULL;
337 key = NULL;
338 keylen = 0;
339 client->pinentry_opts.title = str_asprintf (_ ("Bad passphrase (try %i of %i)"), pin_try, pin_tries);
340 goto pin_again;
344 if (client->pinentry_opts.title != pin_title)
345 xfree (client->pinentry_opts.title);
347 client->pinentry_opts.title = pin_title;
348 if (rc)
350 gcry_free (salted_key);
351 return rc;
354 cdata = xcalloc (1, sizeof (struct cache_data_s));
355 if (!cdata)
357 gcry_free (salted_key);
358 return GPG_ERR_ENOMEM;
361 cdata->key = salted_key;
362 cdata->keylen = keysize;
364 else
365 rc = decrypt_data (client->ctx, client->crypto, NULL, 0);
367 done:
368 if (!rc)
370 rc = parse_xml (ctx, client->flags & FLAG_NEW);
371 if (rc && !cached)
372 free_cache_data_once (cdata);
374 if (!rc)
376 int timeout = config_get_integer (client->filename,
377 "cache_timeout");
379 if (!cached)
381 if (!cdata)
382 cdata = xcalloc (1, sizeof (struct cache_data_s));
384 rc = encrypt_xml (NULL, cache_key, cache_keysize,
385 GCRY_CIPHER_AES, client->crypto->plaintext,
386 client->crypto->plaintext_len, &cdata->doc,
387 &cdata->doclen, &cache_iv, &cache_blocksize,
389 if (!rc && !(client->flags & FLAG_NEW) && IS_PKI (client->crypto))
391 rc = gcry_sexp_build ((gcry_sexp_t *) & cdata->pubkey, NULL,
392 "%S", client->crypto->pkey_sexp);
396 if (cdata->sigkey)
397 gcry_sexp_release (cdata->sigkey);
399 cdata->sigkey = NULL;
400 if (!rc && IS_PKI (client->crypto))
401 rc = gcry_sexp_build ((gcry_sexp_t *) &cdata->sigkey, NULL,
402 "%S", client->crypto->sigpkey_sexp);
404 if (!rc && !cache_add_file (client->md5file,
405 (client->flags & FLAG_NEW)
406 ? NULL
407 : client->crypto->grip, cdata, timeout))
409 if (!cached)
411 free_cache_data_once (cdata);
412 xmlFreeDoc (client->doc);
413 client->doc = NULL;
415 rc = GPG_ERR_ENOMEM;
418 if (!rc)
419 client->flags |= FLAG_OPEN;
423 if (!rc)
424 update_checksum (client);
426 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
427 rc = do_lock (client, 0);
429 return rc;
432 static void
433 req_cleanup (void *arg)
435 if (!arg)
436 return;
438 strv_free ((char **) arg);
441 static gpg_error_t
442 parse_open_opt_lock (void *data, void *value)
444 struct client_s *client = data;
446 client->opts |= OPT_LOCK_ON_OPEN;
447 return 0;
450 static gpg_error_t
451 parse_opt_inquire (void *data, void *value)
453 struct client_s *client = data;
455 (void) value;
456 client->opts |= OPT_INQUIRE;
457 return 0;
460 static gpg_error_t
461 update_checksum (struct client_s *client)
463 unsigned char *crc;
464 size_t len;
465 struct cache_data_s *cdata;
466 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
468 if (rc)
469 return rc;
471 xfree (client->crc);
472 client->crc = crc;
473 cdata = cache_get_data (client->md5file);
474 if (cdata)
476 xfree (cdata->crc);
477 cdata->crc = xmalloc (len);
478 memcpy (cdata->crc, crc, len);
481 return 0;
484 static gpg_error_t
485 validate_checksum (struct client_s *client, struct cache_data_s *cdata)
487 unsigned char *crc;
488 size_t len;
489 gpg_error_t rc;
490 int n = 0;
492 if (cdata && !cdata->crc)
493 return GPG_ERR_CHECKSUM;
495 rc = get_checksum (client->filename, &crc, &len);
496 if (rc)
497 return rc;
499 if (cdata)
500 n = memcmp (cdata->crc, crc, len);
501 else if (client->crc)
502 n = memcmp (client->crc, crc, len);
504 xfree (crc);
505 return n ? GPG_ERR_CHECKSUM : 0;
508 static gpg_error_t
509 do_open (assuan_context_t ctx, const char *filename, const char *password)
511 struct client_s *client = assuan_get_pointer (ctx);
512 struct cache_data_s *cdata;
513 gpg_error_t rc = 0;
514 int done = 0;
516 if (!valid_filename (filename))
517 return GPG_ERR_INV_VALUE;
519 gcry_md_hash_buffer (GCRY_MD_MD5, client->md5file, filename,
520 strlen (filename));
521 client->filename = str_dup (filename);
522 if (!client->filename)
523 return GPG_ERR_ENOMEM;
525 // Cached document?
526 cdata = cache_get_data (client->md5file);
527 if (cdata && cdata->doc)
529 int defer = 0;
531 /* This will check that the key is cached in the agent which needs to
532 * be determined for files that share a keygrip. */
533 if (!rc)
535 rc = cache_iscached (client->filename, &defer);
536 if ((!rc || rc == GPG_ERR_ENOENT) && defer)
537 rc = GPG_ERR_KEY_EXPIRED;
540 if (!rc && !(client->flags & FLAG_NEW))
541 rc = validate_checksum (client, cdata);
543 #ifdef WITH_GNUTLS
544 if (!rc && client->thd->remote
545 && config_get_boolean (client->filename, "tcp_require_key"))
546 rc = GPG_ERR_KEY_EXPIRED;
547 #endif
549 if (!rc && !password)
551 rc = read_data_header (client->filename, &client->crypto->hdr,
552 NULL, NULL);
553 if (!rc)
555 if (IS_PKI (client->crypto))
557 gcry_sexp_build (&client->crypto->pkey_sexp, NULL, "%S",
558 cdata->pubkey);
559 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
560 client->crypto->grip);
561 gcry_sexp_build (&client->crypto->sigpkey_sexp, NULL, "%S",
562 cdata->sigkey);
563 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
564 client->crypto->sign_grip);
567 if (!rc)
569 rc = open_finalize (ctx, NULL, 0);
570 done = 1;
575 /* There was an error accessing the file so clear the cache entry. The
576 * real error will be returned from read_data_file() since the file
577 * may have only disappeared. */
578 if (!done)
580 log_write ("%s: %s", filename, pwmd_strerror (rc));
581 rc = cache_clear (client->md5file);
582 send_status_all (STATUS_CACHE, NULL);
586 if (done || rc)
587 return rc;
589 rc = read_data_file (client->filename, client->crypto);
590 if (rc)
592 if (gpg_err_source (rc) != GPG_ERR_SOURCE_DEFAULT ||
593 gpg_err_code (rc) != GPG_ERR_ENOENT)
595 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
596 return rc;
599 client->flags |= FLAG_NEW;
600 memset (client->crypto->grip, 0, sizeof (client->crypto->grip));
601 memset (client->crypto->sign_grip, 0,
602 sizeof (client->crypto->sign_grip));
603 rc = open_finalize (ctx, NULL, 0);
604 return rc;
607 if (password && IS_PKI (client->crypto))
609 #ifdef WITH_AGENT
610 rc = set_agent_passphrase (client->crypto, password, strlen (password));
611 if (rc)
612 return rc;
613 #endif
616 rc = open_finalize (ctx, (char *)password, password ? strlen (password) : 0);
617 return rc;
620 static gpg_error_t
621 open_command (assuan_context_t ctx, char *line)
623 gpg_error_t rc;
624 struct client_s *client = assuan_get_pointer (ctx);
625 char **req, *filename;
626 unsigned char md5file[16];
627 int same_file = 0;
628 assuan_peercred_t peer;
629 struct argv_s *args[] = {
630 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
631 NULL
634 rc = parse_options (&line, args, client);
635 if (rc)
636 return send_error (ctx, rc);
638 req = str_split (line, " ", 2);
639 if (!req)
640 return send_error (ctx, GPG_ERR_SYNTAX);
642 rc = do_validate_peer (ctx, req[0], &peer);
643 if (rc)
645 strv_free (req);
646 return send_error (ctx, rc);
649 pthread_cleanup_push (req_cleanup, req);
650 filename = req[0];
651 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, filename, strlen (filename));
652 /* This client may have locked a different file with ISCACHED --lock than
653 * the current filename. This will remove that lock. */
654 same_file = !memcmp (md5file, client->md5file, 16) ? 1 : 0;
655 if (client->flags & FLAG_OPEN ||
656 (client->flags & FLAG_HAS_LOCK && !same_file))
658 typeof (client->opts) opts = client->opts;
659 typeof (client->flags) flags = client->flags;
661 if (same_file)
662 client->flags |= FLAG_KEEP_LOCK;
664 cleanup_client (client);
665 client->opts = opts;
666 client->flags |= flags;
667 client->flags &= ~(FLAG_LOCK_CMD);
668 if (!same_file)
669 client->flags &= ~(FLAG_HAS_LOCK | FLAG_NEW);
672 /* Need to lock the mutex here because file_modified() cannot without
673 * knowing the filename. */
674 memcpy (client->md5file, md5file, 16);
675 rc = lock_file_mutex (client, 1);
676 if (!rc)
678 char *password = req[1] && *req[1] ? req[1] : NULL;
680 #ifdef WITH_AGENT
681 if (IS_PKI (client->crypto))
682 rc = set_pinentry_mode (client->crypto->agent,
683 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
684 : "ask");
685 #endif
686 if (!rc)
688 rc = do_open (ctx, filename, password);
689 if (rc)
690 cleanup_client (client);
692 cleanup_crypto_stage1 (client->crypto);
696 pthread_cleanup_pop (1);
698 if (!rc && client->flags & FLAG_NEW)
699 rc = send_status (ctx, STATUS_NEWFILE, NULL);
701 #ifdef WITH_AGENT
702 (void) kill_scd (client->crypto->agent);
703 #endif
704 return send_error (ctx, rc);
707 static gpg_error_t
708 parse_opt_no_passphrase (void *data, void *value)
710 struct client_s *client = data;
712 (void) value;
713 client->opts |= OPT_NO_PASSPHRASE;
714 return 0;
717 static gpg_error_t
718 parse_save_opt_no_agent (void *data, void *value)
720 struct client_s *client = data;
722 client->opts |= OPT_NO_AGENT;
723 return 0;
726 static gpg_error_t
727 parse_save_opt_cipher (void *data, void *value)
729 struct client_s *client = data;
730 int algo = cipher_string_to_gcrypt ((char *) value);
731 file_header_t *hdr = &client->crypto->save.hdr;
733 if (algo == -1)
734 return GPG_ERR_INV_VALUE;
736 hdr->flags = set_cipher_flag (hdr->flags, algo);
737 return 0;
740 static gpg_error_t
741 parse_save_opt_keygrip (void *data, void *value)
743 struct client_s *client = data;
745 if (!IS_PKI (client->crypto))
746 return GPG_ERR_INV_ARG;
748 #ifdef WITH_AGENT
749 if (client->crypto->save.pkey)
750 gcry_sexp_release (client->crypto->save.pkey);
752 client->crypto->save.pkey = NULL;
753 return get_pubkey (client->crypto, value, &client->crypto->save.pkey);
754 #else
755 return GPG_ERR_INV_ARG;
756 #endif
759 static gpg_error_t
760 parse_save_opt_sign_keygrip (void *data, void *value)
762 struct client_s *client = data;
764 if (!IS_PKI (client->crypto))
765 return GPG_ERR_INV_ARG;
767 #ifdef WITH_AGENT
768 if (client->crypto->save.sigpkey)
769 gcry_sexp_release (client->crypto->save.sigpkey);
771 client->crypto->save.sigpkey = NULL;
772 return get_pubkey (client->crypto, value, &client->crypto->save.sigpkey);
773 #else
774 return GPG_ERR_INV_ARG;
775 #endif
778 static gpg_error_t
779 parse_opt_s2k_count (void *data, void *value)
781 struct client_s *client = data;
782 char *v = value;
784 if (!v || !*v)
785 return GPG_ERR_INV_VALUE;
787 client->crypto->save.s2k_count = strtoul (v, NULL, 10);
788 return 0;
791 static gpg_error_t
792 parse_save_opt_iterations (void *data, void *value)
794 struct client_s *client = data;
795 char *v = value, *p;
796 uint64_t n;
798 if (!v || !*v)
799 return GPG_ERR_INV_VALUE;
801 errno = 0;
802 n = strtoull (v, &p, 10);
803 if (n == UINT64_MAX && errno)
804 return gpg_error_from_errno (errno);
805 else if (p && *p)
806 return GPG_ERR_INV_VALUE;
808 client->crypto->save.hdr.iterations = n;
809 return 0;
812 static gpg_error_t
813 save_finalize (assuan_context_t ctx)
815 struct client_s *client = assuan_get_pointer (ctx);
816 gpg_error_t rc = 0;
817 xmlChar *xmlbuf = NULL;
818 int xmlbuflen;
819 void *key = NULL;
820 size_t keylen = 0;
822 xmlDocDumpFormatMemory (client->doc, &xmlbuf, &xmlbuflen, 0);
823 if (!xmlbuf)
824 return GPG_ERR_ENOMEM;
826 pthread_cleanup_push (xmlFree, xmlbuf);
828 if (!use_agent || ((client->flags & FLAG_NEW)
829 && (client->opts & OPT_NO_AGENT))
830 || !(client->crypto->hdr.flags & PWMD_FLAG_PKI))
832 rc = export_common (ctx, client->flags & FLAG_NO_PINENTRY,
833 client->crypto, xmlbuf, xmlbuflen, client->filename,
834 NULL, &key, &keylen, 1, 1,
835 (client->opts & OPT_NO_PASSPHRASE));
837 #ifdef WITH_AGENT
838 else
840 gcry_sexp_t pubkey = client->crypto->save.pkey;
841 gcry_sexp_t sigkey = client->crypto->save.sigpkey;
843 if (!pubkey)
844 pubkey = client->crypto->pkey_sexp;
846 if (!sigkey)
848 sigkey = client->crypto->sigpkey_sexp;
849 if (!sigkey)
851 gcry_sexp_build (&client->crypto->save.sigpkey, 0, "%S", pubkey);
852 sigkey = client->crypto->save.sigpkey;
856 rc = encrypt_data_file (client->ctx, client->crypto, pubkey, sigkey,
857 client->filename, xmlbuf, xmlbuflen);
858 if (pubkey == client->crypto->save.pkey)
860 if (!rc)
862 gcry_sexp_release (client->crypto->pkey_sexp);
863 client->crypto->pkey_sexp = client->crypto->save.pkey;
864 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
865 client->crypto->grip);
867 else
868 gcry_sexp_release (pubkey);
870 client->crypto->save.pkey = NULL;
873 if (!rc)
874 gcry_pk_get_keygrip (sigkey, client->crypto->sign_grip);
876 if (sigkey == client->crypto->save.sigpkey)
878 if (!rc)
880 if (client->crypto->sigpkey_sexp)
881 gcry_sexp_release (client->crypto->sigpkey_sexp);
883 client->crypto->sigpkey_sexp = client->crypto->save.sigpkey;
884 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
885 client->crypto->sign_grip);
887 else
888 gcry_sexp_release (sigkey);
890 client->crypto->save.sigpkey = NULL;
893 #endif
895 if (!rc)
897 int cached;
899 rc = save_common (client->filename, client->crypto, xmlbuf, xmlbuflen,
900 key, keylen, &cached, client->opts & OPT_NO_AGENT);
901 if (rc)
902 gcry_free (key);
904 if (!rc && (!cached || (client->flags & FLAG_NEW)))
905 send_status_all (STATUS_CACHE, NULL);
907 if (!rc)
909 rc = update_checksum (client);
910 client->flags &= ~(FLAG_NEW);
914 pthread_cleanup_pop (1); // xmlFree
915 return rc;
918 static gpg_error_t
919 parse_opt_reset (void *data, void *value)
921 struct client_s *client = data;
923 (void) value;
924 client->opts |= OPT_RESET;
925 return 0;
928 static gpg_error_t
929 save_command (assuan_context_t ctx, char *line)
931 struct client_s *client = assuan_get_pointer (ctx);
932 gpg_error_t rc;
933 struct stat st;
934 struct argv_s *args[] = {
935 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG,
936 parse_opt_no_passphrase},
937 &(struct argv_s) {"cipher", OPTION_TYPE_ARG, parse_save_opt_cipher},
938 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
939 parse_opt_inquire},
940 &(struct argv_s) {"keygrip", OPTION_TYPE_ARG, parse_save_opt_keygrip},
941 &(struct argv_s) {"sign-keygrip", OPTION_TYPE_ARG,
942 parse_save_opt_sign_keygrip},
943 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
944 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
945 &(struct argv_s) {"cipher-iterations", OPTION_TYPE_ARG,
946 parse_save_opt_iterations},
947 &(struct argv_s) {"no-agent", OPTION_TYPE_NOARG,
948 parse_save_opt_no_agent},
949 NULL
952 cleanup_save (&client->crypto->save);
953 memcpy (&client->crypto->save.hdr, &client->crypto->hdr,
954 sizeof (file_header_t));
955 client->crypto->save.s2k_count =
956 config_get_ulong (client->filename, "s2k_count");
958 if (client->flags & FLAG_NEW)
959 client->crypto->save.hdr.iterations =
960 config_get_ulonglong (client->filename, "cipher_iterations");
962 rc = parse_options (&line, args, client);
963 if (rc)
964 return send_error (ctx, rc);
966 if (!(client->flags & FLAG_NEW))
967 client->opts &= ~OPT_NO_AGENT;
968 else if (client->opts & OPT_NO_AGENT)
970 client->crypto->save.hdr.flags &= ~PWMD_FLAG_PKI;
971 client->crypto->hdr.flags &= ~PWMD_FLAG_PKI;
974 if ((client->opts & OPT_NO_PASSPHRASE) && !(client->flags & FLAG_NEW)
975 && !(client->opts & OPT_INQUIRE))
976 return send_error (ctx, GPG_ERR_WRONG_KEY_USAGE);
978 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
979 return send_error (ctx, gpg_error_from_errno (errno));
981 if (errno != ENOENT && !S_ISREG (st.st_mode))
983 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
984 return send_error (ctx, GPG_ERR_ENOANO);
987 int defer = 0;
988 rc = cache_iscached (client->filename, &defer);
989 if (gpg_err_code (rc) == GPG_ERR_ENOENT && client->flags & FLAG_NEW)
990 rc = 0;
991 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
992 rc = 0;
994 if ((!rc && defer) || config_get_boolean ("global", "require_save_key"))
995 client->opts |= OPT_RESET;
997 if (!rc && (client->opts & OPT_RESET))
999 rc = cache_clear (client->md5file);
1000 if (rc)
1001 return send_error (ctx, rc);
1003 log_write ("%s: %s", client->filename,
1004 pwmd_strerror (GPG_ERR_KEY_EXPIRED));
1005 send_status_all (STATUS_CACHE, NULL);
1008 if (!rc)
1009 rc = update_element_mtime (xmlDocGetRootElement (client->doc));
1011 if (rc)
1012 return send_error (ctx, rc);
1014 #ifdef WITH_AGENT
1015 if (!rc && use_agent && !client->crypto->save.pkey &&
1016 (client->flags & FLAG_NEW || client->opts & OPT_INQUIRE)
1017 && !(client->opts & OPT_NO_AGENT))
1019 rc = set_pinentry_mode (client->crypto->agent,
1020 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
1021 : "ask");
1023 if (!(client->flags & FLAG_NEW))
1025 struct crypto_s *crypto;
1026 char *key = NULL;
1027 size_t keylen = 0;
1029 /* Wanting to generate a new key. Require the key to open the
1030 current file before proceeding reguardless of the
1031 require_save_key configuration parameter. */
1032 rc = cache_clear (client->md5file);
1033 if (!rc)
1035 rc = init_client_crypto (&crypto);
1036 if (!rc)
1037 crypto->client_ctx = client->ctx;
1040 if (!rc)
1041 rc = decrypt_common (client->ctx, client->opts&OPT_INQUIRE, crypto,
1042 client->filename, &key, &keylen);
1044 xfree (key);
1045 cleanup_crypto (&crypto);
1048 if (!rc)
1049 rc = send_status (client->ctx, STATUS_GENKEY, NULL);
1051 if (!rc)
1053 struct inquire_data_s idata = { 0 };
1054 char *params = client->opts & OPT_INQUIRE
1055 ? NULL : default_key_params (client->crypto);
1057 pthread_cleanup_push (xfree, params);
1058 idata.crypto = client->crypto;
1060 if (params)
1062 idata.line = params;
1063 idata.len = strlen (params);
1065 else
1066 idata.preset = 1;
1068 client->crypto->agent->inquire_data = &idata;
1069 client->crypto->agent->inquire_cb = NULL;
1070 rc = generate_key (client->crypto, params,
1071 (client->opts & OPT_NO_PASSPHRASE), 1);
1072 pthread_cleanup_pop (1);
1075 #endif
1077 if (!rc)
1078 rc = save_finalize (ctx);
1080 cleanup_crypto_stage1 (client->crypto);
1081 #ifdef WITH_AGENT
1082 (void) kill_scd (client->crypto->agent);
1083 #endif
1084 return send_error (ctx, rc);
1087 static gpg_error_t
1088 do_delete (assuan_context_t ctx, char *line)
1090 struct client_s *client = assuan_get_pointer (ctx);
1091 gpg_error_t rc;
1092 char **req;
1093 xmlNodePtr n;
1095 if (strchr (line, '\t'))
1096 req = str_split (line, "\t", 0);
1097 else
1098 req = str_split (line, " ", 0);
1100 if (!req || !*req)
1101 return GPG_ERR_SYNTAX;
1103 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1104 if (!n)
1106 strv_free (req);
1107 return rc;
1111 * No sub-node defined. Remove the entire node (root element).
1113 if (!req[1])
1115 if (n)
1117 rc = unlink_node (n);
1118 xmlFreeNode (n);
1121 strv_free (req);
1122 return rc;
1126 find_elements (client, client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1127 0, 0, NULL, 0);
1128 strv_free (req);
1129 if (!n)
1130 return rc;
1132 if (n)
1134 rc = unlink_node (n);
1135 xmlFreeNode (n);
1138 return rc;
1141 static gpg_error_t
1142 delete_command (assuan_context_t ctx, char *line)
1144 struct client_s *client = assuan_get_pointer (ctx);
1145 gpg_error_t rc;
1146 struct argv_s *args[] = {
1147 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1148 NULL
1151 rc = parse_options (&line, args, client);
1152 if (rc)
1153 return send_error (ctx, rc);
1155 if (client->opts & OPT_INQUIRE)
1157 unsigned char *result;
1158 size_t len;
1160 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1161 if (rc)
1162 return send_error (ctx, rc);
1164 line = (char *) result;
1167 rc = do_delete (ctx, line);
1169 if (client->opts & OPT_INQUIRE)
1170 xfree (line);
1172 return send_error (ctx, rc);
1175 static gpg_error_t
1176 store_command (assuan_context_t ctx, char *line)
1178 struct client_s *client = assuan_get_pointer (ctx);
1179 gpg_error_t rc;
1180 size_t len;
1181 unsigned char *result;
1182 char **req;
1183 xmlNodePtr n, parent;
1184 int has_content;
1185 char *content = NULL;
1187 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1188 if (rc)
1189 return send_error (ctx, rc);
1191 req = str_split ((char *) result, "\t", 0);
1192 xfree (result);
1194 if (!req || !*req)
1195 return send_error (ctx, GPG_ERR_SYNTAX);
1197 len = strv_length (req);
1198 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1199 if (*(req + 1) && !valid_element_path (req, has_content))
1201 strv_free (req);
1202 return send_error (ctx, GPG_ERR_INV_VALUE);
1205 if (has_content || !*req[len - 1])
1207 has_content = 1;
1208 content = req[len - 1];
1209 req[len - 1] = NULL;
1212 again:
1213 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1214 if (rc && rc == GPG_ERR_ELEMENT_NOT_FOUND)
1216 rc = new_root_element (client, client->doc, *req);
1217 if (rc)
1219 strv_free (req);
1220 return send_error (ctx, rc);
1223 goto again;
1226 if (!n)
1228 strv_free (req);
1229 return send_error (ctx, rc);
1232 parent = n;
1234 if (req[1] && *req[1])
1236 if (!n->children)
1237 parent = create_elements_cb (client, n, req + 1, &rc, NULL);
1238 else
1239 parent = find_elements (client, client->doc, n->children, req + 1, &rc,
1240 NULL, NULL, create_elements_cb, 0, 0, NULL,
1244 if (!rc && len > 1)
1246 n = find_text_node (parent->children);
1247 if (n)
1248 xmlNodeSetContent (n, (xmlChar *) content);
1249 else
1250 xmlNodeAddContent (parent, (xmlChar *) content);
1252 update_element_mtime (parent);
1255 xfree (content);
1256 strv_free (req);
1257 return send_error (ctx, rc);
1260 static gpg_error_t
1261 xfer_data (assuan_context_t ctx, const char *line, int total)
1263 int to_send;
1264 int sent = 0;
1265 gpg_error_t rc;
1266 int progress = config_get_integer ("global", "xfer_progress");
1267 int flush = 0;
1269 progress =
1270 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1271 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1272 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1274 if (rc)
1275 return rc;
1277 again:
1280 if (sent + to_send > total)
1281 to_send = total - sent;
1283 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1284 flush ? 0 : to_send);
1285 if (!rc)
1287 sent += flush ? 0 : to_send;
1289 if ((progress && !(sent % progress) && sent != total) ||
1290 (sent == total && flush))
1291 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1293 if (!flush && !rc && sent == total)
1295 flush = 1;
1296 goto again;
1300 while (!rc && sent < total);
1302 return rc;
1305 static gpg_error_t
1306 do_get (assuan_context_t ctx, char *line)
1308 struct client_s *client = assuan_get_pointer (ctx);
1309 gpg_error_t rc;
1310 char **req;
1311 xmlNodePtr n;
1313 req = str_split (line, "\t", 0);
1315 if (!req || !*req)
1317 strv_free (req);
1318 return GPG_ERR_SYNTAX;
1321 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1322 if (!n)
1324 strv_free (req);
1325 return rc;
1328 if (req[1])
1330 find_elements (client, client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1331 0, 0, NULL, 0);
1333 strv_free (req);
1334 if (rc)
1335 return rc;
1337 if (!n || !n->children)
1338 return GPG_ERR_NO_DATA;
1340 n = find_text_node (n->children);
1341 if (!n || !n->content || !*n->content)
1342 return GPG_ERR_NO_DATA;
1344 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1345 return rc;
1348 static gpg_error_t
1349 get_command (assuan_context_t ctx, char *line)
1351 struct client_s *client = assuan_get_pointer (ctx);
1352 gpg_error_t rc;
1353 struct argv_s *args[] = {
1354 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1355 NULL
1358 rc = parse_options (&line, args, client);
1359 if (rc)
1360 return send_error (ctx, rc);
1362 if (client->opts & OPT_INQUIRE)
1364 unsigned char *result;
1365 size_t len;
1367 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1368 if (rc)
1369 return send_error (ctx, rc);
1371 line = (char *) result;
1374 rc = do_get (ctx, line);
1376 if (client->opts & OPT_INQUIRE)
1377 xfree (line);
1379 return send_error (ctx, rc);
1382 static void list_command_cleanup1 (void *arg);
1383 static gpg_error_t
1384 realpath_command (assuan_context_t ctx, char *line)
1386 struct string_s *string = NULL;
1387 gpg_error_t rc;
1388 struct client_s *client = assuan_get_pointer (ctx);
1389 struct argv_s *args[] = {
1390 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1391 NULL
1394 rc = parse_options (&line, args, client);
1395 if (rc)
1396 return send_error (ctx, rc);
1398 if (client->opts & OPT_INQUIRE)
1400 unsigned char *result;
1401 size_t len;
1403 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1404 if (rc)
1405 return send_error (ctx, rc);
1407 line = (char *) result;
1410 rc = build_realpath (client, client->doc, line, &string);
1411 if (!rc)
1413 pthread_cleanup_push (list_command_cleanup1, string);
1414 rc = xfer_data (ctx, string->str, string->len);
1415 pthread_cleanup_pop (1);
1418 if (client->opts & OPT_INQUIRE)
1419 xfree (line);
1421 return send_error (ctx, rc);
1424 static void
1425 list_command_cleanup1 (void *arg)
1427 if (arg)
1428 string_free ((struct string_s *) arg, 1);
1431 static void
1432 list_command_cleanup2 (void *arg)
1434 struct element_list_s *elements = arg;
1436 if (elements)
1438 if (elements->list)
1440 int total = slist_length (elements->list);
1441 int i;
1443 for (i = 0; i < total; i++)
1445 char *tmp = slist_nth_data (elements->list, i);
1446 xfree (tmp);
1449 slist_free (elements->list);
1452 if (elements->prefix)
1453 xfree (elements->prefix);
1455 if (elements->req)
1456 strv_free (elements->req);
1458 xfree (elements);
1462 static gpg_error_t
1463 parse_list_opt_norecurse (void *data, void *value)
1465 struct client_s *client = data;
1467 client->opts &= ~(OPT_LIST_RECURSE);
1468 return 0;
1471 static gpg_error_t
1472 parse_list_opt_verbose (void *data, void *value)
1474 struct client_s *client = data;
1476 client->opts |= OPT_LIST_VERBOSE;
1477 return 0;
1480 static gpg_error_t
1481 parse_list_opt_target (void *data, void *value)
1483 struct client_s *client = data;
1485 client->opts |= OPT_LIST_WITH_TARGET;
1486 return 0;
1489 static gpg_error_t
1490 parse_list_opt_all (void *data, void *value)
1492 struct client_s *client = data;
1494 client->opts |= OPT_LIST_ALL;
1495 return 0;
1498 static gpg_error_t
1499 list_path_once (struct client_s *client, char *line,
1500 struct element_list_s *elements, struct string_s *result)
1502 gpg_error_t rc;
1504 elements->req = str_split (line, " ", 0);
1505 if (!elements->req)
1506 strv_printf (&elements->req, "%s", line);
1508 rc = create_path_list (client, client->doc, elements, *elements->req);
1509 if (rc == GPG_ERR_ELOOP && elements->verbose)
1510 rc = 0;
1512 if (!rc)
1514 int total = slist_length (elements->list);
1516 if (!total)
1517 rc = GPG_ERR_NO_DATA;
1519 if (!rc)
1521 if (!rc)
1523 int i;
1525 for (i = 0; i < total; i++)
1527 char *tmp = slist_nth_data (elements->list, i);
1529 string_append_printf (result, "%s%s", tmp,
1530 i + 1 == total ? "" : "\n");
1534 else
1535 rc = GPG_ERR_NO_DATA;
1538 return rc;
1541 static int
1542 has_list_flag (char *path, char *flags)
1544 char *p = path;
1546 while (*p && *++p != ' ');
1548 if (!*p)
1549 return 0;
1551 for (; *p; p++)
1553 char *f;
1555 for (f = flags; *f && *f != ' '; f++)
1557 if (*p == *f)
1558 return 1;
1562 return 0;
1565 static gpg_error_t
1566 do_list (assuan_context_t ctx, char *line)
1568 struct client_s *client = assuan_get_pointer (ctx);
1569 gpg_error_t rc;
1570 struct element_list_s *elements = NULL;
1572 elements = xcalloc (1, sizeof (struct element_list_s));
1573 if (!elements)
1574 return GPG_ERR_ENOMEM;
1576 elements->recurse = client->opts & OPT_LIST_RECURSE;
1577 elements->verbose =
1578 (client->opts & OPT_LIST_VERBOSE) | (client->opts & OPT_LIST_WITH_TARGET);
1579 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1581 if (!line || !*line)
1583 struct string_s *str = NULL;
1585 pthread_cleanup_push (list_command_cleanup2, elements);
1586 rc = list_root_elements (client, client->doc, &str, elements->verbose,
1587 elements->with_target);
1588 pthread_cleanup_pop (1);
1589 pthread_cleanup_push (list_command_cleanup1, str);
1591 if (!rc)
1593 if (client->opts & OPT_LIST_ALL)
1595 char **roots = str_split (str->str, "\n", 0);
1596 char **p;
1598 pthread_cleanup_push (req_cleanup, roots);
1599 string_truncate (str, 0);
1601 for (p = roots; *p; p++)
1603 if (strchr (*p, ' '))
1605 if (has_list_flag (*p, "EO"))
1607 string_append_printf (str, "%s%s", *p,
1608 *(p + 1) ? "\n" : "");
1609 continue;
1613 elements = xcalloc (1, sizeof (struct element_list_s));
1614 if (!elements)
1616 rc = GPG_ERR_ENOMEM;
1617 break;
1620 elements->recurse = client->opts & OPT_LIST_RECURSE;
1621 elements->verbose =
1622 (client->opts & OPT_LIST_VERBOSE) | (client->opts &
1623 OPT_LIST_WITH_TARGET);
1624 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1625 pthread_cleanup_push (list_command_cleanup2, elements);
1626 rc = list_path_once (client, *p, elements, str);
1627 pthread_cleanup_pop (1);
1628 if (rc)
1629 break;
1631 if (*(p + 1))
1632 string_append (str, "\n");
1635 pthread_cleanup_pop (1);
1638 if (!rc)
1639 rc = xfer_data (ctx, str->str, str->len);
1642 pthread_cleanup_pop (1);
1643 return rc;
1646 pthread_cleanup_push (list_command_cleanup2, elements);
1647 struct string_s *str = string_new (NULL);
1648 pthread_cleanup_push (list_command_cleanup1, str);
1649 rc = list_path_once (client, line, elements, str);
1650 if (!rc)
1651 rc = xfer_data (ctx, str->str, str->len);
1653 pthread_cleanup_pop (1);
1654 pthread_cleanup_pop (1);
1655 return rc;
1658 static gpg_error_t
1659 list_command (assuan_context_t ctx, char *line)
1661 struct client_s *client = assuan_get_pointer (ctx);
1662 gpg_error_t rc;
1663 struct argv_s *args[] = {
1664 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG,
1665 parse_list_opt_norecurse},
1666 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_list_opt_verbose},
1667 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1668 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG,
1669 parse_list_opt_target},
1670 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_all},
1671 NULL
1674 if (disable_list_and_dump == 1)
1675 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1677 client->opts |= OPT_LIST_RECURSE;
1678 rc = parse_options (&line, args, client);
1679 if (rc)
1680 return send_error (ctx, rc);
1682 if (client->opts & OPT_LIST_ALL)
1683 client->opts |= OPT_LIST_RECURSE | OPT_LIST_VERBOSE;
1685 if (client->opts & OPT_INQUIRE)
1687 unsigned char *result;
1688 size_t len;
1690 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1691 if (rc)
1692 return send_error (ctx, rc);
1694 line = (char *) result;
1697 rc = do_list (ctx, line);
1699 if (client->opts & OPT_INQUIRE)
1700 xfree (line);
1702 return send_error (ctx, rc);
1706 * req[0] - element path
1708 static gpg_error_t
1709 attribute_list (assuan_context_t ctx, char **req)
1711 struct client_s *client = assuan_get_pointer (ctx);
1712 char **attrlist = NULL;
1713 int i = 0;
1714 char **path = NULL;
1715 xmlAttrPtr a;
1716 xmlNodePtr n, an;
1717 char *line;
1718 gpg_error_t rc;
1720 if (!req || !req[0])
1721 return GPG_ERR_SYNTAX;
1723 if ((path = str_split (req[0], "\t", 0)) == NULL)
1726 * The first argument may be only a root element.
1728 if ((path = str_split (req[0], " ", 0)) == NULL)
1729 return GPG_ERR_SYNTAX;
1732 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1734 if (!n)
1736 strv_free (path);
1737 return rc;
1740 if (path[1])
1742 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1743 NULL, NULL, NULL, 0, 0, NULL, 0);
1745 if (!n)
1747 strv_free (path);
1748 return rc;
1752 strv_free (path);
1754 for (a = n->properties; a; a = a->next)
1756 char **pa;
1758 if ((pa = xrealloc (attrlist, (i + 2) * sizeof (char *))) == NULL)
1760 if (attrlist)
1761 strv_free (attrlist);
1763 log_write ("%s(%i): %s", __FILE__, __LINE__,
1764 pwmd_strerror (GPG_ERR_ENOMEM));
1765 return GPG_ERR_ENOMEM;
1768 attrlist = pa;
1769 an = a->children;
1770 attrlist[i] = str_asprintf ("%s %s", (char *) a->name,
1772 && an->content ? (char *) an->content : "");
1774 if (!attrlist[i])
1776 strv_free (attrlist);
1777 log_write ("%s(%i): %s", __FILE__, __LINE__,
1778 pwmd_strerror (GPG_ERR_ENOMEM));
1779 return GPG_ERR_ENOMEM;
1782 attrlist[++i] = NULL;
1785 if (!attrlist)
1786 return GPG_ERR_NO_DATA;
1788 line = strv_join ("\n", attrlist);
1790 if (!line)
1792 log_write ("%s(%i): %s", __FILE__, __LINE__,
1793 pwmd_strerror (GPG_ERR_ENOMEM));
1794 strv_free (attrlist);
1795 return GPG_ERR_ENOMEM;
1798 pthread_cleanup_push (xfree, line);
1799 pthread_cleanup_push (req_cleanup, attrlist);
1800 rc = xfer_data (ctx, line, strlen (line));
1801 pthread_cleanup_pop (1);
1802 pthread_cleanup_pop (1);
1803 return rc;
1807 * req[0] - attribute
1808 * req[1] - element path
1810 static gpg_error_t
1811 attribute_delete (struct client_s *client, char **req)
1813 xmlNodePtr n;
1814 char **path = NULL;
1815 gpg_error_t rc;
1817 if (!req || !req[0] || !req[1])
1818 return GPG_ERR_SYNTAX;
1820 if (!strcmp (req[0], "_name"))
1821 return GPG_ERR_INV_ATTR;
1823 if ((path = str_split (req[1], "\t", 0)) == NULL)
1826 * The first argument may be only a root element.
1828 if ((path = str_split (req[1], " ", 0)) == NULL)
1829 return GPG_ERR_SYNTAX;
1832 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1833 if (!n)
1834 goto fail;
1836 if (path[1])
1838 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1839 NULL, NULL, NULL, 0, 0, NULL, 0);
1840 if (!n)
1841 goto fail;
1844 rc = delete_attribute (n, (xmlChar *) req[0]);
1846 fail:
1847 strv_free (path);
1848 return rc;
1851 static xmlNodePtr
1852 create_element_path (struct client_s *client,
1853 char ***elements, gpg_error_t * rc, xmlNodePtr parent)
1855 char **req = *elements;
1856 char **req_orig = strv_dup (req);
1857 xmlNodePtr n = NULL;
1859 *rc = 0;
1861 if (!req_orig)
1863 *rc = GPG_ERR_ENOMEM;
1864 log_write ("%s(%i): %s", __FILE__, __LINE__,
1865 pwmd_strerror (GPG_ERR_ENOMEM));
1866 goto fail;
1869 again:
1870 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
1871 if (!n)
1873 if (*rc != GPG_ERR_ELEMENT_NOT_FOUND)
1874 goto fail;
1876 *rc = new_root_element (client, client->doc, req[0]);
1877 if (*rc)
1878 goto fail;
1880 goto again;
1882 else if (n == parent)
1884 *rc = GPG_ERR_CONFLICT;
1885 goto fail;
1888 if (req[1])
1890 if (!n->children)
1891 n = create_target_elements_cb (client, n, req + 1, rc, NULL);
1892 else
1893 n = find_elements (client, client->doc, n->children, req + 1, rc,
1894 NULL, NULL, create_target_elements_cb, 0, 0,
1895 parent, 0);
1897 if (!n)
1898 goto fail;
1901 * Reset the position of the element tree now that the elements
1902 * have been created.
1904 strv_free (req);
1905 req = req_orig;
1906 req_orig = NULL;
1907 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
1908 if (!n)
1909 goto fail;
1911 n = find_elements (client, client->doc, n->children, req + 1, rc,
1912 NULL, NULL, NULL, 0, 0, NULL, 0);
1913 if (!n)
1914 goto fail;
1917 fail:
1918 if (req_orig)
1919 strv_free (req_orig);
1921 *elements = req;
1922 return n;
1926 * Creates a "target" attribute. When other commands encounter an element with
1927 * this attribute, the element path is modified to the target value. If the
1928 * source element path doesn't exist when using 'ATTR SET target', it is
1929 * created, but the destination element path must exist.
1931 * req[0] - source element path
1932 * req[1] - destination element path
1934 static gpg_error_t
1935 target_attribute (struct client_s *client, char **req)
1937 char **src, **dst, *line = NULL, **odst = NULL;
1938 gpg_error_t rc;
1939 xmlNodePtr n;
1941 if (!req || !req[0] || !req[1])
1942 return GPG_ERR_SYNTAX;
1944 if ((src = str_split (req[0], "\t", 0)) == NULL)
1947 * The first argument may be only a root element.
1949 if ((src = str_split (req[0], " ", 0)) == NULL)
1950 return GPG_ERR_SYNTAX;
1953 if (!valid_element_path (src, 0))
1954 return GPG_ERR_INV_VALUE;
1956 if ((dst = str_split (req[1], "\t", 0)) == NULL)
1959 * The first argument may be only a root element.
1961 if ((dst = str_split (req[1], " ", 0)) == NULL)
1963 rc = GPG_ERR_SYNTAX;
1964 goto fail;
1968 odst = strv_dup (dst);
1969 if (!odst)
1971 rc = GPG_ERR_ENOMEM;
1972 goto fail;
1975 n = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
1977 * Make sure the destination element path exists.
1979 if (!n)
1980 goto fail;
1982 if (dst[1])
1984 n = find_elements (client, client->doc, n->children, dst + 1, &rc,
1985 NULL, NULL, NULL, 0, 0, NULL, 0);
1986 if (!n)
1987 goto fail;
1990 rc = validate_target_attribute (client, client->doc, req[0], n);
1991 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
1992 goto fail;
1994 n = create_element_path (client, &src, &rc, NULL);
1995 if (rc)
1996 goto fail;
1998 line = strv_join ("\t", odst);
1999 if (!line)
2001 rc = GPG_ERR_ENOMEM;
2002 goto fail;
2005 rc = add_attribute (n, "target", line);
2007 fail:
2008 xfree (line);
2009 strv_free (src);
2010 strv_free (dst);
2011 strv_free (odst);
2012 return rc;
2016 * req[0] - attribute
2017 * req[1] - element path
2019 static gpg_error_t
2020 attribute_get (assuan_context_t ctx, char **req)
2022 struct client_s *client = assuan_get_pointer (ctx);
2023 xmlNodePtr n;
2024 xmlChar *a;
2025 char **path = NULL;
2026 gpg_error_t rc;
2028 if (!req || !req[0] || !req[1])
2029 return GPG_ERR_SYNTAX;
2031 if (strchr (req[1], '\t'))
2033 if ((path = str_split (req[1], "\t", 0)) == NULL)
2034 return GPG_ERR_SYNTAX;
2036 else
2038 if ((path = str_split (req[1], " ", 0)) == NULL)
2039 return GPG_ERR_SYNTAX;
2042 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2044 if (!n)
2045 goto fail;
2047 if (path[1])
2049 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2050 NULL, NULL, NULL, 0, 0, NULL, 0);
2052 if (!n)
2053 goto fail;
2056 strv_free (path);
2058 if ((a = xmlGetProp (n, (xmlChar *) req[0])) == NULL)
2059 return GPG_ERR_NOT_FOUND;
2061 pthread_cleanup_push (xmlFree, a);
2063 if (*a)
2064 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
2065 else
2066 rc = GPG_ERR_NO_DATA;
2068 pthread_cleanup_pop (1);
2069 return rc;
2071 fail:
2072 strv_free (path);
2073 return rc;
2077 * req[0] - attribute
2078 * req[1] - element path
2079 * req[2] - value
2081 static gpg_error_t
2082 attribute_set (struct client_s *client, char **req)
2084 char **path = NULL;
2085 gpg_error_t rc;
2086 xmlNodePtr n;
2088 if (!req || !req[0] || !req[1])
2089 return GPG_ERR_SYNTAX;
2092 * Reserved attribute names.
2094 if (!strcmp (req[0], "_name"))
2095 return GPG_ERR_INV_ATTR;
2096 else if (!strcmp (req[0], "target"))
2097 return target_attribute (client, req + 1);
2098 else if (!valid_xml_attribute (req[0]))
2099 return GPG_ERR_INV_VALUE;
2101 if ((path = str_split (req[1], "\t", 0)) == NULL)
2104 * The first argument may be only a root element.
2106 if ((path = str_split (req[1], " ", 0)) == NULL)
2107 return GPG_ERR_SYNTAX;
2110 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2112 if (!n)
2113 goto fail;
2115 if (path[1])
2117 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2118 NULL, NULL, NULL, 0, 0, NULL, 0);
2120 if (!n)
2121 goto fail;
2124 rc = add_attribute (n, req[0], req[2]);
2126 fail:
2127 strv_free (path);
2128 return rc;
2132 * req[0] - command
2133 * req[1] - attribute name or element path if command is LIST
2134 * req[2] - element path
2135 * req[2] - element path or value
2138 static gpg_error_t
2139 do_attr (assuan_context_t ctx, char *line)
2141 struct client_s *client = assuan_get_pointer (ctx);
2142 gpg_error_t rc = 0;
2143 char **req;
2145 req = str_split (line, " ", 4);
2146 if (!req || !req[0] || !req[1])
2148 strv_free (req);
2149 return GPG_ERR_SYNTAX;
2152 pthread_cleanup_push (req_cleanup, req);
2154 if (strcasecmp (req[0], "SET") == 0)
2155 rc = attribute_set (client, req + 1);
2156 else if (strcasecmp (req[0], "GET") == 0)
2157 rc = attribute_get (ctx, req + 1);
2158 else if (strcasecmp (req[0], "DELETE") == 0)
2159 rc = attribute_delete (client, req + 1);
2160 else if (strcasecmp (req[0], "LIST") == 0)
2161 rc = attribute_list (ctx, req + 1);
2162 else
2163 rc = GPG_ERR_SYNTAX;
2165 pthread_cleanup_pop (1);
2166 return rc;
2169 static gpg_error_t
2170 attr_command (assuan_context_t ctx, char *line)
2172 struct client_s *client = assuan_get_pointer (ctx);
2173 gpg_error_t rc;
2174 struct argv_s *args[] = {
2175 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2176 NULL
2179 rc = parse_options (&line, args, client);
2180 if (rc)
2181 return send_error (ctx, rc);
2183 if (client->opts & OPT_INQUIRE)
2185 unsigned char *result;
2186 size_t len;
2188 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2189 if (rc)
2190 return send_error (ctx, rc);
2192 line = (char *) result;
2195 rc = do_attr (ctx, line);
2197 if (client->opts & OPT_INQUIRE)
2198 xfree (line);
2200 return send_error (ctx, rc);
2203 static gpg_error_t
2204 parse_iscached_opt_lock (void *data, void *value)
2206 struct client_s *client = data;
2208 (void) value;
2209 client->opts |= OPT_LOCK;
2210 return 0;
2213 static gpg_error_t
2214 iscached_command (assuan_context_t ctx, char *line)
2216 struct client_s *client = assuan_get_pointer (ctx);
2217 gpg_error_t rc;
2218 struct argv_s *args[] = {
2219 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2220 NULL
2223 if (!line || !*line)
2224 return send_error (ctx, GPG_ERR_SYNTAX);
2226 rc = parse_options (&line, args, client);
2227 if (rc)
2228 return send_error (ctx, rc);
2229 else if (!valid_filename (line))
2230 return send_error (ctx, GPG_ERR_INV_VALUE);
2232 rc = cache_iscached (line, NULL);
2233 if (client->opts & OPT_LOCK
2234 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2236 unsigned char md5file[16];
2237 gpg_error_t trc = rc;
2239 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2240 if (memcmp (md5file, client->md5file, 16))
2241 cleanup_client (client);
2243 memcpy (client->md5file, md5file, 16);
2244 rc = do_lock (client, 1);
2245 if (!rc)
2246 rc = trc;
2249 return send_error (ctx, rc);
2252 static gpg_error_t
2253 clearcache_command (assuan_context_t ctx, char *line)
2255 gpg_error_t rc = 0, all_rc = 0;
2256 unsigned char md5file[16];
2257 int i;
2258 int t;
2259 int all = 0;
2260 struct client_thread_s *once = NULL;
2262 cache_lock ();
2263 MUTEX_LOCK (&cn_mutex);
2264 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
2266 if (!line || !*line)
2267 all = 1;
2269 t = slist_length (cn_thread_list);
2271 for (i = 0; i < t; i++)
2273 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2274 assuan_peercred_t peer;
2276 if (!thd->cl)
2277 continue;
2279 /* Lock each connected clients' file mutex to prevent any other client
2280 * from accessing the cache entry (the file mutex is locked upon
2281 * command startup). The cache for the entry is not cleared if the
2282 * file mutex is locked by another client to prevent this function
2283 * from blocking.
2285 if (all)
2287 if (thd->cl->filename)
2289 rc = do_validate_peer (ctx, thd->cl->filename, &peer);
2290 all_rc = !all_rc ? rc : all_rc;
2291 if (rc)
2293 rc = 0;
2294 continue;
2298 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
2299 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2301 if (pthread_equal (pthread_self (), thd->tid))
2302 rc = 0;
2303 else
2305 if (!thd->cl->filename ||
2306 cache_iscached (thd->cl->filename,
2307 NULL) == GPG_ERR_NO_DATA)
2309 rc = 0;
2310 continue;
2313 cache_defer_clear (thd->cl->md5file);
2316 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2318 rc = 0;
2319 continue;
2322 if (!rc)
2324 rc = cache_clear (thd->cl->md5file);
2325 cache_unlock_mutex (thd->cl->md5file, 0);
2328 if (rc)
2329 all_rc = rc;
2331 rc = 0;
2333 /* A single data filename was specified. Lock only this data file
2334 * mutex and free the cache entry. */
2335 else
2337 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2338 rc = do_validate_peer (ctx, line, &peer);
2340 if (!rc && !memcmp (thd->cl->md5file, md5file, sizeof (md5file)))
2342 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0,
2343 -1);
2344 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2346 if (pthread_equal (pthread_self (), thd->tid))
2347 rc = 0;
2350 if (!rc)
2352 once = thd;
2353 rc = cache_clear (thd->cl->md5file);
2354 cache_unlock_mutex (thd->cl->md5file, 0);
2356 else
2358 cache_defer_clear (thd->cl->md5file);
2361 break;
2366 /* Only connected clients' cache entries have been cleared. Now clear any
2367 * remaining cache entries without clients but only if there wasn't an
2368 * error from above since this would defeat the locking check of the
2369 * remaining entries. */
2370 if (!all_rc && all)
2372 cache_clear (NULL);
2375 /* No clients are using the specified file. */
2376 else if (!all_rc && !rc && !once)
2378 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2379 rc = cache_clear (md5file);
2382 /* Release the connection mutex. */
2383 pthread_cleanup_pop (1);
2384 cache_unlock ();
2386 if (!rc)
2387 send_status_all (STATUS_CACHE, NULL);
2389 /* One or more files were locked while clearing all cache entries. */
2390 if (all_rc)
2391 rc = all_rc;
2393 return send_error (ctx, rc);
2396 static gpg_error_t
2397 cachetimeout_command (assuan_context_t ctx, char *line)
2399 int timeout;
2400 char **req = str_split (line, " ", 0);
2401 char *p;
2402 gpg_error_t rc = 0;
2403 assuan_peercred_t peer;
2405 if (!req || !*req || !req[1])
2407 strv_free (req);
2408 return send_error (ctx, GPG_ERR_SYNTAX);
2411 errno = 0;
2412 timeout = (int) strtol (req[1], &p, 10);
2413 if (errno != 0 || *p || timeout < -1)
2415 strv_free (req);
2416 return send_error (ctx, GPG_ERR_SYNTAX);
2419 rc = do_validate_peer (ctx, req[0], &peer);
2420 if (!rc)
2422 unsigned char md5file[16];
2424 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, req[0], strlen (req[0]));
2425 rc = cache_set_timeout (md5file, timeout);
2426 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2428 rc = 0;
2429 MUTEX_LOCK (&rcfile_mutex);
2430 config_set_int_param (&global_config, req[0], "cache_timeout",
2431 req[1]);
2432 MUTEX_UNLOCK (&rcfile_mutex);
2436 strv_free (req);
2437 return send_error (ctx, rc);
2440 static gpg_error_t
2441 dump_command (assuan_context_t ctx, char *line)
2443 xmlChar *xml;
2444 int len;
2445 struct client_s *client = assuan_get_pointer (ctx);
2446 gpg_error_t rc;
2448 if (disable_list_and_dump == 1)
2449 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2450 else if (client->thd->peer->uid != invoking_uid)
2451 return send_error (ctx, GPG_ERR_EPERM);
2453 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2455 if (!xml)
2457 log_write ("%s(%i): %s", __FILE__, __LINE__,
2458 pwmd_strerror (GPG_ERR_ENOMEM));
2459 return send_error (ctx, GPG_ERR_ENOMEM);
2462 pthread_cleanup_push (xmlFree, xml);
2463 rc = xfer_data (ctx, (char *) xml, len);
2464 pthread_cleanup_pop (1);
2465 return send_error (ctx, rc);
2468 static gpg_error_t
2469 getconfig_command (assuan_context_t ctx, char *line)
2471 struct client_s *client = assuan_get_pointer (ctx);
2472 gpg_error_t rc = 0;
2473 char filename[255] = { 0 }, param[747] = { 0 };
2474 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2476 if (!line || !*line)
2477 return send_error (ctx, GPG_ERR_SYNTAX);
2479 if (strchr (line, ' '))
2481 sscanf (line, " %254[^ ] %746c", filename, param);
2482 paramp = param;
2483 fp = filename;
2486 if (fp && !valid_filename (fp))
2487 return send_error (ctx, GPG_ERR_INV_VALUE);
2489 paramp = str_down (paramp);
2490 if (!strcmp (paramp, "cipher") && fp)
2492 struct crypto_s *crypto = NULL;
2494 rc = init_client_crypto (&crypto);
2495 if (!rc)
2497 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2498 if (!rc)
2500 const char *t =
2501 gcry_cipher_algo_name (cipher_to_gcrypt (crypto->hdr.flags));
2502 if (t)
2504 tmp = str_dup (t);
2505 if (tmp)
2506 str_down (tmp);
2511 UPDATE_AGENT_CTX (client, crypto);
2512 cleanup_crypto (&crypto);
2513 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2514 return send_error (ctx, rc);
2516 if (!rc && tmp)
2517 goto done;
2519 else if (!strcmp (paramp, "cipher_iterations") && fp)
2521 struct crypto_s *crypto = NULL;
2523 rc = init_client_crypto (&crypto);
2524 if (!rc)
2526 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2527 if (!rc)
2529 tmp = str_asprintf ("%llu",
2530 (unsigned long long) crypto->hdr.
2531 iterations);
2532 if (!tmp)
2533 rc = GPG_ERR_ENOMEM;
2537 UPDATE_AGENT_CTX (client, crypto);
2538 cleanup_crypto (&crypto);
2539 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2540 return send_error (ctx, rc);
2542 if (!rc && tmp)
2543 goto done;
2545 else if (!strcmp (paramp, "passphrase"))
2546 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2548 p = config_get_value (fp ? fp : "global", paramp);
2549 if (!p)
2550 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2552 tmp = expand_homedir (p);
2553 xfree (p);
2554 if (!tmp)
2556 log_write ("%s(%i): %s", __FILE__, __LINE__,
2557 pwmd_strerror (GPG_ERR_ENOMEM));
2558 return send_error (ctx, GPG_ERR_ENOMEM);
2561 done:
2562 p = tmp;
2563 pthread_cleanup_push (xfree, p);
2564 rc = xfer_data (ctx, p, strlen (p));
2565 pthread_cleanup_pop (1);
2566 return send_error (ctx, rc);
2569 struct xpath_s
2571 xmlXPathContextPtr xp;
2572 xmlXPathObjectPtr result;
2573 xmlBufferPtr buf;
2574 char **req;
2577 static void
2578 xpath_command_cleanup (void *arg)
2580 struct xpath_s *xpath = arg;
2582 if (!xpath)
2583 return;
2585 req_cleanup (xpath->req);
2587 if (xpath->buf)
2588 xmlBufferFree (xpath->buf);
2590 if (xpath->result)
2591 xmlXPathFreeObject (xpath->result);
2593 if (xpath->xp)
2594 xmlXPathFreeContext (xpath->xp);
2597 static gpg_error_t
2598 do_xpath (assuan_context_t ctx, char *line)
2600 gpg_error_t rc;
2601 struct client_s *client = assuan_get_pointer (ctx);
2602 struct xpath_s _x = { 0 };
2603 struct xpath_s *xpath = &_x;
2605 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2607 if (strv_printf (&xpath->req, "%s", line) == 0)
2608 return GPG_ERR_ENOMEM;
2611 xpath->xp = xmlXPathNewContext (client->doc);
2612 if (!xpath->xp)
2614 rc = GPG_ERR_BAD_DATA;
2615 goto fail;
2618 xpath->result =
2619 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2620 if (!xpath->result)
2622 rc = GPG_ERR_BAD_DATA;
2623 goto fail;
2626 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2628 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2629 goto fail;
2632 rc = recurse_xpath_nodeset (client->doc, xpath->result->nodesetval,
2633 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2634 NULL);
2635 if (rc)
2636 goto fail;
2637 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2639 rc = GPG_ERR_NO_DATA;
2640 goto fail;
2642 else if (xpath->req[1])
2644 rc = 0;
2645 goto fail;
2648 pthread_cleanup_push (xpath_command_cleanup, &xpath);
2649 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2650 xmlBufferLength (xpath->buf));
2651 pthread_cleanup_pop (0);
2652 fail:
2653 xpath_command_cleanup (xpath);
2654 return rc;
2657 static gpg_error_t
2658 xpath_command (assuan_context_t ctx, char *line)
2660 struct client_s *client = assuan_get_pointer (ctx);
2661 gpg_error_t rc;
2662 struct argv_s *args[] = {
2663 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2664 NULL
2667 if (disable_list_and_dump == 1)
2668 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2669 else if (client->thd->peer->uid != invoking_uid)
2670 return send_error (ctx, GPG_ERR_EPERM);
2672 rc = parse_options (&line, args, client);
2673 if (rc)
2674 return send_error (ctx, rc);
2676 if (client->opts & OPT_INQUIRE)
2678 unsigned char *result;
2679 size_t len;
2681 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2682 if (rc)
2683 return send_error (ctx, rc);
2685 line = (char *) result;
2688 if (!line || !*line)
2689 rc = GPG_ERR_SYNTAX;
2691 if (!rc)
2692 rc = do_xpath (ctx, line);
2694 if (client->opts & OPT_INQUIRE)
2695 xfree (line);
2697 return send_error (ctx, rc);
2700 static gpg_error_t
2701 do_xpathattr (assuan_context_t ctx, char *line)
2703 struct client_s *client = assuan_get_pointer (ctx);
2704 gpg_error_t rc;
2705 char **req = NULL;
2706 int cmd = 0; //SET
2707 struct xpath_s _x = { 0 };
2708 struct xpath_s *xpath = &_x;
2710 if ((req = str_split (line, " ", 3)) == NULL)
2711 return GPG_ERR_ENOMEM;
2713 if (!req[0])
2715 rc = GPG_ERR_SYNTAX;
2716 goto fail;
2719 if (!strcasecmp (req[0], "SET"))
2720 cmd = 0;
2721 else if (!strcasecmp (req[0], "DELETE"))
2722 cmd = 1;
2723 else
2725 rc = GPG_ERR_SYNTAX;
2726 goto fail;
2729 if (!req[1] || !req[2])
2731 rc = GPG_ERR_SYNTAX;
2732 goto fail;
2735 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2737 rc = GPG_ERR_ENOMEM;
2738 goto fail;
2741 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2743 rc = GPG_ERR_SYNTAX;
2744 goto fail;
2747 xpath->xp = xmlXPathNewContext (client->doc);
2748 if (!xpath->xp)
2750 rc = GPG_ERR_BAD_DATA;
2751 goto fail;
2754 xpath->result = xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2755 if (!xpath->result)
2757 rc = GPG_ERR_BAD_DATA;
2758 goto fail;
2761 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2763 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2764 goto fail;
2767 rc = recurse_xpath_nodeset (client->doc, xpath->result->nodesetval,
2768 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2769 (xmlChar *) req[1]);
2771 fail:
2772 xpath_command_cleanup (xpath);
2773 strv_free (req);
2774 return rc;
2777 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2778 static gpg_error_t
2779 xpathattr_command (assuan_context_t ctx, char *line)
2781 struct client_s *client = assuan_get_pointer (ctx);
2782 gpg_error_t rc;
2783 struct argv_s *args[] = {
2784 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2785 NULL
2788 if (disable_list_and_dump == 1)
2789 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2790 else if (client->thd->peer->uid != invoking_uid)
2791 return send_error (ctx, GPG_ERR_EPERM);
2793 rc = parse_options (&line, args, client);
2794 if (rc)
2795 return send_error (ctx, rc);
2797 if (client->opts & OPT_INQUIRE)
2799 unsigned char *result;
2800 size_t len;
2802 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2803 if (rc)
2804 return send_error (ctx, rc);
2806 line = (char *) result;
2809 if (!line || !*line)
2810 rc = GPG_ERR_SYNTAX;
2812 if (!rc)
2813 rc = do_xpathattr (ctx, line);
2815 if (client->opts & OPT_INQUIRE)
2816 xfree (line);
2818 return send_error (ctx, rc);
2821 static gpg_error_t
2822 do_import (struct client_s *client, const char *root_element,
2823 unsigned char *content)
2825 char **dst_path = NULL;
2826 xmlDocPtr doc = NULL;
2827 xmlNodePtr n, root, copy;
2828 gpg_error_t rc;
2830 if (!content || !*content)
2832 xfree (content);
2833 return GPG_ERR_SYNTAX;
2836 if (root_element)
2837 dst_path = str_split (root_element, "\t", 0);
2839 if (dst_path && !valid_element_path (dst_path, 0))
2841 if (dst_path)
2842 strv_free (dst_path);
2844 return GPG_ERR_INV_VALUE;
2847 struct string_s *str = string_new_content ((char *)content);
2848 str = string_prepend (str, "<pwmd>");
2849 str = string_append (str, "</pwmd>");
2850 doc = xmlReadDoc ((xmlChar *) str->str, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2851 string_free (str, 1);
2852 if (!doc)
2854 rc = GPG_ERR_BAD_DATA;
2855 goto fail;
2858 root = xmlDocGetRootElement (doc);
2859 xmlNodePtr root_orig = root->children;
2860 root = root->children;
2861 rc = validate_import (root);
2862 if (rc)
2863 goto fail;
2867 again:
2868 if (dst_path)
2870 char **path = strv_dup (dst_path);
2871 if (!path)
2873 log_write ("%s(%i): %s", __FILE__, __LINE__,
2874 pwmd_strerror (GPG_ERR_ENOMEM));
2875 rc = GPG_ERR_ENOMEM;
2876 goto fail;
2879 xmlChar *a = xmlGetProp (root, (xmlChar *) "_name");
2880 if (!a)
2882 strv_free (path);
2883 rc = GPG_ERR_INV_VALUE;
2884 goto fail;
2887 if (strv_printf (&path, "%s", (char *) a) == 0)
2889 xmlFree (a);
2890 rc = GPG_ERR_ENOMEM;
2891 goto fail;
2894 xmlFree (a);
2895 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2896 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2898 strv_free (path);
2899 goto fail;
2902 if (!rc)
2904 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2905 NULL, NULL, NULL, 0, 0, NULL, 1);
2906 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2908 strv_free (path);
2909 goto fail;
2911 else if (!rc)
2913 xmlUnlinkNode (n);
2914 xmlFreeNode (n);
2915 strv_free (path);
2916 path = NULL;
2917 goto again;
2921 if (rc == GPG_ERR_ELEMENT_NOT_FOUND)
2923 n = create_element_path (client, &path, &rc, NULL);
2924 if (rc)
2925 goto fail;
2928 if (root->children)
2930 copy = xmlCopyNodeList (root->children);
2931 n = xmlAddChildList (n, copy);
2932 if (!n)
2933 rc = GPG_ERR_ENOMEM;
2936 strv_free (path);
2938 else
2940 char **path = NULL;
2942 /* Check if the content root element can create a DTD root element. */
2943 if (!xmlStrEqual ((xmlChar *) "element", root->name))
2945 rc = GPG_ERR_SYNTAX;
2946 goto fail;
2949 xmlChar *a;
2951 if ((a = xmlGetProp (root, (xmlChar *) "_name")) == NULL)
2953 rc = GPG_ERR_SYNTAX;
2954 goto fail;
2957 char *tmp = str_dup ((char *) a);
2958 xmlFree (a);
2959 int literal = is_literal_element (&tmp);
2961 if (!valid_xml_element ((xmlChar *) tmp) || literal)
2963 xfree (tmp);
2964 rc = GPG_ERR_INV_VALUE;
2965 goto fail;
2968 if (strv_printf (&path, "%s", tmp) == 0)
2970 xfree (tmp);
2971 rc = GPG_ERR_ENOMEM;
2972 goto fail;
2975 xfree (tmp);
2976 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 1);
2977 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2979 rc = GPG_ERR_BAD_DATA;
2980 goto fail;
2983 /* Overwriting the existing tree. */
2984 if (!rc)
2986 xmlUnlinkNode (n);
2987 xmlFreeNodeList (n);
2990 rc = 0;
2991 xmlSetProp (root, (xmlChar *) "_name", (xmlChar *) path[0]);
2992 n = xmlCopyNode (root, 1);
2993 n = xmlAddChildList (xmlDocGetRootElement (client->doc), n);
2994 strv_free (path);
2997 if (n && !rc)
2998 rc = update_element_mtime (n->parent);
3000 for (root = root_orig->next; root; root = root->next)
3002 if (root->type == XML_ELEMENT_NODE)
3003 break;
3006 root_orig = root;
3008 while (root);
3010 fail:
3011 if (doc)
3012 xmlFreeDoc (doc);
3014 if (dst_path)
3015 strv_free (dst_path);
3017 return rc;
3020 static gpg_error_t
3021 parse_import_opt_root (void *data, void *value)
3023 struct client_s *client = data;
3025 client->import_root = str_dup (value);
3026 return client->import_root ? 0 : GPG_ERR_ENOMEM;
3029 static gpg_error_t
3030 import_command (assuan_context_t ctx, char *line)
3032 gpg_error_t rc;
3033 struct client_s *client = assuan_get_pointer (ctx);
3034 unsigned char *result;
3035 size_t len;
3036 struct argv_s *args[] = {
3037 &(struct argv_s) {"root", OPTION_TYPE_ARG, parse_import_opt_root},
3038 NULL
3041 xfree (client->import_root);
3042 client->import_root = NULL;
3043 rc = parse_options (&line, args, client);
3044 if (rc)
3045 return send_error (ctx, rc);
3047 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3048 if (rc)
3050 xfree (client->import_root);
3051 client->import_root = NULL;
3052 return send_error (ctx, rc);
3055 rc = do_import (client, client->import_root, result);
3056 xfree (client->import_root);
3057 client->import_root = NULL;
3058 return send_error (ctx, rc);
3061 static gpg_error_t
3062 do_lock (struct client_s *client, int add)
3064 gpg_error_t rc = lock_file_mutex (client, add);
3066 if (!rc)
3067 client->flags |= FLAG_LOCK_CMD;
3069 return rc;
3072 static gpg_error_t
3073 lock_command (assuan_context_t ctx, char *line)
3075 struct client_s *client = assuan_get_pointer (ctx);
3076 gpg_error_t rc = do_lock (client, 0);
3078 return send_error (ctx, rc);
3081 static gpg_error_t
3082 unlock_command (assuan_context_t ctx, char *line)
3084 struct client_s *client = assuan_get_pointer (ctx);
3085 gpg_error_t rc;
3087 rc = unlock_file_mutex (client, 0);
3088 return send_error (ctx, rc);
3091 static gpg_error_t
3092 option_command (assuan_context_t ctx, char *line)
3094 struct client_s *client = assuan_get_pointer (ctx);
3095 gpg_error_t rc = 0;
3096 struct pinentry_option_s *pin_opts = &client->pinentry_opts;
3097 #ifdef WITH_AGENT
3098 struct agent_s *agent = client->crypto->agent;
3099 #endif
3100 char namebuf[255] = { 0 };
3101 char *name = namebuf;
3102 char *value = NULL, *p;
3104 p = strchr (line, '=');
3105 if (!p)
3106 strncpy (namebuf, line, sizeof(namebuf));
3107 else
3109 strncpy (namebuf, line, strlen (line)-strlen (p));
3110 value = p+1;
3113 log_write1 ("OPTION name='%s' value='%s'", name, value);
3115 if (strcasecmp (name, (char *) "log_level") == 0)
3117 long l = 0;
3119 if (value)
3121 l = strtol (value, NULL, 10);
3123 if (l < 0 || l > 2)
3124 return send_error (ctx, GPG_ERR_INV_VALUE);
3127 MUTEX_LOCK (&rcfile_mutex);
3128 config_set_int_param (&global_config, "global", "log_level", value);
3129 MUTEX_UNLOCK (&rcfile_mutex);
3130 goto done;
3132 else if (strcasecmp (name, (char *) "lock-timeout") == 0)
3134 long n = 0;
3135 char *p = NULL;
3137 if (value)
3139 n = strtol (value, &p, 10);
3140 if (p && *p)
3141 return send_error (ctx, GPG_ERR_INV_VALUE);
3144 client->lock_timeout = n;
3145 goto done;
3147 else if (strcasecmp (name, (char *) "NAME") == 0)
3149 char *tmp = pthread_getspecific (thread_name_key);
3151 if (tmp)
3152 xfree (tmp);
3154 if (!value || !*value)
3156 pthread_setspecific (thread_name_key, str_dup (""));
3157 goto done;
3160 pthread_setspecific (thread_name_key, str_dup (value));
3161 goto done;
3163 else if (strcasecmp (name, (char *) "lc-messages") == 0)
3165 xfree (pin_opts->lc_messages);
3166 pin_opts->lc_messages = NULL;
3167 if (value && *value)
3168 pin_opts->lc_messages = str_dup (value);
3169 #ifdef WITH_AGENT
3170 if (use_agent)
3171 rc = set_agent_option (client->crypto->agent, "lc-messages", value);
3172 #endif
3174 else if (strcasecmp (name, (char *) "lc-ctype") == 0)
3176 xfree (pin_opts->lc_ctype);
3177 pin_opts->lc_ctype = NULL;
3178 if (value && *value)
3179 pin_opts->lc_ctype = str_dup (value);
3180 #ifdef WITH_AGENT
3181 if (use_agent)
3182 rc = set_agent_option (client->crypto->agent, "lc-ctype", value);
3183 #endif
3185 else if (strcasecmp (name, (char *) "ttyname") == 0)
3187 xfree (pin_opts->ttyname);
3188 pin_opts->ttyname = NULL;
3189 if (value && *value)
3190 pin_opts->ttyname = str_dup (value);
3191 #ifdef WITH_AGENT
3192 if (use_agent)
3193 rc = set_agent_option (client->crypto->agent, "ttyname", value);
3194 #endif
3196 else if (strcasecmp (name, (char *) "ttytype") == 0)
3198 xfree (pin_opts->ttytype);
3199 pin_opts->ttytype = NULL;
3200 if (value && *value)
3201 pin_opts->ttytype = str_dup (value);
3202 #ifdef WITH_AGENT
3203 if (use_agent)
3204 rc = set_agent_option (client->crypto->agent, "ttytype", value);
3205 #endif
3207 else if (strcasecmp (name, (char *) "display") == 0)
3209 xfree (pin_opts->display);
3210 pin_opts->display = NULL;
3211 if (value && *value)
3212 pin_opts->display = str_dup (value);
3213 #ifdef WITH_AGENT
3214 if (use_agent)
3215 rc = set_agent_option (client->crypto->agent, "display", value);
3216 #endif
3218 else if (strcasecmp (name, (char *) "pinentry-desc") == 0)
3220 xfree (pin_opts->desc);
3221 pin_opts->desc = NULL;
3222 if (value && *value)
3223 pin_opts->desc = str_dup (value);
3225 else if (strcasecmp (name, (char *) "pinentry-title") == 0)
3227 xfree (pin_opts->title);
3228 pin_opts->title = NULL;
3229 if (value && *value)
3230 pin_opts->title = str_dup (value);
3232 else if (strcasecmp (name, (char *) "pinentry-prompt") == 0)
3234 xfree (pin_opts->prompt);
3235 pin_opts->prompt = NULL;
3236 if (value && *value)
3237 pin_opts->prompt = str_dup (value);
3240 else if (strcasecmp (name, "pinentry-timeout") == 0)
3242 char *p = NULL;
3243 int n;
3245 if (!value)
3246 goto done;
3248 n = (int) strtol (value, &p, 10);
3250 if (*p || n < 0)
3251 return send_error (ctx, GPG_ERR_INV_VALUE);
3253 pin_opts->timeout = n;
3254 MUTEX_LOCK (&rcfile_mutex);
3255 config_set_int_param (&global_config,
3256 client->filename ? client->filename : "global",
3257 "pinentry_timeout", value);
3258 MUTEX_UNLOCK (&rcfile_mutex);
3259 goto done;
3261 else if (strcasecmp (name, "disable-pinentry") == 0)
3263 char *p = NULL;
3264 int n = 1;
3266 if (value && *value)
3268 n = (int) strtol (value, &p, 10);
3269 if (*p || n < 0 || n > 1)
3270 return send_error (ctx, GPG_ERR_INV_VALUE);
3273 if (n)
3274 client->flags |= FLAG_NO_PINENTRY;
3275 else
3276 client->flags &= ~FLAG_NO_PINENTRY;
3278 #ifdef WITH_AGENT
3279 if (use_agent)
3281 if (client->flags & FLAG_NO_PINENTRY)
3282 rc = set_agent_option (client->crypto->agent, "pinentry-mode",
3283 "loopback");
3284 else
3285 rc = set_agent_option (client->crypto->agent, "pinentry-mode",
3286 "ask");
3288 if (rc)
3289 return send_error (ctx, rc);
3291 #endif
3293 else
3294 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
3296 done:
3297 #ifdef WITH_AGENT
3298 if (!rc && use_agent && agent)
3300 rc = pinentry_merge_options (&client->crypto->agent->pinentry_opts,
3301 pin_opts);
3303 #endif
3305 return send_error (ctx, rc);
3308 static gpg_error_t
3309 do_rename (assuan_context_t ctx, char *line)
3311 struct client_s *client = assuan_get_pointer (ctx);
3312 gpg_error_t rc;
3313 char **req, **src, *dst;
3314 xmlNodePtr n, ndst;
3316 req = str_split (line, " ", 0);
3318 if (!req || !req[0] || !req[1])
3320 strv_free (req);
3321 return GPG_ERR_SYNTAX;
3324 dst = req[1];
3325 is_literal_element (&dst);
3327 if (!valid_xml_element ((xmlChar *) dst))
3329 strv_free (req);
3330 return GPG_ERR_INV_VALUE;
3333 if (strchr (req[0], '\t'))
3334 src = str_split (req[0], "\t", 0);
3335 else
3336 src = str_split (req[0], " ", 0);
3338 if (!src || !*src)
3340 rc = GPG_ERR_SYNTAX;
3341 goto fail;
3344 n = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3345 if (src[1] && n)
3346 n = find_elements (client, client->doc, n->children, src + 1, &rc, NULL, NULL,
3347 NULL, 0, 0, NULL, 0);
3349 if (!n)
3350 goto fail;
3352 xmlChar *a = xmlGetProp (n, (xmlChar *) "_name");
3353 if (!a)
3355 rc = GPG_ERR_ENOMEM;
3356 goto fail;
3359 /* To prevent unwanted effects:
3361 * <root name="a"><b/></root>
3363 * RENAME a<TAB>b b
3365 if (xmlStrEqual (a, (xmlChar *) dst))
3367 xmlFree (a);
3368 rc = GPG_ERR_AMBIGUOUS_NAME;
3369 goto fail;
3372 xmlFree (a);
3373 char **tmp = NULL;
3374 if (src[1])
3376 char **p;
3378 for (p = src; *p; p++)
3380 if (!*(p + 1))
3381 break;
3382 strv_printf (&tmp, "%s", *p);
3386 strv_printf (&tmp, "!%s", dst);
3387 ndst = find_root_element (client, client->doc, &tmp, &rc, NULL, 0, 0);
3388 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3390 strv_free (tmp);
3391 goto fail;
3394 if (tmp[1] && ndst)
3395 ndst = find_elements (client, client->doc, ndst->children, tmp + 1, &rc, NULL,
3396 NULL, NULL, 0, 0, NULL, 0);
3398 strv_free (tmp);
3399 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3400 goto fail;
3402 rc = 0;
3404 /* Target may exist:
3406 * <root name="a"/>
3407 * <root name="b" target="a"/>
3409 * RENAME b a
3411 * Would need to do:
3412 * RENAME !b a
3414 if (ndst == n)
3416 rc = GPG_ERR_AMBIGUOUS_NAME;
3417 goto fail;
3420 if (ndst)
3422 unlink_node (ndst);
3423 xmlFreeNodeList (ndst);
3426 rc = add_attribute (n, "_name", dst);
3428 fail:
3429 strv_free (req);
3430 strv_free (src);
3431 return rc;
3434 static gpg_error_t
3435 rename_command (assuan_context_t ctx, char *line)
3437 struct client_s *client = assuan_get_pointer (ctx);
3438 gpg_error_t rc;
3439 struct argv_s *args[] = {
3440 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3441 NULL
3444 rc = parse_options (&line, args, client);
3445 if (rc)
3446 return send_error (ctx, rc);
3448 if (client->opts & OPT_INQUIRE)
3450 unsigned char *result;
3451 size_t len;
3453 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3454 if (rc)
3455 return send_error (ctx, rc);
3457 line = (char *) result;
3460 rc = do_rename (ctx, line);
3462 if (client->opts & OPT_INQUIRE)
3463 xfree (line);
3465 return send_error (ctx, rc);
3468 static gpg_error_t
3469 do_copy (assuan_context_t ctx, char *line)
3471 struct client_s *client = assuan_get_pointer (ctx);
3472 gpg_error_t rc;
3473 char **req, **src = NULL, **dst = NULL;
3474 xmlNodePtr nsrc, ndst, new = NULL;
3476 req = str_split (line, " ", 0);
3477 if (!req || !req[0] || !req[1])
3479 strv_free (req);
3480 return GPG_ERR_SYNTAX;
3483 if (strchr (req[0], '\t'))
3484 src = str_split (req[0], "\t", 0);
3485 else
3486 src = str_split (req[0], " ", 0);
3488 if (!src || !*src)
3490 rc = GPG_ERR_SYNTAX;
3491 goto fail;
3494 if (strchr (req[1], '\t'))
3495 dst = str_split (req[1], "\t", 0);
3496 else
3497 dst = str_split (req[1], " ", 0);
3499 if (!dst || !*dst)
3501 rc = GPG_ERR_SYNTAX;
3502 goto fail;
3505 if (!valid_element_path (dst, 0))
3507 rc = GPG_ERR_INV_VALUE;
3508 goto fail;
3511 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3512 if (nsrc && src[1])
3513 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc, NULL,
3514 NULL, NULL, 0, 0, NULL, 0);
3516 if (!nsrc)
3517 goto fail;
3519 new = xmlCopyNodeList (nsrc);
3520 if (!new)
3522 rc = GPG_ERR_ENOMEM;
3523 goto fail;
3526 int create = 0;
3527 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3528 if (ndst && dst[1])
3530 if (ndst->children)
3531 ndst = find_elements (client, client->doc, ndst->children, dst + 1, &rc, NULL,
3532 NULL, create_target_elements_cb, 0, 0, NULL, 0);
3533 else
3534 create = 1;
3536 else
3537 create = 1;
3539 if (!ndst && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3540 goto fail;
3541 else if (!ndst || create)
3543 ndst = create_element_path (client, &dst, &rc, NULL);
3544 if (!ndst)
3545 goto fail;
3548 /* Merge any attributes from the src node to the initial dst node. */
3549 for (xmlAttrPtr attr = new->properties; attr; attr = attr->next)
3551 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3552 continue;
3554 xmlAttrPtr a = xmlHasProp (ndst, attr->name);
3555 if (a)
3556 xmlRemoveProp (a);
3558 xmlChar *tmp = xmlNodeGetContent (attr->children);
3559 xmlNewProp (ndst, attr->name, tmp);
3560 xmlFree (tmp);
3561 rc = add_attribute (ndst, NULL, NULL);
3564 xmlNodePtr n = ndst->children;
3565 xmlUnlinkNode (n);
3566 xmlFreeNodeList (n);
3567 ndst->children = NULL;
3569 if (new->children)
3571 n = xmlCopyNodeList (new->children);
3572 if (!n)
3574 rc = GPG_ERR_ENOMEM;
3575 goto fail;
3578 n = xmlAddChildList (ndst, n);
3579 if (!n)
3581 rc = GPG_ERR_ENOMEM;
3582 goto fail;
3585 rc = update_element_mtime (xmlDocGetRootElement (client->doc) ==
3586 ndst->parent ? ndst : ndst->parent);
3589 fail:
3590 if (new)
3592 xmlUnlinkNode (new);
3593 xmlFreeNodeList (new);
3596 if (req)
3597 strv_free (req);
3599 if (src)
3600 strv_free (src);
3602 if (dst)
3603 strv_free (dst);
3605 return rc;
3608 static gpg_error_t
3609 copy_command (assuan_context_t ctx, char *line)
3611 struct client_s *client = assuan_get_pointer (ctx);
3612 gpg_error_t rc;
3613 struct argv_s *args[] = {
3614 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3615 NULL
3618 rc = parse_options (&line, args, client);
3619 if (rc)
3620 return send_error (ctx, rc);
3622 if (client->opts & OPT_INQUIRE)
3624 unsigned char *result;
3625 size_t len;
3627 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3628 if (rc)
3629 return send_error (ctx, rc);
3631 line = (char *) result;
3634 rc = do_copy (ctx, line);
3636 if (client->opts & OPT_INQUIRE)
3637 xfree (line);
3639 return send_error (ctx, rc);
3642 static gpg_error_t
3643 do_move (assuan_context_t ctx, char *line)
3645 struct client_s *client = assuan_get_pointer (ctx);
3646 gpg_error_t rc;
3647 char **req, **src = NULL, **dst = NULL;
3648 xmlNodePtr nsrc, ndst = NULL;
3650 req = str_split (line, " ", 0);
3652 if (!req || !req[0] || !req[1])
3654 strv_free (req);
3655 return GPG_ERR_SYNTAX;
3658 if (strchr (req[0], '\t'))
3659 src = str_split (req[0], "\t", 0);
3660 else
3661 src = str_split (req[0], " ", 0);
3663 if (!src || !*src)
3665 rc = GPG_ERR_SYNTAX;
3666 goto fail;
3669 if (strchr (req[1], '\t'))
3670 dst = str_split (req[1], "\t", 0);
3671 else
3672 dst = str_split (req[1], " ", 0);
3674 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3675 if (nsrc && src[1])
3676 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc, NULL,
3677 NULL, NULL, 0, 0, NULL, 0);
3679 if (!nsrc)
3680 goto fail;
3682 if (dst)
3684 if (!valid_element_path (dst, 0))
3686 rc = GPG_ERR_INV_VALUE;
3687 goto fail;
3690 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3691 if (ndst && dst[1])
3692 ndst = find_elements (client, client->doc, ndst->children, dst + 1, &rc, NULL,
3693 NULL, NULL, 0, 0, NULL, 0);
3695 else
3696 ndst = xmlDocGetRootElement (client->doc);
3698 for (xmlNodePtr n = ndst; n; n = n->parent)
3700 if (n == nsrc)
3702 rc = GPG_ERR_CONFLICT;
3703 goto fail;
3707 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3708 goto fail;
3710 rc = 0;
3712 if (ndst)
3714 xmlChar *a = node_has_attribute (nsrc, (xmlChar *) "_name");
3716 xmlNodePtr dup = find_element (client, ndst->children, (char *) a,
3717 NULL, &rc);
3718 xmlFree (a);
3720 if (rc)
3721 goto fail;
3723 if (dup)
3725 if (dup == nsrc)
3726 goto fail;
3728 if (ndst == xmlDocGetRootElement (client->doc))
3730 xmlNodePtr n = nsrc;
3731 int match = 0;
3733 while (n->parent && n->parent != ndst)
3734 n = n->parent;
3736 xmlChar *a = node_has_attribute (n, (xmlChar *) "_name");
3737 xmlChar *b = node_has_attribute (nsrc, (xmlChar *) "_name");
3739 if (xmlStrEqual (a, b))
3741 match = 1;
3742 xmlUnlinkNode (nsrc);
3743 xmlUnlinkNode (n);
3744 xmlFreeNodeList (n);
3747 xmlFree (a);
3748 xmlFree (b);
3750 if (!match)
3752 xmlUnlinkNode (dup);
3753 xmlFreeNodeList (dup);
3756 else
3757 xmlUnlinkNode (dup);
3761 if (!ndst && dst)
3763 xmlChar *name = node_has_attribute (nsrc, (xmlChar *) "_name");
3765 if (nsrc->parent == xmlDocGetRootElement (client->doc)
3766 && !strcmp ((char *) name, *dst))
3768 xmlFree (name);
3769 rc = GPG_ERR_CONFLICT;
3770 goto fail;
3773 xmlFree (name);
3774 ndst = create_element_path (client, &dst, &rc, nsrc);
3777 if (!ndst)
3778 goto fail;
3780 update_element_mtime (nsrc->parent);
3781 xmlUnlinkNode (nsrc);
3782 ndst = xmlAddChildList (ndst, nsrc);
3784 if (!ndst)
3785 rc = GPG_ERR_ENOMEM;
3786 else
3787 update_element_mtime (ndst->parent);
3789 fail:
3790 if (req)
3791 strv_free (req);
3793 if (src)
3794 strv_free (src);
3796 if (dst)
3797 strv_free (dst);
3799 return rc;
3802 static gpg_error_t
3803 move_command (assuan_context_t ctx, char *line)
3805 struct client_s *client = assuan_get_pointer (ctx);
3806 gpg_error_t rc;
3807 struct argv_s *args[] = {
3808 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3809 NULL
3812 rc = parse_options (&line, args, client);
3813 if (rc)
3814 return send_error (ctx, rc);
3816 if (client->opts & OPT_INQUIRE)
3818 unsigned char *result;
3819 size_t len;
3821 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3822 if (rc)
3823 return send_error (ctx, rc);
3825 line = (char *) result;
3828 rc = do_move (ctx, line);
3830 if (client->opts & OPT_INQUIRE)
3831 xfree (line);
3833 return send_error (ctx, rc);
3836 static gpg_error_t
3837 ls_command (assuan_context_t ctx, char *line)
3839 gpg_error_t rc;
3840 char *tmp = str_asprintf ("%s/data", homedir);
3841 char *dir = expand_homedir (tmp);
3842 DIR *d = opendir (dir);
3844 rc = gpg_error_from_errno (errno);
3845 xfree (tmp);
3847 if (!d)
3849 xfree (dir);
3850 return send_error (ctx, rc);
3853 size_t len =
3854 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3855 struct dirent *p = xmalloc (len), *cur = NULL;
3856 char *list = NULL;
3858 xfree (dir);
3859 rc = 0;
3861 while (!readdir_r (d, p, &cur) && cur)
3863 if (cur->d_name[0] == '.' && cur->d_name[1] == '\0')
3864 continue;
3865 else if (cur->d_name[0] == '.' && cur->d_name[1] == '.'
3866 && cur->d_name[2] == '\0')
3867 continue;
3869 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3871 if (!tmp)
3873 if (list)
3874 xfree (list);
3876 rc = GPG_ERR_ENOMEM;
3877 break;
3880 xfree (list);
3881 list = tmp;
3884 closedir (d);
3885 xfree (p);
3887 if (rc)
3888 return send_error (ctx, rc);
3890 if (!list)
3891 return send_error (ctx, GPG_ERR_NO_DATA);
3893 list[strlen (list) - 1] = 0;
3894 rc = xfer_data (ctx, list, strlen (list));
3895 xfree (list);
3896 return send_error (ctx, rc);
3899 static gpg_error_t
3900 bye_notify (assuan_context_t ctx, char *line)
3902 struct client_s *cl = assuan_get_pointer (ctx);
3904 #ifdef WITH_GNUTLS
3905 if (cl->thd->remote)
3907 int rc;
3911 struct timeval tv = { 0, 50000 };
3913 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3914 if (rc == GNUTLS_E_AGAIN)
3915 select (0, NULL, NULL, NULL, &tv);
3917 while (rc == GNUTLS_E_AGAIN);
3919 #endif
3921 /* This will let assuan_process_next() return. */
3922 fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK);
3923 cl->last_rc = 0; // BYE command result
3924 return 0;
3927 static gpg_error_t
3928 reset_notify (assuan_context_t ctx, char *line)
3930 struct client_s *client = assuan_get_pointer (ctx);
3932 if (client)
3933 cleanup_client (client);
3935 return 0;
3939 * This is called before every Assuan command.
3941 static gpg_error_t
3942 command_startup (assuan_context_t ctx, const char *name)
3944 struct client_s *client = assuan_get_pointer (ctx);
3945 gpg_error_t rc;
3946 struct command_table_s *cmd = NULL;
3948 log_write1 ("command='%s'", name);
3949 client->last_rc = client->opts = 0;
3951 for (int i = 0; command_table[i]; i++)
3953 if (!strcasecmp (name, command_table[i]->name))
3955 if (command_table[i]->ignore_startup)
3956 return 0;
3957 cmd = command_table[i];
3958 break;
3962 client->last_rc = rc = gpg_error (file_modified (client, cmd));
3963 return rc;
3967 * This is called after every Assuan command.
3969 static void
3970 command_finalize (assuan_context_t ctx, gpg_error_t rc)
3972 struct client_s *client = assuan_get_pointer (ctx);
3974 if (!(client->flags & FLAG_LOCK_CMD))
3975 unlock_file_mutex (client, 0);
3977 log_write1 (_("command completed: rc=%u"), client->last_rc);
3978 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
3979 #ifdef WITH_GNUTLS
3980 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
3981 #endif
3984 static gpg_error_t
3985 help_command (assuan_context_t ctx, char *line)
3987 gpg_error_t rc;
3988 int i;
3990 if (!line || !*line)
3992 char *tmp;
3993 char *help = str_dup (_("Usage: HELP [<COMMAND>]\n"
3994 "For commands that take an element path as an argument, each element is "
3995 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3996 "\n" "COMMANDS:"));
3998 for (i = 0; command_table[i]; i++)
4000 if (!command_table[i]->help)
4001 continue;
4003 tmp = str_asprintf ("%s %s", help, command_table[i]->name);
4004 xfree (help);
4005 help = tmp;
4008 tmp = strip_texi_and_wrap (help);
4009 xfree (help);
4010 rc = xfer_data (ctx, tmp, strlen (tmp));
4011 xfree (tmp);
4012 return send_error (ctx, rc);
4015 for (i = 0; command_table[i]; i++)
4017 if (!strcasecmp (line, command_table[i]->name))
4019 char *help, *tmp;
4021 if (!command_table[i]->help)
4022 break;
4024 help = strip_texi_and_wrap (command_table[i]->help);
4025 tmp = str_asprintf (_("Usage: %s"), help);
4026 xfree (help);
4027 rc = xfer_data (ctx, tmp, strlen (tmp));
4028 xfree (tmp);
4029 return send_error (ctx, rc);
4033 return send_error (ctx, GPG_ERR_INV_NAME);
4036 static void
4037 new_command (const char *name, int ignore, int unlock,
4038 gpg_error_t (*handler) (assuan_context_t, char *),
4039 const char *help)
4041 int i = 0;
4043 if (command_table)
4044 for (i = 0; command_table[i]; i++);
4046 command_table =
4047 xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
4048 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
4049 command_table[i]->name = name;
4050 command_table[i]->handler = handler;
4051 command_table[i]->ignore_startup = ignore;
4052 command_table[i]->unlock = unlock;
4053 command_table[i++]->help = help;
4054 command_table[i] = NULL;
4057 void
4058 deinit_commands ()
4060 int i;
4062 for (i = 0; command_table[i]; i++)
4063 xfree (command_table[i]);
4065 xfree (command_table);
4068 static int
4069 sort_commands (const void *arg1, const void *arg2)
4071 struct command_table_s *const *a = arg1;
4072 struct command_table_s *const *b = arg2;
4074 if (!*a || !*b)
4075 return 0;
4076 else if (*a && !*b)
4077 return 1;
4078 else if (!*a && *b)
4079 return -1;
4081 return strcmp ((*a)->name, (*b)->name);
4084 static gpg_error_t
4085 passwd_command (assuan_context_t ctx, char *line)
4087 struct client_s *client = assuan_get_pointer (ctx);
4088 gpg_error_t rc;
4089 struct argv_s *args[] = {
4090 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
4091 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
4092 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG, parse_opt_no_passphrase},
4093 NULL
4096 if (client->flags & FLAG_NEW)
4097 return send_error (ctx, GPG_ERR_INV_STATE);
4099 client->crypto->save.s2k_count =
4100 config_get_ulong (client->filename, "s2k_count");
4101 rc = parse_options (&line, args, client);
4102 if (rc)
4103 return send_error (ctx, rc);
4105 if (!rc && client->opts & OPT_RESET)
4107 rc = cache_clear (client->md5file);
4108 if (!rc)
4109 send_status_all (STATUS_CACHE, NULL);
4112 if (!rc)
4114 if (!IS_PKI (client->crypto))
4116 struct crypto_s *crypto;
4118 xfree (client->crypto->filename);
4119 client->crypto->filename = str_dup (client->filename);
4120 rc = change_passwd (ctx, client->filename,
4121 client->flags & FLAG_NO_PINENTRY, &crypto,
4122 (client->opts & OPT_NO_PASSPHRASE));
4123 if (!rc)
4125 cleanup_crypto (&client->crypto);
4126 client->crypto = crypto;
4127 update_checksum (client);
4128 cleanup_crypto_stage1 (client->crypto);
4131 #ifdef WITH_AGENT
4132 else
4134 if (client->crypto->save.s2k_count)
4135 rc = send_to_agent (client->crypto->agent, NULL, NULL,
4136 "OPTION s2k-count=%lu",
4137 client->crypto->save.s2k_count);
4139 if (!rc)
4140 rc = agent_passwd (client->crypto);
4142 #endif
4145 return send_error (ctx, rc);
4148 static gpg_error_t
4149 parse_keygrip_opt_sign (void *data, void *value)
4151 struct client_s *client = data;
4153 (void) value;
4154 client->opts |= OPT_SIGN;
4155 return 0;
4158 static gpg_error_t
4159 keygrip_command (assuan_context_t ctx, char *line)
4161 struct client_s *client = assuan_get_pointer (ctx);
4162 gpg_error_t rc;
4163 struct crypto_s *crypto = NULL;
4164 struct argv_s *args[] = {
4165 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_keygrip_opt_sign},
4166 NULL
4169 if (!line || !*line)
4170 return send_error (ctx, GPG_ERR_SYNTAX);
4172 rc = parse_options (&line, args, client);
4173 if (rc)
4174 return send_error (ctx, rc);
4176 if (!valid_filename (line))
4177 return send_error (ctx, GPG_ERR_INV_VALUE);
4179 rc = init_client_crypto (&crypto);
4180 if (rc)
4181 return send_error (ctx, rc);
4183 rc = read_data_file (line, crypto);
4184 if (!rc)
4186 char *hexgrip = NULL;
4188 if (!IS_PKI (crypto))
4190 cleanup_crypto (&crypto);
4191 return send_error (ctx, GPG_ERR_NOT_SUPPORTED);
4194 if (client->opts & OPT_SIGN)
4196 if (valid_keygrip (crypto->sign_grip, sizeof (crypto->sign_grip)))
4197 hexgrip = bin2hex (crypto->sign_grip, sizeof (crypto->sign_grip));
4200 if (!hexgrip)
4201 hexgrip = bin2hex (crypto->grip, sizeof (crypto->grip));
4203 if (!hexgrip)
4204 rc = GPG_ERR_ENOMEM;
4205 else
4206 rc = xfer_data (ctx, hexgrip, strlen (hexgrip));
4208 xfree (hexgrip);
4211 UPDATE_AGENT_CTX (client, crypto);
4212 cleanup_crypto (&crypto);
4213 return send_error (ctx, rc);
4216 static gpg_error_t
4217 parse_opt_data (void *data, void *value)
4219 struct client_s *client = data;
4221 (void) value;
4222 client->opts |= OPT_DATA;
4223 return 0;
4226 static gpg_error_t
4227 getinfo_command (assuan_context_t ctx, char *line)
4229 struct client_s *client = assuan_get_pointer (ctx);
4230 gpg_error_t rc;
4231 char buf[ASSUAN_LINELENGTH];
4232 struct argv_s *args[] = {
4233 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4234 NULL
4237 rc = parse_options (&line, args, client);
4238 if (rc)
4239 return send_error (ctx, rc);
4241 if (!strcasecmp (line, "clients"))
4243 if (client->opts & OPT_DATA)
4245 MUTEX_LOCK (&cn_mutex);
4246 snprintf (buf, sizeof (buf), "%i", slist_length (cn_thread_list));
4247 MUTEX_UNLOCK (&cn_mutex);
4248 rc = xfer_data (ctx, buf, strlen (buf));
4250 else
4251 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4253 else if (!strcasecmp (line, "cache"))
4255 if (client->opts & OPT_DATA)
4257 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4258 rc = xfer_data (ctx, buf, strlen (buf));
4260 else
4261 rc = send_status (ctx, STATUS_CACHE, NULL);
4263 else if (!strcasecmp (line, "pid"))
4265 char buf[32];
4266 pid_t pid = getpid ();
4268 snprintf (buf, sizeof (buf), "%i", pid);
4269 rc = xfer_data (ctx, buf, strlen (buf));
4271 else if (!strcasecmp (line, "version"))
4273 char *buf = str_asprintf ("0x%06x %s%s", VERSION_HEX,
4274 #ifdef WITH_LIBACL
4275 "ACL "
4276 #endif
4277 #ifdef WITH_GNUTLS
4278 "GNUTLS "
4279 #endif
4280 #ifdef WITH_QUALITY
4281 "QUALITY "
4282 #endif
4283 "", use_agent ? "AGENT" : "");
4284 rc = xfer_data (ctx, buf, strlen (buf));
4285 xfree (buf);
4287 else if (!strcasecmp (line, "last_error"))
4289 if (client->last_error)
4290 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4291 else
4292 rc = GPG_ERR_NO_DATA;
4294 else
4295 rc = gpg_error (GPG_ERR_SYNTAX);
4297 return send_error (ctx, rc);
4300 #ifdef WITH_AGENT
4301 static gpg_error_t
4302 send_data_cb (void *user, const void *buf, size_t len)
4304 assuan_context_t ctx = user;
4306 return assuan_send_data (ctx, buf, len);
4309 static gpg_error_t
4310 send_status_cb (void *user, const char *line)
4312 assuan_context_t ctx = user;
4313 char keyword[200], *k;
4314 const char *p;
4316 for (p = line, k = keyword; *p; p++)
4318 if (isspace (*p))
4319 break;
4321 *k++ = *p;
4324 *k = 0;
4325 if (*p == '#')
4326 p++;
4328 while (isspace (*p))
4329 p++;
4331 return assuan_write_status (ctx, keyword, *p ? p : NULL);
4333 #endif
4335 static gpg_error_t
4336 agent_command (assuan_context_t ctx, char *line)
4338 gpg_error_t rc = 0;
4340 if (!use_agent)
4341 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
4343 #ifdef WITH_AGENT
4344 struct client_s *client = assuan_get_pointer (ctx);
4346 if (!line || !*line)
4347 return send_error (ctx, GPG_ERR_SYNTAX);
4349 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 1);
4350 rc = assuan_transact (client->crypto->agent->ctx, line, send_data_cb,
4351 client->ctx, agent_loopback_cb, client->crypto,
4352 send_status_cb, client->ctx);
4353 if (gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
4355 char *line;
4356 size_t len;
4358 rc = assuan_write_line (client->crypto->agent->ctx, "CAN");
4359 if (!rc)
4361 rc = assuan_read_line (client->crypto->agent->ctx, &line, &len);
4362 if (!rc)
4363 rc = gpg_error (GPG_ERR_ASS_CANCELED);
4367 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 0);
4368 #endif
4369 return send_error (ctx, rc);
4372 void
4373 init_commands ()
4375 /* !BEGIN-HELP-TEXT!
4377 * This comment is used as a marker to generate the offline documentation
4378 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4379 * script to determine where commands begin and end.
4381 new_command("HELP", 1, 1, help_command, _(
4382 "HELP [<COMMAND>]\n"
4383 "Show available commands or command specific help text."
4386 new_command("AGENT", 1, 1, agent_command, _(
4387 "AGENT <command>\n"
4388 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4389 "@command{gpg-agent}."
4392 new_command("GETINFO", 1, 1, getinfo_command, _(
4393 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4394 "Get server and other information: @var{cache} returns the number of cached "
4395 "documents via a status message. @var{clients} returns the number of "
4396 "connected clients via a status message. @var{pid} returns the process ID "
4397 "number of the server via a data response. @var{VERSION} returns the server "
4398 "version number and compile-time features with a data response with each "
4399 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4400 "the last failed command when available. @xref{Status Messages}. "
4401 "\n"
4402 "When the @option{--data} option is specified then the result will be sent "
4403 "via a data response rather than a status message."
4406 new_command("PASSWD", 0, 0, passwd_command, _(
4407 "PASSWD [--reset] [--s2k-count=N] [--no-passphrase]\n"
4408 "Changes the passphrase of the secret key required to open the current "
4409 "file or the passphrase of a symmetrically encrypted data file. When the "
4410 "@option{--reset} option is passed then the cache entry for the current "
4411 "file will be reset and the passphrase, if any, will be required during the "
4412 "next @code{OPEN} (@pxref{OPEN})."
4413 "\n"
4414 "The @option{--s2k-count} option sets or changes (@pxref{SAVE}) the number "
4415 "of hash iterations for a passphrase and must be either @code{0} to use "
4416 "the calibrated count of the machine (the default), or a value greater than "
4417 "or equal to @code{65536}. This option has no effect for symmetrically "
4418 "encrypted data files."
4419 "\n"
4420 "The @option{--no-passphrase} option will prevent requiring a passphrase for "
4421 "the data file, although a passphrase may be required when changing it."
4424 new_command("KEYGRIP", 1, 1, keygrip_command, _(
4425 "KEYGRIP [--sign] <filename>\n"
4426 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4427 "data response."
4428 "\n"
4429 "When the @option{--sign} option is specified then the key used for signing "
4430 "of the specified @var{filename} will be returned."
4431 "\n"
4432 "For symmetrically encrypted data files this command returns the error "
4433 "GPG_ERR_NOT_SUPPORTED."
4436 new_command("OPEN", 1, 1, open_command, _(
4437 "OPEN [--lock] <filename> [<passphrase>]\n"
4438 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4439 "found on the file-system then a new document will be created. If the file "
4440 "is found, it is looked for in the file cache. If cached and no "
4441 "@var{passphrase} was specified then the cached document is opened. When not "
4442 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4443 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4444 "specified."
4445 "\n"
4446 "When the @option{--lock} option is passed then the file mutex will be "
4447 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4448 "file has been opened."
4451 new_command("SAVE", 0, 0, save_command, _(
4452 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring] [--sign-keygrip=hexstring]\n"
4453 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4454 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4455 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4456 "keypair will be generated and a pinentry will be used to prompt for the "
4457 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4458 "passed in which case the data file will not be passphrase protected. "
4459 "\n"
4460 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4461 "passphrase retrieval and caching of new files when @command{gpg-agent} "
4462 "use is enabled. The datafile will be symmetrically encrypted and will not "
4463 "use or generate any keypair."
4464 "\n"
4465 "The @option{--reset} option will clear the cache entry for the current file "
4466 "and require a passphrase, if needed, before saving."
4467 "\n"
4468 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4469 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4470 "(@pxref{Configuration}) for available ciphers."
4471 "\n"
4472 "The @option{--cipher-iterations} option specifies the number of times to "
4473 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4474 "\n"
4475 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4476 "the client to obtain the key paramaters to use when generating a new "
4477 "keypair. The inquired data is expected to be an S-expression. If not "
4478 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4479 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4480 "that when this option is specified a new keypair will be generated "
4481 "reguardless if the file is a new one and that if the data file is protected "
4482 "the passphrase to open it will be required before generating the new keypair."
4483 "\n"
4484 "You can encrypt the data file to a public key other than the one that it "
4485 "was originally encrypted with by passing the @option{--keygrip} option with "
4486 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4487 "be of any key that @command{gpg-agent} knows about. The "
4488 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4489 "secret key. Use the @code{KEYGRIP} (@pxref{KEYGRIP}) command to obtain the "
4490 "keygrip of an existing data file. This option may be needed when using a "
4491 "smartcard. This option has no effect with symmetrically encrypted data files."
4492 "\n"
4493 "The @option{--s2k-count} option sets number of hash iterations for a "
4494 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4495 "value and is the default. This setting only affects new files. To change "
4496 "the setting use the @code{PASSWD} command (@pxref{PASSWD}). This option "
4497 "has no effect with symmetrically encrypted data files."
4500 new_command("ISCACHED", 1, 0, iscached_command, _(
4501 "ISCACHED [--lock] <filename>\n"
4502 "An @emph{OK} response is returned if the specified @var{filename} is found "
4503 "in the file cache. If not found in the cache but exists on the filesystem "
4504 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4505 "returned."
4506 "\n"
4507 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4508 "file exists; it does not need to be opened nor cached. The lock will be "
4509 "released when the client exits or sends the @code{UNLOCK} (@pxref{UNLOCK}) "
4510 "command."
4513 new_command("CLEARCACHE", 1, 1, clearcache_command, _(
4514 "CLEARCACHE [<filename>]\n"
4515 "Clears a file cache entry for all or the specified @var{filename}."
4518 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command, _(
4519 "CACHETIMEOUT <filename> <seconds>\n"
4520 "The time in @var{seconds} until @var{filename} will be removed from the "
4521 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4522 "the passphrase for each @code{OPEN} or @code{SAVE} command (@pxref{OPEN}, "
4523 "@pxref{SAVE}). @xref{Configuration}, and the @code{cache_timeout} "
4524 "parameter."
4527 new_command("LIST", 0, 1, list_command, _(
4528 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4529 "If no element path is given then a newline separated list of root elements "
4530 "is returned with a data response. If given, then all reachable elements "
4531 "of the specified element path are returned unless the @option{--no-recurse} "
4532 "option is specified. If specified, only the child elements of the element "
4533 "path are returned without recursing into grandchildren. Each resulting "
4534 "element is prefixed with the literal @code{!} character when the element "
4535 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4536 "\n"
4537 "When the @option{--verbose} option is passed then each element path "
4538 "returned will have zero or more flags appened to it. These flags are "
4539 "delimited from the element path by a single space character. A flag itself "
4540 "is a single character. Flag @code{+} indicates that there are child nodes of "
4541 "the current element path. Flag @code{E} indicates that an element of an "
4542 "element path contained in a @var{target} attribute could not be found. Flag "
4543 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4544 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4545 "of the @var{target} attribute contained in the current element (see below)."
4546 "\n"
4547 "The @option{--with-target} option implies @option{--verbose} and will append "
4548 "an additional flag @code{T} followed by a single space then an element path. "
4549 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4550 "current element when it contains a @var{target} attribute. When no "
4551 "@var{target} attribute is found then no flag will be appended."
4552 "\n"
4553 "The @option{--no-recurse} option limits the amount of data returned to only "
4554 "the listing of children of the specified element path and not any "
4555 "grandchildren."
4556 "\n"
4557 "The @option{--all} option lists the entire element tree for each root "
4558 "element. This option also implies option @option{--verbose}."
4559 "\n"
4560 "When the @option{--inquire} option is passed then all remaining non-option "
4561 "arguments are retrieved via a server @emph{INQUIRE}."
4564 new_command("REALPATH", 0, 1, realpath_command, _(
4565 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4566 "Resolves all @code{target} attributes of the specified element path and "
4567 "returns the result with a data response. @xref{Target Attribute}, for details."
4568 "\n"
4569 "When the @option{--inquire} option is passed then all remaining non-option "
4570 "arguments are retrieved via a server @emph{INQUIRE}."
4573 new_command("STORE", 0, 1, store_command, _(
4574 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4575 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4576 "\n"
4577 "Creates a new element path or modifies the @var{content} of an existing "
4578 "element. If only a single element is specified then a new root element is "
4579 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4580 "set to the final @key{TAB} delimited element. If no @var{content} is "
4581 "specified after the final @key{TAB}, then the content of an existing "
4582 "element will be removed; or empty when creating a new element."
4583 "\n"
4584 "The only restriction of an element name is that it not contain whitespace "
4585 "or begin with the literal element character @code{!} unless specifying a "
4586 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4587 "the @key{TAB} delimited elements. It is recommended that the content of an "
4588 "element be base64 encoded when it contains control or @key{TAB} characters "
4589 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
4592 new_command("RENAME", 0, 1, rename_command, _(
4593 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4594 "Renames the specified @var{element} to the new @var{value}. If an element of "
4595 "the same name as the @var{value} already exists it will be overwritten."
4596 "\n"
4597 "When the @option{--inquire} option is passed then all remaining non-option "
4598 "arguments are retrieved via a server @emph{INQUIRE}."
4601 new_command("COPY", 0, 1, copy_command, _(
4602 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4603 "Copies the entire element tree starting from the child node of the source "
4604 "element, to the destination element path. If the destination element path "
4605 "does not exist then it will be created; otherwise it is overwritten."
4606 "\n"
4607 "Note that attributes from the source element are merged into the "
4608 "destination element when the destination element path exists. When an "
4609 "attribute of the same name exists in both the source and destination "
4610 "elements then the destination attribute will be updated to the source "
4611 "attribute value."
4612 "\n"
4613 "When the @option{--inquire} option is passed then all remaining non-option "
4614 "arguments are retrieved via a server @emph{INQUIRE}."
4617 new_command("MOVE", 0, 1, move_command, _(
4618 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4619 "Moves the source element path to the destination element path. If the "
4620 "destination is not specified then it will be moved to the root node of the "
4621 "document. If the destination is specified and exists then it will be "
4622 "overwritten; otherwise non-existing elements of the destination element "
4623 "path will be created."
4624 "\n"
4625 "When the @option{--inquire} option is passed then all remaining non-option "
4626 "arguments are retrieved via a server @emph{INQUIRE}."
4629 new_command("DELETE", 0, 1, delete_command, _(
4630 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4631 "Removes the specified element path and all of its children. This may break "
4632 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4633 "refers to this element or any of its children."
4634 "\n"
4635 "When the @option{--inquire} option is passed then all remaining non-option "
4636 "arguments are retrieved via a server @emph{INQUIRE}."
4639 new_command("GET", 0, 1, get_command, _(
4640 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4641 "Retrieves the content of the specified element. The content is returned "
4642 "with a data response."
4643 "\n"
4644 "When the @option{--inquire} option is passed then all remaining non-option "
4645 "arguments are retrieved via a server @emph{INQUIRE}."
4648 new_command("ATTR", 0, 1, attr_command, _(
4649 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4650 "@table @asis\n"
4651 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4652 "\n"
4653 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4654 "element. When no @var{value} is specified any existing value will be removed."
4655 "\n"
4656 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4657 "\n"
4658 " Removes an @var{attribute} from an element."
4659 "\n"
4660 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4661 "\n"
4662 " Retrieves a newline separated list of attributes names and values "
4663 "from the specified element. Each attribute name and value is space delimited."
4664 "\n"
4665 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4666 "\n"
4667 " Retrieves the value of an @var{attribute} from an element."
4668 "@end table\n"
4669 "\n"
4670 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4671 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4672 "commands instead."
4673 "\n"
4674 "The @code{_mtime} attribute is updated each time an element is modified by "
4675 "either storing content, editing attributes or by deleting a child element. "
4676 "The @code{_ctime} attribute is created for each new element in an element "
4677 "path."
4678 "\n"
4679 "When the @option{--inquire} option is passed then all remaining non-option "
4680 "arguments are retrieved via a server @emph{INQUIRE}."
4681 "\n"
4682 "@xref{Target Attribute}, for details about this special attribute."
4685 new_command("XPATH", 0, 1, xpath_command, _(
4686 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4687 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4688 "specified it is assumed the expression is a request to return a result. "
4689 "Otherwise, the result is set to the @var{value} argument and the document is "
4690 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4691 "is assumed to be empty and the document is updated. For example:"
4692 "@sp 1\n"
4693 "@example\n"
4694 "XPATH //element[@@_name='password']@key{TAB}\n"
4695 "@end example\n"
4696 "@sp 1"
4697 "would clear the content of all @code{password} elements in the data file "
4698 "while leaving off the trailing @key{TAB} would return all @code{password} "
4699 "elements in @abbr{XML} format."
4700 "\n"
4701 "When the @option{--inquire} option is passed then all remaining non-option "
4702 "arguments are retrieved via a server @emph{INQUIRE}."
4703 "\n"
4704 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4705 "expression syntax."
4708 new_command("XPATHATTR", 0, 1, xpathattr_command, _(
4709 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4710 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4711 "attributes and does not return a result. For the @var{SET} operation the "
4712 "@var{value} is optional but the field is required. If not specified then "
4713 "the attribute value will be empty. For example:"
4714 "@sp 1"
4715 "@example\n"
4716 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4717 "@end example\n"
4718 "@sp 1"
4719 "would create an @code{password} attribute for each @code{password} element "
4720 "found in the document. The attribute value will be empty but still exist."
4721 "\n"
4722 "When the @option{--inquire} option is passed then all remaining non-option "
4723 "arguments are retrieved via a server @emph{INQUIRE}."
4724 "\n"
4725 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4726 "expression syntax."
4729 new_command("IMPORT", 0, 1, import_command, _(
4730 "IMPORT [--root [!]element[<TAB>[!]child[..]]] <content>\n"
4731 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4732 "\n"
4733 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4734 "argument is raw @abbr{XML} data. The content is created as a child of "
4735 "the element path specified with the @option{--root} option or at the "
4736 "document root when not specified. Existing elements of the same name will "
4737 "be overwritten."
4738 "\n"
4739 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4740 "for details."
4743 new_command("DUMP", 0, 1, dump_command, _(
4744 "DUMP\n"
4745 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4746 "dumping a specific node."
4749 new_command("LOCK", 0, 0, lock_command, _(
4750 "LOCK\n"
4751 "Locks the mutex associated with the opened file. This prevents other clients "
4752 "from sending commands to the same opened file until the client "
4753 "that sent this command either disconnects or sends the @code{UNLOCK} "
4754 "command. @xref{UNLOCK}."
4757 new_command("UNLOCK", 1, 0, unlock_command, _(
4758 "UNLOCK\n"
4759 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4760 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4761 "@pxref{ISCACHED})."
4764 new_command("GETCONFIG", 1, 1, getconfig_command, _(
4765 "GETCONFIG [filename] <parameter>\n"
4766 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4767 "data response. If no file has been opened then the value for @var{filename} "
4768 "or the default from the @samp{global} section will be returned. If a file "
4769 "has been opened and no @var{filename} is specified, a value previously "
4770 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4773 new_command("OPTION", 1, 1, option_command, _(
4774 "OPTION <NAME>=<VALUE>\n"
4775 "Sets a client option @var{name} to @var{value}. The value for an option is "
4776 "kept for the duration of the connection."
4777 "\n"
4778 "@table @asis\n"
4779 "@item DISABLE-PINENTRY\n"
4780 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
4781 "server inquire is sent to the client to obtain the passphrase. This option "
4782 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4783 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
4784 "\n"
4785 "@item PINENTRY-TIMEOUT\n"
4786 "Sets the number of seconds before a pinentry prompt will return an error "
4787 "while waiting for user input."
4788 "\n"
4789 "@item TTYNAME\n"
4790 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4791 "\n"
4792 "@item TTYTYPE\n"
4793 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4794 "\n"
4795 "@item DISPLAY\n"
4796 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4797 "\n"
4798 "@item PINENTRY-DESC\n"
4799 "Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4800 "\n"
4801 "@item PINENTRY-TITLE\n"
4802 "Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
4803 "\n"
4804 "@item PINENTRY-PROMPT\n"
4805 "Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
4806 "\n"
4807 "@item LC-CTYPE\n"
4808 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4809 "\n"
4810 "@item LC-MESSAGES\n"
4811 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4812 "\n"
4813 "@item NAME\n"
4814 "Associates the thread ID of the connection with the specified textual "
4815 "representation. Useful for debugging log messages."
4816 "\n"
4817 "@item LOCK-TIMEOUT\n"
4818 "When not @code{0}, the duration in tenths of a second to wait for the file "
4819 "mutex which has been locked by another thread to be released before returning "
4820 "an error. When @code{-1}, then an error will be returned immediately."
4821 "\n"
4822 "@item LOG-LEVEL\n"
4823 "An integer specifiying the logging level."
4824 "@end table\n"
4827 new_command("LS", 1, 1, ls_command, _(
4828 "LS\n"
4829 "Lists the available data files stored in the data directory "
4830 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4833 new_command("RESET", 1, 1, NULL, _(
4834 "RESET\n"
4835 "Closes the currently opened file but keeps any previously set client options."
4838 new_command("NOP", 1, 1, NULL, _(
4839 "NOP\n"
4840 "Does nothing. Always returns successfully."
4843 /* !END-HELP-TEXT! */
4844 new_command ("CANCEL", 1, 1, NULL, NULL);
4845 new_command ("END", 1, 1, NULL, NULL);
4846 new_command ("BYE", 1, 1, NULL, NULL);
4848 int i;
4849 for (i = 0; command_table[i]; i++);
4850 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4851 sort_commands);
4854 gpg_error_t
4855 register_commands (assuan_context_t ctx)
4857 int i = 0, rc;
4859 for (; command_table[i]; i++)
4861 if (!command_table[i]->handler)
4862 continue;
4864 rc = assuan_register_command (ctx, command_table[i]->name,
4865 command_table[i]->handler,
4866 command_table[i]->help);
4867 if (rc)
4868 return rc;
4871 rc = assuan_register_bye_notify (ctx, bye_notify);
4872 if (rc)
4873 return rc;
4875 rc = assuan_register_reset_notify (ctx, reset_notify);
4876 if (rc)
4877 return rc;
4879 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4880 if (rc)
4881 return rc;
4883 return assuan_register_post_cmd_notify (ctx, command_finalize);