Add and rename pinentry OPTION's.
[libpwmd.git] / src / commands.c
blob00e686cbb9db6840cb59eb3ce3bbd30bfc0fb9b9
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 int no_pinentry = (client->flags & FLAG_NO_PINENTRY);
215 struct pinentry_option_s pin_opts;
216 #ifdef WITH_AGENT
217 struct pinentry_option_s agent_pin_opts;
219 if (crypto->agent)
220 memcpy (&agent_pin_opts, &crypto->agent->pinentry_opts,
221 sizeof(struct pinentry_option_s));
222 #endif
224 memcpy (&pin_opts, &client->pinentry_opts, sizeof(struct pinentry_option_s));
225 unlock_file_mutex (client, client->flags & FLAG_NEW);
226 free_client (client);
227 memset (client, 0, sizeof (struct client_s));
228 client->crypto = crypto;
229 client->ctx = ctx;
230 client->thd = thd;
231 client->lock_timeout = lock_timeout;
232 memcpy (&client->pinentry_opts, &pin_opts, sizeof(struct pinentry_option_s));
233 client->flags |= no_pinentry ? FLAG_NO_PINENTRY : 0;
234 #ifdef WITH_AGENT
235 memcpy (&crypto->agent->pinentry_opts, &agent_pin_opts,
236 sizeof(struct pinentry_option_s));
237 #endif
240 static gpg_error_t
241 open_finalize (assuan_context_t ctx, char *key, size_t keylen)
243 struct client_s *client = assuan_get_pointer (ctx);
244 gpg_error_t rc = 0;
245 struct cache_data_s *cdata = cache_get_data (client->md5file);
246 int cached = 0, keyarg = key ? 1 : 0;
247 size_t keysize;
248 unsigned char *salted_key = NULL;
249 int algo;
251 client->crypto->filename = str_dup (client->filename);
252 if (cdata || client->flags & FLAG_NEW)
254 if (!(client->flags & FLAG_NEW))
256 rc = decrypt_xml (client->crypto, cdata->doc, cdata->doclen);
257 if (rc)
258 return rc;
261 cached = cdata != NULL;
262 goto done;
265 if (!key && !IS_PKCS (client->crypto))
267 if (client->flags & FLAG_NO_PINENTRY)
269 rc = inquire_passphrase (ctx, "PASSPHRASE", (unsigned char **)&key,
270 &keylen);
271 if (rc)
272 return rc;
274 else
276 client->pinentry_opts.timeout = config_get_integer (client->filename,
277 "pinentry_timeout");
278 rc = getpin_common (client->ctx, client->filename, PINENTRY_OPEN,
279 &key, &keylen);
280 if (rc)
281 return rc;
285 if (!IS_PKCS (client->crypto))
287 algo = cipher_to_gcrypt (client->crypto->hdr.flags);
288 rc = hash_key (algo, client->crypto->hdr.salt,
289 sizeof(client->crypto->hdr.salt), key, keylen,
290 (void **)&salted_key, &keysize);
291 if (!keyarg)
292 gcry_free (key);
294 if (!rc)
295 rc = decrypt_data (client->ctx, client->crypto, salted_key, keysize);
297 if (rc)
298 return rc;
300 cdata = xcalloc (1, sizeof (struct cache_data_s));
301 if (!cdata)
302 return GPG_ERR_ENOMEM;
304 cdata->key = salted_key;
305 cdata->keylen = keysize;
307 else
308 rc = decrypt_data (client->ctx, client->crypto, NULL, 0);
310 done:
311 if (!rc)
313 rc = parse_xml (ctx, client->flags & FLAG_NEW);
314 if (!rc)
316 int timeout = config_get_integer (client->filename,
317 "cache_timeout");
319 if (!cached)
321 if (!cdata)
322 cdata = xcalloc (1, sizeof (struct cache_data_s));
324 rc = encrypt_xml (NULL, cache_key, cache_keysize,
325 GCRY_CIPHER_AES, client->crypto->plaintext,
326 client->crypto->plaintext_len, &cdata->doc,
327 &cdata->doclen, &cache_iv, &cache_blocksize,
329 if (!rc && !(client->flags & FLAG_NEW) && IS_PKCS (client->crypto))
331 rc = gcry_sexp_build ((gcry_sexp_t *) & cdata->pubkey, NULL,
332 "%S", client->crypto->pkey_sexp);
336 if (cdata->sigkey)
337 gcry_sexp_release (cdata->sigkey);
339 cdata->sigkey = NULL;
341 if (!rc && IS_PKCS (client->crypto))
342 rc = gcry_sexp_build ((gcry_sexp_t *) &cdata->sigkey, NULL,
343 "%S", client->crypto->sigpkey_sexp);
345 if (!rc
346 && !cache_add_file (client->md5file, (client->flags & FLAG_NEW)
347 ? NULL : client->crypto->grip, cdata, timeout))
349 if (!cached)
351 free_cache_data_once (cdata);
352 xmlFreeDoc (client->doc);
353 client->doc = NULL;
355 rc = GPG_ERR_ENOMEM;
358 if (!rc)
359 client->flags |= FLAG_OPEN;
363 if (!rc)
364 update_checksum (client);
366 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
367 rc = do_lock (client, 0);
369 return rc;
372 static void
373 req_cleanup (void *arg)
375 if (!arg)
376 return;
378 strv_free ((char **) arg);
381 static gpg_error_t
382 parse_open_opt_lock (void *data, void *value)
384 struct client_s *client = data;
386 client->opts |= OPT_LOCK_ON_OPEN;
387 return 0;
390 static gpg_error_t
391 parse_opt_inquire (void *data, void *value)
393 struct client_s *client = data;
395 (void) value;
396 client->opts |= OPT_INQUIRE;
397 return 0;
400 static gpg_error_t
401 update_checksum (struct client_s *client)
403 unsigned char *crc;
404 size_t len;
405 struct cache_data_s *cdata;
406 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
408 if (rc)
409 return rc;
411 xfree (client->crc);
412 client->crc = crc;
413 cdata = cache_get_data (client->md5file);
414 if (cdata)
416 xfree (cdata->crc);
417 cdata->crc = xmalloc (len);
418 memcpy (cdata->crc, crc, len);
421 return 0;
424 static gpg_error_t
425 validate_checksum (struct client_s *client, struct cache_data_s *cdata)
427 unsigned char *crc;
428 size_t len;
429 gpg_error_t rc;
430 int n = 0;
432 if (cdata && !cdata->crc)
433 return GPG_ERR_CHECKSUM;
435 rc = get_checksum (client->filename, &crc, &len);
436 if (rc)
437 return rc;
439 if (cdata)
440 n = memcmp (cdata->crc, crc, len);
441 else if (client->crc)
442 n = memcmp (client->crc, crc, len);
444 xfree (crc);
445 return n ? GPG_ERR_CHECKSUM : 0;
448 static gpg_error_t
449 do_open (assuan_context_t ctx, const char *filename, const char *password)
451 struct client_s *client = assuan_get_pointer (ctx);
452 struct cache_data_s *cdata;
453 gpg_error_t rc = 0;
454 int done = 0;
456 if (!valid_filename (filename))
457 return GPG_ERR_INV_VALUE;
459 gcry_md_hash_buffer (GCRY_MD_MD5, client->md5file, filename,
460 strlen (filename));
461 client->filename = str_dup (filename);
462 if (!client->filename)
463 return GPG_ERR_ENOMEM;
465 // Cached document?
466 cdata = cache_get_data (client->md5file);
467 if (cdata && cdata->doc)
469 int defer;
471 rc = validate_checksum (client, cdata);
472 /* This will check that the key is cached in the agent which needs to
473 * be determined for files that share a keygrip. */
474 if (!rc)
476 rc = cache_iscached (client->filename, &defer);
477 if (!rc && defer)
478 rc = GPG_ERR_KEY_EXPIRED;
481 #ifdef WITH_GNUTLS
482 if (!rc && client->thd->remote
483 && config_get_boolean (NULL, "tcp_require_key"))
484 rc = GPG_ERR_KEY_EXPIRED;
485 #endif
487 if (!rc && !password)
489 rc = read_data_header (client->filename, &client->crypto->hdr,
490 NULL, NULL);
491 if (!rc)
493 if (IS_PKCS (client->crypto))
495 gcry_sexp_build (&client->crypto->pkey_sexp, NULL, "%S",
496 cdata->pubkey);
497 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
498 client->crypto->grip);
499 gcry_sexp_build (&client->crypto->sigpkey_sexp, NULL, "%S",
500 cdata->sigkey);
501 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
502 client->crypto->sign_grip);
505 if (!rc)
507 rc = open_finalize (ctx, NULL, 0);
508 done = 1;
513 /* There was an error accessing the file so clear the cache entry. The
514 * real error will be returned from read_data_file() since the file
515 * may have only disappeared. */
516 if (!done)
518 log_write ("%s: %s", filename, pwmd_strerror (rc));
519 rc = cache_clear (client->md5file);
520 send_status_all (STATUS_CACHE, NULL);
524 if (done || rc)
525 return rc;
527 rc = read_data_file (client->filename, client->crypto);
528 if (rc)
530 if (gpg_err_source (rc) != GPG_ERR_SOURCE_DEFAULT ||
531 gpg_err_code (rc) != GPG_ERR_ENOENT)
533 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
534 return rc;
537 client->flags |= FLAG_NEW;
538 memset (client->crypto->grip, 0, sizeof (client->crypto->grip));
539 memset (client->crypto->sign_grip, 0,
540 sizeof (client->crypto->sign_grip));
541 rc = open_finalize (ctx, NULL, 0);
542 return rc;
545 if (password && IS_PKCS (client->crypto))
547 #ifdef WITH_AGENT
548 rc = set_agent_passphrase (client->crypto, password, strlen (password));
549 if (rc)
550 return rc;
551 #endif
554 rc = open_finalize (ctx, (char *)password, password ? strlen (password) : 0);
555 return rc;
558 static gpg_error_t
559 open_command (assuan_context_t ctx, char *line)
561 gpg_error_t rc;
562 struct client_s *client = assuan_get_pointer (ctx);
563 char **req, *password = NULL, *filename;
564 unsigned char md5file[16];
565 int same_file = 0;
566 struct argv_s *args[] = {
567 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
568 NULL
571 rc = parse_options (&line, args, client);
572 if (rc)
573 return send_error (ctx, rc);
575 req = str_split (line, " ", 2);
576 if (!req)
577 return send_error (ctx, GPG_ERR_SYNTAX);
579 #ifdef WITH_GNUTLS
580 if (client->thd->remote)
582 rc = tls_validate_access (client, req[0]);
583 if (rc)
585 if (rc == GPG_ERR_INV_USER_ID)
586 log_write (_("client validation failed for file '%s'"), req[0]);
587 strv_free (req);
588 return send_error (ctx, rc);
591 /* Remote pinentries are not supported. */
592 client->flags |= FLAG_NO_PINENTRY;
594 #endif
596 pthread_cleanup_push (req_cleanup, req);
597 filename = req[0];
598 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, filename, strlen (filename));
599 /* This client may have locked a different file with ISCACHED --lock than
600 * the current filename. This will remove that lock. */
601 same_file = !memcmp (md5file, client->md5file, 16) ? 1 : 0;
602 if (client->flags & FLAG_OPEN ||
603 (client->flags & FLAG_HAS_LOCK && !same_file))
605 typeof (client->opts) opts = client->opts;
606 typeof (client->flags) flags = client->flags;
608 if (same_file)
609 client->flags |= FLAG_KEEP_LOCK;
611 cleanup_client (client);
612 client->opts = opts;
613 client->flags |= flags;
614 client->flags &= ~(FLAG_LOCK_CMD | FLAG_NEW);
615 if (!same_file)
616 client->flags &= ~(FLAG_HAS_LOCK);
619 /* Need to lock the mutex here because file_modified() cannot without
620 * knowing the filename. */
621 memcpy (client->md5file, md5file, 16);
622 rc = lock_file_mutex (client, 1);
623 if (!rc)
625 password = req[1] && *req[1] ? req[1] : NULL;
626 #ifdef WITH_AGENT
627 if (IS_PKCS (client->crypto))
628 rc = set_pinentry_mode (client->crypto->agent,
629 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
630 : "ask");
631 #endif
632 if (!rc)
634 rc = do_open (ctx, filename, password);
635 if (rc)
636 cleanup_client (client);
638 cleanup_crypto_stage1 (client->crypto);
642 pthread_cleanup_pop (1);
644 if (!rc && client->flags & FLAG_NEW)
645 rc = send_status (ctx, STATUS_NEWFILE, NULL);
647 #ifdef WITH_AGENT
648 (void) kill_scd (client->crypto->agent);
649 #endif
650 return send_error (ctx, rc);
653 static gpg_error_t
654 parse_save_opt_no_passphrase (void *data, void *value)
656 struct client_s *client = data;
658 (void) value;
659 client->opts |= OPT_NO_PASSPHRASE;
660 return 0;
663 static gpg_error_t
664 parse_save_opt_no_agent (void *data, void *value)
666 struct client_s *client = data;
668 client->opts |= OPT_NO_AGENT;
669 return 0;
672 static gpg_error_t
673 parse_save_opt_cipher (void *data, void *value)
675 struct client_s *client = data;
676 int algo = cipher_string_to_gcrypt ((char *) value);
677 file_header_t *hdr = &client->crypto->save.hdr;
679 if (algo == -1)
680 return GPG_ERR_INV_VALUE;
682 hdr->flags = set_cipher_flag (hdr->flags, algo);
683 return 0;
686 static gpg_error_t
687 parse_save_opt_keygrip (void *data, void *value)
689 struct client_s *client = data;
691 if (!IS_PKCS (client->crypto))
692 return GPG_ERR_INV_ARG;
694 #ifdef WITH_AGENT
695 if (client->crypto->save.pkey)
696 gcry_sexp_release (client->crypto->save.pkey);
698 client->crypto->save.pkey = NULL;
699 return get_pubkey (client->crypto, value, &client->crypto->save.pkey);
700 #else
701 return GPG_ERR_INV_ARG;
702 #endif
705 static gpg_error_t
706 parse_save_opt_sign_keygrip (void *data, void *value)
708 struct client_s *client = data;
710 if (!IS_PKCS (client->crypto))
711 return GPG_ERR_INV_ARG;
713 #ifdef WITH_AGENT
714 if (client->crypto->save.sigpkey)
715 gcry_sexp_release (client->crypto->save.sigpkey);
717 client->crypto->save.sigpkey = NULL;
718 return get_pubkey (client->crypto, value, &client->crypto->save.sigpkey);
719 #else
720 return GPG_ERR_INV_ARG;
721 #endif
724 static gpg_error_t
725 parse_opt_s2k_count (void *data, void *value)
727 struct client_s *client = data;
728 char *v = value;
730 if (!v || !*v)
731 return GPG_ERR_INV_VALUE;
733 client->crypto->save.s2k_count = strtoul (v, NULL, 10);
734 return 0;
737 static gpg_error_t
738 parse_save_opt_iterations (void *data, void *value)
740 struct client_s *client = data;
741 char *v = value, *p;
742 uint64_t n;
744 if (!v || !*v)
745 return GPG_ERR_INV_VALUE;
747 errno = 0;
748 n = strtoull (v, &p, 10);
749 if (n == UINT64_MAX && errno)
750 return gpg_error_from_syserror ();
751 else if (p && *p)
752 return GPG_ERR_INV_VALUE;
754 client->crypto->save.hdr.iterations = n;
755 return 0;
758 static gpg_error_t
759 save_finalize (assuan_context_t ctx)
761 struct client_s *client = assuan_get_pointer (ctx);
762 gpg_error_t rc = 0;
763 xmlChar *xmlbuf = NULL;
764 int xmlbuflen;
765 void *key = NULL;
766 size_t keylen = 0;
768 xmlDocDumpFormatMemory (client->doc, &xmlbuf, &xmlbuflen, 0);
769 if (!xmlbuf)
770 return GPG_ERR_ENOMEM;
772 pthread_cleanup_push (xmlFree, xmlbuf);
774 if (!use_agent || ((client->flags & FLAG_NEW)
775 && (client->opts & OPT_NO_AGENT))
776 || !(client->crypto->hdr.flags & PWMD_FLAG_PKCS))
778 rc = export_common (ctx, client->flags & FLAG_NO_PINENTRY,
779 client->crypto, xmlbuf, xmlbuflen, client->filename,
780 NULL, &key, &keylen, 1, 1);
782 #ifdef WITH_AGENT
783 else
785 gcry_sexp_t pubkey = client->crypto->save.pkey;
786 gcry_sexp_t sigkey = client->crypto->save.sigpkey;
788 if (!pubkey)
789 pubkey = client->crypto->pkey_sexp;
791 if (!sigkey)
793 sigkey = client->crypto->sigpkey_sexp;
794 if (!sigkey)
796 gcry_sexp_build (&client->crypto->save.sigpkey, 0, "%S", pubkey);
797 sigkey = client->crypto->save.sigpkey;
801 rc = encrypt_data_file (client->ctx, client->crypto, pubkey, sigkey,
802 client->filename, xmlbuf, xmlbuflen);
803 if (pubkey == client->crypto->save.pkey)
805 if (!rc)
807 gcry_sexp_release (client->crypto->pkey_sexp);
808 client->crypto->pkey_sexp = client->crypto->save.pkey;
809 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
810 client->crypto->grip);
812 else
813 gcry_sexp_release (pubkey);
815 client->crypto->save.pkey = NULL;
818 if (!rc)
819 gcry_pk_get_keygrip (sigkey, client->crypto->sign_grip);
821 if (sigkey == client->crypto->save.sigpkey)
823 if (!rc)
825 if (client->crypto->sigpkey_sexp)
826 gcry_sexp_release (client->crypto->sigpkey_sexp);
828 client->crypto->sigpkey_sexp = client->crypto->save.sigpkey;
829 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
830 client->crypto->sign_grip);
832 else
833 gcry_sexp_release (sigkey);
835 client->crypto->save.sigpkey = NULL;
838 #endif
840 if (!rc)
842 int cached;
844 rc = save_common (client->filename, client->crypto, xmlbuf, xmlbuflen,
845 key, keylen, &cached, client->opts & OPT_NO_AGENT);
846 if (rc)
847 gcry_free (key);
849 if (!rc && (!cached || (client->flags & FLAG_NEW)))
850 send_status_all (STATUS_CACHE, NULL);
852 if (!rc)
854 update_checksum (client);
855 client->flags &= ~(FLAG_NEW);
857 else
858 rc = GPG_ERR_ENOMEM;
861 pthread_cleanup_pop (1); // xmlFree
862 return rc;
865 static gpg_error_t
866 parse_opt_reset (void *data, void *value)
868 struct client_s *client = data;
870 (void) value;
871 client->opts |= OPT_RESET;
872 return 0;
875 static gpg_error_t
876 save_command (assuan_context_t ctx, char *line)
878 struct client_s *client = assuan_get_pointer (ctx);
879 gpg_error_t rc;
880 struct stat st;
881 struct argv_s *args[] = {
882 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG,
883 parse_save_opt_no_passphrase},
884 &(struct argv_s) {"cipher", OPTION_TYPE_ARG, parse_save_opt_cipher},
885 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
886 parse_opt_inquire},
887 &(struct argv_s) {"keygrip", OPTION_TYPE_ARG, parse_save_opt_keygrip},
888 &(struct argv_s) {"sign-keygrip", OPTION_TYPE_ARG,
889 parse_save_opt_sign_keygrip},
890 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
891 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
892 &(struct argv_s) {"cipher-iterations", OPTION_TYPE_ARG,
893 parse_save_opt_iterations},
894 &(struct argv_s) {"no-agent", OPTION_TYPE_NOARG,
895 parse_save_opt_no_agent},
896 NULL
899 cleanup_save (&client->crypto->save);
900 memcpy (&client->crypto->save.hdr, &client->crypto->hdr,
901 sizeof (file_header_t));
902 client->crypto->save.s2k_count =
903 config_get_ulong (client->filename, "s2k_count");
905 if (client->flags & FLAG_NEW)
906 client->crypto->save.hdr.iterations =
907 config_get_ulonglong (client->filename, "cipher_iterations");
909 rc = parse_options (&line, args, client);
910 if (rc)
911 return send_error (ctx, rc);
913 if (!(client->flags & FLAG_NEW))
914 client->opts &= ~OPT_NO_AGENT;
915 else if (client->opts & OPT_NO_AGENT)
917 client->crypto->save.hdr.flags &= ~PWMD_FLAG_PKCS;
918 client->crypto->hdr.flags &= ~PWMD_FLAG_PKCS;
921 if ((client->opts & OPT_NO_PASSPHRASE) && !(client->flags & FLAG_NEW)
922 && !(client->opts & OPT_INQUIRE))
923 return send_error (ctx, GPG_ERR_WRONG_KEY_USAGE);
925 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
926 return send_error (ctx, gpg_error_from_syserror ());
928 if (errno != ENOENT && !S_ISREG (st.st_mode))
930 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
931 return send_error (ctx, GPG_ERR_ENOANO);
934 int defer;
935 rc = cache_iscached (client->filename, &defer);
936 if (!rc && defer)
938 log_write ("%s: %s", client->filename,
939 pwmd_strerror (GPG_ERR_KEY_EXPIRED));
940 client->opts |= OPT_RESET;
943 if (client->opts & OPT_RESET)
945 rc = cache_clear (client->md5file);
946 if (rc)
947 return send_error (ctx, rc);
949 send_status_all (STATUS_CACHE, NULL);
952 rc = update_element_mtime (xmlDocGetRootElement (client->doc));
953 if (rc)
954 return send_error (ctx, rc);
956 #ifdef WITH_AGENT
957 if (!rc && use_agent && !client->crypto->save.pkey &&
958 (client->flags & FLAG_NEW || client->opts & OPT_INQUIRE)
959 && !(client->opts & OPT_NO_AGENT))
961 rc = send_status (client->ctx, STATUS_GENKEY, NULL);
962 if (!rc)
964 struct inquire_data_s idata = { 0 };
965 char *params = client->opts & OPT_INQUIRE ? NULL
966 : default_key_params (client->crypto);
968 pthread_cleanup_push (xfree, params);
969 idata.crypto = client->crypto;
971 if (params)
973 idata.line = params;
974 idata.len = strlen (params);
976 else
977 idata.preset = 1;
979 client->crypto->agent->inquire_data = &idata;
980 client->crypto->agent->inquire_cb = NULL;
981 rc = generate_key (client->crypto, params,
982 (client->opts & OPT_NO_PASSPHRASE), 1);
983 pthread_cleanup_pop (1);
986 #endif
988 if (!rc)
989 rc = save_finalize (ctx);
991 cleanup_crypto_stage1 (client->crypto);
992 #ifdef WITH_AGENT
993 (void) kill_scd (client->crypto->agent);
994 #endif
995 return send_error (ctx, rc);
998 static gpg_error_t
999 do_delete (assuan_context_t ctx, char *line)
1001 struct client_s *client = assuan_get_pointer (ctx);
1002 gpg_error_t rc;
1003 char **req;
1004 xmlNodePtr n;
1006 if (strchr (line, '\t'))
1007 req = str_split (line, "\t", 0);
1008 else
1009 req = str_split (line, " ", 0);
1011 if (!req || !*req)
1012 return GPG_ERR_SYNTAX;
1014 n = find_root_element (client->doc, &req, &rc, NULL, 0, 0);
1015 if (!n)
1017 strv_free (req);
1018 return rc;
1022 * No sub-node defined. Remove the entire node (root element).
1024 if (!req[1])
1026 if (n)
1028 rc = unlink_node (n);
1029 xmlFreeNode (n);
1032 strv_free (req);
1033 return rc;
1037 find_elements (client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1038 0, 0, NULL, 0);
1039 strv_free (req);
1040 if (!n)
1041 return rc;
1043 if (n)
1045 rc = unlink_node (n);
1046 xmlFreeNode (n);
1049 return rc;
1052 static gpg_error_t
1053 delete_command (assuan_context_t ctx, char *line)
1055 struct client_s *client = assuan_get_pointer (ctx);
1056 gpg_error_t rc;
1057 struct argv_s *args[] = {
1058 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1059 NULL
1062 rc = parse_options (&line, args, client);
1063 if (rc)
1064 return send_error (ctx, rc);
1066 if (client->opts & OPT_INQUIRE)
1068 unsigned char *result;
1069 size_t len;
1071 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1072 if (rc)
1073 return send_error (ctx, rc);
1075 line = (char *) result;
1078 rc = do_delete (ctx, line);
1080 if (client->opts & OPT_INQUIRE)
1081 xfree (line);
1083 return send_error (ctx, rc);
1086 static gpg_error_t
1087 store_command (assuan_context_t ctx, char *line)
1089 struct client_s *client = assuan_get_pointer (ctx);
1090 gpg_error_t rc;
1091 size_t len;
1092 unsigned char *result;
1093 char **req;
1094 xmlNodePtr n, parent;
1095 int has_content;
1096 char *content = NULL;
1098 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1099 if (rc)
1100 return send_error (ctx, rc);
1102 req = str_split ((char *) result, "\t", 0);
1103 xfree (result);
1105 if (!req || !*req)
1106 return send_error (ctx, GPG_ERR_SYNTAX);
1108 len = strv_length (req);
1109 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1110 if (*(req + 1) && !valid_element_path (req, has_content))
1112 strv_free (req);
1113 return send_error (ctx, GPG_ERR_INV_VALUE);
1116 if (has_content || !*req[len - 1])
1118 has_content = 1;
1119 content = req[len - 1];
1120 req[len - 1] = NULL;
1123 again:
1124 n = find_root_element (client->doc, &req, &rc, NULL, 0, 0);
1125 if (rc && rc == GPG_ERR_ELEMENT_NOT_FOUND)
1127 rc = new_root_element (client->doc, *req);
1128 if (rc)
1130 strv_free (req);
1131 return send_error (ctx, GPG_ERR_SYNTAX);
1134 goto again;
1137 if (!n)
1139 strv_free (req);
1140 return send_error (ctx, rc);
1143 parent = n;
1145 if (req[1] && *req[1])
1147 if (!n->children)
1148 parent = create_elements_cb (n, req + 1, &rc, NULL);
1149 else
1150 parent = find_elements (client->doc, n->children, req + 1, &rc,
1151 NULL, NULL, create_elements_cb, 0, 0, NULL,
1155 if (!rc && len > 1)
1157 n = find_text_node (parent->children);
1158 if (n)
1159 xmlNodeSetContent (n, (xmlChar *) content);
1160 else
1161 xmlNodeAddContent (parent, (xmlChar *) content);
1163 update_element_mtime (parent);
1166 xfree (content);
1167 strv_free (req);
1168 return send_error (ctx, rc);
1171 static gpg_error_t
1172 xfer_data (assuan_context_t ctx, const char *line, int total)
1174 int to_send;
1175 int sent = 0;
1176 gpg_error_t rc;
1177 int progress = config_get_integer ("global", "xfer_progress");
1178 int flush = 0;
1180 progress =
1181 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1182 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1183 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1185 if (rc)
1186 return rc;
1188 again:
1191 if (sent + to_send > total)
1192 to_send = total - sent;
1194 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1195 flush ? 0 : to_send);
1196 if (!rc)
1198 sent += flush ? 0 : to_send;
1200 if ((progress && !(sent % progress) && sent != total) ||
1201 (sent == total && flush))
1202 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1204 if (!flush && !rc && sent == total)
1206 flush = 1;
1207 goto again;
1211 while (!rc && sent < total);
1213 return rc;
1216 static gpg_error_t
1217 do_get (assuan_context_t ctx, char *line)
1219 struct client_s *client = assuan_get_pointer (ctx);
1220 gpg_error_t rc;
1221 char **req;
1222 xmlNodePtr n;
1224 req = str_split (line, "\t", 0);
1226 if (!req || !*req)
1228 strv_free (req);
1229 return GPG_ERR_SYNTAX;
1232 n = find_root_element (client->doc, &req, &rc, NULL, 0, 0);
1233 if (!n)
1235 strv_free (req);
1236 return rc;
1239 if (req[1])
1241 find_elements (client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1242 0, 0, NULL, 0);
1244 strv_free (req);
1245 if (rc)
1246 return rc;
1248 if (!n || !n->children)
1249 return GPG_ERR_NO_DATA;
1251 n = find_text_node (n->children);
1252 if (!n || !n->content || !*n->content)
1253 return GPG_ERR_NO_DATA;
1255 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1256 return rc;
1259 static gpg_error_t
1260 get_command (assuan_context_t ctx, char *line)
1262 struct client_s *client = assuan_get_pointer (ctx);
1263 gpg_error_t rc;
1264 struct argv_s *args[] = {
1265 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1266 NULL
1269 rc = parse_options (&line, args, client);
1270 if (rc)
1271 return send_error (ctx, rc);
1273 if (client->opts & OPT_INQUIRE)
1275 unsigned char *result;
1276 size_t len;
1278 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1279 if (rc)
1280 return send_error (ctx, rc);
1282 line = (char *) result;
1285 rc = do_get (ctx, line);
1287 if (client->opts & OPT_INQUIRE)
1288 xfree (line);
1290 return send_error (ctx, rc);
1293 static void list_command_cleanup1 (void *arg);
1294 static gpg_error_t
1295 realpath_command (assuan_context_t ctx, char *line)
1297 struct string_s *string = NULL;
1298 gpg_error_t rc;
1299 struct client_s *client = assuan_get_pointer (ctx);
1300 struct argv_s *args[] = {
1301 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1302 NULL
1305 rc = parse_options (&line, args, client);
1306 if (rc)
1307 return send_error (ctx, rc);
1309 if (client->opts & OPT_INQUIRE)
1311 unsigned char *result;
1312 size_t len;
1314 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1315 if (rc)
1316 return send_error (ctx, rc);
1318 line = (char *) result;
1321 rc = build_realpath (client->doc, line, &string);
1322 if (!rc)
1324 pthread_cleanup_push (list_command_cleanup1, string);
1325 rc = xfer_data (ctx, string->str, string->len);
1326 pthread_cleanup_pop (1);
1329 if (client->opts & OPT_INQUIRE)
1330 xfree (line);
1332 return send_error (ctx, rc);
1335 static void
1336 list_command_cleanup1 (void *arg)
1338 if (arg)
1339 string_free ((struct string_s *) arg, 1);
1342 static void
1343 list_command_cleanup2 (void *arg)
1345 struct element_list_s *elements = arg;
1347 if (elements)
1349 if (elements->list)
1351 int total = slist_length (elements->list);
1352 int i;
1354 for (i = 0; i < total; i++)
1356 char *tmp = slist_nth_data (elements->list, i);
1357 xfree (tmp);
1360 slist_free (elements->list);
1363 if (elements->prefix)
1364 xfree (elements->prefix);
1366 if (elements->req)
1367 strv_free (elements->req);
1369 xfree (elements);
1373 static gpg_error_t
1374 parse_list_opt_norecurse (void *data, void *value)
1376 struct client_s *client = data;
1378 client->opts &= ~(OPT_LIST_RECURSE);
1379 return 0;
1382 static gpg_error_t
1383 parse_list_opt_verbose (void *data, void *value)
1385 struct client_s *client = data;
1387 client->opts |= OPT_LIST_VERBOSE;
1388 return 0;
1391 static gpg_error_t
1392 parse_list_opt_target (void *data, void *value)
1394 struct client_s *client = data;
1396 client->opts |= OPT_LIST_WITH_TARGET;
1397 return 0;
1400 static gpg_error_t
1401 parse_list_opt_all (void *data, void *value)
1403 struct client_s *client = data;
1405 client->opts |= OPT_LIST_ALL;
1406 return 0;
1409 static gpg_error_t
1410 list_path_once (struct client_s *client, char *line,
1411 struct element_list_s *elements, struct string_s *result)
1413 gpg_error_t rc;
1415 elements->req = str_split (line, " ", 0);
1416 if (!elements->req)
1417 strv_printf (&elements->req, "%s", line);
1419 rc = create_path_list (client->doc, elements, *elements->req);
1420 if (rc == GPG_ERR_ELOOP && elements->verbose)
1421 rc = 0;
1423 if (!rc)
1425 if (elements)
1427 int total = slist_length (elements->list);
1428 int i;
1430 if (!total)
1431 rc = GPG_ERR_NO_DATA;
1433 if (!rc)
1435 if (!rc)
1437 for (i = 0; i < total; i++)
1439 char *tmp = slist_nth_data (elements->list, i);
1441 string_append_printf (result, "%s%s", tmp,
1442 i + 1 == total ? "" : "\n");
1447 else
1448 rc = GPG_ERR_NO_DATA;
1451 return rc;
1454 static int
1455 has_list_flag (char *path, char *flags)
1457 char *p = path;
1459 while (*p && *++p != ' ');
1461 if (!*p)
1462 return 0;
1464 for (; *p; p++)
1466 char *f;
1468 for (f = flags; *f && *f != ' '; f++)
1470 if (*p == *f)
1471 return 1;
1475 return 0;
1478 static gpg_error_t
1479 do_list (assuan_context_t ctx, char *line)
1481 struct client_s *client = assuan_get_pointer (ctx);
1482 gpg_error_t rc;
1483 struct element_list_s *elements = NULL;
1485 elements = xcalloc (1, sizeof (struct element_list_s));
1486 if (!elements)
1487 return GPG_ERR_ENOMEM;
1489 elements->recurse = client->opts & OPT_LIST_RECURSE;
1490 elements->verbose =
1491 (client->opts & OPT_LIST_VERBOSE) | (client->opts & OPT_LIST_WITH_TARGET);
1492 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1494 if (!line || !*line)
1496 struct string_s *str = NULL;
1498 pthread_cleanup_push (list_command_cleanup2, elements);
1499 rc = list_root_elements (client->doc, &str, elements->verbose,
1500 elements->with_target);
1501 pthread_cleanup_pop (1);
1502 pthread_cleanup_push (list_command_cleanup1, str);
1504 if (!rc)
1506 if (client->opts & OPT_LIST_ALL)
1508 char **roots = str_split (str->str, "\n", 0);
1509 char **p;
1511 pthread_cleanup_push (req_cleanup, roots);
1512 string_truncate (str, 0);
1514 for (p = roots; *p; p++)
1516 if (strchr (*p, ' '))
1518 if (has_list_flag (*p, "EO"))
1520 string_append_printf (str, "%s%s", *p,
1521 *(p + 1) ? "\n" : "");
1522 continue;
1526 elements = xcalloc (1, sizeof (struct element_list_s));
1527 if (!elements)
1529 rc = GPG_ERR_ENOMEM;
1530 break;
1533 elements->recurse = client->opts & OPT_LIST_RECURSE;
1534 elements->verbose =
1535 (client->opts & OPT_LIST_VERBOSE) | (client->opts &
1536 OPT_LIST_WITH_TARGET);
1537 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1538 pthread_cleanup_push (list_command_cleanup2, elements);
1539 rc = list_path_once (client, *p, elements, str);
1540 pthread_cleanup_pop (1);
1541 if (rc)
1542 break;
1544 if (*(p + 1))
1545 string_append (str, "\n");
1548 pthread_cleanup_pop (1);
1551 if (!rc)
1552 rc = xfer_data (ctx, str->str, str->len);
1555 pthread_cleanup_pop (1);
1556 return rc;
1559 pthread_cleanup_push (list_command_cleanup2, elements);
1560 struct string_s *str = string_new (NULL);
1561 pthread_cleanup_push (list_command_cleanup1, str);
1562 rc = list_path_once (client, line, elements, str);
1563 if (!rc)
1564 rc = xfer_data (ctx, str->str, str->len);
1566 pthread_cleanup_pop (1);
1567 pthread_cleanup_pop (1);
1568 return rc;
1571 static gpg_error_t
1572 list_command (assuan_context_t ctx, char *line)
1574 struct client_s *client = assuan_get_pointer (ctx);
1575 gpg_error_t rc;
1576 struct argv_s *args[] = {
1577 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG,
1578 parse_list_opt_norecurse},
1579 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_list_opt_verbose},
1580 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1581 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG,
1582 parse_list_opt_target},
1583 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_all},
1584 NULL
1587 if (disable_list_and_dump == 1)
1588 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1590 client->opts |= OPT_LIST_RECURSE;
1591 rc = parse_options (&line, args, client);
1592 if (rc)
1593 return send_error (ctx, rc);
1595 if (client->opts & OPT_LIST_ALL)
1596 client->opts |= OPT_LIST_RECURSE | OPT_LIST_VERBOSE;
1598 if (client->opts & OPT_INQUIRE)
1600 unsigned char *result;
1601 size_t len;
1603 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1604 if (rc)
1605 return send_error (ctx, rc);
1607 line = (char *) result;
1610 rc = do_list (ctx, line);
1612 if (client->opts & OPT_INQUIRE)
1613 xfree (line);
1615 return send_error (ctx, rc);
1619 * req[0] - element path
1621 static gpg_error_t
1622 attribute_list (assuan_context_t ctx, char **req)
1624 struct client_s *client = assuan_get_pointer (ctx);
1625 char **attrlist = NULL;
1626 int i = 0;
1627 char **path = NULL;
1628 xmlAttrPtr a;
1629 xmlNodePtr n, an;
1630 char *line;
1631 gpg_error_t rc;
1633 if (!req || !req[0])
1634 return GPG_ERR_SYNTAX;
1636 if ((path = str_split (req[0], "\t", 0)) == NULL)
1639 * The first argument may be only a root element.
1641 if ((path = str_split (req[0], " ", 0)) == NULL)
1642 return GPG_ERR_SYNTAX;
1645 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
1647 if (!n)
1649 strv_free (path);
1650 return rc;
1653 if (path[1])
1655 n = find_elements (client->doc, n->children, path + 1, &rc,
1656 NULL, NULL, NULL, 0, 0, NULL, 0);
1658 if (!n)
1660 strv_free (path);
1661 return rc;
1665 strv_free (path);
1667 for (a = n->properties; a; a = a->next)
1669 char **pa;
1671 if ((pa = xrealloc (attrlist, (i + 2) * sizeof (char *))) == NULL)
1673 if (attrlist)
1674 strv_free (attrlist);
1676 log_write ("%s(%i): %s", __FILE__, __LINE__,
1677 pwmd_strerror (GPG_ERR_ENOMEM));
1678 return GPG_ERR_ENOMEM;
1681 attrlist = pa;
1682 an = a->children;
1683 attrlist[i] = str_asprintf ("%s %s", (char *) a->name,
1685 && an->content ? (char *) an->content : "");
1687 if (!attrlist[i])
1689 strv_free (attrlist);
1690 log_write ("%s(%i): %s", __FILE__, __LINE__,
1691 pwmd_strerror (GPG_ERR_ENOMEM));
1692 return GPG_ERR_ENOMEM;
1695 attrlist[++i] = NULL;
1698 if (!attrlist)
1699 return GPG_ERR_NO_DATA;
1701 line = strv_join ("\n", attrlist);
1703 if (!line)
1705 log_write ("%s(%i): %s", __FILE__, __LINE__,
1706 pwmd_strerror (GPG_ERR_ENOMEM));
1707 strv_free (attrlist);
1708 return GPG_ERR_ENOMEM;
1711 pthread_cleanup_push (xfree, line);
1712 pthread_cleanup_push (req_cleanup, attrlist);
1713 rc = xfer_data (ctx, line, strlen (line));
1714 pthread_cleanup_pop (1);
1715 pthread_cleanup_pop (1);
1716 return rc;
1720 * req[0] - attribute
1721 * req[1] - element path
1723 static gpg_error_t
1724 attribute_delete (struct client_s *client, char **req)
1726 xmlNodePtr n;
1727 char **path = NULL;
1728 gpg_error_t rc;
1730 if (!req || !req[0] || !req[1])
1731 return GPG_ERR_SYNTAX;
1733 if (!strcmp (req[0], "_name"))
1734 return GPG_ERR_INV_ATTR;
1736 if ((path = str_split (req[1], "\t", 0)) == NULL)
1739 * The first argument may be only a root element.
1741 if ((path = str_split (req[1], " ", 0)) == NULL)
1742 return GPG_ERR_SYNTAX;
1745 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
1746 if (!n)
1747 goto fail;
1749 if (path[1])
1751 n = find_elements (client->doc, n->children, path + 1, &rc,
1752 NULL, NULL, NULL, 0, 0, NULL, 0);
1753 if (!n)
1754 goto fail;
1757 rc = delete_attribute (n, (xmlChar *) req[0]);
1759 fail:
1760 strv_free (path);
1761 return rc;
1764 static xmlNodePtr
1765 create_element_path (struct client_s *client,
1766 char ***elements, gpg_error_t * rc, xmlNodePtr parent)
1768 char **req = *elements;
1769 char **req_orig = strv_dup (req);
1770 xmlNodePtr n = NULL;
1772 *rc = 0;
1774 if (!req_orig)
1776 *rc = GPG_ERR_ENOMEM;
1777 log_write ("%s(%i): %s", __FILE__, __LINE__,
1778 pwmd_strerror (GPG_ERR_ENOMEM));
1779 goto fail;
1782 again:
1783 n = find_root_element (client->doc, &req, rc, NULL, 0, 0);
1784 if (!n)
1786 if (*rc != GPG_ERR_ELEMENT_NOT_FOUND)
1787 goto fail;
1789 *rc = new_root_element (client->doc, req[0]);
1790 if (*rc)
1791 goto fail;
1793 goto again;
1795 else if (n == parent)
1797 *rc = GPG_ERR_CONFLICT;
1798 goto fail;
1801 if (req[1])
1803 if (!n->children)
1804 n = create_target_elements_cb (n, req + 1, rc, NULL);
1805 else
1806 n = find_elements (client->doc, n->children, req + 1, rc, NULL, NULL,
1807 create_target_elements_cb, 0, 0, parent, 0);
1809 if (!n)
1810 goto fail;
1813 * Reset the position of the element tree now that the elements
1814 * have been created.
1816 strv_free (req);
1817 req = req_orig;
1818 req_orig = NULL;
1819 n = find_root_element (client->doc, &req, rc, NULL, 0, 0);
1820 if (!n)
1821 goto fail;
1823 n = find_elements (client->doc, n->children, req + 1, rc,
1824 NULL, NULL, NULL, 0, 0, NULL, 0);
1825 if (!n)
1826 goto fail;
1829 fail:
1830 if (req_orig)
1831 strv_free (req_orig);
1833 *elements = req;
1834 return n;
1838 * Creates a "target" attribute. When other commands encounter an element with
1839 * this attribute, the element path is modified to the target value. If the
1840 * source element path doesn't exist when using 'ATTR SET target', it is
1841 * created, but the destination element path must exist.
1843 * req[0] - source element path
1844 * req[1] - destination element path
1846 static gpg_error_t
1847 target_attribute (struct client_s *client, char **req)
1849 char **src, **dst, *line = NULL, **odst = NULL;
1850 gpg_error_t rc;
1851 xmlNodePtr n;
1853 if (!req || !req[0] || !req[1])
1854 return GPG_ERR_SYNTAX;
1856 if ((src = str_split (req[0], "\t", 0)) == NULL)
1859 * The first argument may be only a root element.
1861 if ((src = str_split (req[0], " ", 0)) == NULL)
1862 return GPG_ERR_SYNTAX;
1865 if (!valid_element_path (src, 0))
1866 return GPG_ERR_INV_VALUE;
1868 if ((dst = str_split (req[1], "\t", 0)) == NULL)
1871 * The first argument may be only a root element.
1873 if ((dst = str_split (req[1], " ", 0)) == NULL)
1875 rc = GPG_ERR_SYNTAX;
1876 goto fail;
1880 odst = strv_dup (dst);
1881 if (!odst)
1883 rc = GPG_ERR_ENOMEM;
1884 goto fail;
1887 n = find_root_element (client->doc, &dst, &rc, NULL, 0, 0);
1889 * Make sure the destination element path exists.
1891 if (!n)
1892 goto fail;
1894 if (dst[1])
1896 n = find_elements (client->doc, n->children, dst + 1, &rc,
1897 NULL, NULL, NULL, 0, 0, NULL, 0);
1898 if (!n)
1899 goto fail;
1902 rc = validate_target_attribute (client->doc, req[0], n);
1903 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
1904 goto fail;
1906 n = create_element_path (client, &src, &rc, NULL);
1907 if (rc)
1908 goto fail;
1910 line = strv_join ("\t", odst);
1911 if (!line)
1913 rc = GPG_ERR_ENOMEM;
1914 goto fail;
1917 rc = add_attribute (n, "target", line);
1919 fail:
1920 xfree (line);
1921 strv_free (src);
1922 strv_free (dst);
1923 strv_free (odst);
1924 return rc;
1928 * req[0] - attribute
1929 * req[1] - element path
1931 static gpg_error_t
1932 attribute_get (assuan_context_t ctx, char **req)
1934 struct client_s *client = assuan_get_pointer (ctx);
1935 xmlNodePtr n;
1936 xmlChar *a;
1937 char **path = NULL;
1938 gpg_error_t rc;
1940 if (!req || !req[0] || !req[1])
1941 return GPG_ERR_SYNTAX;
1943 if (strchr (req[1], '\t'))
1945 if ((path = str_split (req[1], "\t", 0)) == NULL)
1946 return GPG_ERR_SYNTAX;
1948 else
1950 if ((path = str_split (req[1], " ", 0)) == NULL)
1951 return GPG_ERR_SYNTAX;
1954 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
1956 if (!n)
1957 goto fail;
1959 if (path[1])
1961 n = find_elements (client->doc, n->children, path + 1, &rc,
1962 NULL, NULL, NULL, 0, 0, NULL, 0);
1964 if (!n)
1965 goto fail;
1968 strv_free (path);
1970 if ((a = xmlGetProp (n, (xmlChar *) req[0])) == NULL)
1971 return GPG_ERR_NOT_FOUND;
1973 pthread_cleanup_push (xmlFree, a);
1975 if (*a)
1976 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
1977 else
1978 rc = GPG_ERR_NO_DATA;
1980 pthread_cleanup_pop (1);
1981 return rc;
1983 fail:
1984 strv_free (path);
1985 return rc;
1989 * req[0] - attribute
1990 * req[1] - element path
1991 * req[2] - value
1993 static gpg_error_t
1994 attribute_set (struct client_s *client, char **req)
1996 char **path = NULL;
1997 gpg_error_t rc;
1998 xmlNodePtr n;
2000 if (!req || !req[0] || !req[1])
2001 return GPG_ERR_SYNTAX;
2004 * Reserved attribute names.
2006 if (!strcmp (req[0], "_name"))
2007 return GPG_ERR_INV_ATTR;
2008 else if (!strcmp (req[0], "target"))
2009 return target_attribute (client, req + 1);
2011 if ((path = str_split (req[1], "\t", 0)) == NULL)
2014 * The first argument may be only a root element.
2016 if ((path = str_split (req[1], " ", 0)) == NULL)
2017 return GPG_ERR_SYNTAX;
2020 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
2022 if (!n)
2023 goto fail;
2025 if (path[1])
2027 n = find_elements (client->doc, n->children, path + 1, &rc,
2028 NULL, NULL, NULL, 0, 0, NULL, 0);
2030 if (!n)
2031 goto fail;
2034 rc = add_attribute (n, req[0], req[2]);
2036 fail:
2037 strv_free (path);
2038 return rc;
2042 * req[0] - command
2043 * req[1] - attribute name or element path if command is LIST
2044 * req[2] - element path
2045 * req[2] - element path or value
2048 static gpg_error_t
2049 do_attr (assuan_context_t ctx, char *line)
2051 struct client_s *client = assuan_get_pointer (ctx);
2052 gpg_error_t rc = 0;
2053 char **req;
2055 req = str_split (line, " ", 4);
2056 if (!req || !req[0] || !req[1])
2058 strv_free (req);
2059 return GPG_ERR_SYNTAX;
2062 pthread_cleanup_push (req_cleanup, req);
2064 if (strcasecmp (req[0], "SET") == 0)
2065 rc = attribute_set (client, req + 1);
2066 else if (strcasecmp (req[0], "GET") == 0)
2067 rc = attribute_get (ctx, req + 1);
2068 else if (strcasecmp (req[0], "DELETE") == 0)
2069 rc = attribute_delete (client, req + 1);
2070 else if (strcasecmp (req[0], "LIST") == 0)
2071 rc = attribute_list (ctx, req + 1);
2072 else
2073 rc = GPG_ERR_SYNTAX;
2075 pthread_cleanup_pop (1);
2076 return rc;
2079 static gpg_error_t
2080 attr_command (assuan_context_t ctx, char *line)
2082 struct client_s *client = assuan_get_pointer (ctx);
2083 gpg_error_t rc;
2084 struct argv_s *args[] = {
2085 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2086 NULL
2089 rc = parse_options (&line, args, client);
2090 if (rc)
2091 return send_error (ctx, rc);
2093 if (client->opts & OPT_INQUIRE)
2095 unsigned char *result;
2096 size_t len;
2098 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2099 if (rc)
2100 return send_error (ctx, rc);
2102 line = (char *) result;
2105 rc = do_attr (ctx, line);
2107 if (client->opts & OPT_INQUIRE)
2108 xfree (line);
2110 return send_error (ctx, rc);
2113 static gpg_error_t
2114 parse_iscached_opt_lock (void *data, void *value)
2116 struct client_s *client = data;
2118 (void) value;
2119 client->opts |= OPT_LOCK;
2120 return 0;
2123 static gpg_error_t
2124 iscached_command (assuan_context_t ctx, char *line)
2126 struct client_s *client = assuan_get_pointer (ctx);
2127 gpg_error_t rc;
2128 unsigned char md5file[16];
2129 struct argv_s *args[] = {
2130 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2131 NULL
2134 if (!line || !*line)
2135 return send_error (ctx, GPG_ERR_SYNTAX);
2137 rc = parse_options (&line, args, client);
2138 if (rc)
2139 return send_error (ctx, rc);
2140 else if (!valid_filename (line))
2141 return send_error (ctx, GPG_ERR_INV_VALUE);
2143 rc = cache_iscached (line, NULL);
2144 if (client->opts & OPT_LOCK
2145 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2147 gpg_error_t trc = rc;
2148 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2149 if (memcmp (md5file, client->md5file, 16))
2150 cleanup_client (client);
2152 memcpy (client->md5file, md5file, 16);
2153 rc = do_lock (client, 1);
2154 if (!rc)
2155 rc = trc;
2158 return send_error (ctx, rc);
2161 static gpg_error_t
2162 clearcache_command (assuan_context_t ctx, char *line)
2164 gpg_error_t rc = 0, all_rc = 0;
2165 unsigned char md5file[16];
2166 int i;
2167 int t;
2168 int all = 0;
2169 struct client_thread_s *once = NULL;
2171 cache_lock ();
2172 MUTEX_LOCK (&cn_mutex);
2173 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
2175 if (!line || !*line)
2176 all = 1;
2178 t = slist_length (cn_thread_list);
2180 for (i = 0; i < t; i++)
2182 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2184 if (!thd->cl)
2185 continue;
2187 /* Lock each connected clients' file mutex to prevent any other client
2188 * from accessing the cache entry (the file mutex is locked upon
2189 * command startup). The cache for the entry is not cleared if the
2190 * file mutex is locked by another client to prevent this function
2191 * from blocking.
2193 if (all)
2195 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
2196 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2198 if (pthread_equal (pthread_self (), thd->tid))
2199 rc = 0;
2200 else
2202 if (!thd->cl->filename ||
2203 cache_iscached (thd->cl->filename,
2204 NULL) == GPG_ERR_NO_DATA)
2206 rc = 0;
2207 continue;
2210 cache_defer_clear (thd->cl->md5file);
2213 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2215 rc = 0;
2216 continue;
2219 if (!rc)
2221 rc = cache_clear (thd->cl->md5file);
2222 if (rc)
2223 all_rc = rc;
2225 cache_unlock_mutex (thd->cl->md5file, 0);
2227 else
2228 all_rc = rc;
2230 rc = 0;
2232 /* A single data filename was specified. Lock only this data file
2233 * mutex and free the cache entry. */
2234 else
2236 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2238 if (!memcmp (thd->cl->md5file, md5file, sizeof (md5file)))
2240 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0,
2241 -1);
2242 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2244 if (pthread_equal (pthread_self (), thd->tid))
2245 rc = 0;
2248 if (!rc)
2250 once = thd;
2251 rc = cache_clear (thd->cl->md5file);
2252 cache_unlock_mutex (thd->cl->md5file, 0);
2254 else
2256 cache_defer_clear (thd->cl->md5file);
2259 break;
2264 /* Only connected clients' cache entries have been cleared. Now clear any
2265 * remaining cache entries without clients but only if there wasn't an
2266 * error from above since this would defeat the locking check of the
2267 * remaining entries. */
2268 if (!all_rc && all)
2270 cache_clear (NULL);
2273 /* No clients are using the specified file. */
2274 else if (!all_rc && !rc && !once)
2276 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2277 rc = cache_clear (md5file);
2280 /* Release the connection mutex. */
2281 pthread_cleanup_pop (1);
2282 cache_unlock ();
2284 if (!rc)
2285 send_status_all (STATUS_CACHE, NULL);
2287 /* One or more files were locked while clearing all cache entries. */
2288 if (all_rc)
2289 rc = all_rc;
2291 return send_error (ctx, rc);
2294 static gpg_error_t
2295 cachetimeout_command (assuan_context_t ctx, char *line)
2297 unsigned char md5file[16];
2298 int timeout;
2299 char **req = str_split (line, " ", 0);
2300 char *p;
2301 gpg_error_t rc = 0;
2303 if (!req || !*req || !req[1])
2305 strv_free (req);
2306 return send_error (ctx, GPG_ERR_SYNTAX);
2309 errno = 0;
2310 timeout = (int) strtol (req[1], &p, 10);
2311 if (errno != 0 || *p || timeout < -1)
2313 strv_free (req);
2314 return send_error (ctx, GPG_ERR_SYNTAX);
2317 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, req[0], strlen (req[0]));
2318 rc = cache_set_timeout (md5file, timeout);
2319 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2321 rc = 0;
2322 MUTEX_LOCK (&rcfile_mutex);
2323 config_set_int_param (&global_config, req[0], "cache_timeout", req[1]);
2324 MUTEX_UNLOCK (&rcfile_mutex);
2327 strv_free (req);
2328 return send_error (ctx, rc);
2331 static gpg_error_t
2332 dump_command (assuan_context_t ctx, char *line)
2334 xmlChar *xml;
2335 int len;
2336 struct client_s *client = assuan_get_pointer (ctx);
2337 gpg_error_t rc;
2339 if (disable_list_and_dump == 1)
2340 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2342 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2344 if (!xml)
2346 log_write ("%s(%i): %s", __FILE__, __LINE__,
2347 pwmd_strerror (GPG_ERR_ENOMEM));
2348 return send_error (ctx, GPG_ERR_ENOMEM);
2351 pthread_cleanup_push (xmlFree, xml);
2352 rc = xfer_data (ctx, (char *) xml, len);
2353 pthread_cleanup_pop (1);
2354 return send_error (ctx, rc);
2357 static gpg_error_t
2358 getconfig_command (assuan_context_t ctx, char *line)
2360 struct client_s *client = assuan_get_pointer (ctx);
2361 gpg_error_t rc = 0;
2362 char filename[255] = { 0 }, param[747] =
2365 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2367 if (!line || !*line)
2368 return send_error (ctx, GPG_ERR_SYNTAX);
2370 if (strchr (line, ' '))
2372 sscanf (line, " %254[^ ] %746c", filename, param);
2373 paramp = param;
2374 fp = filename;
2377 if (fp && !valid_filename (fp))
2378 return send_error (ctx, GPG_ERR_INV_VALUE);
2380 paramp = str_down (paramp);
2381 if (!strcmp (paramp, "cipher") && fp)
2383 struct crypto_s *crypto;
2385 rc = init_client_crypto (&crypto);
2386 if (!rc)
2388 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2389 if (!rc)
2391 const char *t =
2392 gcry_cipher_algo_name (cipher_to_gcrypt (crypto->hdr.flags));
2393 if (t)
2395 tmp = str_dup (t);
2396 if (tmp)
2397 str_down (tmp);
2402 cleanup_crypto (&crypto);
2403 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2404 return send_error (ctx, rc);
2406 if (!rc && tmp)
2407 goto done;
2409 else if (!strcmp (paramp, "cipher_iterations") && fp)
2411 struct crypto_s *crypto;
2413 rc = init_client_crypto (&crypto);
2414 if (!rc)
2416 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2417 if (!rc)
2419 tmp = str_asprintf ("%llu",
2420 (unsigned long long) crypto->hdr.
2421 iterations);
2422 if (!tmp)
2423 rc = GPG_ERR_ENOMEM;
2427 cleanup_crypto (&crypto);
2428 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2429 return send_error (ctx, rc);
2431 if (!rc && tmp)
2432 goto done;
2434 else if (!strcmp (paramp, "passphrase"))
2435 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2437 p = config_get_value (fp ? fp : "global", paramp);
2438 if (!p)
2439 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2441 tmp = expand_homedir (p);
2442 xfree (p);
2443 if (!tmp)
2445 log_write ("%s(%i): %s", __FILE__, __LINE__,
2446 pwmd_strerror (GPG_ERR_ENOMEM));
2447 return send_error (ctx, GPG_ERR_ENOMEM);
2450 done:
2451 p = tmp;
2452 pthread_cleanup_push (xfree, p);
2453 rc = xfer_data (ctx, p, strlen (p));
2454 pthread_cleanup_pop (1);
2455 return send_error (ctx, rc);
2458 struct xpath_s
2460 xmlXPathContextPtr xp;
2461 xmlXPathObjectPtr result;
2462 xmlBufferPtr buf;
2463 char **req;
2466 static void
2467 xpath_command_cleanup (void *arg)
2469 struct xpath_s *xpath = arg;
2471 if (!xpath)
2472 return;
2474 req_cleanup (xpath->req);
2476 if (xpath->buf)
2477 xmlBufferFree (xpath->buf);
2479 if (xpath->result)
2480 xmlXPathFreeObject (xpath->result);
2482 if (xpath->xp)
2483 xmlXPathFreeContext (xpath->xp);
2486 static gpg_error_t
2487 do_xpath (assuan_context_t ctx, char *line)
2489 gpg_error_t rc;
2490 struct client_s *client = assuan_get_pointer (ctx);
2491 struct xpath_s _x = { 0 };
2492 struct xpath_s *xpath = &_x;
2494 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2496 if (strv_printf (&xpath->req, "%s", line) == 0)
2497 return GPG_ERR_ENOMEM;
2500 xpath->xp = xmlXPathNewContext (client->doc);
2501 if (!xpath->xp)
2503 rc = GPG_ERR_BAD_DATA;
2504 goto fail;
2507 xpath->result =
2508 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2509 if (!xpath->result)
2511 rc = GPG_ERR_BAD_DATA;
2512 goto fail;
2515 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2517 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2518 goto fail;
2521 rc = recurse_xpath_nodeset (client->doc, xpath->result->nodesetval,
2522 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2523 NULL);
2524 if (rc)
2525 goto fail;
2526 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2528 rc = GPG_ERR_NO_DATA;
2529 goto fail;
2531 else if (xpath->req[1])
2533 rc = 0;
2534 goto fail;
2537 pthread_cleanup_push (xpath_command_cleanup, &xpath);
2538 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2539 xmlBufferLength (xpath->buf));
2540 pthread_cleanup_pop (0);
2541 fail:
2542 xpath_command_cleanup (xpath);
2543 return rc;
2546 static gpg_error_t
2547 xpath_command (assuan_context_t ctx, char *line)
2549 struct client_s *client = assuan_get_pointer (ctx);
2550 gpg_error_t rc;
2551 struct argv_s *args[] = {
2552 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2553 NULL
2556 if (disable_list_and_dump == 1)
2557 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2559 rc = parse_options (&line, args, client);
2560 if (rc)
2561 return send_error (ctx, rc);
2563 if (client->opts & OPT_INQUIRE)
2565 unsigned char *result;
2566 size_t len;
2568 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2569 if (rc)
2570 return send_error (ctx, rc);
2572 line = (char *) result;
2575 if (!line || !*line)
2576 rc = GPG_ERR_SYNTAX;
2578 if (!rc)
2579 rc = do_xpath (ctx, line);
2581 if (client->opts & OPT_INQUIRE)
2582 xfree (line);
2584 return send_error (ctx, rc);
2587 static gpg_error_t
2588 do_xpathattr (assuan_context_t ctx, char *line)
2590 struct client_s *client = assuan_get_pointer (ctx);
2591 gpg_error_t rc;
2592 char **req = NULL;
2593 int cmd = 0; //SET
2594 struct xpath_s _x = { 0 };
2595 struct xpath_s *xpath = &_x;
2597 if ((req = str_split (line, " ", 3)) == NULL)
2598 return GPG_ERR_ENOMEM;
2600 if (!req[0])
2602 rc = GPG_ERR_SYNTAX;
2603 goto fail;
2606 if (!strcasecmp (req[0], "SET"))
2607 cmd = 0;
2608 else if (!strcasecmp (req[0], "DELETE"))
2609 cmd = 1;
2610 else
2612 rc = GPG_ERR_SYNTAX;
2613 goto fail;
2616 if (!req[1] || !req[2])
2618 rc = GPG_ERR_SYNTAX;
2619 goto fail;
2622 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2624 rc = GPG_ERR_ENOMEM;
2625 goto fail;
2628 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2630 rc = GPG_ERR_SYNTAX;
2631 goto fail;
2634 xpath->xp = xmlXPathNewContext (client->doc);
2635 if (!xpath->xp)
2637 rc = GPG_ERR_BAD_DATA;
2638 goto fail;
2641 xpath->result =
2642 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2643 if (!xpath->result)
2645 rc = GPG_ERR_BAD_DATA;
2646 goto fail;
2649 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2651 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2652 goto fail;
2655 rc = recurse_xpath_nodeset (client->doc, xpath->result->nodesetval,
2656 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2657 (xmlChar *) req[1]);
2659 fail:
2660 xpath_command_cleanup (xpath);
2661 strv_free (req);
2662 return rc;
2665 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2666 static gpg_error_t
2667 xpathattr_command (assuan_context_t ctx, char *line)
2669 struct client_s *client = assuan_get_pointer (ctx);
2670 gpg_error_t rc;
2671 struct argv_s *args[] = {
2672 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2673 NULL
2676 if (disable_list_and_dump == 1)
2677 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2679 rc = parse_options (&line, args, client);
2680 if (rc)
2681 return send_error (ctx, rc);
2683 if (client->opts & OPT_INQUIRE)
2685 unsigned char *result;
2686 size_t len;
2688 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2689 if (rc)
2690 return send_error (ctx, rc);
2692 line = (char *) result;
2695 if (!line || !*line)
2696 rc = GPG_ERR_SYNTAX;
2698 if (!rc)
2699 rc = do_xpathattr (ctx, line);
2701 if (client->opts & OPT_INQUIRE)
2702 xfree (line);
2704 return send_error (ctx, rc);
2707 static gpg_error_t
2708 do_import (struct client_s *client, unsigned char *line)
2710 char **req, **path = NULL, **path_orig = NULL, *content;
2711 xmlDocPtr doc = NULL;
2712 xmlNodePtr n, root, copy;
2713 gpg_error_t rc;
2715 req = str_split ((char *) line, "\t", 2);
2716 xfree (line);
2717 if (!req || !*req)
2718 return GPG_ERR_SYNTAX;
2720 content = req[0];
2721 path = str_split (req[1], "\t", 0);
2722 if (!content || !*content)
2724 rc = GPG_ERR_SYNTAX;
2725 goto fail;
2728 if (path && !valid_element_path (path, 0))
2730 rc = GPG_ERR_INV_VALUE;
2731 goto fail;
2734 doc = xmlReadDoc ((xmlChar *) content, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2735 if (!doc)
2737 rc = GPG_ERR_BAD_DATA;
2738 goto fail;
2741 root = xmlDocGetRootElement (doc);
2742 rc = validate_import (root);
2743 if (rc)
2744 goto fail;
2746 if (path)
2748 path_orig = strv_dup (path);
2749 if (!path_orig)
2751 log_write ("%s(%i): %s", __FILE__, __LINE__,
2752 pwmd_strerror (GPG_ERR_ENOMEM));
2753 rc = GPG_ERR_ENOMEM;
2754 goto fail;
2757 xmlChar *a = xmlGetProp (root, (xmlChar *) "_name");
2758 if (!a)
2760 strv_free (path_orig);
2761 rc = GPG_ERR_ENOMEM;
2762 goto fail;
2765 if (strv_printf (&path, "%s", (char *) a) == 0)
2767 xmlFree (a);
2768 strv_free (path_orig);
2769 rc = GPG_ERR_ENOMEM;
2770 goto fail;
2773 xmlFree (a);
2774 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
2776 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2778 strv_free (path_orig);
2779 goto fail;
2782 if (!rc)
2785 find_elements (client->doc, n->children, path + 1, &rc, NULL,
2786 NULL, NULL, 0, 0, NULL, 1);
2788 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2790 strv_free (path_orig);
2791 goto fail;
2793 else if (!rc)
2795 xmlNodePtr parent = n->parent;
2797 xmlUnlinkNode (n);
2798 xmlFreeNode (n);
2799 n = parent;
2803 strv_free (path);
2804 path = path_orig;
2806 if (rc == GPG_ERR_ELEMENT_NOT_FOUND)
2808 n = create_element_path (client, &path, &rc, NULL);
2810 if (rc)
2811 goto fail;
2814 copy = xmlCopyNodeList (root);
2815 n = xmlAddChildList (n, copy);
2816 if (!n)
2817 rc = GPG_ERR_BAD_DATA;
2819 else
2821 /* Check if the content root element can create a DTD root element. */
2822 if (!xmlStrEqual ((xmlChar *) "element", root->name))
2824 rc = GPG_ERR_SYNTAX;
2825 goto fail;
2828 xmlChar *a;
2830 if ((a = xmlGetProp (root, (xmlChar *) "_name")) == NULL)
2832 rc = GPG_ERR_SYNTAX;
2833 goto fail;
2836 char *tmp = str_dup ((char *) a);
2837 xmlFree (a);
2838 int literal = is_literal_element (&tmp);
2840 if (!valid_xml_element ((xmlChar *) tmp) || literal)
2842 xfree (tmp);
2843 rc = GPG_ERR_INV_VALUE;
2844 goto fail;
2847 if (strv_printf (&path, "%s", tmp) == 0)
2849 xfree (tmp);
2850 rc = GPG_ERR_ENOMEM;
2851 goto fail;
2854 xfree (tmp);
2855 n = find_root_element (client->doc, &path, &rc, NULL, 0, 1);
2857 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2859 rc = GPG_ERR_BAD_DATA;
2860 goto fail;
2863 /* Overwriting the existing tree. */
2864 if (!rc)
2866 xmlUnlinkNode (n);
2867 xmlFreeNodeList (n);
2870 rc = 0;
2871 xmlSetProp (root, (xmlChar *) "_name", (xmlChar *) path[0]);
2872 n = xmlCopyNode (root, 1);
2873 n = xmlAddChildList (xmlDocGetRootElement (client->doc), n);
2876 if (n && !rc)
2877 rc = update_element_mtime (n->parent);
2879 fail:
2880 if (doc)
2881 xmlFreeDoc (doc);
2883 if (path)
2884 strv_free (path);
2886 strv_free (req);
2887 return rc;
2890 static gpg_error_t
2891 import_command (assuan_context_t ctx, char *line)
2893 gpg_error_t rc;
2894 struct client_s *client = assuan_get_pointer (ctx);
2895 unsigned char *result;
2896 size_t len;
2898 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2899 if (rc)
2900 return send_error (ctx, rc);
2902 rc = do_import (client, result);
2903 return send_error (ctx, rc);
2906 static gpg_error_t
2907 do_lock (struct client_s *client, int add)
2909 gpg_error_t rc = lock_file_mutex (client, add);
2911 if (!rc)
2912 client->flags |= FLAG_LOCK_CMD;
2914 return rc;
2917 static gpg_error_t
2918 lock_command (assuan_context_t ctx, char *line)
2920 struct client_s *client = assuan_get_pointer (ctx);
2921 gpg_error_t rc = do_lock (client, 0);
2923 return send_error (ctx, rc);
2926 static gpg_error_t
2927 unlock_command (assuan_context_t ctx, char *line)
2929 struct client_s *client = assuan_get_pointer (ctx);
2930 gpg_error_t rc;
2932 rc = unlock_file_mutex (client, 0);
2933 return send_error (ctx, rc);
2936 static gpg_error_t
2937 option_command (assuan_context_t ctx, char *line)
2939 struct client_s *client = assuan_get_pointer (ctx);
2940 gpg_error_t rc = 0;
2941 struct pinentry_option_s *pin_opts = &client->pinentry_opts;
2942 #ifdef WITH_AGENT
2943 struct agent_s *agent = client->crypto->agent;
2944 #endif
2945 char namebuf[255] = { 0 };
2946 char *name = namebuf;
2947 char *value = NULL, *p;
2949 p = strchr (line, '=');
2950 if (!p)
2951 strncpy (namebuf, line, sizeof(namebuf));
2952 else
2954 strncpy (namebuf, line, strlen (line)-strlen (p));
2955 value = p+1;
2958 log_write1 ("OPTION name='%s' value='%s'", name, value);
2960 if (strcasecmp (name, (char *) "log_level") == 0)
2962 long l = 0;
2964 if (value)
2966 l = strtol (value, NULL, 10);
2968 if (l < 0 || l > 2)
2969 return send_error (ctx, GPG_ERR_INV_VALUE);
2972 MUTEX_LOCK (&rcfile_mutex);
2973 config_set_int_param (&global_config, "global", "log_level", value);
2974 MUTEX_UNLOCK (&rcfile_mutex);
2975 goto done;
2977 else if (strcasecmp (name, (char *) "lock-timeout") == 0)
2979 long n = 0;
2980 char *p = NULL;
2982 if (value)
2984 n = strtol (value, &p, 10);
2985 if (p && *p)
2986 return send_error (ctx, GPG_ERR_INV_VALUE);
2989 client->lock_timeout = n;
2990 goto done;
2992 else if (strcasecmp (name, (char *) "NAME") == 0)
2994 char *tmp = pthread_getspecific (thread_name_key);
2996 if (tmp)
2997 xfree (tmp);
2999 if (!value || !*value)
3001 pthread_setspecific (thread_name_key, str_dup (""));
3002 goto done;
3005 pthread_setspecific (thread_name_key, str_dup (value));
3006 goto done;
3008 else if (strcasecmp (name, (char *) "lc-messages") == 0)
3010 xfree (pin_opts->lc_messages);
3011 pin_opts->lc_messages = NULL;
3012 if (value && *value)
3013 pin_opts->lc_messages = str_dup (value);
3014 #ifdef WITH_AGENT
3015 if (use_agent)
3016 rc = set_agent_option (client->crypto->agent, "lc-messages", value);
3017 #endif
3019 else if (strcasecmp (name, (char *) "lc-ctype") == 0)
3021 xfree (pin_opts->lc_ctype);
3022 pin_opts->lc_ctype = NULL;
3023 if (value && *value)
3024 pin_opts->lc_ctype = str_dup (value);
3025 #ifdef WITH_AGENT
3026 if (use_agent)
3027 rc = set_agent_option (client->crypto->agent, "lc-ctype", value);
3028 #endif
3030 else if (strcasecmp (name, (char *) "ttyname") == 0)
3032 xfree (pin_opts->ttyname);
3033 pin_opts->ttyname = NULL;
3034 if (value && *value)
3035 pin_opts->ttyname = str_dup (value);
3036 #ifdef WITH_AGENT
3037 if (use_agent)
3038 rc = set_agent_option (client->crypto->agent, "ttyname", value);
3039 #endif
3041 else if (strcasecmp (name, (char *) "ttytype") == 0)
3043 xfree (pin_opts->ttytype);
3044 pin_opts->ttytype = NULL;
3045 if (value && *value)
3046 pin_opts->ttytype = str_dup (value);
3047 #ifdef WITH_AGENT
3048 if (use_agent)
3049 rc = set_agent_option (client->crypto->agent, "ttytype", value);
3050 #endif
3052 else if (strcasecmp (name, (char *) "display") == 0)
3054 xfree (pin_opts->display);
3055 pin_opts->display = NULL;
3056 if (value && *value)
3057 pin_opts->display = str_dup (value);
3058 #ifdef WITH_AGENT
3059 if (use_agent)
3060 rc = set_agent_option (client->crypto->agent, "display", value);
3061 #endif
3063 else if (strcasecmp (name, (char *) "pinentry-desc") == 0)
3065 xfree (pin_opts->desc);
3066 pin_opts->desc = NULL;
3067 if (value && *value)
3068 pin_opts->desc = str_dup (value);
3070 else if (strcasecmp (name, (char *) "pinentry-title") == 0)
3072 xfree (pin_opts->title);
3073 pin_opts->title = NULL;
3074 if (value && *value)
3075 pin_opts->title = str_dup (value);
3077 else if (strcasecmp (name, (char *) "pinentry-prompt") == 0)
3079 xfree (pin_opts->prompt);
3080 pin_opts->prompt = NULL;
3081 if (value && *value)
3082 pin_opts->prompt = str_dup (value);
3085 else if (strcasecmp (name, "pinentry-timeout") == 0)
3087 char *p = NULL;
3088 int n;
3090 if (!value)
3091 goto done;
3093 n = (int) strtol (value, &p, 10);
3095 if (*p || n < 0)
3096 return send_error (ctx, GPG_ERR_INV_VALUE);
3098 pin_opts->timeout = n;
3099 MUTEX_LOCK (&rcfile_mutex);
3100 config_set_int_param (&global_config,
3101 client->filename ? client->filename : "global",
3102 "pinentry_timeout", value);
3103 MUTEX_UNLOCK (&rcfile_mutex);
3104 goto done;
3106 else if (strcasecmp (name, "disable-pinentry") == 0)
3108 char *p = NULL;
3109 int n = 1;
3111 if (value && *value)
3113 n = (int) strtol (value, &p, 10);
3114 if (*p || n < 0 || n > 1)
3115 return send_error (ctx, GPG_ERR_INV_VALUE);
3118 if (n)
3119 client->flags |= FLAG_NO_PINENTRY;
3120 else
3121 client->flags &= ~FLAG_NO_PINENTRY;
3123 else
3124 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
3126 done:
3127 #ifdef WITH_AGENT
3128 if (!rc && use_agent && agent)
3130 rc = pinentry_merge_options (&client->crypto->agent->pinentry_opts,
3131 pin_opts);
3133 #endif
3135 return send_error (ctx, rc);
3138 static gpg_error_t
3139 do_rename (assuan_context_t ctx, char *line)
3141 struct client_s *client = assuan_get_pointer (ctx);
3142 gpg_error_t rc;
3143 char **req, **src, *dst;
3144 xmlNodePtr n, ndst;
3146 req = str_split (line, " ", 0);
3148 if (!req || !req[0] || !req[1])
3150 strv_free (req);
3151 return GPG_ERR_SYNTAX;
3154 dst = req[1];
3155 is_literal_element (&dst);
3157 if (!valid_xml_element ((xmlChar *) dst))
3159 strv_free (req);
3160 return GPG_ERR_INV_VALUE;
3163 if (strchr (req[0], '\t'))
3164 src = str_split (req[0], "\t", 0);
3165 else
3166 src = str_split (req[0], " ", 0);
3168 if (!src || !*src)
3170 rc = GPG_ERR_SYNTAX;
3171 goto fail;
3174 n = find_root_element (client->doc, &src, &rc, NULL, 0, 0);
3175 if (src[1] && n)
3176 n = find_elements (client->doc, n->children, src + 1, &rc, NULL, NULL,
3177 NULL, 0, 0, NULL, 0);
3179 if (!n)
3180 goto fail;
3182 xmlChar *a = xmlGetProp (n, (xmlChar *) "_name");
3183 if (!a)
3185 rc = GPG_ERR_ENOMEM;
3186 goto fail;
3189 /* To prevent unwanted effects:
3191 * <root name="a"><b/></root>
3193 * RENAME a<TAB>b b
3195 if (xmlStrEqual (a, (xmlChar *) dst))
3197 xmlFree (a);
3198 rc = GPG_ERR_AMBIGUOUS_NAME;
3199 goto fail;
3202 xmlFree (a);
3203 char **tmp = NULL;
3204 if (src[1])
3206 char **p;
3208 for (p = src; *p; p++)
3210 if (!*(p + 1))
3211 break;
3212 strv_printf (&tmp, "%s", *p);
3216 strv_printf (&tmp, "!%s", dst);
3217 ndst = find_root_element (client->doc, &tmp, &rc, NULL, 0, 0);
3218 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3220 strv_free (tmp);
3221 goto fail;
3224 if (tmp[1] && ndst)
3225 ndst = find_elements (client->doc, ndst->children, tmp + 1, &rc, NULL,
3226 NULL, NULL, 0, 0, NULL, 0);
3228 strv_free (tmp);
3229 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3230 goto fail;
3232 rc = 0;
3234 /* Target may exist:
3236 * <root name="a"/>
3237 * <root name="b" target="a"/>
3239 * RENAME b a
3241 * Would need to do:
3242 * RENAME !b a
3244 if (ndst == n)
3246 rc = GPG_ERR_AMBIGUOUS_NAME;
3247 goto fail;
3250 if (ndst)
3252 unlink_node (ndst);
3253 xmlFreeNodeList (ndst);
3256 rc = add_attribute (n, "_name", dst);
3258 fail:
3259 strv_free (req);
3260 strv_free (src);
3261 return rc;
3264 static gpg_error_t
3265 rename_command (assuan_context_t ctx, char *line)
3267 struct client_s *client = assuan_get_pointer (ctx);
3268 gpg_error_t rc;
3269 struct argv_s *args[] = {
3270 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3271 NULL
3274 rc = parse_options (&line, args, client);
3275 if (rc)
3276 return send_error (ctx, rc);
3278 if (client->opts & OPT_INQUIRE)
3280 unsigned char *result;
3281 size_t len;
3283 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3284 if (rc)
3285 return send_error (ctx, rc);
3287 line = (char *) result;
3290 rc = do_rename (ctx, line);
3292 if (client->opts & OPT_INQUIRE)
3293 xfree (line);
3295 return send_error (ctx, rc);
3298 static gpg_error_t
3299 do_copy (assuan_context_t ctx, char *line)
3301 struct client_s *client = assuan_get_pointer (ctx);
3302 gpg_error_t rc;
3303 char **req, **src = NULL, **dst = NULL;
3304 xmlNodePtr nsrc, ndst, new = NULL;
3306 req = str_split (line, " ", 0);
3307 if (!req || !req[0] || !req[1])
3309 strv_free (req);
3310 return GPG_ERR_SYNTAX;
3313 if (strchr (req[0], '\t'))
3314 src = str_split (req[0], "\t", 0);
3315 else
3316 src = str_split (req[0], " ", 0);
3318 if (!src || !*src)
3320 rc = GPG_ERR_SYNTAX;
3321 goto fail;
3324 if (strchr (req[1], '\t'))
3325 dst = str_split (req[1], "\t", 0);
3326 else
3327 dst = str_split (req[1], " ", 0);
3329 if (!dst || !*dst)
3331 rc = GPG_ERR_SYNTAX;
3332 goto fail;
3335 if (!valid_element_path (dst, 0))
3337 rc = GPG_ERR_INV_VALUE;
3338 goto fail;
3341 nsrc = find_root_element (client->doc, &src, &rc, NULL, 0, 0);
3342 if (nsrc && src[1])
3343 nsrc = find_elements (client->doc, nsrc->children, src + 1, &rc, NULL,
3344 NULL, NULL, 0, 0, NULL, 0);
3346 if (!nsrc)
3347 goto fail;
3349 new = xmlCopyNodeList (nsrc);
3350 if (!new)
3352 rc = GPG_ERR_ENOMEM;
3353 goto fail;
3356 int create = 0;
3357 ndst = find_root_element (client->doc, &dst, &rc, NULL, 0, 0);
3358 if (ndst && dst[1])
3360 if (ndst->children)
3361 ndst = find_elements (client->doc, ndst->children, dst + 1, &rc, NULL,
3362 NULL, create_target_elements_cb, 0, 0, NULL, 0);
3363 else
3364 create = 1;
3366 else
3367 create = 1;
3369 if (!ndst && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3370 goto fail;
3371 else if (create)
3373 ndst = create_element_path (client, &dst, &rc, NULL);
3374 if (!ndst)
3375 goto fail;
3378 /* Merge any attributes from the src node to the initial dst node. */
3379 for (xmlAttrPtr attr = new->properties; attr; attr = attr->next)
3381 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3382 continue;
3384 xmlAttrPtr a = xmlHasProp (ndst, attr->name);
3385 if (a)
3386 xmlRemoveProp (a);
3388 xmlChar *tmp = xmlNodeGetContent (attr->children);
3389 xmlNewProp (ndst, attr->name, tmp);
3390 xmlFree (tmp);
3391 rc = add_attribute (ndst, NULL, NULL);
3394 xmlNodePtr n = ndst->children;
3395 xmlUnlinkNode (n);
3396 xmlFreeNodeList (n);
3397 ndst->children = NULL;
3399 if (new->children)
3401 n = xmlCopyNodeList (new->children);
3402 if (!n)
3404 rc = GPG_ERR_ENOMEM;
3405 goto fail;
3408 n = xmlAddChildList (ndst, n);
3409 if (!n)
3411 rc = GPG_ERR_ENOMEM;
3412 goto fail;
3415 rc = update_element_mtime (xmlDocGetRootElement (client->doc) ==
3416 ndst->parent ? ndst : ndst->parent);
3419 fail:
3420 if (new)
3422 xmlUnlinkNode (new);
3423 xmlFreeNodeList (new);
3426 if (req)
3427 strv_free (req);
3429 if (src)
3430 strv_free (src);
3432 if (dst)
3433 strv_free (dst);
3435 return rc;
3438 static gpg_error_t
3439 copy_command (assuan_context_t ctx, char *line)
3441 struct client_s *client = assuan_get_pointer (ctx);
3442 gpg_error_t rc;
3443 struct argv_s *args[] = {
3444 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3445 NULL
3448 rc = parse_options (&line, args, client);
3449 if (rc)
3450 return send_error (ctx, rc);
3452 if (client->opts & OPT_INQUIRE)
3454 unsigned char *result;
3455 size_t len;
3457 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3458 if (rc)
3459 return send_error (ctx, rc);
3461 line = (char *) result;
3464 rc = do_copy (ctx, line);
3466 if (client->opts & OPT_INQUIRE)
3467 xfree (line);
3469 return send_error (ctx, rc);
3472 static gpg_error_t
3473 do_move (assuan_context_t ctx, char *line)
3475 struct client_s *client = assuan_get_pointer (ctx);
3476 gpg_error_t rc;
3477 char **req, **src = NULL, **dst = NULL;
3478 xmlNodePtr nsrc, ndst = NULL;
3480 req = str_split (line, " ", 0);
3482 if (!req || !req[0] || !req[1])
3484 strv_free (req);
3485 return GPG_ERR_SYNTAX;
3488 if (strchr (req[0], '\t'))
3489 src = str_split (req[0], "\t", 0);
3490 else
3491 src = str_split (req[0], " ", 0);
3493 if (!src || !*src)
3495 rc = GPG_ERR_SYNTAX;
3496 goto fail;
3499 if (strchr (req[1], '\t'))
3500 dst = str_split (req[1], "\t", 0);
3501 else
3502 dst = str_split (req[1], " ", 0);
3504 nsrc = find_root_element (client->doc, &src, &rc, NULL, 0, 0);
3505 if (nsrc && src[1])
3506 nsrc = find_elements (client->doc, nsrc->children, src + 1, &rc, NULL,
3507 NULL, NULL, 0, 0, NULL, 0);
3509 if (!nsrc)
3510 goto fail;
3512 if (dst)
3514 if (!valid_element_path (dst, 0))
3516 rc = GPG_ERR_INV_VALUE;
3517 goto fail;
3520 ndst = find_root_element (client->doc, &dst, &rc, NULL, 0, 0);
3521 if (ndst && dst[1])
3522 ndst = find_elements (client->doc, ndst->children, dst + 1, &rc, NULL,
3523 NULL, NULL, 0, 0, NULL, 0);
3525 else
3526 ndst = xmlDocGetRootElement (client->doc);
3528 for (xmlNodePtr n = ndst; n; n = n->parent)
3530 if (n == nsrc)
3532 rc = GPG_ERR_CONFLICT;
3533 goto fail;
3537 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3538 goto fail;
3540 rc = 0;
3542 if (ndst)
3544 xmlChar *a = node_has_attribute (nsrc, (xmlChar *) "_name");
3545 xmlNodePtr dup = find_element (ndst->children, (char *) a, NULL);
3547 xmlFree (a);
3548 if (dup)
3550 if (dup == nsrc)
3551 goto fail;
3553 if (ndst == xmlDocGetRootElement (client->doc))
3555 xmlNodePtr n = nsrc;
3556 int match = 0;
3558 while (n->parent && n->parent != ndst)
3559 n = n->parent;
3561 xmlChar *a = node_has_attribute (n, (xmlChar *) "_name");
3562 xmlChar *b = node_has_attribute (nsrc, (xmlChar *) "_name");
3564 if (xmlStrEqual (a, b))
3566 match = 1;
3567 xmlUnlinkNode (nsrc);
3568 xmlUnlinkNode (n);
3569 xmlFreeNodeList (n);
3572 xmlFree (a);
3573 xmlFree (b);
3575 if (!match)
3577 xmlUnlinkNode (dup);
3578 xmlFreeNodeList (dup);
3581 else
3582 xmlUnlinkNode (dup);
3586 if (!ndst && dst)
3588 xmlChar *name = node_has_attribute (nsrc, (xmlChar *) "_name");
3590 if (nsrc->parent == xmlDocGetRootElement (client->doc)
3591 && !strcmp ((char *) name, *dst))
3593 xmlFree (name);
3594 rc = GPG_ERR_CONFLICT;
3595 goto fail;
3598 xmlFree (name);
3599 ndst = create_element_path (client, &dst, &rc, nsrc);
3602 if (!ndst)
3603 goto fail;
3605 update_element_mtime (nsrc->parent);
3606 xmlUnlinkNode (nsrc);
3607 ndst = xmlAddChildList (ndst, nsrc);
3609 if (!ndst)
3610 rc = GPG_ERR_ENOMEM;
3612 update_element_mtime (ndst->parent);
3614 fail:
3615 if (req)
3616 strv_free (req);
3618 if (src)
3619 strv_free (src);
3621 if (dst)
3622 strv_free (dst);
3624 return rc;
3627 static gpg_error_t
3628 move_command (assuan_context_t ctx, char *line)
3630 struct client_s *client = assuan_get_pointer (ctx);
3631 gpg_error_t rc;
3632 struct argv_s *args[] = {
3633 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3634 NULL
3637 rc = parse_options (&line, args, client);
3638 if (rc)
3639 return send_error (ctx, rc);
3641 if (client->opts & OPT_INQUIRE)
3643 unsigned char *result;
3644 size_t len;
3646 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3647 if (rc)
3648 return send_error (ctx, rc);
3650 line = (char *) result;
3653 rc = do_move (ctx, line);
3655 if (client->opts & OPT_INQUIRE)
3656 xfree (line);
3658 return send_error (ctx, rc);
3661 static gpg_error_t
3662 ls_command (assuan_context_t ctx, char *line)
3664 gpg_error_t rc;
3665 char *tmp = str_asprintf ("%s/data", homedir);
3666 char *dir = expand_homedir (tmp);
3667 DIR *d = opendir (dir);
3669 rc = gpg_error_from_syserror ();
3670 xfree (tmp);
3672 if (!d)
3674 xfree (dir);
3675 return send_error (ctx, rc);
3678 size_t len =
3679 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3680 struct dirent *p = xmalloc (len), *cur = NULL;
3681 char *list = NULL;
3683 xfree (dir);
3684 rc = 0;
3686 while (!readdir_r (d, p, &cur) && cur)
3688 if (cur->d_name[0] == '.' && cur->d_name[1] == '\0')
3689 continue;
3690 else if (cur->d_name[0] == '.' && cur->d_name[1] == '.'
3691 && cur->d_name[2] == '\0')
3692 continue;
3694 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3696 if (!tmp)
3698 if (list)
3699 xfree (list);
3701 rc = GPG_ERR_ENOMEM;
3702 break;
3705 xfree (list);
3706 list = tmp;
3709 closedir (d);
3710 xfree (p);
3712 if (rc)
3713 return send_error (ctx, rc);
3715 if (!list)
3716 return send_error (ctx, GPG_ERR_NO_DATA);
3718 list[strlen (list) - 1] = 0;
3719 rc = xfer_data (ctx, list, strlen (list));
3720 xfree (list);
3721 return send_error (ctx, rc);
3724 static gpg_error_t
3725 bye_notify (assuan_context_t ctx, char *line)
3727 struct client_s *cl = assuan_get_pointer (ctx);
3729 #ifdef WITH_GNUTLS
3730 if (cl->thd->remote)
3732 int rc;
3736 struct timeval tv = { 0, 50000 };
3738 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3739 if (rc == GNUTLS_E_AGAIN)
3740 select (0, NULL, NULL, NULL, &tv);
3742 while (rc == GNUTLS_E_AGAIN);
3744 #endif
3746 /* This will let assuan_process_next() return. */
3747 fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK);
3748 cl->last_rc = 0; // BYE command result
3749 return 0;
3752 static gpg_error_t
3753 reset_notify (assuan_context_t ctx, char *line)
3755 struct client_s *client = assuan_get_pointer (ctx);
3757 if (client)
3758 cleanup_client (client);
3760 return 0;
3764 * This is called before every Assuan command.
3766 static gpg_error_t
3767 command_startup (assuan_context_t ctx, const char *name)
3769 struct client_s *client = assuan_get_pointer (ctx);
3770 gpg_error_t rc;
3771 struct command_table_s *cmd = NULL;
3773 log_write1 ("command='%s'", name);
3774 client->last_rc = client->opts = 0;
3776 for (int i = 0; command_table[i]; i++)
3778 if (!strcasecmp (name, command_table[i]->name))
3780 if (command_table[i]->ignore_startup)
3781 return 0;
3782 cmd = command_table[i];
3783 break;
3787 client->last_rc = rc = gpg_error (file_modified (client, cmd));
3788 return rc;
3792 * This is called after every Assuan command.
3794 static void
3795 command_finalize (assuan_context_t ctx, gpg_error_t rc)
3797 struct client_s *client = assuan_get_pointer (ctx);
3799 if (!(client->flags & FLAG_LOCK_CMD))
3800 unlock_file_mutex (client, 0);
3802 log_write1 (_("command completed: rc=%u"), client->last_rc);
3803 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
3806 static gpg_error_t
3807 help_command (assuan_context_t ctx, char *line)
3809 gpg_error_t rc;
3810 int i;
3812 if (!line || !*line)
3814 char *tmp;
3815 char *help = str_dup (_("Usage: HELP [<COMMAND>]\n"
3816 "For commands that take an element path as an argument, each element is "
3817 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3818 "\n" "COMMANDS:"));
3820 for (i = 0; command_table[i]; i++)
3822 if (!command_table[i]->help)
3823 continue;
3825 tmp = str_asprintf ("%s %s", help, command_table[i]->name);
3826 xfree (help);
3827 help = tmp;
3830 tmp = strip_texi_and_wrap (help);
3831 xfree (help);
3832 rc = xfer_data (ctx, tmp, strlen (tmp));
3833 xfree (tmp);
3834 return send_error (ctx, rc);
3837 for (i = 0; command_table[i]; i++)
3839 if (!strcasecmp (line, command_table[i]->name))
3841 char *help, *tmp;
3843 if (!command_table[i]->help)
3844 break;
3846 help = strip_texi_and_wrap (command_table[i]->help);
3847 tmp = str_asprintf (_("Usage: %s"), help);
3848 xfree (help);
3849 rc = xfer_data (ctx, tmp, strlen (tmp));
3850 xfree (tmp);
3851 return send_error (ctx, rc);
3855 return send_error (ctx, GPG_ERR_INV_NAME);
3858 static void
3859 new_command (const char *name, int ignore, int unlock,
3860 gpg_error_t (*handler) (assuan_context_t, char *),
3861 const char *help)
3863 int i = 0;
3865 if (command_table)
3866 for (i = 0; command_table[i]; i++);
3868 command_table =
3869 xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
3870 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
3871 command_table[i]->name = name;
3872 command_table[i]->handler = handler;
3873 command_table[i]->ignore_startup = ignore;
3874 command_table[i]->unlock = unlock;
3875 command_table[i++]->help = help;
3876 command_table[i] = NULL;
3879 void
3880 deinit_commands ()
3882 int i;
3884 for (i = 0; command_table[i]; i++)
3885 xfree (command_table[i]);
3887 xfree (command_table);
3890 static int
3891 sort_commands (const void *arg1, const void *arg2)
3893 struct command_table_s *const *a = arg1;
3894 struct command_table_s *const *b = arg2;
3896 if (!*a || !*b)
3897 return 0;
3898 else if (*a && !*b)
3899 return 1;
3900 else if (!*a && *b)
3901 return -1;
3903 return strcmp ((*a)->name, (*b)->name);
3906 static gpg_error_t
3907 passwd_command (assuan_context_t ctx, char *line)
3909 struct client_s *client = assuan_get_pointer (ctx);
3910 gpg_error_t rc;
3911 struct argv_s *args[] = {
3912 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
3913 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
3914 NULL
3917 if (client->flags & FLAG_NEW)
3918 return send_error (ctx, GPG_ERR_INV_STATE);
3920 client->crypto->save.s2k_count =
3921 config_get_ulong (client->filename, "s2k_count");
3922 rc = parse_options (&line, args, client);
3923 if (rc)
3924 return send_error (ctx, rc);
3926 if (!rc && client->opts & OPT_RESET)
3928 rc = cache_clear (client->md5file);
3929 if (!rc)
3930 send_status_all (STATUS_CACHE, NULL);
3933 if (!rc)
3935 if (!IS_PKCS (client->crypto))
3937 struct crypto_s *crypto;
3939 xfree (client->crypto->filename);
3940 client->crypto->filename = str_dup (client->filename);
3941 rc = change_passwd (ctx, client->filename,
3942 client->flags & FLAG_NO_PINENTRY, &crypto);
3943 if (!rc)
3945 cleanup_crypto (&client->crypto);
3946 client->crypto = crypto;
3947 update_checksum (client);
3948 cleanup_crypto_stage1 (client->crypto);
3951 #ifdef WITH_AGENT
3952 else
3954 if (client->crypto->save.s2k_count)
3955 rc = send_to_agent (client->crypto->agent, NULL, NULL,
3956 "OPTION s2k-count=%lu",
3957 client->crypto->save.s2k_count);
3959 if (!rc)
3960 rc = agent_passwd (client->crypto);
3962 #endif
3965 return send_error (ctx, rc);
3968 static gpg_error_t
3969 parse_keygrip_opt_sign (void *data, void *value)
3971 struct client_s *client = data;
3973 (void) value;
3974 client->opts |= OPT_SIGN;
3975 return 0;
3978 static gpg_error_t
3979 keygrip_command (assuan_context_t ctx, char *line)
3981 struct client_s *client = assuan_get_pointer (ctx);
3982 gpg_error_t rc;
3983 struct crypto_s *crypto;
3984 struct argv_s *args[] = {
3985 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_keygrip_opt_sign},
3986 NULL
3989 if (!line || !*line)
3990 return send_error (ctx, GPG_ERR_SYNTAX);
3992 rc = parse_options (&line, args, client);
3993 if (rc)
3994 return send_error (ctx, rc);
3996 if (!valid_filename (line))
3997 return send_error (ctx, GPG_ERR_INV_VALUE);
3999 rc = init_client_crypto (&crypto);
4000 if (rc)
4001 return send_error (ctx, rc);
4003 rc = read_data_file (line, crypto);
4004 if (!rc)
4006 char *hexgrip = NULL;
4008 if (!IS_PKCS (crypto))
4010 cleanup_crypto (&crypto);
4011 return send_error (ctx, GPG_ERR_NOT_SUPPORTED);
4014 if (client->opts & OPT_SIGN)
4016 if (valid_keygrip (crypto->sign_grip, sizeof (crypto->sign_grip)))
4017 hexgrip = bin2hex (crypto->sign_grip, sizeof (crypto->sign_grip));
4020 if (!hexgrip)
4021 hexgrip = bin2hex (crypto->grip, sizeof (crypto->grip));
4023 if (!hexgrip)
4024 rc = GPG_ERR_ENOMEM;
4025 else
4026 rc = xfer_data (ctx, hexgrip, strlen (hexgrip));
4028 xfree (hexgrip);
4031 cleanup_crypto (&crypto);
4032 return send_error (ctx, rc);
4035 static gpg_error_t
4036 parse_opt_data (void *data, void *value)
4038 struct client_s *client = data;
4040 (void) value;
4041 client->opts |= OPT_DATA;
4042 return 0;
4045 static gpg_error_t
4046 getinfo_command (assuan_context_t ctx, char *line)
4048 struct client_s *client = assuan_get_pointer (ctx);
4049 gpg_error_t rc;
4050 char buf[ASSUAN_LINELENGTH];
4051 struct argv_s *args[] = {
4052 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4053 NULL
4056 rc = parse_options (&line, args, client);
4057 if (rc)
4058 return send_error (ctx, rc);
4060 if (!strcasecmp (line, "clients"))
4062 if (client->opts & OPT_DATA)
4064 MUTEX_LOCK (&cn_mutex);
4065 snprintf (buf, sizeof (buf), "%i", slist_length (cn_thread_list));
4066 MUTEX_UNLOCK (&cn_mutex);
4067 rc = xfer_data (ctx, buf, strlen (buf));
4069 else
4070 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4072 else if (!strcasecmp (line, "cache"))
4074 if (client->opts & OPT_DATA)
4076 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4077 rc = xfer_data (ctx, buf, strlen (buf));
4079 else
4080 rc = send_status (ctx, STATUS_CACHE, NULL);
4082 else if (!strcasecmp (line, "pid"))
4084 char buf[32];
4085 pid_t pid = getpid ();
4087 snprintf (buf, sizeof (buf), "%i", pid);
4088 rc = xfer_data (ctx, buf, strlen (buf));
4090 else if (!strcasecmp (line, "version"))
4092 char *buf = str_asprintf ("0x%06x %s", VERSION_HEX,
4093 #ifdef WITH_LIBACL
4094 "ACL "
4095 #endif
4096 #ifdef WITH_GNUTLS
4097 "GNUTLS "
4098 #endif
4099 #ifdef WITH_AGENT
4100 "AGENT "
4101 #endif
4102 #ifdef WITH_QUALITY
4103 "QUALITY "
4104 #endif
4105 "");
4106 rc = xfer_data (ctx, buf, strlen (buf));
4107 xfree (buf);
4109 else if (!strcasecmp (line, "last_error"))
4111 if (client->last_error)
4112 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4113 else
4114 rc = GPG_ERR_NO_DATA;
4116 else
4117 rc = gpg_error (GPG_ERR_SYNTAX);
4119 return send_error (ctx, rc);
4122 #ifdef WITH_AGENT
4123 static gpg_error_t
4124 send_data_cb (void *user, const void *buf, size_t len)
4126 assuan_context_t ctx = user;
4128 return assuan_send_data (ctx, buf, len);
4131 static gpg_error_t
4132 send_status_cb (void *user, const char *line)
4134 assuan_context_t ctx = user;
4135 char keyword[200], *k;
4136 const char *p;
4138 for (p = line, k = keyword; *p; p++)
4140 if (isspace (*p))
4141 break;
4143 *k++ = *p;
4146 *k = 0;
4147 if (*p == '#')
4148 p++;
4150 while (isspace (*p))
4151 p++;
4153 return assuan_write_status (ctx, keyword, *p ? p : NULL);
4155 #endif
4157 static gpg_error_t
4158 agent_command (assuan_context_t ctx, char *line)
4160 gpg_error_t rc = 0;
4162 if (!use_agent)
4163 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
4165 #ifdef WITH_AGENT
4166 struct client_s *client = assuan_get_pointer (ctx);
4168 if (!line || !*line)
4169 return send_error (ctx, GPG_ERR_SYNTAX);
4171 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 1);
4172 rc = assuan_transact (client->crypto->agent->ctx, line, send_data_cb,
4173 client->ctx, agent_loopback_cb, client->crypto,
4174 send_status_cb, client->ctx);
4175 if (gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
4177 char *line;
4178 size_t len;
4180 rc = assuan_write_line (client->crypto->agent->ctx, "CAN");
4181 if (!rc)
4183 rc = assuan_read_line (client->crypto->agent->ctx, &line, &len);
4184 if (!rc)
4185 rc = gpg_error (GPG_ERR_ASS_CANCELED);
4189 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 0);
4190 #endif
4191 return send_error (ctx, rc);
4194 void
4195 init_commands ()
4197 /* !BEGIN-HELP-TEXT!
4199 * This comment is used as a marker to generate the offline documentation
4200 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4201 * script to determine where commands begin and end.
4203 new_command("HELP", 1, 1, help_command, _(
4204 "HELP [<COMMAND>]\n"
4205 "Show available commands or command specific help text."
4208 new_command("AGENT", 1, 1, agent_command, _(
4209 "AGENT <command>\n"
4210 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4211 "@command{gpg-agent}."
4214 new_command("GETINFO", 1, 1, getinfo_command, _(
4215 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4216 "Get server and other information: @var{cache} returns the number of cached "
4217 "documents via a status message. @var{clients} returns the number of "
4218 "connected clients via a status message. @var{pid} returns the process ID "
4219 "number of the server via a data response. @var{VERSION} returns the server "
4220 "version number and compile-time features with a data response with each "
4221 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4222 "the last failed command when available. @xref{Status Messages}. "
4223 "\n"
4224 "When the @option{--data} option is specified then the result will be send "
4225 "via a data response rather than a status message."
4228 new_command("PASSWD", 0, 0, passwd_command, _(
4229 "PASSWD [--reset] [--s2k-count=N]\n"
4230 "Changes the passphrase of the secret key required to open the current "
4231 "file. When the @option{--reset} option is passed then the cache entry for "
4232 "the current file will be reset and the passphrase, if any, will be required "
4233 "during the next @code{OPEN}. @xref{OPEN}."
4234 "\n"
4235 "The @option{--s2k-count} option sets number of hash iterations for a "
4236 "passphrase and must be either @code{0} to use the calibrated count of the "
4237 "machine (the default), or a value greater than or equal to @code{65536}. "
4238 "@xref{SAVE}."
4241 new_command("KEYGRIP", 1, 1, keygrip_command, _(
4242 "KEYGRIP [--sign] <filename>\n"
4243 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4244 "data response."
4245 "\n"
4246 "When the @option{--sign} option is specified then the key used for signing "
4247 "of the specified @var{filename} will be returned."
4250 new_command("OPEN", 1, 1, open_command, _(
4251 "OPEN [--lock] <filename> [<passphrase>]\n"
4252 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4253 "found on the file-system then a new document will be created. If the file "
4254 "is found, it is looked for in the file cache. If cached and no "
4255 "@var{passphrase} was specified then the cached document is opened. When not "
4256 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4257 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4258 "specified."
4259 "\n"
4260 "When the @option{--lock} option is passed then the file mutex will be "
4261 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4262 "file has been opened."
4265 new_command("SAVE", 0, 0, save_command, _(
4266 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring [--sign-keygrip=hexstring]]\n"
4267 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4268 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4269 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4270 "keypair will be generated and a pinentry will be used to prompt for the "
4271 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4272 "passed, in which case the data file will not be passphrase protected."
4273 "\n"
4274 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4275 "passphrase retrieval and caching for new files and behaves as if the "
4276 "@option{--no-agent} commmand line option to @command{pwmd} was specified."
4277 "\n"
4278 "The @option{--reset} option will clear the cache entry for the current file "
4279 "before saving."
4280 "\n"
4281 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4282 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4283 "(@pxref{Configuration}) for available ciphers."
4284 "\n"
4285 "The @option{--cipher-iterations} option specifies the number of times to "
4286 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4287 "\n"
4288 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4289 "the client to obtain the key paramaters to use when generating a new "
4290 "keypair. The inquired data is expected to be an S-expression. If not "
4291 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4292 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4293 "that when this option is specified a new keypair will be generated "
4294 "reguardless if the file is a new one or not."
4295 "\n"
4296 "You can encrypt the data file to a public key other than the one that it "
4297 "was originally encrypted with by passing the @option{--keygrip} option with "
4298 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4299 "be of any key that @command{gpg-agent} knows about. The "
4300 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4301 "secret key. This option may be needed when using a smartcard."
4302 "\n"
4303 "The @option{--s2k-count} option sets number of hash iterations for a "
4304 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4305 "value which is the default. This setting only affects new files. To change "
4306 "the setting, use the @code{PASSWD} command (@pxref{PASSWD})."
4309 new_command("ISCACHED", 1, 0, iscached_command, _(
4310 "ISCACHED [--lock] <filename>\n"
4311 "An @emph{OK} response is returned if the specified @var{filename} is found "
4312 "in the file cache. If not found in the cache but exists on the filesystem "
4313 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4314 "returned."
4315 "\n"
4316 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4317 "file exists; it does not need to be opened nor cached."
4320 new_command("CLEARCACHE", 1, 1, clearcache_command, _(
4321 "CLEARCACHE [<filename>]\n"
4322 "Clears a file cache entry for all or the specified @var{filename}."
4325 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command, _(
4326 "CACHETIMEOUT <filename> <seconds>\n"
4327 "The time in @var{seconds} until @var{filename} will be removed from the "
4328 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4329 "the passphrase for each @code{OPEN} command (@pxref{OPEN}). "
4330 "@xref{Configuration}, and the @code{cache_timeout} parameter."
4333 new_command("LIST", 0, 1, list_command, _(
4334 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4335 "If no element path is given then a newline separated list of root elements "
4336 "is returned with a data response. If given, then all reachable elements "
4337 "of the specified element path are returned unless the @option{--no-recurse} "
4338 "option is specified. If specified, only the child elements of the element "
4339 "path are returned without recursing into grandchildren. Each resulting "
4340 "element is prefixed with the literal @code{!} character when the element "
4341 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4342 "\n"
4343 "When the @option{--verbose} option is passed then each element path "
4344 "returned will have zero or more flags appened to it. These flags are "
4345 "delimited from the element path by a single space character. A flag itself "
4346 "is a single character. Flag @code{+} indicates that there are child nodes of "
4347 "the current element path. Flag @code{E} indicates that an element of an "
4348 "element path contained in a @var{target} attribute could not be found. Flag "
4349 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4350 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4351 "of a @var{target} attribute (see below)."
4352 "\n"
4353 "The @option{--with-target} option implies @option{--verbose} and will append "
4354 "an additional flag @code{T} followed by a single space then an element path. "
4355 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4356 "current element when it contains a @var{target} attribute. When no "
4357 "@var{target} attribute is found then no flag will be appended."
4358 "\n"
4359 "The @option{--no-recurse} option limits the amount of data returned to only "
4360 "the listing of children of the specified element path and not any "
4361 "grandchildren."
4362 "\n"
4363 "The @option{--all} option lists the entire element tree for each root "
4364 "element. This option also implies option @option{--verbose}."
4365 "\n"
4366 "When the @option{--inquire} option is passed then all remaining non-option "
4367 "arguments are retrieved via a server @emph{INQUIRE}."
4370 new_command("REALPATH", 0, 1, realpath_command, _(
4371 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4372 "Resolves all @code{target} attributes of the specified element path and "
4373 "returns the result with a data response. @xref{Target Attribute}, for details."
4374 "\n"
4375 "When the @option{--inquire} option is passed then all remaining non-option "
4376 "arguments are retrieved via a server @emph{INQUIRE}."
4379 new_command("STORE", 0, 1, store_command, _(
4380 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4381 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4382 "\n"
4383 "Creates a new element path or modifies the @var{content} of an existing "
4384 "element. If only a single element is specified then a new root element is "
4385 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4386 "set to the final @key{TAB} delimited element. If no @var{content} is "
4387 "specified after the final @key{TAB}, then the content of the element will "
4388 "be removed, or empty when creating a new element."
4389 "\n"
4390 "The only restriction of an element name is that it not contain whitespace "
4391 "or begin with the literal element character @code{!} unless specifying a "
4392 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4393 "the @key{TAB} delimited elements. It is recommended that the content of an "
4394 "element be base64 encoded when it contains control or @key{TAB} characters "
4395 "to prevent @abbr{XML} and @command{pwmd} parsing errors."
4398 new_command("RENAME", 0, 1, rename_command, _(
4399 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4400 "Renames the specified @var{element} to the new @var{value}. If an element of "
4401 "the same name as the @var{value} already exists it will be overwritten."
4402 "\n"
4403 "When the @option{--inquire} option is passed then all remaining non-option "
4404 "arguments are retrieved via a server @emph{INQUIRE}."
4407 new_command("COPY", 0, 1, copy_command, _(
4408 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4409 "Copies the entire element tree starting from the child node of the source "
4410 "element, to the destination element path. If the destination element path "
4411 "does not exist then it will be created; otherwise it is overwritten."
4412 "\n"
4413 "Note that attributes from the source element are merged into the "
4414 "destination element when the destination element path exists. When an "
4415 "attribute of the same name exists in both the source and destination "
4416 "elements then the destination attribute will be updated to the source "
4417 "attribute value."
4418 "\n"
4419 "When the @option{--inquire} option is passed then all remaining non-option "
4420 "arguments are retrieved via a server @emph{INQUIRE}."
4423 new_command("MOVE", 0, 1, move_command, _(
4424 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4425 "Moves the source element path to the destination element path. If the "
4426 "destination is not specified then it will be moved to the root node of the "
4427 "document. If the destination is specified and exists then it will be "
4428 "overwritten; otherwise it will be created."
4429 "\n"
4430 "When the @option{--inquire} option is passed then all remaining non-option "
4431 "arguments are retrieved via a server @emph{INQUIRE}."
4434 new_command("DELETE", 0, 1, delete_command, _(
4435 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4436 "Removes the specified element path and all of its children. This may break "
4437 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4438 "refers to this element or any of its children."
4439 "\n"
4440 "When the @option{--inquire} option is passed then all remaining non-option "
4441 "arguments are retrieved via a server @emph{INQUIRE}."
4444 new_command("GET", 0, 1, get_command, _(
4445 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4446 "Retrieves the content of the specified element. The content is returned "
4447 "with a data response."
4448 "\n"
4449 "When the @option{--inquire} option is passed then all remaining non-option "
4450 "arguments are retrieved via a server @emph{INQUIRE}."
4453 new_command("ATTR", 0, 1, attr_command, _(
4454 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4455 "@table @asis\n"
4456 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4457 "\n"
4458 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4459 "element. When no @var{value} is specified any existing value will be removed."
4460 "\n"
4461 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4462 "\n"
4463 " Removes an @var{attribute} from an element."
4464 "\n"
4465 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4466 "\n"
4467 " Retrieves a newline separated list of attributes names and values "
4468 "from the specified element. Each attribute name and value is space delimited."
4469 "\n"
4470 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4471 "\n"
4472 " Retrieves the value of an @var{attribute} from an element."
4473 "@end table\n"
4474 "\n"
4475 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4476 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4477 "commands instead."
4478 "\n"
4479 "The @code{_mtime} attribute is updated each time an element is modified by "
4480 "either storing content, editing attributes or by deleting a child element. "
4481 "The @code{_ctime} attribute is created for each new element in an element "
4482 "path."
4483 "\n"
4484 "When the @option{--inquire} option is passed then all remaining non-option "
4485 "arguments are retrieved via a server @emph{INQUIRE}."
4486 "\n"
4487 "@xref{Target Attribute}, for details about this special attribute."
4490 new_command("XPATH", 0, 1, xpath_command, _(
4491 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4492 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4493 "specified, it is assumed the expression is a request to return a result. "
4494 "Otherwise, the result is set to the @var{value} argument and the document is "
4495 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4496 "is assumed to be empty and the document is updated. For example:"
4497 "@sp 1\n"
4498 "@example\n"
4499 "XPATH //element[@@_name='password']@key{TAB}\n"
4500 "@end example\n"
4501 "@sp 1"
4502 "would clear the content of all @code{password} elements in the data file "
4503 "while leaving off the trailing @key{TAB} would return all @code{password} "
4504 "elements in @abbr{XML} format."
4505 "\n"
4506 "When the @option{--inquire} option is passed then all remaining non-option "
4507 "arguments are retrieved via a server @emph{INQUIRE}."
4508 "\n"
4509 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4510 "expression syntax."
4513 new_command("XPATHATTR", 0, 1, xpathattr_command, _(
4514 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4515 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4516 "attributes and does not return a result. For the @var{SET} operation the "
4517 "@var{value} is optional but the field is required. If not specified then "
4518 "the attribute value will be empty. For example:"
4519 "@sp 1"
4520 "@example\n"
4521 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4522 "@end example\n"
4523 "@sp 1"
4524 "would create an @code{password} attribute for each @code{password} element "
4525 "found in the document. The attribute value will be empty but still exist."
4526 "\n"
4527 "When the @option{--inquire} option is passed then all remaining non-option "
4528 "arguments are retrieved via a server @emph{INQUIRE}."
4529 "\n"
4530 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4531 "expression syntax."
4534 new_command("IMPORT", 0, 1, import_command, _(
4535 "IMPORT <content>[<TAB>[!]element[<TAB>[!]child[..]]]\n"
4536 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4537 "\n"
4538 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4539 "argument is raw @abbr{XML} data. The content is created as a child of the "
4540 "specified element path and will overwrite an existing element of the same "
4541 "name. If an element of the element path does not exist then it will be "
4542 "created."
4543 "\n"
4544 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4545 "for details."
4548 new_command("DUMP", 0, 1, dump_command, _(
4549 "DUMP\n"
4550 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4551 "dumping a specific node."
4554 new_command("LOCK", 0, 0, lock_command, _(
4555 "LOCK\n"
4556 "Locks the mutex associated with the opened file. This prevents other clients "
4557 "from sending commands to the same opened file until the client "
4558 "that sent this command either disconnects or sends the @code{UNLOCK} "
4559 "command. @xref{UNLOCK}."
4562 new_command("UNLOCK", 1, 0, unlock_command, _(
4563 "UNLOCK\n"
4564 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4565 "a commands' @option{lock} option. @xref{LOCK}."
4568 new_command("GETCONFIG", 1, 1, getconfig_command, _(
4569 "GETCONFIG [filename] <parameter>\n"
4570 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4571 "data response. If no file has been opened then the value for @var{filename} "
4572 "or the default from the @samp{global} section will be returned. If a file "
4573 "has been opened and no @var{filename} is specified, a value previously "
4574 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4577 new_command("OPTION", 1, 1, option_command, _(
4578 "OPTION <NAME>=<VALUE>\n"
4579 "Sets a client option @var{name} to @var{value}. The value for an option is "
4580 "kept for the duration of the connection."
4581 "\n"
4582 "@table @asis\n"
4583 "@item DISABLE-PINENTRY\n"
4584 " Disable use of @command{pinentry} for passphrase retrieval. When set, a "
4585 " server inquire is sent to the client to obtain the passphrase. This option "
4586 " may be set as needed before the @pxref{OPEN}, @pxref{PASSWD}, and "
4587 " @pxref{SAVE} commands."
4588 "\n"
4589 "@item TTYNAME\n"
4590 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4591 "\n"
4592 "@item TTYTYPE\n"
4593 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4594 "\n"
4595 "@item DISPLAY\n"
4596 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4597 "\n"
4598 "@item PINENTRY-DESC\n"
4599 " Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4600 "\n"
4601 "@item PINENTRY-TITLE\n"
4602 " Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
4603 "\n"
4604 "@item PINENTRY-PROMPT\n"
4605 " Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
4606 "\n"
4607 "@item LC-CTYPE\n"
4608 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4609 "\n"
4610 "@item LC-MESSAGES\n"
4611 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4612 "\n"
4613 "@item NAME\n"
4614 " Associates the thread ID of the connection with the specified textual "
4615 "representation. Useful for debugging log messages."
4616 "\n"
4617 "@item LOCK-TIMEOUT\n"
4618 " When not @code{0}, the duration in tenths of a second to wait for the file "
4619 "mutex which has been locked by another thread to be released before returning "
4620 "an error. When @code{-1}, then an error will be returned immediately."
4621 "@end table\n"
4624 new_command("LS", 1, 1, ls_command, _(
4625 "LS\n"
4626 "Lists the available data files stored in the data directory "
4627 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4630 new_command("RESET", 1, 1, NULL, _(
4631 "RESET\n"
4632 "Closes the currently opened file but keeps any previously set client options."
4635 new_command("NOP", 1, 1, NULL, _(
4636 "NOP\n"
4637 "Does nothing. Always returns successfully."
4640 /* !END-HELP-TEXT! */
4641 new_command ("CANCEL", 1, 1, NULL, NULL);
4642 new_command ("END", 1, 1, NULL, NULL);
4643 new_command ("BYE", 1, 1, NULL, NULL);
4645 int i;
4646 for (i = 0; command_table[i]; i++);
4647 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4648 sort_commands);
4651 gpg_error_t
4652 register_commands (assuan_context_t ctx)
4654 int i = 0, rc;
4656 for (; command_table[i]; i++)
4658 if (!command_table[i]->handler)
4659 continue;
4661 rc = assuan_register_command (ctx, command_table[i]->name,
4662 command_table[i]->handler,
4663 command_table[i]->help);
4664 if (rc)
4665 return rc;
4668 rc = assuan_register_bye_notify (ctx, bye_notify);
4669 if (rc)
4670 return rc;
4672 rc = assuan_register_reset_notify (ctx, reset_notify);
4673 if (rc)
4674 return rc;
4676 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4677 if (rc)
4678 return rc;
4680 return assuan_register_post_cmd_notify (ctx, command_finalize);