Let CACHETIMEOUT work for non-cached files.
[libpwmd.git] / src / commands.c
blob0615a949b52d9bb69cc1ed42974391c98d992c76
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 "common.h"
47 #include "rcfile.h"
48 #include "cache.h"
49 #include "util-misc.h"
50 #include "commands.h"
51 #include "mutex.h"
53 /* Flags needed to be retained for a client across commands. */
54 #define FLAG_NEW 0x0001
55 #define FLAG_HAS_LOCK 0x0002
56 #define FLAG_LOCK_CMD 0x0004
57 #define FLAG_NO_PINENTRY 0x0008
58 #define FLAG_OPEN 0x0010
59 #define FLAG_KEEP_LOCK 0x0020
61 /* These are command option flags. */
62 #define OPT_INQUIRE 0x0001
63 #define OPT_NO_PASSPHRASE 0x0002
64 #define OPT_RESET 0x0004
65 #define OPT_LIST_RECURSE 0x0008
66 #define OPT_LIST_VERBOSE 0x0010
67 #define OPT_LOCK 0x0020
68 #define OPT_LOCK_ON_OPEN 0x0040
69 #define OPT_SIGN 0x0080
70 #define OPT_LIST_ALL 0x0100
71 #define OPT_LIST_WITH_TARGET 0x0200
72 #define OPT_DATA 0x0400
74 struct command_table_s
76 const char *name;
77 gpg_error_t (*handler) (assuan_context_t, char *line);
78 const char *help;
79 int ignore_startup;
80 int unlock; // unlock the file mutex after validating the checksum
83 static struct command_table_s **command_table;
85 static gpg_error_t do_lock (struct client_s *client, int add);
86 static gpg_error_t validate_checksum (struct client_s *,
87 struct cache_data_s *);
88 static gpg_error_t update_checksum (struct client_s *client);
90 static gpg_error_t
91 unlock_file_mutex (struct client_s *client, int remove)
93 gpg_error_t rc = 0;
95 // OPEN: keep the lock for the same file being reopened.
96 if (client->flags & FLAG_KEEP_LOCK && client->flags & FLAG_HAS_LOCK)
97 return 0;
99 if (!(client->flags & FLAG_HAS_LOCK))
100 return GPG_ERR_NOT_LOCKED;
102 rc = cache_unlock_mutex (client->md5file, remove);
103 if (rc)
104 rc = GPG_ERR_INV_STATE;
105 else
106 client->flags &= ~(FLAG_HAS_LOCK | FLAG_LOCK_CMD);
108 return rc;
111 static gpg_error_t
112 lock_file_mutex (struct client_s *client, int add)
114 gpg_error_t rc = 0;
115 int timeout = config_get_integer (client->filename, "cache_timeout");
117 if (client->flags & FLAG_HAS_LOCK)
118 return 0;
120 rc = cache_lock_mutex (client->ctx, client->md5file,
121 client->lock_timeout, add, timeout);
122 if (!rc)
123 client->flags |= FLAG_HAS_LOCK;
125 return rc;
128 static gpg_error_t
129 file_modified (struct client_s *client, struct command_table_s *cmd)
131 gpg_error_t rc = 0;
133 if (!(client->flags & FLAG_OPEN))
134 return GPG_ERR_INV_STATE;
136 rc = lock_file_mutex (client, 0);
137 if (!rc || rc == GPG_ERR_NO_DATA)
139 rc = validate_checksum (client, NULL);
140 if (gpg_err_code (rc) == GPG_ERR_ENOENT)
141 rc = 0;
144 if ((cmd->unlock && !(client->flags & FLAG_HAS_LOCK)) || rc)
145 unlock_file_mutex (client, 0);
147 return rc;
150 static gpg_error_t
151 parse_xml (assuan_context_t ctx, int new)
153 struct client_s *client = assuan_get_pointer (ctx);
154 int cached = client->doc != NULL;
156 if (new)
158 client->doc = new_document ();
159 if (client->doc)
161 xmlDocDumpFormatMemory (client->doc,
162 (xmlChar **) & client->crypto->plaintext,
163 (int *) &client->crypto->plaintext_len, 0);
164 if (!client->crypto->plaintext)
166 xmlFreeDoc (client->doc);
167 client->doc = NULL;
171 else if (!cached)
172 client->doc = parse_doc ((char *) client->crypto->plaintext,
173 client->crypto->plaintext_len);
175 return !client->doc ? GPG_ERR_ENOMEM : 0;
178 static void
179 free_client (struct client_s *client)
181 if (client->doc)
182 xmlFreeDoc (client->doc);
184 xfree (client->crc);
185 xfree (client->filename);
186 xfree (client->last_error);
188 if (client->crypto)
190 cleanup_crypto_stage2 (client->crypto);
191 if (client->crypto->pkey_sexp)
192 gcry_sexp_release (client->crypto->pkey_sexp);
194 client->crypto->pkey_sexp = NULL;
195 memset (client->crypto->sign_grip, 0,
196 sizeof (client->crypto->sign_grip));
199 if (client->xml_error)
200 xmlResetError (client->xml_error);
203 void
204 cleanup_client (struct client_s *client)
206 assuan_context_t ctx = client->ctx;
207 struct client_thread_s *thd = client->thd;
208 struct crypto_s *crypto = client->crypto;
209 long lock_timeout = client->lock_timeout;
211 unlock_file_mutex (client, client->flags & FLAG_NEW);
212 free_client (client);
213 memset (client, 0, sizeof (struct client_s));
214 client->crypto = crypto;
215 client->ctx = ctx;
216 client->thd = thd;
217 client->lock_timeout = lock_timeout;
220 static gpg_error_t
221 open_finalize (assuan_context_t ctx)
223 struct client_s *client = assuan_get_pointer (ctx);
224 gpg_error_t rc = 0;
225 struct cache_data_s *cdata = cache_get_data (client->md5file);
226 int cached = 0;
228 client->crypto->filename = str_dup (client->filename);
229 if (cdata || client->flags & FLAG_NEW)
231 if (!(client->flags & FLAG_NEW))
233 rc = decrypt_xml (client->crypto, cdata->doc, cdata->doclen);
234 if (rc)
235 return rc;
238 cached = cdata != NULL;
239 goto done;
242 rc = decrypt_data (client->ctx, client->crypto);
244 done:
245 if (!rc)
247 rc = parse_xml (ctx, client->flags & FLAG_NEW);
248 if (!rc)
250 int timeout = config_get_integer (client->filename,
251 "cache_timeout");
253 if (!cached)
255 cdata = xcalloc (1, sizeof (struct cache_data_s));
256 rc = encrypt_xml (NULL, cache_key, cache_keysize,
257 GCRY_CIPHER_AES, client->crypto->plaintext,
258 client->crypto->plaintext_len, &cdata->doc,
259 &cdata->doclen, &cache_iv, &cache_blocksize,
261 if (!rc && !(client->flags & FLAG_NEW))
263 rc = gcry_sexp_build ((gcry_sexp_t *) & cdata->pubkey, NULL,
264 "%S", client->crypto->pkey_sexp);
268 if (cdata->sigkey)
269 gcry_sexp_release (cdata->sigkey);
271 if (!rc)
272 rc = gcry_sexp_build ((gcry_sexp_t *) & cdata->sigkey, NULL,
273 "%S", client->crypto->sigpkey_sexp);
275 if (!rc
276 && !cache_add_file (client->md5file,
277 (client->flags & FLAG_NEW) ? NULL : client->
278 crypto->grip, cdata, timeout))
280 if (!cached)
282 free_cache_data_once (cdata);
283 xmlFreeDoc (client->doc);
284 client->doc = NULL;
286 rc = GPG_ERR_ENOMEM;
289 if (!rc)
290 client->flags |= FLAG_OPEN;
294 if (!rc)
295 update_checksum (client);
297 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
298 rc = do_lock (client, 0);
300 return rc;
303 static void
304 req_cleanup (void *arg)
306 if (!arg)
307 return;
309 strv_free ((char **) arg);
312 static gpg_error_t
313 parse_open_opt_lock (void *data, void *value)
315 struct client_s *client = data;
317 client->opts |= OPT_LOCK_ON_OPEN;
318 return 0;
321 static gpg_error_t
322 parse_opt_pinentry (void *data, void *value)
324 struct client_s *client = data;
326 client->flags |= FLAG_NO_PINENTRY;
327 return 0;
330 static gpg_error_t
331 parse_opt_inquire (void *data, void *value)
333 struct client_s *client = data;
335 (void) value;
336 client->opts |= OPT_INQUIRE;
337 return 0;
340 static gpg_error_t
341 update_checksum (struct client_s *client)
343 unsigned char *crc;
344 size_t len;
345 struct cache_data_s *cdata;
346 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
348 if (rc)
349 return rc;
351 xfree (client->crc);
352 client->crc = crc;
353 cdata = cache_get_data (client->md5file);
354 if (cdata)
356 xfree (cdata->crc);
357 cdata->crc = xmalloc (len);
358 memcpy (cdata->crc, crc, len);
361 return 0;
364 static gpg_error_t
365 validate_checksum (struct client_s *client, struct cache_data_s *cdata)
367 unsigned char *crc;
368 size_t len;
369 gpg_error_t rc;
370 int n = 0;
372 if (cdata && !cdata->crc)
373 return GPG_ERR_CHECKSUM;
375 rc = get_checksum (client->filename, &crc, &len);
376 if (rc)
377 return rc;
379 if (cdata)
380 n = memcmp (cdata->crc, crc, len);
381 else if (client->crc)
382 n = memcmp (client->crc, crc, len);
384 xfree (crc);
385 return n ? GPG_ERR_CHECKSUM : 0;
388 static gpg_error_t
389 do_open (assuan_context_t ctx, const char *filename, const char *password)
391 struct client_s *client = assuan_get_pointer (ctx);
392 struct cache_data_s *cdata;
393 gpg_error_t rc = 0;
394 int done = 0;
396 if (!valid_filename (filename))
397 return GPG_ERR_INV_VALUE;
399 gcry_md_hash_buffer (GCRY_MD_MD5, client->md5file, filename,
400 strlen (filename));
401 client->filename = str_dup (filename);
402 if (!client->filename)
403 return GPG_ERR_ENOMEM;
405 // Cached document?
406 cdata = cache_get_data (client->md5file);
407 if (cdata && cdata->doc)
409 int defer;
411 rc = validate_checksum (client, cdata);
412 /* This will check that the key is cached in the agent which needs to
413 * be determined for files that share a keygrip. */
414 if (!rc)
416 rc = cache_iscached (client->filename, &defer);
417 if (!rc && defer)
418 rc = GPG_ERR_KEY_EXPIRED;
421 #ifdef WITH_GNUTLS
422 if (!rc && client->thd->remote &&
423 config_get_boolean (NULL, "tcp_require_key"))
424 rc = GPG_ERR_KEY_EXPIRED;
425 #endif
427 if (!rc && !password)
429 rc = read_data_header (client->filename, &client->crypto->hdr,
430 NULL, NULL);
431 if (!rc)
433 gcry_sexp_build (&client->crypto->pkey_sexp, NULL, "%S",
434 cdata->pubkey);
435 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
436 client->crypto->grip);
437 gcry_sexp_build (&client->crypto->sigpkey_sexp, NULL, "%S",
438 cdata->sigkey);
439 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
440 client->crypto->sign_grip);
441 rc = open_finalize (ctx);
442 done = 1;
446 /* There was an error accessing the file so clear the cache entry. The
447 * real error will be returned from read_data_file() since the file
448 * may have only disappeared. */
449 if (!done)
451 log_write ("%s: %s", filename, pwmd_strerror (rc));
452 rc = cache_clear (client->md5file);
453 send_status_all (STATUS_CACHE, NULL);
457 if (done || rc)
458 return rc;
460 rc = read_data_file (client->filename, client->crypto);
461 if (rc)
463 if (gpg_err_source (rc) != GPG_ERR_SOURCE_DEFAULT ||
464 gpg_err_code (rc) != GPG_ERR_ENOENT)
466 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
467 return rc;
470 client->flags |= FLAG_NEW;
471 memset (client->crypto->grip, 0, sizeof (client->crypto->grip));
472 memset (client->crypto->sign_grip, 0,
473 sizeof (client->crypto->sign_grip));
474 rc = open_finalize (ctx);
475 return rc;
478 if (password)
480 rc = set_agent_passphrase (client->crypto, password, strlen (password));
481 if (rc)
482 return rc;
485 rc = open_finalize (ctx);
486 return rc;
489 static gpg_error_t
490 open_command (assuan_context_t ctx, char *line)
492 gpg_error_t rc;
493 struct client_s *client = assuan_get_pointer (ctx);
494 char **req, *password = NULL, *filename;
495 unsigned char md5file[16];
496 int same_file = 0;
497 struct argv_s *args[] = {
498 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
499 &(struct argv_s) {"no-pinentry", OPTION_TYPE_NOARG,
500 parse_opt_pinentry},
501 NULL
504 client->flags &= ~(FLAG_NO_PINENTRY);
505 rc = parse_options (&line, args, client);
506 if (rc)
507 return send_error (ctx, rc);
509 req = str_split (line, " ", 2);
510 if (!req)
511 return send_error (ctx, GPG_ERR_SYNTAX);
513 #ifdef WITH_GNUTLS
514 if (client->thd->remote)
516 rc = tls_validate_access (client, req[0]);
517 if (rc)
519 if (rc == GPG_ERR_INV_USER_ID)
520 log_write (_("client validation failed for file '%s'"), req[0]);
521 strv_free (req);
522 return send_error (ctx, rc);
525 /* Remote pinentries are not supported. */
526 client->flags |= FLAG_NO_PINENTRY;
528 #endif
530 pthread_cleanup_push (req_cleanup, req);
531 filename = req[0];
532 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, filename, strlen (filename));
533 /* This client may have locked a different file with ISCACHED --lock than
534 * the current filename. This will remove that lock. */
535 same_file = !memcmp (md5file, client->md5file, 16) ? 1 : 0;
536 if (client->flags & FLAG_OPEN ||
537 (client->flags & FLAG_HAS_LOCK && !same_file))
539 typeof (client->opts) opts = client->opts;
540 typeof (client->flags) flags = client->flags;
542 if (same_file)
543 client->flags |= FLAG_KEEP_LOCK;
545 cleanup_client (client);
546 client->opts = opts;
547 client->flags |= flags;
548 client->flags &= ~(FLAG_LOCK_CMD | FLAG_NEW);
549 if (!same_file)
550 client->flags &= ~(FLAG_HAS_LOCK);
553 /* Need to lock the mutex here because file_modified() cannot without
554 * knowing the filename. */
555 memcpy (client->md5file, md5file, 16);
556 rc = lock_file_mutex (client, 1);
557 if (!rc)
559 password = req[1] && *req[1] ? req[1] : NULL;
560 rc = set_pinentry_mode (client->crypto->agent,
561 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
562 : "ask");
563 if (!rc)
565 rc = do_open (ctx, filename, password);
566 if (rc)
567 cleanup_client (client);
569 cleanup_crypto_stage1 (client->crypto);
573 pthread_cleanup_pop (1);
575 if (!rc && client->flags & FLAG_NEW)
576 rc = send_status (ctx, STATUS_NEWFILE, NULL);
578 (void) kill_scd (client->crypto->agent);
579 return send_error (ctx, rc);
582 static gpg_error_t
583 parse_save_opt_no_passphrase (void *data, void *value)
585 struct client_s *client = data;
587 (void) value;
588 client->opts |= OPT_NO_PASSPHRASE;
589 return 0;
592 static gpg_error_t
593 parse_save_opt_cipher (void *data, void *value)
595 struct client_s *client = data;
596 int algo = cipher_string_to_gcrypt ((char *) value);
597 file_header_t *hdr = &client->crypto->save.hdr;
599 if (algo == -1)
600 return GPG_ERR_INV_VALUE;
602 hdr->flags = set_cipher_flag (hdr->flags, algo);
603 return 0;
606 static gpg_error_t
607 parse_save_opt_keygrip (void *data, void *value)
609 struct client_s *client = data;
611 if (client->crypto->save.pkey)
612 gcry_sexp_release (client->crypto->save.pkey);
614 client->crypto->save.pkey = NULL;
615 return get_pubkey (client->crypto, value, &client->crypto->save.pkey);
618 static gpg_error_t
619 parse_save_opt_sign_keygrip (void *data, void *value)
621 struct client_s *client = data;
623 if (client->crypto->save.sigpkey)
624 gcry_sexp_release (client->crypto->save.sigpkey);
626 client->crypto->save.sigpkey = NULL;
627 return get_pubkey (client->crypto, value, &client->crypto->save.sigpkey);
630 static gpg_error_t
631 parse_opt_s2k_count (void *data, void *value)
633 struct client_s *client = data;
634 char *v = value;
636 if (!v || !*v)
637 return GPG_ERR_INV_VALUE;
639 client->crypto->save.s2k_count = strtoul (v, NULL, 10);
640 return 0;
643 static gpg_error_t
644 parse_save_opt_iterations (void *data, void *value)
646 struct client_s *client = data;
647 char *v = value, *p;
648 uint64_t n;
650 if (!v || !*v)
651 return GPG_ERR_INV_VALUE;
653 errno = 0;
654 n = strtoull (v, &p, 10);
655 if (n == UINT64_MAX && errno)
656 return gpg_error_from_syserror ();
657 else if (p && *p)
658 return GPG_ERR_INV_VALUE;
660 client->crypto->save.hdr.iterations = n;
661 return 0;
664 static gpg_error_t
665 save_finalize (assuan_context_t ctx)
667 struct client_s *client = assuan_get_pointer (ctx);
668 gpg_error_t rc;
669 xmlChar *xmlbuf = NULL;
670 int xmlbuflen;
671 gcry_sexp_t pubkey = client->crypto->save.pkey;
672 gcry_sexp_t sigkey = client->crypto->save.sigpkey;
673 struct cache_data_s *cdata;
675 xmlDocDumpFormatMemory (client->doc, &xmlbuf, &xmlbuflen, 0);
676 if (!xmlbuf)
677 return GPG_ERR_ENOMEM;
679 if (!pubkey)
680 pubkey = client->crypto->pkey_sexp;
682 if (!sigkey)
684 sigkey = client->crypto->sigpkey_sexp;
685 if (!sigkey)
687 gcry_sexp_build (&client->crypto->save.sigpkey, 0, "%S", pubkey);
688 sigkey = client->crypto->save.sigpkey;
692 pthread_cleanup_push (xmlFree, xmlbuf);
693 rc = encrypt_data_file (client->ctx, client->crypto, pubkey, sigkey,
694 client->filename, xmlbuf, xmlbuflen);
695 if (pubkey == client->crypto->save.pkey)
697 if (!rc)
699 gcry_sexp_release (client->crypto->pkey_sexp);
700 client->crypto->pkey_sexp = client->crypto->save.pkey;
701 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
702 client->crypto->grip);
704 else
705 gcry_sexp_release (pubkey);
707 client->crypto->save.pkey = NULL;
710 if (!rc)
711 gcry_pk_get_keygrip (sigkey, client->crypto->sign_grip);
713 if (sigkey == client->crypto->save.sigpkey)
715 if (!rc)
717 if (client->crypto->sigpkey_sexp)
718 gcry_sexp_release (client->crypto->sigpkey_sexp);
720 client->crypto->sigpkey_sexp = client->crypto->save.sigpkey;
721 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
722 client->crypto->sign_grip);
724 else
725 gcry_sexp_release (sigkey);
727 client->crypto->save.sigpkey = NULL;
730 if (!rc)
732 /* This is safe since it is a fast operation. */
733 cache_lock ();
734 pthread_cleanup_push (cleanup_cache_mutex, NULL);
735 cdata = cache_get_data (client->md5file);
736 int cached = cdata != NULL;
738 if (!cdata)
739 cdata = xcalloc (1, sizeof (struct cache_data_s));
741 /* Update in case of any --keygrip argument */
742 if (cdata->pubkey)
743 gcry_sexp_release (cdata->pubkey);
745 gcry_sexp_build ((gcry_sexp_t *) & cdata->pubkey, NULL, "%S",
746 client->crypto->pkey_sexp);
748 if (cdata->sigkey)
749 gcry_sexp_release (cdata->sigkey);
751 gcry_sexp_build ((gcry_sexp_t *) & cdata->sigkey, NULL, "%S",
752 client->crypto->sigpkey_sexp);
754 gcry_free (cdata->doc);
755 cdata->doc = NULL;
756 rc = encrypt_xml (NULL, cache_key, cache_keysize, GCRY_CIPHER_AES,
757 xmlbuf, xmlbuflen, &cdata->doc, &cdata->doclen,
758 &cache_iv, &cache_blocksize, 0);
760 rc = cache_set_data (client->md5file, cdata, client->crypto->grip);
761 if (!rc && (!cached || (client->flags & FLAG_NEW)))
762 send_status_all (STATUS_CACHE, NULL);
764 if (!rc)
766 update_checksum (client);
767 client->flags &= ~(FLAG_NEW);
769 else
770 rc = GPG_ERR_ENOMEM;
772 pthread_cleanup_pop (1); // mutex unlock
775 pthread_cleanup_pop (1); // xmlFree
776 return rc;
779 static gpg_error_t
780 parse_opt_reset (void *data, void *value)
782 struct client_s *client = data;
784 (void) value;
785 client->opts |= OPT_RESET;
786 return 0;
789 static gpg_error_t
790 save_command (assuan_context_t ctx, char *line)
792 struct client_s *client = assuan_get_pointer (ctx);
793 gpg_error_t rc;
794 struct stat st;
795 struct argv_s *args[] = {
796 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG,
797 parse_save_opt_no_passphrase},
798 &(struct argv_s) {"cipher", OPTION_TYPE_ARG, parse_save_opt_cipher},
799 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
800 parse_opt_inquire},
801 &(struct argv_s) {"keygrip", OPTION_TYPE_ARG, parse_save_opt_keygrip},
802 &(struct argv_s) {"sign-keygrip", OPTION_TYPE_ARG,
803 parse_save_opt_sign_keygrip},
804 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
805 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
806 &(struct argv_s) {"cipher-iterations", OPTION_TYPE_ARG,
807 parse_save_opt_iterations},
808 NULL
811 cleanup_save (&client->crypto->save);
812 memcpy (&client->crypto->save.hdr, &client->crypto->hdr,
813 sizeof (file_header_t));
814 client->crypto->save.s2k_count =
815 config_get_ulong (client->filename, "s2k_count");
817 if (client->flags & FLAG_NEW)
818 client->crypto->save.hdr.iterations =
819 config_get_ulonglong (client->filename, "cipher_iterations");
821 rc = parse_options (&line, args, client);
822 if (rc)
823 return send_error (ctx, rc);
825 if ((client->opts & OPT_NO_PASSPHRASE) && !(client->flags & FLAG_NEW)
826 && !(client->opts & OPT_INQUIRE))
827 return send_error (ctx, GPG_ERR_WRONG_KEY_USAGE);
829 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
830 return send_error (ctx, gpg_error_from_syserror ());
832 if (errno != ENOENT && !S_ISREG (st.st_mode))
834 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
835 return send_error (ctx, GPG_ERR_ENOANO);
838 int defer;
839 rc = cache_iscached (client->filename, &defer);
840 if (!rc && defer)
842 log_write ("%s: %s", client->filename,
843 pwmd_strerror (GPG_ERR_KEY_EXPIRED));
844 client->opts |= OPT_RESET;
847 if (client->opts & OPT_RESET)
849 rc = cache_clear (client->md5file);
850 if (rc)
851 return send_error (ctx, rc);
853 send_status_all (STATUS_CACHE, NULL);
856 rc = update_element_mtime (xmlDocGetRootElement (client->doc));
857 if (rc)
858 return send_error (ctx, rc);
860 if (!rc && !client->crypto->save.pkey &&
861 (client->flags & FLAG_NEW || client->opts & OPT_INQUIRE))
863 rc = send_status (client->ctx, STATUS_GENKEY, NULL);
864 if (!rc)
866 struct inquire_data_s idata = { 0 };
867 char *params = client->opts & OPT_INQUIRE ? NULL
868 : default_key_params (client->crypto);
870 pthread_cleanup_push (xfree, params);
871 idata.crypto = client->crypto;
873 if (params)
875 idata.line = params;
876 idata.len = strlen (params);
878 else
879 idata.preset = 1;
881 client->crypto->agent->inquire_data = &idata;
882 client->crypto->agent->inquire_cb = NULL;
883 rc = generate_key (client->crypto, params,
884 (client->opts & OPT_NO_PASSPHRASE), 1);
885 pthread_cleanup_pop (1);
889 if (!rc)
890 rc = save_finalize (ctx);
892 cleanup_crypto_stage1 (client->crypto);
893 (void) kill_scd (client->crypto->agent);
894 return send_error (ctx, rc);
897 static gpg_error_t
898 do_delete (assuan_context_t ctx, char *line)
900 struct client_s *client = assuan_get_pointer (ctx);
901 gpg_error_t rc;
902 char **req;
903 xmlNodePtr n;
905 if (strchr (line, '\t'))
906 req = str_split (line, "\t", 0);
907 else
908 req = str_split (line, " ", 0);
910 if (!req || !*req)
911 return GPG_ERR_SYNTAX;
913 n = find_root_element (client->doc, &req, &rc, NULL, 0, 0);
914 if (!n)
916 strv_free (req);
917 return rc;
921 * No sub-node defined. Remove the entire node (root element).
923 if (!req[1])
925 if (n)
927 rc = unlink_node (n);
928 xmlFreeNode (n);
931 strv_free (req);
932 return rc;
936 find_elements (client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
937 0, 0, NULL, 0);
938 strv_free (req);
939 if (!n)
940 return rc;
942 if (n)
944 rc = unlink_node (n);
945 xmlFreeNode (n);
948 return rc;
951 static gpg_error_t
952 delete_command (assuan_context_t ctx, char *line)
954 struct client_s *client = assuan_get_pointer (ctx);
955 gpg_error_t rc;
956 struct argv_s *args[] = {
957 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
958 NULL
961 rc = parse_options (&line, args, client);
962 if (rc)
963 return send_error (ctx, rc);
965 if (client->opts & OPT_INQUIRE)
967 unsigned char *result;
968 size_t len;
970 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
971 if (rc)
972 return send_error (ctx, rc);
974 line = (char *) result;
977 rc = do_delete (ctx, line);
979 if (client->opts & OPT_INQUIRE)
980 xfree (line);
982 return send_error (ctx, rc);
985 static gpg_error_t
986 store_command (assuan_context_t ctx, char *line)
988 struct client_s *client = assuan_get_pointer (ctx);
989 gpg_error_t rc;
990 size_t len;
991 unsigned char *result;
992 char **req;
993 xmlNodePtr n, parent;
994 int has_content;
995 char *content = NULL;
997 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
998 if (rc)
999 return send_error (ctx, rc);
1001 req = str_split ((char *) result, "\t", 0);
1002 xfree (result);
1004 if (!req || !*req)
1005 return send_error (ctx, GPG_ERR_SYNTAX);
1007 len = strv_length (req);
1008 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1009 if (*(req + 1) && !valid_element_path (req, has_content))
1011 strv_free (req);
1012 return send_error (ctx, GPG_ERR_INV_VALUE);
1015 if (has_content || !*req[len - 1])
1017 has_content = 1;
1018 content = req[len - 1];
1019 req[len - 1] = NULL;
1022 again:
1023 n = find_root_element (client->doc, &req, &rc, NULL, 0, 0);
1024 if (rc && rc == GPG_ERR_ELEMENT_NOT_FOUND)
1026 rc = new_root_element (client->doc, *req);
1027 if (rc)
1029 strv_free (req);
1030 return send_error (ctx, GPG_ERR_SYNTAX);
1033 goto again;
1036 if (!n)
1038 strv_free (req);
1039 return send_error (ctx, rc);
1042 parent = n;
1044 if (req[1] && *req[1])
1046 if (!n->children)
1047 parent = create_elements_cb (n, req + 1, &rc, NULL);
1048 else
1049 parent = find_elements (client->doc, n->children, req + 1, &rc,
1050 NULL, NULL, create_elements_cb, 0, 0, NULL,
1054 if (!rc && len > 1)
1056 n = find_text_node (parent->children);
1057 if (n)
1058 xmlNodeSetContent (n, (xmlChar *) content);
1059 else
1060 xmlNodeAddContent (parent, (xmlChar *) content);
1062 update_element_mtime (parent);
1065 xfree (content);
1066 strv_free (req);
1067 return send_error (ctx, rc);
1070 static gpg_error_t
1071 xfer_data (assuan_context_t ctx, const char *line, int total)
1073 int to_send;
1074 int sent = 0;
1075 gpg_error_t rc;
1076 int progress = config_get_integer ("global", "xfer_progress");
1077 int flush = 0;
1079 progress =
1080 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1081 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1082 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1084 if (rc)
1085 return rc;
1087 again:
1090 if (sent + to_send > total)
1091 to_send = total - sent;
1093 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1094 flush ? 0 : to_send);
1095 if (!rc)
1097 sent += flush ? 0 : to_send;
1099 if ((progress && !(sent % progress) && sent != total) ||
1100 (sent == total && flush))
1101 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1103 if (!flush && !rc && sent == total)
1105 flush = 1;
1106 goto again;
1110 while (!rc && sent < total);
1112 return rc;
1115 static gpg_error_t
1116 do_get (assuan_context_t ctx, char *line)
1118 struct client_s *client = assuan_get_pointer (ctx);
1119 gpg_error_t rc;
1120 char **req;
1121 xmlNodePtr n;
1123 req = str_split (line, "\t", 0);
1125 if (!req || !*req)
1127 strv_free (req);
1128 return GPG_ERR_SYNTAX;
1131 n = find_root_element (client->doc, &req, &rc, NULL, 0, 0);
1132 if (!n)
1134 strv_free (req);
1135 return rc;
1138 if (req[1])
1140 find_elements (client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1141 0, 0, NULL, 0);
1143 strv_free (req);
1144 if (rc)
1145 return rc;
1147 if (!n || !n->children)
1148 return GPG_ERR_NO_DATA;
1150 n = find_text_node (n->children);
1151 if (!n || !n->content || !*n->content)
1152 return GPG_ERR_NO_DATA;
1154 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1155 return rc;
1158 static gpg_error_t
1159 get_command (assuan_context_t ctx, char *line)
1161 struct client_s *client = assuan_get_pointer (ctx);
1162 gpg_error_t rc;
1163 struct argv_s *args[] = {
1164 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1165 NULL
1168 rc = parse_options (&line, args, client);
1169 if (rc)
1170 return send_error (ctx, rc);
1172 if (client->opts & OPT_INQUIRE)
1174 unsigned char *result;
1175 size_t len;
1177 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1178 if (rc)
1179 return send_error (ctx, rc);
1181 line = (char *) result;
1184 rc = do_get (ctx, line);
1186 if (client->opts & OPT_INQUIRE)
1187 xfree (line);
1189 return send_error (ctx, rc);
1192 static void list_command_cleanup1 (void *arg);
1193 static gpg_error_t
1194 realpath_command (assuan_context_t ctx, char *line)
1196 struct string_s *string = NULL;
1197 gpg_error_t rc;
1198 struct client_s *client = assuan_get_pointer (ctx);
1199 struct argv_s *args[] = {
1200 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1201 NULL
1204 rc = parse_options (&line, args, client);
1205 if (rc)
1206 return send_error (ctx, rc);
1208 if (client->opts & OPT_INQUIRE)
1210 unsigned char *result;
1211 size_t len;
1213 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1214 if (rc)
1215 return send_error (ctx, rc);
1217 line = (char *) result;
1220 rc = build_realpath (client->doc, line, &string);
1221 if (!rc)
1223 pthread_cleanup_push (list_command_cleanup1, string);
1224 rc = xfer_data (ctx, string->str, string->len);
1225 pthread_cleanup_pop (1);
1228 if (client->opts & OPT_INQUIRE)
1229 xfree (line);
1231 return send_error (ctx, rc);
1234 static void
1235 list_command_cleanup1 (void *arg)
1237 if (arg)
1238 string_free ((struct string_s *) arg, 1);
1241 static void
1242 list_command_cleanup2 (void *arg)
1244 struct element_list_s *elements = arg;
1246 if (elements)
1248 if (elements->list)
1250 int total = slist_length (elements->list);
1251 int i;
1253 for (i = 0; i < total; i++)
1255 char *tmp = slist_nth_data (elements->list, i);
1256 xfree (tmp);
1259 slist_free (elements->list);
1262 if (elements->prefix)
1263 xfree (elements->prefix);
1265 if (elements->req)
1266 strv_free (elements->req);
1268 xfree (elements);
1272 static gpg_error_t
1273 parse_list_opt_norecurse (void *data, void *value)
1275 struct client_s *client = data;
1277 client->opts &= ~(OPT_LIST_RECURSE);
1278 return 0;
1281 static gpg_error_t
1282 parse_list_opt_verbose (void *data, void *value)
1284 struct client_s *client = data;
1286 client->opts |= OPT_LIST_VERBOSE;
1287 return 0;
1290 static gpg_error_t
1291 parse_list_opt_target (void *data, void *value)
1293 struct client_s *client = data;
1295 client->opts |= OPT_LIST_WITH_TARGET;
1296 return 0;
1299 static gpg_error_t
1300 parse_list_opt_all (void *data, void *value)
1302 struct client_s *client = data;
1304 client->opts |= OPT_LIST_ALL;
1305 return 0;
1308 static gpg_error_t
1309 list_path_once (struct client_s *client, char *line,
1310 struct element_list_s *elements, struct string_s *result)
1312 gpg_error_t rc;
1314 elements->req = str_split (line, " ", 0);
1315 if (!elements->req)
1316 strv_printf (&elements->req, "%s", line);
1318 rc = create_path_list (client->doc, elements, *elements->req);
1319 if (rc == GPG_ERR_ELOOP && elements->verbose)
1320 rc = 0;
1322 if (!rc)
1324 if (elements)
1326 int total = slist_length (elements->list);
1327 int i;
1329 if (!total)
1330 rc = GPG_ERR_NO_DATA;
1332 if (!rc)
1334 if (!rc)
1336 for (i = 0; i < total; i++)
1338 char *tmp = slist_nth_data (elements->list, i);
1340 string_append_printf (result, "%s%s", tmp,
1341 i + 1 == total ? "" : "\n");
1346 else
1347 rc = GPG_ERR_NO_DATA;
1350 return rc;
1353 static int
1354 has_list_flag (char *path, char *flags)
1356 char *p = path;
1358 while (*p && *++p != ' ');
1360 if (!*p)
1361 return 0;
1363 for (; *p; p++)
1365 char *f;
1367 for (f = flags; *f && *f != ' '; f++)
1369 if (*p == *f)
1370 return 1;
1374 return 0;
1377 static gpg_error_t
1378 do_list (assuan_context_t ctx, char *line)
1380 struct client_s *client = assuan_get_pointer (ctx);
1381 gpg_error_t rc;
1382 struct element_list_s *elements = NULL;
1384 elements = xcalloc (1, sizeof (struct element_list_s));
1385 if (!elements)
1386 return GPG_ERR_ENOMEM;
1388 elements->recurse = client->opts & OPT_LIST_RECURSE;
1389 elements->verbose =
1390 (client->opts & OPT_LIST_VERBOSE) | (client->opts & OPT_LIST_WITH_TARGET);
1391 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1393 if (!line || !*line)
1395 struct string_s *str = NULL;
1397 pthread_cleanup_push (list_command_cleanup2, elements);
1398 rc = list_root_elements (client->doc, &str, elements->verbose,
1399 elements->with_target);
1400 pthread_cleanup_pop (1);
1401 pthread_cleanup_push (list_command_cleanup1, str);
1403 if (!rc)
1405 if (client->opts & OPT_LIST_ALL)
1407 char **roots = str_split (str->str, "\n", 0);
1408 char **p;
1410 pthread_cleanup_push (req_cleanup, roots);
1411 string_truncate (str, 0);
1413 for (p = roots; *p; p++)
1415 if (strchr (*p, ' '))
1417 if (has_list_flag (*p, "EO"))
1419 string_append_printf (str, "%s%s", *p,
1420 *(p + 1) ? "\n" : "");
1421 continue;
1425 elements = xcalloc (1, sizeof (struct element_list_s));
1426 if (!elements)
1428 rc = GPG_ERR_ENOMEM;
1429 break;
1432 elements->recurse = client->opts & OPT_LIST_RECURSE;
1433 elements->verbose =
1434 (client->opts & OPT_LIST_VERBOSE) | (client->opts &
1435 OPT_LIST_WITH_TARGET);
1436 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1437 pthread_cleanup_push (list_command_cleanup2, elements);
1438 rc = list_path_once (client, *p, elements, str);
1439 pthread_cleanup_pop (1);
1440 if (rc)
1441 break;
1443 if (*(p + 1))
1444 string_append (str, "\n");
1447 pthread_cleanup_pop (1);
1450 if (!rc)
1451 rc = xfer_data (ctx, str->str, str->len);
1454 pthread_cleanup_pop (1);
1455 return rc;
1458 pthread_cleanup_push (list_command_cleanup2, elements);
1459 struct string_s *str = string_new (NULL);
1460 pthread_cleanup_push (list_command_cleanup1, str);
1461 rc = list_path_once (client, line, elements, str);
1462 if (!rc)
1463 rc = xfer_data (ctx, str->str, str->len);
1465 pthread_cleanup_pop (1);
1466 pthread_cleanup_pop (1);
1467 return rc;
1470 static gpg_error_t
1471 list_command (assuan_context_t ctx, char *line)
1473 struct client_s *client = assuan_get_pointer (ctx);
1474 gpg_error_t rc;
1475 struct argv_s *args[] = {
1476 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG,
1477 parse_list_opt_norecurse},
1478 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_list_opt_verbose},
1479 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1480 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG,
1481 parse_list_opt_target},
1482 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_all},
1483 NULL
1486 if (disable_list_and_dump == 1)
1487 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1489 client->opts |= OPT_LIST_RECURSE;
1490 rc = parse_options (&line, args, client);
1491 if (rc)
1492 return send_error (ctx, rc);
1494 if (client->opts & OPT_LIST_ALL)
1495 client->opts |= OPT_LIST_RECURSE | OPT_LIST_VERBOSE;
1497 if (client->opts & OPT_INQUIRE)
1499 unsigned char *result;
1500 size_t len;
1502 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1503 if (rc)
1504 return send_error (ctx, rc);
1506 line = (char *) result;
1509 rc = do_list (ctx, line);
1511 if (client->opts & OPT_INQUIRE)
1512 xfree (line);
1514 return send_error (ctx, rc);
1518 * req[0] - element path
1520 static gpg_error_t
1521 attribute_list (assuan_context_t ctx, char **req)
1523 struct client_s *client = assuan_get_pointer (ctx);
1524 char **attrlist = NULL;
1525 int i = 0;
1526 char **path = NULL;
1527 xmlAttrPtr a;
1528 xmlNodePtr n, an;
1529 char *line;
1530 gpg_error_t rc;
1532 if (!req || !req[0])
1533 return GPG_ERR_SYNTAX;
1535 if ((path = str_split (req[0], "\t", 0)) == NULL)
1538 * The first argument may be only a root element.
1540 if ((path = str_split (req[0], " ", 0)) == NULL)
1541 return GPG_ERR_SYNTAX;
1544 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
1546 if (!n)
1548 strv_free (path);
1549 return rc;
1552 if (path[1])
1554 n = find_elements (client->doc, n->children, path + 1, &rc,
1555 NULL, NULL, NULL, 0, 0, NULL, 0);
1557 if (!n)
1559 strv_free (path);
1560 return rc;
1564 strv_free (path);
1566 for (a = n->properties; a; a = a->next)
1568 char **pa;
1570 if ((pa = xrealloc (attrlist, (i + 2) * sizeof (char *))) == NULL)
1572 if (attrlist)
1573 strv_free (attrlist);
1575 log_write ("%s(%i): %s", __FILE__, __LINE__,
1576 pwmd_strerror (GPG_ERR_ENOMEM));
1577 return GPG_ERR_ENOMEM;
1580 attrlist = pa;
1581 an = a->children;
1582 attrlist[i] = str_asprintf ("%s %s", (char *) a->name,
1584 && an->content ? (char *) an->content : "");
1586 if (!attrlist[i])
1588 strv_free (attrlist);
1589 log_write ("%s(%i): %s", __FILE__, __LINE__,
1590 pwmd_strerror (GPG_ERR_ENOMEM));
1591 return GPG_ERR_ENOMEM;
1594 attrlist[++i] = NULL;
1597 if (!attrlist)
1598 return GPG_ERR_NO_DATA;
1600 line = strv_join ("\n", attrlist);
1602 if (!line)
1604 log_write ("%s(%i): %s", __FILE__, __LINE__,
1605 pwmd_strerror (GPG_ERR_ENOMEM));
1606 strv_free (attrlist);
1607 return GPG_ERR_ENOMEM;
1610 pthread_cleanup_push (xfree, line);
1611 pthread_cleanup_push (req_cleanup, attrlist);
1612 rc = xfer_data (ctx, line, strlen (line));
1613 pthread_cleanup_pop (1);
1614 pthread_cleanup_pop (1);
1615 return rc;
1619 * req[0] - attribute
1620 * req[1] - element path
1622 static gpg_error_t
1623 attribute_delete (struct client_s *client, char **req)
1625 xmlNodePtr n;
1626 char **path = NULL;
1627 gpg_error_t rc;
1629 if (!req || !req[0] || !req[1])
1630 return GPG_ERR_SYNTAX;
1632 if (!strcmp (req[0], "_name"))
1633 return GPG_ERR_INV_ATTR;
1635 if ((path = str_split (req[1], "\t", 0)) == NULL)
1638 * The first argument may be only a root element.
1640 if ((path = str_split (req[1], " ", 0)) == NULL)
1641 return GPG_ERR_SYNTAX;
1644 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
1645 if (!n)
1646 goto fail;
1648 if (path[1])
1650 n = find_elements (client->doc, n->children, path + 1, &rc,
1651 NULL, NULL, NULL, 0, 0, NULL, 0);
1652 if (!n)
1653 goto fail;
1656 rc = delete_attribute (n, (xmlChar *) req[0]);
1658 fail:
1659 strv_free (path);
1660 return rc;
1663 static xmlNodePtr
1664 create_element_path (struct client_s *client,
1665 char ***elements, gpg_error_t * rc, xmlNodePtr parent)
1667 char **req = *elements;
1668 char **req_orig = strv_dup (req);
1669 xmlNodePtr n = NULL;
1671 *rc = 0;
1673 if (!req_orig)
1675 *rc = GPG_ERR_ENOMEM;
1676 log_write ("%s(%i): %s", __FILE__, __LINE__,
1677 pwmd_strerror (GPG_ERR_ENOMEM));
1678 goto fail;
1681 again:
1682 n = find_root_element (client->doc, &req, rc, NULL, 0, 0);
1683 if (!n)
1685 if (*rc != GPG_ERR_ELEMENT_NOT_FOUND)
1686 goto fail;
1688 *rc = new_root_element (client->doc, req[0]);
1689 if (*rc)
1690 goto fail;
1692 goto again;
1694 else if (n == parent)
1696 *rc = GPG_ERR_CONFLICT;
1697 goto fail;
1700 if (req[1])
1702 if (!n->children)
1703 n = create_target_elements_cb (n, req + 1, rc, NULL);
1704 else
1705 n = find_elements (client->doc, n->children, req + 1, rc, NULL, NULL,
1706 create_target_elements_cb, 0, 0, parent, 0);
1708 if (!n)
1709 goto fail;
1712 * Reset the position of the element tree now that the elements
1713 * have been created.
1715 strv_free (req);
1716 req = req_orig;
1717 req_orig = NULL;
1718 n = find_root_element (client->doc, &req, rc, NULL, 0, 0);
1719 if (!n)
1720 goto fail;
1722 n = find_elements (client->doc, n->children, req + 1, rc,
1723 NULL, NULL, NULL, 0, 0, NULL, 0);
1724 if (!n)
1725 goto fail;
1728 fail:
1729 if (req_orig)
1730 strv_free (req_orig);
1732 *elements = req;
1733 return n;
1737 * Creates a "target" attribute. When other commands encounter an element with
1738 * this attribute, the element path is modified to the target value. If the
1739 * source element path doesn't exist when using 'ATTR SET target', it is
1740 * created, but the destination element path must exist.
1742 * req[0] - source element path
1743 * req[1] - destination element path
1745 static gpg_error_t
1746 target_attribute (struct client_s *client, char **req)
1748 char **src, **dst, *line = NULL, **odst = NULL;
1749 gpg_error_t rc;
1750 xmlNodePtr n;
1752 if (!req || !req[0] || !req[1])
1753 return GPG_ERR_SYNTAX;
1755 if ((src = str_split (req[0], "\t", 0)) == NULL)
1758 * The first argument may be only a root element.
1760 if ((src = str_split (req[0], " ", 0)) == NULL)
1761 return GPG_ERR_SYNTAX;
1764 if (!valid_element_path (src, 0))
1765 return GPG_ERR_INV_VALUE;
1767 if ((dst = str_split (req[1], "\t", 0)) == NULL)
1770 * The first argument may be only a root element.
1772 if ((dst = str_split (req[1], " ", 0)) == NULL)
1774 rc = GPG_ERR_SYNTAX;
1775 goto fail;
1779 odst = strv_dup (dst);
1780 if (!odst)
1782 rc = GPG_ERR_ENOMEM;
1783 goto fail;
1786 n = find_root_element (client->doc, &dst, &rc, NULL, 0, 0);
1788 * Make sure the destination element path exists.
1790 if (!n)
1791 goto fail;
1793 if (dst[1])
1795 n = find_elements (client->doc, n->children, dst + 1, &rc,
1796 NULL, NULL, NULL, 0, 0, NULL, 0);
1797 if (!n)
1798 goto fail;
1801 rc = validate_target_attribute (client->doc, req[0], n);
1802 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
1803 goto fail;
1805 n = create_element_path (client, &src, &rc, NULL);
1806 if (rc)
1807 goto fail;
1809 line = strv_join ("\t", odst);
1810 if (!line)
1812 rc = GPG_ERR_ENOMEM;
1813 goto fail;
1816 rc = add_attribute (n, "target", line);
1818 fail:
1819 xfree (line);
1820 strv_free (src);
1821 strv_free (dst);
1822 strv_free (odst);
1823 return rc;
1827 * req[0] - attribute
1828 * req[1] - element path
1830 static gpg_error_t
1831 attribute_get (assuan_context_t ctx, char **req)
1833 struct client_s *client = assuan_get_pointer (ctx);
1834 xmlNodePtr n;
1835 xmlChar *a;
1836 char **path = NULL;
1837 gpg_error_t rc;
1839 if (!req || !req[0] || !req[1])
1840 return GPG_ERR_SYNTAX;
1842 if (strchr (req[1], '\t'))
1844 if ((path = str_split (req[1], "\t", 0)) == NULL)
1845 return GPG_ERR_SYNTAX;
1847 else
1849 if ((path = str_split (req[1], " ", 0)) == NULL)
1850 return GPG_ERR_SYNTAX;
1853 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
1855 if (!n)
1856 goto fail;
1858 if (path[1])
1860 n = find_elements (client->doc, n->children, path + 1, &rc,
1861 NULL, NULL, NULL, 0, 0, NULL, 0);
1863 if (!n)
1864 goto fail;
1867 strv_free (path);
1869 if ((a = xmlGetProp (n, (xmlChar *) req[0])) == NULL)
1870 return GPG_ERR_NOT_FOUND;
1872 pthread_cleanup_push (xmlFree, a);
1874 if (*a)
1875 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
1876 else
1877 rc = GPG_ERR_NO_DATA;
1879 pthread_cleanup_pop (1);
1880 return rc;
1882 fail:
1883 strv_free (path);
1884 return rc;
1888 * req[0] - attribute
1889 * req[1] - element path
1890 * req[2] - value
1892 static gpg_error_t
1893 attribute_set (struct client_s *client, char **req)
1895 char **path = NULL;
1896 gpg_error_t rc;
1897 xmlNodePtr n;
1899 if (!req || !req[0] || !req[1])
1900 return GPG_ERR_SYNTAX;
1903 * Reserved attribute names.
1905 if (!strcmp (req[0], "_name"))
1906 return GPG_ERR_INV_ATTR;
1907 else if (!strcmp (req[0], "target"))
1908 return target_attribute (client, req + 1);
1910 if ((path = str_split (req[1], "\t", 0)) == NULL)
1913 * The first argument may be only a root element.
1915 if ((path = str_split (req[1], " ", 0)) == NULL)
1916 return GPG_ERR_SYNTAX;
1919 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
1921 if (!n)
1922 goto fail;
1924 if (path[1])
1926 n = find_elements (client->doc, n->children, path + 1, &rc,
1927 NULL, NULL, NULL, 0, 0, NULL, 0);
1929 if (!n)
1930 goto fail;
1933 rc = add_attribute (n, req[0], req[2]);
1935 fail:
1936 strv_free (path);
1937 return rc;
1941 * req[0] - command
1942 * req[1] - attribute name or element path if command is LIST
1943 * req[2] - element path
1944 * req[2] - element path or value
1947 static gpg_error_t
1948 do_attr (assuan_context_t ctx, char *line)
1950 struct client_s *client = assuan_get_pointer (ctx);
1951 gpg_error_t rc = 0;
1952 char **req;
1954 req = str_split (line, " ", 4);
1955 if (!req || !req[0] || !req[1])
1957 strv_free (req);
1958 return GPG_ERR_SYNTAX;
1961 pthread_cleanup_push (req_cleanup, req);
1963 if (strcasecmp (req[0], "SET") == 0)
1964 rc = attribute_set (client, req + 1);
1965 else if (strcasecmp (req[0], "GET") == 0)
1966 rc = attribute_get (ctx, req + 1);
1967 else if (strcasecmp (req[0], "DELETE") == 0)
1968 rc = attribute_delete (client, req + 1);
1969 else if (strcasecmp (req[0], "LIST") == 0)
1970 rc = attribute_list (ctx, req + 1);
1971 else
1972 rc = GPG_ERR_SYNTAX;
1974 pthread_cleanup_pop (1);
1975 return rc;
1978 static gpg_error_t
1979 attr_command (assuan_context_t ctx, char *line)
1981 struct client_s *client = assuan_get_pointer (ctx);
1982 gpg_error_t rc;
1983 struct argv_s *args[] = {
1984 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1985 NULL
1988 rc = parse_options (&line, args, client);
1989 if (rc)
1990 return send_error (ctx, rc);
1992 if (client->opts & OPT_INQUIRE)
1994 unsigned char *result;
1995 size_t len;
1997 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1998 if (rc)
1999 return send_error (ctx, rc);
2001 line = (char *) result;
2004 rc = do_attr (ctx, line);
2006 if (client->opts & OPT_INQUIRE)
2007 xfree (line);
2009 return send_error (ctx, rc);
2012 static gpg_error_t
2013 parse_iscached_opt_lock (void *data, void *value)
2015 struct client_s *client = data;
2017 (void) value;
2018 client->opts |= OPT_LOCK;
2019 return 0;
2022 static gpg_error_t
2023 iscached_command (assuan_context_t ctx, char *line)
2025 struct client_s *client = assuan_get_pointer (ctx);
2026 gpg_error_t rc;
2027 unsigned char md5file[16];
2028 struct argv_s *args[] = {
2029 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2030 NULL
2033 if (!line || !*line)
2034 return send_error (ctx, GPG_ERR_SYNTAX);
2036 rc = parse_options (&line, args, client);
2037 if (rc)
2038 return send_error (ctx, rc);
2039 else if (!valid_filename (line))
2040 return send_error (ctx, GPG_ERR_INV_VALUE);
2042 rc = cache_iscached (line, NULL);
2043 if (client->opts & OPT_LOCK
2044 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2046 gpg_error_t trc = rc;
2047 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2048 if (memcmp (md5file, client->md5file, 16))
2049 cleanup_client (client);
2051 memcpy (client->md5file, md5file, 16);
2052 rc = do_lock (client, 1);
2053 if (!rc)
2054 rc = trc;
2057 return send_error (ctx, rc);
2060 static gpg_error_t
2061 clearcache_command (assuan_context_t ctx, char *line)
2063 gpg_error_t rc = 0, all_rc = 0;
2064 unsigned char md5file[16];
2065 int i;
2066 int t;
2067 int all = 0;
2068 struct client_thread_s *once = NULL;
2070 cache_lock ();
2071 MUTEX_LOCK (&cn_mutex);
2072 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
2074 if (!line || !*line)
2075 all = 1;
2077 t = slist_length (cn_thread_list);
2079 for (i = 0; i < t; i++)
2081 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2083 if (!thd->cl)
2084 continue;
2086 /* Lock each connected clients' file mutex to prevent any other client
2087 * from accessing the cache entry (the file mutex is locked upon
2088 * command startup). The cache for the entry is not cleared if the
2089 * file mutex is locked by another client to prevent this function
2090 * from blocking.
2092 if (all)
2094 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
2095 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2097 if (pthread_equal (pthread_self (), thd->tid))
2098 rc = 0;
2099 else
2101 if (!thd->cl->filename ||
2102 cache_iscached (thd->cl->filename,
2103 NULL) == GPG_ERR_NO_DATA)
2105 rc = 0;
2106 continue;
2109 cache_defer_clear (thd->cl->md5file);
2112 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2114 rc = 0;
2115 continue;
2118 if (!rc)
2120 rc = cache_clear (thd->cl->md5file);
2121 if (rc)
2122 all_rc = rc;
2124 cache_unlock_mutex (thd->cl->md5file, 0);
2126 else
2127 all_rc = rc;
2129 rc = 0;
2131 /* A single data filename was specified. Lock only this data file
2132 * mutex and free the cache entry. */
2133 else
2135 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2137 if (!memcmp (thd->cl->md5file, md5file, sizeof (md5file)))
2139 rc =
2140 cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
2141 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2143 if (pthread_equal (pthread_self (), thd->tid))
2144 rc = 0;
2147 if (!rc)
2149 once = thd;
2150 rc = cache_clear (thd->cl->md5file);
2151 cache_unlock_mutex (thd->cl->md5file, 0);
2153 else
2154 cache_defer_clear (thd->cl->md5file);
2156 break;
2161 /* Only connected clients' cache entries have been cleared. Now clear any
2162 * remaining cache entries without clients but only if there wasn't an
2163 * error from above since this would defeat the locking check of the
2164 * remaining entries. */
2165 if (!all_rc && all)
2166 cache_clear (NULL);
2167 /* No clients are using the specified file. */
2168 else if (!all_rc && !rc && !once)
2170 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2171 rc = cache_clear (md5file);
2174 /* Release the connection mutex. */
2175 pthread_cleanup_pop (1);
2176 cache_unlock ();
2178 if (!rc)
2179 send_status_all (STATUS_CACHE, NULL);
2181 /* One or more files were locked while clearing all cache entries. */
2182 if (all_rc)
2183 rc = all_rc;
2185 return send_error (ctx, rc);
2188 static gpg_error_t
2189 cachetimeout_command (assuan_context_t ctx, char *line)
2191 unsigned char md5file[16];
2192 int timeout;
2193 char **req = str_split (line, " ", 0);
2194 char *p;
2195 gpg_error_t rc = 0;
2197 if (!req || !*req || !req[1])
2199 strv_free (req);
2200 return send_error (ctx, GPG_ERR_SYNTAX);
2203 errno = 0;
2204 timeout = (int) strtol (req[1], &p, 10);
2205 if (errno != 0 || *p != 0 || timeout < -1)
2207 strv_free (req);
2208 return send_error (ctx, GPG_ERR_SYNTAX);
2211 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, req[0], strlen (req[0]));
2213 rc = cache_set_timeout (md5file, timeout);
2214 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2216 rc = 0;
2217 MUTEX_LOCK (&rcfile_mutex);
2218 config_set_int_param (&global_config, req[0], "cache_timeout", req[1]);
2219 MUTEX_UNLOCK (&rcfile_mutex);
2222 strv_free (req);
2223 return send_error (ctx, rc);
2226 static gpg_error_t
2227 dump_command (assuan_context_t ctx, char *line)
2229 xmlChar *xml;
2230 int len;
2231 struct client_s *client = assuan_get_pointer (ctx);
2232 gpg_error_t rc;
2234 if (disable_list_and_dump == 1)
2235 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2237 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2239 if (!xml)
2241 log_write ("%s(%i): %s", __FILE__, __LINE__,
2242 pwmd_strerror (GPG_ERR_ENOMEM));
2243 return send_error (ctx, GPG_ERR_ENOMEM);
2246 pthread_cleanup_push (xmlFree, xml);
2247 rc = xfer_data (ctx, (char *) xml, len);
2248 pthread_cleanup_pop (1);
2249 return send_error (ctx, rc);
2252 static gpg_error_t
2253 getconfig_command (assuan_context_t ctx, char *line)
2255 struct client_s *client = assuan_get_pointer (ctx);
2256 gpg_error_t rc = 0;
2257 char filename[255] = { 0 }, param[747] =
2260 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2262 if (!line || !*line)
2263 return send_error (ctx, GPG_ERR_SYNTAX);
2265 if (strchr (line, ' '))
2267 sscanf (line, " %254[^ ] %746c", filename, param);
2268 paramp = param;
2269 fp = filename;
2272 if (fp && !valid_filename (fp))
2273 return send_error (ctx, GPG_ERR_INV_VALUE);
2275 paramp = str_down (paramp);
2276 if (!strcmp (paramp, "cipher") && fp)
2278 struct crypto_s *crypto;
2280 rc = init_client_crypto (&crypto);
2281 if (!rc)
2283 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2284 if (!rc)
2286 const char *t =
2287 gcry_cipher_algo_name (cipher_to_gcrypt (crypto->hdr.flags));
2288 if (t)
2290 tmp = str_dup (t);
2291 if (tmp)
2292 str_down (tmp);
2297 cleanup_crypto (&crypto);
2298 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2299 return send_error (ctx, rc);
2301 if (!rc && tmp)
2302 goto done;
2304 else if (!strcmp (paramp, "cipher_iterations") && fp)
2306 struct crypto_s *crypto;
2308 rc = init_client_crypto (&crypto);
2309 if (!rc)
2311 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2312 if (!rc)
2314 tmp = str_asprintf ("%llu",
2315 (unsigned long long) crypto->hdr.
2316 iterations);
2317 if (!tmp)
2318 rc = GPG_ERR_ENOMEM;
2322 cleanup_crypto (&crypto);
2323 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2324 return send_error (ctx, rc);
2326 if (!rc && tmp)
2327 goto done;
2329 else if (!strcmp (paramp, "passphrase"))
2330 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2332 p = config_get_value (fp ? fp : "global", paramp);
2333 if (!p)
2334 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2336 tmp = expand_homedir (p);
2337 xfree (p);
2338 if (!tmp)
2340 log_write ("%s(%i): %s", __FILE__, __LINE__,
2341 pwmd_strerror (GPG_ERR_ENOMEM));
2342 return send_error (ctx, GPG_ERR_ENOMEM);
2345 done:
2346 p = tmp;
2347 pthread_cleanup_push (xfree, p);
2348 rc = xfer_data (ctx, p, strlen (p));
2349 pthread_cleanup_pop (1);
2350 return send_error (ctx, rc);
2353 struct xpath_s
2355 xmlXPathContextPtr xp;
2356 xmlXPathObjectPtr result;
2357 xmlBufferPtr buf;
2358 char **req;
2361 static void
2362 xpath_command_cleanup (void *arg)
2364 struct xpath_s *xpath = arg;
2366 if (!xpath)
2367 return;
2369 req_cleanup (xpath->req);
2371 if (xpath->buf)
2372 xmlBufferFree (xpath->buf);
2374 if (xpath->result)
2375 xmlXPathFreeObject (xpath->result);
2377 if (xpath->xp)
2378 xmlXPathFreeContext (xpath->xp);
2381 static gpg_error_t
2382 do_xpath (assuan_context_t ctx, char *line)
2384 gpg_error_t rc;
2385 struct client_s *client = assuan_get_pointer (ctx);
2386 struct xpath_s _x = { 0 };
2387 struct xpath_s *xpath = &_x;
2389 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2391 if (strv_printf (&xpath->req, "%s", line) == 0)
2392 return GPG_ERR_ENOMEM;
2395 xpath->xp = xmlXPathNewContext (client->doc);
2396 if (!xpath->xp)
2398 rc = GPG_ERR_BAD_DATA;
2399 goto fail;
2402 xpath->result =
2403 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2404 if (!xpath->result)
2406 rc = GPG_ERR_BAD_DATA;
2407 goto fail;
2410 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2412 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2413 goto fail;
2416 rc = recurse_xpath_nodeset (client->doc, xpath->result->nodesetval,
2417 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2418 NULL);
2419 if (rc)
2420 goto fail;
2421 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2423 rc = GPG_ERR_NO_DATA;
2424 goto fail;
2426 else if (xpath->req[1])
2428 rc = 0;
2429 goto fail;
2432 pthread_cleanup_push (xpath_command_cleanup, &xpath);
2433 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2434 xmlBufferLength (xpath->buf));
2435 pthread_cleanup_pop (0);
2436 fail:
2437 xpath_command_cleanup (xpath);
2438 return rc;
2441 static gpg_error_t
2442 xpath_command (assuan_context_t ctx, char *line)
2444 struct client_s *client = assuan_get_pointer (ctx);
2445 gpg_error_t rc;
2446 struct argv_s *args[] = {
2447 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2448 NULL
2451 if (disable_list_and_dump == 1)
2452 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2454 rc = parse_options (&line, args, client);
2455 if (rc)
2456 return send_error (ctx, rc);
2458 if (client->opts & OPT_INQUIRE)
2460 unsigned char *result;
2461 size_t len;
2463 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2464 if (rc)
2465 return send_error (ctx, rc);
2467 line = (char *) result;
2470 if (!line || !*line)
2471 rc = GPG_ERR_SYNTAX;
2473 if (!rc)
2474 rc = do_xpath (ctx, line);
2476 if (client->opts & OPT_INQUIRE)
2477 xfree (line);
2479 return send_error (ctx, rc);
2482 static gpg_error_t
2483 do_xpathattr (assuan_context_t ctx, char *line)
2485 struct client_s *client = assuan_get_pointer (ctx);
2486 gpg_error_t rc;
2487 char **req = NULL;
2488 int cmd = 0; //SET
2489 struct xpath_s _x = { 0 };
2490 struct xpath_s *xpath = &_x;
2492 if ((req = str_split (line, " ", 3)) == NULL)
2493 return GPG_ERR_ENOMEM;
2495 if (!req[0])
2497 rc = GPG_ERR_SYNTAX;
2498 goto fail;
2501 if (!strcasecmp (req[0], "SET"))
2502 cmd = 0;
2503 else if (!strcasecmp (req[0], "DELETE"))
2504 cmd = 1;
2505 else
2507 rc = GPG_ERR_SYNTAX;
2508 goto fail;
2511 if (!req[1] || !req[2])
2513 rc = GPG_ERR_SYNTAX;
2514 goto fail;
2517 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2519 rc = GPG_ERR_ENOMEM;
2520 goto fail;
2523 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2525 rc = GPG_ERR_SYNTAX;
2526 goto fail;
2529 xpath->xp = xmlXPathNewContext (client->doc);
2530 if (!xpath->xp)
2532 rc = GPG_ERR_BAD_DATA;
2533 goto fail;
2536 xpath->result =
2537 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2538 if (!xpath->result)
2540 rc = GPG_ERR_BAD_DATA;
2541 goto fail;
2544 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2546 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2547 goto fail;
2550 rc = recurse_xpath_nodeset (client->doc, xpath->result->nodesetval,
2551 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2552 (xmlChar *) req[1]);
2554 fail:
2555 xpath_command_cleanup (xpath);
2556 strv_free (req);
2557 return rc;
2560 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2561 static gpg_error_t
2562 xpathattr_command (assuan_context_t ctx, char *line)
2564 struct client_s *client = assuan_get_pointer (ctx);
2565 gpg_error_t rc;
2566 struct argv_s *args[] = {
2567 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2568 NULL
2571 if (disable_list_and_dump == 1)
2572 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2574 rc = parse_options (&line, args, client);
2575 if (rc)
2576 return send_error (ctx, rc);
2578 if (client->opts & OPT_INQUIRE)
2580 unsigned char *result;
2581 size_t len;
2583 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2584 if (rc)
2585 return send_error (ctx, rc);
2587 line = (char *) result;
2590 if (!line || !*line)
2591 rc = GPG_ERR_SYNTAX;
2593 if (!rc)
2594 rc = do_xpathattr (ctx, line);
2596 if (client->opts & OPT_INQUIRE)
2597 xfree (line);
2599 return send_error (ctx, rc);
2602 static gpg_error_t
2603 do_import (struct client_s *client, unsigned char *line)
2605 char **req, **path = NULL, **path_orig = NULL, *content;
2606 xmlDocPtr doc = NULL;
2607 xmlNodePtr n, root, copy;
2608 gpg_error_t rc;
2610 req = str_split ((char *) line, "\t", 2);
2611 xfree (line);
2612 if (!req || !*req)
2613 return GPG_ERR_SYNTAX;
2615 content = req[0];
2616 path = str_split (req[1], "\t", 0);
2617 if (!content || !*content)
2619 rc = GPG_ERR_SYNTAX;
2620 goto fail;
2623 if (path && !valid_element_path (path, 0))
2625 rc = GPG_ERR_INV_VALUE;
2626 goto fail;
2629 doc = xmlReadDoc ((xmlChar *) content, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2630 if (!doc)
2632 rc = GPG_ERR_BAD_DATA;
2633 goto fail;
2636 root = xmlDocGetRootElement (doc);
2637 rc = validate_import (root);
2638 if (rc)
2639 goto fail;
2641 if (path)
2643 path_orig = strv_dup (path);
2644 if (!path_orig)
2646 log_write ("%s(%i): %s", __FILE__, __LINE__,
2647 pwmd_strerror (GPG_ERR_ENOMEM));
2648 rc = GPG_ERR_ENOMEM;
2649 goto fail;
2652 xmlChar *a = xmlGetProp (root, (xmlChar *) "_name");
2653 if (!a)
2655 strv_free (path_orig);
2656 rc = GPG_ERR_ENOMEM;
2657 goto fail;
2660 if (strv_printf (&path, "%s", (char *) a) == 0)
2662 xmlFree (a);
2663 strv_free (path_orig);
2664 rc = GPG_ERR_ENOMEM;
2665 goto fail;
2668 xmlFree (a);
2669 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
2671 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2673 strv_free (path_orig);
2674 goto fail;
2677 if (!rc)
2680 find_elements (client->doc, n->children, path + 1, &rc, NULL,
2681 NULL, NULL, 0, 0, NULL, 1);
2683 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2685 strv_free (path_orig);
2686 goto fail;
2688 else if (!rc)
2690 xmlNodePtr parent = n->parent;
2692 xmlUnlinkNode (n);
2693 xmlFreeNode (n);
2694 n = parent;
2698 strv_free (path);
2699 path = path_orig;
2701 if (rc == GPG_ERR_ELEMENT_NOT_FOUND)
2703 n = create_element_path (client, &path, &rc, NULL);
2705 if (rc)
2706 goto fail;
2709 copy = xmlCopyNodeList (root);
2710 n = xmlAddChildList (n, copy);
2711 if (!n)
2712 rc = GPG_ERR_BAD_DATA;
2714 else
2716 /* Check if the content root element can create a DTD root element. */
2717 if (!xmlStrEqual ((xmlChar *) "element", root->name))
2719 rc = GPG_ERR_SYNTAX;
2720 goto fail;
2723 xmlChar *a;
2725 if ((a = xmlGetProp (root, (xmlChar *) "_name")) == NULL)
2727 rc = GPG_ERR_SYNTAX;
2728 goto fail;
2731 char *tmp = str_dup ((char *) a);
2732 xmlFree (a);
2733 int literal = is_literal_element (&tmp);
2735 if (!valid_xml_element ((xmlChar *) tmp) || literal)
2737 xfree (tmp);
2738 rc = GPG_ERR_INV_VALUE;
2739 goto fail;
2742 if (strv_printf (&path, "%s", tmp) == 0)
2744 xfree (tmp);
2745 rc = GPG_ERR_ENOMEM;
2746 goto fail;
2749 xfree (tmp);
2750 n = find_root_element (client->doc, &path, &rc, NULL, 0, 1);
2752 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2754 rc = GPG_ERR_BAD_DATA;
2755 goto fail;
2758 /* Overwriting the existing tree. */
2759 if (!rc)
2761 xmlUnlinkNode (n);
2762 xmlFreeNodeList (n);
2765 rc = 0;
2766 xmlSetProp (root, (xmlChar *) "_name", (xmlChar *) path[0]);
2767 n = xmlCopyNode (root, 1);
2768 n = xmlAddChildList (xmlDocGetRootElement (client->doc), n);
2771 if (n && !rc)
2772 rc = update_element_mtime (n->parent);
2774 fail:
2775 if (doc)
2776 xmlFreeDoc (doc);
2778 if (path)
2779 strv_free (path);
2781 strv_free (req);
2782 return rc;
2785 static gpg_error_t
2786 import_command (assuan_context_t ctx, char *line)
2788 gpg_error_t rc;
2789 struct client_s *client = assuan_get_pointer (ctx);
2790 unsigned char *result;
2791 size_t len;
2793 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2794 if (rc)
2795 return send_error (ctx, rc);
2797 rc = do_import (client, result);
2798 return send_error (ctx, rc);
2801 static gpg_error_t
2802 do_lock (struct client_s *client, int add)
2804 gpg_error_t rc = lock_file_mutex (client, add);
2806 if (!rc)
2807 client->flags |= FLAG_LOCK_CMD;
2809 return rc;
2812 static gpg_error_t
2813 lock_command (assuan_context_t ctx, char *line)
2815 struct client_s *client = assuan_get_pointer (ctx);
2816 gpg_error_t rc = do_lock (client, 0);
2818 return send_error (ctx, rc);
2821 static gpg_error_t
2822 unlock_command (assuan_context_t ctx, char *line)
2824 struct client_s *client = assuan_get_pointer (ctx);
2825 gpg_error_t rc;
2827 rc = unlock_file_mutex (client, 0);
2828 return send_error (ctx, rc);
2831 static gpg_error_t
2832 option_command (assuan_context_t ctx, const char *name, const char *value)
2834 struct client_s *client = assuan_get_pointer (ctx);
2835 struct agent_s *agent = client->crypto->agent;
2836 gpg_error_t rc = 0;
2838 log_write1 ("OPTION name='%s' value='%s'", name, value);
2840 if (strcasecmp (name, (char *) "log_level") == 0)
2842 long l = 0;
2844 if (value)
2846 l = strtol (value, NULL, 10);
2848 if (l < 0 || l > 2)
2849 return gpg_error (GPG_ERR_INV_VALUE);
2852 MUTEX_LOCK (&rcfile_mutex);
2853 config_set_int_param (&global_config, "global", "log_level", value);
2854 MUTEX_UNLOCK (&rcfile_mutex);
2855 goto done;
2857 else if (strcasecmp (name, (char *) "lock-timeout") == 0)
2859 long n = 0;
2860 char *p = NULL;
2862 if (value)
2864 n = strtol (value, &p, 10);
2865 if (p && *p)
2866 return gpg_error (GPG_ERR_INV_VALUE);
2869 client->lock_timeout = n;
2870 goto done;
2872 else if (strcasecmp (name, (char *) "NAME") == 0)
2874 char *tmp = pthread_getspecific (thread_name_key);
2876 if (tmp)
2877 xfree (tmp);
2879 if (!value || !*value)
2881 pthread_setspecific (thread_name_key, str_dup (""));
2882 goto done;
2885 pthread_setspecific (thread_name_key, str_dup (value));
2886 goto done;
2888 else if (strcasecmp (name, (char *) "lc-messages") == 0)
2890 rc = set_agent_option (client->crypto->agent, "lc-messages", value);
2891 if (!rc)
2893 xfree (agent->lc_messages);
2894 agent->lc_messages = str_dup (value);
2897 else if (strcasecmp (name, (char *) "lc-ctype") == 0)
2899 rc = set_agent_option (client->crypto->agent, "lc-ctype", value);
2900 if (!rc)
2902 xfree (agent->lc_ctype);
2903 agent->lc_ctype = str_dup (value);
2906 else if (strcasecmp (name, (char *) "ttyname") == 0)
2908 rc = set_agent_option (client->crypto->agent, "ttyname", value);
2909 if (!rc)
2911 xfree (agent->ttyname);
2912 agent->ttyname = str_dup (value);
2915 else if (strcasecmp (name, (char *) "ttytype") == 0)
2917 rc = set_agent_option (client->crypto->agent, "ttytype", value);
2918 if (!rc)
2920 xfree (agent->ttytype);
2921 agent->ttytype = str_dup (value);
2924 else if (strcasecmp (name, (char *) "display") == 0)
2926 rc = set_agent_option (client->crypto->agent, "display", value);
2927 if (!rc)
2929 xfree (agent->display);
2930 agent->display = str_dup (value);
2933 else if (strcasecmp (name, (char *) "desc") == 0)
2935 if (client->crypto->agent->desc)
2936 xfree (client->crypto->agent->desc);
2938 client->crypto->agent->desc = str_dup (value);
2939 if (!client->crypto->agent->desc)
2940 rc = GPG_ERR_ENOMEM;
2942 #if 0
2943 else if (strcasecmp (name, "pinentry_timeout") == 0)
2945 char *p = NULL;
2946 int n;
2948 if (!value)
2949 goto done;
2951 n = (int) strtol (value, &p, 10);
2953 if (*p || n < 0)
2954 return gpg_error (GPG_ERR_INV_VALUE);
2956 MUTEX_LOCK (&rcfile_mutex);
2957 config_set_int_param (global_config,
2958 client->filename ? client->filename : "global",
2959 "pinentry_timeout", value);
2960 MUTEX_UNLOCK (&rcfile_mutex);
2961 goto done;
2963 #endif
2964 else
2965 return gpg_error (GPG_ERR_UNKNOWN_OPTION);
2967 done:
2968 return rc;
2971 static gpg_error_t
2972 do_rename (assuan_context_t ctx, char *line)
2974 struct client_s *client = assuan_get_pointer (ctx);
2975 gpg_error_t rc;
2976 char **req, **src, *dst;
2977 xmlNodePtr n, ndst;
2979 req = str_split (line, " ", 0);
2981 if (!req || !req[0] || !req[1])
2983 strv_free (req);
2984 return GPG_ERR_SYNTAX;
2987 dst = req[1];
2988 is_literal_element (&dst);
2990 if (!valid_xml_element ((xmlChar *) dst))
2992 strv_free (req);
2993 return GPG_ERR_INV_VALUE;
2996 if (strchr (req[0], '\t'))
2997 src = str_split (req[0], "\t", 0);
2998 else
2999 src = str_split (req[0], " ", 0);
3001 if (!src || !*src)
3003 rc = GPG_ERR_SYNTAX;
3004 goto fail;
3007 n = find_root_element (client->doc, &src, &rc, NULL, 0, 0);
3008 if (src[1] && n)
3009 n = find_elements (client->doc, n->children, src + 1, &rc, NULL, NULL,
3010 NULL, 0, 0, NULL, 0);
3012 if (!n)
3013 goto fail;
3015 xmlChar *a = xmlGetProp (n, (xmlChar *) "_name");
3016 if (!a)
3018 rc = GPG_ERR_ENOMEM;
3019 goto fail;
3022 /* To prevent unwanted effects:
3024 * <root name="a"><b/></root>
3026 * RENAME a<TAB>b b
3028 if (xmlStrEqual (a, (xmlChar *) dst))
3030 xmlFree (a);
3031 rc = GPG_ERR_AMBIGUOUS_NAME;
3032 goto fail;
3035 xmlFree (a);
3036 char **tmp = NULL;
3037 if (src[1])
3039 char **p;
3041 for (p = src; *p; p++)
3043 if (!*(p + 1))
3044 break;
3045 strv_printf (&tmp, "%s", *p);
3049 strv_printf (&tmp, "!%s", dst);
3050 ndst = find_root_element (client->doc, &tmp, &rc, NULL, 0, 0);
3051 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3053 strv_free (tmp);
3054 goto fail;
3057 if (tmp[1] && ndst)
3058 ndst = find_elements (client->doc, ndst->children, tmp + 1, &rc, NULL,
3059 NULL, NULL, 0, 0, NULL, 0);
3061 strv_free (tmp);
3062 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3063 goto fail;
3065 rc = 0;
3067 /* Target may exist:
3069 * <root name="a"/>
3070 * <root name="b" target="a"/>
3072 * RENAME b a
3074 * Would need to do:
3075 * RENAME !b a
3077 if (ndst == n)
3079 rc = GPG_ERR_AMBIGUOUS_NAME;
3080 goto fail;
3083 if (ndst)
3085 unlink_node (ndst);
3086 xmlFreeNodeList (ndst);
3089 rc = add_attribute (n, "_name", dst);
3091 fail:
3092 strv_free (req);
3093 strv_free (src);
3094 return rc;
3097 static gpg_error_t
3098 rename_command (assuan_context_t ctx, char *line)
3100 struct client_s *client = assuan_get_pointer (ctx);
3101 gpg_error_t rc;
3102 struct argv_s *args[] = {
3103 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3104 NULL
3107 rc = parse_options (&line, args, client);
3108 if (rc)
3109 return send_error (ctx, rc);
3111 if (client->opts & OPT_INQUIRE)
3113 unsigned char *result;
3114 size_t len;
3116 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3117 if (rc)
3118 return send_error (ctx, rc);
3120 line = (char *) result;
3123 rc = do_rename (ctx, line);
3125 if (client->opts & OPT_INQUIRE)
3126 xfree (line);
3128 return send_error (ctx, rc);
3131 static gpg_error_t
3132 do_copy (assuan_context_t ctx, char *line)
3134 struct client_s *client = assuan_get_pointer (ctx);
3135 gpg_error_t rc;
3136 char **req, **src = NULL, **dst = NULL;
3137 xmlNodePtr nsrc, ndst, new = NULL;
3139 req = str_split (line, " ", 0);
3140 if (!req || !req[0] || !req[1])
3142 strv_free (req);
3143 return GPG_ERR_SYNTAX;
3146 if (strchr (req[0], '\t'))
3147 src = str_split (req[0], "\t", 0);
3148 else
3149 src = str_split (req[0], " ", 0);
3151 if (!src || !*src)
3153 rc = GPG_ERR_SYNTAX;
3154 goto fail;
3157 if (strchr (req[1], '\t'))
3158 dst = str_split (req[1], "\t", 0);
3159 else
3160 dst = str_split (req[1], " ", 0);
3162 if (!dst || !*dst)
3164 rc = GPG_ERR_SYNTAX;
3165 goto fail;
3168 if (!valid_element_path (dst, 0))
3170 rc = GPG_ERR_INV_VALUE;
3171 goto fail;
3174 nsrc = find_root_element (client->doc, &src, &rc, NULL, 0, 0);
3175 if (nsrc && src[1])
3176 nsrc = find_elements (client->doc, nsrc->children, src + 1, &rc, NULL,
3177 NULL, NULL, 0, 0, NULL, 0);
3179 if (!nsrc)
3180 goto fail;
3182 new = xmlCopyNodeList (nsrc);
3183 if (!new)
3185 rc = GPG_ERR_ENOMEM;
3186 goto fail;
3189 int create = 0;
3190 ndst = find_root_element (client->doc, &dst, &rc, NULL, 0, 0);
3191 if (ndst && dst[1])
3193 if (ndst->children)
3194 ndst = find_elements (client->doc, ndst->children, dst + 1, &rc, NULL,
3195 NULL, create_target_elements_cb, 0, 0, NULL, 0);
3196 else
3197 create = 1;
3199 else
3200 create = 1;
3202 if (!ndst && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3203 goto fail;
3204 else if (create)
3206 ndst = create_element_path (client, &dst, &rc, NULL);
3207 if (!ndst)
3208 goto fail;
3211 /* Merge any attributes from the src node to the initial dst node. */
3212 for (xmlAttrPtr attr = new->properties; attr; attr = attr->next)
3214 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3215 continue;
3217 xmlAttrPtr a = xmlHasProp (ndst, attr->name);
3218 if (a)
3219 xmlRemoveProp (a);
3221 xmlChar *tmp = xmlNodeGetContent (attr->children);
3222 xmlNewProp (ndst, attr->name, tmp);
3223 xmlFree (tmp);
3224 rc = add_attribute (ndst, NULL, NULL);
3227 xmlNodePtr n = ndst->children;
3228 xmlUnlinkNode (n);
3229 xmlFreeNodeList (n);
3230 ndst->children = NULL;
3232 if (new->children)
3234 n = xmlCopyNodeList (new->children);
3235 if (!n)
3237 rc = GPG_ERR_ENOMEM;
3238 goto fail;
3241 n = xmlAddChildList (ndst, n);
3242 if (!n)
3244 rc = GPG_ERR_ENOMEM;
3245 goto fail;
3248 rc = update_element_mtime (xmlDocGetRootElement (client->doc) ==
3249 ndst->parent ? ndst : ndst->parent);
3252 fail:
3253 if (new)
3255 xmlUnlinkNode (new);
3256 xmlFreeNodeList (new);
3259 if (req)
3260 strv_free (req);
3262 if (src)
3263 strv_free (src);
3265 if (dst)
3266 strv_free (dst);
3268 return rc;
3271 static gpg_error_t
3272 copy_command (assuan_context_t ctx, char *line)
3274 struct client_s *client = assuan_get_pointer (ctx);
3275 gpg_error_t rc;
3276 struct argv_s *args[] = {
3277 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3278 NULL
3281 rc = parse_options (&line, args, client);
3282 if (rc)
3283 return send_error (ctx, rc);
3285 if (client->opts & OPT_INQUIRE)
3287 unsigned char *result;
3288 size_t len;
3290 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3291 if (rc)
3292 return send_error (ctx, rc);
3294 line = (char *) result;
3297 rc = do_copy (ctx, line);
3299 if (client->opts & OPT_INQUIRE)
3300 xfree (line);
3302 return send_error (ctx, rc);
3305 static gpg_error_t
3306 do_move (assuan_context_t ctx, char *line)
3308 struct client_s *client = assuan_get_pointer (ctx);
3309 gpg_error_t rc;
3310 char **req, **src = NULL, **dst = NULL;
3311 xmlNodePtr nsrc, ndst = NULL;
3313 req = str_split (line, " ", 0);
3315 if (!req || !req[0] || !req[1])
3317 strv_free (req);
3318 return GPG_ERR_SYNTAX;
3321 if (strchr (req[0], '\t'))
3322 src = str_split (req[0], "\t", 0);
3323 else
3324 src = str_split (req[0], " ", 0);
3326 if (!src || !*src)
3328 rc = GPG_ERR_SYNTAX;
3329 goto fail;
3332 if (strchr (req[1], '\t'))
3333 dst = str_split (req[1], "\t", 0);
3334 else
3335 dst = str_split (req[1], " ", 0);
3337 nsrc = find_root_element (client->doc, &src, &rc, NULL, 0, 0);
3338 if (nsrc && src[1])
3339 nsrc = find_elements (client->doc, nsrc->children, src + 1, &rc, NULL,
3340 NULL, NULL, 0, 0, NULL, 0);
3342 if (!nsrc)
3343 goto fail;
3345 if (dst)
3347 if (!valid_element_path (dst, 0))
3349 rc = GPG_ERR_INV_VALUE;
3350 goto fail;
3353 ndst = find_root_element (client->doc, &dst, &rc, NULL, 0, 0);
3354 if (ndst && dst[1])
3355 ndst = find_elements (client->doc, ndst->children, dst + 1, &rc, NULL,
3356 NULL, NULL, 0, 0, NULL, 0);
3358 else
3359 ndst = xmlDocGetRootElement (client->doc);
3361 for (xmlNodePtr n = ndst; n; n = n->parent)
3363 if (n == nsrc)
3365 rc = GPG_ERR_CONFLICT;
3366 goto fail;
3370 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3371 goto fail;
3373 rc = 0;
3375 if (ndst)
3377 xmlChar *a = node_has_attribute (nsrc, (xmlChar *) "_name");
3378 xmlNodePtr dup = find_element (ndst->children, (char *) a, NULL);
3380 xmlFree (a);
3381 if (dup)
3383 if (dup == nsrc)
3384 goto fail;
3386 if (ndst == xmlDocGetRootElement (client->doc))
3388 xmlNodePtr n = nsrc;
3389 int match = 0;
3391 while (n->parent && n->parent != ndst)
3392 n = n->parent;
3394 xmlChar *a = node_has_attribute (n, (xmlChar *) "_name");
3395 xmlChar *b = node_has_attribute (nsrc, (xmlChar *) "_name");
3397 if (xmlStrEqual (a, b))
3399 match = 1;
3400 xmlUnlinkNode (nsrc);
3401 xmlUnlinkNode (n);
3402 xmlFreeNodeList (n);
3405 xmlFree (a);
3406 xmlFree (b);
3408 if (!match)
3410 xmlUnlinkNode (dup);
3411 xmlFreeNodeList (dup);
3414 else
3415 xmlUnlinkNode (dup);
3419 if (!ndst && dst)
3421 xmlChar *name = node_has_attribute (nsrc, (xmlChar *) "_name");
3423 if (nsrc->parent == xmlDocGetRootElement (client->doc)
3424 && !strcmp ((char *) name, *dst))
3426 xmlFree (name);
3427 rc = GPG_ERR_CONFLICT;
3428 goto fail;
3431 xmlFree (name);
3432 ndst = create_element_path (client, &dst, &rc, nsrc);
3435 if (!ndst)
3436 goto fail;
3438 update_element_mtime (nsrc->parent);
3439 xmlUnlinkNode (nsrc);
3440 ndst = xmlAddChildList (ndst, nsrc);
3442 if (!ndst)
3443 rc = GPG_ERR_ENOMEM;
3445 update_element_mtime (ndst->parent);
3447 fail:
3448 if (req)
3449 strv_free (req);
3451 if (src)
3452 strv_free (src);
3454 if (dst)
3455 strv_free (dst);
3457 return rc;
3460 static gpg_error_t
3461 move_command (assuan_context_t ctx, char *line)
3463 struct client_s *client = assuan_get_pointer (ctx);
3464 gpg_error_t rc;
3465 struct argv_s *args[] = {
3466 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3467 NULL
3470 rc = parse_options (&line, args, client);
3471 if (rc)
3472 return send_error (ctx, rc);
3474 if (client->opts & OPT_INQUIRE)
3476 unsigned char *result;
3477 size_t len;
3479 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3480 if (rc)
3481 return send_error (ctx, rc);
3483 line = (char *) result;
3486 rc = do_move (ctx, line);
3488 if (client->opts & OPT_INQUIRE)
3489 xfree (line);
3491 return send_error (ctx, rc);
3494 static gpg_error_t
3495 ls_command (assuan_context_t ctx, char *line)
3497 gpg_error_t rc;
3498 char *tmp = str_asprintf ("%s/data", homedir);
3499 char *dir = expand_homedir (tmp);
3500 DIR *d = opendir (dir);
3502 rc = gpg_error_from_syserror ();
3503 xfree (tmp);
3505 if (!d)
3507 xfree (dir);
3508 return send_error (ctx, rc);
3511 size_t len =
3512 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3513 struct dirent *p = xmalloc (len), *cur = NULL;
3514 char *list = NULL;
3516 xfree (dir);
3517 rc = 0;
3519 while (!readdir_r (d, p, &cur) && cur)
3521 if (cur->d_name[0] == '.' && cur->d_name[1] == '\0')
3522 continue;
3523 else if (cur->d_name[0] == '.' && cur->d_name[1] == '.'
3524 && cur->d_name[2] == '\0')
3525 continue;
3527 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3529 if (!tmp)
3531 if (list)
3532 xfree (list);
3534 rc = GPG_ERR_ENOMEM;
3535 break;
3538 xfree (list);
3539 list = tmp;
3542 closedir (d);
3543 xfree (p);
3545 if (rc)
3546 return send_error (ctx, rc);
3548 if (!list)
3549 return send_error (ctx, GPG_ERR_NO_DATA);
3551 list[strlen (list) - 1] = 0;
3552 rc = xfer_data (ctx, list, strlen (list));
3553 xfree (list);
3554 return send_error (ctx, rc);
3557 static gpg_error_t
3558 bye_notify (assuan_context_t ctx, char *line)
3560 struct client_s *cl = assuan_get_pointer (ctx);
3562 #ifdef WITH_GNUTLS
3563 if (cl->thd->remote)
3565 int rc;
3569 struct timeval tv = { 0, 50000 };
3571 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3572 if (rc == GNUTLS_E_AGAIN)
3573 select (0, NULL, NULL, NULL, &tv);
3575 while (rc == GNUTLS_E_AGAIN);
3577 #endif
3579 /* This will let assuan_process_next() return. */
3580 fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK);
3581 cl->last_rc = 0; // BYE command result
3582 return 0;
3585 static gpg_error_t
3586 reset_notify (assuan_context_t ctx, char *line)
3588 struct client_s *client = assuan_get_pointer (ctx);
3590 if (client)
3591 cleanup_client (client);
3593 return 0;
3597 * This is called before every Assuan command.
3599 static gpg_error_t
3600 command_startup (assuan_context_t ctx, const char *name)
3602 struct client_s *client = assuan_get_pointer (ctx);
3603 gpg_error_t rc;
3604 struct command_table_s *cmd = NULL;
3606 log_write1 ("command='%s'", name);
3607 client->last_rc = client->opts = 0;
3609 for (int i = 0; command_table[i]; i++)
3611 if (!strcasecmp (name, command_table[i]->name))
3613 if (command_table[i]->ignore_startup)
3614 return 0;
3615 cmd = command_table[i];
3616 break;
3620 client->last_rc = rc = gpg_error (file_modified (client, cmd));
3621 return rc;
3625 * This is called after every Assuan command.
3627 static void
3628 command_finalize (assuan_context_t ctx, gpg_error_t rc)
3630 struct client_s *client = assuan_get_pointer (ctx);
3632 if (!(client->flags & FLAG_LOCK_CMD))
3633 unlock_file_mutex (client, 0);
3635 log_write1 (_("command completed: rc=%u"), client->last_rc);
3636 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
3639 static gpg_error_t
3640 help_command (assuan_context_t ctx, char *line)
3642 gpg_error_t rc;
3643 int i;
3645 if (!line || !*line)
3647 char *tmp;
3648 char *help = str_dup (_("Usage: HELP [<COMMAND>]\n"
3649 "For commands that take an element path as an argument, each element is "
3650 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3651 "\n" "COMMANDS:"));
3653 for (i = 0; command_table[i]; i++)
3655 if (!command_table[i]->help)
3656 continue;
3658 tmp = str_asprintf ("%s %s", help, command_table[i]->name);
3659 xfree (help);
3660 help = tmp;
3663 tmp = strip_texi_and_wrap (help);
3664 xfree (help);
3665 rc = xfer_data (ctx, tmp, strlen (tmp));
3666 xfree (tmp);
3667 return send_error (ctx, rc);
3670 for (i = 0; command_table[i]; i++)
3672 if (!strcasecmp (line, command_table[i]->name))
3674 char *help, *tmp;
3676 if (!command_table[i]->help)
3677 break;
3679 help = strip_texi_and_wrap (command_table[i]->help);
3680 tmp = str_asprintf (_("Usage: %s"), help);
3681 xfree (help);
3682 rc = xfer_data (ctx, tmp, strlen (tmp));
3683 xfree (tmp);
3684 return send_error (ctx, rc);
3688 return send_error (ctx, GPG_ERR_INV_NAME);
3691 static void
3692 new_command (const char *name, int ignore, int unlock,
3693 gpg_error_t (*handler) (assuan_context_t, char *),
3694 const char *help)
3696 int i = 0;
3698 if (command_table)
3699 for (i = 0; command_table[i]; i++);
3701 command_table =
3702 xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
3703 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
3704 command_table[i]->name = name;
3705 command_table[i]->handler = handler;
3706 command_table[i]->ignore_startup = ignore;
3707 command_table[i]->unlock = unlock;
3708 command_table[i++]->help = help;
3709 command_table[i] = NULL;
3712 void
3713 deinit_commands ()
3715 int i;
3717 for (i = 0; command_table[i]; i++)
3718 xfree (command_table[i]);
3720 xfree (command_table);
3723 static int
3724 sort_commands (const void *arg1, const void *arg2)
3726 struct command_table_s *const *a = arg1;
3727 struct command_table_s *const *b = arg2;
3729 if (!*a || !*b)
3730 return 0;
3731 else if (*a && !*b)
3732 return 1;
3733 else if (!*a && *b)
3734 return -1;
3736 return strcmp ((*a)->name, (*b)->name);
3739 static gpg_error_t
3740 passwd_command (assuan_context_t ctx, char *line)
3742 struct client_s *client = assuan_get_pointer (ctx);
3743 gpg_error_t rc;
3744 struct argv_s *args[] = {
3745 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
3746 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
3747 NULL
3750 if (client->flags & FLAG_NEW)
3751 return send_error (ctx, GPG_ERR_INV_STATE);
3753 client->crypto->save.s2k_count =
3754 config_get_ulong (client->filename, "s2k_count");
3755 rc = parse_options (&line, args, client);
3756 if (rc)
3757 return send_error (ctx, rc);
3759 if (!rc && client->opts & OPT_RESET)
3761 rc = cache_clear (client->md5file);
3762 if (!rc)
3763 send_status_all (STATUS_CACHE, NULL);
3766 if (!rc)
3768 if (client->crypto->save.s2k_count)
3769 rc = send_to_agent (client->crypto->agent, NULL, NULL,
3770 "OPTION s2k-count=%lu",
3771 client->crypto->save.s2k_count);
3773 if (!rc)
3774 rc = agent_passwd (client->crypto);
3777 return send_error (ctx, rc);
3780 static gpg_error_t
3781 parse_keygrip_opt_sign (void *data, void *value)
3783 struct client_s *client = data;
3785 (void) value;
3786 client->opts |= OPT_SIGN;
3787 return 0;
3790 static gpg_error_t
3791 keygrip_command (assuan_context_t ctx, char *line)
3793 struct client_s *client = assuan_get_pointer (ctx);
3794 gpg_error_t rc;
3795 struct crypto_s *crypto;
3796 struct argv_s *args[] = {
3797 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_keygrip_opt_sign},
3798 NULL
3801 if (!line || !*line)
3802 return send_error (ctx, GPG_ERR_SYNTAX);
3804 rc = parse_options (&line, args, client);
3805 if (rc)
3806 return send_error (ctx, rc);
3808 if (!valid_filename (line))
3809 return send_error (ctx, GPG_ERR_INV_VALUE);
3811 rc = init_client_crypto (&crypto);
3812 if (rc)
3813 return send_error (ctx, rc);
3815 rc = read_data_file (line, crypto);
3816 if (!rc)
3818 char *hexgrip = NULL;
3820 if (client->opts & OPT_SIGN)
3822 if (valid_keygrip (crypto->sign_grip, sizeof (crypto->sign_grip)))
3823 hexgrip = bin2hex (crypto->sign_grip, sizeof (crypto->sign_grip));
3826 if (!hexgrip)
3827 hexgrip = bin2hex (crypto->grip, sizeof (crypto->grip));
3829 if (!hexgrip)
3830 rc = GPG_ERR_ENOMEM;
3831 else
3832 rc = xfer_data (ctx, hexgrip, strlen (hexgrip));
3834 xfree (hexgrip);
3837 cleanup_crypto (&crypto);
3838 return send_error (ctx, rc);
3841 static gpg_error_t
3842 parse_opt_data (void *data, void *value)
3844 struct client_s *client = data;
3846 (void) value;
3847 client->opts |= OPT_DATA;
3848 return 0;
3851 static gpg_error_t
3852 getinfo_command (assuan_context_t ctx, char *line)
3854 struct client_s *client = assuan_get_pointer (ctx);
3855 gpg_error_t rc;
3856 char buf[ASSUAN_LINELENGTH];
3857 struct argv_s *args[] = {
3858 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
3859 NULL
3862 rc = parse_options (&line, args, client);
3863 if (rc)
3864 return send_error (ctx, rc);
3866 if (!strcasecmp (line, "clients"))
3868 if (client->opts & OPT_DATA)
3870 MUTEX_LOCK (&cn_mutex);
3871 snprintf (buf, sizeof (buf), "%i", slist_length (cn_thread_list));
3872 MUTEX_UNLOCK (&cn_mutex);
3873 rc = xfer_data (ctx, buf, strlen (buf));
3875 else
3876 rc = send_status (ctx, STATUS_CLIENTS, NULL);
3878 else if (!strcasecmp (line, "cache"))
3880 if (client->opts & OPT_DATA)
3882 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
3883 rc = xfer_data (ctx, buf, strlen (buf));
3885 else
3886 rc = send_status (ctx, STATUS_CACHE, NULL);
3888 else if (!strcasecmp (line, "pid"))
3890 char buf[32];
3891 pid_t pid = getpid ();
3893 snprintf (buf, sizeof (buf), "%i", pid);
3894 rc = xfer_data (ctx, buf, strlen (buf));
3896 else if (!strcasecmp (line, "version"))
3898 char *buf = str_asprintf ("0x%06x %s", VERSION_HEX,
3899 #ifdef WITH_LIBACL
3900 "ACL "
3901 #endif
3902 #ifdef WITH_GNUTLS
3903 "GNUTLS "
3904 #endif
3905 "");
3906 rc = xfer_data (ctx, buf, strlen (buf));
3907 xfree (buf);
3909 else if (!strcasecmp (line, "last_error"))
3911 if (client->last_error)
3912 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
3913 else
3914 rc = GPG_ERR_NO_DATA;
3916 else
3917 rc = gpg_error (GPG_ERR_SYNTAX);
3919 return send_error (ctx, rc);
3922 static gpg_error_t
3923 send_data_cb (void *user, const void *buf, size_t len)
3925 assuan_context_t ctx = user;
3927 return assuan_send_data (ctx, buf, len);
3930 static gpg_error_t
3931 send_status_cb (void *user, const char *line)
3933 assuan_context_t ctx = user;
3934 char keyword[200], *k;
3935 const char *p;
3937 for (p = line, k = keyword; *p; p++)
3939 if (isspace (*p))
3940 break;
3942 *k++ = *p;
3945 *k = 0;
3946 if (*p == '#')
3947 p++;
3949 while (isspace (*p))
3950 p++;
3952 return assuan_write_status (ctx, keyword, *p ? p : NULL);
3955 static gpg_error_t
3956 agent_command (assuan_context_t ctx, char *line)
3958 gpg_error_t rc;
3959 struct client_s *client = assuan_get_pointer (ctx);
3961 if (!line || !*line)
3962 return send_error (ctx, GPG_ERR_SYNTAX);
3964 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 1);
3965 rc = assuan_transact (client->crypto->agent->ctx, line, send_data_cb,
3966 client->ctx, agent_loopback_cb, client->crypto,
3967 send_status_cb, client->ctx);
3968 if (gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
3970 char *line;
3971 size_t len;
3973 rc = assuan_write_line (client->crypto->agent->ctx, "CAN");
3974 if (!rc)
3976 rc = assuan_read_line (client->crypto->agent->ctx, &line, &len);
3977 if (!rc)
3978 rc = gpg_error (GPG_ERR_ASS_CANCELED);
3982 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 0);
3983 return send_error (ctx, rc);
3986 void
3987 init_commands ()
3989 /* !BEGIN-HELP-TEXT!
3991 * This comment is used as a marker to generate the offline documentation
3992 * found in doc/pwmd.info. The indentation needs to be kept for the awk
3993 * script to determine where commands begin and end.
3995 new_command("HELP", 1, 1, help_command, _(
3996 "HELP [<COMMAND>]\n"
3997 "Show available commands or command specific help text."
4000 new_command("AGENT", 1, 1, agent_command, _(
4001 "AGENT <command>\n"
4002 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4003 "@command{gpg-agent}."
4006 new_command("GETINFO", 1, 1, getinfo_command, _(
4007 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4008 "Get server and other information: @var{cache} returns the number of cached "
4009 "documents via a status message. @var{clients} returns the number of "
4010 "connected clients via a status message. @var{pid} returns the process ID "
4011 "number of the server via a data response. @var{VERSION} returns the server "
4012 "version number and compile-time features with a data response with each "
4013 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4014 "the last failed command when available. @xref{Status Messages}. "
4015 "\n"
4016 "When the @option{--data} option is specified then the result will be send "
4017 "via a data response rather than a status message."
4020 new_command("PASSWD", 0, 0, passwd_command, _(
4021 "PASSWD [--reset] [--s2k-count=N]\n"
4022 "Changes the passphrase of the secret key required to open the current "
4023 "file. When the @option{--reset} option is passed then the cache entry for "
4024 "the current file will be reset and the passphrase, if any, will be required "
4025 "during the next @code{OPEN}. @xref{OPEN}."
4026 "\n"
4027 "The @option{--s2k-count} option sets number of hash iterations for a "
4028 "passphrase and must be either @code{0} to use the calibrated count of the "
4029 "machine (the default), or a value greater than or equal to @code{65536}. "
4030 "@xref{SAVE}."
4033 new_command("KEYGRIP", 1, 1, keygrip_command, _(
4034 "KEYGRIP [--sign] <filename>\n"
4035 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4036 "data response."
4037 "\n"
4038 "When the @option{--sign} option is specified then the key used for signing "
4039 "of the specified @var{filename} will be returned."
4042 new_command("OPEN", 1, 1, open_command, _(
4043 "OPEN [--lock] [--no-pinentry] <filename> [<passphrase>]\n"
4044 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4045 "found on the file-system then a new document will be created. If the file "
4046 "is found, it is looked for in the file cache. If cached and no "
4047 "@var{passphrase} was specified then the cached document is opened. When not "
4048 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4049 "for decryption unless @option{--no-pinentry} was specified (see below)."
4050 "\n"
4051 "When the @option{--lock} option is passed then the file mutex will be "
4052 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4053 "file has been opened."
4054 "\n"
4055 "By default, a pinentry is used to retrieve a passphrase when required. "
4056 "Passing @option{--no-pinentry} will disable pinentry use for the rest of "
4057 "the session. When pinentry use is disabled but required for some operation "
4058 "then a server @emph{INQUIRE} will be send to the client to retrieve the "
4059 "passphrase. See the @code{OPTION} command (@pxref{OPTION}), for pinentry "
4060 "specific options."
4063 new_command("SAVE", 0, 0, save_command, _(
4064 "SAVE [--no-passphrase] [--reset] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring [--sign-keygrip=hexstring]]\n"
4065 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4066 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4067 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4068 "keypair will be generated and a pinentry will be used to prompt for the "
4069 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4070 "passed, in which case the data file will not be passphrase protected."
4071 "\n"
4072 "The @option{--reset} option will clear the cache entry for the current file "
4073 "before saving."
4074 "\n"
4075 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4076 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4077 "(@pxref{Configuration}) for available ciphers."
4078 "\n"
4079 "The @option{--cipher-iterations} option specifies the number of times to "
4080 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4081 "\n"
4082 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4083 "the client to obtain the key paramaters to use when generating a new "
4084 "keypair. The inquired data is expected to be an S-expression. If not "
4085 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4086 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4087 "that when this option is specified a new keypair will be generated "
4088 "reguardless if the file is a new one or not."
4089 "\n"
4090 "You can encrypt the data file to a public key other than the one that it "
4091 "was originally encrypted with by passing the @option{--keygrip} option with "
4092 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4093 "be of any key that @command{gpg-agent} knows about. The "
4094 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4095 "secret key. This option may be needed when using a smartcard."
4096 "\n"
4097 "The @option{--s2k-count} option sets number of hash iterations for a "
4098 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4099 "value which is the default. This setting only affects new files. To change "
4100 "the setting, use the @code{PASSWD} command (@pxref{PASSWD})."
4103 new_command("ISCACHED", 1, 0, iscached_command, _(
4104 "ISCACHED [--lock] <filename>\n"
4105 "An @emph{OK} response is returned if the specified @var{filename} is found "
4106 "in the file cache. If not found in the cache but exists on the filesystem "
4107 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4108 "returned."
4109 "\n"
4110 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4111 "file exists; it does not need to be opened nor cached."
4114 new_command("CLEARCACHE", 1, 1, clearcache_command, _(
4115 "CLEARCACHE [<filename>]\n"
4116 "Clears a file cache entry for all or the specified @var{filename}."
4119 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command, _(
4120 "CACHETIMEOUT <filename> <seconds>\n"
4121 "The time in @var{seconds} until @var{filename} will be removed from the "
4122 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4123 "the passphrase for each @code{OPEN} command (@pxref{OPEN}). "
4124 "@xref{Configuration}, and the @code{cache_timeout} parameter."
4127 new_command("LIST", 0, 1, list_command, _(
4128 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4129 "If no element path is given then a newline separated list of root elements "
4130 "is returned with a data response. If given, then all reachable elements "
4131 "of the specified element path are returned unless the @option{--no-recurse} "
4132 "option is specified. If specified, only the child elements of the element "
4133 "path are returned without recursing into grandchildren. Each resulting "
4134 "element is prefixed with the literal @code{!} character when the element "
4135 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4136 "\n"
4137 "When the @option{--verbose} option is passed then each element path "
4138 "returned will have zero or more flags appened to it. These flags are "
4139 "delimited from the element path by a single space character. A flag itself "
4140 "is a single character. Flag @code{+} indicates that there are child nodes of "
4141 "the current element path. Flag @code{E} indicates that an element of an "
4142 "element path contained in a @var{target} attribute could not be found. Flag "
4143 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4144 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4145 "of a @var{target} attribute (see below)."
4146 "\n"
4147 "The @option{--with-target} option implies @option{--verbose} and will append "
4148 "an additional flag @code{T} followed by a single space then an element path. "
4149 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4150 "current element when it contains a @var{target} attribute. When no "
4151 "@var{target} attribute is found then no flag will be appended."
4152 "\n"
4153 "The @option{--no-recurse} option limits the amount of data returned to only "
4154 "the listing of children of the specified element path and not any "
4155 "grandchildren."
4156 "\n"
4157 "The @option{--all} option lists the entire element tree for each root "
4158 "element. This option also implies option @option{--verbose}."
4159 "\n"
4160 "When the @option{--inquire} option is passed then all remaining non-option "
4161 "arguments are retrieved via a server @emph{INQUIRE}."
4164 new_command("REALPATH", 0, 1, realpath_command, _(
4165 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4166 "Resolves all @code{target} attributes of the specified element path and "
4167 "returns the result with a data response. @xref{Target Attribute}, for details."
4168 "\n"
4169 "When the @option{--inquire} option is passed then all remaining non-option "
4170 "arguments are retrieved via a server @emph{INQUIRE}."
4173 new_command("STORE", 0, 1, store_command, _(
4174 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4175 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4176 "\n"
4177 "Creates a new element path or modifies the @var{content} of an existing "
4178 "element. If only a single element is specified then a new root element is "
4179 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4180 "set to the final @key{TAB} delimited element. If no @var{content} is "
4181 "specified after the final @key{TAB}, then the content of the element will "
4182 "be removed, or empty when creating a new element."
4183 "\n"
4184 "The only restriction of an element name is that it not contain whitespace "
4185 "or begin with the literal element character @code{!} unless specifying a "
4186 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4187 "the @key{TAB} delimited elements. It is recommended that the content of an "
4188 "element be base64 encoded when it contains control or @key{TAB} characters "
4189 "to prevent @abbr{XML} and @command{pwmd} parsing errors."
4192 new_command("RENAME", 0, 1, rename_command, _(
4193 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4194 "Renames the specified @var{element} to the new @var{value}. If an element of "
4195 "the same name as the @var{value} already exists it will be overwritten."
4196 "\n"
4197 "When the @option{--inquire} option is passed then all remaining non-option "
4198 "arguments are retrieved via a server @emph{INQUIRE}."
4201 new_command("COPY", 0, 1, copy_command, _(
4202 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4203 "Copies the entire element tree starting from the child node of the source "
4204 "element, to the destination element path. If the destination element path "
4205 "does not exist then it will be created; otherwise it is overwritten."
4206 "\n"
4207 "Note that attributes from the source element are merged into the "
4208 "destination element when the destination element path exists. When an "
4209 "attribute of the same name exists in both the source and destination "
4210 "elements then the destination attribute will be updated to the source "
4211 "attribute value."
4212 "\n"
4213 "When the @option{--inquire} option is passed then all remaining non-option "
4214 "arguments are retrieved via a server @emph{INQUIRE}."
4217 new_command("MOVE", 0, 1, move_command, _(
4218 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4219 "Moves the source element path to the destination element path. If the "
4220 "destination is not specified then it will be moved to the root node of the "
4221 "document. If the destination is specified and exists then it will be "
4222 "overwritten; otherwise it will be created."
4223 "\n"
4224 "When the @option{--inquire} option is passed then all remaining non-option "
4225 "arguments are retrieved via a server @emph{INQUIRE}."
4228 new_command("DELETE", 0, 1, delete_command, _(
4229 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4230 "Removes the specified element path and all of its children. This may break "
4231 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4232 "refers to this element or any of its children."
4233 "\n"
4234 "When the @option{--inquire} option is passed then all remaining non-option "
4235 "arguments are retrieved via a server @emph{INQUIRE}."
4238 new_command("GET", 0, 1, get_command, _(
4239 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4240 "Retrieves the content of the specified element. The content is returned "
4241 "with a data response."
4242 "\n"
4243 "When the @option{--inquire} option is passed then all remaining non-option "
4244 "arguments are retrieved via a server @emph{INQUIRE}."
4247 new_command("ATTR", 0, 1, attr_command, _(
4248 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4249 "@table @asis\n"
4250 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4251 "\n"
4252 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4253 "element. When no @var{value} is specified any existing value will be removed."
4254 "\n"
4255 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4256 "\n"
4257 " Removes an @var{attribute} from an element."
4258 "\n"
4259 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4260 "\n"
4261 " Retrieves a newline separated list of attributes names and values "
4262 "from the specified element. Each attribute name and value is space delimited."
4263 "\n"
4264 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4265 "\n"
4266 " Retrieves the value of an @var{attribute} from an element."
4267 "@end table\n"
4268 "\n"
4269 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4270 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4271 "commands instead."
4272 "\n"
4273 "The @code{_mtime} attribute is updated each time an element is modified by "
4274 "either storing content, editing attributes or by deleting a child element. "
4275 "The @code{_ctime} attribute is created for each new element in an element "
4276 "path."
4277 "\n"
4278 "When the @option{--inquire} option is passed then all remaining non-option "
4279 "arguments are retrieved via a server @emph{INQUIRE}."
4280 "\n"
4281 "@xref{Target Attribute}, for details about this special attribute."
4284 new_command("XPATH", 0, 1, xpath_command, _(
4285 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4286 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4287 "specified, it is assumed the expression is a request to return a result. "
4288 "Otherwise, the result is set to the @var{value} argument and the document is "
4289 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4290 "is assumed to be empty and the document is updated. For example:"
4291 "@sp 1\n"
4292 "@example\n"
4293 "XPATH //element[@@_name='password']@key{TAB}\n"
4294 "@end example\n"
4295 "@sp 1"
4296 "would clear the content of all @code{password} elements in the data file "
4297 "while leaving off the trailing @key{TAB} would return all @code{password} "
4298 "elements in @abbr{XML} format."
4299 "\n"
4300 "When the @option{--inquire} option is passed then all remaining non-option "
4301 "arguments are retrieved via a server @emph{INQUIRE}."
4302 "\n"
4303 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4304 "expression syntax."
4307 new_command("XPATHATTR", 0, 1, xpathattr_command, _(
4308 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4309 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4310 "attributes and does not return a result. For the @var{SET} operation the "
4311 "@var{value} is optional but the field is required. If not specified then "
4312 "the attribute value will be empty. For example:"
4313 "@sp 1"
4314 "@example\n"
4315 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4316 "@end example\n"
4317 "@sp 1"
4318 "would create an @code{password} attribute for each @code{password} element "
4319 "found in the document. The attribute value will be empty but still exist."
4320 "\n"
4321 "When the @option{--inquire} option is passed then all remaining non-option "
4322 "arguments are retrieved via a server @emph{INQUIRE}."
4323 "\n"
4324 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4325 "expression syntax."
4328 new_command("IMPORT", 0, 1, import_command, _(
4329 "IMPORT <content>[<TAB>[!]element[<TAB>[!]child[..]]]\n"
4330 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4331 "\n"
4332 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4333 "argument is raw @abbr{XML} data. The content is created as a child of the "
4334 "specified element path and will overwrite an existing element of the same "
4335 "name. If an element of the element path does not exist then it will be "
4336 "created."
4337 "\n"
4338 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4339 "for details."
4342 new_command("DUMP", 0, 1, dump_command, _(
4343 "DUMP\n"
4344 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4345 "dumping a specific node."
4348 new_command("LOCK", 0, 0, lock_command, _(
4349 "LOCK\n"
4350 "Locks the mutex associated with the opened file. This prevents other clients "
4351 "from sending commands to the same opened file until the client "
4352 "that sent this command either disconnects or sends the @code{UNLOCK} "
4353 "command. @xref{UNLOCK}."
4356 new_command("UNLOCK", 1, 0, unlock_command, _(
4357 "UNLOCK\n"
4358 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4359 "a commands' @option{lock} option. @xref{LOCK}."
4362 new_command("GETCONFIG", 1, 1, getconfig_command, _(
4363 "GETCONFIG [filename] <parameter>\n"
4364 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4365 "data response. If no file has been opened then the value for @var{filename} "
4366 "or the default from the @samp{global} section will be returned. If a file "
4367 "has been opened and no @var{filename} is specified, a value previously "
4368 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4371 new_command("OPTION", 1, 1, NULL, _(
4372 "OPTION <NAME>=<VALUE>\n"
4373 "Sets a client option @var{name} to @var{value}. The value for an option is "
4374 "kept for the duration of the connection."
4375 "\n"
4376 "@table @asis\n"
4377 "@item TTYNAME\n"
4378 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4379 "\n"
4380 "@item TTYTYPE\n"
4381 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4382 "\n"
4383 "@item DISPLAY\n"
4384 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4385 "\n"
4386 "@item DESC\n"
4387 " Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4388 "\n"
4389 "@item LC-CTYPE\n"
4390 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4391 "\n"
4392 "@item LC-MESSAGES\n"
4393 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4394 "\n"
4395 "@item NAME\n"
4396 " Associates the thread ID of the connection with the specified textual "
4397 "representation. Useful for debugging log messages."
4398 "\n"
4399 "@item LOCK-TIMEOUT\n"
4400 " When not @code{0}, the duration in tenths of a second to wait for the file "
4401 "mutex which has been locked by another thread to be released before returning "
4402 "an error. When @code{-1}, then an error will be returned immediately."
4403 "@end table\n"
4406 new_command("LS", 1, 1, ls_command, _(
4407 "LS\n"
4408 "Lists the available data files stored in the data directory "
4409 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4412 new_command("RESET", 1, 1, NULL, _(
4413 "RESET\n"
4414 "Closes the currently opened file but keeps any previously set client options."
4417 new_command("NOP", 1, 1, NULL, _(
4418 "NOP\n"
4419 "Does nothing. Always returns successfully."
4422 /* !END-HELP-TEXT! */
4423 new_command ("CANCEL", 1, 1, NULL, NULL);
4424 new_command ("END", 1, 1, NULL, NULL);
4425 new_command ("BYE", 1, 1, NULL, NULL);
4427 int i;
4428 for (i = 0; command_table[i]; i++);
4429 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4430 sort_commands);
4433 gpg_error_t
4434 register_commands (assuan_context_t ctx)
4436 int i = 0, rc;
4438 for (; command_table[i]; i++)
4440 if (!command_table[i]->handler)
4441 continue;
4443 rc = assuan_register_command (ctx, command_table[i]->name,
4444 command_table[i]->handler,
4445 command_table[i]->help);
4446 if (rc)
4447 return rc;
4450 rc = assuan_register_option_handler (ctx, option_command);
4451 if (rc)
4452 return rc;
4454 rc = assuan_register_bye_notify (ctx, bye_notify);
4455 if (rc)
4456 return rc;
4458 rc = assuan_register_reset_notify (ctx, reset_notify);
4459 if (rc)
4460 return rc;
4462 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4463 if (rc)
4464 return rc;
4466 return assuan_register_post_cmd_notify (ctx, command_finalize);