Fix GETINFO VERSION to append AGENT conditionally.
[pwmd.git] / src / commands.c
blob8bc94f86582fc355ca78cea81f97384f74c43001
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 #ifdef WITH_AGENT
78 /* The GETCONFIG command, for example, may fail because pwmd lost the
79 * gpg-agent connection. Update the recovered agent ctx. */
80 #define UPDATE_AGENT_CTX(client, crypto) do { \
81 if (crypto && crypto->agent && client->crypto && client->crypto->agent \
82 && client->crypto->agent->ctx && \
83 crypto->agent->ctx != client->crypto->agent->ctx) \
84 { \
85 client->crypto->agent->ctx = crypto->agent->ctx; \
86 crypto->agent->ctx = NULL; \
87 } \
88 } while (0)
89 #else
90 #define UPDATE_AGENT_CTX(client, crypto)
91 #endif
93 struct command_table_s
95 const char *name;
96 gpg_error_t (*handler) (assuan_context_t, char *line);
97 const char *help;
98 int ignore_startup;
99 int unlock; // unlock the file mutex after validating the checksum
102 static struct command_table_s **command_table;
104 static gpg_error_t do_lock (struct client_s *client, int add);
105 static gpg_error_t validate_checksum (struct client_s *,
106 struct cache_data_s *);
107 static gpg_error_t update_checksum (struct client_s *client);
109 static gpg_error_t
110 unlock_file_mutex (struct client_s *client, int remove)
112 gpg_error_t rc = 0;
114 // OPEN: keep the lock for the same file being reopened.
115 if (client->flags & FLAG_KEEP_LOCK && client->flags & FLAG_HAS_LOCK)
116 return 0;
118 if (!(client->flags & FLAG_HAS_LOCK))
119 return GPG_ERR_NOT_LOCKED;
121 rc = cache_unlock_mutex (client->md5file, remove);
122 if (rc)
123 rc = GPG_ERR_INV_STATE;
124 else
125 client->flags &= ~(FLAG_HAS_LOCK | FLAG_LOCK_CMD);
127 return rc;
130 static gpg_error_t
131 lock_file_mutex (struct client_s *client, int add)
133 gpg_error_t rc = 0;
134 int timeout = config_get_integer (client->filename, "cache_timeout");
136 if (client->flags & FLAG_HAS_LOCK)
137 return 0;
139 rc = cache_lock_mutex (client->ctx, client->md5file,
140 client->lock_timeout, add, timeout);
141 if (!rc)
142 client->flags |= FLAG_HAS_LOCK;
144 return rc;
147 static gpg_error_t
148 file_modified (struct client_s *client, struct command_table_s *cmd)
150 gpg_error_t rc = 0;
152 if (!(client->flags & FLAG_OPEN))
153 return GPG_ERR_INV_STATE;
155 rc = lock_file_mutex (client, 0);
156 if (!rc || rc == GPG_ERR_NO_DATA)
158 rc = validate_checksum (client, NULL);
159 if (gpg_err_code (rc) == GPG_ERR_ENOENT)
160 rc = 0;
163 if ((cmd->unlock && !(client->flags & FLAG_HAS_LOCK)) || rc)
164 unlock_file_mutex (client, 0);
166 return rc;
169 static gpg_error_t
170 parse_xml (assuan_context_t ctx, int new)
172 struct client_s *client = assuan_get_pointer (ctx);
173 int cached = client->doc != NULL;
175 if (new)
177 client->doc = new_document ();
178 if (client->doc)
180 xmlDocDumpFormatMemory (client->doc,
181 (xmlChar **) & client->crypto->plaintext,
182 (int *) &client->crypto->plaintext_len, 0);
183 if (!client->crypto->plaintext)
185 xmlFreeDoc (client->doc);
186 client->doc = NULL;
190 else if (!cached)
191 client->doc = parse_doc ((char *) client->crypto->plaintext,
192 client->crypto->plaintext_len);
194 return !client->doc ? GPG_ERR_ENOMEM : 0;
197 static void
198 free_client (struct client_s *client)
200 if (client->doc)
201 xmlFreeDoc (client->doc);
203 xfree (client->crc);
204 xfree (client->filename);
205 xfree (client->last_error);
207 if (client->crypto)
209 cleanup_crypto_stage2 (client->crypto);
210 if (client->crypto->pkey_sexp)
211 gcry_sexp_release (client->crypto->pkey_sexp);
213 client->crypto->pkey_sexp = NULL;
214 memset (client->crypto->sign_grip, 0,
215 sizeof (client->crypto->sign_grip));
216 memset (client->crypto->grip, 0, sizeof(client->crypto->grip));
219 if (client->xml_error)
220 xmlResetError (client->xml_error);
223 void
224 cleanup_client (struct client_s *client)
226 assuan_context_t ctx = client->ctx;
227 struct client_thread_s *thd = client->thd;
228 struct crypto_s *crypto = client->crypto;
229 long lock_timeout = client->lock_timeout;
230 int no_pinentry = (client->flags & FLAG_NO_PINENTRY);
231 struct pinentry_option_s pin_opts;
232 #ifdef WITH_AGENT
233 struct pinentry_option_s agent_pin_opts;
235 if (crypto && crypto->agent)
236 memcpy (&agent_pin_opts, &crypto->agent->pinentry_opts,
237 sizeof(struct pinentry_option_s));
238 #endif
240 memcpy (&pin_opts, &client->pinentry_opts, sizeof(struct pinentry_option_s));
241 unlock_file_mutex (client, client->flags & FLAG_NEW);
242 free_client (client);
243 memset (client, 0, sizeof (struct client_s));
244 client->crypto = crypto;
245 client->ctx = ctx;
246 client->thd = thd;
247 client->lock_timeout = lock_timeout;
248 memcpy (&client->pinentry_opts, &pin_opts, sizeof(struct pinentry_option_s));
249 client->flags |= no_pinentry ? FLAG_NO_PINENTRY : 0;
250 #ifdef WITH_AGENT
251 if (crypto && crypto->agent)
252 memcpy (&crypto->agent->pinentry_opts, &agent_pin_opts,
253 sizeof(struct pinentry_option_s));
254 #endif
257 static gpg_error_t
258 open_finalize (assuan_context_t ctx, char *key, size_t keylen)
260 struct client_s *client = assuan_get_pointer (ctx);
261 gpg_error_t rc = 0;
262 struct cache_data_s *cdata = cache_get_data (client->md5file);
263 int cached = 0, keyarg = key ? 1 : 0;
264 size_t keysize;
265 unsigned char *salted_key = NULL;
266 int algo;
268 client->crypto->filename = str_dup (client->filename);
269 if (cdata || client->flags & FLAG_NEW)
271 if (!(client->flags & FLAG_NEW))
273 rc = decrypt_xml (client->crypto, cdata->doc, cdata->doclen);
274 if (rc)
275 return rc;
278 cached = cdata != NULL;
279 goto done;
282 if (!key && !IS_PKCS (client->crypto))
284 if (client->flags & FLAG_NO_PINENTRY)
286 rc = inquire_passphrase (ctx, "PASSPHRASE", (unsigned char **)&key,
287 &keylen);
288 if (rc)
289 return rc;
291 else
293 client->pinentry_opts.timeout = config_get_integer (client->filename,
294 "pinentry_timeout");
295 rc = getpin_common (client->ctx, client->filename, PINENTRY_OPEN,
296 &key, &keylen);
297 if (rc)
298 return rc;
302 if (!IS_PKCS (client->crypto))
304 algo = cipher_to_gcrypt (client->crypto->hdr.flags);
305 rc = hash_key (algo, client->crypto->hdr.salt,
306 sizeof(client->crypto->hdr.salt), key, keylen,
307 (void **)&salted_key, &keysize);
308 if (!keyarg)
309 gcry_free (key);
311 if (!rc)
312 rc = decrypt_data (client->ctx, client->crypto, salted_key, keysize);
314 if (rc)
315 return rc;
317 cdata = xcalloc (1, sizeof (struct cache_data_s));
318 if (!cdata)
319 return GPG_ERR_ENOMEM;
321 cdata->key = salted_key;
322 cdata->keylen = keysize;
324 else
325 rc = decrypt_data (client->ctx, client->crypto, NULL, 0);
327 done:
328 if (!rc)
330 rc = parse_xml (ctx, client->flags & FLAG_NEW);
331 if (!rc)
333 int timeout = config_get_integer (client->filename,
334 "cache_timeout");
336 if (!cached)
338 if (!cdata)
339 cdata = xcalloc (1, sizeof (struct cache_data_s));
341 rc = encrypt_xml (NULL, cache_key, cache_keysize,
342 GCRY_CIPHER_AES, client->crypto->plaintext,
343 client->crypto->plaintext_len, &cdata->doc,
344 &cdata->doclen, &cache_iv, &cache_blocksize,
346 if (!rc && !(client->flags & FLAG_NEW) && IS_PKCS (client->crypto))
348 rc = gcry_sexp_build ((gcry_sexp_t *) & cdata->pubkey, NULL,
349 "%S", client->crypto->pkey_sexp);
353 if (cdata->sigkey)
354 gcry_sexp_release (cdata->sigkey);
356 cdata->sigkey = NULL;
358 if (!rc && IS_PKCS (client->crypto))
359 rc = gcry_sexp_build ((gcry_sexp_t *) &cdata->sigkey, NULL,
360 "%S", client->crypto->sigpkey_sexp);
362 if (!rc
363 && !cache_add_file (client->md5file, (client->flags & FLAG_NEW)
364 ? NULL : client->crypto->grip, cdata, timeout))
366 if (!cached)
368 free_cache_data_once (cdata);
369 xmlFreeDoc (client->doc);
370 client->doc = NULL;
372 rc = GPG_ERR_ENOMEM;
375 if (!rc)
376 client->flags |= FLAG_OPEN;
380 if (!rc)
381 update_checksum (client);
383 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
384 rc = do_lock (client, 0);
386 return rc;
389 static void
390 req_cleanup (void *arg)
392 if (!arg)
393 return;
395 strv_free ((char **) arg);
398 static gpg_error_t
399 parse_open_opt_lock (void *data, void *value)
401 struct client_s *client = data;
403 client->opts |= OPT_LOCK_ON_OPEN;
404 return 0;
407 static gpg_error_t
408 parse_opt_inquire (void *data, void *value)
410 struct client_s *client = data;
412 (void) value;
413 client->opts |= OPT_INQUIRE;
414 return 0;
417 static gpg_error_t
418 update_checksum (struct client_s *client)
420 unsigned char *crc;
421 size_t len;
422 struct cache_data_s *cdata;
423 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
425 if (rc)
426 return rc;
428 xfree (client->crc);
429 client->crc = crc;
430 cdata = cache_get_data (client->md5file);
431 if (cdata)
433 xfree (cdata->crc);
434 cdata->crc = xmalloc (len);
435 memcpy (cdata->crc, crc, len);
438 return 0;
441 static gpg_error_t
442 validate_checksum (struct client_s *client, struct cache_data_s *cdata)
444 unsigned char *crc;
445 size_t len;
446 gpg_error_t rc;
447 int n = 0;
449 if (cdata && !cdata->crc)
450 return GPG_ERR_CHECKSUM;
452 rc = get_checksum (client->filename, &crc, &len);
453 if (rc)
454 return rc;
456 if (cdata)
457 n = memcmp (cdata->crc, crc, len);
458 else if (client->crc)
459 n = memcmp (client->crc, crc, len);
461 xfree (crc);
462 return n ? GPG_ERR_CHECKSUM : 0;
465 static gpg_error_t
466 do_open (assuan_context_t ctx, const char *filename, const char *password)
468 struct client_s *client = assuan_get_pointer (ctx);
469 struct cache_data_s *cdata;
470 gpg_error_t rc = 0;
471 int done = 0;
473 if (!valid_filename (filename))
474 return GPG_ERR_INV_VALUE;
476 gcry_md_hash_buffer (GCRY_MD_MD5, client->md5file, filename,
477 strlen (filename));
478 client->filename = str_dup (filename);
479 if (!client->filename)
480 return GPG_ERR_ENOMEM;
482 // Cached document?
483 cdata = cache_get_data (client->md5file);
484 if (cdata && cdata->doc)
486 int defer;
488 rc = validate_checksum (client, cdata);
489 /* This will check that the key is cached in the agent which needs to
490 * be determined for files that share a keygrip. */
491 if (!rc)
493 rc = cache_iscached (client->filename, &defer);
494 if (!rc && defer)
495 rc = GPG_ERR_KEY_EXPIRED;
498 #ifdef WITH_GNUTLS
499 if (!rc && client->thd->remote
500 && config_get_boolean (NULL, "tcp_require_key"))
501 rc = GPG_ERR_KEY_EXPIRED;
502 #endif
504 if (!rc && !password)
506 rc = read_data_header (client->filename, &client->crypto->hdr,
507 NULL, NULL);
508 if (!rc)
510 if (IS_PKCS (client->crypto))
512 gcry_sexp_build (&client->crypto->pkey_sexp, NULL, "%S",
513 cdata->pubkey);
514 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
515 client->crypto->grip);
516 gcry_sexp_build (&client->crypto->sigpkey_sexp, NULL, "%S",
517 cdata->sigkey);
518 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
519 client->crypto->sign_grip);
522 if (!rc)
524 rc = open_finalize (ctx, NULL, 0);
525 done = 1;
530 /* There was an error accessing the file so clear the cache entry. The
531 * real error will be returned from read_data_file() since the file
532 * may have only disappeared. */
533 if (!done)
535 log_write ("%s: %s", filename, pwmd_strerror (rc));
536 rc = cache_clear (client->md5file);
537 send_status_all (STATUS_CACHE, NULL);
541 if (done || rc)
542 return rc;
544 rc = read_data_file (client->filename, client->crypto);
545 if (rc)
547 if (gpg_err_source (rc) != GPG_ERR_SOURCE_DEFAULT ||
548 gpg_err_code (rc) != GPG_ERR_ENOENT)
550 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
551 return rc;
554 client->flags |= FLAG_NEW;
555 memset (client->crypto->grip, 0, sizeof (client->crypto->grip));
556 memset (client->crypto->sign_grip, 0,
557 sizeof (client->crypto->sign_grip));
558 rc = open_finalize (ctx, NULL, 0);
559 return rc;
562 if (password && IS_PKCS (client->crypto))
564 #ifdef WITH_AGENT
565 rc = set_agent_passphrase (client->crypto, password, strlen (password));
566 if (rc)
567 return rc;
568 #endif
571 rc = open_finalize (ctx, (char *)password, password ? strlen (password) : 0);
572 return rc;
575 static gpg_error_t
576 open_command (assuan_context_t ctx, char *line)
578 gpg_error_t rc;
579 struct client_s *client = assuan_get_pointer (ctx);
580 char **req, *password = NULL, *filename;
581 unsigned char md5file[16];
582 int same_file = 0;
583 struct argv_s *args[] = {
584 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
585 NULL
588 rc = parse_options (&line, args, client);
589 if (rc)
590 return send_error (ctx, rc);
592 req = str_split (line, " ", 2);
593 if (!req)
594 return send_error (ctx, GPG_ERR_SYNTAX);
596 #ifdef WITH_GNUTLS
597 if (client->thd->remote)
599 rc = tls_validate_access (client, req[0]);
600 if (rc)
602 if (rc == GPG_ERR_INV_USER_ID)
603 log_write (_("client validation failed for file '%s'"), req[0]);
604 strv_free (req);
605 return send_error (ctx, rc);
608 /* Remote pinentries are not supported. */
609 client->flags |= FLAG_NO_PINENTRY;
611 #endif
613 pthread_cleanup_push (req_cleanup, req);
614 filename = req[0];
615 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, filename, strlen (filename));
616 /* This client may have locked a different file with ISCACHED --lock than
617 * the current filename. This will remove that lock. */
618 same_file = !memcmp (md5file, client->md5file, 16) ? 1 : 0;
619 if (client->flags & FLAG_OPEN ||
620 (client->flags & FLAG_HAS_LOCK && !same_file))
622 typeof (client->opts) opts = client->opts;
623 typeof (client->flags) flags = client->flags;
625 if (same_file)
626 client->flags |= FLAG_KEEP_LOCK;
628 cleanup_client (client);
629 client->opts = opts;
630 client->flags |= flags;
631 client->flags &= ~(FLAG_LOCK_CMD | FLAG_NEW);
632 if (!same_file)
633 client->flags &= ~(FLAG_HAS_LOCK);
636 /* Need to lock the mutex here because file_modified() cannot without
637 * knowing the filename. */
638 memcpy (client->md5file, md5file, 16);
639 rc = lock_file_mutex (client, 1);
640 if (!rc)
642 password = req[1] && *req[1] ? req[1] : NULL;
643 #ifdef WITH_AGENT
644 if (IS_PKCS (client->crypto))
645 rc = set_pinentry_mode (client->crypto->agent,
646 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
647 : "ask");
648 #endif
649 if (!rc)
651 rc = do_open (ctx, filename, password);
652 if (rc)
653 cleanup_client (client);
655 cleanup_crypto_stage1 (client->crypto);
659 pthread_cleanup_pop (1);
661 if (!rc && client->flags & FLAG_NEW)
662 rc = send_status (ctx, STATUS_NEWFILE, NULL);
664 #ifdef WITH_AGENT
665 (void) kill_scd (client->crypto->agent);
666 #endif
667 return send_error (ctx, rc);
670 static gpg_error_t
671 parse_save_opt_no_passphrase (void *data, void *value)
673 struct client_s *client = data;
675 (void) value;
676 client->opts |= OPT_NO_PASSPHRASE;
677 return 0;
680 static gpg_error_t
681 parse_save_opt_no_agent (void *data, void *value)
683 struct client_s *client = data;
685 client->opts |= OPT_NO_AGENT;
686 return 0;
689 static gpg_error_t
690 parse_save_opt_cipher (void *data, void *value)
692 struct client_s *client = data;
693 int algo = cipher_string_to_gcrypt ((char *) value);
694 file_header_t *hdr = &client->crypto->save.hdr;
696 if (algo == -1)
697 return GPG_ERR_INV_VALUE;
699 hdr->flags = set_cipher_flag (hdr->flags, algo);
700 return 0;
703 static gpg_error_t
704 parse_save_opt_keygrip (void *data, void *value)
706 struct client_s *client = data;
708 if (!IS_PKCS (client->crypto))
709 return GPG_ERR_INV_ARG;
711 #ifdef WITH_AGENT
712 if (client->crypto->save.pkey)
713 gcry_sexp_release (client->crypto->save.pkey);
715 client->crypto->save.pkey = NULL;
716 return get_pubkey (client->crypto, value, &client->crypto->save.pkey);
717 #else
718 return GPG_ERR_INV_ARG;
719 #endif
722 static gpg_error_t
723 parse_save_opt_sign_keygrip (void *data, void *value)
725 struct client_s *client = data;
727 if (!IS_PKCS (client->crypto))
728 return GPG_ERR_INV_ARG;
730 #ifdef WITH_AGENT
731 if (client->crypto->save.sigpkey)
732 gcry_sexp_release (client->crypto->save.sigpkey);
734 client->crypto->save.sigpkey = NULL;
735 return get_pubkey (client->crypto, value, &client->crypto->save.sigpkey);
736 #else
737 return GPG_ERR_INV_ARG;
738 #endif
741 static gpg_error_t
742 parse_opt_s2k_count (void *data, void *value)
744 struct client_s *client = data;
745 char *v = value;
747 if (!v || !*v)
748 return GPG_ERR_INV_VALUE;
750 client->crypto->save.s2k_count = strtoul (v, NULL, 10);
751 return 0;
754 static gpg_error_t
755 parse_save_opt_iterations (void *data, void *value)
757 struct client_s *client = data;
758 char *v = value, *p;
759 uint64_t n;
761 if (!v || !*v)
762 return GPG_ERR_INV_VALUE;
764 errno = 0;
765 n = strtoull (v, &p, 10);
766 if (n == UINT64_MAX && errno)
767 return gpg_error_from_syserror ();
768 else if (p && *p)
769 return GPG_ERR_INV_VALUE;
771 client->crypto->save.hdr.iterations = n;
772 return 0;
775 static gpg_error_t
776 save_finalize (assuan_context_t ctx)
778 struct client_s *client = assuan_get_pointer (ctx);
779 gpg_error_t rc = 0;
780 xmlChar *xmlbuf = NULL;
781 int xmlbuflen;
782 void *key = NULL;
783 size_t keylen = 0;
785 xmlDocDumpFormatMemory (client->doc, &xmlbuf, &xmlbuflen, 0);
786 if (!xmlbuf)
787 return GPG_ERR_ENOMEM;
789 pthread_cleanup_push (xmlFree, xmlbuf);
791 if (!use_agent || ((client->flags & FLAG_NEW)
792 && (client->opts & OPT_NO_AGENT))
793 || !(client->crypto->hdr.flags & PWMD_FLAG_PKCS))
795 rc = export_common (ctx, client->flags & FLAG_NO_PINENTRY,
796 client->crypto, xmlbuf, xmlbuflen, client->filename,
797 NULL, &key, &keylen, 1, 1);
799 #ifdef WITH_AGENT
800 else
802 gcry_sexp_t pubkey = client->crypto->save.pkey;
803 gcry_sexp_t sigkey = client->crypto->save.sigpkey;
805 if (!pubkey)
806 pubkey = client->crypto->pkey_sexp;
808 if (!sigkey)
810 sigkey = client->crypto->sigpkey_sexp;
811 if (!sigkey)
813 gcry_sexp_build (&client->crypto->save.sigpkey, 0, "%S", pubkey);
814 sigkey = client->crypto->save.sigpkey;
818 rc = encrypt_data_file (client->ctx, client->crypto, pubkey, sigkey,
819 client->filename, xmlbuf, xmlbuflen);
820 if (pubkey == client->crypto->save.pkey)
822 if (!rc)
824 gcry_sexp_release (client->crypto->pkey_sexp);
825 client->crypto->pkey_sexp = client->crypto->save.pkey;
826 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
827 client->crypto->grip);
829 else
830 gcry_sexp_release (pubkey);
832 client->crypto->save.pkey = NULL;
835 if (!rc)
836 gcry_pk_get_keygrip (sigkey, client->crypto->sign_grip);
838 if (sigkey == client->crypto->save.sigpkey)
840 if (!rc)
842 if (client->crypto->sigpkey_sexp)
843 gcry_sexp_release (client->crypto->sigpkey_sexp);
845 client->crypto->sigpkey_sexp = client->crypto->save.sigpkey;
846 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
847 client->crypto->sign_grip);
849 else
850 gcry_sexp_release (sigkey);
852 client->crypto->save.sigpkey = NULL;
855 #endif
857 if (!rc)
859 int cached;
861 rc = save_common (client->filename, client->crypto, xmlbuf, xmlbuflen,
862 key, keylen, &cached, client->opts & OPT_NO_AGENT);
863 if (rc)
864 gcry_free (key);
866 if (!rc && (!cached || (client->flags & FLAG_NEW)))
867 send_status_all (STATUS_CACHE, NULL);
869 if (!rc)
871 update_checksum (client);
872 client->flags &= ~(FLAG_NEW);
874 else
875 rc = GPG_ERR_ENOMEM;
878 pthread_cleanup_pop (1); // xmlFree
879 return rc;
882 static gpg_error_t
883 parse_opt_reset (void *data, void *value)
885 struct client_s *client = data;
887 (void) value;
888 client->opts |= OPT_RESET;
889 return 0;
892 static gpg_error_t
893 save_command (assuan_context_t ctx, char *line)
895 struct client_s *client = assuan_get_pointer (ctx);
896 gpg_error_t rc;
897 struct stat st;
898 struct argv_s *args[] = {
899 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG,
900 parse_save_opt_no_passphrase},
901 &(struct argv_s) {"cipher", OPTION_TYPE_ARG, parse_save_opt_cipher},
902 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
903 parse_opt_inquire},
904 &(struct argv_s) {"keygrip", OPTION_TYPE_ARG, parse_save_opt_keygrip},
905 &(struct argv_s) {"sign-keygrip", OPTION_TYPE_ARG,
906 parse_save_opt_sign_keygrip},
907 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
908 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
909 &(struct argv_s) {"cipher-iterations", OPTION_TYPE_ARG,
910 parse_save_opt_iterations},
911 &(struct argv_s) {"no-agent", OPTION_TYPE_NOARG,
912 parse_save_opt_no_agent},
913 NULL
916 cleanup_save (&client->crypto->save);
917 memcpy (&client->crypto->save.hdr, &client->crypto->hdr,
918 sizeof (file_header_t));
919 client->crypto->save.s2k_count =
920 config_get_ulong (client->filename, "s2k_count");
922 if (client->flags & FLAG_NEW)
923 client->crypto->save.hdr.iterations =
924 config_get_ulonglong (client->filename, "cipher_iterations");
926 rc = parse_options (&line, args, client);
927 if (rc)
928 return send_error (ctx, rc);
930 if (!(client->flags & FLAG_NEW))
931 client->opts &= ~OPT_NO_AGENT;
932 else if (client->opts & OPT_NO_AGENT)
934 client->crypto->save.hdr.flags &= ~PWMD_FLAG_PKCS;
935 client->crypto->hdr.flags &= ~PWMD_FLAG_PKCS;
938 if ((client->opts & OPT_NO_PASSPHRASE) && !(client->flags & FLAG_NEW)
939 && !(client->opts & OPT_INQUIRE))
940 return send_error (ctx, GPG_ERR_WRONG_KEY_USAGE);
942 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
943 return send_error (ctx, gpg_error_from_syserror ());
945 if (errno != ENOENT && !S_ISREG (st.st_mode))
947 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
948 return send_error (ctx, GPG_ERR_ENOANO);
951 int defer;
952 rc = cache_iscached (client->filename, &defer);
953 if (!rc && defer)
955 log_write ("%s: %s", client->filename,
956 pwmd_strerror (GPG_ERR_KEY_EXPIRED));
957 client->opts |= OPT_RESET;
960 if (client->opts & OPT_RESET)
962 rc = cache_clear (client->md5file);
963 if (rc)
964 return send_error (ctx, rc);
966 send_status_all (STATUS_CACHE, NULL);
969 rc = update_element_mtime (xmlDocGetRootElement (client->doc));
970 if (rc)
971 return send_error (ctx, rc);
973 #ifdef WITH_AGENT
974 if (!rc && use_agent && !client->crypto->save.pkey &&
975 (client->flags & FLAG_NEW || client->opts & OPT_INQUIRE)
976 && !(client->opts & OPT_NO_AGENT))
978 rc = send_status (client->ctx, STATUS_GENKEY, NULL);
979 if (!rc)
981 struct inquire_data_s idata = { 0 };
982 char *params = client->opts & OPT_INQUIRE ? NULL
983 : default_key_params (client->crypto);
985 pthread_cleanup_push (xfree, params);
986 idata.crypto = client->crypto;
988 if (params)
990 idata.line = params;
991 idata.len = strlen (params);
993 else
994 idata.preset = 1;
996 client->crypto->agent->inquire_data = &idata;
997 client->crypto->agent->inquire_cb = NULL;
998 rc = generate_key (client->crypto, params,
999 (client->opts & OPT_NO_PASSPHRASE), 1);
1000 pthread_cleanup_pop (1);
1003 #endif
1005 if (!rc)
1006 rc = save_finalize (ctx);
1008 cleanup_crypto_stage1 (client->crypto);
1009 #ifdef WITH_AGENT
1010 (void) kill_scd (client->crypto->agent);
1011 #endif
1012 return send_error (ctx, rc);
1015 static gpg_error_t
1016 do_delete (assuan_context_t ctx, char *line)
1018 struct client_s *client = assuan_get_pointer (ctx);
1019 gpg_error_t rc;
1020 char **req;
1021 xmlNodePtr n;
1023 if (strchr (line, '\t'))
1024 req = str_split (line, "\t", 0);
1025 else
1026 req = str_split (line, " ", 0);
1028 if (!req || !*req)
1029 return GPG_ERR_SYNTAX;
1031 n = find_root_element (client->doc, &req, &rc, NULL, 0, 0);
1032 if (!n)
1034 strv_free (req);
1035 return rc;
1039 * No sub-node defined. Remove the entire node (root element).
1041 if (!req[1])
1043 if (n)
1045 rc = unlink_node (n);
1046 xmlFreeNode (n);
1049 strv_free (req);
1050 return rc;
1054 find_elements (client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1055 0, 0, NULL, 0);
1056 strv_free (req);
1057 if (!n)
1058 return rc;
1060 if (n)
1062 rc = unlink_node (n);
1063 xmlFreeNode (n);
1066 return rc;
1069 static gpg_error_t
1070 delete_command (assuan_context_t ctx, char *line)
1072 struct client_s *client = assuan_get_pointer (ctx);
1073 gpg_error_t rc;
1074 struct argv_s *args[] = {
1075 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1076 NULL
1079 rc = parse_options (&line, args, client);
1080 if (rc)
1081 return send_error (ctx, rc);
1083 if (client->opts & OPT_INQUIRE)
1085 unsigned char *result;
1086 size_t len;
1088 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1089 if (rc)
1090 return send_error (ctx, rc);
1092 line = (char *) result;
1095 rc = do_delete (ctx, line);
1097 if (client->opts & OPT_INQUIRE)
1098 xfree (line);
1100 return send_error (ctx, rc);
1103 static gpg_error_t
1104 store_command (assuan_context_t ctx, char *line)
1106 struct client_s *client = assuan_get_pointer (ctx);
1107 gpg_error_t rc;
1108 size_t len;
1109 unsigned char *result;
1110 char **req;
1111 xmlNodePtr n, parent;
1112 int has_content;
1113 char *content = NULL;
1115 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1116 if (rc)
1117 return send_error (ctx, rc);
1119 req = str_split ((char *) result, "\t", 0);
1120 xfree (result);
1122 if (!req || !*req)
1123 return send_error (ctx, GPG_ERR_SYNTAX);
1125 len = strv_length (req);
1126 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1127 if (*(req + 1) && !valid_element_path (req, has_content))
1129 strv_free (req);
1130 return send_error (ctx, GPG_ERR_INV_VALUE);
1133 if (has_content || !*req[len - 1])
1135 has_content = 1;
1136 content = req[len - 1];
1137 req[len - 1] = NULL;
1140 again:
1141 n = find_root_element (client->doc, &req, &rc, NULL, 0, 0);
1142 if (rc && rc == GPG_ERR_ELEMENT_NOT_FOUND)
1144 rc = new_root_element (client->doc, *req);
1145 if (rc)
1147 strv_free (req);
1148 return send_error (ctx, GPG_ERR_SYNTAX);
1151 goto again;
1154 if (!n)
1156 strv_free (req);
1157 return send_error (ctx, rc);
1160 parent = n;
1162 if (req[1] && *req[1])
1164 if (!n->children)
1165 parent = create_elements_cb (n, req + 1, &rc, NULL);
1166 else
1167 parent = find_elements (client->doc, n->children, req + 1, &rc,
1168 NULL, NULL, create_elements_cb, 0, 0, NULL,
1172 if (!rc && len > 1)
1174 n = find_text_node (parent->children);
1175 if (n)
1176 xmlNodeSetContent (n, (xmlChar *) content);
1177 else
1178 xmlNodeAddContent (parent, (xmlChar *) content);
1180 update_element_mtime (parent);
1183 xfree (content);
1184 strv_free (req);
1185 return send_error (ctx, rc);
1188 static gpg_error_t
1189 xfer_data (assuan_context_t ctx, const char *line, int total)
1191 int to_send;
1192 int sent = 0;
1193 gpg_error_t rc;
1194 int progress = config_get_integer ("global", "xfer_progress");
1195 int flush = 0;
1197 progress =
1198 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1199 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1200 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1202 if (rc)
1203 return rc;
1205 again:
1208 if (sent + to_send > total)
1209 to_send = total - sent;
1211 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1212 flush ? 0 : to_send);
1213 if (!rc)
1215 sent += flush ? 0 : to_send;
1217 if ((progress && !(sent % progress) && sent != total) ||
1218 (sent == total && flush))
1219 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1221 if (!flush && !rc && sent == total)
1223 flush = 1;
1224 goto again;
1228 while (!rc && sent < total);
1230 return rc;
1233 static gpg_error_t
1234 do_get (assuan_context_t ctx, char *line)
1236 struct client_s *client = assuan_get_pointer (ctx);
1237 gpg_error_t rc;
1238 char **req;
1239 xmlNodePtr n;
1241 req = str_split (line, "\t", 0);
1243 if (!req || !*req)
1245 strv_free (req);
1246 return GPG_ERR_SYNTAX;
1249 n = find_root_element (client->doc, &req, &rc, NULL, 0, 0);
1250 if (!n)
1252 strv_free (req);
1253 return rc;
1256 if (req[1])
1258 find_elements (client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1259 0, 0, NULL, 0);
1261 strv_free (req);
1262 if (rc)
1263 return rc;
1265 if (!n || !n->children)
1266 return GPG_ERR_NO_DATA;
1268 n = find_text_node (n->children);
1269 if (!n || !n->content || !*n->content)
1270 return GPG_ERR_NO_DATA;
1272 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1273 return rc;
1276 static gpg_error_t
1277 get_command (assuan_context_t ctx, char *line)
1279 struct client_s *client = assuan_get_pointer (ctx);
1280 gpg_error_t rc;
1281 struct argv_s *args[] = {
1282 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1283 NULL
1286 rc = parse_options (&line, args, client);
1287 if (rc)
1288 return send_error (ctx, rc);
1290 if (client->opts & OPT_INQUIRE)
1292 unsigned char *result;
1293 size_t len;
1295 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1296 if (rc)
1297 return send_error (ctx, rc);
1299 line = (char *) result;
1302 rc = do_get (ctx, line);
1304 if (client->opts & OPT_INQUIRE)
1305 xfree (line);
1307 return send_error (ctx, rc);
1310 static void list_command_cleanup1 (void *arg);
1311 static gpg_error_t
1312 realpath_command (assuan_context_t ctx, char *line)
1314 struct string_s *string = NULL;
1315 gpg_error_t rc;
1316 struct client_s *client = assuan_get_pointer (ctx);
1317 struct argv_s *args[] = {
1318 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1319 NULL
1322 rc = parse_options (&line, args, client);
1323 if (rc)
1324 return send_error (ctx, rc);
1326 if (client->opts & OPT_INQUIRE)
1328 unsigned char *result;
1329 size_t len;
1331 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1332 if (rc)
1333 return send_error (ctx, rc);
1335 line = (char *) result;
1338 rc = build_realpath (client->doc, line, &string);
1339 if (!rc)
1341 pthread_cleanup_push (list_command_cleanup1, string);
1342 rc = xfer_data (ctx, string->str, string->len);
1343 pthread_cleanup_pop (1);
1346 if (client->opts & OPT_INQUIRE)
1347 xfree (line);
1349 return send_error (ctx, rc);
1352 static void
1353 list_command_cleanup1 (void *arg)
1355 if (arg)
1356 string_free ((struct string_s *) arg, 1);
1359 static void
1360 list_command_cleanup2 (void *arg)
1362 struct element_list_s *elements = arg;
1364 if (elements)
1366 if (elements->list)
1368 int total = slist_length (elements->list);
1369 int i;
1371 for (i = 0; i < total; i++)
1373 char *tmp = slist_nth_data (elements->list, i);
1374 xfree (tmp);
1377 slist_free (elements->list);
1380 if (elements->prefix)
1381 xfree (elements->prefix);
1383 if (elements->req)
1384 strv_free (elements->req);
1386 xfree (elements);
1390 static gpg_error_t
1391 parse_list_opt_norecurse (void *data, void *value)
1393 struct client_s *client = data;
1395 client->opts &= ~(OPT_LIST_RECURSE);
1396 return 0;
1399 static gpg_error_t
1400 parse_list_opt_verbose (void *data, void *value)
1402 struct client_s *client = data;
1404 client->opts |= OPT_LIST_VERBOSE;
1405 return 0;
1408 static gpg_error_t
1409 parse_list_opt_target (void *data, void *value)
1411 struct client_s *client = data;
1413 client->opts |= OPT_LIST_WITH_TARGET;
1414 return 0;
1417 static gpg_error_t
1418 parse_list_opt_all (void *data, void *value)
1420 struct client_s *client = data;
1422 client->opts |= OPT_LIST_ALL;
1423 return 0;
1426 static gpg_error_t
1427 list_path_once (struct client_s *client, char *line,
1428 struct element_list_s *elements, struct string_s *result)
1430 gpg_error_t rc;
1432 elements->req = str_split (line, " ", 0);
1433 if (!elements->req)
1434 strv_printf (&elements->req, "%s", line);
1436 rc = create_path_list (client->doc, elements, *elements->req);
1437 if (rc == GPG_ERR_ELOOP && elements->verbose)
1438 rc = 0;
1440 if (!rc)
1442 if (elements)
1444 int total = slist_length (elements->list);
1445 int i;
1447 if (!total)
1448 rc = GPG_ERR_NO_DATA;
1450 if (!rc)
1452 if (!rc)
1454 for (i = 0; i < total; i++)
1456 char *tmp = slist_nth_data (elements->list, i);
1458 string_append_printf (result, "%s%s", tmp,
1459 i + 1 == total ? "" : "\n");
1464 else
1465 rc = GPG_ERR_NO_DATA;
1468 return rc;
1471 static int
1472 has_list_flag (char *path, char *flags)
1474 char *p = path;
1476 while (*p && *++p != ' ');
1478 if (!*p)
1479 return 0;
1481 for (; *p; p++)
1483 char *f;
1485 for (f = flags; *f && *f != ' '; f++)
1487 if (*p == *f)
1488 return 1;
1492 return 0;
1495 static gpg_error_t
1496 do_list (assuan_context_t ctx, char *line)
1498 struct client_s *client = assuan_get_pointer (ctx);
1499 gpg_error_t rc;
1500 struct element_list_s *elements = NULL;
1502 elements = xcalloc (1, sizeof (struct element_list_s));
1503 if (!elements)
1504 return GPG_ERR_ENOMEM;
1506 elements->recurse = client->opts & OPT_LIST_RECURSE;
1507 elements->verbose =
1508 (client->opts & OPT_LIST_VERBOSE) | (client->opts & OPT_LIST_WITH_TARGET);
1509 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1511 if (!line || !*line)
1513 struct string_s *str = NULL;
1515 pthread_cleanup_push (list_command_cleanup2, elements);
1516 rc = list_root_elements (client->doc, &str, elements->verbose,
1517 elements->with_target);
1518 pthread_cleanup_pop (1);
1519 pthread_cleanup_push (list_command_cleanup1, str);
1521 if (!rc)
1523 if (client->opts & OPT_LIST_ALL)
1525 char **roots = str_split (str->str, "\n", 0);
1526 char **p;
1528 pthread_cleanup_push (req_cleanup, roots);
1529 string_truncate (str, 0);
1531 for (p = roots; *p; p++)
1533 if (strchr (*p, ' '))
1535 if (has_list_flag (*p, "EO"))
1537 string_append_printf (str, "%s%s", *p,
1538 *(p + 1) ? "\n" : "");
1539 continue;
1543 elements = xcalloc (1, sizeof (struct element_list_s));
1544 if (!elements)
1546 rc = GPG_ERR_ENOMEM;
1547 break;
1550 elements->recurse = client->opts & OPT_LIST_RECURSE;
1551 elements->verbose =
1552 (client->opts & OPT_LIST_VERBOSE) | (client->opts &
1553 OPT_LIST_WITH_TARGET);
1554 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1555 pthread_cleanup_push (list_command_cleanup2, elements);
1556 rc = list_path_once (client, *p, elements, str);
1557 pthread_cleanup_pop (1);
1558 if (rc)
1559 break;
1561 if (*(p + 1))
1562 string_append (str, "\n");
1565 pthread_cleanup_pop (1);
1568 if (!rc)
1569 rc = xfer_data (ctx, str->str, str->len);
1572 pthread_cleanup_pop (1);
1573 return rc;
1576 pthread_cleanup_push (list_command_cleanup2, elements);
1577 struct string_s *str = string_new (NULL);
1578 pthread_cleanup_push (list_command_cleanup1, str);
1579 rc = list_path_once (client, line, elements, str);
1580 if (!rc)
1581 rc = xfer_data (ctx, str->str, str->len);
1583 pthread_cleanup_pop (1);
1584 pthread_cleanup_pop (1);
1585 return rc;
1588 static gpg_error_t
1589 list_command (assuan_context_t ctx, char *line)
1591 struct client_s *client = assuan_get_pointer (ctx);
1592 gpg_error_t rc;
1593 struct argv_s *args[] = {
1594 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG,
1595 parse_list_opt_norecurse},
1596 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_list_opt_verbose},
1597 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1598 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG,
1599 parse_list_opt_target},
1600 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_all},
1601 NULL
1604 if (disable_list_and_dump == 1)
1605 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1607 client->opts |= OPT_LIST_RECURSE;
1608 rc = parse_options (&line, args, client);
1609 if (rc)
1610 return send_error (ctx, rc);
1612 if (client->opts & OPT_LIST_ALL)
1613 client->opts |= OPT_LIST_RECURSE | OPT_LIST_VERBOSE;
1615 if (client->opts & OPT_INQUIRE)
1617 unsigned char *result;
1618 size_t len;
1620 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1621 if (rc)
1622 return send_error (ctx, rc);
1624 line = (char *) result;
1627 rc = do_list (ctx, line);
1629 if (client->opts & OPT_INQUIRE)
1630 xfree (line);
1632 return send_error (ctx, rc);
1636 * req[0] - element path
1638 static gpg_error_t
1639 attribute_list (assuan_context_t ctx, char **req)
1641 struct client_s *client = assuan_get_pointer (ctx);
1642 char **attrlist = NULL;
1643 int i = 0;
1644 char **path = NULL;
1645 xmlAttrPtr a;
1646 xmlNodePtr n, an;
1647 char *line;
1648 gpg_error_t rc;
1650 if (!req || !req[0])
1651 return GPG_ERR_SYNTAX;
1653 if ((path = str_split (req[0], "\t", 0)) == NULL)
1656 * The first argument may be only a root element.
1658 if ((path = str_split (req[0], " ", 0)) == NULL)
1659 return GPG_ERR_SYNTAX;
1662 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
1664 if (!n)
1666 strv_free (path);
1667 return rc;
1670 if (path[1])
1672 n = find_elements (client->doc, n->children, path + 1, &rc,
1673 NULL, NULL, NULL, 0, 0, NULL, 0);
1675 if (!n)
1677 strv_free (path);
1678 return rc;
1682 strv_free (path);
1684 for (a = n->properties; a; a = a->next)
1686 char **pa;
1688 if ((pa = xrealloc (attrlist, (i + 2) * sizeof (char *))) == NULL)
1690 if (attrlist)
1691 strv_free (attrlist);
1693 log_write ("%s(%i): %s", __FILE__, __LINE__,
1694 pwmd_strerror (GPG_ERR_ENOMEM));
1695 return GPG_ERR_ENOMEM;
1698 attrlist = pa;
1699 an = a->children;
1700 attrlist[i] = str_asprintf ("%s %s", (char *) a->name,
1702 && an->content ? (char *) an->content : "");
1704 if (!attrlist[i])
1706 strv_free (attrlist);
1707 log_write ("%s(%i): %s", __FILE__, __LINE__,
1708 pwmd_strerror (GPG_ERR_ENOMEM));
1709 return GPG_ERR_ENOMEM;
1712 attrlist[++i] = NULL;
1715 if (!attrlist)
1716 return GPG_ERR_NO_DATA;
1718 line = strv_join ("\n", attrlist);
1720 if (!line)
1722 log_write ("%s(%i): %s", __FILE__, __LINE__,
1723 pwmd_strerror (GPG_ERR_ENOMEM));
1724 strv_free (attrlist);
1725 return GPG_ERR_ENOMEM;
1728 pthread_cleanup_push (xfree, line);
1729 pthread_cleanup_push (req_cleanup, attrlist);
1730 rc = xfer_data (ctx, line, strlen (line));
1731 pthread_cleanup_pop (1);
1732 pthread_cleanup_pop (1);
1733 return rc;
1737 * req[0] - attribute
1738 * req[1] - element path
1740 static gpg_error_t
1741 attribute_delete (struct client_s *client, char **req)
1743 xmlNodePtr n;
1744 char **path = NULL;
1745 gpg_error_t rc;
1747 if (!req || !req[0] || !req[1])
1748 return GPG_ERR_SYNTAX;
1750 if (!strcmp (req[0], "_name"))
1751 return GPG_ERR_INV_ATTR;
1753 if ((path = str_split (req[1], "\t", 0)) == NULL)
1756 * The first argument may be only a root element.
1758 if ((path = str_split (req[1], " ", 0)) == NULL)
1759 return GPG_ERR_SYNTAX;
1762 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
1763 if (!n)
1764 goto fail;
1766 if (path[1])
1768 n = find_elements (client->doc, n->children, path + 1, &rc,
1769 NULL, NULL, NULL, 0, 0, NULL, 0);
1770 if (!n)
1771 goto fail;
1774 rc = delete_attribute (n, (xmlChar *) req[0]);
1776 fail:
1777 strv_free (path);
1778 return rc;
1781 static xmlNodePtr
1782 create_element_path (struct client_s *client,
1783 char ***elements, gpg_error_t * rc, xmlNodePtr parent)
1785 char **req = *elements;
1786 char **req_orig = strv_dup (req);
1787 xmlNodePtr n = NULL;
1789 *rc = 0;
1791 if (!req_orig)
1793 *rc = GPG_ERR_ENOMEM;
1794 log_write ("%s(%i): %s", __FILE__, __LINE__,
1795 pwmd_strerror (GPG_ERR_ENOMEM));
1796 goto fail;
1799 again:
1800 n = find_root_element (client->doc, &req, rc, NULL, 0, 0);
1801 if (!n)
1803 if (*rc != GPG_ERR_ELEMENT_NOT_FOUND)
1804 goto fail;
1806 *rc = new_root_element (client->doc, req[0]);
1807 if (*rc)
1808 goto fail;
1810 goto again;
1812 else if (n == parent)
1814 *rc = GPG_ERR_CONFLICT;
1815 goto fail;
1818 if (req[1])
1820 if (!n->children)
1821 n = create_target_elements_cb (n, req + 1, rc, NULL);
1822 else
1823 n = find_elements (client->doc, n->children, req + 1, rc, NULL, NULL,
1824 create_target_elements_cb, 0, 0, parent, 0);
1826 if (!n)
1827 goto fail;
1830 * Reset the position of the element tree now that the elements
1831 * have been created.
1833 strv_free (req);
1834 req = req_orig;
1835 req_orig = NULL;
1836 n = find_root_element (client->doc, &req, rc, NULL, 0, 0);
1837 if (!n)
1838 goto fail;
1840 n = find_elements (client->doc, n->children, req + 1, rc,
1841 NULL, NULL, NULL, 0, 0, NULL, 0);
1842 if (!n)
1843 goto fail;
1846 fail:
1847 if (req_orig)
1848 strv_free (req_orig);
1850 *elements = req;
1851 return n;
1855 * Creates a "target" attribute. When other commands encounter an element with
1856 * this attribute, the element path is modified to the target value. If the
1857 * source element path doesn't exist when using 'ATTR SET target', it is
1858 * created, but the destination element path must exist.
1860 * req[0] - source element path
1861 * req[1] - destination element path
1863 static gpg_error_t
1864 target_attribute (struct client_s *client, char **req)
1866 char **src, **dst, *line = NULL, **odst = NULL;
1867 gpg_error_t rc;
1868 xmlNodePtr n;
1870 if (!req || !req[0] || !req[1])
1871 return GPG_ERR_SYNTAX;
1873 if ((src = str_split (req[0], "\t", 0)) == NULL)
1876 * The first argument may be only a root element.
1878 if ((src = str_split (req[0], " ", 0)) == NULL)
1879 return GPG_ERR_SYNTAX;
1882 if (!valid_element_path (src, 0))
1883 return GPG_ERR_INV_VALUE;
1885 if ((dst = str_split (req[1], "\t", 0)) == NULL)
1888 * The first argument may be only a root element.
1890 if ((dst = str_split (req[1], " ", 0)) == NULL)
1892 rc = GPG_ERR_SYNTAX;
1893 goto fail;
1897 odst = strv_dup (dst);
1898 if (!odst)
1900 rc = GPG_ERR_ENOMEM;
1901 goto fail;
1904 n = find_root_element (client->doc, &dst, &rc, NULL, 0, 0);
1906 * Make sure the destination element path exists.
1908 if (!n)
1909 goto fail;
1911 if (dst[1])
1913 n = find_elements (client->doc, n->children, dst + 1, &rc,
1914 NULL, NULL, NULL, 0, 0, NULL, 0);
1915 if (!n)
1916 goto fail;
1919 rc = validate_target_attribute (client->doc, req[0], n);
1920 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
1921 goto fail;
1923 n = create_element_path (client, &src, &rc, NULL);
1924 if (rc)
1925 goto fail;
1927 line = strv_join ("\t", odst);
1928 if (!line)
1930 rc = GPG_ERR_ENOMEM;
1931 goto fail;
1934 rc = add_attribute (n, "target", line);
1936 fail:
1937 xfree (line);
1938 strv_free (src);
1939 strv_free (dst);
1940 strv_free (odst);
1941 return rc;
1945 * req[0] - attribute
1946 * req[1] - element path
1948 static gpg_error_t
1949 attribute_get (assuan_context_t ctx, char **req)
1951 struct client_s *client = assuan_get_pointer (ctx);
1952 xmlNodePtr n;
1953 xmlChar *a;
1954 char **path = NULL;
1955 gpg_error_t rc;
1957 if (!req || !req[0] || !req[1])
1958 return GPG_ERR_SYNTAX;
1960 if (strchr (req[1], '\t'))
1962 if ((path = str_split (req[1], "\t", 0)) == NULL)
1963 return GPG_ERR_SYNTAX;
1965 else
1967 if ((path = str_split (req[1], " ", 0)) == NULL)
1968 return GPG_ERR_SYNTAX;
1971 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
1973 if (!n)
1974 goto fail;
1976 if (path[1])
1978 n = find_elements (client->doc, n->children, path + 1, &rc,
1979 NULL, NULL, NULL, 0, 0, NULL, 0);
1981 if (!n)
1982 goto fail;
1985 strv_free (path);
1987 if ((a = xmlGetProp (n, (xmlChar *) req[0])) == NULL)
1988 return GPG_ERR_NOT_FOUND;
1990 pthread_cleanup_push (xmlFree, a);
1992 if (*a)
1993 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
1994 else
1995 rc = GPG_ERR_NO_DATA;
1997 pthread_cleanup_pop (1);
1998 return rc;
2000 fail:
2001 strv_free (path);
2002 return rc;
2006 * req[0] - attribute
2007 * req[1] - element path
2008 * req[2] - value
2010 static gpg_error_t
2011 attribute_set (struct client_s *client, char **req)
2013 char **path = NULL;
2014 gpg_error_t rc;
2015 xmlNodePtr n;
2017 if (!req || !req[0] || !req[1])
2018 return GPG_ERR_SYNTAX;
2021 * Reserved attribute names.
2023 if (!strcmp (req[0], "_name"))
2024 return GPG_ERR_INV_ATTR;
2025 else if (!strcmp (req[0], "target"))
2026 return target_attribute (client, req + 1);
2028 if ((path = str_split (req[1], "\t", 0)) == NULL)
2031 * The first argument may be only a root element.
2033 if ((path = str_split (req[1], " ", 0)) == NULL)
2034 return GPG_ERR_SYNTAX;
2037 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
2039 if (!n)
2040 goto fail;
2042 if (path[1])
2044 n = find_elements (client->doc, n->children, path + 1, &rc,
2045 NULL, NULL, NULL, 0, 0, NULL, 0);
2047 if (!n)
2048 goto fail;
2051 rc = add_attribute (n, req[0], req[2]);
2053 fail:
2054 strv_free (path);
2055 return rc;
2059 * req[0] - command
2060 * req[1] - attribute name or element path if command is LIST
2061 * req[2] - element path
2062 * req[2] - element path or value
2065 static gpg_error_t
2066 do_attr (assuan_context_t ctx, char *line)
2068 struct client_s *client = assuan_get_pointer (ctx);
2069 gpg_error_t rc = 0;
2070 char **req;
2072 req = str_split (line, " ", 4);
2073 if (!req || !req[0] || !req[1])
2075 strv_free (req);
2076 return GPG_ERR_SYNTAX;
2079 pthread_cleanup_push (req_cleanup, req);
2081 if (strcasecmp (req[0], "SET") == 0)
2082 rc = attribute_set (client, req + 1);
2083 else if (strcasecmp (req[0], "GET") == 0)
2084 rc = attribute_get (ctx, req + 1);
2085 else if (strcasecmp (req[0], "DELETE") == 0)
2086 rc = attribute_delete (client, req + 1);
2087 else if (strcasecmp (req[0], "LIST") == 0)
2088 rc = attribute_list (ctx, req + 1);
2089 else
2090 rc = GPG_ERR_SYNTAX;
2092 pthread_cleanup_pop (1);
2093 return rc;
2096 static gpg_error_t
2097 attr_command (assuan_context_t ctx, char *line)
2099 struct client_s *client = assuan_get_pointer (ctx);
2100 gpg_error_t rc;
2101 struct argv_s *args[] = {
2102 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2103 NULL
2106 rc = parse_options (&line, args, client);
2107 if (rc)
2108 return send_error (ctx, rc);
2110 if (client->opts & OPT_INQUIRE)
2112 unsigned char *result;
2113 size_t len;
2115 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2116 if (rc)
2117 return send_error (ctx, rc);
2119 line = (char *) result;
2122 rc = do_attr (ctx, line);
2124 if (client->opts & OPT_INQUIRE)
2125 xfree (line);
2127 return send_error (ctx, rc);
2130 static gpg_error_t
2131 parse_iscached_opt_lock (void *data, void *value)
2133 struct client_s *client = data;
2135 (void) value;
2136 client->opts |= OPT_LOCK;
2137 return 0;
2140 static gpg_error_t
2141 iscached_command (assuan_context_t ctx, char *line)
2143 struct client_s *client = assuan_get_pointer (ctx);
2144 gpg_error_t rc;
2145 unsigned char md5file[16];
2146 struct argv_s *args[] = {
2147 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2148 NULL
2151 if (!line || !*line)
2152 return send_error (ctx, GPG_ERR_SYNTAX);
2154 rc = parse_options (&line, args, client);
2155 if (rc)
2156 return send_error (ctx, rc);
2157 else if (!valid_filename (line))
2158 return send_error (ctx, GPG_ERR_INV_VALUE);
2160 rc = cache_iscached (line, NULL);
2161 if (client->opts & OPT_LOCK
2162 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2164 gpg_error_t trc = rc;
2165 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2166 if (memcmp (md5file, client->md5file, 16))
2167 cleanup_client (client);
2169 memcpy (client->md5file, md5file, 16);
2170 rc = do_lock (client, 1);
2171 if (!rc)
2172 rc = trc;
2175 return send_error (ctx, rc);
2178 static gpg_error_t
2179 clearcache_command (assuan_context_t ctx, char *line)
2181 gpg_error_t rc = 0, all_rc = 0;
2182 unsigned char md5file[16];
2183 int i;
2184 int t;
2185 int all = 0;
2186 struct client_thread_s *once = NULL;
2188 cache_lock ();
2189 MUTEX_LOCK (&cn_mutex);
2190 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
2192 if (!line || !*line)
2193 all = 1;
2195 t = slist_length (cn_thread_list);
2197 for (i = 0; i < t; i++)
2199 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2201 if (!thd->cl)
2202 continue;
2204 /* Lock each connected clients' file mutex to prevent any other client
2205 * from accessing the cache entry (the file mutex is locked upon
2206 * command startup). The cache for the entry is not cleared if the
2207 * file mutex is locked by another client to prevent this function
2208 * from blocking.
2210 if (all)
2212 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
2213 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2215 if (pthread_equal (pthread_self (), thd->tid))
2216 rc = 0;
2217 else
2219 if (!thd->cl->filename ||
2220 cache_iscached (thd->cl->filename,
2221 NULL) == GPG_ERR_NO_DATA)
2223 rc = 0;
2224 continue;
2227 cache_defer_clear (thd->cl->md5file);
2230 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2232 rc = 0;
2233 continue;
2236 if (!rc)
2238 rc = cache_clear (thd->cl->md5file);
2239 if (rc)
2240 all_rc = rc;
2242 cache_unlock_mutex (thd->cl->md5file, 0);
2244 else
2245 all_rc = rc;
2247 rc = 0;
2249 /* A single data filename was specified. Lock only this data file
2250 * mutex and free the cache entry. */
2251 else
2253 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2255 if (!memcmp (thd->cl->md5file, md5file, sizeof (md5file)))
2257 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0,
2258 -1);
2259 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2261 if (pthread_equal (pthread_self (), thd->tid))
2262 rc = 0;
2265 if (!rc)
2267 once = thd;
2268 rc = cache_clear (thd->cl->md5file);
2269 cache_unlock_mutex (thd->cl->md5file, 0);
2271 else
2273 cache_defer_clear (thd->cl->md5file);
2276 break;
2281 /* Only connected clients' cache entries have been cleared. Now clear any
2282 * remaining cache entries without clients but only if there wasn't an
2283 * error from above since this would defeat the locking check of the
2284 * remaining entries. */
2285 if (!all_rc && all)
2287 cache_clear (NULL);
2290 /* No clients are using the specified file. */
2291 else if (!all_rc && !rc && !once)
2293 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2294 rc = cache_clear (md5file);
2297 /* Release the connection mutex. */
2298 pthread_cleanup_pop (1);
2299 cache_unlock ();
2301 if (!rc)
2302 send_status_all (STATUS_CACHE, NULL);
2304 /* One or more files were locked while clearing all cache entries. */
2305 if (all_rc)
2306 rc = all_rc;
2308 return send_error (ctx, rc);
2311 static gpg_error_t
2312 cachetimeout_command (assuan_context_t ctx, char *line)
2314 unsigned char md5file[16];
2315 int timeout;
2316 char **req = str_split (line, " ", 0);
2317 char *p;
2318 gpg_error_t rc = 0;
2320 if (!req || !*req || !req[1])
2322 strv_free (req);
2323 return send_error (ctx, GPG_ERR_SYNTAX);
2326 errno = 0;
2327 timeout = (int) strtol (req[1], &p, 10);
2328 if (errno != 0 || *p || timeout < -1)
2330 strv_free (req);
2331 return send_error (ctx, GPG_ERR_SYNTAX);
2334 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, req[0], strlen (req[0]));
2335 rc = cache_set_timeout (md5file, timeout);
2336 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2338 rc = 0;
2339 MUTEX_LOCK (&rcfile_mutex);
2340 config_set_int_param (&global_config, req[0], "cache_timeout", req[1]);
2341 MUTEX_UNLOCK (&rcfile_mutex);
2344 strv_free (req);
2345 return send_error (ctx, rc);
2348 static gpg_error_t
2349 dump_command (assuan_context_t ctx, char *line)
2351 xmlChar *xml;
2352 int len;
2353 struct client_s *client = assuan_get_pointer (ctx);
2354 gpg_error_t rc;
2356 if (disable_list_and_dump == 1)
2357 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2359 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2361 if (!xml)
2363 log_write ("%s(%i): %s", __FILE__, __LINE__,
2364 pwmd_strerror (GPG_ERR_ENOMEM));
2365 return send_error (ctx, GPG_ERR_ENOMEM);
2368 pthread_cleanup_push (xmlFree, xml);
2369 rc = xfer_data (ctx, (char *) xml, len);
2370 pthread_cleanup_pop (1);
2371 return send_error (ctx, rc);
2374 static gpg_error_t
2375 getconfig_command (assuan_context_t ctx, char *line)
2377 struct client_s *client = assuan_get_pointer (ctx);
2378 gpg_error_t rc = 0;
2379 char filename[255] = { 0 }, param[747] =
2382 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2384 if (!line || !*line)
2385 return send_error (ctx, GPG_ERR_SYNTAX);
2387 if (strchr (line, ' '))
2389 sscanf (line, " %254[^ ] %746c", filename, param);
2390 paramp = param;
2391 fp = filename;
2394 if (fp && !valid_filename (fp))
2395 return send_error (ctx, GPG_ERR_INV_VALUE);
2397 paramp = str_down (paramp);
2398 if (!strcmp (paramp, "cipher") && fp)
2400 struct crypto_s *crypto = NULL;
2402 rc = init_client_crypto (&crypto);
2403 if (!rc)
2405 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2406 if (!rc)
2408 const char *t =
2409 gcry_cipher_algo_name (cipher_to_gcrypt (crypto->hdr.flags));
2410 if (t)
2412 tmp = str_dup (t);
2413 if (tmp)
2414 str_down (tmp);
2419 UPDATE_AGENT_CTX (client, crypto);
2420 cleanup_crypto (&crypto);
2421 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2422 return send_error (ctx, rc);
2424 if (!rc && tmp)
2425 goto done;
2427 else if (!strcmp (paramp, "cipher_iterations") && fp)
2429 struct crypto_s *crypto = NULL;
2431 rc = init_client_crypto (&crypto);
2432 if (!rc)
2434 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2435 if (!rc)
2437 tmp = str_asprintf ("%llu",
2438 (unsigned long long) crypto->hdr.
2439 iterations);
2440 if (!tmp)
2441 rc = GPG_ERR_ENOMEM;
2445 UPDATE_AGENT_CTX (client, crypto);
2446 cleanup_crypto (&crypto);
2447 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2448 return send_error (ctx, rc);
2450 if (!rc && tmp)
2451 goto done;
2453 else if (!strcmp (paramp, "passphrase"))
2454 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2456 p = config_get_value (fp ? fp : "global", paramp);
2457 if (!p)
2458 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2460 tmp = expand_homedir (p);
2461 xfree (p);
2462 if (!tmp)
2464 log_write ("%s(%i): %s", __FILE__, __LINE__,
2465 pwmd_strerror (GPG_ERR_ENOMEM));
2466 return send_error (ctx, GPG_ERR_ENOMEM);
2469 done:
2470 p = tmp;
2471 pthread_cleanup_push (xfree, p);
2472 rc = xfer_data (ctx, p, strlen (p));
2473 pthread_cleanup_pop (1);
2474 return send_error (ctx, rc);
2477 struct xpath_s
2479 xmlXPathContextPtr xp;
2480 xmlXPathObjectPtr result;
2481 xmlBufferPtr buf;
2482 char **req;
2485 static void
2486 xpath_command_cleanup (void *arg)
2488 struct xpath_s *xpath = arg;
2490 if (!xpath)
2491 return;
2493 req_cleanup (xpath->req);
2495 if (xpath->buf)
2496 xmlBufferFree (xpath->buf);
2498 if (xpath->result)
2499 xmlXPathFreeObject (xpath->result);
2501 if (xpath->xp)
2502 xmlXPathFreeContext (xpath->xp);
2505 static gpg_error_t
2506 do_xpath (assuan_context_t ctx, char *line)
2508 gpg_error_t rc;
2509 struct client_s *client = assuan_get_pointer (ctx);
2510 struct xpath_s _x = { 0 };
2511 struct xpath_s *xpath = &_x;
2513 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2515 if (strv_printf (&xpath->req, "%s", line) == 0)
2516 return GPG_ERR_ENOMEM;
2519 xpath->xp = xmlXPathNewContext (client->doc);
2520 if (!xpath->xp)
2522 rc = GPG_ERR_BAD_DATA;
2523 goto fail;
2526 xpath->result =
2527 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2528 if (!xpath->result)
2530 rc = GPG_ERR_BAD_DATA;
2531 goto fail;
2534 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2536 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2537 goto fail;
2540 rc = recurse_xpath_nodeset (client->doc, xpath->result->nodesetval,
2541 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2542 NULL);
2543 if (rc)
2544 goto fail;
2545 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2547 rc = GPG_ERR_NO_DATA;
2548 goto fail;
2550 else if (xpath->req[1])
2552 rc = 0;
2553 goto fail;
2556 pthread_cleanup_push (xpath_command_cleanup, &xpath);
2557 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2558 xmlBufferLength (xpath->buf));
2559 pthread_cleanup_pop (0);
2560 fail:
2561 xpath_command_cleanup (xpath);
2562 return rc;
2565 static gpg_error_t
2566 xpath_command (assuan_context_t ctx, char *line)
2568 struct client_s *client = assuan_get_pointer (ctx);
2569 gpg_error_t rc;
2570 struct argv_s *args[] = {
2571 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2572 NULL
2575 if (disable_list_and_dump == 1)
2576 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2578 rc = parse_options (&line, args, client);
2579 if (rc)
2580 return send_error (ctx, rc);
2582 if (client->opts & OPT_INQUIRE)
2584 unsigned char *result;
2585 size_t len;
2587 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2588 if (rc)
2589 return send_error (ctx, rc);
2591 line = (char *) result;
2594 if (!line || !*line)
2595 rc = GPG_ERR_SYNTAX;
2597 if (!rc)
2598 rc = do_xpath (ctx, line);
2600 if (client->opts & OPT_INQUIRE)
2601 xfree (line);
2603 return send_error (ctx, rc);
2606 static gpg_error_t
2607 do_xpathattr (assuan_context_t ctx, char *line)
2609 struct client_s *client = assuan_get_pointer (ctx);
2610 gpg_error_t rc;
2611 char **req = NULL;
2612 int cmd = 0; //SET
2613 struct xpath_s _x = { 0 };
2614 struct xpath_s *xpath = &_x;
2616 if ((req = str_split (line, " ", 3)) == NULL)
2617 return GPG_ERR_ENOMEM;
2619 if (!req[0])
2621 rc = GPG_ERR_SYNTAX;
2622 goto fail;
2625 if (!strcasecmp (req[0], "SET"))
2626 cmd = 0;
2627 else if (!strcasecmp (req[0], "DELETE"))
2628 cmd = 1;
2629 else
2631 rc = GPG_ERR_SYNTAX;
2632 goto fail;
2635 if (!req[1] || !req[2])
2637 rc = GPG_ERR_SYNTAX;
2638 goto fail;
2641 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2643 rc = GPG_ERR_ENOMEM;
2644 goto fail;
2647 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2649 rc = GPG_ERR_SYNTAX;
2650 goto fail;
2653 xpath->xp = xmlXPathNewContext (client->doc);
2654 if (!xpath->xp)
2656 rc = GPG_ERR_BAD_DATA;
2657 goto fail;
2660 xpath->result =
2661 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2662 if (!xpath->result)
2664 rc = GPG_ERR_BAD_DATA;
2665 goto fail;
2668 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2670 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2671 goto fail;
2674 rc = recurse_xpath_nodeset (client->doc, xpath->result->nodesetval,
2675 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2676 (xmlChar *) req[1]);
2678 fail:
2679 xpath_command_cleanup (xpath);
2680 strv_free (req);
2681 return rc;
2684 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2685 static gpg_error_t
2686 xpathattr_command (assuan_context_t ctx, char *line)
2688 struct client_s *client = assuan_get_pointer (ctx);
2689 gpg_error_t rc;
2690 struct argv_s *args[] = {
2691 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2692 NULL
2695 if (disable_list_and_dump == 1)
2696 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2698 rc = parse_options (&line, args, client);
2699 if (rc)
2700 return send_error (ctx, rc);
2702 if (client->opts & OPT_INQUIRE)
2704 unsigned char *result;
2705 size_t len;
2707 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2708 if (rc)
2709 return send_error (ctx, rc);
2711 line = (char *) result;
2714 if (!line || !*line)
2715 rc = GPG_ERR_SYNTAX;
2717 if (!rc)
2718 rc = do_xpathattr (ctx, line);
2720 if (client->opts & OPT_INQUIRE)
2721 xfree (line);
2723 return send_error (ctx, rc);
2726 static gpg_error_t
2727 do_import (struct client_s *client, unsigned char *line)
2729 char **req, **path = NULL, **path_orig = NULL, *content;
2730 xmlDocPtr doc = NULL;
2731 xmlNodePtr n, root, copy;
2732 gpg_error_t rc;
2734 req = str_split ((char *) line, "\t", 2);
2735 xfree (line);
2736 if (!req || !*req)
2737 return GPG_ERR_SYNTAX;
2739 content = req[0];
2740 path = str_split (req[1], "\t", 0);
2741 if (!content || !*content)
2743 rc = GPG_ERR_SYNTAX;
2744 goto fail;
2747 if (path && !valid_element_path (path, 0))
2749 rc = GPG_ERR_INV_VALUE;
2750 goto fail;
2753 doc = xmlReadDoc ((xmlChar *) content, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2754 if (!doc)
2756 rc = GPG_ERR_BAD_DATA;
2757 goto fail;
2760 root = xmlDocGetRootElement (doc);
2761 rc = validate_import (root);
2762 if (rc)
2763 goto fail;
2765 if (path)
2767 path_orig = strv_dup (path);
2768 if (!path_orig)
2770 log_write ("%s(%i): %s", __FILE__, __LINE__,
2771 pwmd_strerror (GPG_ERR_ENOMEM));
2772 rc = GPG_ERR_ENOMEM;
2773 goto fail;
2776 xmlChar *a = xmlGetProp (root, (xmlChar *) "_name");
2777 if (!a)
2779 strv_free (path_orig);
2780 rc = GPG_ERR_ENOMEM;
2781 goto fail;
2784 if (strv_printf (&path, "%s", (char *) a) == 0)
2786 xmlFree (a);
2787 strv_free (path_orig);
2788 rc = GPG_ERR_ENOMEM;
2789 goto fail;
2792 xmlFree (a);
2793 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
2795 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2797 strv_free (path_orig);
2798 goto fail;
2801 if (!rc)
2804 find_elements (client->doc, n->children, path + 1, &rc, NULL,
2805 NULL, NULL, 0, 0, NULL, 1);
2807 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2809 strv_free (path_orig);
2810 goto fail;
2812 else if (!rc)
2814 xmlNodePtr parent = n->parent;
2816 xmlUnlinkNode (n);
2817 xmlFreeNode (n);
2818 n = parent;
2822 strv_free (path);
2823 path = path_orig;
2825 if (rc == GPG_ERR_ELEMENT_NOT_FOUND)
2827 n = create_element_path (client, &path, &rc, NULL);
2829 if (rc)
2830 goto fail;
2833 copy = xmlCopyNodeList (root);
2834 n = xmlAddChildList (n, copy);
2835 if (!n)
2836 rc = GPG_ERR_BAD_DATA;
2838 else
2840 /* Check if the content root element can create a DTD root element. */
2841 if (!xmlStrEqual ((xmlChar *) "element", root->name))
2843 rc = GPG_ERR_SYNTAX;
2844 goto fail;
2847 xmlChar *a;
2849 if ((a = xmlGetProp (root, (xmlChar *) "_name")) == NULL)
2851 rc = GPG_ERR_SYNTAX;
2852 goto fail;
2855 char *tmp = str_dup ((char *) a);
2856 xmlFree (a);
2857 int literal = is_literal_element (&tmp);
2859 if (!valid_xml_element ((xmlChar *) tmp) || literal)
2861 xfree (tmp);
2862 rc = GPG_ERR_INV_VALUE;
2863 goto fail;
2866 if (strv_printf (&path, "%s", tmp) == 0)
2868 xfree (tmp);
2869 rc = GPG_ERR_ENOMEM;
2870 goto fail;
2873 xfree (tmp);
2874 n = find_root_element (client->doc, &path, &rc, NULL, 0, 1);
2876 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2878 rc = GPG_ERR_BAD_DATA;
2879 goto fail;
2882 /* Overwriting the existing tree. */
2883 if (!rc)
2885 xmlUnlinkNode (n);
2886 xmlFreeNodeList (n);
2889 rc = 0;
2890 xmlSetProp (root, (xmlChar *) "_name", (xmlChar *) path[0]);
2891 n = xmlCopyNode (root, 1);
2892 n = xmlAddChildList (xmlDocGetRootElement (client->doc), n);
2895 if (n && !rc)
2896 rc = update_element_mtime (n->parent);
2898 fail:
2899 if (doc)
2900 xmlFreeDoc (doc);
2902 if (path)
2903 strv_free (path);
2905 strv_free (req);
2906 return rc;
2909 static gpg_error_t
2910 import_command (assuan_context_t ctx, char *line)
2912 gpg_error_t rc;
2913 struct client_s *client = assuan_get_pointer (ctx);
2914 unsigned char *result;
2915 size_t len;
2917 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2918 if (rc)
2919 return send_error (ctx, rc);
2921 rc = do_import (client, result);
2922 return send_error (ctx, rc);
2925 static gpg_error_t
2926 do_lock (struct client_s *client, int add)
2928 gpg_error_t rc = lock_file_mutex (client, add);
2930 if (!rc)
2931 client->flags |= FLAG_LOCK_CMD;
2933 return rc;
2936 static gpg_error_t
2937 lock_command (assuan_context_t ctx, char *line)
2939 struct client_s *client = assuan_get_pointer (ctx);
2940 gpg_error_t rc = do_lock (client, 0);
2942 return send_error (ctx, rc);
2945 static gpg_error_t
2946 unlock_command (assuan_context_t ctx, char *line)
2948 struct client_s *client = assuan_get_pointer (ctx);
2949 gpg_error_t rc;
2951 rc = unlock_file_mutex (client, 0);
2952 return send_error (ctx, rc);
2955 static gpg_error_t
2956 option_command (assuan_context_t ctx, char *line)
2958 struct client_s *client = assuan_get_pointer (ctx);
2959 gpg_error_t rc = 0;
2960 struct pinentry_option_s *pin_opts = &client->pinentry_opts;
2961 #ifdef WITH_AGENT
2962 struct agent_s *agent = client->crypto->agent;
2963 #endif
2964 char namebuf[255] = { 0 };
2965 char *name = namebuf;
2966 char *value = NULL, *p;
2968 p = strchr (line, '=');
2969 if (!p)
2970 strncpy (namebuf, line, sizeof(namebuf));
2971 else
2973 strncpy (namebuf, line, strlen (line)-strlen (p));
2974 value = p+1;
2977 log_write1 ("OPTION name='%s' value='%s'", name, value);
2979 if (strcasecmp (name, (char *) "log_level") == 0)
2981 long l = 0;
2983 if (value)
2985 l = strtol (value, NULL, 10);
2987 if (l < 0 || l > 2)
2988 return send_error (ctx, GPG_ERR_INV_VALUE);
2991 MUTEX_LOCK (&rcfile_mutex);
2992 config_set_int_param (&global_config, "global", "log_level", value);
2993 MUTEX_UNLOCK (&rcfile_mutex);
2994 goto done;
2996 else if (strcasecmp (name, (char *) "lock-timeout") == 0)
2998 long n = 0;
2999 char *p = NULL;
3001 if (value)
3003 n = strtol (value, &p, 10);
3004 if (p && *p)
3005 return send_error (ctx, GPG_ERR_INV_VALUE);
3008 client->lock_timeout = n;
3009 goto done;
3011 else if (strcasecmp (name, (char *) "NAME") == 0)
3013 char *tmp = pthread_getspecific (thread_name_key);
3015 if (tmp)
3016 xfree (tmp);
3018 if (!value || !*value)
3020 pthread_setspecific (thread_name_key, str_dup (""));
3021 goto done;
3024 pthread_setspecific (thread_name_key, str_dup (value));
3025 goto done;
3027 else if (strcasecmp (name, (char *) "lc-messages") == 0)
3029 xfree (pin_opts->lc_messages);
3030 pin_opts->lc_messages = NULL;
3031 if (value && *value)
3032 pin_opts->lc_messages = str_dup (value);
3033 #ifdef WITH_AGENT
3034 if (use_agent)
3035 rc = set_agent_option (client->crypto->agent, "lc-messages", value);
3036 #endif
3038 else if (strcasecmp (name, (char *) "lc-ctype") == 0)
3040 xfree (pin_opts->lc_ctype);
3041 pin_opts->lc_ctype = NULL;
3042 if (value && *value)
3043 pin_opts->lc_ctype = str_dup (value);
3044 #ifdef WITH_AGENT
3045 if (use_agent)
3046 rc = set_agent_option (client->crypto->agent, "lc-ctype", value);
3047 #endif
3049 else if (strcasecmp (name, (char *) "ttyname") == 0)
3051 xfree (pin_opts->ttyname);
3052 pin_opts->ttyname = NULL;
3053 if (value && *value)
3054 pin_opts->ttyname = str_dup (value);
3055 #ifdef WITH_AGENT
3056 if (use_agent)
3057 rc = set_agent_option (client->crypto->agent, "ttyname", value);
3058 #endif
3060 else if (strcasecmp (name, (char *) "ttytype") == 0)
3062 xfree (pin_opts->ttytype);
3063 pin_opts->ttytype = NULL;
3064 if (value && *value)
3065 pin_opts->ttytype = str_dup (value);
3066 #ifdef WITH_AGENT
3067 if (use_agent)
3068 rc = set_agent_option (client->crypto->agent, "ttytype", value);
3069 #endif
3071 else if (strcasecmp (name, (char *) "display") == 0)
3073 xfree (pin_opts->display);
3074 pin_opts->display = NULL;
3075 if (value && *value)
3076 pin_opts->display = str_dup (value);
3077 #ifdef WITH_AGENT
3078 if (use_agent)
3079 rc = set_agent_option (client->crypto->agent, "display", value);
3080 #endif
3082 else if (strcasecmp (name, (char *) "pinentry-desc") == 0)
3084 xfree (pin_opts->desc);
3085 pin_opts->desc = NULL;
3086 if (value && *value)
3087 pin_opts->desc = str_dup (value);
3089 else if (strcasecmp (name, (char *) "pinentry-title") == 0)
3091 xfree (pin_opts->title);
3092 pin_opts->title = NULL;
3093 if (value && *value)
3094 pin_opts->title = str_dup (value);
3096 else if (strcasecmp (name, (char *) "pinentry-prompt") == 0)
3098 xfree (pin_opts->prompt);
3099 pin_opts->prompt = NULL;
3100 if (value && *value)
3101 pin_opts->prompt = str_dup (value);
3104 else if (strcasecmp (name, "pinentry-timeout") == 0)
3106 char *p = NULL;
3107 int n;
3109 if (!value)
3110 goto done;
3112 n = (int) strtol (value, &p, 10);
3114 if (*p || n < 0)
3115 return send_error (ctx, GPG_ERR_INV_VALUE);
3117 pin_opts->timeout = n;
3118 MUTEX_LOCK (&rcfile_mutex);
3119 config_set_int_param (&global_config,
3120 client->filename ? client->filename : "global",
3121 "pinentry_timeout", value);
3122 MUTEX_UNLOCK (&rcfile_mutex);
3123 goto done;
3125 else if (strcasecmp (name, "disable-pinentry") == 0)
3127 char *p = NULL;
3128 int n = 1;
3130 if (value && *value)
3132 n = (int) strtol (value, &p, 10);
3133 if (*p || n < 0 || n > 1)
3134 return send_error (ctx, GPG_ERR_INV_VALUE);
3137 if (n)
3138 client->flags |= FLAG_NO_PINENTRY;
3139 else
3140 client->flags &= ~FLAG_NO_PINENTRY;
3142 else
3143 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
3145 done:
3146 #ifdef WITH_AGENT
3147 if (!rc && use_agent && agent)
3149 rc = pinentry_merge_options (&client->crypto->agent->pinentry_opts,
3150 pin_opts);
3152 #endif
3154 return send_error (ctx, rc);
3157 static gpg_error_t
3158 do_rename (assuan_context_t ctx, char *line)
3160 struct client_s *client = assuan_get_pointer (ctx);
3161 gpg_error_t rc;
3162 char **req, **src, *dst;
3163 xmlNodePtr n, ndst;
3165 req = str_split (line, " ", 0);
3167 if (!req || !req[0] || !req[1])
3169 strv_free (req);
3170 return GPG_ERR_SYNTAX;
3173 dst = req[1];
3174 is_literal_element (&dst);
3176 if (!valid_xml_element ((xmlChar *) dst))
3178 strv_free (req);
3179 return GPG_ERR_INV_VALUE;
3182 if (strchr (req[0], '\t'))
3183 src = str_split (req[0], "\t", 0);
3184 else
3185 src = str_split (req[0], " ", 0);
3187 if (!src || !*src)
3189 rc = GPG_ERR_SYNTAX;
3190 goto fail;
3193 n = find_root_element (client->doc, &src, &rc, NULL, 0, 0);
3194 if (src[1] && n)
3195 n = find_elements (client->doc, n->children, src + 1, &rc, NULL, NULL,
3196 NULL, 0, 0, NULL, 0);
3198 if (!n)
3199 goto fail;
3201 xmlChar *a = xmlGetProp (n, (xmlChar *) "_name");
3202 if (!a)
3204 rc = GPG_ERR_ENOMEM;
3205 goto fail;
3208 /* To prevent unwanted effects:
3210 * <root name="a"><b/></root>
3212 * RENAME a<TAB>b b
3214 if (xmlStrEqual (a, (xmlChar *) dst))
3216 xmlFree (a);
3217 rc = GPG_ERR_AMBIGUOUS_NAME;
3218 goto fail;
3221 xmlFree (a);
3222 char **tmp = NULL;
3223 if (src[1])
3225 char **p;
3227 for (p = src; *p; p++)
3229 if (!*(p + 1))
3230 break;
3231 strv_printf (&tmp, "%s", *p);
3235 strv_printf (&tmp, "!%s", dst);
3236 ndst = find_root_element (client->doc, &tmp, &rc, NULL, 0, 0);
3237 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3239 strv_free (tmp);
3240 goto fail;
3243 if (tmp[1] && ndst)
3244 ndst = find_elements (client->doc, ndst->children, tmp + 1, &rc, NULL,
3245 NULL, NULL, 0, 0, NULL, 0);
3247 strv_free (tmp);
3248 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3249 goto fail;
3251 rc = 0;
3253 /* Target may exist:
3255 * <root name="a"/>
3256 * <root name="b" target="a"/>
3258 * RENAME b a
3260 * Would need to do:
3261 * RENAME !b a
3263 if (ndst == n)
3265 rc = GPG_ERR_AMBIGUOUS_NAME;
3266 goto fail;
3269 if (ndst)
3271 unlink_node (ndst);
3272 xmlFreeNodeList (ndst);
3275 rc = add_attribute (n, "_name", dst);
3277 fail:
3278 strv_free (req);
3279 strv_free (src);
3280 return rc;
3283 static gpg_error_t
3284 rename_command (assuan_context_t ctx, char *line)
3286 struct client_s *client = assuan_get_pointer (ctx);
3287 gpg_error_t rc;
3288 struct argv_s *args[] = {
3289 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3290 NULL
3293 rc = parse_options (&line, args, client);
3294 if (rc)
3295 return send_error (ctx, rc);
3297 if (client->opts & OPT_INQUIRE)
3299 unsigned char *result;
3300 size_t len;
3302 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3303 if (rc)
3304 return send_error (ctx, rc);
3306 line = (char *) result;
3309 rc = do_rename (ctx, line);
3311 if (client->opts & OPT_INQUIRE)
3312 xfree (line);
3314 return send_error (ctx, rc);
3317 static gpg_error_t
3318 do_copy (assuan_context_t ctx, char *line)
3320 struct client_s *client = assuan_get_pointer (ctx);
3321 gpg_error_t rc;
3322 char **req, **src = NULL, **dst = NULL;
3323 xmlNodePtr nsrc, ndst, new = NULL;
3325 req = str_split (line, " ", 0);
3326 if (!req || !req[0] || !req[1])
3328 strv_free (req);
3329 return GPG_ERR_SYNTAX;
3332 if (strchr (req[0], '\t'))
3333 src = str_split (req[0], "\t", 0);
3334 else
3335 src = str_split (req[0], " ", 0);
3337 if (!src || !*src)
3339 rc = GPG_ERR_SYNTAX;
3340 goto fail;
3343 if (strchr (req[1], '\t'))
3344 dst = str_split (req[1], "\t", 0);
3345 else
3346 dst = str_split (req[1], " ", 0);
3348 if (!dst || !*dst)
3350 rc = GPG_ERR_SYNTAX;
3351 goto fail;
3354 if (!valid_element_path (dst, 0))
3356 rc = GPG_ERR_INV_VALUE;
3357 goto fail;
3360 nsrc = find_root_element (client->doc, &src, &rc, NULL, 0, 0);
3361 if (nsrc && src[1])
3362 nsrc = find_elements (client->doc, nsrc->children, src + 1, &rc, NULL,
3363 NULL, NULL, 0, 0, NULL, 0);
3365 if (!nsrc)
3366 goto fail;
3368 new = xmlCopyNodeList (nsrc);
3369 if (!new)
3371 rc = GPG_ERR_ENOMEM;
3372 goto fail;
3375 int create = 0;
3376 ndst = find_root_element (client->doc, &dst, &rc, NULL, 0, 0);
3377 if (ndst && dst[1])
3379 if (ndst->children)
3380 ndst = find_elements (client->doc, ndst->children, dst + 1, &rc, NULL,
3381 NULL, create_target_elements_cb, 0, 0, NULL, 0);
3382 else
3383 create = 1;
3385 else
3386 create = 1;
3388 if (!ndst && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3389 goto fail;
3390 else if (create)
3392 ndst = create_element_path (client, &dst, &rc, NULL);
3393 if (!ndst)
3394 goto fail;
3397 /* Merge any attributes from the src node to the initial dst node. */
3398 for (xmlAttrPtr attr = new->properties; attr; attr = attr->next)
3400 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3401 continue;
3403 xmlAttrPtr a = xmlHasProp (ndst, attr->name);
3404 if (a)
3405 xmlRemoveProp (a);
3407 xmlChar *tmp = xmlNodeGetContent (attr->children);
3408 xmlNewProp (ndst, attr->name, tmp);
3409 xmlFree (tmp);
3410 rc = add_attribute (ndst, NULL, NULL);
3413 xmlNodePtr n = ndst->children;
3414 xmlUnlinkNode (n);
3415 xmlFreeNodeList (n);
3416 ndst->children = NULL;
3418 if (new->children)
3420 n = xmlCopyNodeList (new->children);
3421 if (!n)
3423 rc = GPG_ERR_ENOMEM;
3424 goto fail;
3427 n = xmlAddChildList (ndst, n);
3428 if (!n)
3430 rc = GPG_ERR_ENOMEM;
3431 goto fail;
3434 rc = update_element_mtime (xmlDocGetRootElement (client->doc) ==
3435 ndst->parent ? ndst : ndst->parent);
3438 fail:
3439 if (new)
3441 xmlUnlinkNode (new);
3442 xmlFreeNodeList (new);
3445 if (req)
3446 strv_free (req);
3448 if (src)
3449 strv_free (src);
3451 if (dst)
3452 strv_free (dst);
3454 return rc;
3457 static gpg_error_t
3458 copy_command (assuan_context_t ctx, char *line)
3460 struct client_s *client = assuan_get_pointer (ctx);
3461 gpg_error_t rc;
3462 struct argv_s *args[] = {
3463 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3464 NULL
3467 rc = parse_options (&line, args, client);
3468 if (rc)
3469 return send_error (ctx, rc);
3471 if (client->opts & OPT_INQUIRE)
3473 unsigned char *result;
3474 size_t len;
3476 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3477 if (rc)
3478 return send_error (ctx, rc);
3480 line = (char *) result;
3483 rc = do_copy (ctx, line);
3485 if (client->opts & OPT_INQUIRE)
3486 xfree (line);
3488 return send_error (ctx, rc);
3491 static gpg_error_t
3492 do_move (assuan_context_t ctx, char *line)
3494 struct client_s *client = assuan_get_pointer (ctx);
3495 gpg_error_t rc;
3496 char **req, **src = NULL, **dst = NULL;
3497 xmlNodePtr nsrc, ndst = NULL;
3499 req = str_split (line, " ", 0);
3501 if (!req || !req[0] || !req[1])
3503 strv_free (req);
3504 return GPG_ERR_SYNTAX;
3507 if (strchr (req[0], '\t'))
3508 src = str_split (req[0], "\t", 0);
3509 else
3510 src = str_split (req[0], " ", 0);
3512 if (!src || !*src)
3514 rc = GPG_ERR_SYNTAX;
3515 goto fail;
3518 if (strchr (req[1], '\t'))
3519 dst = str_split (req[1], "\t", 0);
3520 else
3521 dst = str_split (req[1], " ", 0);
3523 nsrc = find_root_element (client->doc, &src, &rc, NULL, 0, 0);
3524 if (nsrc && src[1])
3525 nsrc = find_elements (client->doc, nsrc->children, src + 1, &rc, NULL,
3526 NULL, NULL, 0, 0, NULL, 0);
3528 if (!nsrc)
3529 goto fail;
3531 if (dst)
3533 if (!valid_element_path (dst, 0))
3535 rc = GPG_ERR_INV_VALUE;
3536 goto fail;
3539 ndst = find_root_element (client->doc, &dst, &rc, NULL, 0, 0);
3540 if (ndst && dst[1])
3541 ndst = find_elements (client->doc, ndst->children, dst + 1, &rc, NULL,
3542 NULL, NULL, 0, 0, NULL, 0);
3544 else
3545 ndst = xmlDocGetRootElement (client->doc);
3547 for (xmlNodePtr n = ndst; n; n = n->parent)
3549 if (n == nsrc)
3551 rc = GPG_ERR_CONFLICT;
3552 goto fail;
3556 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3557 goto fail;
3559 rc = 0;
3561 if (ndst)
3563 xmlChar *a = node_has_attribute (nsrc, (xmlChar *) "_name");
3564 xmlNodePtr dup = find_element (ndst->children, (char *) a, NULL);
3566 xmlFree (a);
3567 if (dup)
3569 if (dup == nsrc)
3570 goto fail;
3572 if (ndst == xmlDocGetRootElement (client->doc))
3574 xmlNodePtr n = nsrc;
3575 int match = 0;
3577 while (n->parent && n->parent != ndst)
3578 n = n->parent;
3580 xmlChar *a = node_has_attribute (n, (xmlChar *) "_name");
3581 xmlChar *b = node_has_attribute (nsrc, (xmlChar *) "_name");
3583 if (xmlStrEqual (a, b))
3585 match = 1;
3586 xmlUnlinkNode (nsrc);
3587 xmlUnlinkNode (n);
3588 xmlFreeNodeList (n);
3591 xmlFree (a);
3592 xmlFree (b);
3594 if (!match)
3596 xmlUnlinkNode (dup);
3597 xmlFreeNodeList (dup);
3600 else
3601 xmlUnlinkNode (dup);
3605 if (!ndst && dst)
3607 xmlChar *name = node_has_attribute (nsrc, (xmlChar *) "_name");
3609 if (nsrc->parent == xmlDocGetRootElement (client->doc)
3610 && !strcmp ((char *) name, *dst))
3612 xmlFree (name);
3613 rc = GPG_ERR_CONFLICT;
3614 goto fail;
3617 xmlFree (name);
3618 ndst = create_element_path (client, &dst, &rc, nsrc);
3621 if (!ndst)
3622 goto fail;
3624 update_element_mtime (nsrc->parent);
3625 xmlUnlinkNode (nsrc);
3626 ndst = xmlAddChildList (ndst, nsrc);
3628 if (!ndst)
3629 rc = GPG_ERR_ENOMEM;
3631 update_element_mtime (ndst->parent);
3633 fail:
3634 if (req)
3635 strv_free (req);
3637 if (src)
3638 strv_free (src);
3640 if (dst)
3641 strv_free (dst);
3643 return rc;
3646 static gpg_error_t
3647 move_command (assuan_context_t ctx, char *line)
3649 struct client_s *client = assuan_get_pointer (ctx);
3650 gpg_error_t rc;
3651 struct argv_s *args[] = {
3652 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3653 NULL
3656 rc = parse_options (&line, args, client);
3657 if (rc)
3658 return send_error (ctx, rc);
3660 if (client->opts & OPT_INQUIRE)
3662 unsigned char *result;
3663 size_t len;
3665 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3666 if (rc)
3667 return send_error (ctx, rc);
3669 line = (char *) result;
3672 rc = do_move (ctx, line);
3674 if (client->opts & OPT_INQUIRE)
3675 xfree (line);
3677 return send_error (ctx, rc);
3680 static gpg_error_t
3681 ls_command (assuan_context_t ctx, char *line)
3683 gpg_error_t rc;
3684 char *tmp = str_asprintf ("%s/data", homedir);
3685 char *dir = expand_homedir (tmp);
3686 DIR *d = opendir (dir);
3688 rc = gpg_error_from_syserror ();
3689 xfree (tmp);
3691 if (!d)
3693 xfree (dir);
3694 return send_error (ctx, rc);
3697 size_t len =
3698 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3699 struct dirent *p = xmalloc (len), *cur = NULL;
3700 char *list = NULL;
3702 xfree (dir);
3703 rc = 0;
3705 while (!readdir_r (d, p, &cur) && cur)
3707 if (cur->d_name[0] == '.' && cur->d_name[1] == '\0')
3708 continue;
3709 else if (cur->d_name[0] == '.' && cur->d_name[1] == '.'
3710 && cur->d_name[2] == '\0')
3711 continue;
3713 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3715 if (!tmp)
3717 if (list)
3718 xfree (list);
3720 rc = GPG_ERR_ENOMEM;
3721 break;
3724 xfree (list);
3725 list = tmp;
3728 closedir (d);
3729 xfree (p);
3731 if (rc)
3732 return send_error (ctx, rc);
3734 if (!list)
3735 return send_error (ctx, GPG_ERR_NO_DATA);
3737 list[strlen (list) - 1] = 0;
3738 rc = xfer_data (ctx, list, strlen (list));
3739 xfree (list);
3740 return send_error (ctx, rc);
3743 static gpg_error_t
3744 bye_notify (assuan_context_t ctx, char *line)
3746 struct client_s *cl = assuan_get_pointer (ctx);
3748 #ifdef WITH_GNUTLS
3749 if (cl->thd->remote)
3751 int rc;
3755 struct timeval tv = { 0, 50000 };
3757 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3758 if (rc == GNUTLS_E_AGAIN)
3759 select (0, NULL, NULL, NULL, &tv);
3761 while (rc == GNUTLS_E_AGAIN);
3763 #endif
3765 /* This will let assuan_process_next() return. */
3766 fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK);
3767 cl->last_rc = 0; // BYE command result
3768 return 0;
3771 static gpg_error_t
3772 reset_notify (assuan_context_t ctx, char *line)
3774 struct client_s *client = assuan_get_pointer (ctx);
3776 if (client)
3777 cleanup_client (client);
3779 return 0;
3783 * This is called before every Assuan command.
3785 static gpg_error_t
3786 command_startup (assuan_context_t ctx, const char *name)
3788 struct client_s *client = assuan_get_pointer (ctx);
3789 gpg_error_t rc;
3790 struct command_table_s *cmd = NULL;
3792 log_write1 ("command='%s'", name);
3793 client->last_rc = client->opts = 0;
3795 for (int i = 0; command_table[i]; i++)
3797 if (!strcasecmp (name, command_table[i]->name))
3799 if (command_table[i]->ignore_startup)
3800 return 0;
3801 cmd = command_table[i];
3802 break;
3806 client->last_rc = rc = gpg_error (file_modified (client, cmd));
3807 return rc;
3811 * This is called after every Assuan command.
3813 static void
3814 command_finalize (assuan_context_t ctx, gpg_error_t rc)
3816 struct client_s *client = assuan_get_pointer (ctx);
3818 if (!(client->flags & FLAG_LOCK_CMD))
3819 unlock_file_mutex (client, 0);
3821 log_write1 (_("command completed: rc=%u"), client->last_rc);
3822 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
3825 static gpg_error_t
3826 help_command (assuan_context_t ctx, char *line)
3828 gpg_error_t rc;
3829 int i;
3831 if (!line || !*line)
3833 char *tmp;
3834 char *help = str_dup (_("Usage: HELP [<COMMAND>]\n"
3835 "For commands that take an element path as an argument, each element is "
3836 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3837 "\n" "COMMANDS:"));
3839 for (i = 0; command_table[i]; i++)
3841 if (!command_table[i]->help)
3842 continue;
3844 tmp = str_asprintf ("%s %s", help, command_table[i]->name);
3845 xfree (help);
3846 help = tmp;
3849 tmp = strip_texi_and_wrap (help);
3850 xfree (help);
3851 rc = xfer_data (ctx, tmp, strlen (tmp));
3852 xfree (tmp);
3853 return send_error (ctx, rc);
3856 for (i = 0; command_table[i]; i++)
3858 if (!strcasecmp (line, command_table[i]->name))
3860 char *help, *tmp;
3862 if (!command_table[i]->help)
3863 break;
3865 help = strip_texi_and_wrap (command_table[i]->help);
3866 tmp = str_asprintf (_("Usage: %s"), help);
3867 xfree (help);
3868 rc = xfer_data (ctx, tmp, strlen (tmp));
3869 xfree (tmp);
3870 return send_error (ctx, rc);
3874 return send_error (ctx, GPG_ERR_INV_NAME);
3877 static void
3878 new_command (const char *name, int ignore, int unlock,
3879 gpg_error_t (*handler) (assuan_context_t, char *),
3880 const char *help)
3882 int i = 0;
3884 if (command_table)
3885 for (i = 0; command_table[i]; i++);
3887 command_table =
3888 xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
3889 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
3890 command_table[i]->name = name;
3891 command_table[i]->handler = handler;
3892 command_table[i]->ignore_startup = ignore;
3893 command_table[i]->unlock = unlock;
3894 command_table[i++]->help = help;
3895 command_table[i] = NULL;
3898 void
3899 deinit_commands ()
3901 int i;
3903 for (i = 0; command_table[i]; i++)
3904 xfree (command_table[i]);
3906 xfree (command_table);
3909 static int
3910 sort_commands (const void *arg1, const void *arg2)
3912 struct command_table_s *const *a = arg1;
3913 struct command_table_s *const *b = arg2;
3915 if (!*a || !*b)
3916 return 0;
3917 else if (*a && !*b)
3918 return 1;
3919 else if (!*a && *b)
3920 return -1;
3922 return strcmp ((*a)->name, (*b)->name);
3925 static gpg_error_t
3926 passwd_command (assuan_context_t ctx, char *line)
3928 struct client_s *client = assuan_get_pointer (ctx);
3929 gpg_error_t rc;
3930 struct argv_s *args[] = {
3931 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
3932 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
3933 NULL
3936 if (client->flags & FLAG_NEW)
3937 return send_error (ctx, GPG_ERR_INV_STATE);
3939 client->crypto->save.s2k_count =
3940 config_get_ulong (client->filename, "s2k_count");
3941 rc = parse_options (&line, args, client);
3942 if (rc)
3943 return send_error (ctx, rc);
3945 if (!rc && client->opts & OPT_RESET)
3947 rc = cache_clear (client->md5file);
3948 if (!rc)
3949 send_status_all (STATUS_CACHE, NULL);
3952 if (!rc)
3954 if (!IS_PKCS (client->crypto))
3956 struct crypto_s *crypto;
3958 xfree (client->crypto->filename);
3959 client->crypto->filename = str_dup (client->filename);
3960 rc = change_passwd (ctx, client->filename,
3961 client->flags & FLAG_NO_PINENTRY, &crypto);
3962 if (!rc)
3964 cleanup_crypto (&client->crypto);
3965 client->crypto = crypto;
3966 update_checksum (client);
3967 cleanup_crypto_stage1 (client->crypto);
3970 #ifdef WITH_AGENT
3971 else
3973 if (client->crypto->save.s2k_count)
3974 rc = send_to_agent (client->crypto->agent, NULL, NULL,
3975 "OPTION s2k-count=%lu",
3976 client->crypto->save.s2k_count);
3978 if (!rc)
3979 rc = agent_passwd (client->crypto);
3981 #endif
3984 return send_error (ctx, rc);
3987 static gpg_error_t
3988 parse_keygrip_opt_sign (void *data, void *value)
3990 struct client_s *client = data;
3992 (void) value;
3993 client->opts |= OPT_SIGN;
3994 return 0;
3997 static gpg_error_t
3998 keygrip_command (assuan_context_t ctx, char *line)
4000 struct client_s *client = assuan_get_pointer (ctx);
4001 gpg_error_t rc;
4002 struct crypto_s *crypto = NULL;
4003 struct argv_s *args[] = {
4004 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_keygrip_opt_sign},
4005 NULL
4008 if (!line || !*line)
4009 return send_error (ctx, GPG_ERR_SYNTAX);
4011 rc = parse_options (&line, args, client);
4012 if (rc)
4013 return send_error (ctx, rc);
4015 if (!valid_filename (line))
4016 return send_error (ctx, GPG_ERR_INV_VALUE);
4018 rc = init_client_crypto (&crypto);
4019 if (rc)
4020 return send_error (ctx, rc);
4022 rc = read_data_file (line, crypto);
4023 if (!rc)
4025 char *hexgrip = NULL;
4027 if (!IS_PKCS (crypto))
4029 cleanup_crypto (&crypto);
4030 return send_error (ctx, GPG_ERR_NOT_SUPPORTED);
4033 if (client->opts & OPT_SIGN)
4035 if (valid_keygrip (crypto->sign_grip, sizeof (crypto->sign_grip)))
4036 hexgrip = bin2hex (crypto->sign_grip, sizeof (crypto->sign_grip));
4039 if (!hexgrip)
4040 hexgrip = bin2hex (crypto->grip, sizeof (crypto->grip));
4042 if (!hexgrip)
4043 rc = GPG_ERR_ENOMEM;
4044 else
4045 rc = xfer_data (ctx, hexgrip, strlen (hexgrip));
4047 xfree (hexgrip);
4050 UPDATE_AGENT_CTX (client, crypto);
4051 cleanup_crypto (&crypto);
4052 return send_error (ctx, rc);
4055 static gpg_error_t
4056 parse_opt_data (void *data, void *value)
4058 struct client_s *client = data;
4060 (void) value;
4061 client->opts |= OPT_DATA;
4062 return 0;
4065 static gpg_error_t
4066 getinfo_command (assuan_context_t ctx, char *line)
4068 struct client_s *client = assuan_get_pointer (ctx);
4069 gpg_error_t rc;
4070 char buf[ASSUAN_LINELENGTH];
4071 struct argv_s *args[] = {
4072 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4073 NULL
4076 rc = parse_options (&line, args, client);
4077 if (rc)
4078 return send_error (ctx, rc);
4080 if (!strcasecmp (line, "clients"))
4082 if (client->opts & OPT_DATA)
4084 MUTEX_LOCK (&cn_mutex);
4085 snprintf (buf, sizeof (buf), "%i", slist_length (cn_thread_list));
4086 MUTEX_UNLOCK (&cn_mutex);
4087 rc = xfer_data (ctx, buf, strlen (buf));
4089 else
4090 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4092 else if (!strcasecmp (line, "cache"))
4094 if (client->opts & OPT_DATA)
4096 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4097 rc = xfer_data (ctx, buf, strlen (buf));
4099 else
4100 rc = send_status (ctx, STATUS_CACHE, NULL);
4102 else if (!strcasecmp (line, "pid"))
4104 char buf[32];
4105 pid_t pid = getpid ();
4107 snprintf (buf, sizeof (buf), "%i", pid);
4108 rc = xfer_data (ctx, buf, strlen (buf));
4110 else if (!strcasecmp (line, "version"))
4112 char *buf = str_asprintf ("0x%06x %s%s", VERSION_HEX,
4113 #ifdef WITH_LIBACL
4114 "ACL "
4115 #endif
4116 #ifdef WITH_GNUTLS
4117 "GNUTLS "
4118 #endif
4119 #ifdef WITH_QUALITY
4120 "QUALITY "
4121 #endif
4122 "", use_agent ? "AGENT" : "");
4123 rc = xfer_data (ctx, buf, strlen (buf));
4124 xfree (buf);
4126 else if (!strcasecmp (line, "last_error"))
4128 if (client->last_error)
4129 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4130 else
4131 rc = GPG_ERR_NO_DATA;
4133 else
4134 rc = gpg_error (GPG_ERR_SYNTAX);
4136 return send_error (ctx, rc);
4139 #ifdef WITH_AGENT
4140 static gpg_error_t
4141 send_data_cb (void *user, const void *buf, size_t len)
4143 assuan_context_t ctx = user;
4145 return assuan_send_data (ctx, buf, len);
4148 static gpg_error_t
4149 send_status_cb (void *user, const char *line)
4151 assuan_context_t ctx = user;
4152 char keyword[200], *k;
4153 const char *p;
4155 for (p = line, k = keyword; *p; p++)
4157 if (isspace (*p))
4158 break;
4160 *k++ = *p;
4163 *k = 0;
4164 if (*p == '#')
4165 p++;
4167 while (isspace (*p))
4168 p++;
4170 return assuan_write_status (ctx, keyword, *p ? p : NULL);
4172 #endif
4174 static gpg_error_t
4175 agent_command (assuan_context_t ctx, char *line)
4177 gpg_error_t rc = 0;
4179 if (!use_agent)
4180 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
4182 #ifdef WITH_AGENT
4183 struct client_s *client = assuan_get_pointer (ctx);
4185 if (!line || !*line)
4186 return send_error (ctx, GPG_ERR_SYNTAX);
4188 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 1);
4189 rc = assuan_transact (client->crypto->agent->ctx, line, send_data_cb,
4190 client->ctx, agent_loopback_cb, client->crypto,
4191 send_status_cb, client->ctx);
4192 if (gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
4194 char *line;
4195 size_t len;
4197 rc = assuan_write_line (client->crypto->agent->ctx, "CAN");
4198 if (!rc)
4200 rc = assuan_read_line (client->crypto->agent->ctx, &line, &len);
4201 if (!rc)
4202 rc = gpg_error (GPG_ERR_ASS_CANCELED);
4206 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 0);
4207 #endif
4208 return send_error (ctx, rc);
4211 void
4212 init_commands ()
4214 /* !BEGIN-HELP-TEXT!
4216 * This comment is used as a marker to generate the offline documentation
4217 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4218 * script to determine where commands begin and end.
4220 new_command("HELP", 1, 1, help_command, _(
4221 "HELP [<COMMAND>]\n"
4222 "Show available commands or command specific help text."
4225 new_command("AGENT", 1, 1, agent_command, _(
4226 "AGENT <command>\n"
4227 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4228 "@command{gpg-agent}."
4231 new_command("GETINFO", 1, 1, getinfo_command, _(
4232 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4233 "Get server and other information: @var{cache} returns the number of cached "
4234 "documents via a status message. @var{clients} returns the number of "
4235 "connected clients via a status message. @var{pid} returns the process ID "
4236 "number of the server via a data response. @var{VERSION} returns the server "
4237 "version number and compile-time features with a data response with each "
4238 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4239 "the last failed command when available. @xref{Status Messages}. "
4240 "\n"
4241 "When the @option{--data} option is specified then the result will be send "
4242 "via a data response rather than a status message."
4245 new_command("PASSWD", 0, 0, passwd_command, _(
4246 "PASSWD [--reset] [--s2k-count=N]\n"
4247 "Changes the passphrase of the secret key required to open the current "
4248 "file. When the @option{--reset} option is passed then the cache entry for "
4249 "the current file will be reset and the passphrase, if any, will be required "
4250 "during the next @code{OPEN}. @xref{OPEN}."
4251 "\n"
4252 "The @option{--s2k-count} option sets number of hash iterations for a "
4253 "passphrase and must be either @code{0} to use the calibrated count of the "
4254 "machine (the default), or a value greater than or equal to @code{65536}. "
4255 "@xref{SAVE}."
4258 new_command("KEYGRIP", 1, 1, keygrip_command, _(
4259 "KEYGRIP [--sign] <filename>\n"
4260 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4261 "data response."
4262 "\n"
4263 "When the @option{--sign} option is specified then the key used for signing "
4264 "of the specified @var{filename} will be returned."
4267 new_command("OPEN", 1, 1, open_command, _(
4268 "OPEN [--lock] <filename> [<passphrase>]\n"
4269 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4270 "found on the file-system then a new document will be created. If the file "
4271 "is found, it is looked for in the file cache. If cached and no "
4272 "@var{passphrase} was specified then the cached document is opened. When not "
4273 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4274 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4275 "specified."
4276 "\n"
4277 "When the @option{--lock} option is passed then the file mutex will be "
4278 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4279 "file has been opened."
4282 new_command("SAVE", 0, 0, save_command, _(
4283 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring [--sign-keygrip=hexstring]]\n"
4284 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4285 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4286 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4287 "keypair will be generated and a pinentry will be used to prompt for the "
4288 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4289 "passed, in which case the data file will not be passphrase protected."
4290 "\n"
4291 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4292 "passphrase retrieval and caching for new files and behaves as if the "
4293 "@option{--no-agent} commmand line option to @command{pwmd} was specified."
4294 "\n"
4295 "The @option{--reset} option will clear the cache entry for the current file "
4296 "before saving."
4297 "\n"
4298 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4299 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4300 "(@pxref{Configuration}) for available ciphers."
4301 "\n"
4302 "The @option{--cipher-iterations} option specifies the number of times to "
4303 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4304 "\n"
4305 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4306 "the client to obtain the key paramaters to use when generating a new "
4307 "keypair. The inquired data is expected to be an S-expression. If not "
4308 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4309 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4310 "that when this option is specified a new keypair will be generated "
4311 "reguardless if the file is a new one or not."
4312 "\n"
4313 "You can encrypt the data file to a public key other than the one that it "
4314 "was originally encrypted with by passing the @option{--keygrip} option with "
4315 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4316 "be of any key that @command{gpg-agent} knows about. The "
4317 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4318 "secret key. This option may be needed when using a smartcard."
4319 "\n"
4320 "The @option{--s2k-count} option sets number of hash iterations for a "
4321 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4322 "value which is the default. This setting only affects new files. To change "
4323 "the setting, use the @code{PASSWD} command (@pxref{PASSWD})."
4326 new_command("ISCACHED", 1, 0, iscached_command, _(
4327 "ISCACHED [--lock] <filename>\n"
4328 "An @emph{OK} response is returned if the specified @var{filename} is found "
4329 "in the file cache. If not found in the cache but exists on the filesystem "
4330 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4331 "returned."
4332 "\n"
4333 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4334 "file exists; it does not need to be opened nor cached."
4337 new_command("CLEARCACHE", 1, 1, clearcache_command, _(
4338 "CLEARCACHE [<filename>]\n"
4339 "Clears a file cache entry for all or the specified @var{filename}."
4342 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command, _(
4343 "CACHETIMEOUT <filename> <seconds>\n"
4344 "The time in @var{seconds} until @var{filename} will be removed from the "
4345 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4346 "the passphrase for each @code{OPEN} command (@pxref{OPEN}). "
4347 "@xref{Configuration}, and the @code{cache_timeout} parameter."
4350 new_command("LIST", 0, 1, list_command, _(
4351 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4352 "If no element path is given then a newline separated list of root elements "
4353 "is returned with a data response. If given, then all reachable elements "
4354 "of the specified element path are returned unless the @option{--no-recurse} "
4355 "option is specified. If specified, only the child elements of the element "
4356 "path are returned without recursing into grandchildren. Each resulting "
4357 "element is prefixed with the literal @code{!} character when the element "
4358 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4359 "\n"
4360 "When the @option{--verbose} option is passed then each element path "
4361 "returned will have zero or more flags appened to it. These flags are "
4362 "delimited from the element path by a single space character. A flag itself "
4363 "is a single character. Flag @code{+} indicates that there are child nodes of "
4364 "the current element path. Flag @code{E} indicates that an element of an "
4365 "element path contained in a @var{target} attribute could not be found. Flag "
4366 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4367 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4368 "of a @var{target} attribute (see below)."
4369 "\n"
4370 "The @option{--with-target} option implies @option{--verbose} and will append "
4371 "an additional flag @code{T} followed by a single space then an element path. "
4372 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4373 "current element when it contains a @var{target} attribute. When no "
4374 "@var{target} attribute is found then no flag will be appended."
4375 "\n"
4376 "The @option{--no-recurse} option limits the amount of data returned to only "
4377 "the listing of children of the specified element path and not any "
4378 "grandchildren."
4379 "\n"
4380 "The @option{--all} option lists the entire element tree for each root "
4381 "element. This option also implies option @option{--verbose}."
4382 "\n"
4383 "When the @option{--inquire} option is passed then all remaining non-option "
4384 "arguments are retrieved via a server @emph{INQUIRE}."
4387 new_command("REALPATH", 0, 1, realpath_command, _(
4388 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4389 "Resolves all @code{target} attributes of the specified element path and "
4390 "returns the result with a data response. @xref{Target Attribute}, for details."
4391 "\n"
4392 "When the @option{--inquire} option is passed then all remaining non-option "
4393 "arguments are retrieved via a server @emph{INQUIRE}."
4396 new_command("STORE", 0, 1, store_command, _(
4397 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4398 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4399 "\n"
4400 "Creates a new element path or modifies the @var{content} of an existing "
4401 "element. If only a single element is specified then a new root element is "
4402 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4403 "set to the final @key{TAB} delimited element. If no @var{content} is "
4404 "specified after the final @key{TAB}, then the content of the element will "
4405 "be removed, or empty when creating a new element."
4406 "\n"
4407 "The only restriction of an element name is that it not contain whitespace "
4408 "or begin with the literal element character @code{!} unless specifying a "
4409 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4410 "the @key{TAB} delimited elements. It is recommended that the content of an "
4411 "element be base64 encoded when it contains control or @key{TAB} characters "
4412 "to prevent @abbr{XML} and @command{pwmd} parsing errors."
4415 new_command("RENAME", 0, 1, rename_command, _(
4416 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4417 "Renames the specified @var{element} to the new @var{value}. If an element of "
4418 "the same name as the @var{value} already exists it will be overwritten."
4419 "\n"
4420 "When the @option{--inquire} option is passed then all remaining non-option "
4421 "arguments are retrieved via a server @emph{INQUIRE}."
4424 new_command("COPY", 0, 1, copy_command, _(
4425 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4426 "Copies the entire element tree starting from the child node of the source "
4427 "element, to the destination element path. If the destination element path "
4428 "does not exist then it will be created; otherwise it is overwritten."
4429 "\n"
4430 "Note that attributes from the source element are merged into the "
4431 "destination element when the destination element path exists. When an "
4432 "attribute of the same name exists in both the source and destination "
4433 "elements then the destination attribute will be updated to the source "
4434 "attribute value."
4435 "\n"
4436 "When the @option{--inquire} option is passed then all remaining non-option "
4437 "arguments are retrieved via a server @emph{INQUIRE}."
4440 new_command("MOVE", 0, 1, move_command, _(
4441 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4442 "Moves the source element path to the destination element path. If the "
4443 "destination is not specified then it will be moved to the root node of the "
4444 "document. If the destination is specified and exists then it will be "
4445 "overwritten; otherwise it will be created."
4446 "\n"
4447 "When the @option{--inquire} option is passed then all remaining non-option "
4448 "arguments are retrieved via a server @emph{INQUIRE}."
4451 new_command("DELETE", 0, 1, delete_command, _(
4452 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4453 "Removes the specified element path and all of its children. This may break "
4454 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4455 "refers to this element or any of its children."
4456 "\n"
4457 "When the @option{--inquire} option is passed then all remaining non-option "
4458 "arguments are retrieved via a server @emph{INQUIRE}."
4461 new_command("GET", 0, 1, get_command, _(
4462 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4463 "Retrieves the content of the specified element. The content is returned "
4464 "with a data response."
4465 "\n"
4466 "When the @option{--inquire} option is passed then all remaining non-option "
4467 "arguments are retrieved via a server @emph{INQUIRE}."
4470 new_command("ATTR", 0, 1, attr_command, _(
4471 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4472 "@table @asis\n"
4473 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4474 "\n"
4475 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4476 "element. When no @var{value} is specified any existing value will be removed."
4477 "\n"
4478 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4479 "\n"
4480 " Removes an @var{attribute} from an element."
4481 "\n"
4482 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4483 "\n"
4484 " Retrieves a newline separated list of attributes names and values "
4485 "from the specified element. Each attribute name and value is space delimited."
4486 "\n"
4487 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4488 "\n"
4489 " Retrieves the value of an @var{attribute} from an element."
4490 "@end table\n"
4491 "\n"
4492 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4493 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4494 "commands instead."
4495 "\n"
4496 "The @code{_mtime} attribute is updated each time an element is modified by "
4497 "either storing content, editing attributes or by deleting a child element. "
4498 "The @code{_ctime} attribute is created for each new element in an element "
4499 "path."
4500 "\n"
4501 "When the @option{--inquire} option is passed then all remaining non-option "
4502 "arguments are retrieved via a server @emph{INQUIRE}."
4503 "\n"
4504 "@xref{Target Attribute}, for details about this special attribute."
4507 new_command("XPATH", 0, 1, xpath_command, _(
4508 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4509 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4510 "specified, it is assumed the expression is a request to return a result. "
4511 "Otherwise, the result is set to the @var{value} argument and the document is "
4512 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4513 "is assumed to be empty and the document is updated. For example:"
4514 "@sp 1\n"
4515 "@example\n"
4516 "XPATH //element[@@_name='password']@key{TAB}\n"
4517 "@end example\n"
4518 "@sp 1"
4519 "would clear the content of all @code{password} elements in the data file "
4520 "while leaving off the trailing @key{TAB} would return all @code{password} "
4521 "elements in @abbr{XML} format."
4522 "\n"
4523 "When the @option{--inquire} option is passed then all remaining non-option "
4524 "arguments are retrieved via a server @emph{INQUIRE}."
4525 "\n"
4526 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4527 "expression syntax."
4530 new_command("XPATHATTR", 0, 1, xpathattr_command, _(
4531 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4532 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4533 "attributes and does not return a result. For the @var{SET} operation the "
4534 "@var{value} is optional but the field is required. If not specified then "
4535 "the attribute value will be empty. For example:"
4536 "@sp 1"
4537 "@example\n"
4538 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4539 "@end example\n"
4540 "@sp 1"
4541 "would create an @code{password} attribute for each @code{password} element "
4542 "found in the document. The attribute value will be empty but still exist."
4543 "\n"
4544 "When the @option{--inquire} option is passed then all remaining non-option "
4545 "arguments are retrieved via a server @emph{INQUIRE}."
4546 "\n"
4547 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4548 "expression syntax."
4551 new_command("IMPORT", 0, 1, import_command, _(
4552 "IMPORT <content>[<TAB>[!]element[<TAB>[!]child[..]]]\n"
4553 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4554 "\n"
4555 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4556 "argument is raw @abbr{XML} data. The content is created as a child of the "
4557 "specified element path and will overwrite an existing element of the same "
4558 "name. If an element of the element path does not exist then it will be "
4559 "created."
4560 "\n"
4561 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4562 "for details."
4565 new_command("DUMP", 0, 1, dump_command, _(
4566 "DUMP\n"
4567 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4568 "dumping a specific node."
4571 new_command("LOCK", 0, 0, lock_command, _(
4572 "LOCK\n"
4573 "Locks the mutex associated with the opened file. This prevents other clients "
4574 "from sending commands to the same opened file until the client "
4575 "that sent this command either disconnects or sends the @code{UNLOCK} "
4576 "command. @xref{UNLOCK}."
4579 new_command("UNLOCK", 1, 0, unlock_command, _(
4580 "UNLOCK\n"
4581 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4582 "a commands' @option{lock} option. @xref{LOCK}."
4585 new_command("GETCONFIG", 1, 1, getconfig_command, _(
4586 "GETCONFIG [filename] <parameter>\n"
4587 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4588 "data response. If no file has been opened then the value for @var{filename} "
4589 "or the default from the @samp{global} section will be returned. If a file "
4590 "has been opened and no @var{filename} is specified, a value previously "
4591 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4594 new_command("OPTION", 1, 1, option_command, _(
4595 "OPTION <NAME>=<VALUE>\n"
4596 "Sets a client option @var{name} to @var{value}. The value for an option is "
4597 "kept for the duration of the connection."
4598 "\n"
4599 "@table @asis\n"
4600 "@item DISABLE-PINENTRY\n"
4601 " Disable use of @command{pinentry} for passphrase retrieval. When set, a "
4602 " server inquire is sent to the client to obtain the passphrase. This option "
4603 " may be set as needed before the @pxref{OPEN}, @pxref{PASSWD}, and "
4604 " @pxref{SAVE} commands."
4605 "\n"
4606 "@item TTYNAME\n"
4607 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4608 "\n"
4609 "@item TTYTYPE\n"
4610 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4611 "\n"
4612 "@item DISPLAY\n"
4613 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4614 "\n"
4615 "@item PINENTRY-DESC\n"
4616 " Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4617 "\n"
4618 "@item PINENTRY-TITLE\n"
4619 " Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
4620 "\n"
4621 "@item PINENTRY-PROMPT\n"
4622 " Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
4623 "\n"
4624 "@item LC-CTYPE\n"
4625 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4626 "\n"
4627 "@item LC-MESSAGES\n"
4628 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4629 "\n"
4630 "@item NAME\n"
4631 " Associates the thread ID of the connection with the specified textual "
4632 "representation. Useful for debugging log messages."
4633 "\n"
4634 "@item LOCK-TIMEOUT\n"
4635 " When not @code{0}, the duration in tenths of a second to wait for the file "
4636 "mutex which has been locked by another thread to be released before returning "
4637 "an error. When @code{-1}, then an error will be returned immediately."
4638 "@end table\n"
4641 new_command("LS", 1, 1, ls_command, _(
4642 "LS\n"
4643 "Lists the available data files stored in the data directory "
4644 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4647 new_command("RESET", 1, 1, NULL, _(
4648 "RESET\n"
4649 "Closes the currently opened file but keeps any previously set client options."
4652 new_command("NOP", 1, 1, NULL, _(
4653 "NOP\n"
4654 "Does nothing. Always returns successfully."
4657 /* !END-HELP-TEXT! */
4658 new_command ("CANCEL", 1, 1, NULL, NULL);
4659 new_command ("END", 1, 1, NULL, NULL);
4660 new_command ("BYE", 1, 1, NULL, NULL);
4662 int i;
4663 for (i = 0; command_table[i]; i++);
4664 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4665 sort_commands);
4668 gpg_error_t
4669 register_commands (assuan_context_t ctx)
4671 int i = 0, rc;
4673 for (; command_table[i]; i++)
4675 if (!command_table[i]->handler)
4676 continue;
4678 rc = assuan_register_command (ctx, command_table[i]->name,
4679 command_table[i]->handler,
4680 command_table[i]->help);
4681 if (rc)
4682 return rc;
4685 rc = assuan_register_bye_notify (ctx, bye_notify);
4686 if (rc)
4687 return rc;
4689 rc = assuan_register_reset_notify (ctx, reset_notify);
4690 if (rc)
4691 return rc;
4693 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4694 if (rc)
4695 return rc;
4697 return assuan_register_post_cmd_notify (ctx, command_finalize);