Reindent to GNU coding style.
[pwmd.git] / src / commands.c
blob5397e983254bd8a939850cc27c5d212d34695baa
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)
2216 MUTEX_LOCK (&rcfile_mutex);
2217 config_set_int_param (&global_config, req[0], "cache_timeout", req[1]);
2218 MUTEX_UNLOCK (&rcfile_mutex);
2221 strv_free (req);
2222 return send_error (ctx, rc);
2225 static gpg_error_t
2226 dump_command (assuan_context_t ctx, char *line)
2228 xmlChar *xml;
2229 int len;
2230 struct client_s *client = assuan_get_pointer (ctx);
2231 gpg_error_t rc;
2233 if (disable_list_and_dump == 1)
2234 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2236 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2238 if (!xml)
2240 log_write ("%s(%i): %s", __FILE__, __LINE__,
2241 pwmd_strerror (GPG_ERR_ENOMEM));
2242 return send_error (ctx, GPG_ERR_ENOMEM);
2245 pthread_cleanup_push (xmlFree, xml);
2246 rc = xfer_data (ctx, (char *) xml, len);
2247 pthread_cleanup_pop (1);
2248 return send_error (ctx, rc);
2251 static gpg_error_t
2252 getconfig_command (assuan_context_t ctx, char *line)
2254 struct client_s *client = assuan_get_pointer (ctx);
2255 gpg_error_t rc = 0;
2256 char filename[255] = { 0 }, param[747] =
2259 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2261 if (!line || !*line)
2262 return send_error (ctx, GPG_ERR_SYNTAX);
2264 if (strchr (line, ' '))
2266 sscanf (line, " %254[^ ] %746c", filename, param);
2267 paramp = param;
2268 fp = filename;
2271 if (fp && !valid_filename (fp))
2272 return send_error (ctx, GPG_ERR_INV_VALUE);
2274 paramp = str_down (paramp);
2275 if (!strcmp (paramp, "cipher") && fp)
2277 struct crypto_s *crypto;
2279 rc = init_client_crypto (&crypto);
2280 if (!rc)
2282 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2283 if (!rc)
2285 const char *t =
2286 gcry_cipher_algo_name (cipher_to_gcrypt (crypto->hdr.flags));
2287 if (t)
2289 tmp = str_dup (t);
2290 if (tmp)
2291 str_down (tmp);
2296 cleanup_crypto (&crypto);
2297 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2298 return send_error (ctx, rc);
2300 if (!rc && tmp)
2301 goto done;
2303 else if (!strcmp (paramp, "cipher_iterations") && fp)
2305 struct crypto_s *crypto;
2307 rc = init_client_crypto (&crypto);
2308 if (!rc)
2310 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2311 if (!rc)
2313 tmp = str_asprintf ("%llu",
2314 (unsigned long long) crypto->hdr.
2315 iterations);
2316 if (!tmp)
2317 rc = GPG_ERR_ENOMEM;
2321 cleanup_crypto (&crypto);
2322 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2323 return send_error (ctx, rc);
2325 if (!rc && tmp)
2326 goto done;
2328 else if (!strcmp (paramp, "passphrase"))
2329 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2331 p = config_get_value (fp ? fp : "global", paramp);
2332 if (!p)
2333 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2335 tmp = expand_homedir (p);
2336 xfree (p);
2337 if (!tmp)
2339 log_write ("%s(%i): %s", __FILE__, __LINE__,
2340 pwmd_strerror (GPG_ERR_ENOMEM));
2341 return send_error (ctx, GPG_ERR_ENOMEM);
2344 done:
2345 p = tmp;
2346 pthread_cleanup_push (xfree, p);
2347 rc = xfer_data (ctx, p, strlen (p));
2348 pthread_cleanup_pop (1);
2349 return send_error (ctx, rc);
2352 struct xpath_s
2354 xmlXPathContextPtr xp;
2355 xmlXPathObjectPtr result;
2356 xmlBufferPtr buf;
2357 char **req;
2360 static void
2361 xpath_command_cleanup (void *arg)
2363 struct xpath_s *xpath = arg;
2365 if (!xpath)
2366 return;
2368 req_cleanup (xpath->req);
2370 if (xpath->buf)
2371 xmlBufferFree (xpath->buf);
2373 if (xpath->result)
2374 xmlXPathFreeObject (xpath->result);
2376 if (xpath->xp)
2377 xmlXPathFreeContext (xpath->xp);
2380 static gpg_error_t
2381 do_xpath (assuan_context_t ctx, char *line)
2383 gpg_error_t rc;
2384 struct client_s *client = assuan_get_pointer (ctx);
2385 struct xpath_s _x = { 0 };
2386 struct xpath_s *xpath = &_x;
2388 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2390 if (strv_printf (&xpath->req, "%s", line) == 0)
2391 return GPG_ERR_ENOMEM;
2394 xpath->xp = xmlXPathNewContext (client->doc);
2395 if (!xpath->xp)
2397 rc = GPG_ERR_BAD_DATA;
2398 goto fail;
2401 xpath->result =
2402 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2403 if (!xpath->result)
2405 rc = GPG_ERR_BAD_DATA;
2406 goto fail;
2409 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2411 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2412 goto fail;
2415 rc = recurse_xpath_nodeset (client->doc, xpath->result->nodesetval,
2416 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2417 NULL);
2418 if (rc)
2419 goto fail;
2420 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2422 rc = GPG_ERR_NO_DATA;
2423 goto fail;
2425 else if (xpath->req[1])
2427 rc = 0;
2428 goto fail;
2431 pthread_cleanup_push (xpath_command_cleanup, &xpath);
2432 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2433 xmlBufferLength (xpath->buf));
2434 pthread_cleanup_pop (0);
2435 fail:
2436 xpath_command_cleanup (xpath);
2437 return rc;
2440 static gpg_error_t
2441 xpath_command (assuan_context_t ctx, char *line)
2443 struct client_s *client = assuan_get_pointer (ctx);
2444 gpg_error_t rc;
2445 struct argv_s *args[] = {
2446 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2447 NULL
2450 if (disable_list_and_dump == 1)
2451 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2453 rc = parse_options (&line, args, client);
2454 if (rc)
2455 return send_error (ctx, rc);
2457 if (client->opts & OPT_INQUIRE)
2459 unsigned char *result;
2460 size_t len;
2462 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2463 if (rc)
2464 return send_error (ctx, rc);
2466 line = (char *) result;
2469 if (!line || !*line)
2470 rc = GPG_ERR_SYNTAX;
2472 if (!rc)
2473 rc = do_xpath (ctx, line);
2475 if (client->opts & OPT_INQUIRE)
2476 xfree (line);
2478 return send_error (ctx, rc);
2481 static gpg_error_t
2482 do_xpathattr (assuan_context_t ctx, char *line)
2484 struct client_s *client = assuan_get_pointer (ctx);
2485 gpg_error_t rc;
2486 char **req = NULL;
2487 int cmd = 0; //SET
2488 struct xpath_s _x = { 0 };
2489 struct xpath_s *xpath = &_x;
2491 if ((req = str_split (line, " ", 3)) == NULL)
2492 return GPG_ERR_ENOMEM;
2494 if (!req[0])
2496 rc = GPG_ERR_SYNTAX;
2497 goto fail;
2500 if (!strcasecmp (req[0], "SET"))
2501 cmd = 0;
2502 else if (!strcasecmp (req[0], "DELETE"))
2503 cmd = 1;
2504 else
2506 rc = GPG_ERR_SYNTAX;
2507 goto fail;
2510 if (!req[1] || !req[2])
2512 rc = GPG_ERR_SYNTAX;
2513 goto fail;
2516 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2518 rc = GPG_ERR_ENOMEM;
2519 goto fail;
2522 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2524 rc = GPG_ERR_SYNTAX;
2525 goto fail;
2528 xpath->xp = xmlXPathNewContext (client->doc);
2529 if (!xpath->xp)
2531 rc = GPG_ERR_BAD_DATA;
2532 goto fail;
2535 xpath->result =
2536 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2537 if (!xpath->result)
2539 rc = GPG_ERR_BAD_DATA;
2540 goto fail;
2543 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2545 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2546 goto fail;
2549 rc = recurse_xpath_nodeset (client->doc, xpath->result->nodesetval,
2550 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2551 (xmlChar *) req[1]);
2553 fail:
2554 xpath_command_cleanup (xpath);
2555 strv_free (req);
2556 return rc;
2559 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2560 static gpg_error_t
2561 xpathattr_command (assuan_context_t ctx, char *line)
2563 struct client_s *client = assuan_get_pointer (ctx);
2564 gpg_error_t rc;
2565 struct argv_s *args[] = {
2566 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2567 NULL
2570 if (disable_list_and_dump == 1)
2571 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2573 rc = parse_options (&line, args, client);
2574 if (rc)
2575 return send_error (ctx, rc);
2577 if (client->opts & OPT_INQUIRE)
2579 unsigned char *result;
2580 size_t len;
2582 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2583 if (rc)
2584 return send_error (ctx, rc);
2586 line = (char *) result;
2589 if (!line || !*line)
2590 rc = GPG_ERR_SYNTAX;
2592 if (!rc)
2593 rc = do_xpathattr (ctx, line);
2595 if (client->opts & OPT_INQUIRE)
2596 xfree (line);
2598 return send_error (ctx, rc);
2601 static gpg_error_t
2602 do_import (struct client_s *client, unsigned char *line)
2604 char **req, **path = NULL, **path_orig = NULL, *content;
2605 xmlDocPtr doc = NULL;
2606 xmlNodePtr n, root, copy;
2607 gpg_error_t rc;
2609 req = str_split ((char *) line, "\t", 2);
2610 xfree (line);
2611 if (!req || !*req)
2612 return GPG_ERR_SYNTAX;
2614 content = req[0];
2615 path = str_split (req[1], "\t", 0);
2616 if (!content || !*content)
2618 rc = GPG_ERR_SYNTAX;
2619 goto fail;
2622 if (path && !valid_element_path (path, 0))
2624 rc = GPG_ERR_INV_VALUE;
2625 goto fail;
2628 doc = xmlReadDoc ((xmlChar *) content, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2629 if (!doc)
2631 rc = GPG_ERR_BAD_DATA;
2632 goto fail;
2635 root = xmlDocGetRootElement (doc);
2636 rc = validate_import (root);
2637 if (rc)
2638 goto fail;
2640 if (path)
2642 path_orig = strv_dup (path);
2643 if (!path_orig)
2645 log_write ("%s(%i): %s", __FILE__, __LINE__,
2646 pwmd_strerror (GPG_ERR_ENOMEM));
2647 rc = GPG_ERR_ENOMEM;
2648 goto fail;
2651 xmlChar *a = xmlGetProp (root, (xmlChar *) "_name");
2652 if (!a)
2654 strv_free (path_orig);
2655 rc = GPG_ERR_ENOMEM;
2656 goto fail;
2659 if (strv_printf (&path, "%s", (char *) a) == 0)
2661 xmlFree (a);
2662 strv_free (path_orig);
2663 rc = GPG_ERR_ENOMEM;
2664 goto fail;
2667 xmlFree (a);
2668 n = find_root_element (client->doc, &path, &rc, NULL, 0, 0);
2670 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2672 strv_free (path_orig);
2673 goto fail;
2676 if (!rc)
2679 find_elements (client->doc, n->children, path + 1, &rc, NULL,
2680 NULL, NULL, 0, 0, NULL, 1);
2682 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2684 strv_free (path_orig);
2685 goto fail;
2687 else if (!rc)
2689 xmlNodePtr parent = n->parent;
2691 xmlUnlinkNode (n);
2692 xmlFreeNode (n);
2693 n = parent;
2697 strv_free (path);
2698 path = path_orig;
2700 if (rc == GPG_ERR_ELEMENT_NOT_FOUND)
2702 n = create_element_path (client, &path, &rc, NULL);
2704 if (rc)
2705 goto fail;
2708 copy = xmlCopyNodeList (root);
2709 n = xmlAddChildList (n, copy);
2710 if (!n)
2711 rc = GPG_ERR_BAD_DATA;
2713 else
2715 /* Check if the content root element can create a DTD root element. */
2716 if (!xmlStrEqual ((xmlChar *) "element", root->name))
2718 rc = GPG_ERR_SYNTAX;
2719 goto fail;
2722 xmlChar *a;
2724 if ((a = xmlGetProp (root, (xmlChar *) "_name")) == NULL)
2726 rc = GPG_ERR_SYNTAX;
2727 goto fail;
2730 char *tmp = str_dup ((char *) a);
2731 xmlFree (a);
2732 int literal = is_literal_element (&tmp);
2734 if (!valid_xml_element ((xmlChar *) tmp) || literal)
2736 xfree (tmp);
2737 rc = GPG_ERR_INV_VALUE;
2738 goto fail;
2741 if (strv_printf (&path, "%s", tmp) == 0)
2743 xfree (tmp);
2744 rc = GPG_ERR_ENOMEM;
2745 goto fail;
2748 xfree (tmp);
2749 n = find_root_element (client->doc, &path, &rc, NULL, 0, 1);
2751 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2753 rc = GPG_ERR_BAD_DATA;
2754 goto fail;
2757 /* Overwriting the existing tree. */
2758 if (!rc)
2760 xmlUnlinkNode (n);
2761 xmlFreeNodeList (n);
2764 rc = 0;
2765 xmlSetProp (root, (xmlChar *) "_name", (xmlChar *) path[0]);
2766 n = xmlCopyNode (root, 1);
2767 n = xmlAddChildList (xmlDocGetRootElement (client->doc), n);
2770 if (n && !rc)
2771 rc = update_element_mtime (n->parent);
2773 fail:
2774 if (doc)
2775 xmlFreeDoc (doc);
2777 if (path)
2778 strv_free (path);
2780 strv_free (req);
2781 return rc;
2784 static gpg_error_t
2785 import_command (assuan_context_t ctx, char *line)
2787 gpg_error_t rc;
2788 struct client_s *client = assuan_get_pointer (ctx);
2789 unsigned char *result;
2790 size_t len;
2792 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2793 if (rc)
2794 return send_error (ctx, rc);
2796 rc = do_import (client, result);
2797 return send_error (ctx, rc);
2800 static gpg_error_t
2801 do_lock (struct client_s *client, int add)
2803 gpg_error_t rc = lock_file_mutex (client, add);
2805 if (!rc)
2806 client->flags |= FLAG_LOCK_CMD;
2808 return rc;
2811 static gpg_error_t
2812 lock_command (assuan_context_t ctx, char *line)
2814 struct client_s *client = assuan_get_pointer (ctx);
2815 gpg_error_t rc = do_lock (client, 0);
2817 return send_error (ctx, rc);
2820 static gpg_error_t
2821 unlock_command (assuan_context_t ctx, char *line)
2823 struct client_s *client = assuan_get_pointer (ctx);
2824 gpg_error_t rc;
2826 rc = unlock_file_mutex (client, 0);
2827 return send_error (ctx, rc);
2830 static gpg_error_t
2831 option_command (assuan_context_t ctx, const char *name, const char *value)
2833 struct client_s *client = assuan_get_pointer (ctx);
2834 struct agent_s *agent = client->crypto->agent;
2835 gpg_error_t rc = 0;
2837 log_write1 ("OPTION name='%s' value='%s'", name, value);
2839 if (strcasecmp (name, (char *) "log_level") == 0)
2841 long l = 0;
2843 if (value)
2845 l = strtol (value, NULL, 10);
2847 if (l < 0 || l > 2)
2848 return gpg_error (GPG_ERR_INV_VALUE);
2851 MUTEX_LOCK (&rcfile_mutex);
2852 config_set_int_param (&global_config, "global", "log_level", value);
2853 MUTEX_UNLOCK (&rcfile_mutex);
2854 goto done;
2856 else if (strcasecmp (name, (char *) "lock-timeout") == 0)
2858 long n = 0;
2859 char *p = NULL;
2861 if (value)
2863 n = strtol (value, &p, 10);
2864 if (p && *p)
2865 return gpg_error (GPG_ERR_INV_VALUE);
2868 client->lock_timeout = n;
2869 goto done;
2871 else if (strcasecmp (name, (char *) "NAME") == 0)
2873 char *tmp = pthread_getspecific (thread_name_key);
2875 if (tmp)
2876 xfree (tmp);
2878 if (!value || !*value)
2880 pthread_setspecific (thread_name_key, str_dup (""));
2881 goto done;
2884 pthread_setspecific (thread_name_key, str_dup (value));
2885 goto done;
2887 else if (strcasecmp (name, (char *) "lc-messages") == 0)
2889 rc = set_agent_option (client->crypto->agent, "lc-messages", value);
2890 if (!rc)
2892 xfree (agent->lc_messages);
2893 agent->lc_messages = str_dup (value);
2896 else if (strcasecmp (name, (char *) "lc-ctype") == 0)
2898 rc = set_agent_option (client->crypto->agent, "lc-ctype", value);
2899 if (!rc)
2901 xfree (agent->lc_ctype);
2902 agent->lc_ctype = str_dup (value);
2905 else if (strcasecmp (name, (char *) "ttyname") == 0)
2907 rc = set_agent_option (client->crypto->agent, "ttyname", value);
2908 if (!rc)
2910 xfree (agent->ttyname);
2911 agent->ttyname = str_dup (value);
2914 else if (strcasecmp (name, (char *) "ttytype") == 0)
2916 rc = set_agent_option (client->crypto->agent, "ttytype", value);
2917 if (!rc)
2919 xfree (agent->ttytype);
2920 agent->ttytype = str_dup (value);
2923 else if (strcasecmp (name, (char *) "display") == 0)
2925 rc = set_agent_option (client->crypto->agent, "display", value);
2926 if (!rc)
2928 xfree (agent->display);
2929 agent->display = str_dup (value);
2932 else if (strcasecmp (name, (char *) "desc") == 0)
2934 if (client->crypto->agent->desc)
2935 xfree (client->crypto->agent->desc);
2937 client->crypto->agent->desc = str_dup (value);
2938 if (!client->crypto->agent->desc)
2939 rc = GPG_ERR_ENOMEM;
2941 #if 0
2942 else if (strcasecmp (name, "pinentry_timeout") == 0)
2944 char *p = NULL;
2945 int n;
2947 if (!value)
2948 goto done;
2950 n = (int) strtol (value, &p, 10);
2952 if (*p || n < 0)
2953 return gpg_error (GPG_ERR_INV_VALUE);
2955 MUTEX_LOCK (&rcfile_mutex);
2956 config_set_int_param (global_config,
2957 client->filename ? client->filename : "global",
2958 "pinentry_timeout", value);
2959 MUTEX_UNLOCK (&rcfile_mutex);
2960 goto done;
2962 #endif
2963 else
2964 return gpg_error (GPG_ERR_UNKNOWN_OPTION);
2966 done:
2967 return rc;
2970 static gpg_error_t
2971 do_rename (assuan_context_t ctx, char *line)
2973 struct client_s *client = assuan_get_pointer (ctx);
2974 gpg_error_t rc;
2975 char **req, **src, *dst;
2976 xmlNodePtr n, ndst;
2978 req = str_split (line, " ", 0);
2980 if (!req || !req[0] || !req[1])
2982 strv_free (req);
2983 return GPG_ERR_SYNTAX;
2986 dst = req[1];
2987 is_literal_element (&dst);
2989 if (!valid_xml_element ((xmlChar *) dst))
2991 strv_free (req);
2992 return GPG_ERR_INV_VALUE;
2995 if (strchr (req[0], '\t'))
2996 src = str_split (req[0], "\t", 0);
2997 else
2998 src = str_split (req[0], " ", 0);
3000 if (!src || !*src)
3002 rc = GPG_ERR_SYNTAX;
3003 goto fail;
3006 n = find_root_element (client->doc, &src, &rc, NULL, 0, 0);
3007 if (src[1] && n)
3008 n = find_elements (client->doc, n->children, src + 1, &rc, NULL, NULL,
3009 NULL, 0, 0, NULL, 0);
3011 if (!n)
3012 goto fail;
3014 xmlChar *a = xmlGetProp (n, (xmlChar *) "_name");
3015 if (!a)
3017 rc = GPG_ERR_ENOMEM;
3018 goto fail;
3021 /* To prevent unwanted effects:
3023 * <root name="a"><b/></root>
3025 * RENAME a<TAB>b b
3027 if (xmlStrEqual (a, (xmlChar *) dst))
3029 xmlFree (a);
3030 rc = GPG_ERR_AMBIGUOUS_NAME;
3031 goto fail;
3034 xmlFree (a);
3035 char **tmp = NULL;
3036 if (src[1])
3038 char **p;
3040 for (p = src; *p; p++)
3042 if (!*(p + 1))
3043 break;
3044 strv_printf (&tmp, "%s", *p);
3048 strv_printf (&tmp, "!%s", dst);
3049 ndst = find_root_element (client->doc, &tmp, &rc, NULL, 0, 0);
3050 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3052 strv_free (tmp);
3053 goto fail;
3056 if (tmp[1] && ndst)
3057 ndst = find_elements (client->doc, ndst->children, tmp + 1, &rc, NULL,
3058 NULL, NULL, 0, 0, NULL, 0);
3060 strv_free (tmp);
3061 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3062 goto fail;
3064 rc = 0;
3066 /* Target may exist:
3068 * <root name="a"/>
3069 * <root name="b" target="a"/>
3071 * RENAME b a
3073 * Would need to do:
3074 * RENAME !b a
3076 if (ndst == n)
3078 rc = GPG_ERR_AMBIGUOUS_NAME;
3079 goto fail;
3082 if (ndst)
3084 unlink_node (ndst);
3085 xmlFreeNodeList (ndst);
3088 rc = add_attribute (n, "_name", dst);
3090 fail:
3091 strv_free (req);
3092 strv_free (src);
3093 return rc;
3096 static gpg_error_t
3097 rename_command (assuan_context_t ctx, char *line)
3099 struct client_s *client = assuan_get_pointer (ctx);
3100 gpg_error_t rc;
3101 struct argv_s *args[] = {
3102 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3103 NULL
3106 rc = parse_options (&line, args, client);
3107 if (rc)
3108 return send_error (ctx, rc);
3110 if (client->opts & OPT_INQUIRE)
3112 unsigned char *result;
3113 size_t len;
3115 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3116 if (rc)
3117 return send_error (ctx, rc);
3119 line = (char *) result;
3122 rc = do_rename (ctx, line);
3124 if (client->opts & OPT_INQUIRE)
3125 xfree (line);
3127 return send_error (ctx, rc);
3130 static gpg_error_t
3131 do_copy (assuan_context_t ctx, char *line)
3133 struct client_s *client = assuan_get_pointer (ctx);
3134 gpg_error_t rc;
3135 char **req, **src = NULL, **dst = NULL;
3136 xmlNodePtr nsrc, ndst, new = NULL;
3138 req = str_split (line, " ", 0);
3139 if (!req || !req[0] || !req[1])
3141 strv_free (req);
3142 return GPG_ERR_SYNTAX;
3145 if (strchr (req[0], '\t'))
3146 src = str_split (req[0], "\t", 0);
3147 else
3148 src = str_split (req[0], " ", 0);
3150 if (!src || !*src)
3152 rc = GPG_ERR_SYNTAX;
3153 goto fail;
3156 if (strchr (req[1], '\t'))
3157 dst = str_split (req[1], "\t", 0);
3158 else
3159 dst = str_split (req[1], " ", 0);
3161 if (!dst || !*dst)
3163 rc = GPG_ERR_SYNTAX;
3164 goto fail;
3167 if (!valid_element_path (dst, 0))
3169 rc = GPG_ERR_INV_VALUE;
3170 goto fail;
3173 nsrc = find_root_element (client->doc, &src, &rc, NULL, 0, 0);
3174 if (nsrc && src[1])
3175 nsrc = find_elements (client->doc, nsrc->children, src + 1, &rc, NULL,
3176 NULL, NULL, 0, 0, NULL, 0);
3178 if (!nsrc)
3179 goto fail;
3181 new = xmlCopyNodeList (nsrc);
3182 if (!new)
3184 rc = GPG_ERR_ENOMEM;
3185 goto fail;
3188 int create = 0;
3189 ndst = find_root_element (client->doc, &dst, &rc, NULL, 0, 0);
3190 if (ndst && dst[1])
3192 if (ndst->children)
3193 ndst = find_elements (client->doc, ndst->children, dst + 1, &rc, NULL,
3194 NULL, create_target_elements_cb, 0, 0, NULL, 0);
3195 else
3196 create = 1;
3198 else
3199 create = 1;
3201 if (!ndst && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3202 goto fail;
3203 else if (create)
3205 ndst = create_element_path (client, &dst, &rc, NULL);
3206 if (!ndst)
3207 goto fail;
3210 /* Merge any attributes from the src node to the initial dst node. */
3211 for (xmlAttrPtr attr = new->properties; attr; attr = attr->next)
3213 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3214 continue;
3216 xmlAttrPtr a = xmlHasProp (ndst, attr->name);
3217 if (a)
3218 xmlRemoveProp (a);
3220 xmlChar *tmp = xmlNodeGetContent (attr->children);
3221 xmlNewProp (ndst, attr->name, tmp);
3222 xmlFree (tmp);
3223 rc = add_attribute (ndst, NULL, NULL);
3226 xmlNodePtr n = ndst->children;
3227 xmlUnlinkNode (n);
3228 xmlFreeNodeList (n);
3229 ndst->children = NULL;
3231 if (new->children)
3233 n = xmlCopyNodeList (new->children);
3234 if (!n)
3236 rc = GPG_ERR_ENOMEM;
3237 goto fail;
3240 n = xmlAddChildList (ndst, n);
3241 if (!n)
3243 rc = GPG_ERR_ENOMEM;
3244 goto fail;
3247 rc = update_element_mtime (xmlDocGetRootElement (client->doc) ==
3248 ndst->parent ? ndst : ndst->parent);
3251 fail:
3252 if (new)
3254 xmlUnlinkNode (new);
3255 xmlFreeNodeList (new);
3258 if (req)
3259 strv_free (req);
3261 if (src)
3262 strv_free (src);
3264 if (dst)
3265 strv_free (dst);
3267 return rc;
3270 static gpg_error_t
3271 copy_command (assuan_context_t ctx, char *line)
3273 struct client_s *client = assuan_get_pointer (ctx);
3274 gpg_error_t rc;
3275 struct argv_s *args[] = {
3276 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3277 NULL
3280 rc = parse_options (&line, args, client);
3281 if (rc)
3282 return send_error (ctx, rc);
3284 if (client->opts & OPT_INQUIRE)
3286 unsigned char *result;
3287 size_t len;
3289 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3290 if (rc)
3291 return send_error (ctx, rc);
3293 line = (char *) result;
3296 rc = do_copy (ctx, line);
3298 if (client->opts & OPT_INQUIRE)
3299 xfree (line);
3301 return send_error (ctx, rc);
3304 static gpg_error_t
3305 do_move (assuan_context_t ctx, char *line)
3307 struct client_s *client = assuan_get_pointer (ctx);
3308 gpg_error_t rc;
3309 char **req, **src = NULL, **dst = NULL;
3310 xmlNodePtr nsrc, ndst = NULL;
3312 req = str_split (line, " ", 0);
3314 if (!req || !req[0] || !req[1])
3316 strv_free (req);
3317 return GPG_ERR_SYNTAX;
3320 if (strchr (req[0], '\t'))
3321 src = str_split (req[0], "\t", 0);
3322 else
3323 src = str_split (req[0], " ", 0);
3325 if (!src || !*src)
3327 rc = GPG_ERR_SYNTAX;
3328 goto fail;
3331 if (strchr (req[1], '\t'))
3332 dst = str_split (req[1], "\t", 0);
3333 else
3334 dst = str_split (req[1], " ", 0);
3336 nsrc = find_root_element (client->doc, &src, &rc, NULL, 0, 0);
3337 if (nsrc && src[1])
3338 nsrc = find_elements (client->doc, nsrc->children, src + 1, &rc, NULL,
3339 NULL, NULL, 0, 0, NULL, 0);
3341 if (!nsrc)
3342 goto fail;
3344 if (dst)
3346 if (!valid_element_path (dst, 0))
3348 rc = GPG_ERR_INV_VALUE;
3349 goto fail;
3352 ndst = find_root_element (client->doc, &dst, &rc, NULL, 0, 0);
3353 if (ndst && dst[1])
3354 ndst = find_elements (client->doc, ndst->children, dst + 1, &rc, NULL,
3355 NULL, NULL, 0, 0, NULL, 0);
3357 else
3358 ndst = xmlDocGetRootElement (client->doc);
3360 for (xmlNodePtr n = ndst; n; n = n->parent)
3362 if (n == nsrc)
3364 rc = GPG_ERR_CONFLICT;
3365 goto fail;
3369 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3370 goto fail;
3372 rc = 0;
3374 if (ndst)
3376 xmlChar *a = node_has_attribute (nsrc, (xmlChar *) "_name");
3377 xmlNodePtr dup = find_element (ndst->children, (char *) a, NULL);
3379 xmlFree (a);
3380 if (dup)
3382 if (dup == nsrc)
3383 goto fail;
3385 if (ndst == xmlDocGetRootElement (client->doc))
3387 xmlNodePtr n = nsrc;
3388 int match = 0;
3390 while (n->parent && n->parent != ndst)
3391 n = n->parent;
3393 xmlChar *a = node_has_attribute (n, (xmlChar *) "_name");
3394 xmlChar *b = node_has_attribute (nsrc, (xmlChar *) "_name");
3396 if (xmlStrEqual (a, b))
3398 match = 1;
3399 xmlUnlinkNode (nsrc);
3400 xmlUnlinkNode (n);
3401 xmlFreeNodeList (n);
3404 xmlFree (a);
3405 xmlFree (b);
3407 if (!match)
3409 xmlUnlinkNode (dup);
3410 xmlFreeNodeList (dup);
3413 else
3414 xmlUnlinkNode (dup);
3418 if (!ndst && dst)
3420 xmlChar *name = node_has_attribute (nsrc, (xmlChar *) "_name");
3422 if (nsrc->parent == xmlDocGetRootElement (client->doc)
3423 && !strcmp ((char *) name, *dst))
3425 xmlFree (name);
3426 rc = GPG_ERR_CONFLICT;
3427 goto fail;
3430 xmlFree (name);
3431 ndst = create_element_path (client, &dst, &rc, nsrc);
3434 if (!ndst)
3435 goto fail;
3437 update_element_mtime (nsrc->parent);
3438 xmlUnlinkNode (nsrc);
3439 ndst = xmlAddChildList (ndst, nsrc);
3441 if (!ndst)
3442 rc = GPG_ERR_ENOMEM;
3444 update_element_mtime (ndst->parent);
3446 fail:
3447 if (req)
3448 strv_free (req);
3450 if (src)
3451 strv_free (src);
3453 if (dst)
3454 strv_free (dst);
3456 return rc;
3459 static gpg_error_t
3460 move_command (assuan_context_t ctx, char *line)
3462 struct client_s *client = assuan_get_pointer (ctx);
3463 gpg_error_t rc;
3464 struct argv_s *args[] = {
3465 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3466 NULL
3469 rc = parse_options (&line, args, client);
3470 if (rc)
3471 return send_error (ctx, rc);
3473 if (client->opts & OPT_INQUIRE)
3475 unsigned char *result;
3476 size_t len;
3478 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3479 if (rc)
3480 return send_error (ctx, rc);
3482 line = (char *) result;
3485 rc = do_move (ctx, line);
3487 if (client->opts & OPT_INQUIRE)
3488 xfree (line);
3490 return send_error (ctx, rc);
3493 static gpg_error_t
3494 ls_command (assuan_context_t ctx, char *line)
3496 gpg_error_t rc;
3497 char *tmp = str_asprintf ("%s/data", homedir);
3498 char *dir = expand_homedir (tmp);
3499 DIR *d = opendir (dir);
3501 rc = gpg_error_from_syserror ();
3502 xfree (tmp);
3504 if (!d)
3506 xfree (dir);
3507 return send_error (ctx, rc);
3510 size_t len =
3511 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3512 struct dirent *p = xmalloc (len), *cur = NULL;
3513 char *list = NULL;
3515 xfree (dir);
3516 rc = 0;
3518 while (!readdir_r (d, p, &cur) && cur)
3520 if (cur->d_name[0] == '.' && cur->d_name[1] == '\0')
3521 continue;
3522 else if (cur->d_name[0] == '.' && cur->d_name[1] == '.'
3523 && cur->d_name[2] == '\0')
3524 continue;
3526 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3528 if (!tmp)
3530 if (list)
3531 xfree (list);
3533 rc = GPG_ERR_ENOMEM;
3534 break;
3537 xfree (list);
3538 list = tmp;
3541 closedir (d);
3542 xfree (p);
3544 if (rc)
3545 return send_error (ctx, rc);
3547 if (!list)
3548 return send_error (ctx, GPG_ERR_NO_DATA);
3550 list[strlen (list) - 1] = 0;
3551 rc = xfer_data (ctx, list, strlen (list));
3552 xfree (list);
3553 return send_error (ctx, rc);
3556 static gpg_error_t
3557 bye_notify (assuan_context_t ctx, char *line)
3559 struct client_s *cl = assuan_get_pointer (ctx);
3561 #ifdef WITH_GNUTLS
3562 if (cl->thd->remote)
3564 int rc;
3568 struct timeval tv = { 0, 50000 };
3570 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3571 if (rc == GNUTLS_E_AGAIN)
3572 select (0, NULL, NULL, NULL, &tv);
3574 while (rc == GNUTLS_E_AGAIN);
3576 #endif
3578 /* This will let assuan_process_next() return. */
3579 fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK);
3580 cl->last_rc = 0; // BYE command result
3581 return 0;
3584 static gpg_error_t
3585 reset_notify (assuan_context_t ctx, char *line)
3587 struct client_s *client = assuan_get_pointer (ctx);
3589 if (client)
3590 cleanup_client (client);
3592 return 0;
3596 * This is called before every Assuan command.
3598 static gpg_error_t
3599 command_startup (assuan_context_t ctx, const char *name)
3601 struct client_s *client = assuan_get_pointer (ctx);
3602 gpg_error_t rc;
3603 struct command_table_s *cmd = NULL;
3605 log_write1 ("command='%s'", name);
3606 client->last_rc = client->opts = 0;
3608 for (int i = 0; command_table[i]; i++)
3610 if (!strcasecmp (name, command_table[i]->name))
3612 if (command_table[i]->ignore_startup)
3613 return 0;
3614 cmd = command_table[i];
3615 break;
3619 client->last_rc = rc = gpg_error (file_modified (client, cmd));
3620 return rc;
3624 * This is called after every Assuan command.
3626 static void
3627 command_finalize (assuan_context_t ctx, gpg_error_t rc)
3629 struct client_s *client = assuan_get_pointer (ctx);
3631 if (!(client->flags & FLAG_LOCK_CMD))
3632 unlock_file_mutex (client, 0);
3634 log_write1 (_("command completed: rc=%u"), client->last_rc);
3635 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
3638 static gpg_error_t
3639 help_command (assuan_context_t ctx, char *line)
3641 gpg_error_t rc;
3642 int i;
3644 if (!line || !*line)
3646 char *tmp;
3647 char *help = str_dup (_("Usage: HELP [<COMMAND>]\n"
3648 "For commands that take an element path as an argument, each element is "
3649 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3650 "\n" "COMMANDS:"));
3652 for (i = 0; command_table[i]; i++)
3654 if (!command_table[i]->help)
3655 continue;
3657 tmp = str_asprintf ("%s %s", help, command_table[i]->name);
3658 xfree (help);
3659 help = tmp;
3662 tmp = strip_texi_and_wrap (help);
3663 xfree (help);
3664 rc = xfer_data (ctx, tmp, strlen (tmp));
3665 xfree (tmp);
3666 return send_error (ctx, rc);
3669 for (i = 0; command_table[i]; i++)
3671 if (!strcasecmp (line, command_table[i]->name))
3673 char *help, *tmp;
3675 if (!command_table[i]->help)
3676 break;
3678 help = strip_texi_and_wrap (command_table[i]->help);
3679 tmp = str_asprintf (_("Usage: %s"), help);
3680 xfree (help);
3681 rc = xfer_data (ctx, tmp, strlen (tmp));
3682 xfree (tmp);
3683 return send_error (ctx, rc);
3687 return send_error (ctx, GPG_ERR_INV_NAME);
3690 static void
3691 new_command (const char *name, int ignore, int unlock,
3692 gpg_error_t (*handler) (assuan_context_t, char *),
3693 const char *help)
3695 int i = 0;
3697 if (command_table)
3698 for (i = 0; command_table[i]; i++);
3700 command_table =
3701 xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
3702 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
3703 command_table[i]->name = name;
3704 command_table[i]->handler = handler;
3705 command_table[i]->ignore_startup = ignore;
3706 command_table[i]->unlock = unlock;
3707 command_table[i++]->help = help;
3708 command_table[i] = NULL;
3711 void
3712 deinit_commands ()
3714 int i;
3716 for (i = 0; command_table[i]; i++)
3717 xfree (command_table[i]);
3719 xfree (command_table);
3722 static int
3723 sort_commands (const void *arg1, const void *arg2)
3725 struct command_table_s *const *a = arg1;
3726 struct command_table_s *const *b = arg2;
3728 if (!*a || !*b)
3729 return 0;
3730 else if (*a && !*b)
3731 return 1;
3732 else if (!*a && *b)
3733 return -1;
3735 return strcmp ((*a)->name, (*b)->name);
3738 static gpg_error_t
3739 passwd_command (assuan_context_t ctx, char *line)
3741 struct client_s *client = assuan_get_pointer (ctx);
3742 gpg_error_t rc;
3743 struct argv_s *args[] = {
3744 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
3745 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
3746 NULL
3749 if (client->flags & FLAG_NEW)
3750 return send_error (ctx, GPG_ERR_INV_STATE);
3752 client->crypto->save.s2k_count =
3753 config_get_ulong (client->filename, "s2k_count");
3754 rc = parse_options (&line, args, client);
3755 if (rc)
3756 return send_error (ctx, rc);
3758 if (!rc && client->opts & OPT_RESET)
3760 rc = cache_clear (client->md5file);
3761 if (!rc)
3762 send_status_all (STATUS_CACHE, NULL);
3765 if (!rc)
3767 if (client->crypto->save.s2k_count)
3768 rc = send_to_agent (client->crypto->agent, NULL, NULL,
3769 "OPTION s2k-count=%lu",
3770 client->crypto->save.s2k_count);
3772 if (!rc)
3773 rc = agent_passwd (client->crypto);
3776 return send_error (ctx, rc);
3779 static gpg_error_t
3780 parse_keygrip_opt_sign (void *data, void *value)
3782 struct client_s *client = data;
3784 (void) value;
3785 client->opts |= OPT_SIGN;
3786 return 0;
3789 static gpg_error_t
3790 keygrip_command (assuan_context_t ctx, char *line)
3792 struct client_s *client = assuan_get_pointer (ctx);
3793 gpg_error_t rc;
3794 struct crypto_s *crypto;
3795 struct argv_s *args[] = {
3796 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_keygrip_opt_sign},
3797 NULL
3800 if (!line || !*line)
3801 return send_error (ctx, GPG_ERR_SYNTAX);
3803 rc = parse_options (&line, args, client);
3804 if (rc)
3805 return send_error (ctx, rc);
3807 if (!valid_filename (line))
3808 return send_error (ctx, GPG_ERR_INV_VALUE);
3810 rc = init_client_crypto (&crypto);
3811 if (rc)
3812 return send_error (ctx, rc);
3814 rc = read_data_file (line, crypto);
3815 if (!rc)
3817 char *hexgrip = NULL;
3819 if (client->opts & OPT_SIGN)
3821 if (valid_keygrip (crypto->sign_grip, sizeof (crypto->sign_grip)))
3822 hexgrip = bin2hex (crypto->sign_grip, sizeof (crypto->sign_grip));
3825 if (!hexgrip)
3826 hexgrip = bin2hex (crypto->grip, sizeof (crypto->grip));
3828 if (!hexgrip)
3829 rc = GPG_ERR_ENOMEM;
3830 else
3831 rc = xfer_data (ctx, hexgrip, strlen (hexgrip));
3833 xfree (hexgrip);
3836 cleanup_crypto (&crypto);
3837 return send_error (ctx, rc);
3840 static gpg_error_t
3841 parse_opt_data (void *data, void *value)
3843 struct client_s *client = data;
3845 (void) value;
3846 client->opts |= OPT_DATA;
3847 return 0;
3850 static gpg_error_t
3851 getinfo_command (assuan_context_t ctx, char *line)
3853 struct client_s *client = assuan_get_pointer (ctx);
3854 gpg_error_t rc;
3855 char buf[ASSUAN_LINELENGTH];
3856 struct argv_s *args[] = {
3857 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
3858 NULL
3861 rc = parse_options (&line, args, client);
3862 if (rc)
3863 return send_error (ctx, rc);
3865 if (!strcasecmp (line, "clients"))
3867 if (client->opts & OPT_DATA)
3869 MUTEX_LOCK (&cn_mutex);
3870 snprintf (buf, sizeof (buf), "%i", slist_length (cn_thread_list));
3871 MUTEX_UNLOCK (&cn_mutex);
3872 rc = xfer_data (ctx, buf, strlen (buf));
3874 else
3875 rc = send_status (ctx, STATUS_CLIENTS, NULL);
3877 else if (!strcasecmp (line, "cache"))
3879 if (client->opts & OPT_DATA)
3881 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
3882 rc = xfer_data (ctx, buf, strlen (buf));
3884 else
3885 rc = send_status (ctx, STATUS_CACHE, NULL);
3887 else if (!strcasecmp (line, "pid"))
3889 char buf[32];
3890 pid_t pid = getpid ();
3892 snprintf (buf, sizeof (buf), "%i", pid);
3893 rc = xfer_data (ctx, buf, strlen (buf));
3895 else if (!strcasecmp (line, "version"))
3897 char *buf = str_asprintf ("0x%06x %s", VERSION_HEX,
3898 #ifdef WITH_LIBACL
3899 "ACL "
3900 #endif
3901 #ifdef WITH_GNUTLS
3902 "GNUTLS "
3903 #endif
3904 "");
3905 rc = xfer_data (ctx, buf, strlen (buf));
3906 xfree (buf);
3908 else if (!strcasecmp (line, "last_error"))
3910 if (client->last_error)
3911 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
3912 else
3913 rc = GPG_ERR_NO_DATA;
3915 else
3916 rc = gpg_error (GPG_ERR_SYNTAX);
3918 return send_error (ctx, rc);
3921 static gpg_error_t
3922 send_data_cb (void *user, const void *buf, size_t len)
3924 assuan_context_t ctx = user;
3926 return assuan_send_data (ctx, buf, len);
3929 static gpg_error_t
3930 send_status_cb (void *user, const char *line)
3932 assuan_context_t ctx = user;
3933 char keyword[200], *k;
3934 const char *p;
3936 for (p = line, k = keyword; *p; p++)
3938 if (isspace (*p))
3939 break;
3941 *k++ = *p;
3944 *k = 0;
3945 if (*p == '#')
3946 p++;
3948 while (isspace (*p))
3949 p++;
3951 return assuan_write_status (ctx, keyword, *p ? p : NULL);
3954 static gpg_error_t
3955 agent_command (assuan_context_t ctx, char *line)
3957 gpg_error_t rc;
3958 struct client_s *client = assuan_get_pointer (ctx);
3960 if (!line || !*line)
3961 return send_error (ctx, GPG_ERR_SYNTAX);
3963 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 1);
3964 rc = assuan_transact (client->crypto->agent->ctx, line, send_data_cb,
3965 client->ctx, agent_loopback_cb, client->crypto,
3966 send_status_cb, client->ctx);
3967 if (gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
3969 char *line;
3970 size_t len;
3972 rc = assuan_write_line (client->crypto->agent->ctx, "CAN");
3973 if (!rc)
3975 rc = assuan_read_line (client->crypto->agent->ctx, &line, &len);
3976 if (!rc)
3977 rc = gpg_error (GPG_ERR_ASS_CANCELED);
3981 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 0);
3982 return send_error (ctx, rc);
3985 void
3986 init_commands ()
3988 /* !BEGIN-HELP-TEXT!
3990 * This comment is used as a marker to generate the offline documentation
3991 * found in doc/pwmd.info. The indentation needs to be kept for the awk
3992 * script to determine where commands begin and end.
3994 new_command("HELP", 1, 1, help_command, _(
3995 "HELP [<COMMAND>]\n"
3996 "Show available commands or command specific help text."
3999 new_command("AGENT", 1, 1, agent_command, _(
4000 "AGENT <command>\n"
4001 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4002 "@command{gpg-agent}."
4005 new_command("GETINFO", 1, 1, getinfo_command, _(
4006 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4007 "Get server and other information: @var{cache} returns the number of cached "
4008 "documents via a status message. @var{clients} returns the number of "
4009 "connected clients via a status message. @var{pid} returns the process ID "
4010 "number of the server via a data response. @var{VERSION} returns the server "
4011 "version number and compile-time features with a data response with each "
4012 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4013 "the last failed command when available. @xref{Status Messages}. "
4014 "\n"
4015 "When the @option{--data} option is specified then the result will be send "
4016 "via a data response rather than a status message."
4019 new_command("PASSWD", 0, 0, passwd_command, _(
4020 "PASSWD [--reset] [--s2k-count=N]\n"
4021 "Changes the passphrase of the secret key required to open the current "
4022 "file. When the @option{--reset} option is passed then the cache entry for "
4023 "the current file will be reset and the passphrase, if any, will be required "
4024 "during the next @code{OPEN}. @xref{OPEN}."
4025 "\n"
4026 "The @option{--s2k-count} option sets number of hash iterations for a "
4027 "passphrase and must be either @code{0} to use the calibrated count of the "
4028 "machine (the default), or a value greater than or equal to @code{65536}. "
4029 "@xref{SAVE}."
4032 new_command("KEYGRIP", 1, 1, keygrip_command, _(
4033 "KEYGRIP [--sign] <filename>\n"
4034 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4035 "data response."
4036 "\n"
4037 "When the @option{--sign} option is specified then the key used for signing "
4038 "of the specified @var{filename} will be returned."
4041 new_command("OPEN", 1, 1, open_command, _(
4042 "OPEN [--lock] [--no-pinentry] <filename> [<passphrase>]\n"
4043 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4044 "found on the file-system then a new document will be created. If the file "
4045 "is found, it is looked for in the file cache. If cached and no "
4046 "@var{passphrase} was specified then the cached document is opened. When not "
4047 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4048 "for decryption unless @option{--no-pinentry} was specified (see below)."
4049 "\n"
4050 "When the @option{--lock} option is passed then the file mutex will be "
4051 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4052 "file has been opened."
4053 "\n"
4054 "By default, a pinentry is used to retrieve a passphrase when required. "
4055 "Passing @option{--no-pinentry} will disable pinentry use for the rest of "
4056 "the session. When pinentry use is disabled but required for some operation "
4057 "then a server @emph{INQUIRE} will be send to the client to retrieve the "
4058 "passphrase. See the @code{OPTION} command (@pxref{OPTION}), for pinentry "
4059 "specific options."
4062 new_command("SAVE", 0, 0, save_command, _(
4063 "SAVE [--no-passphrase] [--reset] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring [--sign-keygrip=hexstring]]\n"
4064 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4065 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4066 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4067 "keypair will be generated and a pinentry will be used to prompt for the "
4068 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4069 "passed, in which case the data file will not be passphrase protected."
4070 "\n"
4071 "The @option{--reset} option will clear the cache entry for the current file "
4072 "before saving."
4073 "\n"
4074 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4075 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4076 "(@pxref{Configuration}) for available ciphers."
4077 "\n"
4078 "The @option{--cipher-iterations} option specifies the number of times to "
4079 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4080 "\n"
4081 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4082 "the client to obtain the key paramaters to use when generating a new "
4083 "keypair. The inquired data is expected to be an S-expression. If not "
4084 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4085 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4086 "that when this option is specified a new keypair will be generated "
4087 "reguardless if the file is a new one or not."
4088 "\n"
4089 "You can encrypt the data file to a public key other than the one that it "
4090 "was originally encrypted with by passing the @option{--keygrip} option with "
4091 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4092 "be of any key that @command{gpg-agent} knows about. The "
4093 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4094 "secret key. This option may be needed when using a smartcard."
4095 "\n"
4096 "The @option{--s2k-count} option sets number of hash iterations for a "
4097 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4098 "value which is the default. This setting only affects new files. To change "
4099 "the setting, use the @code{PASSWD} command (@pxref{PASSWD})."
4102 new_command("ISCACHED", 1, 0, iscached_command, _(
4103 "ISCACHED [--lock] <filename>\n"
4104 "An @emph{OK} response is returned if the specified @var{filename} is found "
4105 "in the file cache. If not found in the cache but exists on the filesystem "
4106 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4107 "returned."
4108 "\n"
4109 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4110 "file exists; it does not need to be opened nor cached."
4113 new_command("CLEARCACHE", 1, 1, clearcache_command, _(
4114 "CLEARCACHE [<filename>]\n"
4115 "Clears a file cache entry for all or the specified @var{filename}."
4118 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command, _(
4119 "CACHETIMEOUT <filename> <seconds>\n"
4120 "The time in @var{seconds} until @var{filename} will be removed from the "
4121 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4122 "the passphrase for each @code{OPEN} command (@pxref{OPEN}). "
4123 "@xref{Configuration}, and the @code{cache_timeout} parameter."
4126 new_command("LIST", 0, 1, list_command, _(
4127 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4128 "If no element path is given then a newline separated list of root elements "
4129 "is returned with a data response. If given, then all reachable elements "
4130 "of the specified element path are returned unless the @option{--no-recurse} "
4131 "option is specified. If specified, only the child elements of the element "
4132 "path are returned without recursing into grandchildren. Each resulting "
4133 "element is prefixed with the literal @code{!} character when the element "
4134 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4135 "\n"
4136 "When the @option{--verbose} option is passed then each element path "
4137 "returned will have zero or more flags appened to it. These flags are "
4138 "delimited from the element path by a single space character. A flag itself "
4139 "is a single character. Flag @code{+} indicates that there are child nodes of "
4140 "the current element path. Flag @code{E} indicates that an element of an "
4141 "element path contained in a @var{target} attribute could not be found. Flag "
4142 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4143 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4144 "of a @var{target} attribute (see below)."
4145 "\n"
4146 "The @option{--with-target} option implies @option{--verbose} and will append "
4147 "an additional flag @code{T} followed by a single space then an element path. "
4148 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4149 "current element when it contains a @var{target} attribute. When no "
4150 "@var{target} attribute is found then no flag will be appended."
4151 "\n"
4152 "The @option{--no-recurse} option limits the amount of data returned to only "
4153 "the listing of children of the specified element path and not any "
4154 "grandchildren."
4155 "\n"
4156 "The @option{--all} option lists the entire element tree for each root "
4157 "element. This option also implies option @option{--verbose}."
4158 "\n"
4159 "When the @option{--inquire} option is passed then all remaining non-option "
4160 "arguments are retrieved via a server @emph{INQUIRE}."
4163 new_command("REALPATH", 0, 1, realpath_command, _(
4164 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4165 "Resolves all @code{target} attributes of the specified element path and "
4166 "returns the result with a data response. @xref{Target Attribute}, for details."
4167 "\n"
4168 "When the @option{--inquire} option is passed then all remaining non-option "
4169 "arguments are retrieved via a server @emph{INQUIRE}."
4172 new_command("STORE", 0, 1, store_command, _(
4173 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4174 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4175 "\n"
4176 "Creates a new element path or modifies the @var{content} of an existing "
4177 "element. If only a single element is specified then a new root element is "
4178 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4179 "set to the final @key{TAB} delimited element. If no @var{content} is "
4180 "specified after the final @key{TAB}, then the content of the element will "
4181 "be removed, or empty when creating a new element."
4182 "\n"
4183 "The only restriction of an element name is that it not contain whitespace "
4184 "or begin with the literal element character @code{!} unless specifying a "
4185 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4186 "the @key{TAB} delimited elements. It is recommended that the content of an "
4187 "element be base64 encoded when it contains control or @key{TAB} characters "
4188 "to prevent @abbr{XML} and @command{pwmd} parsing errors."
4191 new_command("RENAME", 0, 1, rename_command, _(
4192 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4193 "Renames the specified @var{element} to the new @var{value}. If an element of "
4194 "the same name as the @var{value} already exists it will be overwritten."
4195 "\n"
4196 "When the @option{--inquire} option is passed then all remaining non-option "
4197 "arguments are retrieved via a server @emph{INQUIRE}."
4200 new_command("COPY", 0, 1, copy_command, _(
4201 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4202 "Copies the entire element tree starting from the child node of the source "
4203 "element, to the destination element path. If the destination element path "
4204 "does not exist then it will be created; otherwise it is overwritten."
4205 "\n"
4206 "Note that attributes from the source element are merged into the "
4207 "destination element when the destination element path exists. When an "
4208 "attribute of the same name exists in both the source and destination "
4209 "elements then the destination attribute will be updated to the source "
4210 "attribute value."
4211 "\n"
4212 "When the @option{--inquire} option is passed then all remaining non-option "
4213 "arguments are retrieved via a server @emph{INQUIRE}."
4216 new_command("MOVE", 0, 1, move_command, _(
4217 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4218 "Moves the source element path to the destination element path. If the "
4219 "destination is not specified then it will be moved to the root node of the "
4220 "document. If the destination is specified and exists then it will be "
4221 "overwritten; otherwise it will be created."
4222 "\n"
4223 "When the @option{--inquire} option is passed then all remaining non-option "
4224 "arguments are retrieved via a server @emph{INQUIRE}."
4227 new_command("DELETE", 0, 1, delete_command, _(
4228 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4229 "Removes the specified element path and all of its children. This may break "
4230 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4231 "refers to this element or any of its children."
4232 "\n"
4233 "When the @option{--inquire} option is passed then all remaining non-option "
4234 "arguments are retrieved via a server @emph{INQUIRE}."
4237 new_command("GET", 0, 1, get_command, _(
4238 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4239 "Retrieves the content of the specified element. The content is returned "
4240 "with a data response."
4241 "\n"
4242 "When the @option{--inquire} option is passed then all remaining non-option "
4243 "arguments are retrieved via a server @emph{INQUIRE}."
4246 new_command("ATTR", 0, 1, attr_command, _(
4247 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4248 "@table @asis\n"
4249 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4250 "\n"
4251 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4252 "element. When no @var{value} is specified any existing value will be removed."
4253 "\n"
4254 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4255 "\n"
4256 " Removes an @var{attribute} from an element."
4257 "\n"
4258 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4259 "\n"
4260 " Retrieves a newline separated list of attributes names and values "
4261 "from the specified element. Each attribute name and value is space delimited."
4262 "\n"
4263 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4264 "\n"
4265 " Retrieves the value of an @var{attribute} from an element."
4266 "@end table\n"
4267 "\n"
4268 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4269 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4270 "commands instead."
4271 "\n"
4272 "The @code{_mtime} attribute is updated each time an element is modified by "
4273 "either storing content, editing attributes or by deleting a child element. "
4274 "The @code{_ctime} attribute is created for each new element in an element "
4275 "path."
4276 "\n"
4277 "When the @option{--inquire} option is passed then all remaining non-option "
4278 "arguments are retrieved via a server @emph{INQUIRE}."
4279 "\n"
4280 "@xref{Target Attribute}, for details about this special attribute."
4283 new_command("XPATH", 0, 1, xpath_command, _(
4284 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4285 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4286 "specified, it is assumed the expression is a request to return a result. "
4287 "Otherwise, the result is set to the @var{value} argument and the document is "
4288 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4289 "is assumed to be empty and the document is updated. For example:"
4290 "@sp 1\n"
4291 "@example\n"
4292 "XPATH //element[@@_name='password']@key{TAB}\n"
4293 "@end example\n"
4294 "@sp 1"
4295 "would clear the content of all @code{password} elements in the data file "
4296 "while leaving off the trailing @key{TAB} would return all @code{password} "
4297 "elements in @abbr{XML} format."
4298 "\n"
4299 "When the @option{--inquire} option is passed then all remaining non-option "
4300 "arguments are retrieved via a server @emph{INQUIRE}."
4301 "\n"
4302 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4303 "expression syntax."
4306 new_command("XPATHATTR", 0, 1, xpathattr_command, _(
4307 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4308 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4309 "attributes and does not return a result. For the @var{SET} operation the "
4310 "@var{value} is optional but the field is required. If not specified then "
4311 "the attribute value will be empty. For example:"
4312 "@sp 1"
4313 "@example\n"
4314 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4315 "@end example\n"
4316 "@sp 1"
4317 "would create an @code{password} attribute for each @code{password} element "
4318 "found in the document. The attribute value will be empty but still exist."
4319 "\n"
4320 "When the @option{--inquire} option is passed then all remaining non-option "
4321 "arguments are retrieved via a server @emph{INQUIRE}."
4322 "\n"
4323 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4324 "expression syntax."
4327 new_command("IMPORT", 0, 1, import_command, _(
4328 "IMPORT <content>[<TAB>[!]element[<TAB>[!]child[..]]]\n"
4329 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4330 "\n"
4331 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4332 "argument is raw @abbr{XML} data. The content is created as a child of the "
4333 "specified element path and will overwrite an existing element of the same "
4334 "name. If an element of the element path does not exist then it will be "
4335 "created."
4336 "\n"
4337 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4338 "for details."
4341 new_command("DUMP", 0, 1, dump_command, _(
4342 "DUMP\n"
4343 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4344 "dumping a specific node."
4347 new_command("LOCK", 0, 0, lock_command, _(
4348 "LOCK\n"
4349 "Locks the mutex associated with the opened file. This prevents other clients "
4350 "from sending commands to the same opened file until the client "
4351 "that sent this command either disconnects or sends the @code{UNLOCK} "
4352 "command. @xref{UNLOCK}."
4355 new_command("UNLOCK", 1, 0, unlock_command, _(
4356 "UNLOCK\n"
4357 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4358 "a commands' @option{lock} option. @xref{LOCK}."
4361 new_command("GETCONFIG", 1, 1, getconfig_command, _(
4362 "GETCONFIG [filename] <parameter>\n"
4363 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4364 "data response. If no file has been opened then the value for @var{filename} "
4365 "or the default from the @samp{global} section will be returned. If a file "
4366 "has been opened and no @var{filename} is specified, a value previously "
4367 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4370 new_command("OPTION", 1, 1, NULL, _(
4371 "OPTION <NAME>=<VALUE>\n"
4372 "Sets a client option @var{name} to @var{value}. The value for an option is "
4373 "kept for the duration of the connection."
4374 "\n"
4375 "@table @asis\n"
4376 "@item TTYNAME\n"
4377 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4378 "\n"
4379 "@item TTYTYPE\n"
4380 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4381 "\n"
4382 "@item DISPLAY\n"
4383 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4384 "\n"
4385 "@item DESC\n"
4386 " Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4387 "\n"
4388 "@item LC-CTYPE\n"
4389 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4390 "\n"
4391 "@item LC-MESSAGES\n"
4392 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4393 "\n"
4394 "@item NAME\n"
4395 " Associates the thread ID of the connection with the specified textual "
4396 "representation. Useful for debugging log messages."
4397 "\n"
4398 "@item LOCK-TIMEOUT\n"
4399 " When not @code{0}, the duration in tenths of a second to wait for the file "
4400 "mutex which has been locked by another thread to be released before returning "
4401 "an error. When @code{-1}, then an error will be returned immediately."
4402 "@end table\n"
4405 new_command("LS", 1, 1, ls_command, _(
4406 "LS\n"
4407 "Lists the available data files stored in the data directory "
4408 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4411 new_command("RESET", 1, 1, NULL, _(
4412 "RESET\n"
4413 "Closes the currently opened file but keeps any previously set client options."
4416 new_command("NOP", 1, 1, NULL, _(
4417 "NOP\n"
4418 "Does nothing. Always returns successfully."
4421 /* !END-HELP-TEXT! */
4422 new_command ("CANCEL", 1, 1, NULL, NULL);
4423 new_command ("END", 1, 1, NULL, NULL);
4424 new_command ("BYE", 1, 1, NULL, NULL);
4426 int i;
4427 for (i = 0; command_table[i]; i++);
4428 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4429 sort_commands);
4432 gpg_error_t
4433 register_commands (assuan_context_t ctx)
4435 int i = 0, rc;
4437 for (; command_table[i]; i++)
4439 if (!command_table[i]->handler)
4440 continue;
4442 rc = assuan_register_command (ctx, command_table[i]->name,
4443 command_table[i]->handler,
4444 command_table[i]->help);
4445 if (rc)
4446 return rc;
4449 rc = assuan_register_option_handler (ctx, option_command);
4450 if (rc)
4451 return rc;
4453 rc = assuan_register_bye_notify (ctx, bye_notify);
4454 if (rc)
4455 return rc;
4457 rc = assuan_register_reset_notify (ctx, reset_notify);
4458 if (rc)
4459 return rc;
4461 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4462 if (rc)
4463 return rc;
4465 return assuan_register_post_cmd_notify (ctx, command_finalize);