Fixup commit 5f89607.
[pwmd.git] / src / commands.c
blob1abcbe2c67c00fb4414c73a999d03787999a8d1b
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012
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 struct command_table_s
79 const char *name;
80 gpg_error_t (*handler) (assuan_context_t, char *line);
81 const char *help;
82 int ignore_startup;
83 int unlock; // unlock the file mutex after validating the checksum
86 static struct command_table_s **command_table;
88 static gpg_error_t do_lock (struct client_s *client, int add);
89 static gpg_error_t validate_checksum (struct client_s *,
90 struct cache_data_s *);
91 static gpg_error_t update_checksum (struct client_s *client);
93 static gpg_error_t
94 unlock_file_mutex (struct client_s *client, int remove)
96 gpg_error_t rc = 0;
98 // OPEN: keep the lock for the same file being reopened.
99 if (client->flags & FLAG_KEEP_LOCK && client->flags & FLAG_HAS_LOCK)
100 return 0;
102 if (!(client->flags & FLAG_HAS_LOCK))
103 return GPG_ERR_NOT_LOCKED;
105 rc = cache_unlock_mutex (client->md5file, remove);
106 if (rc)
107 rc = GPG_ERR_INV_STATE;
108 else
109 client->flags &= ~(FLAG_HAS_LOCK | FLAG_LOCK_CMD);
111 return rc;
114 static gpg_error_t
115 lock_file_mutex (struct client_s *client, int add)
117 gpg_error_t rc = 0;
118 int timeout = config_get_integer (client->filename, "cache_timeout");
120 if (client->flags & FLAG_HAS_LOCK)
121 return 0;
123 rc = cache_lock_mutex (client->ctx, client->md5file,
124 client->lock_timeout, add, timeout);
125 if (!rc)
126 client->flags |= FLAG_HAS_LOCK;
128 return rc;
131 static gpg_error_t
132 file_modified (struct client_s *client, struct command_table_s *cmd)
134 gpg_error_t rc = 0;
136 if (!(client->flags & FLAG_OPEN))
137 return GPG_ERR_INV_STATE;
139 rc = lock_file_mutex (client, 0);
140 if (!rc || rc == GPG_ERR_NO_DATA)
142 rc = validate_checksum (client, NULL);
143 if (gpg_err_code (rc) == GPG_ERR_ENOENT)
144 rc = 0;
147 if ((cmd->unlock && !(client->flags & FLAG_HAS_LOCK)) || rc)
148 unlock_file_mutex (client, 0);
150 return rc;
153 static gpg_error_t
154 parse_xml (assuan_context_t ctx, int new)
156 struct client_s *client = assuan_get_pointer (ctx);
157 int cached = client->doc != NULL;
159 if (new)
161 client->doc = new_document ();
162 if (client->doc)
164 xmlDocDumpFormatMemory (client->doc,
165 (xmlChar **) & client->crypto->plaintext,
166 (int *) &client->crypto->plaintext_len, 0);
167 if (!client->crypto->plaintext)
169 xmlFreeDoc (client->doc);
170 client->doc = NULL;
174 else if (!cached)
175 client->doc = parse_doc ((char *) client->crypto->plaintext,
176 client->crypto->plaintext_len);
178 return !client->doc ? GPG_ERR_ENOMEM : 0;
181 static void
182 free_client (struct client_s *client)
184 if (client->doc)
185 xmlFreeDoc (client->doc);
187 xfree (client->crc);
188 xfree (client->filename);
189 xfree (client->last_error);
191 if (client->crypto)
193 cleanup_crypto_stage2 (client->crypto);
194 if (client->crypto->pkey_sexp)
195 gcry_sexp_release (client->crypto->pkey_sexp);
197 client->crypto->pkey_sexp = NULL;
198 memset (client->crypto->sign_grip, 0,
199 sizeof (client->crypto->sign_grip));
200 memset (client->crypto->grip, 0, sizeof(client->crypto->grip));
203 if (client->xml_error)
204 xmlResetError (client->xml_error);
207 void
208 cleanup_client (struct client_s *client)
210 assuan_context_t ctx = client->ctx;
211 struct client_thread_s *thd = client->thd;
212 struct crypto_s *crypto = client->crypto;
213 long lock_timeout = client->lock_timeout;
214 struct pinentry_option_s pin_opts;
215 #ifdef WITH_AGENT
216 struct pinentry_option_s agent_pin_opts;
218 if (crypto->agent)
219 memcpy (&agent_pin_opts, &crypto->agent->pinentry_opts,
220 sizeof(struct pinentry_option_s));
221 #endif
223 memcpy (&pin_opts, &client->pinentry_opts, sizeof(struct pinentry_option_s));
224 unlock_file_mutex (client, client->flags & FLAG_NEW);
225 free_client (client);
226 memset (client, 0, sizeof (struct client_s));
227 client->crypto = crypto;
228 client->ctx = ctx;
229 client->thd = thd;
230 client->lock_timeout = lock_timeout;
231 memcpy (&client->pinentry_opts, &pin_opts, sizeof(struct pinentry_option_s));
232 #ifdef WITH_AGENT
233 memcpy (&crypto->agent->pinentry_opts, &agent_pin_opts,
234 sizeof(struct pinentry_option_s));
235 #endif
238 static gpg_error_t
239 open_finalize (assuan_context_t ctx, char *key, size_t keylen)
241 struct client_s *client = assuan_get_pointer (ctx);
242 gpg_error_t rc = 0;
243 struct cache_data_s *cdata = cache_get_data (client->md5file);
244 int cached = 0, keyarg = key ? 1 : 0;
245 size_t keysize;
246 unsigned char *salted_key = NULL;
247 int algo;
249 client->crypto->filename = str_dup (client->filename);
250 if (cdata || client->flags & FLAG_NEW)
252 if (!(client->flags & FLAG_NEW))
254 rc = decrypt_xml (client->crypto, cdata->doc, cdata->doclen);
255 if (rc)
256 return rc;
259 cached = cdata != NULL;
260 goto done;
263 if (!key && !IS_PKCS (client->crypto))
265 if (client->flags & FLAG_NO_PINENTRY)
267 rc = inquire_passphrase (ctx, "PASSPHRASE", (unsigned char **)&key,
268 &keylen);
269 if (rc)
270 return rc;
272 else
274 client->pinentry_opts.timeout = config_get_integer (client->filename,
275 "pinentry_timeout");
276 rc = getpin_common (client->ctx, client->filename, PINENTRY_OPEN,
277 &key, &keylen);
278 if (rc)
279 return rc;
283 if (!IS_PKCS (client->crypto))
285 algo = cipher_to_gcrypt (client->crypto->hdr.flags);
286 rc = hash_key (algo, client->crypto->hdr.salt,
287 sizeof(client->crypto->hdr.salt), key, keylen,
288 (void **)&salted_key, &keysize);
289 if (!keyarg)
290 gcry_free (key);
292 if (!rc)
293 rc = decrypt_data (client->ctx, client->crypto, salted_key, keysize);
295 if (rc)
296 return rc;
298 cdata = xcalloc (1, sizeof (struct cache_data_s));
299 if (!cdata)
300 return GPG_ERR_ENOMEM;
302 cdata->key = salted_key;
303 cdata->keylen = keysize;
305 else
306 rc = decrypt_data (client->ctx, client->crypto, NULL, 0);
308 done:
309 if (!rc)
311 rc = parse_xml (ctx, client->flags & FLAG_NEW);
312 if (!rc)
314 int timeout = config_get_integer (client->filename,
315 "cache_timeout");
317 if (!cached)
319 if (!cdata)
320 cdata = xcalloc (1, sizeof (struct cache_data_s));
322 rc = encrypt_xml (NULL, cache_key, cache_keysize,
323 GCRY_CIPHER_AES, client->crypto->plaintext,
324 client->crypto->plaintext_len, &cdata->doc,
325 &cdata->doclen, &cache_iv, &cache_blocksize,
327 if (!rc && !(client->flags & FLAG_NEW) && IS_PKCS (client->crypto))
329 rc = gcry_sexp_build ((gcry_sexp_t *) & cdata->pubkey, NULL,
330 "%S", client->crypto->pkey_sexp);
334 if (cdata->sigkey)
335 gcry_sexp_release (cdata->sigkey);
337 cdata->sigkey = NULL;
339 if (!rc && IS_PKCS (client->crypto))
340 rc = gcry_sexp_build ((gcry_sexp_t *) &cdata->sigkey, NULL,
341 "%S", client->crypto->sigpkey_sexp);
343 if (!rc
344 && !cache_add_file (client->md5file, (client->flags & FLAG_NEW)
345 ? NULL : client->crypto->grip, cdata, timeout))
347 if (!cached)
349 free_cache_data_once (cdata);
350 xmlFreeDoc (client->doc);
351 client->doc = NULL;
353 rc = GPG_ERR_ENOMEM;
356 if (!rc)
357 client->flags |= FLAG_OPEN;
361 if (!rc)
362 update_checksum (client);
364 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
365 rc = do_lock (client, 0);
367 return rc;
370 static void
371 req_cleanup (void *arg)
373 if (!arg)
374 return;
376 strv_free ((char **) arg);
379 static gpg_error_t
380 parse_open_opt_lock (void *data, void *value)
382 struct client_s *client = data;
384 client->opts |= OPT_LOCK_ON_OPEN;
385 return 0;
388 static gpg_error_t
389 parse_opt_pinentry (void *data, void *value)
391 struct client_s *client = data;
393 client->flags |= FLAG_NO_PINENTRY;
394 return 0;
397 static gpg_error_t
398 parse_opt_inquire (void *data, void *value)
400 struct client_s *client = data;
402 (void) value;
403 client->opts |= OPT_INQUIRE;
404 return 0;
407 static gpg_error_t
408 update_checksum (struct client_s *client)
410 unsigned char *crc;
411 size_t len;
412 struct cache_data_s *cdata;
413 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
415 if (rc)
416 return rc;
418 xfree (client->crc);
419 client->crc = crc;
420 cdata = cache_get_data (client->md5file);
421 if (cdata)
423 xfree (cdata->crc);
424 cdata->crc = xmalloc (len);
425 memcpy (cdata->crc, crc, len);
428 return 0;
431 static gpg_error_t
432 validate_checksum (struct client_s *client, struct cache_data_s *cdata)
434 unsigned char *crc;
435 size_t len;
436 gpg_error_t rc;
437 int n = 0;
439 if (cdata && !cdata->crc)
440 return GPG_ERR_CHECKSUM;
442 rc = get_checksum (client->filename, &crc, &len);
443 if (rc)
444 return rc;
446 if (cdata)
447 n = memcmp (cdata->crc, crc, len);
448 else if (client->crc)
449 n = memcmp (client->crc, crc, len);
451 xfree (crc);
452 return n ? GPG_ERR_CHECKSUM : 0;
455 static gpg_error_t
456 do_open (assuan_context_t ctx, const char *filename, const char *password)
458 struct client_s *client = assuan_get_pointer (ctx);
459 struct cache_data_s *cdata;
460 gpg_error_t rc = 0;
461 int done = 0;
463 if (!valid_filename (filename))
464 return GPG_ERR_INV_VALUE;
466 gcry_md_hash_buffer (GCRY_MD_MD5, client->md5file, filename,
467 strlen (filename));
468 client->filename = str_dup (filename);
469 if (!client->filename)
470 return GPG_ERR_ENOMEM;
472 // Cached document?
473 cdata = cache_get_data (client->md5file);
474 if (cdata && cdata->doc)
476 int defer;
478 rc = validate_checksum (client, cdata);
479 /* This will check that the key is cached in the agent which needs to
480 * be determined for files that share a keygrip. */
481 if (!rc)
483 rc = cache_iscached (client->filename, &defer);
484 if (!rc && defer)
485 rc = GPG_ERR_KEY_EXPIRED;
488 #ifdef WITH_GNUTLS
489 if (!rc && client->thd->remote
490 && config_get_boolean (NULL, "tcp_require_key"))
491 rc = GPG_ERR_KEY_EXPIRED;
492 #endif
494 if (!rc && !password)
496 rc = read_data_header (client->filename, &client->crypto->hdr,
497 NULL, NULL);
498 if (!rc)
500 if (IS_PKCS (client->crypto))
502 gcry_sexp_build (&client->crypto->pkey_sexp, NULL, "%S",
503 cdata->pubkey);
504 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
505 client->crypto->grip);
506 gcry_sexp_build (&client->crypto->sigpkey_sexp, NULL, "%S",
507 cdata->sigkey);
508 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
509 client->crypto->sign_grip);
512 if (!rc)
514 rc = open_finalize (ctx, NULL, 0);
515 done = 1;
520 /* There was an error accessing the file so clear the cache entry. The
521 * real error will be returned from read_data_file() since the file
522 * may have only disappeared. */
523 if (!done)
525 log_write ("%s: %s", filename, pwmd_strerror (rc));
526 rc = cache_clear (client->md5file);
527 send_status_all (STATUS_CACHE, NULL);
531 if (done || rc)
532 return rc;
534 rc = read_data_file (client->filename, client->crypto);
535 if (rc)
537 if (gpg_err_source (rc) != GPG_ERR_SOURCE_DEFAULT ||
538 gpg_err_code (rc) != GPG_ERR_ENOENT)
540 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
541 return rc;
544 client->flags |= FLAG_NEW;
545 memset (client->crypto->grip, 0, sizeof (client->crypto->grip));
546 memset (client->crypto->sign_grip, 0,
547 sizeof (client->crypto->sign_grip));
548 rc = open_finalize (ctx, NULL, 0);
549 return rc;
552 if (password && IS_PKCS (client->crypto))
554 #ifdef WITH_AGENT
555 rc = set_agent_passphrase (client->crypto, password, strlen (password));
556 if (rc)
557 return rc;
558 #endif
561 rc = open_finalize (ctx, (char *)password, password ? strlen (password) : 0);
562 return rc;
565 static gpg_error_t
566 open_command (assuan_context_t ctx, char *line)
568 gpg_error_t rc;
569 struct client_s *client = assuan_get_pointer (ctx);
570 char **req, *password = NULL, *filename;
571 unsigned char md5file[16];
572 int same_file = 0;
573 struct argv_s *args[] = {
574 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
575 &(struct argv_s) {"no-pinentry", OPTION_TYPE_NOARG,
576 parse_opt_pinentry},
577 NULL
580 client->flags &= ~(FLAG_NO_PINENTRY);
581 rc = parse_options (&line, args, client);
582 if (rc)
583 return send_error (ctx, rc);
585 req = str_split (line, " ", 2);
586 if (!req)
587 return send_error (ctx, GPG_ERR_SYNTAX);
589 #ifdef WITH_GNUTLS
590 if (client->thd->remote)
592 rc = tls_validate_access (client, req[0]);
593 if (rc)
595 if (rc == GPG_ERR_INV_USER_ID)
596 log_write (_("client validation failed for file '%s'"), req[0]);
597 strv_free (req);
598 return send_error (ctx, rc);
601 /* Remote pinentries are not supported. */
602 client->flags |= FLAG_NO_PINENTRY;
604 #endif
606 pthread_cleanup_push (req_cleanup, req);
607 filename = req[0];
608 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, filename, strlen (filename));
609 /* This client may have locked a different file with ISCACHED --lock than
610 * the current filename. This will remove that lock. */
611 same_file = !memcmp (md5file, client->md5file, 16) ? 1 : 0;
612 if (client->flags & FLAG_OPEN ||
613 (client->flags & FLAG_HAS_LOCK && !same_file))
615 typeof (client->opts) opts = client->opts;
616 typeof (client->flags) flags = client->flags;
618 if (same_file)
619 client->flags |= FLAG_KEEP_LOCK;
621 cleanup_client (client);
622 client->opts = opts;
623 client->flags |= flags;
624 client->flags &= ~(FLAG_LOCK_CMD | FLAG_NEW);
625 if (!same_file)
626 client->flags &= ~(FLAG_HAS_LOCK);
629 /* Need to lock the mutex here because file_modified() cannot without
630 * knowing the filename. */
631 memcpy (client->md5file, md5file, 16);
632 rc = lock_file_mutex (client, 1);
633 if (!rc)
635 password = req[1] && *req[1] ? req[1] : NULL;
636 #ifdef WITH_AGENT
637 if (IS_PKCS (client->crypto))
638 rc = set_pinentry_mode (client->crypto->agent,
639 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
640 : "ask");
641 #endif
642 if (!rc)
644 rc = do_open (ctx, filename, password);
645 if (rc)
646 cleanup_client (client);
648 cleanup_crypto_stage1 (client->crypto);
652 pthread_cleanup_pop (1);
654 if (!rc && client->flags & FLAG_NEW)
655 rc = send_status (ctx, STATUS_NEWFILE, NULL);
657 #ifdef WITH_AGENT
658 (void) kill_scd (client->crypto->agent);
659 #endif
660 return send_error (ctx, rc);
663 static gpg_error_t
664 parse_save_opt_no_passphrase (void *data, void *value)
666 struct client_s *client = data;
668 (void) value;
669 client->opts |= OPT_NO_PASSPHRASE;
670 return 0;
673 static gpg_error_t
674 parse_save_opt_no_agent (void *data, void *value)
676 struct client_s *client = data;
678 client->opts |= OPT_NO_AGENT;
679 return 0;
682 static gpg_error_t
683 parse_save_opt_cipher (void *data, void *value)
685 struct client_s *client = data;
686 int algo = cipher_string_to_gcrypt ((char *) value);
687 file_header_t *hdr = &client->crypto->save.hdr;
689 if (algo == -1)
690 return GPG_ERR_INV_VALUE;
692 hdr->flags = set_cipher_flag (hdr->flags, algo);
693 return 0;
696 static gpg_error_t
697 parse_save_opt_keygrip (void *data, void *value)
699 struct client_s *client = data;
701 if (!IS_PKCS (client->crypto))
702 return GPG_ERR_INV_ARG;
704 #ifdef WITH_AGENT
705 if (client->crypto->save.pkey)
706 gcry_sexp_release (client->crypto->save.pkey);
708 client->crypto->save.pkey = NULL;
709 return get_pubkey (client->crypto, value, &client->crypto->save.pkey);
710 #else
711 return GPG_ERR_INV_ARG;
712 #endif
715 static gpg_error_t
716 parse_save_opt_sign_keygrip (void *data, void *value)
718 struct client_s *client = data;
720 if (!IS_PKCS (client->crypto))
721 return GPG_ERR_INV_ARG;
723 #ifdef WITH_AGENT
724 if (client->crypto->save.sigpkey)
725 gcry_sexp_release (client->crypto->save.sigpkey);
727 client->crypto->save.sigpkey = NULL;
728 return get_pubkey (client->crypto, value, &client->crypto->save.sigpkey);
729 #else
730 return GPG_ERR_INV_ARG;
731 #endif
734 static gpg_error_t
735 parse_opt_s2k_count (void *data, void *value)
737 struct client_s *client = data;
738 char *v = value;
740 if (!v || !*v)
741 return GPG_ERR_INV_VALUE;
743 client->crypto->save.s2k_count = strtoul (v, NULL, 10);
744 return 0;
747 static gpg_error_t
748 parse_save_opt_iterations (void *data, void *value)
750 struct client_s *client = data;
751 char *v = value, *p;
752 uint64_t n;
754 if (!v || !*v)
755 return GPG_ERR_INV_VALUE;
757 errno = 0;
758 n = strtoull (v, &p, 10);
759 if (n == UINT64_MAX && errno)
760 return gpg_error_from_syserror ();
761 else if (p && *p)
762 return GPG_ERR_INV_VALUE;
764 client->crypto->save.hdr.iterations = n;
765 return 0;
768 static gpg_error_t
769 save_finalize (assuan_context_t ctx)
771 struct client_s *client = assuan_get_pointer (ctx);
772 gpg_error_t rc = 0;
773 xmlChar *xmlbuf = NULL;
774 int xmlbuflen;
775 void *key = NULL;
776 size_t keylen = 0;
778 xmlDocDumpFormatMemory (client->doc, &xmlbuf, &xmlbuflen, 0);
779 if (!xmlbuf)
780 return GPG_ERR_ENOMEM;
782 pthread_cleanup_push (xmlFree, xmlbuf);
784 if (!use_agent || ((client->flags & FLAG_NEW)
785 && (client->opts & OPT_NO_AGENT))
786 || !(client->crypto->hdr.flags & PWMD_FLAG_PKCS))
788 rc = export_common (ctx, client->flags & FLAG_NO_PINENTRY,
789 client->crypto, xmlbuf, xmlbuflen, client->filename,
790 NULL, &key, &keylen, 1, 1);
792 #ifdef WITH_AGENT
793 else
795 gcry_sexp_t pubkey = client->crypto->save.pkey;
796 gcry_sexp_t sigkey = client->crypto->save.sigpkey;
798 if (!pubkey)
799 pubkey = client->crypto->pkey_sexp;
801 if (!sigkey)
803 sigkey = client->crypto->sigpkey_sexp;
804 if (!sigkey)
806 gcry_sexp_build (&client->crypto->save.sigpkey, 0, "%S", pubkey);
807 sigkey = client->crypto->save.sigpkey;
811 rc = encrypt_data_file (client->ctx, client->crypto, pubkey, sigkey,
812 client->filename, xmlbuf, xmlbuflen);
813 if (pubkey == client->crypto->save.pkey)
815 if (!rc)
817 gcry_sexp_release (client->crypto->pkey_sexp);
818 client->crypto->pkey_sexp = client->crypto->save.pkey;
819 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
820 client->crypto->grip);
822 else
823 gcry_sexp_release (pubkey);
825 client->crypto->save.pkey = NULL;
828 if (!rc)
829 gcry_pk_get_keygrip (sigkey, client->crypto->sign_grip);
831 if (sigkey == client->crypto->save.sigpkey)
833 if (!rc)
835 if (client->crypto->sigpkey_sexp)
836 gcry_sexp_release (client->crypto->sigpkey_sexp);
838 client->crypto->sigpkey_sexp = client->crypto->save.sigpkey;
839 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
840 client->crypto->sign_grip);
842 else
843 gcry_sexp_release (sigkey);
845 client->crypto->save.sigpkey = NULL;
848 #endif
850 if (!rc)
852 int cached;
854 rc = save_common (client->filename, client->crypto, xmlbuf, xmlbuflen,
855 key, keylen, &cached, client->opts & OPT_NO_AGENT);
856 if (rc)
857 gcry_free (key);
859 if (!rc && (!cached || (client->flags & FLAG_NEW)))
860 send_status_all (STATUS_CACHE, NULL);
862 if (!rc)
864 update_checksum (client);
865 client->flags &= ~(FLAG_NEW);
867 else
868 rc = GPG_ERR_ENOMEM;
871 pthread_cleanup_pop (1); // xmlFree
872 return rc;
875 static gpg_error_t
876 parse_opt_reset (void *data, void *value)
878 struct client_s *client = data;
880 (void) value;
881 client->opts |= OPT_RESET;
882 return 0;
885 static gpg_error_t
886 save_command (assuan_context_t ctx, char *line)
888 struct client_s *client = assuan_get_pointer (ctx);
889 gpg_error_t rc;
890 struct stat st;
891 struct argv_s *args[] = {
892 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG,
893 parse_save_opt_no_passphrase},
894 &(struct argv_s) {"cipher", OPTION_TYPE_ARG, parse_save_opt_cipher},
895 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
896 parse_opt_inquire},
897 &(struct argv_s) {"keygrip", OPTION_TYPE_ARG, parse_save_opt_keygrip},
898 &(struct argv_s) {"sign-keygrip", OPTION_TYPE_ARG,
899 parse_save_opt_sign_keygrip},
900 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
901 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
902 &(struct argv_s) {"cipher-iterations", OPTION_TYPE_ARG,
903 parse_save_opt_iterations},
904 &(struct argv_s) {"no-agent", OPTION_TYPE_NOARG,
905 parse_save_opt_no_agent},
906 NULL
909 cleanup_save (&client->crypto->save);
910 memcpy (&client->crypto->save.hdr, &client->crypto->hdr,
911 sizeof (file_header_t));
912 client->crypto->save.s2k_count =
913 config_get_ulong (client->filename, "s2k_count");
915 if (client->flags & FLAG_NEW)
916 client->crypto->save.hdr.iterations =
917 config_get_ulonglong (client->filename, "cipher_iterations");
919 rc = parse_options (&line, args, client);
920 if (rc)
921 return send_error (ctx, rc);
923 if (!(client->flags & FLAG_NEW))
924 client->opts &= ~OPT_NO_AGENT;
925 else if (client->opts & OPT_NO_AGENT)
927 client->crypto->save.hdr.flags &= ~PWMD_FLAG_PKCS;
928 client->crypto->hdr.flags &= ~PWMD_FLAG_PKCS;
931 if ((client->opts & OPT_NO_PASSPHRASE) && !(client->flags & FLAG_NEW)
932 && !(client->opts & OPT_INQUIRE))
933 return send_error (ctx, GPG_ERR_WRONG_KEY_USAGE);
935 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
936 return send_error (ctx, gpg_error_from_syserror ());
938 if (errno != ENOENT && !S_ISREG (st.st_mode))
940 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
941 return send_error (ctx, GPG_ERR_ENOANO);
944 int defer;
945 rc = cache_iscached (client->filename, &defer);
946 if (!rc && defer)
948 log_write ("%s: %s", client->filename,
949 pwmd_strerror (GPG_ERR_KEY_EXPIRED));
950 client->opts |= OPT_RESET;
953 if (client->opts & OPT_RESET)
955 rc = cache_clear (client->md5file);
956 if (rc)
957 return send_error (ctx, rc);
959 send_status_all (STATUS_CACHE, NULL);
962 rc = update_element_mtime (xmlDocGetRootElement (client->doc));
963 if (rc)
964 return send_error (ctx, rc);
966 #ifdef WITH_AGENT
967 if (!rc && use_agent && !client->crypto->save.pkey &&
968 (client->flags & FLAG_NEW || client->opts & OPT_INQUIRE)
969 && !(client->opts & OPT_NO_AGENT))
971 rc = send_status (client->ctx, STATUS_GENKEY, NULL);
972 if (!rc)
974 struct inquire_data_s idata = { 0 };
975 char *params = client->opts & OPT_INQUIRE ? NULL
976 : default_key_params (client->crypto);
978 pthread_cleanup_push (xfree, params);
979 idata.crypto = client->crypto;
981 if (params)
983 idata.line = params;
984 idata.len = strlen (params);
986 else
987 idata.preset = 1;
989 client->crypto->agent->inquire_data = &idata;
990 client->crypto->agent->inquire_cb = NULL;
991 rc = generate_key (client->crypto, params,
992 (client->opts & OPT_NO_PASSPHRASE), 1);
993 pthread_cleanup_pop (1);
996 #endif
998 if (!rc)
999 rc = save_finalize (ctx);
1001 cleanup_crypto_stage1 (client->crypto);
1002 #ifdef WITH_AGENT
1003 (void) kill_scd (client->crypto->agent);
1004 #endif
1005 return send_error (ctx, rc);
1008 static gpg_error_t
1009 do_delete (assuan_context_t ctx, char *line)
1011 struct client_s *client = assuan_get_pointer (ctx);
1012 gpg_error_t rc;
1013 char **req;
1014 xmlNodePtr n;
1016 if (strchr (line, '\t'))
1017 req = str_split (line, "\t", 0);
1018 else
1019 req = str_split (line, " ", 0);
1021 if (!req || !*req)
1022 return GPG_ERR_SYNTAX;
1024 n = find_root_element (client->doc, &req, &rc, NULL, 0, 0);
1025 if (!n)
1027 strv_free (req);
1028 return rc;
1032 * No sub-node defined. Remove the entire node (root element).
1034 if (!req[1])
1036 if (n)
1038 rc = unlink_node (n);
1039 xmlFreeNode (n);
1042 strv_free (req);
1043 return rc;
1047 find_elements (client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1048 0, 0, NULL, 0);
1049 strv_free (req);
1050 if (!n)
1051 return rc;
1053 if (n)
1055 rc = unlink_node (n);
1056 xmlFreeNode (n);
1059 return rc;
1062 static gpg_error_t
1063 delete_command (assuan_context_t ctx, char *line)
1065 struct client_s *client = assuan_get_pointer (ctx);
1066 gpg_error_t rc;
1067 struct argv_s *args[] = {
1068 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1069 NULL
1072 rc = parse_options (&line, args, client);
1073 if (rc)
1074 return send_error (ctx, rc);
1076 if (client->opts & OPT_INQUIRE)
1078 unsigned char *result;
1079 size_t len;
1081 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1082 if (rc)
1083 return send_error (ctx, rc);
1085 line = (char *) result;
1088 rc = do_delete (ctx, line);
1090 if (client->opts & OPT_INQUIRE)
1091 xfree (line);
1093 return send_error (ctx, rc);
1096 static gpg_error_t
1097 store_command (assuan_context_t ctx, char *line)
1099 struct client_s *client = assuan_get_pointer (ctx);
1100 gpg_error_t rc;
1101 size_t len;
1102 unsigned char *result;
1103 char **req;
1104 xmlNodePtr n, parent;
1105 int has_content;
1106 char *content = NULL;
1108 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1109 if (rc)
1110 return send_error (ctx, rc);
1112 req = str_split ((char *) result, "\t", 0);
1113 xfree (result);
1115 if (!req || !*req)
1116 return send_error (ctx, GPG_ERR_SYNTAX);
1118 len = strv_length (req);
1119 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1120 if (*(req + 1) && !valid_element_path (req, has_content))
1122 strv_free (req);
1123 return send_error (ctx, GPG_ERR_INV_VALUE);
1126 if (has_content || !*req[len - 1])
1128 has_content = 1;
1129 content = req[len - 1];
1130 req[len - 1] = NULL;
1133 again:
1134 n = find_root_element (client->doc, &req, &rc, NULL, 0, 0);
1135 if (rc && rc == GPG_ERR_ELEMENT_NOT_FOUND)
1137 rc = new_root_element (client->doc, *req);
1138 if (rc)
1140 strv_free (req);
1141 return send_error (ctx, GPG_ERR_SYNTAX);
1144 goto again;
1147 if (!n)
1149 strv_free (req);
1150 return send_error (ctx, rc);
1153 parent = n;
1155 if (req[1] && *req[1])
1157 if (!n->children)
1158 parent = create_elements_cb (n, req + 1, &rc, NULL);
1159 else
1160 parent = find_elements (client->doc, n->children, req + 1, &rc,
1161 NULL, NULL, create_elements_cb, 0, 0, NULL,
1165 if (!rc && len > 1)
1167 n = find_text_node (parent->children);
1168 if (n)
1169 xmlNodeSetContent (n, (xmlChar *) content);
1170 else
1171 xmlNodeAddContent (parent, (xmlChar *) content);
1173 update_element_mtime (parent);
1176 xfree (content);
1177 strv_free (req);
1178 return send_error (ctx, rc);
1181 static gpg_error_t
1182 xfer_data (assuan_context_t ctx, const char *line, int total)
1184 int to_send;
1185 int sent = 0;
1186 gpg_error_t rc;
1187 int progress = config_get_integer ("global", "xfer_progress");
1188 int flush = 0;
1190 progress =
1191 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1192 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1193 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1195 if (rc)
1196 return rc;
1198 again:
1201 if (sent + to_send > total)
1202 to_send = total - sent;
1204 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1205 flush ? 0 : to_send);
1206 if (!rc)
1208 sent += flush ? 0 : to_send;
1210 if ((progress && !(sent % progress) && sent != total) ||
1211 (sent == total && flush))
1212 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1214 if (!flush && !rc && sent == total)
1216 flush = 1;
1217 goto again;
1221 while (!rc && sent < total);
1223 return rc;
1226 static gpg_error_t
1227 do_get (assuan_context_t ctx, char *line)
1229 struct client_s *client = assuan_get_pointer (ctx);
1230 gpg_error_t rc;
1231 char **req;
1232 xmlNodePtr n;
1234 req = str_split (line, "\t", 0);
1236 if (!req || !*req)
1238 strv_free (req);
1239 return GPG_ERR_SYNTAX;
1242 n = find_root_element (client->doc, &req, &rc, NULL, 0, 0);
1243 if (!n)
1245 strv_free (req);
1246 return rc;
1249 if (req[1])
1251 find_elements (client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1252 0, 0, NULL, 0);
1254 strv_free (req);
1255 if (rc)
1256 return rc;
1258 if (!n || !n->children)
1259 return GPG_ERR_NO_DATA;
1261 n = find_text_node (n->children);
1262 if (!n || !n->content || !*n->content)
1263 return GPG_ERR_NO_DATA;
1265 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1266 return rc;
1269 static gpg_error_t
1270 get_command (assuan_context_t ctx, char *line)
1272 struct client_s *client = assuan_get_pointer (ctx);
1273 gpg_error_t rc;
1274 struct argv_s *args[] = {
1275 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1276 NULL
1279 rc = parse_options (&line, args, client);
1280 if (rc)
1281 return send_error (ctx, rc);
1283 if (client->opts & OPT_INQUIRE)
1285 unsigned char *result;
1286 size_t len;
1288 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1289 if (rc)
1290 return send_error (ctx, rc);
1292 line = (char *) result;
1295 rc = do_get (ctx, line);
1297 if (client->opts & OPT_INQUIRE)
1298 xfree (line);
1300 return send_error (ctx, rc);
1303 static void list_command_cleanup1 (void *arg);
1304 static gpg_error_t
1305 realpath_command (assuan_context_t ctx, char *line)
1307 struct string_s *string = NULL;
1308 gpg_error_t rc;
1309 struct client_s *client = assuan_get_pointer (ctx);
1310 struct argv_s *args[] = {
1311 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1312 NULL
1315 rc = parse_options (&line, args, client);
1316 if (rc)
1317 return send_error (ctx, rc);
1319 if (client->opts & OPT_INQUIRE)
1321 unsigned char *result;
1322 size_t len;
1324 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1325 if (rc)
1326 return send_error (ctx, rc);
1328 line = (char *) result;
1331 rc = build_realpath (client->doc, line, &string);
1332 if (!rc)
1334 pthread_cleanup_push (list_command_cleanup1, string);
1335 rc = xfer_data (ctx, string->str, string->len);
1336 pthread_cleanup_pop (1);
1339 if (client->opts & OPT_INQUIRE)
1340 xfree (line);
1342 return send_error (ctx, rc);
1345 static void
1346 list_command_cleanup1 (void *arg)
1348 if (arg)
1349 string_free ((struct string_s *) arg, 1);
1352 static void
1353 list_command_cleanup2 (void *arg)
1355 struct element_list_s *elements = arg;
1357 if (elements)
1359 if (elements->list)
1361 int total = slist_length (elements->list);
1362 int i;
1364 for (i = 0; i < total; i++)
1366 char *tmp = slist_nth_data (elements->list, i);
1367 xfree (tmp);
1370 slist_free (elements->list);
1373 if (elements->prefix)
1374 xfree (elements->prefix);
1376 if (elements->req)
1377 strv_free (elements->req);
1379 xfree (elements);
1383 static gpg_error_t
1384 parse_list_opt_norecurse (void *data, void *value)
1386 struct client_s *client = data;
1388 client->opts &= ~(OPT_LIST_RECURSE);
1389 return 0;
1392 static gpg_error_t
1393 parse_list_opt_verbose (void *data, void *value)
1395 struct client_s *client = data;
1397 client->opts |= OPT_LIST_VERBOSE;
1398 return 0;
1401 static gpg_error_t
1402 parse_list_opt_target (void *data, void *value)
1404 struct client_s *client = data;
1406 client->opts |= OPT_LIST_WITH_TARGET;
1407 return 0;
1410 static gpg_error_t
1411 parse_list_opt_all (void *data, void *value)
1413 struct client_s *client = data;
1415 client->opts |= OPT_LIST_ALL;
1416 return 0;
1419 static gpg_error_t
1420 list_path_once (struct client_s *client, char *line,
1421 struct element_list_s *elements, struct string_s *result)
1423 gpg_error_t rc;
1425 elements->req = str_split (line, " ", 0);
1426 if (!elements->req)
1427 strv_printf (&elements->req, "%s", line);
1429 rc = create_path_list (client->doc, elements, *elements->req);
1430 if (rc == GPG_ERR_ELOOP && elements->verbose)
1431 rc = 0;
1433 if (!rc)
1435 if (elements)
1437 int total = slist_length (elements->list);
1438 int i;
1440 if (!total)
1441 rc = GPG_ERR_NO_DATA;
1443 if (!rc)
1445 if (!rc)
1447 for (i = 0; i < total; i++)
1449 char *tmp = slist_nth_data (elements->list, i);
1451 string_append_printf (result, "%s%s", tmp,
1452 i + 1 == total ? "" : "\n");
1457 else
1458 rc = GPG_ERR_NO_DATA;
1461 return rc;
1464 static int
1465 has_list_flag (char *path, char *flags)
1467 char *p = path;
1469 while (*p && *++p != ' ');
1471 if (!*p)
1472 return 0;
1474 for (; *p; p++)
1476 char *f;
1478 for (f = flags; *f && *f != ' '; f++)
1480 if (*p == *f)
1481 return 1;
1485 return 0;
1488 static gpg_error_t
1489 do_list (assuan_context_t ctx, char *line)
1491 struct client_s *client = assuan_get_pointer (ctx);
1492 gpg_error_t rc;
1493 struct element_list_s *elements = NULL;
1495 elements = xcalloc (1, sizeof (struct element_list_s));
1496 if (!elements)
1497 return GPG_ERR_ENOMEM;
1499 elements->recurse = client->opts & OPT_LIST_RECURSE;
1500 elements->verbose =
1501 (client->opts & OPT_LIST_VERBOSE) | (client->opts & OPT_LIST_WITH_TARGET);
1502 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1504 if (!line || !*line)
1506 struct string_s *str = NULL;
1508 pthread_cleanup_push (list_command_cleanup2, elements);
1509 rc = list_root_elements (client->doc, &str, elements->verbose,
1510 elements->with_target);
1511 pthread_cleanup_pop (1);
1512 pthread_cleanup_push (list_command_cleanup1, str);
1514 if (!rc)
1516 if (client->opts & OPT_LIST_ALL)
1518 char **roots = str_split (str->str, "\n", 0);
1519 char **p;
1521 pthread_cleanup_push (req_cleanup, roots);
1522 string_truncate (str, 0);
1524 for (p = roots; *p; p++)
1526 if (strchr (*p, ' '))
1528 if (has_list_flag (*p, "EO"))
1530 string_append_printf (str, "%s%s", *p,
1531 *(p + 1) ? "\n" : "");
1532 continue;
1536 elements = xcalloc (1, sizeof (struct element_list_s));
1537 if (!elements)
1539 rc = GPG_ERR_ENOMEM;
1540 break;
1543 elements->recurse = client->opts & OPT_LIST_RECURSE;
1544 elements->verbose =
1545 (client->opts & OPT_LIST_VERBOSE) | (client->opts &
1546 OPT_LIST_WITH_TARGET);
1547 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1548 pthread_cleanup_push (list_command_cleanup2, elements);
1549 rc = list_path_once (client, *p, elements, str);
1550 pthread_cleanup_pop (1);
1551 if (rc)
1552 break;
1554 if (*(p + 1))
1555 string_append (str, "\n");
1558 pthread_cleanup_pop (1);
1561 if (!rc)
1562 rc = xfer_data (ctx, str->str, str->len);
1565 pthread_cleanup_pop (1);
1566 return rc;
1569 pthread_cleanup_push (list_command_cleanup2, elements);
1570 struct string_s *str = string_new (NULL);
1571 pthread_cleanup_push (list_command_cleanup1, str);
1572 rc = list_path_once (client, line, elements, str);
1573 if (!rc)
1574 rc = xfer_data (ctx, str->str, str->len);
1576 pthread_cleanup_pop (1);
1577 pthread_cleanup_pop (1);
1578 return rc;
1581 static gpg_error_t
1582 list_command (assuan_context_t ctx, char *line)
1584 struct client_s *client = assuan_get_pointer (ctx);
1585 gpg_error_t rc;
1586 struct argv_s *args[] = {
1587 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG,
1588 parse_list_opt_norecurse},
1589 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_list_opt_verbose},
1590 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1591 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG,
1592 parse_list_opt_target},
1593 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_all},
1594 NULL
1597 if (disable_list_and_dump == 1)
1598 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1600 client->opts |= OPT_LIST_RECURSE;
1601 rc = parse_options (&line, args, client);
1602 if (rc)
1603 return send_error (ctx, rc);
1605 if (client->opts & OPT_LIST_ALL)
1606 client->opts |= OPT_LIST_RECURSE | OPT_LIST_VERBOSE;
1608 if (client->opts & OPT_INQUIRE)
1610 unsigned char *result;
1611 size_t len;
1613 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1614 if (rc)
1615 return send_error (ctx, rc);
1617 line = (char *) result;
1620 rc = do_list (ctx, line);
1622 if (client->opts & OPT_INQUIRE)
1623 xfree (line);
1625 return send_error (ctx, rc);
1629 * req[0] - element path
1631 static gpg_error_t
1632 attribute_list (assuan_context_t ctx, char **req)
1634 struct client_s *client = assuan_get_pointer (ctx);
1635 char **attrlist = NULL;
1636 int i = 0;
1637 char **path = NULL;
1638 xmlAttrPtr a;
1639 xmlNodePtr n, an;
1640 char *line;
1641 gpg_error_t rc;
1643 if (!req || !req[0])
1644 return GPG_ERR_SYNTAX;
1646 if ((path = str_split (req[0], "\t", 0)) == NULL)
1649 * The first argument may be only a root element.
1651 if ((path = str_split (req[0], " ", 0)) == NULL)
1652 return GPG_ERR_SYNTAX;
1655 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
1657 if (!n)
1659 strv_free (path);
1660 return rc;
1663 if (path[1])
1665 n = find_elements (client->doc, n->children, path + 1, &rc,
1666 NULL, NULL, NULL, 0, 0, NULL, 0);
1668 if (!n)
1670 strv_free (path);
1671 return rc;
1675 strv_free (path);
1677 for (a = n->properties; a; a = a->next)
1679 char **pa;
1681 if ((pa = xrealloc (attrlist, (i + 2) * sizeof (char *))) == NULL)
1683 if (attrlist)
1684 strv_free (attrlist);
1686 log_write ("%s(%i): %s", __FILE__, __LINE__,
1687 pwmd_strerror (GPG_ERR_ENOMEM));
1688 return GPG_ERR_ENOMEM;
1691 attrlist = pa;
1692 an = a->children;
1693 attrlist[i] = str_asprintf ("%s %s", (char *) a->name,
1695 && an->content ? (char *) an->content : "");
1697 if (!attrlist[i])
1699 strv_free (attrlist);
1700 log_write ("%s(%i): %s", __FILE__, __LINE__,
1701 pwmd_strerror (GPG_ERR_ENOMEM));
1702 return GPG_ERR_ENOMEM;
1705 attrlist[++i] = NULL;
1708 if (!attrlist)
1709 return GPG_ERR_NO_DATA;
1711 line = strv_join ("\n", attrlist);
1713 if (!line)
1715 log_write ("%s(%i): %s", __FILE__, __LINE__,
1716 pwmd_strerror (GPG_ERR_ENOMEM));
1717 strv_free (attrlist);
1718 return GPG_ERR_ENOMEM;
1721 pthread_cleanup_push (xfree, line);
1722 pthread_cleanup_push (req_cleanup, attrlist);
1723 rc = xfer_data (ctx, line, strlen (line));
1724 pthread_cleanup_pop (1);
1725 pthread_cleanup_pop (1);
1726 return rc;
1730 * req[0] - attribute
1731 * req[1] - element path
1733 static gpg_error_t
1734 attribute_delete (struct client_s *client, char **req)
1736 xmlNodePtr n;
1737 char **path = NULL;
1738 gpg_error_t rc;
1740 if (!req || !req[0] || !req[1])
1741 return GPG_ERR_SYNTAX;
1743 if (!strcmp (req[0], "_name"))
1744 return GPG_ERR_INV_ATTR;
1746 if ((path = str_split (req[1], "\t", 0)) == NULL)
1749 * The first argument may be only a root element.
1751 if ((path = str_split (req[1], " ", 0)) == NULL)
1752 return GPG_ERR_SYNTAX;
1755 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
1756 if (!n)
1757 goto fail;
1759 if (path[1])
1761 n = find_elements (client->doc, n->children, path + 1, &rc,
1762 NULL, NULL, NULL, 0, 0, NULL, 0);
1763 if (!n)
1764 goto fail;
1767 rc = delete_attribute (n, (xmlChar *) req[0]);
1769 fail:
1770 strv_free (path);
1771 return rc;
1774 static xmlNodePtr
1775 create_element_path (struct client_s *client,
1776 char ***elements, gpg_error_t * rc, xmlNodePtr parent)
1778 char **req = *elements;
1779 char **req_orig = strv_dup (req);
1780 xmlNodePtr n = NULL;
1782 *rc = 0;
1784 if (!req_orig)
1786 *rc = GPG_ERR_ENOMEM;
1787 log_write ("%s(%i): %s", __FILE__, __LINE__,
1788 pwmd_strerror (GPG_ERR_ENOMEM));
1789 goto fail;
1792 again:
1793 n = find_root_element (client->doc, &req, rc, NULL, 0, 0);
1794 if (!n)
1796 if (*rc != GPG_ERR_ELEMENT_NOT_FOUND)
1797 goto fail;
1799 *rc = new_root_element (client->doc, req[0]);
1800 if (*rc)
1801 goto fail;
1803 goto again;
1805 else if (n == parent)
1807 *rc = GPG_ERR_CONFLICT;
1808 goto fail;
1811 if (req[1])
1813 if (!n->children)
1814 n = create_target_elements_cb (n, req + 1, rc, NULL);
1815 else
1816 n = find_elements (client->doc, n->children, req + 1, rc, NULL, NULL,
1817 create_target_elements_cb, 0, 0, parent, 0);
1819 if (!n)
1820 goto fail;
1823 * Reset the position of the element tree now that the elements
1824 * have been created.
1826 strv_free (req);
1827 req = req_orig;
1828 req_orig = NULL;
1829 n = find_root_element (client->doc, &req, rc, NULL, 0, 0);
1830 if (!n)
1831 goto fail;
1833 n = find_elements (client->doc, n->children, req + 1, rc,
1834 NULL, NULL, NULL, 0, 0, NULL, 0);
1835 if (!n)
1836 goto fail;
1839 fail:
1840 if (req_orig)
1841 strv_free (req_orig);
1843 *elements = req;
1844 return n;
1848 * Creates a "target" attribute. When other commands encounter an element with
1849 * this attribute, the element path is modified to the target value. If the
1850 * source element path doesn't exist when using 'ATTR SET target', it is
1851 * created, but the destination element path must exist.
1853 * req[0] - source element path
1854 * req[1] - destination element path
1856 static gpg_error_t
1857 target_attribute (struct client_s *client, char **req)
1859 char **src, **dst, *line = NULL, **odst = NULL;
1860 gpg_error_t rc;
1861 xmlNodePtr n;
1863 if (!req || !req[0] || !req[1])
1864 return GPG_ERR_SYNTAX;
1866 if ((src = str_split (req[0], "\t", 0)) == NULL)
1869 * The first argument may be only a root element.
1871 if ((src = str_split (req[0], " ", 0)) == NULL)
1872 return GPG_ERR_SYNTAX;
1875 if (!valid_element_path (src, 0))
1876 return GPG_ERR_INV_VALUE;
1878 if ((dst = str_split (req[1], "\t", 0)) == NULL)
1881 * The first argument may be only a root element.
1883 if ((dst = str_split (req[1], " ", 0)) == NULL)
1885 rc = GPG_ERR_SYNTAX;
1886 goto fail;
1890 odst = strv_dup (dst);
1891 if (!odst)
1893 rc = GPG_ERR_ENOMEM;
1894 goto fail;
1897 n = find_root_element (client->doc, &dst, &rc, NULL, 0, 0);
1899 * Make sure the destination element path exists.
1901 if (!n)
1902 goto fail;
1904 if (dst[1])
1906 n = find_elements (client->doc, n->children, dst + 1, &rc,
1907 NULL, NULL, NULL, 0, 0, NULL, 0);
1908 if (!n)
1909 goto fail;
1912 rc = validate_target_attribute (client->doc, req[0], n);
1913 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
1914 goto fail;
1916 n = create_element_path (client, &src, &rc, NULL);
1917 if (rc)
1918 goto fail;
1920 line = strv_join ("\t", odst);
1921 if (!line)
1923 rc = GPG_ERR_ENOMEM;
1924 goto fail;
1927 rc = add_attribute (n, "target", line);
1929 fail:
1930 xfree (line);
1931 strv_free (src);
1932 strv_free (dst);
1933 strv_free (odst);
1934 return rc;
1938 * req[0] - attribute
1939 * req[1] - element path
1941 static gpg_error_t
1942 attribute_get (assuan_context_t ctx, char **req)
1944 struct client_s *client = assuan_get_pointer (ctx);
1945 xmlNodePtr n;
1946 xmlChar *a;
1947 char **path = NULL;
1948 gpg_error_t rc;
1950 if (!req || !req[0] || !req[1])
1951 return GPG_ERR_SYNTAX;
1953 if (strchr (req[1], '\t'))
1955 if ((path = str_split (req[1], "\t", 0)) == NULL)
1956 return GPG_ERR_SYNTAX;
1958 else
1960 if ((path = str_split (req[1], " ", 0)) == NULL)
1961 return GPG_ERR_SYNTAX;
1964 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
1966 if (!n)
1967 goto fail;
1969 if (path[1])
1971 n = find_elements (client->doc, n->children, path + 1, &rc,
1972 NULL, NULL, NULL, 0, 0, NULL, 0);
1974 if (!n)
1975 goto fail;
1978 strv_free (path);
1980 if ((a = xmlGetProp (n, (xmlChar *) req[0])) == NULL)
1981 return GPG_ERR_NOT_FOUND;
1983 pthread_cleanup_push (xmlFree, a);
1985 if (*a)
1986 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
1987 else
1988 rc = GPG_ERR_NO_DATA;
1990 pthread_cleanup_pop (1);
1991 return rc;
1993 fail:
1994 strv_free (path);
1995 return rc;
1999 * req[0] - attribute
2000 * req[1] - element path
2001 * req[2] - value
2003 static gpg_error_t
2004 attribute_set (struct client_s *client, char **req)
2006 char **path = NULL;
2007 gpg_error_t rc;
2008 xmlNodePtr n;
2010 if (!req || !req[0] || !req[1])
2011 return GPG_ERR_SYNTAX;
2014 * Reserved attribute names.
2016 if (!strcmp (req[0], "_name"))
2017 return GPG_ERR_INV_ATTR;
2018 else if (!strcmp (req[0], "target"))
2019 return target_attribute (client, req + 1);
2021 if ((path = str_split (req[1], "\t", 0)) == NULL)
2024 * The first argument may be only a root element.
2026 if ((path = str_split (req[1], " ", 0)) == NULL)
2027 return GPG_ERR_SYNTAX;
2030 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
2032 if (!n)
2033 goto fail;
2035 if (path[1])
2037 n = find_elements (client->doc, n->children, path + 1, &rc,
2038 NULL, NULL, NULL, 0, 0, NULL, 0);
2040 if (!n)
2041 goto fail;
2044 rc = add_attribute (n, req[0], req[2]);
2046 fail:
2047 strv_free (path);
2048 return rc;
2052 * req[0] - command
2053 * req[1] - attribute name or element path if command is LIST
2054 * req[2] - element path
2055 * req[2] - element path or value
2058 static gpg_error_t
2059 do_attr (assuan_context_t ctx, char *line)
2061 struct client_s *client = assuan_get_pointer (ctx);
2062 gpg_error_t rc = 0;
2063 char **req;
2065 req = str_split (line, " ", 4);
2066 if (!req || !req[0] || !req[1])
2068 strv_free (req);
2069 return GPG_ERR_SYNTAX;
2072 pthread_cleanup_push (req_cleanup, req);
2074 if (strcasecmp (req[0], "SET") == 0)
2075 rc = attribute_set (client, req + 1);
2076 else if (strcasecmp (req[0], "GET") == 0)
2077 rc = attribute_get (ctx, req + 1);
2078 else if (strcasecmp (req[0], "DELETE") == 0)
2079 rc = attribute_delete (client, req + 1);
2080 else if (strcasecmp (req[0], "LIST") == 0)
2081 rc = attribute_list (ctx, req + 1);
2082 else
2083 rc = GPG_ERR_SYNTAX;
2085 pthread_cleanup_pop (1);
2086 return rc;
2089 static gpg_error_t
2090 attr_command (assuan_context_t ctx, char *line)
2092 struct client_s *client = assuan_get_pointer (ctx);
2093 gpg_error_t rc;
2094 struct argv_s *args[] = {
2095 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2096 NULL
2099 rc = parse_options (&line, args, client);
2100 if (rc)
2101 return send_error (ctx, rc);
2103 if (client->opts & OPT_INQUIRE)
2105 unsigned char *result;
2106 size_t len;
2108 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2109 if (rc)
2110 return send_error (ctx, rc);
2112 line = (char *) result;
2115 rc = do_attr (ctx, line);
2117 if (client->opts & OPT_INQUIRE)
2118 xfree (line);
2120 return send_error (ctx, rc);
2123 static gpg_error_t
2124 parse_iscached_opt_lock (void *data, void *value)
2126 struct client_s *client = data;
2128 (void) value;
2129 client->opts |= OPT_LOCK;
2130 return 0;
2133 static gpg_error_t
2134 iscached_command (assuan_context_t ctx, char *line)
2136 struct client_s *client = assuan_get_pointer (ctx);
2137 gpg_error_t rc;
2138 unsigned char md5file[16];
2139 struct argv_s *args[] = {
2140 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2141 NULL
2144 if (!line || !*line)
2145 return send_error (ctx, GPG_ERR_SYNTAX);
2147 rc = parse_options (&line, args, client);
2148 if (rc)
2149 return send_error (ctx, rc);
2150 else if (!valid_filename (line))
2151 return send_error (ctx, GPG_ERR_INV_VALUE);
2153 rc = cache_iscached (line, NULL);
2154 if (client->opts & OPT_LOCK
2155 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2157 gpg_error_t trc = rc;
2158 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2159 if (memcmp (md5file, client->md5file, 16))
2160 cleanup_client (client);
2162 memcpy (client->md5file, md5file, 16);
2163 rc = do_lock (client, 1);
2164 if (!rc)
2165 rc = trc;
2168 return send_error (ctx, rc);
2171 static gpg_error_t
2172 clearcache_command (assuan_context_t ctx, char *line)
2174 gpg_error_t rc = 0, all_rc = 0;
2175 unsigned char md5file[16];
2176 int i;
2177 int t;
2178 int all = 0;
2179 struct client_thread_s *once = NULL;
2181 cache_lock ();
2182 MUTEX_LOCK (&cn_mutex);
2183 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
2185 if (!line || !*line)
2186 all = 1;
2188 t = slist_length (cn_thread_list);
2190 for (i = 0; i < t; i++)
2192 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2194 if (!thd->cl)
2195 continue;
2197 /* Lock each connected clients' file mutex to prevent any other client
2198 * from accessing the cache entry (the file mutex is locked upon
2199 * command startup). The cache for the entry is not cleared if the
2200 * file mutex is locked by another client to prevent this function
2201 * from blocking.
2203 if (all)
2205 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
2206 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2208 if (pthread_equal (pthread_self (), thd->tid))
2209 rc = 0;
2210 else
2212 if (!thd->cl->filename ||
2213 cache_iscached (thd->cl->filename,
2214 NULL) == GPG_ERR_NO_DATA)
2216 rc = 0;
2217 continue;
2220 cache_defer_clear (thd->cl->md5file);
2223 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2225 rc = 0;
2226 continue;
2229 if (!rc)
2231 rc = cache_clear (thd->cl->md5file);
2232 if (rc)
2233 all_rc = rc;
2235 cache_unlock_mutex (thd->cl->md5file, 0);
2237 else
2238 all_rc = rc;
2240 rc = 0;
2242 /* A single data filename was specified. Lock only this data file
2243 * mutex and free the cache entry. */
2244 else
2246 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2248 if (!memcmp (thd->cl->md5file, md5file, sizeof (md5file)))
2250 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0,
2251 -1);
2252 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2254 if (pthread_equal (pthread_self (), thd->tid))
2255 rc = 0;
2258 if (!rc)
2260 once = thd;
2261 rc = cache_clear (thd->cl->md5file);
2262 cache_unlock_mutex (thd->cl->md5file, 0);
2264 else
2266 cache_defer_clear (thd->cl->md5file);
2269 break;
2274 /* Only connected clients' cache entries have been cleared. Now clear any
2275 * remaining cache entries without clients but only if there wasn't an
2276 * error from above since this would defeat the locking check of the
2277 * remaining entries. */
2278 if (!all_rc && all)
2280 cache_clear (NULL);
2283 /* No clients are using the specified file. */
2284 else if (!all_rc && !rc && !once)
2286 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2287 rc = cache_clear (md5file);
2290 /* Release the connection mutex. */
2291 pthread_cleanup_pop (1);
2292 cache_unlock ();
2294 if (!rc)
2295 send_status_all (STATUS_CACHE, NULL);
2297 /* One or more files were locked while clearing all cache entries. */
2298 if (all_rc)
2299 rc = all_rc;
2301 return send_error (ctx, rc);
2304 static gpg_error_t
2305 cachetimeout_command (assuan_context_t ctx, char *line)
2307 unsigned char md5file[16];
2308 int timeout;
2309 char **req = str_split (line, " ", 0);
2310 char *p;
2311 gpg_error_t rc = 0;
2313 if (!req || !*req || !req[1])
2315 strv_free (req);
2316 return send_error (ctx, GPG_ERR_SYNTAX);
2319 errno = 0;
2320 timeout = (int) strtol (req[1], &p, 10);
2321 if (errno != 0 || *p || timeout < -1)
2323 strv_free (req);
2324 return send_error (ctx, GPG_ERR_SYNTAX);
2327 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, req[0], strlen (req[0]));
2328 rc = cache_set_timeout (md5file, timeout);
2329 if (!rc)
2331 MUTEX_LOCK (&rcfile_mutex);
2332 config_set_int_param (&global_config, req[0], "cache_timeout", req[1]);
2333 MUTEX_UNLOCK (&rcfile_mutex);
2336 strv_free (req);
2337 return send_error (ctx, rc);
2340 static gpg_error_t
2341 dump_command (assuan_context_t ctx, char *line)
2343 xmlChar *xml;
2344 int len;
2345 struct client_s *client = assuan_get_pointer (ctx);
2346 gpg_error_t rc;
2348 if (disable_list_and_dump == 1)
2349 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2351 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2353 if (!xml)
2355 log_write ("%s(%i): %s", __FILE__, __LINE__,
2356 pwmd_strerror (GPG_ERR_ENOMEM));
2357 return send_error (ctx, GPG_ERR_ENOMEM);
2360 pthread_cleanup_push (xmlFree, xml);
2361 rc = xfer_data (ctx, (char *) xml, len);
2362 pthread_cleanup_pop (1);
2363 return send_error (ctx, rc);
2366 static gpg_error_t
2367 getconfig_command (assuan_context_t ctx, char *line)
2369 struct client_s *client = assuan_get_pointer (ctx);
2370 gpg_error_t rc = 0;
2371 char filename[255] = { 0 }, param[747] =
2374 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2376 if (!line || !*line)
2377 return send_error (ctx, GPG_ERR_SYNTAX);
2379 if (strchr (line, ' '))
2381 sscanf (line, " %254[^ ] %746c", filename, param);
2382 paramp = param;
2383 fp = filename;
2386 if (fp && !valid_filename (fp))
2387 return send_error (ctx, GPG_ERR_INV_VALUE);
2389 paramp = str_down (paramp);
2390 if (!strcmp (paramp, "cipher") && fp)
2392 struct crypto_s *crypto;
2394 rc = init_client_crypto (&crypto);
2395 if (!rc)
2397 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2398 if (!rc)
2400 const char *t =
2401 gcry_cipher_algo_name (cipher_to_gcrypt (crypto->hdr.flags));
2402 if (t)
2404 tmp = str_dup (t);
2405 if (tmp)
2406 str_down (tmp);
2411 cleanup_crypto (&crypto);
2412 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2413 return send_error (ctx, rc);
2415 if (!rc && tmp)
2416 goto done;
2418 else if (!strcmp (paramp, "cipher_iterations") && fp)
2420 struct crypto_s *crypto;
2422 rc = init_client_crypto (&crypto);
2423 if (!rc)
2425 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2426 if (!rc)
2428 tmp = str_asprintf ("%llu",
2429 (unsigned long long) crypto->hdr.
2430 iterations);
2431 if (!tmp)
2432 rc = GPG_ERR_ENOMEM;
2436 cleanup_crypto (&crypto);
2437 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2438 return send_error (ctx, rc);
2440 if (!rc && tmp)
2441 goto done;
2443 else if (!strcmp (paramp, "passphrase"))
2444 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2446 p = config_get_value (fp ? fp : "global", paramp);
2447 if (!p)
2448 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2450 tmp = expand_homedir (p);
2451 xfree (p);
2452 if (!tmp)
2454 log_write ("%s(%i): %s", __FILE__, __LINE__,
2455 pwmd_strerror (GPG_ERR_ENOMEM));
2456 return send_error (ctx, GPG_ERR_ENOMEM);
2459 done:
2460 p = tmp;
2461 pthread_cleanup_push (xfree, p);
2462 rc = xfer_data (ctx, p, strlen (p));
2463 pthread_cleanup_pop (1);
2464 return send_error (ctx, rc);
2467 struct xpath_s
2469 xmlXPathContextPtr xp;
2470 xmlXPathObjectPtr result;
2471 xmlBufferPtr buf;
2472 char **req;
2475 static void
2476 xpath_command_cleanup (void *arg)
2478 struct xpath_s *xpath = arg;
2480 if (!xpath)
2481 return;
2483 req_cleanup (xpath->req);
2485 if (xpath->buf)
2486 xmlBufferFree (xpath->buf);
2488 if (xpath->result)
2489 xmlXPathFreeObject (xpath->result);
2491 if (xpath->xp)
2492 xmlXPathFreeContext (xpath->xp);
2495 static gpg_error_t
2496 do_xpath (assuan_context_t ctx, char *line)
2498 gpg_error_t rc;
2499 struct client_s *client = assuan_get_pointer (ctx);
2500 struct xpath_s _x = { 0 };
2501 struct xpath_s *xpath = &_x;
2503 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2505 if (strv_printf (&xpath->req, "%s", line) == 0)
2506 return GPG_ERR_ENOMEM;
2509 xpath->xp = xmlXPathNewContext (client->doc);
2510 if (!xpath->xp)
2512 rc = GPG_ERR_BAD_DATA;
2513 goto fail;
2516 xpath->result =
2517 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2518 if (!xpath->result)
2520 rc = GPG_ERR_BAD_DATA;
2521 goto fail;
2524 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2526 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2527 goto fail;
2530 rc = recurse_xpath_nodeset (client->doc, xpath->result->nodesetval,
2531 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2532 NULL);
2533 if (rc)
2534 goto fail;
2535 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2537 rc = GPG_ERR_NO_DATA;
2538 goto fail;
2540 else if (xpath->req[1])
2542 rc = 0;
2543 goto fail;
2546 pthread_cleanup_push (xpath_command_cleanup, &xpath);
2547 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2548 xmlBufferLength (xpath->buf));
2549 pthread_cleanup_pop (0);
2550 fail:
2551 xpath_command_cleanup (xpath);
2552 return rc;
2555 static gpg_error_t
2556 xpath_command (assuan_context_t ctx, char *line)
2558 struct client_s *client = assuan_get_pointer (ctx);
2559 gpg_error_t rc;
2560 struct argv_s *args[] = {
2561 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2562 NULL
2565 if (disable_list_and_dump == 1)
2566 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2568 rc = parse_options (&line, args, client);
2569 if (rc)
2570 return send_error (ctx, rc);
2572 if (client->opts & OPT_INQUIRE)
2574 unsigned char *result;
2575 size_t len;
2577 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2578 if (rc)
2579 return send_error (ctx, rc);
2581 line = (char *) result;
2584 if (!line || !*line)
2585 rc = GPG_ERR_SYNTAX;
2587 if (!rc)
2588 rc = do_xpath (ctx, line);
2590 if (client->opts & OPT_INQUIRE)
2591 xfree (line);
2593 return send_error (ctx, rc);
2596 static gpg_error_t
2597 do_xpathattr (assuan_context_t ctx, char *line)
2599 struct client_s *client = assuan_get_pointer (ctx);
2600 gpg_error_t rc;
2601 char **req = NULL;
2602 int cmd = 0; //SET
2603 struct xpath_s _x = { 0 };
2604 struct xpath_s *xpath = &_x;
2606 if ((req = str_split (line, " ", 3)) == NULL)
2607 return GPG_ERR_ENOMEM;
2609 if (!req[0])
2611 rc = GPG_ERR_SYNTAX;
2612 goto fail;
2615 if (!strcasecmp (req[0], "SET"))
2616 cmd = 0;
2617 else if (!strcasecmp (req[0], "DELETE"))
2618 cmd = 1;
2619 else
2621 rc = GPG_ERR_SYNTAX;
2622 goto fail;
2625 if (!req[1] || !req[2])
2627 rc = GPG_ERR_SYNTAX;
2628 goto fail;
2631 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2633 rc = GPG_ERR_ENOMEM;
2634 goto fail;
2637 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2639 rc = GPG_ERR_SYNTAX;
2640 goto fail;
2643 xpath->xp = xmlXPathNewContext (client->doc);
2644 if (!xpath->xp)
2646 rc = GPG_ERR_BAD_DATA;
2647 goto fail;
2650 xpath->result =
2651 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2652 if (!xpath->result)
2654 rc = GPG_ERR_BAD_DATA;
2655 goto fail;
2658 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2660 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2661 goto fail;
2664 rc = recurse_xpath_nodeset (client->doc, xpath->result->nodesetval,
2665 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2666 (xmlChar *) req[1]);
2668 fail:
2669 xpath_command_cleanup (xpath);
2670 strv_free (req);
2671 return rc;
2674 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2675 static gpg_error_t
2676 xpathattr_command (assuan_context_t ctx, char *line)
2678 struct client_s *client = assuan_get_pointer (ctx);
2679 gpg_error_t rc;
2680 struct argv_s *args[] = {
2681 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2682 NULL
2685 if (disable_list_and_dump == 1)
2686 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2688 rc = parse_options (&line, args, client);
2689 if (rc)
2690 return send_error (ctx, rc);
2692 if (client->opts & OPT_INQUIRE)
2694 unsigned char *result;
2695 size_t len;
2697 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2698 if (rc)
2699 return send_error (ctx, rc);
2701 line = (char *) result;
2704 if (!line || !*line)
2705 rc = GPG_ERR_SYNTAX;
2707 if (!rc)
2708 rc = do_xpathattr (ctx, line);
2710 if (client->opts & OPT_INQUIRE)
2711 xfree (line);
2713 return send_error (ctx, rc);
2716 static gpg_error_t
2717 do_import (struct client_s *client, unsigned char *line)
2719 char **req, **path = NULL, **path_orig = NULL, *content;
2720 xmlDocPtr doc = NULL;
2721 xmlNodePtr n, root, copy;
2722 gpg_error_t rc;
2724 req = str_split ((char *) line, "\t", 2);
2725 xfree (line);
2726 if (!req || !*req)
2727 return GPG_ERR_SYNTAX;
2729 content = req[0];
2730 path = str_split (req[1], "\t", 0);
2731 if (!content || !*content)
2733 rc = GPG_ERR_SYNTAX;
2734 goto fail;
2737 if (path && !valid_element_path (path, 0))
2739 rc = GPG_ERR_INV_VALUE;
2740 goto fail;
2743 doc = xmlReadDoc ((xmlChar *) content, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2744 if (!doc)
2746 rc = GPG_ERR_BAD_DATA;
2747 goto fail;
2750 root = xmlDocGetRootElement (doc);
2751 rc = validate_import (root);
2752 if (rc)
2753 goto fail;
2755 if (path)
2757 path_orig = strv_dup (path);
2758 if (!path_orig)
2760 log_write ("%s(%i): %s", __FILE__, __LINE__,
2761 pwmd_strerror (GPG_ERR_ENOMEM));
2762 rc = GPG_ERR_ENOMEM;
2763 goto fail;
2766 xmlChar *a = xmlGetProp (root, (xmlChar *) "_name");
2767 if (!a)
2769 strv_free (path_orig);
2770 rc = GPG_ERR_ENOMEM;
2771 goto fail;
2774 if (strv_printf (&path, "%s", (char *) a) == 0)
2776 xmlFree (a);
2777 strv_free (path_orig);
2778 rc = GPG_ERR_ENOMEM;
2779 goto fail;
2782 xmlFree (a);
2783 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
2785 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2787 strv_free (path_orig);
2788 goto fail;
2791 if (!rc)
2794 find_elements (client->doc, n->children, path + 1, &rc, NULL,
2795 NULL, NULL, 0, 0, NULL, 1);
2797 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2799 strv_free (path_orig);
2800 goto fail;
2802 else if (!rc)
2804 xmlNodePtr parent = n->parent;
2806 xmlUnlinkNode (n);
2807 xmlFreeNode (n);
2808 n = parent;
2812 strv_free (path);
2813 path = path_orig;
2815 if (rc == GPG_ERR_ELEMENT_NOT_FOUND)
2817 n = create_element_path (client, &path, &rc, NULL);
2819 if (rc)
2820 goto fail;
2823 copy = xmlCopyNodeList (root);
2824 n = xmlAddChildList (n, copy);
2825 if (!n)
2826 rc = GPG_ERR_BAD_DATA;
2828 else
2830 /* Check if the content root element can create a DTD root element. */
2831 if (!xmlStrEqual ((xmlChar *) "element", root->name))
2833 rc = GPG_ERR_SYNTAX;
2834 goto fail;
2837 xmlChar *a;
2839 if ((a = xmlGetProp (root, (xmlChar *) "_name")) == NULL)
2841 rc = GPG_ERR_SYNTAX;
2842 goto fail;
2845 char *tmp = str_dup ((char *) a);
2846 xmlFree (a);
2847 int literal = is_literal_element (&tmp);
2849 if (!valid_xml_element ((xmlChar *) tmp) || literal)
2851 xfree (tmp);
2852 rc = GPG_ERR_INV_VALUE;
2853 goto fail;
2856 if (strv_printf (&path, "%s", tmp) == 0)
2858 xfree (tmp);
2859 rc = GPG_ERR_ENOMEM;
2860 goto fail;
2863 xfree (tmp);
2864 n = find_root_element (client->doc, &path, &rc, NULL, 0, 1);
2866 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2868 rc = GPG_ERR_BAD_DATA;
2869 goto fail;
2872 /* Overwriting the existing tree. */
2873 if (!rc)
2875 xmlUnlinkNode (n);
2876 xmlFreeNodeList (n);
2879 rc = 0;
2880 xmlSetProp (root, (xmlChar *) "_name", (xmlChar *) path[0]);
2881 n = xmlCopyNode (root, 1);
2882 n = xmlAddChildList (xmlDocGetRootElement (client->doc), n);
2885 if (n && !rc)
2886 rc = update_element_mtime (n->parent);
2888 fail:
2889 if (doc)
2890 xmlFreeDoc (doc);
2892 if (path)
2893 strv_free (path);
2895 strv_free (req);
2896 return rc;
2899 static gpg_error_t
2900 import_command (assuan_context_t ctx, char *line)
2902 gpg_error_t rc;
2903 struct client_s *client = assuan_get_pointer (ctx);
2904 unsigned char *result;
2905 size_t len;
2907 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2908 if (rc)
2909 return send_error (ctx, rc);
2911 rc = do_import (client, result);
2912 return send_error (ctx, rc);
2915 static gpg_error_t
2916 do_lock (struct client_s *client, int add)
2918 gpg_error_t rc = lock_file_mutex (client, add);
2920 if (!rc)
2921 client->flags |= FLAG_LOCK_CMD;
2923 return rc;
2926 static gpg_error_t
2927 lock_command (assuan_context_t ctx, char *line)
2929 struct client_s *client = assuan_get_pointer (ctx);
2930 gpg_error_t rc = do_lock (client, 0);
2932 return send_error (ctx, rc);
2935 static gpg_error_t
2936 unlock_command (assuan_context_t ctx, char *line)
2938 struct client_s *client = assuan_get_pointer (ctx);
2939 gpg_error_t rc;
2941 rc = unlock_file_mutex (client, 0);
2942 return send_error (ctx, rc);
2945 static gpg_error_t
2946 option_command (assuan_context_t ctx, const char *name, const char *value)
2948 struct client_s *client = assuan_get_pointer (ctx);
2949 gpg_error_t rc = 0;
2950 struct pinentry_option_s *pin_opts = &client->pinentry_opts;
2951 #ifdef WITH_AGENT
2952 struct agent_s *agent = client->crypto->agent;
2953 #endif
2955 log_write1 ("OPTION name='%s' value='%s'", name, value);
2957 if (strcasecmp (name, (char *) "log_level") == 0)
2959 long l = 0;
2961 if (value)
2963 l = strtol (value, NULL, 10);
2965 if (l < 0 || l > 2)
2966 return gpg_error (GPG_ERR_INV_VALUE);
2969 MUTEX_LOCK (&rcfile_mutex);
2970 config_set_int_param (&global_config, "global", "log_level", value);
2971 MUTEX_UNLOCK (&rcfile_mutex);
2972 goto done;
2974 else if (strcasecmp (name, (char *) "lock-timeout") == 0)
2976 long n = 0;
2977 char *p = NULL;
2979 if (value)
2981 n = strtol (value, &p, 10);
2982 if (p && *p)
2983 return gpg_error (GPG_ERR_INV_VALUE);
2986 client->lock_timeout = n;
2987 goto done;
2989 else if (strcasecmp (name, (char *) "NAME") == 0)
2991 char *tmp = pthread_getspecific (thread_name_key);
2993 if (tmp)
2994 xfree (tmp);
2996 if (!value || !*value)
2998 pthread_setspecific (thread_name_key, str_dup (""));
2999 goto done;
3002 pthread_setspecific (thread_name_key, str_dup (value));
3003 goto done;
3005 else if (strcasecmp (name, (char *) "lc-messages") == 0)
3007 xfree (pin_opts->lc_messages);
3008 pin_opts->lc_messages = str_dup (value);
3009 #ifdef WITH_AGENT
3010 if (use_agent)
3011 rc = set_agent_option (client->crypto->agent, "lc-messages", value);
3012 #endif
3014 else if (strcasecmp (name, (char *) "lc-ctype") == 0)
3016 xfree (pin_opts->lc_ctype);
3017 pin_opts->lc_ctype = str_dup (value);
3018 #ifdef WITH_AGENT
3019 if (use_agent)
3020 rc = set_agent_option (client->crypto->agent, "lc-ctype", value);
3021 #endif
3023 else if (strcasecmp (name, (char *) "ttyname") == 0)
3025 xfree (pin_opts->ttyname);
3026 pin_opts->ttyname = str_dup (value);
3027 #ifdef WITH_AGENT
3028 if (use_agent)
3029 rc = set_agent_option (client->crypto->agent, "ttyname", value);
3030 #endif
3032 else if (strcasecmp (name, (char *) "ttytype") == 0)
3034 xfree (pin_opts->ttytype);
3035 pin_opts->ttytype = str_dup (value);
3036 #ifdef WITH_AGENT
3037 if (use_agent)
3038 rc = set_agent_option (client->crypto->agent, "ttytype", value);
3039 #endif
3041 else if (strcasecmp (name, (char *) "display") == 0)
3043 xfree (pin_opts->display);
3044 pin_opts->display = str_dup (value);
3045 #ifdef WITH_AGENT
3046 if (use_agent)
3047 rc = set_agent_option (client->crypto->agent, "display", value);
3048 #endif
3050 else if (strcasecmp (name, (char *) "desc") == 0)
3052 xfree (pin_opts->desc);
3053 pin_opts->desc = str_dup (value);
3055 else if (strcasecmp (name, "pinentry_timeout") == 0)
3057 char *p = NULL;
3058 int n;
3060 if (!value)
3061 goto done;
3063 n = (int) strtol (value, &p, 10);
3065 if (*p || n < 0)
3066 return gpg_error (GPG_ERR_INV_VALUE);
3068 pin_opts->timeout = n;
3069 MUTEX_LOCK (&rcfile_mutex);
3070 config_set_int_param (&global_config,
3071 client->filename ? client->filename : "global",
3072 "pinentry_timeout", value);
3073 MUTEX_UNLOCK (&rcfile_mutex);
3074 goto done;
3076 else
3077 return gpg_error (GPG_ERR_UNKNOWN_OPTION);
3079 done:
3080 #ifdef WITH_AGENT
3081 if (!rc && use_agent && agent)
3083 rc = pinentry_merge_options (&client->crypto->agent->pinentry_opts,
3084 pin_opts);
3086 #endif
3088 return rc;
3091 static gpg_error_t
3092 do_rename (assuan_context_t ctx, char *line)
3094 struct client_s *client = assuan_get_pointer (ctx);
3095 gpg_error_t rc;
3096 char **req, **src, *dst;
3097 xmlNodePtr n, ndst;
3099 req = str_split (line, " ", 0);
3101 if (!req || !req[0] || !req[1])
3103 strv_free (req);
3104 return GPG_ERR_SYNTAX;
3107 dst = req[1];
3108 is_literal_element (&dst);
3110 if (!valid_xml_element ((xmlChar *) dst))
3112 strv_free (req);
3113 return GPG_ERR_INV_VALUE;
3116 if (strchr (req[0], '\t'))
3117 src = str_split (req[0], "\t", 0);
3118 else
3119 src = str_split (req[0], " ", 0);
3121 if (!src || !*src)
3123 rc = GPG_ERR_SYNTAX;
3124 goto fail;
3127 n = find_root_element (client->doc, &src, &rc, NULL, 0, 0);
3128 if (src[1] && n)
3129 n = find_elements (client->doc, n->children, src + 1, &rc, NULL, NULL,
3130 NULL, 0, 0, NULL, 0);
3132 if (!n)
3133 goto fail;
3135 xmlChar *a = xmlGetProp (n, (xmlChar *) "_name");
3136 if (!a)
3138 rc = GPG_ERR_ENOMEM;
3139 goto fail;
3142 /* To prevent unwanted effects:
3144 * <root name="a"><b/></root>
3146 * RENAME a<TAB>b b
3148 if (xmlStrEqual (a, (xmlChar *) dst))
3150 xmlFree (a);
3151 rc = GPG_ERR_AMBIGUOUS_NAME;
3152 goto fail;
3155 xmlFree (a);
3156 char **tmp = NULL;
3157 if (src[1])
3159 char **p;
3161 for (p = src; *p; p++)
3163 if (!*(p + 1))
3164 break;
3165 strv_printf (&tmp, "%s", *p);
3169 strv_printf (&tmp, "!%s", dst);
3170 ndst = find_root_element (client->doc, &tmp, &rc, NULL, 0, 0);
3171 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3173 strv_free (tmp);
3174 goto fail;
3177 if (tmp[1] && ndst)
3178 ndst = find_elements (client->doc, ndst->children, tmp + 1, &rc, NULL,
3179 NULL, NULL, 0, 0, NULL, 0);
3181 strv_free (tmp);
3182 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3183 goto fail;
3185 rc = 0;
3187 /* Target may exist:
3189 * <root name="a"/>
3190 * <root name="b" target="a"/>
3192 * RENAME b a
3194 * Would need to do:
3195 * RENAME !b a
3197 if (ndst == n)
3199 rc = GPG_ERR_AMBIGUOUS_NAME;
3200 goto fail;
3203 if (ndst)
3205 unlink_node (ndst);
3206 xmlFreeNodeList (ndst);
3209 rc = add_attribute (n, "_name", dst);
3211 fail:
3212 strv_free (req);
3213 strv_free (src);
3214 return rc;
3217 static gpg_error_t
3218 rename_command (assuan_context_t ctx, char *line)
3220 struct client_s *client = assuan_get_pointer (ctx);
3221 gpg_error_t rc;
3222 struct argv_s *args[] = {
3223 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3224 NULL
3227 rc = parse_options (&line, args, client);
3228 if (rc)
3229 return send_error (ctx, rc);
3231 if (client->opts & OPT_INQUIRE)
3233 unsigned char *result;
3234 size_t len;
3236 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3237 if (rc)
3238 return send_error (ctx, rc);
3240 line = (char *) result;
3243 rc = do_rename (ctx, line);
3245 if (client->opts & OPT_INQUIRE)
3246 xfree (line);
3248 return send_error (ctx, rc);
3251 static gpg_error_t
3252 do_copy (assuan_context_t ctx, char *line)
3254 struct client_s *client = assuan_get_pointer (ctx);
3255 gpg_error_t rc;
3256 char **req, **src = NULL, **dst = NULL;
3257 xmlNodePtr nsrc, ndst, new = NULL;
3259 req = str_split (line, " ", 0);
3260 if (!req || !req[0] || !req[1])
3262 strv_free (req);
3263 return GPG_ERR_SYNTAX;
3266 if (strchr (req[0], '\t'))
3267 src = str_split (req[0], "\t", 0);
3268 else
3269 src = str_split (req[0], " ", 0);
3271 if (!src || !*src)
3273 rc = GPG_ERR_SYNTAX;
3274 goto fail;
3277 if (strchr (req[1], '\t'))
3278 dst = str_split (req[1], "\t", 0);
3279 else
3280 dst = str_split (req[1], " ", 0);
3282 if (!dst || !*dst)
3284 rc = GPG_ERR_SYNTAX;
3285 goto fail;
3288 if (!valid_element_path (dst, 0))
3290 rc = GPG_ERR_INV_VALUE;
3291 goto fail;
3294 nsrc = find_root_element (client->doc, &src, &rc, NULL, 0, 0);
3295 if (nsrc && src[1])
3296 nsrc = find_elements (client->doc, nsrc->children, src + 1, &rc, NULL,
3297 NULL, NULL, 0, 0, NULL, 0);
3299 if (!nsrc)
3300 goto fail;
3302 new = xmlCopyNodeList (nsrc);
3303 if (!new)
3305 rc = GPG_ERR_ENOMEM;
3306 goto fail;
3309 int create = 0;
3310 ndst = find_root_element (client->doc, &dst, &rc, NULL, 0, 0);
3311 if (ndst && dst[1])
3313 if (ndst->children)
3314 ndst = find_elements (client->doc, ndst->children, dst + 1, &rc, NULL,
3315 NULL, create_target_elements_cb, 0, 0, NULL, 0);
3316 else
3317 create = 1;
3319 else
3320 create = 1;
3322 if (!ndst && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3323 goto fail;
3324 else if (create)
3326 ndst = create_element_path (client, &dst, &rc, NULL);
3327 if (!ndst)
3328 goto fail;
3331 /* Merge any attributes from the src node to the initial dst node. */
3332 for (xmlAttrPtr attr = new->properties; attr; attr = attr->next)
3334 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3335 continue;
3337 xmlAttrPtr a = xmlHasProp (ndst, attr->name);
3338 if (a)
3339 xmlRemoveProp (a);
3341 xmlChar *tmp = xmlNodeGetContent (attr->children);
3342 xmlNewProp (ndst, attr->name, tmp);
3343 xmlFree (tmp);
3344 rc = add_attribute (ndst, NULL, NULL);
3347 xmlNodePtr n = ndst->children;
3348 xmlUnlinkNode (n);
3349 xmlFreeNodeList (n);
3350 ndst->children = NULL;
3352 if (new->children)
3354 n = xmlCopyNodeList (new->children);
3355 if (!n)
3357 rc = GPG_ERR_ENOMEM;
3358 goto fail;
3361 n = xmlAddChildList (ndst, n);
3362 if (!n)
3364 rc = GPG_ERR_ENOMEM;
3365 goto fail;
3368 rc = update_element_mtime (xmlDocGetRootElement (client->doc) ==
3369 ndst->parent ? ndst : ndst->parent);
3372 fail:
3373 if (new)
3375 xmlUnlinkNode (new);
3376 xmlFreeNodeList (new);
3379 if (req)
3380 strv_free (req);
3382 if (src)
3383 strv_free (src);
3385 if (dst)
3386 strv_free (dst);
3388 return rc;
3391 static gpg_error_t
3392 copy_command (assuan_context_t ctx, char *line)
3394 struct client_s *client = assuan_get_pointer (ctx);
3395 gpg_error_t rc;
3396 struct argv_s *args[] = {
3397 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3398 NULL
3401 rc = parse_options (&line, args, client);
3402 if (rc)
3403 return send_error (ctx, rc);
3405 if (client->opts & OPT_INQUIRE)
3407 unsigned char *result;
3408 size_t len;
3410 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3411 if (rc)
3412 return send_error (ctx, rc);
3414 line = (char *) result;
3417 rc = do_copy (ctx, line);
3419 if (client->opts & OPT_INQUIRE)
3420 xfree (line);
3422 return send_error (ctx, rc);
3425 static gpg_error_t
3426 do_move (assuan_context_t ctx, char *line)
3428 struct client_s *client = assuan_get_pointer (ctx);
3429 gpg_error_t rc;
3430 char **req, **src = NULL, **dst = NULL;
3431 xmlNodePtr nsrc, ndst = NULL;
3433 req = str_split (line, " ", 0);
3435 if (!req || !req[0] || !req[1])
3437 strv_free (req);
3438 return GPG_ERR_SYNTAX;
3441 if (strchr (req[0], '\t'))
3442 src = str_split (req[0], "\t", 0);
3443 else
3444 src = str_split (req[0], " ", 0);
3446 if (!src || !*src)
3448 rc = GPG_ERR_SYNTAX;
3449 goto fail;
3452 if (strchr (req[1], '\t'))
3453 dst = str_split (req[1], "\t", 0);
3454 else
3455 dst = str_split (req[1], " ", 0);
3457 nsrc = find_root_element (client->doc, &src, &rc, NULL, 0, 0);
3458 if (nsrc && src[1])
3459 nsrc = find_elements (client->doc, nsrc->children, src + 1, &rc, NULL,
3460 NULL, NULL, 0, 0, NULL, 0);
3462 if (!nsrc)
3463 goto fail;
3465 if (dst)
3467 if (!valid_element_path (dst, 0))
3469 rc = GPG_ERR_INV_VALUE;
3470 goto fail;
3473 ndst = find_root_element (client->doc, &dst, &rc, NULL, 0, 0);
3474 if (ndst && dst[1])
3475 ndst = find_elements (client->doc, ndst->children, dst + 1, &rc, NULL,
3476 NULL, NULL, 0, 0, NULL, 0);
3478 else
3479 ndst = xmlDocGetRootElement (client->doc);
3481 for (xmlNodePtr n = ndst; n; n = n->parent)
3483 if (n == nsrc)
3485 rc = GPG_ERR_CONFLICT;
3486 goto fail;
3490 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3491 goto fail;
3493 rc = 0;
3495 if (ndst)
3497 xmlChar *a = node_has_attribute (nsrc, (xmlChar *) "_name");
3498 xmlNodePtr dup = find_element (ndst->children, (char *) a, NULL);
3500 xmlFree (a);
3501 if (dup)
3503 if (dup == nsrc)
3504 goto fail;
3506 if (ndst == xmlDocGetRootElement (client->doc))
3508 xmlNodePtr n = nsrc;
3509 int match = 0;
3511 while (n->parent && n->parent != ndst)
3512 n = n->parent;
3514 xmlChar *a = node_has_attribute (n, (xmlChar *) "_name");
3515 xmlChar *b = node_has_attribute (nsrc, (xmlChar *) "_name");
3517 if (xmlStrEqual (a, b))
3519 match = 1;
3520 xmlUnlinkNode (nsrc);
3521 xmlUnlinkNode (n);
3522 xmlFreeNodeList (n);
3525 xmlFree (a);
3526 xmlFree (b);
3528 if (!match)
3530 xmlUnlinkNode (dup);
3531 xmlFreeNodeList (dup);
3534 else
3535 xmlUnlinkNode (dup);
3539 if (!ndst && dst)
3541 xmlChar *name = node_has_attribute (nsrc, (xmlChar *) "_name");
3543 if (nsrc->parent == xmlDocGetRootElement (client->doc)
3544 && !strcmp ((char *) name, *dst))
3546 xmlFree (name);
3547 rc = GPG_ERR_CONFLICT;
3548 goto fail;
3551 xmlFree (name);
3552 ndst = create_element_path (client, &dst, &rc, nsrc);
3555 if (!ndst)
3556 goto fail;
3558 update_element_mtime (nsrc->parent);
3559 xmlUnlinkNode (nsrc);
3560 ndst = xmlAddChildList (ndst, nsrc);
3562 if (!ndst)
3563 rc = GPG_ERR_ENOMEM;
3565 update_element_mtime (ndst->parent);
3567 fail:
3568 if (req)
3569 strv_free (req);
3571 if (src)
3572 strv_free (src);
3574 if (dst)
3575 strv_free (dst);
3577 return rc;
3580 static gpg_error_t
3581 move_command (assuan_context_t ctx, char *line)
3583 struct client_s *client = assuan_get_pointer (ctx);
3584 gpg_error_t rc;
3585 struct argv_s *args[] = {
3586 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3587 NULL
3590 rc = parse_options (&line, args, client);
3591 if (rc)
3592 return send_error (ctx, rc);
3594 if (client->opts & OPT_INQUIRE)
3596 unsigned char *result;
3597 size_t len;
3599 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3600 if (rc)
3601 return send_error (ctx, rc);
3603 line = (char *) result;
3606 rc = do_move (ctx, line);
3608 if (client->opts & OPT_INQUIRE)
3609 xfree (line);
3611 return send_error (ctx, rc);
3614 static gpg_error_t
3615 ls_command (assuan_context_t ctx, char *line)
3617 gpg_error_t rc;
3618 char *tmp = str_asprintf ("%s/data", homedir);
3619 char *dir = expand_homedir (tmp);
3620 DIR *d = opendir (dir);
3622 rc = gpg_error_from_syserror ();
3623 xfree (tmp);
3625 if (!d)
3627 xfree (dir);
3628 return send_error (ctx, rc);
3631 size_t len =
3632 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3633 struct dirent *p = xmalloc (len), *cur = NULL;
3634 char *list = NULL;
3636 xfree (dir);
3637 rc = 0;
3639 while (!readdir_r (d, p, &cur) && cur)
3641 if (cur->d_name[0] == '.' && cur->d_name[1] == '\0')
3642 continue;
3643 else if (cur->d_name[0] == '.' && cur->d_name[1] == '.'
3644 && cur->d_name[2] == '\0')
3645 continue;
3647 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3649 if (!tmp)
3651 if (list)
3652 xfree (list);
3654 rc = GPG_ERR_ENOMEM;
3655 break;
3658 xfree (list);
3659 list = tmp;
3662 closedir (d);
3663 xfree (p);
3665 if (rc)
3666 return send_error (ctx, rc);
3668 if (!list)
3669 return send_error (ctx, GPG_ERR_NO_DATA);
3671 list[strlen (list) - 1] = 0;
3672 rc = xfer_data (ctx, list, strlen (list));
3673 xfree (list);
3674 return send_error (ctx, rc);
3677 static gpg_error_t
3678 bye_notify (assuan_context_t ctx, char *line)
3680 struct client_s *cl = assuan_get_pointer (ctx);
3682 #ifdef WITH_GNUTLS
3683 if (cl->thd->remote)
3685 int rc;
3689 struct timeval tv = { 0, 50000 };
3691 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3692 if (rc == GNUTLS_E_AGAIN)
3693 select (0, NULL, NULL, NULL, &tv);
3695 while (rc == GNUTLS_E_AGAIN);
3697 #endif
3699 /* This will let assuan_process_next() return. */
3700 fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK);
3701 cl->last_rc = 0; // BYE command result
3702 return 0;
3705 static gpg_error_t
3706 reset_notify (assuan_context_t ctx, char *line)
3708 struct client_s *client = assuan_get_pointer (ctx);
3710 if (client)
3711 cleanup_client (client);
3713 return 0;
3717 * This is called before every Assuan command.
3719 static gpg_error_t
3720 command_startup (assuan_context_t ctx, const char *name)
3722 struct client_s *client = assuan_get_pointer (ctx);
3723 gpg_error_t rc;
3724 struct command_table_s *cmd = NULL;
3726 log_write1 ("command='%s'", name);
3727 client->last_rc = client->opts = 0;
3729 for (int i = 0; command_table[i]; i++)
3731 if (!strcasecmp (name, command_table[i]->name))
3733 if (command_table[i]->ignore_startup)
3734 return 0;
3735 cmd = command_table[i];
3736 break;
3740 client->last_rc = rc = gpg_error (file_modified (client, cmd));
3741 return rc;
3745 * This is called after every Assuan command.
3747 static void
3748 command_finalize (assuan_context_t ctx, gpg_error_t rc)
3750 struct client_s *client = assuan_get_pointer (ctx);
3752 if (!(client->flags & FLAG_LOCK_CMD))
3753 unlock_file_mutex (client, 0);
3755 log_write1 (_("command completed: rc=%u"), client->last_rc);
3756 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
3759 static gpg_error_t
3760 help_command (assuan_context_t ctx, char *line)
3762 gpg_error_t rc;
3763 int i;
3765 if (!line || !*line)
3767 char *tmp;
3768 char *help = str_dup (_("Usage: HELP [<COMMAND>]\n"
3769 "For commands that take an element path as an argument, each element is "
3770 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3771 "\n" "COMMANDS:"));
3773 for (i = 0; command_table[i]; i++)
3775 if (!command_table[i]->help)
3776 continue;
3778 tmp = str_asprintf ("%s %s", help, command_table[i]->name);
3779 xfree (help);
3780 help = tmp;
3783 tmp = strip_texi_and_wrap (help);
3784 xfree (help);
3785 rc = xfer_data (ctx, tmp, strlen (tmp));
3786 xfree (tmp);
3787 return send_error (ctx, rc);
3790 for (i = 0; command_table[i]; i++)
3792 if (!strcasecmp (line, command_table[i]->name))
3794 char *help, *tmp;
3796 if (!command_table[i]->help)
3797 break;
3799 help = strip_texi_and_wrap (command_table[i]->help);
3800 tmp = str_asprintf (_("Usage: %s"), help);
3801 xfree (help);
3802 rc = xfer_data (ctx, tmp, strlen (tmp));
3803 xfree (tmp);
3804 return send_error (ctx, rc);
3808 return send_error (ctx, GPG_ERR_INV_NAME);
3811 static void
3812 new_command (const char *name, int ignore, int unlock,
3813 gpg_error_t (*handler) (assuan_context_t, char *),
3814 const char *help)
3816 int i = 0;
3818 if (command_table)
3819 for (i = 0; command_table[i]; i++);
3821 command_table =
3822 xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
3823 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
3824 command_table[i]->name = name;
3825 command_table[i]->handler = handler;
3826 command_table[i]->ignore_startup = ignore;
3827 command_table[i]->unlock = unlock;
3828 command_table[i++]->help = help;
3829 command_table[i] = NULL;
3832 void
3833 deinit_commands ()
3835 int i;
3837 for (i = 0; command_table[i]; i++)
3838 xfree (command_table[i]);
3840 xfree (command_table);
3843 static int
3844 sort_commands (const void *arg1, const void *arg2)
3846 struct command_table_s *const *a = arg1;
3847 struct command_table_s *const *b = arg2;
3849 if (!*a || !*b)
3850 return 0;
3851 else if (*a && !*b)
3852 return 1;
3853 else if (!*a && *b)
3854 return -1;
3856 return strcmp ((*a)->name, (*b)->name);
3859 static gpg_error_t
3860 passwd_command (assuan_context_t ctx, char *line)
3862 struct client_s *client = assuan_get_pointer (ctx);
3863 gpg_error_t rc;
3864 struct argv_s *args[] = {
3865 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
3866 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
3867 NULL
3870 if (client->flags & FLAG_NEW)
3871 return send_error (ctx, GPG_ERR_INV_STATE);
3873 client->crypto->save.s2k_count =
3874 config_get_ulong (client->filename, "s2k_count");
3875 rc = parse_options (&line, args, client);
3876 if (rc)
3877 return send_error (ctx, rc);
3879 if (!rc && client->opts & OPT_RESET)
3881 rc = cache_clear (client->md5file);
3882 if (!rc)
3883 send_status_all (STATUS_CACHE, NULL);
3886 if (!rc)
3888 if (!IS_PKCS (client->crypto))
3890 struct crypto_s *crypto;
3892 xfree (client->crypto->filename);
3893 client->crypto->filename = str_dup (client->filename);
3894 rc = change_passwd (ctx, client->filename,
3895 client->flags & FLAG_NO_PINENTRY, &crypto);
3896 if (!rc)
3898 cleanup_crypto (&client->crypto);
3899 client->crypto = crypto;
3900 update_checksum (client);
3901 cleanup_crypto_stage1 (client->crypto);
3904 #ifdef WITH_AGENT
3905 else
3907 if (client->crypto->save.s2k_count)
3908 rc = send_to_agent (client->crypto->agent, NULL, NULL,
3909 "OPTION s2k-count=%lu",
3910 client->crypto->save.s2k_count);
3912 if (!rc)
3913 rc = agent_passwd (client->crypto);
3915 #endif
3918 return send_error (ctx, rc);
3921 static gpg_error_t
3922 parse_keygrip_opt_sign (void *data, void *value)
3924 struct client_s *client = data;
3926 (void) value;
3927 client->opts |= OPT_SIGN;
3928 return 0;
3931 static gpg_error_t
3932 keygrip_command (assuan_context_t ctx, char *line)
3934 struct client_s *client = assuan_get_pointer (ctx);
3935 gpg_error_t rc;
3936 struct crypto_s *crypto;
3937 struct argv_s *args[] = {
3938 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_keygrip_opt_sign},
3939 NULL
3942 if (!line || !*line)
3943 return send_error (ctx, GPG_ERR_SYNTAX);
3945 rc = parse_options (&line, args, client);
3946 if (rc)
3947 return send_error (ctx, rc);
3949 if (!valid_filename (line))
3950 return send_error (ctx, GPG_ERR_INV_VALUE);
3952 rc = init_client_crypto (&crypto);
3953 if (rc)
3954 return send_error (ctx, rc);
3956 rc = read_data_file (line, crypto);
3957 if (!rc)
3959 char *hexgrip = NULL;
3961 if (!IS_PKCS (crypto))
3963 cleanup_crypto (&crypto);
3964 return send_error (ctx, GPG_ERR_NOT_SUPPORTED);
3967 if (client->opts & OPT_SIGN)
3969 if (valid_keygrip (crypto->sign_grip, sizeof (crypto->sign_grip)))
3970 hexgrip = bin2hex (crypto->sign_grip, sizeof (crypto->sign_grip));
3973 if (!hexgrip)
3974 hexgrip = bin2hex (crypto->grip, sizeof (crypto->grip));
3976 if (!hexgrip)
3977 rc = GPG_ERR_ENOMEM;
3978 else
3979 rc = xfer_data (ctx, hexgrip, strlen (hexgrip));
3981 xfree (hexgrip);
3984 cleanup_crypto (&crypto);
3985 return send_error (ctx, rc);
3988 static gpg_error_t
3989 parse_opt_data (void *data, void *value)
3991 struct client_s *client = data;
3993 (void) value;
3994 client->opts |= OPT_DATA;
3995 return 0;
3998 static gpg_error_t
3999 getinfo_command (assuan_context_t ctx, char *line)
4001 struct client_s *client = assuan_get_pointer (ctx);
4002 gpg_error_t rc;
4003 char buf[ASSUAN_LINELENGTH];
4004 struct argv_s *args[] = {
4005 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4006 NULL
4009 rc = parse_options (&line, args, client);
4010 if (rc)
4011 return send_error (ctx, rc);
4013 if (!strcasecmp (line, "clients"))
4015 if (client->opts & OPT_DATA)
4017 MUTEX_LOCK (&cn_mutex);
4018 snprintf (buf, sizeof (buf), "%i", slist_length (cn_thread_list));
4019 MUTEX_UNLOCK (&cn_mutex);
4020 rc = xfer_data (ctx, buf, strlen (buf));
4022 else
4023 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4025 else if (!strcasecmp (line, "cache"))
4027 if (client->opts & OPT_DATA)
4029 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4030 rc = xfer_data (ctx, buf, strlen (buf));
4032 else
4033 rc = send_status (ctx, STATUS_CACHE, NULL);
4035 else if (!strcasecmp (line, "pid"))
4037 char buf[32];
4038 pid_t pid = getpid ();
4040 snprintf (buf, sizeof (buf), "%i", pid);
4041 rc = xfer_data (ctx, buf, strlen (buf));
4043 else if (!strcasecmp (line, "version"))
4045 char *buf = str_asprintf ("0x%06x %s", VERSION_HEX,
4046 #ifdef WITH_LIBACL
4047 "ACL "
4048 #endif
4049 #ifdef WITH_GNUTLS
4050 "GNUTLS "
4051 #endif
4052 #ifdef WITH_AGENT
4053 "AGENT "
4054 #endif
4055 #ifdef WITH_QUALITY
4056 "QUALITY "
4057 #endif
4058 "");
4059 rc = xfer_data (ctx, buf, strlen (buf));
4060 xfree (buf);
4062 else if (!strcasecmp (line, "last_error"))
4064 if (client->last_error)
4065 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4066 else
4067 rc = GPG_ERR_NO_DATA;
4069 else
4070 rc = gpg_error (GPG_ERR_SYNTAX);
4072 return send_error (ctx, rc);
4075 #ifdef WITH_AGENT
4076 static gpg_error_t
4077 send_data_cb (void *user, const void *buf, size_t len)
4079 assuan_context_t ctx = user;
4081 return assuan_send_data (ctx, buf, len);
4084 static gpg_error_t
4085 send_status_cb (void *user, const char *line)
4087 assuan_context_t ctx = user;
4088 char keyword[200], *k;
4089 const char *p;
4091 for (p = line, k = keyword; *p; p++)
4093 if (isspace (*p))
4094 break;
4096 *k++ = *p;
4099 *k = 0;
4100 if (*p == '#')
4101 p++;
4103 while (isspace (*p))
4104 p++;
4106 return assuan_write_status (ctx, keyword, *p ? p : NULL);
4108 #endif
4110 static gpg_error_t
4111 agent_command (assuan_context_t ctx, char *line)
4113 gpg_error_t rc = 0;
4115 if (!use_agent)
4116 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
4118 #ifdef WITH_AGENT
4119 struct client_s *client = assuan_get_pointer (ctx);
4121 if (!line || !*line)
4122 return send_error (ctx, GPG_ERR_SYNTAX);
4124 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 1);
4125 rc = assuan_transact (client->crypto->agent->ctx, line, send_data_cb,
4126 client->ctx, agent_loopback_cb, client->crypto,
4127 send_status_cb, client->ctx);
4128 if (gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
4130 char *line;
4131 size_t len;
4133 rc = assuan_write_line (client->crypto->agent->ctx, "CAN");
4134 if (!rc)
4136 rc = assuan_read_line (client->crypto->agent->ctx, &line, &len);
4137 if (!rc)
4138 rc = gpg_error (GPG_ERR_ASS_CANCELED);
4142 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 0);
4143 #endif
4144 return send_error (ctx, rc);
4147 void
4148 init_commands ()
4150 /* !BEGIN-HELP-TEXT!
4152 * This comment is used as a marker to generate the offline documentation
4153 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4154 * script to determine where commands begin and end.
4156 new_command("HELP", 1, 1, help_command, _(
4157 "HELP [<COMMAND>]\n"
4158 "Show available commands or command specific help text."
4161 new_command("AGENT", 1, 1, agent_command, _(
4162 "AGENT <command>\n"
4163 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4164 "@command{gpg-agent}."
4167 new_command("GETINFO", 1, 1, getinfo_command, _(
4168 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4169 "Get server and other information: @var{cache} returns the number of cached "
4170 "documents via a status message. @var{clients} returns the number of "
4171 "connected clients via a status message. @var{pid} returns the process ID "
4172 "number of the server via a data response. @var{VERSION} returns the server "
4173 "version number and compile-time features with a data response with each "
4174 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4175 "the last failed command when available. @xref{Status Messages}. "
4176 "\n"
4177 "When the @option{--data} option is specified then the result will be send "
4178 "via a data response rather than a status message."
4181 new_command("PASSWD", 0, 0, passwd_command, _(
4182 "PASSWD [--reset] [--s2k-count=N]\n"
4183 "Changes the passphrase of the secret key required to open the current "
4184 "file. When the @option{--reset} option is passed then the cache entry for "
4185 "the current file will be reset and the passphrase, if any, will be required "
4186 "during the next @code{OPEN}. @xref{OPEN}."
4187 "\n"
4188 "The @option{--s2k-count} option sets number of hash iterations for a "
4189 "passphrase and must be either @code{0} to use the calibrated count of the "
4190 "machine (the default), or a value greater than or equal to @code{65536}. "
4191 "@xref{SAVE}."
4194 new_command("KEYGRIP", 1, 1, keygrip_command, _(
4195 "KEYGRIP [--sign] <filename>\n"
4196 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4197 "data response."
4198 "\n"
4199 "When the @option{--sign} option is specified then the key used for signing "
4200 "of the specified @var{filename} will be returned."
4203 new_command("OPEN", 1, 1, open_command, _(
4204 "OPEN [--lock] [--no-pinentry] <filename> [<passphrase>]\n"
4205 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4206 "found on the file-system then a new document will be created. If the file "
4207 "is found, it is looked for in the file cache. If cached and no "
4208 "@var{passphrase} was specified then the cached document is opened. When not "
4209 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4210 "for decryption unless @option{--no-pinentry} was specified (see below)."
4211 "\n"
4212 "When the @option{--lock} option is passed then the file mutex will be "
4213 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4214 "file has been opened."
4215 "\n"
4216 "By default, a pinentry is used to retrieve a passphrase when required. "
4217 "Passing @option{--no-pinentry} will disable pinentry use for the rest of "
4218 "the session. When pinentry use is disabled but required for some operation "
4219 "then a server @emph{INQUIRE} will be send to the client to retrieve the "
4220 "passphrase. See the @code{OPTION} command (@pxref{OPTION}), for pinentry "
4221 "specific options."
4224 new_command("SAVE", 0, 0, save_command, _(
4225 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring [--sign-keygrip=hexstring]]\n"
4226 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4227 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4228 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4229 "keypair will be generated and a pinentry will be used to prompt for the "
4230 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4231 "passed, in which case the data file will not be passphrase protected."
4232 "\n"
4233 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4234 "passphrase retrieval and caching for new files and behaves as if the "
4235 "@option{--no-agent} commmand line option to @command{pwmd} was specified."
4236 "\n"
4237 "The @option{--reset} option will clear the cache entry for the current file "
4238 "before saving."
4239 "\n"
4240 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4241 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4242 "(@pxref{Configuration}) for available ciphers."
4243 "\n"
4244 "The @option{--cipher-iterations} option specifies the number of times to "
4245 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4246 "\n"
4247 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4248 "the client to obtain the key paramaters to use when generating a new "
4249 "keypair. The inquired data is expected to be an S-expression. If not "
4250 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4251 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4252 "that when this option is specified a new keypair will be generated "
4253 "reguardless if the file is a new one or not."
4254 "\n"
4255 "You can encrypt the data file to a public key other than the one that it "
4256 "was originally encrypted with by passing the @option{--keygrip} option with "
4257 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4258 "be of any key that @command{gpg-agent} knows about. The "
4259 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4260 "secret key. This option may be needed when using a smartcard."
4261 "\n"
4262 "The @option{--s2k-count} option sets number of hash iterations for a "
4263 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4264 "value which is the default. This setting only affects new files. To change "
4265 "the setting, use the @code{PASSWD} command (@pxref{PASSWD})."
4268 new_command("ISCACHED", 1, 0, iscached_command, _(
4269 "ISCACHED [--lock] <filename>\n"
4270 "An @emph{OK} response is returned if the specified @var{filename} is found "
4271 "in the file cache. If not found in the cache but exists on the filesystem "
4272 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4273 "returned."
4274 "\n"
4275 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4276 "file exists; it does not need to be opened nor cached."
4279 new_command("CLEARCACHE", 1, 1, clearcache_command, _(
4280 "CLEARCACHE [<filename>]\n"
4281 "Clears a file cache entry for all or the specified @var{filename}."
4284 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command, _(
4285 "CACHETIMEOUT <filename> <seconds>\n"
4286 "The time in @var{seconds} until @var{filename} will be removed from the "
4287 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4288 "the passphrase for each @code{OPEN} command (@pxref{OPEN}). "
4289 "@xref{Configuration}, and the @code{cache_timeout} parameter."
4292 new_command("LIST", 0, 1, list_command, _(
4293 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4294 "If no element path is given then a newline separated list of root elements "
4295 "is returned with a data response. If given, then all reachable elements "
4296 "of the specified element path are returned unless the @option{--no-recurse} "
4297 "option is specified. If specified, only the child elements of the element "
4298 "path are returned without recursing into grandchildren. Each resulting "
4299 "element is prefixed with the literal @code{!} character when the element "
4300 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4301 "\n"
4302 "When the @option{--verbose} option is passed then each element path "
4303 "returned will have zero or more flags appened to it. These flags are "
4304 "delimited from the element path by a single space character. A flag itself "
4305 "is a single character. Flag @code{+} indicates that there are child nodes of "
4306 "the current element path. Flag @code{E} indicates that an element of an "
4307 "element path contained in a @var{target} attribute could not be found. Flag "
4308 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4309 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4310 "of a @var{target} attribute (see below)."
4311 "\n"
4312 "The @option{--with-target} option implies @option{--verbose} and will append "
4313 "an additional flag @code{T} followed by a single space then an element path. "
4314 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4315 "current element when it contains a @var{target} attribute. When no "
4316 "@var{target} attribute is found then no flag will be appended."
4317 "\n"
4318 "The @option{--no-recurse} option limits the amount of data returned to only "
4319 "the listing of children of the specified element path and not any "
4320 "grandchildren."
4321 "\n"
4322 "The @option{--all} option lists the entire element tree for each root "
4323 "element. This option also implies option @option{--verbose}."
4324 "\n"
4325 "When the @option{--inquire} option is passed then all remaining non-option "
4326 "arguments are retrieved via a server @emph{INQUIRE}."
4329 new_command("REALPATH", 0, 1, realpath_command, _(
4330 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4331 "Resolves all @code{target} attributes of the specified element path and "
4332 "returns the result with a data response. @xref{Target Attribute}, for details."
4333 "\n"
4334 "When the @option{--inquire} option is passed then all remaining non-option "
4335 "arguments are retrieved via a server @emph{INQUIRE}."
4338 new_command("STORE", 0, 1, store_command, _(
4339 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4340 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4341 "\n"
4342 "Creates a new element path or modifies the @var{content} of an existing "
4343 "element. If only a single element is specified then a new root element is "
4344 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4345 "set to the final @key{TAB} delimited element. If no @var{content} is "
4346 "specified after the final @key{TAB}, then the content of the element will "
4347 "be removed, or empty when creating a new element."
4348 "\n"
4349 "The only restriction of an element name is that it not contain whitespace "
4350 "or begin with the literal element character @code{!} unless specifying a "
4351 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4352 "the @key{TAB} delimited elements. It is recommended that the content of an "
4353 "element be base64 encoded when it contains control or @key{TAB} characters "
4354 "to prevent @abbr{XML} and @command{pwmd} parsing errors."
4357 new_command("RENAME", 0, 1, rename_command, _(
4358 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4359 "Renames the specified @var{element} to the new @var{value}. If an element of "
4360 "the same name as the @var{value} already exists it will be overwritten."
4361 "\n"
4362 "When the @option{--inquire} option is passed then all remaining non-option "
4363 "arguments are retrieved via a server @emph{INQUIRE}."
4366 new_command("COPY", 0, 1, copy_command, _(
4367 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4368 "Copies the entire element tree starting from the child node of the source "
4369 "element, to the destination element path. If the destination element path "
4370 "does not exist then it will be created; otherwise it is overwritten."
4371 "\n"
4372 "Note that attributes from the source element are merged into the "
4373 "destination element when the destination element path exists. When an "
4374 "attribute of the same name exists in both the source and destination "
4375 "elements then the destination attribute will be updated to the source "
4376 "attribute value."
4377 "\n"
4378 "When the @option{--inquire} option is passed then all remaining non-option "
4379 "arguments are retrieved via a server @emph{INQUIRE}."
4382 new_command("MOVE", 0, 1, move_command, _(
4383 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4384 "Moves the source element path to the destination element path. If the "
4385 "destination is not specified then it will be moved to the root node of the "
4386 "document. If the destination is specified and exists then it will be "
4387 "overwritten; otherwise it will be created."
4388 "\n"
4389 "When the @option{--inquire} option is passed then all remaining non-option "
4390 "arguments are retrieved via a server @emph{INQUIRE}."
4393 new_command("DELETE", 0, 1, delete_command, _(
4394 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4395 "Removes the specified element path and all of its children. This may break "
4396 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4397 "refers to this element or any of its children."
4398 "\n"
4399 "When the @option{--inquire} option is passed then all remaining non-option "
4400 "arguments are retrieved via a server @emph{INQUIRE}."
4403 new_command("GET", 0, 1, get_command, _(
4404 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4405 "Retrieves the content of the specified element. The content is returned "
4406 "with a data response."
4407 "\n"
4408 "When the @option{--inquire} option is passed then all remaining non-option "
4409 "arguments are retrieved via a server @emph{INQUIRE}."
4412 new_command("ATTR", 0, 1, attr_command, _(
4413 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4414 "@table @asis\n"
4415 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4416 "\n"
4417 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4418 "element. When no @var{value} is specified any existing value will be removed."
4419 "\n"
4420 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4421 "\n"
4422 " Removes an @var{attribute} from an element."
4423 "\n"
4424 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4425 "\n"
4426 " Retrieves a newline separated list of attributes names and values "
4427 "from the specified element. Each attribute name and value is space delimited."
4428 "\n"
4429 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4430 "\n"
4431 " Retrieves the value of an @var{attribute} from an element."
4432 "@end table\n"
4433 "\n"
4434 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4435 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4436 "commands instead."
4437 "\n"
4438 "The @code{_mtime} attribute is updated each time an element is modified by "
4439 "either storing content, editing attributes or by deleting a child element. "
4440 "The @code{_ctime} attribute is created for each new element in an element "
4441 "path."
4442 "\n"
4443 "When the @option{--inquire} option is passed then all remaining non-option "
4444 "arguments are retrieved via a server @emph{INQUIRE}."
4445 "\n"
4446 "@xref{Target Attribute}, for details about this special attribute."
4449 new_command("XPATH", 0, 1, xpath_command, _(
4450 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4451 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4452 "specified, it is assumed the expression is a request to return a result. "
4453 "Otherwise, the result is set to the @var{value} argument and the document is "
4454 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4455 "is assumed to be empty and the document is updated. For example:"
4456 "@sp 1\n"
4457 "@example\n"
4458 "XPATH //element[@@_name='password']@key{TAB}\n"
4459 "@end example\n"
4460 "@sp 1"
4461 "would clear the content of all @code{password} elements in the data file "
4462 "while leaving off the trailing @key{TAB} would return all @code{password} "
4463 "elements in @abbr{XML} format."
4464 "\n"
4465 "When the @option{--inquire} option is passed then all remaining non-option "
4466 "arguments are retrieved via a server @emph{INQUIRE}."
4467 "\n"
4468 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4469 "expression syntax."
4472 new_command("XPATHATTR", 0, 1, xpathattr_command, _(
4473 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4474 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4475 "attributes and does not return a result. For the @var{SET} operation the "
4476 "@var{value} is optional but the field is required. If not specified then "
4477 "the attribute value will be empty. For example:"
4478 "@sp 1"
4479 "@example\n"
4480 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4481 "@end example\n"
4482 "@sp 1"
4483 "would create an @code{password} attribute for each @code{password} element "
4484 "found in the document. The attribute value will be empty but still exist."
4485 "\n"
4486 "When the @option{--inquire} option is passed then all remaining non-option "
4487 "arguments are retrieved via a server @emph{INQUIRE}."
4488 "\n"
4489 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4490 "expression syntax."
4493 new_command("IMPORT", 0, 1, import_command, _(
4494 "IMPORT <content>[<TAB>[!]element[<TAB>[!]child[..]]]\n"
4495 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4496 "\n"
4497 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4498 "argument is raw @abbr{XML} data. The content is created as a child of the "
4499 "specified element path and will overwrite an existing element of the same "
4500 "name. If an element of the element path does not exist then it will be "
4501 "created."
4502 "\n"
4503 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4504 "for details."
4507 new_command("DUMP", 0, 1, dump_command, _(
4508 "DUMP\n"
4509 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4510 "dumping a specific node."
4513 new_command("LOCK", 0, 0, lock_command, _(
4514 "LOCK\n"
4515 "Locks the mutex associated with the opened file. This prevents other clients "
4516 "from sending commands to the same opened file until the client "
4517 "that sent this command either disconnects or sends the @code{UNLOCK} "
4518 "command. @xref{UNLOCK}."
4521 new_command("UNLOCK", 1, 0, unlock_command, _(
4522 "UNLOCK\n"
4523 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4524 "a commands' @option{lock} option. @xref{LOCK}."
4527 new_command("GETCONFIG", 1, 1, getconfig_command, _(
4528 "GETCONFIG [filename] <parameter>\n"
4529 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4530 "data response. If no file has been opened then the value for @var{filename} "
4531 "or the default from the @samp{global} section will be returned. If a file "
4532 "has been opened and no @var{filename} is specified, a value previously "
4533 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4536 new_command("OPTION", 1, 1, NULL, _(
4537 "OPTION <NAME>=<VALUE>\n"
4538 "Sets a client option @var{name} to @var{value}. The value for an option is "
4539 "kept for the duration of the connection."
4540 "\n"
4541 "@table @asis\n"
4542 "@item TTYNAME\n"
4543 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4544 "\n"
4545 "@item TTYTYPE\n"
4546 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4547 "\n"
4548 "@item DISPLAY\n"
4549 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4550 "\n"
4551 "@item DESC\n"
4552 " Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4553 "\n"
4554 "@item LC-CTYPE\n"
4555 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4556 "\n"
4557 "@item LC-MESSAGES\n"
4558 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4559 "\n"
4560 "@item NAME\n"
4561 " Associates the thread ID of the connection with the specified textual "
4562 "representation. Useful for debugging log messages."
4563 "\n"
4564 "@item LOCK-TIMEOUT\n"
4565 " When not @code{0}, the duration in tenths of a second to wait for the file "
4566 "mutex which has been locked by another thread to be released before returning "
4567 "an error. When @code{-1}, then an error will be returned immediately."
4568 "@end table\n"
4571 new_command("LS", 1, 1, ls_command, _(
4572 "LS\n"
4573 "Lists the available data files stored in the data directory "
4574 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4577 new_command("RESET", 1, 1, NULL, _(
4578 "RESET\n"
4579 "Closes the currently opened file but keeps any previously set client options."
4582 new_command("NOP", 1, 1, NULL, _(
4583 "NOP\n"
4584 "Does nothing. Always returns successfully."
4587 /* !END-HELP-TEXT! */
4588 new_command ("CANCEL", 1, 1, NULL, NULL);
4589 new_command ("END", 1, 1, NULL, NULL);
4590 new_command ("BYE", 1, 1, NULL, NULL);
4592 int i;
4593 for (i = 0; command_table[i]; i++);
4594 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4595 sort_commands);
4598 gpg_error_t
4599 register_commands (assuan_context_t ctx)
4601 int i = 0, rc;
4603 for (; command_table[i]; i++)
4605 if (!command_table[i]->handler)
4606 continue;
4608 rc = assuan_register_command (ctx, command_table[i]->name,
4609 command_table[i]->handler,
4610 command_table[i]->help);
4611 if (rc)
4612 return rc;
4615 rc = assuan_register_option_handler (ctx, option_command);
4616 if (rc)
4617 return rc;
4619 rc = assuan_register_bye_notify (ctx, bye_notify);
4620 if (rc)
4621 return rc;
4623 rc = assuan_register_reset_notify (ctx, reset_notify);
4624 if (rc)
4625 return rc;
4627 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4628 if (rc)
4629 return rc;
4631 return assuan_register_post_cmd_notify (ctx, command_finalize);