Fix SAVE --keygrip and --sign-keygrip.
[pwmd.git] / src / commands.c
blobc836d1833d5e54d18d1fd335563911aba34c274c
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
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 #include "pwmd-error.h"
38 #include <gcrypt.h>
40 #include "mem.h"
41 #include "xml.h"
42 #include "util-misc.h"
43 #include "common.h"
44 #include "rcfile.h"
45 #include "cache.h"
46 #include "commands.h"
47 #include "mutex.h"
48 #include "crypto.h"
49 #include "pinentry.h"
51 /* Flags needed to be retained for a client across commands. */
52 #define FLAG_NEW 0x0001
53 #define FLAG_HAS_LOCK 0x0002
54 #define FLAG_LOCK_CMD 0x0004
55 #define FLAG_NO_PINENTRY 0x0008
56 #define FLAG_OPEN 0x0010
57 #define FLAG_KEEP_LOCK 0x0020
59 /* These are command option flags. */
60 #define OPT_INQUIRE 0x0001
61 #define OPT_NO_PASSPHRASE 0x0002
62 #define OPT_RESET 0x0004
63 #define OPT_LIST_RECURSE 0x0008
64 #define OPT_LIST_VERBOSE 0x0010
65 #define OPT_LOCK 0x0020
66 #define OPT_LOCK_ON_OPEN 0x0040
67 #define OPT_SIGN 0x0080
68 #define OPT_LIST_ALL 0x0100
69 #define OPT_LIST_WITH_TARGET 0x0200
70 #define OPT_DATA 0x0400
71 #define OPT_NO_AGENT 0x0800
73 #ifdef WITH_AGENT
74 /* The GETCONFIG command, for example, may fail because pwmd lost the
75 * gpg-agent connection. Update the recovered agent ctx. */
76 #define UPDATE_AGENT_CTX(client, crypto) do { \
77 if (crypto && crypto->agent && crypto->agent->did_restart \
78 && client->crypto && client->crypto->agent \
79 && client->crypto->agent->ctx && \
80 crypto->agent->ctx != client->crypto->agent->ctx) \
81 { \
82 client->crypto->agent->ctx = crypto->agent->ctx; \
83 crypto->agent->ctx = NULL; \
84 } \
85 } while (0)
86 #else
87 #define UPDATE_AGENT_CTX(client, crypto)
88 #endif
90 struct command_table_s
92 const char *name;
93 gpg_error_t (*handler) (assuan_context_t, char *line);
94 const char *help;
95 int ignore_startup;
96 int unlock; // unlock the file mutex after validating the checksum
99 static struct command_table_s **command_table;
101 static gpg_error_t do_lock (struct client_s *client, int add);
102 static gpg_error_t validate_checksum (struct client_s *,
103 struct cache_data_s *);
104 static gpg_error_t update_checksum (struct client_s *client);
106 static gpg_error_t
107 unlock_file_mutex (struct client_s *client, int remove)
109 gpg_error_t rc = 0;
111 // OPEN: keep the lock for the same file being reopened.
112 if (client->flags & FLAG_KEEP_LOCK && client->flags & FLAG_HAS_LOCK)
113 return 0;
115 if (!(client->flags & FLAG_HAS_LOCK))
116 return GPG_ERR_NOT_LOCKED;
118 rc = cache_unlock_mutex (client->md5file, remove);
119 if (rc)
120 rc = GPG_ERR_INV_STATE;
121 else
122 client->flags &= ~(FLAG_HAS_LOCK | FLAG_LOCK_CMD);
124 return rc;
127 static gpg_error_t
128 lock_file_mutex (struct client_s *client, int add)
130 gpg_error_t rc = 0;
131 int timeout = config_get_integer (client->filename, "cache_timeout");
133 if (client->flags & FLAG_HAS_LOCK)
134 return 0;
136 rc = cache_lock_mutex (client->ctx, client->md5file,
137 client->lock_timeout, add, timeout);
138 if (!rc)
139 client->flags |= FLAG_HAS_LOCK;
141 return rc;
144 static gpg_error_t
145 file_modified (struct client_s *client, struct command_table_s *cmd)
147 gpg_error_t rc = 0;
149 if (!(client->flags & FLAG_OPEN))
150 return GPG_ERR_INV_STATE;
152 rc = lock_file_mutex (client, 0);
153 if (!rc || rc == GPG_ERR_NO_DATA)
155 rc = validate_checksum (client, NULL);
156 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
157 rc = 0;
158 else if (rc)
159 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
162 if ((cmd->unlock && !(client->flags & FLAG_HAS_LOCK)) || rc)
163 unlock_file_mutex (client, 0);
165 return rc;
168 static gpg_error_t
169 parse_xml (assuan_context_t ctx, int new)
171 struct client_s *client = assuan_get_pointer (ctx);
172 int cached = client->doc != NULL;
173 gpg_error_t rc = 0;
175 if (new)
177 client->doc = new_document ();
178 if (client->doc)
180 xmlChar *result;
181 int len;
183 xmlDocDumpFormatMemory (client->doc, &result, &len, 0);
184 client->crypto->plaintext = result;
185 client->crypto->plaintext_len = len;
186 if (!client->crypto->plaintext)
188 xmlFreeDoc (client->doc);
189 client->doc = NULL;
193 else if (!cached)
194 rc = parse_doc ((char *) client->crypto->plaintext,
195 client->crypto->plaintext_len, (xmlDocPtr *)&client->doc);
197 return rc;
200 static void
201 free_client (struct client_s *client)
203 if (client->doc)
204 xmlFreeDoc (client->doc);
206 xfree (client->crc);
207 xfree (client->filename);
208 xfree (client->last_error);
210 if (client->crypto)
212 cleanup_crypto_stage2 (client->crypto);
213 if (client->crypto->pkey_sexp)
214 gcry_sexp_release (client->crypto->pkey_sexp);
216 client->crypto->pkey_sexp = NULL;
217 memset (client->crypto->sign_grip, 0,
218 sizeof (client->crypto->sign_grip));
219 memset (client->crypto->grip, 0, sizeof(client->crypto->grip));
223 void
224 cleanup_client (struct client_s *client)
226 assuan_context_t ctx = client->ctx;
227 struct client_thread_s *thd = client->thd;
228 struct crypto_s *crypto = client->crypto;
229 long lock_timeout = client->lock_timeout;
230 int no_pinentry = (client->flags & FLAG_NO_PINENTRY);
231 struct pinentry_option_s pin_opts;
232 xmlErrorPtr xml_error = client->xml_error;
233 #ifdef WITH_AGENT
234 struct pinentry_option_s agent_pin_opts;
236 if (crypto && crypto->agent)
237 memcpy (&agent_pin_opts, &crypto->agent->pinentry_opts,
238 sizeof(struct pinentry_option_s));
239 #endif
241 memcpy (&pin_opts, &client->pinentry_opts, sizeof(struct pinentry_option_s));
242 unlock_file_mutex (client, client->flags & FLAG_NEW);
243 free_client (client);
244 memset (client, 0, sizeof (struct client_s));
245 client->xml_error = xml_error;
246 client->crypto = crypto;
247 client->ctx = ctx;
248 client->thd = thd;
249 client->lock_timeout = lock_timeout;
250 memcpy (&client->pinentry_opts, &pin_opts, sizeof(struct pinentry_option_s));
251 client->flags |= no_pinentry ? FLAG_NO_PINENTRY : 0;
252 #ifdef WITH_AGENT
253 if (crypto && crypto->agent)
254 memcpy (&crypto->agent->pinentry_opts, &agent_pin_opts,
255 sizeof(struct pinentry_option_s));
256 #endif
259 static gpg_error_t
260 open_finalize (assuan_context_t ctx, char *key, size_t keylen)
262 struct client_s *client = assuan_get_pointer (ctx);
263 gpg_error_t rc = 0;
264 struct cache_data_s *cdata = cache_get_data (client->md5file);
265 int cached = 0, keyarg = key ? 1 : 0;
266 size_t keysize;
267 unsigned char *salted_key = NULL;
268 int algo;
269 int pin_try = 1;
270 int pin_tries = 3;
271 char *pin_title = client->pinentry_opts.title;
273 client->crypto->filename = str_dup (client->filename);
274 if (cdata || client->flags & FLAG_NEW)
276 if (cdata && !(client->flags & FLAG_NEW))
278 rc = decrypt_cache (client->crypto, cdata->doc, cdata->doclen);
279 if (rc)
280 return rc;
283 cached = cdata != NULL;
284 goto done;
287 if (!key && !IS_PKI (client->crypto)
288 && !(client->crypto->hdr.flags & PWMD_FLAG_NO_PASSPHRASE))
290 if (client->flags & FLAG_NO_PINENTRY)
292 rc = inquire_passphrase (ctx, "PASSPHRASE", (unsigned char **)&key,
293 &keylen);
294 if (rc)
295 return rc;
297 else
299 client->pinentry_opts.timeout = config_get_integer (client->filename,
300 "pinentry_timeout");
301 pin_again:
302 rc = getpin_common (client->ctx, client->filename, PINENTRY_OPEN,
303 &key, &keylen);
304 if (rc)
305 return rc;
309 if (!IS_PKI (client->crypto))
311 if (client->crypto->hdr.flags & PWMD_FLAG_NO_PASSPHRASE)
313 keylen = 1;
314 key = gcry_malloc (keylen);
315 memset (key, 0, keylen);
318 algo = cipher_to_gcrypt (client->crypto->hdr.flags);
319 rc = hash_key (algo, client->crypto->hdr.salt,
320 sizeof(client->crypto->hdr.salt), key, keylen,
321 (void **)&salted_key, &keysize);
322 if (!keyarg)
323 gcry_free (key);
325 if (!rc)
327 rc = decrypt_data (client->ctx, client->crypto, salted_key, keysize);
328 if (gpg_err_code (rc) == GPG_ERR_BAD_PASSPHRASE
329 && ++pin_try <= pin_tries && !(client->flags & FLAG_NO_PINENTRY))
331 gcry_free (salted_key);
332 salted_key = NULL;
333 key = NULL;
334 keylen = 0;
335 client->pinentry_opts.title = str_asprintf (_ ("Bad passphrase (try %i of %i)"), pin_try, pin_tries);
336 goto pin_again;
340 if (client->pinentry_opts.title != pin_title)
341 xfree (client->pinentry_opts.title);
343 client->pinentry_opts.title = pin_title;
344 if (rc)
346 gcry_free (salted_key);
347 return rc;
350 cdata = xcalloc (1, sizeof (struct cache_data_s));
351 if (!cdata)
353 gcry_free (salted_key);
354 return GPG_ERR_ENOMEM;
357 cdata->key = salted_key;
358 cdata->keylen = keysize;
360 else
361 rc = decrypt_data (client->ctx, client->crypto, NULL, 0);
363 done:
364 if (!rc)
366 rc = parse_xml (ctx, client->flags & FLAG_NEW);
367 if (rc && !cached)
368 free_cache_data_once (cdata);
370 if (!rc)
372 int timeout = config_get_integer (client->filename,
373 "cache_timeout");
375 if (!cached)
377 if (!cdata)
378 cdata = xcalloc (1, sizeof (struct cache_data_s));
380 rc = encrypt_xml (NULL, cache_key, cache_keysize,
381 GCRY_CIPHER_AES, client->crypto->plaintext,
382 client->crypto->plaintext_len, &cdata->doc,
383 &cdata->doclen, &cache_iv, &cache_blocksize,
385 if (!rc && !(client->flags & FLAG_NEW) && IS_PKI (client->crypto))
387 rc = gcry_sexp_build ((gcry_sexp_t *) & cdata->pubkey, NULL,
388 "%S", client->crypto->pkey_sexp);
392 if (cdata->sigkey)
393 gcry_sexp_release (cdata->sigkey);
395 cdata->sigkey = NULL;
396 if (!rc && IS_PKI (client->crypto))
397 rc = gcry_sexp_build ((gcry_sexp_t *) &cdata->sigkey, NULL,
398 "%S", client->crypto->sigpkey_sexp);
400 if (!rc && !cache_add_file (client->md5file,
401 (client->flags & FLAG_NEW)
402 ? NULL
403 : client->crypto->grip, cdata, timeout))
405 if (!cached)
407 free_cache_data_once (cdata);
408 xmlFreeDoc (client->doc);
409 client->doc = NULL;
411 rc = GPG_ERR_ENOMEM;
414 if (!rc)
415 client->flags |= FLAG_OPEN;
419 if (!rc)
420 update_checksum (client);
422 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
423 rc = do_lock (client, 0);
425 return rc;
428 static void
429 req_cleanup (void *arg)
431 if (!arg)
432 return;
434 strv_free ((char **) arg);
437 static gpg_error_t
438 parse_open_opt_lock (void *data, void *value)
440 struct client_s *client = data;
442 client->opts |= OPT_LOCK_ON_OPEN;
443 return 0;
446 static gpg_error_t
447 parse_save_opt_inquire (void *data, void *value)
449 struct client_s *client = data;
450 gpg_error_t rc;
452 if (!(client->flags & FLAG_NEW))
454 rc = peer_is_invoker (client);
455 if (rc == GPG_ERR_EACCES)
456 return rc;
459 (void) value;
460 client->opts |= OPT_INQUIRE;
461 return 0;
464 static gpg_error_t
465 parse_opt_inquire (void *data, void *value)
467 struct client_s *client = data;
469 (void) value;
470 client->opts |= OPT_INQUIRE;
471 return 0;
474 static gpg_error_t
475 update_checksum (struct client_s *client)
477 unsigned char *crc;
478 size_t len;
479 struct cache_data_s *cdata;
480 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
482 if (rc)
483 return rc;
485 xfree (client->crc);
486 client->crc = crc;
487 cdata = cache_get_data (client->md5file);
488 if (cdata)
490 xfree (cdata->crc);
491 cdata->crc = xmalloc (len);
492 memcpy (cdata->crc, crc, len);
495 return 0;
498 static gpg_error_t
499 validate_checksum (struct client_s *client, struct cache_data_s *cdata)
501 unsigned char *crc;
502 size_t len;
503 gpg_error_t rc;
504 int n = 0;
506 if (cdata && !cdata->crc)
507 return GPG_ERR_CHECKSUM;
509 rc = get_checksum (client->filename, &crc, &len);
510 if (rc)
511 return rc;
513 if (cdata)
514 n = memcmp (cdata->crc, crc, len);
515 else if (client->crc)
516 n = memcmp (client->crc, crc, len);
518 xfree (crc);
519 return n ? GPG_ERR_CHECKSUM : 0;
522 static gpg_error_t
523 do_open (assuan_context_t ctx, const char *filename, const char *password)
525 struct client_s *client = assuan_get_pointer (ctx);
526 struct cache_data_s *cdata;
527 gpg_error_t rc = 0;
528 int done = 0;
530 if (!valid_filename (filename))
531 return GPG_ERR_INV_VALUE;
533 gcry_md_hash_buffer (GCRY_MD_MD5, client->md5file, filename,
534 strlen (filename));
535 client->filename = str_dup (filename);
536 if (!client->filename)
537 return GPG_ERR_ENOMEM;
539 // Cached document?
540 cdata = cache_get_data (client->md5file);
541 if (cdata && cdata->doc)
543 int defer = 0;
545 /* This will check that the key is cached in the agent which needs to
546 * be determined for files that share a keygrip. */
547 if (!rc)
549 rc = cache_iscached (client->filename, &defer);
550 if ((!rc || rc == GPG_ERR_ENOENT) && defer)
551 rc = GPG_ERR_KEY_EXPIRED;
554 if (!rc && !(client->flags & FLAG_NEW))
555 rc = validate_checksum (client, cdata);
557 #ifdef WITH_GNUTLS
558 if (!rc && client->thd->remote
559 && config_get_boolean (client->filename, "tcp_require_key"))
560 rc = GPG_ERR_KEY_EXPIRED;
561 #endif
563 if (!rc && !password)
565 rc = read_data_header (client->filename, &client->crypto->hdr,
566 NULL, NULL);
567 if (!rc)
569 if (IS_PKI (client->crypto))
571 gcry_sexp_build (&client->crypto->pkey_sexp, NULL, "%S",
572 cdata->pubkey);
573 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
574 client->crypto->grip);
575 gcry_sexp_build (&client->crypto->sigpkey_sexp, NULL, "%S",
576 cdata->sigkey);
577 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
578 client->crypto->sign_grip);
581 if (!rc)
583 rc = open_finalize (ctx, NULL, 0);
584 done = 1;
589 /* There was an error accessing the file so clear the cache entry. The
590 * real error will be returned from read_data_file() since the file
591 * may have only disappeared. */
592 if (!done)
594 log_write ("%s: %s", filename, pwmd_strerror (rc));
595 rc = cache_clear (client->md5file);
596 send_status_all (STATUS_CACHE, NULL);
600 if (done || rc)
601 return rc;
603 rc = read_data_file (client->filename, client->crypto);
604 if (rc)
606 if (gpg_err_source (rc) != GPG_ERR_SOURCE_DEFAULT ||
607 gpg_err_code (rc) != GPG_ERR_ENOENT)
609 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
610 return rc;
613 client->flags |= FLAG_NEW;
614 memset (client->crypto->grip, 0, sizeof (client->crypto->grip));
615 memset (client->crypto->sign_grip, 0,
616 sizeof (client->crypto->sign_grip));
617 rc = open_finalize (ctx, NULL, 0);
618 return rc;
621 if (password && IS_PKI (client->crypto))
623 #ifdef WITH_AGENT
624 rc = set_agent_passphrase (client->crypto, password, strlen (password));
625 if (rc)
626 return rc;
627 #endif
630 rc = open_finalize (ctx, (char *)password, password ? strlen (password) : 0);
631 return rc;
634 static gpg_error_t
635 open_command (assuan_context_t ctx, char *line)
637 gpg_error_t rc;
638 struct client_s *client = assuan_get_pointer (ctx);
639 char **req, *filename;
640 unsigned char md5file[16];
641 int same_file = 0;
642 assuan_peercred_t peer;
643 struct argv_s *args[] = {
644 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
645 NULL
648 rc = parse_options (&line, args, client);
649 if (rc)
650 return send_error (ctx, rc);
652 req = str_split (line, " ", 2);
653 if (!req)
654 return send_error (ctx, GPG_ERR_SYNTAX);
656 rc = do_validate_peer (ctx, req[0], &peer);
657 if (rc)
659 strv_free (req);
660 return send_error (ctx, rc);
663 pthread_cleanup_push (req_cleanup, req);
664 filename = req[0];
665 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, filename, strlen (filename));
666 /* This client may have locked a different file with ISCACHED --lock than
667 * the current filename. This will remove that lock. */
668 same_file = !memcmp (md5file, client->md5file, 16) ? 1 : 0;
669 if (client->flags & FLAG_OPEN ||
670 (client->flags & FLAG_HAS_LOCK && !same_file))
672 typeof (client->opts) opts = client->opts;
673 typeof (client->flags) flags = client->flags;
675 if (same_file)
676 client->flags |= FLAG_KEEP_LOCK;
678 cleanup_client (client);
679 client->opts = opts;
680 client->flags |= flags;
681 client->flags &= ~(FLAG_LOCK_CMD);
682 if (!same_file)
683 client->flags &= ~(FLAG_HAS_LOCK | FLAG_NEW);
686 /* Need to lock the mutex here because file_modified() cannot without
687 * knowing the filename. */
688 memcpy (client->md5file, md5file, 16);
689 rc = lock_file_mutex (client, 1);
690 if (rc)
691 client->flags &= ~FLAG_OPEN;
693 if (!rc)
695 char *password = req[1] && *req[1] ? req[1] : NULL;
697 #ifdef WITH_AGENT
698 if (IS_PKI (client->crypto))
699 rc = set_pinentry_mode (client->crypto->agent,
700 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
701 : "ask");
702 #endif
703 if (!rc)
705 rc = do_open (ctx, filename, password);
706 if (rc)
707 cleanup_client (client);
709 cleanup_crypto_stage1 (client->crypto);
713 pthread_cleanup_pop (1);
715 if (!rc && client->flags & FLAG_NEW)
716 rc = send_status (ctx, STATUS_NEWFILE, NULL);
718 #ifdef WITH_AGENT
719 (void) kill_scd (client->crypto->agent);
720 #endif
722 return send_error (ctx, rc);
725 static gpg_error_t
726 parse_opt_no_passphrase (void *data, void *value)
728 struct client_s *client = data;
730 (void) value;
731 client->opts |= OPT_NO_PASSPHRASE;
732 return 0;
735 static gpg_error_t
736 parse_save_opt_no_agent (void *data, void *value)
738 struct client_s *client = data;
740 client->opts |= OPT_NO_AGENT;
741 return 0;
744 static gpg_error_t
745 parse_save_opt_cipher (void *data, void *value)
747 struct client_s *client = data;
748 int algo = cipher_string_to_gcrypt ((char *) value);
749 file_header_t *hdr = &client->crypto->save.hdr;
750 gpg_error_t rc = 0;
752 if (algo == -1)
753 return GPG_ERR_INV_VALUE;
755 if (!(client->flags & FLAG_NEW))
757 rc = peer_is_invoker (client);
758 if (rc == GPG_ERR_EACCES)
760 uint64_t flags = 0;
762 flags &= ((uint16_t) ((uint32_t) (flags & 0xFFFFFFFF)));
763 if (!(client->crypto->hdr.flags & gcrypt_to_cipher (algo)))
764 return rc;
766 rc = 0;
768 else if (rc)
769 return rc;
772 if (!rc)
773 hdr->flags = set_cipher_flag (hdr->flags, algo);
775 return rc;
778 #ifdef WITH_AGENT
779 static gpg_error_t
780 permitted_to_save (struct client_s *client, const unsigned char *grip,
781 size_t size, const char *value)
783 char *hexgrip;
784 gpg_error_t rc = 0;
786 if (!(client->flags & FLAG_NEW))
788 hexgrip = bin2hex (grip, size);
789 rc = !strcmp (hexgrip, (char *)value) ? 0 : GPG_ERR_EACCES;
790 xfree (hexgrip);
791 if (rc)
792 rc = peer_is_invoker (client);
795 return rc;
797 #endif
799 static gpg_error_t
800 parse_save_opt_keygrip (void *data, void *value)
802 #ifdef WITH_AGENT
803 struct client_s *client = data;
804 gpg_error_t rc = permitted_to_save (client, client->crypto->grip,
805 sizeof (client->crypto->grip),
806 value);
808 if (!IS_PKI (client->crypto))
809 return GPG_ERR_INV_ARG;
811 if (rc)
812 return rc;
814 if (client->crypto->save.pkey)
815 gcry_sexp_release (client->crypto->save.pkey);
817 client->crypto->save.pkey = NULL;
818 return get_pubkey (client->crypto, value, &client->crypto->save.pkey);
819 #else
820 return GPG_ERR_INV_ARG;
821 #endif
824 static gpg_error_t
825 parse_save_opt_sign_keygrip (void *data, void *value)
827 #ifdef WITH_AGENT
828 struct client_s *client = data;
829 gpg_error_t rc = permitted_to_save (client, client->crypto->sign_grip,
830 sizeof (client->crypto->sign_grip),
831 value);
833 if (!IS_PKI (client->crypto))
834 return GPG_ERR_INV_ARG;
836 if (rc)
837 return rc;
839 if (client->crypto->save.sigpkey)
840 gcry_sexp_release (client->crypto->save.sigpkey);
842 client->crypto->save.sigpkey = NULL;
843 return get_pubkey (client->crypto, value, &client->crypto->save.sigpkey);
844 #else
845 return GPG_ERR_INV_ARG;
846 #endif
849 static gpg_error_t
850 parse_opt_s2k_count (void *data, void *value)
852 struct client_s *client = data;
853 char *v = value;
855 if (!v || !*v)
856 return GPG_ERR_INV_VALUE;
858 client->crypto->save.s2k_count = strtoul (v, NULL, 10);
859 return 0;
862 static gpg_error_t
863 parse_save_opt_iterations (void *data, void *value)
865 struct client_s *client = data;
866 char *v = value, *p;
867 uint64_t n;
869 if (!v || !*v)
870 return GPG_ERR_INV_VALUE;
872 errno = 0;
873 n = strtoull (v, &p, 10);
874 if (n == UINT64_MAX && errno)
875 return gpg_error_from_errno (errno);
876 else if (p && *p)
877 return GPG_ERR_INV_VALUE;
879 if (!(client->flags & FLAG_NEW))
881 gpg_error_t rc = peer_is_invoker (client);
883 if (rc == GPG_ERR_EACCES)
885 if (client->crypto->hdr.iterations != n)
886 return rc;
888 rc = 0;
890 else if (rc)
891 return rc;
894 client->crypto->save.hdr.iterations = n;
895 return 0;
898 static gpg_error_t
899 save_finalize (assuan_context_t ctx)
901 struct client_s *client = assuan_get_pointer (ctx);
902 gpg_error_t rc = 0;
903 xmlChar *xmlbuf = NULL;
904 int xmlbuflen;
905 void *key = NULL;
906 size_t keylen = 0;
908 xmlDocDumpFormatMemory (client->doc, &xmlbuf, &xmlbuflen, 0);
909 if (!xmlbuf)
910 return GPG_ERR_ENOMEM;
912 pthread_cleanup_push (xmlFree, xmlbuf);
914 if (!use_agent || ((client->flags & FLAG_NEW)
915 && (client->opts & OPT_NO_AGENT))
916 || !(client->crypto->hdr.flags & PWMD_FLAG_PKI))
918 rc = export_common (ctx, client->flags & FLAG_NO_PINENTRY,
919 client->crypto, xmlbuf, xmlbuflen, client->filename,
920 NULL, &key, &keylen, 1, 1,
921 (client->opts & OPT_NO_PASSPHRASE));
923 #ifdef WITH_AGENT
924 else
926 gcry_sexp_t pubkey = client->crypto->save.pkey;
927 gcry_sexp_t sigkey = client->crypto->save.sigpkey;
929 if (!pubkey)
930 pubkey = client->crypto->pkey_sexp;
932 if (!sigkey)
934 sigkey = client->crypto->sigpkey_sexp;
935 if (!sigkey)
937 gcry_sexp_build (&client->crypto->save.sigpkey, 0, "%S", pubkey);
938 sigkey = client->crypto->save.sigpkey;
942 rc = encrypt_data_file (client->ctx, client->crypto, pubkey, sigkey,
943 client->filename, xmlbuf, xmlbuflen);
944 if (pubkey == client->crypto->save.pkey)
946 if (!rc)
948 gcry_sexp_release (client->crypto->pkey_sexp);
949 client->crypto->pkey_sexp = client->crypto->save.pkey;
950 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
951 client->crypto->grip);
953 else
954 gcry_sexp_release (pubkey);
956 client->crypto->save.pkey = NULL;
959 if (!rc)
960 gcry_pk_get_keygrip (sigkey, client->crypto->sign_grip);
962 if (sigkey == client->crypto->save.sigpkey)
964 if (!rc)
966 if (client->crypto->sigpkey_sexp)
967 gcry_sexp_release (client->crypto->sigpkey_sexp);
969 client->crypto->sigpkey_sexp = client->crypto->save.sigpkey;
970 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
971 client->crypto->sign_grip);
973 else
974 gcry_sexp_release (sigkey);
976 client->crypto->save.sigpkey = NULL;
979 #endif
981 if (!rc)
983 int cached;
985 rc = save_common (client->filename, client->crypto, xmlbuf, xmlbuflen,
986 key, keylen, &cached, client->opts & OPT_NO_AGENT);
987 if (rc)
988 gcry_free (key);
990 if (!rc && (!cached || (client->flags & FLAG_NEW)))
991 send_status_all (STATUS_CACHE, NULL);
993 if (!rc)
995 rc = update_checksum (client);
996 client->flags &= ~(FLAG_NEW);
1000 pthread_cleanup_pop (1); // xmlFree
1001 return rc;
1004 static gpg_error_t
1005 parse_opt_reset (void *data, void *value)
1007 struct client_s *client = data;
1009 (void) value;
1010 client->opts |= OPT_RESET;
1011 return 0;
1014 static gpg_error_t
1015 save_command (assuan_context_t ctx, char *line)
1017 struct client_s *client = assuan_get_pointer (ctx);
1018 gpg_error_t rc;
1019 struct stat st;
1020 struct argv_s *args[] = {
1021 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG,
1022 parse_opt_no_passphrase},
1023 &(struct argv_s) {"cipher", OPTION_TYPE_ARG, parse_save_opt_cipher},
1024 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
1025 parse_save_opt_inquire},
1026 &(struct argv_s) {"keygrip", OPTION_TYPE_ARG, parse_save_opt_keygrip},
1027 &(struct argv_s) {"sign-keygrip", OPTION_TYPE_ARG,
1028 parse_save_opt_sign_keygrip},
1029 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
1030 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
1031 &(struct argv_s) {"cipher-iterations", OPTION_TYPE_ARG,
1032 parse_save_opt_iterations},
1033 &(struct argv_s) {"no-agent", OPTION_TYPE_NOARG,
1034 parse_save_opt_no_agent},
1035 NULL
1038 cleanup_save (&client->crypto->save);
1039 memcpy (&client->crypto->save.hdr, &client->crypto->hdr,
1040 sizeof (file_header_t));
1041 client->crypto->save.s2k_count =
1042 config_get_ulong (client->filename, "s2k_count");
1044 if (client->flags & FLAG_NEW)
1045 client->crypto->save.hdr.iterations =
1046 config_get_ulonglong (client->filename, "cipher_iterations");
1048 rc = parse_options (&line, args, client);
1049 if (rc)
1050 return send_error (ctx, rc);
1052 if (!(client->flags & FLAG_NEW))
1053 client->opts &= ~OPT_NO_AGENT;
1054 else if (client->opts & OPT_NO_AGENT)
1056 client->crypto->save.hdr.flags &= ~PWMD_FLAG_PKI;
1057 client->crypto->hdr.flags &= ~PWMD_FLAG_PKI;
1060 if ((client->opts & OPT_NO_PASSPHRASE) && !(client->flags & FLAG_NEW)
1061 && !(client->opts & OPT_INQUIRE))
1062 return send_error (ctx, GPG_ERR_WRONG_KEY_USAGE);
1064 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
1065 return send_error (ctx, gpg_error_from_errno (errno));
1067 if (errno != ENOENT && !S_ISREG (st.st_mode))
1069 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
1070 return send_error (ctx, GPG_ERR_ENOANO);
1073 int defer = 0;
1074 rc = cache_iscached (client->filename, &defer);
1075 if (gpg_err_code (rc) == GPG_ERR_ENOENT && client->flags & FLAG_NEW)
1076 rc = 0;
1077 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
1078 rc = 0;
1080 if ((!rc && defer) || config_get_boolean ("global", "require_save_key"))
1081 client->opts |= OPT_RESET;
1083 if (!rc && (client->opts & OPT_RESET))
1085 rc = cache_clear (client->md5file);
1086 if (rc)
1087 return send_error (ctx, rc);
1089 log_write ("%s: %s", client->filename,
1090 pwmd_strerror (GPG_ERR_KEY_EXPIRED));
1091 send_status_all (STATUS_CACHE, NULL);
1094 if (!rc)
1095 rc = update_element_mtime (client, xmlDocGetRootElement (client->doc));
1097 if (rc)
1098 return send_error (ctx, rc);
1100 #ifdef WITH_AGENT
1101 if (!rc && use_agent && !client->crypto->save.pkey &&
1102 (client->flags & FLAG_NEW || client->opts & OPT_INQUIRE)
1103 && !(client->opts & OPT_NO_AGENT))
1105 rc = set_pinentry_mode (client->crypto->agent,
1106 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
1107 : "ask");
1109 if (!(client->flags & FLAG_NEW))
1111 struct crypto_s *crypto;
1112 char *key = NULL;
1113 size_t keylen = 0;
1115 /* Wanting to generate a new key. Require the key to open the
1116 current file before proceeding reguardless of the
1117 require_save_key configuration parameter. */
1118 rc = cache_clear (client->md5file);
1119 if (!rc)
1121 rc = init_client_crypto (&crypto);
1122 if (!rc)
1123 crypto->client_ctx = client->ctx;
1126 if (!rc)
1127 rc = decrypt_common (client->ctx, client->opts&OPT_INQUIRE, crypto,
1128 client->filename, &key, &keylen);
1130 xfree (key);
1131 cleanup_crypto (&crypto);
1134 if (!rc)
1135 rc = send_status (client->ctx, STATUS_GENKEY, NULL);
1137 if (!rc)
1139 struct inquire_data_s idata = { 0 };
1140 char *params = client->opts & OPT_INQUIRE
1141 ? NULL : default_key_params (client->crypto);
1143 pthread_cleanup_push (xfree, params);
1144 idata.crypto = client->crypto;
1146 if (params)
1148 idata.line = params;
1149 idata.len = strlen (params);
1151 else
1152 idata.preset = 1;
1154 client->crypto->agent->inquire_data = &idata;
1155 client->crypto->agent->inquire_cb = NULL;
1156 rc = generate_key (client->crypto, params,
1157 (client->opts & OPT_NO_PASSPHRASE), 1);
1158 pthread_cleanup_pop (1);
1161 #endif
1163 if (!rc)
1164 rc = save_finalize (ctx);
1166 cleanup_crypto_stage1 (client->crypto);
1167 #ifdef WITH_AGENT
1168 (void) kill_scd (client->crypto->agent);
1169 #endif
1170 return send_error (ctx, rc);
1173 static gpg_error_t
1174 do_delete (assuan_context_t ctx, char *line)
1176 struct client_s *client = assuan_get_pointer (ctx);
1177 gpg_error_t rc;
1178 char **req;
1179 xmlNodePtr n;
1181 if (strchr (line, '\t'))
1182 req = str_split (line, "\t", 0);
1183 else
1184 req = str_split (line, " ", 0);
1186 if (!req || !*req)
1187 return GPG_ERR_SYNTAX;
1189 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1190 if (!n)
1192 strv_free (req);
1193 return rc;
1197 * No sub-node defined. Remove the entire node (root element).
1199 if (!req[1])
1201 if (n)
1203 rc = is_element_owner (client, n);
1204 if (!rc)
1206 rc = unlink_node (client, n);
1207 xmlFreeNode (n);
1211 strv_free (req);
1212 return rc;
1215 n = find_elements (client, client->doc, n->children, req + 1, &rc, NULL,
1216 NULL, NULL, 0, 0, NULL, 0);
1217 strv_free (req);
1218 if (!n)
1219 return rc;
1221 rc = is_element_owner (client, n);
1222 if (!rc)
1224 rc = unlink_node (client, n);
1225 xmlFreeNode (n);
1228 return rc;
1231 static gpg_error_t
1232 delete_command (assuan_context_t ctx, char *line)
1234 struct client_s *client = assuan_get_pointer (ctx);
1235 gpg_error_t rc;
1236 struct argv_s *args[] = {
1237 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1238 NULL
1241 rc = parse_options (&line, args, client);
1242 if (rc)
1243 return send_error (ctx, rc);
1245 if (client->opts & OPT_INQUIRE)
1247 unsigned char *result;
1248 size_t len;
1250 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1251 if (rc)
1252 return send_error (ctx, rc);
1254 line = (char *) result;
1257 rc = do_delete (ctx, line);
1259 if (client->opts & OPT_INQUIRE)
1260 xfree (line);
1262 return send_error (ctx, rc);
1265 static gpg_error_t
1266 store_command (assuan_context_t ctx, char *line)
1268 struct client_s *client = assuan_get_pointer (ctx);
1269 gpg_error_t rc;
1270 size_t len;
1271 unsigned char *result;
1272 char **req;
1273 xmlNodePtr n, parent;
1274 int has_content;
1275 char *content = NULL;
1277 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1278 if (rc)
1279 return send_error (ctx, rc);
1281 req = str_split ((char *) result, "\t", 0);
1282 xfree (result);
1284 if (!req || !*req)
1285 return send_error (ctx, GPG_ERR_SYNTAX);
1287 len = strv_length (req);
1288 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1289 if (*(req + 1) && !valid_element_path (req, has_content))
1291 strv_free (req);
1292 return send_error (ctx, GPG_ERR_INV_VALUE);
1295 if (has_content || !*req[len - 1])
1297 has_content = 1;
1298 content = req[len - 1];
1299 req[len - 1] = NULL;
1302 again:
1303 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1304 if (rc && rc == GPG_ERR_ELEMENT_NOT_FOUND)
1306 rc = new_root_element (client, client->doc, *req);
1307 if (rc)
1309 strv_free (req);
1310 return send_error (ctx, rc);
1313 goto again;
1316 if (!n)
1318 strv_free (req);
1319 return send_error (ctx, rc);
1322 parent = n;
1324 if (req[1] && *req[1])
1326 if (!n->children)
1327 parent = create_elements_cb (client, 1, n, req + 1, &rc, NULL);
1328 else
1329 parent = find_elements (client, client->doc, n->children, req + 1, &rc,
1330 NULL, NULL, create_elements_cb, 0, 0, NULL,
1334 if (!rc && len > 1)
1336 rc = is_element_owner (client, parent);
1337 if (!rc)
1339 n = find_text_node (parent->children);
1340 if (n)
1341 xmlNodeSetContent (n, (xmlChar *) content);
1342 else
1343 xmlNodeAddContent (parent, (xmlChar *) content);
1345 update_element_mtime (client, parent);
1349 xfree (content);
1350 strv_free (req);
1351 return send_error (ctx, rc);
1354 static gpg_error_t
1355 xfer_data (assuan_context_t ctx, const char *line, int total)
1357 int to_send;
1358 int sent = 0;
1359 gpg_error_t rc;
1360 int progress = config_get_integer ("global", "xfer_progress");
1361 int flush = 0;
1363 progress =
1364 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1365 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1366 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1368 if (rc)
1369 return rc;
1371 again:
1374 if (sent + to_send > total)
1375 to_send = total - sent;
1377 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1378 flush ? 0 : to_send);
1379 if (!rc)
1381 sent += flush ? 0 : to_send;
1383 if ((progress && !(sent % progress) && sent != total) ||
1384 (sent == total && flush))
1385 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1387 if (!flush && !rc && sent == total)
1389 flush = 1;
1390 goto again;
1394 while (!rc && sent < total);
1396 return rc;
1399 static gpg_error_t
1400 do_get (assuan_context_t ctx, char *line)
1402 struct client_s *client = assuan_get_pointer (ctx);
1403 gpg_error_t rc;
1404 char **req;
1405 xmlNodePtr n;
1407 req = str_split (line, "\t", 0);
1409 if (!req || !*req)
1411 strv_free (req);
1412 return GPG_ERR_SYNTAX;
1415 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1416 if (!n)
1418 strv_free (req);
1419 return rc;
1422 if (req[1])
1424 find_elements (client, client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1425 0, 0, NULL, 0);
1427 strv_free (req);
1428 if (rc)
1429 return rc;
1431 if (!n || !n->children)
1432 return GPG_ERR_NO_DATA;
1434 n = find_text_node (n->children);
1435 if (!n || !n->content || !*n->content)
1436 return GPG_ERR_NO_DATA;
1438 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1439 return rc;
1442 static gpg_error_t
1443 get_command (assuan_context_t ctx, char *line)
1445 struct client_s *client = assuan_get_pointer (ctx);
1446 gpg_error_t rc;
1447 struct argv_s *args[] = {
1448 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1449 NULL
1452 rc = parse_options (&line, args, client);
1453 if (rc)
1454 return send_error (ctx, rc);
1456 if (client->opts & OPT_INQUIRE)
1458 unsigned char *result;
1459 size_t len;
1461 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1462 if (rc)
1463 return send_error (ctx, rc);
1465 line = (char *) result;
1468 rc = do_get (ctx, line);
1470 if (client->opts & OPT_INQUIRE)
1471 xfree (line);
1473 return send_error (ctx, rc);
1476 static void list_command_cleanup1 (void *arg);
1477 static gpg_error_t
1478 realpath_command (assuan_context_t ctx, char *line)
1480 struct string_s *string = NULL;
1481 gpg_error_t rc;
1482 struct client_s *client = assuan_get_pointer (ctx);
1483 struct argv_s *args[] = {
1484 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1485 NULL
1488 rc = parse_options (&line, args, client);
1489 if (rc)
1490 return send_error (ctx, rc);
1492 if (client->opts & OPT_INQUIRE)
1494 unsigned char *result;
1495 size_t len;
1497 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1498 if (rc)
1499 return send_error (ctx, rc);
1501 line = (char *) result;
1504 rc = build_realpath (client, client->doc, line, &string);
1505 if (!rc)
1507 pthread_cleanup_push (list_command_cleanup1, string);
1508 rc = xfer_data (ctx, string->str, string->len);
1509 pthread_cleanup_pop (1);
1512 if (client->opts & OPT_INQUIRE)
1513 xfree (line);
1515 return send_error (ctx, rc);
1518 static void
1519 list_command_cleanup1 (void *arg)
1521 if (arg)
1522 string_free ((struct string_s *) arg, 1);
1525 static void
1526 list_command_cleanup2 (void *arg)
1528 struct element_list_s *elements = arg;
1530 if (elements)
1532 if (elements->list)
1534 int total = slist_length (elements->list);
1535 int i;
1537 for (i = 0; i < total; i++)
1539 char *tmp = slist_nth_data (elements->list, i);
1540 xfree (tmp);
1543 slist_free (elements->list);
1546 if (elements->prefix)
1547 xfree (elements->prefix);
1549 if (elements->req)
1550 strv_free (elements->req);
1552 xfree (elements);
1556 static gpg_error_t
1557 parse_list_opt_norecurse (void *data, void *value)
1559 struct client_s *client = data;
1561 client->opts &= ~(OPT_LIST_RECURSE);
1562 return 0;
1565 static gpg_error_t
1566 parse_list_opt_verbose (void *data, void *value)
1568 struct client_s *client = data;
1570 client->opts |= OPT_LIST_VERBOSE;
1571 return 0;
1574 static gpg_error_t
1575 parse_list_opt_target (void *data, void *value)
1577 struct client_s *client = data;
1579 client->opts |= OPT_LIST_WITH_TARGET;
1580 return 0;
1583 static gpg_error_t
1584 parse_list_opt_all (void *data, void *value)
1586 struct client_s *client = data;
1588 client->opts |= OPT_LIST_ALL;
1589 return 0;
1592 static gpg_error_t
1593 list_path_once (struct client_s *client, char *line,
1594 struct element_list_s *elements, struct string_s *result)
1596 gpg_error_t rc;
1598 elements->req = str_split (line, " ", 0);
1599 if (!elements->req)
1600 strv_printf (&elements->req, "%s", line);
1602 rc = create_path_list (client, client->doc, elements, *elements->req);
1603 if ((rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES) && elements->verbose)
1604 rc = 0;
1606 if (!rc)
1608 int total = slist_length (elements->list);
1610 if (!total)
1611 rc = GPG_ERR_NO_DATA;
1613 if (!rc)
1615 if (!rc)
1617 int i;
1619 for (i = 0; i < total; i++)
1621 char *tmp = slist_nth_data (elements->list, i);
1623 string_append_printf (result, "%s%s", tmp,
1624 i + 1 == total ? "" : "\n");
1628 else
1629 rc = GPG_ERR_NO_DATA;
1632 return rc;
1635 static int
1636 has_list_flag (char *path, char *flags)
1638 char *p = path;
1640 while (*p && *++p != ' ');
1642 if (!*p)
1643 return 0;
1645 for (; *p; p++)
1647 char *f;
1649 for (f = flags; *f && *f != ' '; f++)
1651 if (*p == *f)
1652 return 1;
1656 return 0;
1659 static gpg_error_t
1660 do_list (assuan_context_t ctx, char *line)
1662 struct client_s *client = assuan_get_pointer (ctx);
1663 gpg_error_t rc;
1664 struct element_list_s *elements = NULL;
1666 elements = xcalloc (1, sizeof (struct element_list_s));
1667 if (!elements)
1668 return GPG_ERR_ENOMEM;
1670 elements->recurse = client->opts & OPT_LIST_RECURSE;
1671 elements->verbose =
1672 (client->opts & OPT_LIST_VERBOSE) | (client->opts & OPT_LIST_WITH_TARGET);
1673 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1675 if (!line || !*line)
1677 struct string_s *str = NULL;
1679 pthread_cleanup_push (list_command_cleanup2, elements);
1680 rc = list_root_elements (client, client->doc, &str, elements->verbose,
1681 elements->with_target);
1682 pthread_cleanup_pop (1);
1683 pthread_cleanup_push (list_command_cleanup1, str);
1685 if (!rc)
1687 if (client->opts & OPT_LIST_ALL)
1689 char **roots = str_split (str->str, "\n", 0);
1690 char **p;
1692 pthread_cleanup_push (req_cleanup, roots);
1693 string_truncate (str, 0);
1695 for (p = roots; *p; p++)
1697 if (strchr (*p, ' '))
1699 if (has_list_flag (*p, "EOP"))
1701 string_append_printf (str, "%s%s", *p,
1702 *(p + 1) ? "\n" : "");
1703 continue;
1707 elements = xcalloc (1, sizeof (struct element_list_s));
1708 if (!elements)
1710 rc = GPG_ERR_ENOMEM;
1711 break;
1714 elements->recurse = client->opts & OPT_LIST_RECURSE;
1715 elements->verbose =
1716 (client->opts & OPT_LIST_VERBOSE) | (client->opts &
1717 OPT_LIST_WITH_TARGET);
1718 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1719 pthread_cleanup_push (list_command_cleanup2, elements);
1720 rc = list_path_once (client, *p, elements, str);
1721 pthread_cleanup_pop (1);
1722 if (rc)
1723 break;
1725 if (*(p + 1))
1726 string_append (str, "\n");
1729 pthread_cleanup_pop (1);
1732 if (!rc)
1733 rc = xfer_data (ctx, str->str, str->len);
1736 pthread_cleanup_pop (1);
1737 return rc;
1740 pthread_cleanup_push (list_command_cleanup2, elements);
1741 struct string_s *str = string_new (NULL);
1742 pthread_cleanup_push (list_command_cleanup1, str);
1743 rc = list_path_once (client, line, elements, str);
1744 if (!rc)
1745 rc = xfer_data (ctx, str->str, str->len);
1747 pthread_cleanup_pop (1);
1748 pthread_cleanup_pop (1);
1749 return rc;
1752 static gpg_error_t
1753 list_command (assuan_context_t ctx, char *line)
1755 struct client_s *client = assuan_get_pointer (ctx);
1756 gpg_error_t rc;
1757 struct argv_s *args[] = {
1758 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG,
1759 parse_list_opt_norecurse},
1760 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_list_opt_verbose},
1761 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1762 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG,
1763 parse_list_opt_target},
1764 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_all},
1765 NULL
1768 if (disable_list_and_dump == 1)
1769 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1771 client->opts |= OPT_LIST_RECURSE;
1772 rc = parse_options (&line, args, client);
1773 if (rc)
1774 return send_error (ctx, rc);
1776 if (client->opts & OPT_LIST_ALL)
1777 client->opts |= OPT_LIST_RECURSE | OPT_LIST_VERBOSE;
1779 if (client->opts & OPT_INQUIRE)
1781 unsigned char *result;
1782 size_t len;
1784 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1785 if (rc)
1786 return send_error (ctx, rc);
1788 line = (char *) result;
1791 rc = do_list (ctx, line);
1793 if (client->opts & OPT_INQUIRE)
1794 xfree (line);
1796 return send_error (ctx, rc);
1800 * req[0] - element path
1802 static gpg_error_t
1803 attribute_list (assuan_context_t ctx, char **req)
1805 struct client_s *client = assuan_get_pointer (ctx);
1806 char **attrlist = NULL;
1807 int i = 0;
1808 char **path = NULL;
1809 xmlAttrPtr a;
1810 xmlNodePtr n, an;
1811 char *line;
1812 gpg_error_t rc;
1814 if (!req || !req[0])
1815 return GPG_ERR_SYNTAX;
1817 if ((path = str_split (req[0], "\t", 0)) == NULL)
1820 * The first argument may be only a root element.
1822 if ((path = str_split (req[0], " ", 0)) == NULL)
1823 return GPG_ERR_SYNTAX;
1826 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1828 if (!n)
1830 strv_free (path);
1831 return rc;
1834 if (path[1])
1836 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1837 NULL, NULL, NULL, 0, 0, NULL, 0);
1839 if (!n)
1841 strv_free (path);
1842 return rc;
1846 strv_free (path);
1848 for (a = n->properties; a; a = a->next)
1850 char **pa;
1852 if ((pa = xrealloc (attrlist, (i + 2) * sizeof (char *))) == NULL)
1854 if (attrlist)
1855 strv_free (attrlist);
1857 log_write ("%s(%i): %s", __FILE__, __LINE__,
1858 pwmd_strerror (GPG_ERR_ENOMEM));
1859 return GPG_ERR_ENOMEM;
1862 attrlist = pa;
1863 an = a->children;
1864 attrlist[i] = str_asprintf ("%s %s", (char *) a->name,
1866 && an->content ? (char *) an->content : "");
1868 if (!attrlist[i])
1870 strv_free (attrlist);
1871 log_write ("%s(%i): %s", __FILE__, __LINE__,
1872 pwmd_strerror (GPG_ERR_ENOMEM));
1873 return GPG_ERR_ENOMEM;
1876 attrlist[++i] = NULL;
1879 if (!attrlist)
1880 return GPG_ERR_NO_DATA;
1882 line = strv_join ("\n", attrlist);
1884 if (!line)
1886 log_write ("%s(%i): %s", __FILE__, __LINE__,
1887 pwmd_strerror (GPG_ERR_ENOMEM));
1888 strv_free (attrlist);
1889 return GPG_ERR_ENOMEM;
1892 pthread_cleanup_push (xfree, line);
1893 pthread_cleanup_push (req_cleanup, attrlist);
1894 rc = xfer_data (ctx, line, strlen (line));
1895 pthread_cleanup_pop (1);
1896 pthread_cleanup_pop (1);
1897 return rc;
1901 * req[0] - attribute
1902 * req[1] - element path
1904 static gpg_error_t
1905 attribute_delete (struct client_s *client, char **req)
1907 xmlNodePtr n;
1908 char **path = NULL;
1909 gpg_error_t rc;
1911 if (!req || !req[0] || !req[1])
1912 return GPG_ERR_SYNTAX;
1914 if (!strcmp (req[0], "_name"))
1915 return GPG_ERR_INV_ATTR;
1917 if ((path = str_split (req[1], "\t", 0)) == NULL)
1920 * The first argument may be only a root element.
1922 if ((path = str_split (req[1], " ", 0)) == NULL)
1923 return GPG_ERR_SYNTAX;
1926 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1927 if (!n)
1928 goto fail;
1930 if (path[1])
1932 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1933 NULL, NULL, NULL, 0, 0, NULL, 0);
1934 if (!n)
1935 goto fail;
1938 if (!strcmp (req[0], (char *) "_acl"))
1940 rc = is_element_owner (client, n);
1941 if (rc)
1942 goto fail;
1945 rc = delete_attribute (client, n, (xmlChar *) req[0]);
1947 fail:
1948 strv_free (path);
1949 return rc;
1952 static xmlNodePtr
1953 create_element_path (struct client_s *client,
1954 char ***elements, gpg_error_t * rc, xmlNodePtr parent)
1956 char **req = *elements;
1957 char **req_orig = strv_dup (req);
1958 xmlNodePtr n = NULL;
1960 *rc = 0;
1962 if (!req_orig)
1964 *rc = GPG_ERR_ENOMEM;
1965 log_write ("%s(%i): %s", __FILE__, __LINE__,
1966 pwmd_strerror (GPG_ERR_ENOMEM));
1967 goto fail;
1970 again:
1971 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
1972 if (!n)
1974 if (*rc != GPG_ERR_ELEMENT_NOT_FOUND)
1975 goto fail;
1977 *rc = new_root_element (client, client->doc, req[0]);
1978 if (*rc)
1979 goto fail;
1981 goto again;
1983 else if (n == parent)
1985 *rc = GPG_ERR_CONFLICT;
1986 goto fail;
1989 if (req[1])
1991 if (!n->children)
1992 n = create_target_elements_cb (client, 1, n, req + 1, rc, NULL);
1993 else
1994 n = find_elements (client, client->doc, n->children, req + 1, rc,
1995 NULL, NULL, create_target_elements_cb, 0, 0,
1996 parent, 0);
1998 if (!n)
1999 goto fail;
2002 * Reset the position of the element tree now that the elements
2003 * have been created.
2005 strv_free (req);
2006 req = req_orig;
2007 req_orig = NULL;
2008 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
2009 if (!n)
2010 goto fail;
2012 n = find_elements (client, client->doc, n->children, req + 1, rc,
2013 NULL, NULL, NULL, 0, 0, NULL, 0);
2014 if (!n)
2015 goto fail;
2018 fail:
2019 if (req_orig)
2020 strv_free (req_orig);
2022 *elements = req;
2023 return n;
2027 * Creates a "target" attribute. When other commands encounter an element with
2028 * this attribute, the element path is modified to the target value. If the
2029 * source element path doesn't exist when using 'ATTR SET target', it is
2030 * created, but the destination element path must exist.
2032 * req[0] - source element path
2033 * req[1] - destination element path
2035 static gpg_error_t
2036 target_attribute (struct client_s *client, char **req)
2038 char **src, **dst, *line = NULL, **odst = NULL;
2039 gpg_error_t rc;
2040 xmlNodePtr n;
2042 if (!req || !req[0] || !req[1])
2043 return GPG_ERR_SYNTAX;
2045 if ((src = str_split (req[0], "\t", 0)) == NULL)
2048 * The first argument may be only a root element.
2050 if ((src = str_split (req[0], " ", 0)) == NULL)
2051 return GPG_ERR_SYNTAX;
2054 if (!valid_element_path (src, 0))
2055 return GPG_ERR_INV_VALUE;
2057 if ((dst = str_split (req[1], "\t", 0)) == NULL)
2060 * The first argument may be only a root element.
2062 if ((dst = str_split (req[1], " ", 0)) == NULL)
2064 rc = GPG_ERR_SYNTAX;
2065 goto fail;
2069 odst = strv_dup (dst);
2070 if (!odst)
2072 rc = GPG_ERR_ENOMEM;
2073 goto fail;
2076 n = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
2078 * Make sure the destination element path exists.
2080 if (!n)
2081 goto fail;
2083 if (dst[1])
2085 n = find_elements (client, client->doc, n->children, dst + 1, &rc,
2086 NULL, NULL, NULL, 0, 0, NULL, 0);
2087 if (!n)
2088 goto fail;
2091 rc = validate_target_attribute (client, client->doc, req[0], n);
2092 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2093 goto fail;
2095 n = create_element_path (client, &src, &rc, NULL);
2096 if (rc)
2097 goto fail;
2099 line = strv_join ("\t", odst);
2100 if (!line)
2102 rc = GPG_ERR_ENOMEM;
2103 goto fail;
2106 rc = add_attribute (client, n, "target", line);
2108 fail:
2109 xfree (line);
2110 strv_free (src);
2111 strv_free (dst);
2112 strv_free (odst);
2113 return rc;
2117 * req[0] - attribute
2118 * req[1] - element path
2120 static gpg_error_t
2121 attribute_get (assuan_context_t ctx, char **req)
2123 struct client_s *client = assuan_get_pointer (ctx);
2124 xmlNodePtr n;
2125 xmlChar *a;
2126 char **path = NULL;
2127 gpg_error_t rc;
2129 if (!req || !req[0] || !req[1])
2130 return GPG_ERR_SYNTAX;
2132 if (strchr (req[1], '\t'))
2134 if ((path = str_split (req[1], "\t", 0)) == NULL)
2135 return GPG_ERR_SYNTAX;
2137 else
2139 if ((path = str_split (req[1], " ", 0)) == NULL)
2140 return GPG_ERR_SYNTAX;
2143 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2145 if (!n)
2146 goto fail;
2148 if (path[1])
2150 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2151 NULL, NULL, NULL, 0, 0, NULL, 0);
2153 if (!n)
2154 goto fail;
2157 strv_free (path);
2159 if ((a = xmlGetProp (n, (xmlChar *) req[0])) == NULL)
2160 return GPG_ERR_NOT_FOUND;
2162 pthread_cleanup_push (xmlFree, a);
2164 if (*a)
2165 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
2166 else
2167 rc = GPG_ERR_NO_DATA;
2169 pthread_cleanup_pop (1);
2170 return rc;
2172 fail:
2173 strv_free (path);
2174 return rc;
2178 * req[0] - attribute
2179 * req[1] - element path
2180 * req[2] - value
2182 static gpg_error_t
2183 attribute_set (struct client_s *client, char **req)
2185 char **path = NULL;
2186 gpg_error_t rc;
2187 xmlNodePtr n;
2189 if (!req || !req[0] || !req[1])
2190 return GPG_ERR_SYNTAX;
2193 * Reserved attribute names.
2195 if (!strcmp (req[0], "_name"))
2196 return GPG_ERR_INV_ATTR;
2197 else if (!strcmp (req[0], "target"))
2198 return target_attribute (client, req + 1);
2199 else if (!valid_xml_attribute (req[0]))
2200 return GPG_ERR_INV_VALUE;
2202 if ((path = str_split (req[1], "\t", 0)) == NULL)
2205 * The first argument may be only a root element.
2207 if ((path = str_split (req[1], " ", 0)) == NULL)
2208 return GPG_ERR_SYNTAX;
2211 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2213 if (!n)
2214 goto fail;
2216 if (path[1])
2218 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2219 NULL, NULL, NULL, 0, 0, NULL, 0);
2221 if (!n)
2222 goto fail;
2225 if (!strcmp (req[0], (char *) "_acl"))
2227 rc = is_element_owner (client, n);
2228 if (rc)
2229 goto fail;
2232 rc = add_attribute (client, n, req[0], req[2]);
2234 fail:
2235 strv_free (path);
2236 return rc;
2240 * req[0] - command
2241 * req[1] - attribute name or element path if command is LIST
2242 * req[2] - element path
2243 * req[2] - element path or value
2246 static gpg_error_t
2247 do_attr (assuan_context_t ctx, char *line)
2249 struct client_s *client = assuan_get_pointer (ctx);
2250 gpg_error_t rc = 0;
2251 char **req;
2253 req = str_split (line, " ", 4);
2254 if (!req || !req[0] || !req[1])
2256 strv_free (req);
2257 return GPG_ERR_SYNTAX;
2260 pthread_cleanup_push (req_cleanup, req);
2262 if (strcasecmp (req[0], "SET") == 0)
2263 rc = attribute_set (client, req + 1);
2264 else if (strcasecmp (req[0], "GET") == 0)
2265 rc = attribute_get (ctx, req + 1);
2266 else if (strcasecmp (req[0], "DELETE") == 0)
2267 rc = attribute_delete (client, req + 1);
2268 else if (strcasecmp (req[0], "LIST") == 0)
2269 rc = attribute_list (ctx, req + 1);
2270 else
2271 rc = GPG_ERR_SYNTAX;
2273 pthread_cleanup_pop (1);
2274 return rc;
2277 static gpg_error_t
2278 attr_command (assuan_context_t ctx, char *line)
2280 struct client_s *client = assuan_get_pointer (ctx);
2281 gpg_error_t rc;
2282 struct argv_s *args[] = {
2283 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2284 NULL
2287 rc = parse_options (&line, args, client);
2288 if (rc)
2289 return send_error (ctx, rc);
2291 if (client->opts & OPT_INQUIRE)
2293 unsigned char *result;
2294 size_t len;
2296 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2297 if (rc)
2298 return send_error (ctx, rc);
2300 line = (char *) result;
2303 rc = do_attr (ctx, line);
2305 if (client->opts & OPT_INQUIRE)
2306 xfree (line);
2308 return send_error (ctx, rc);
2311 static gpg_error_t
2312 parse_iscached_opt_lock (void *data, void *value)
2314 struct client_s *client = data;
2316 (void) value;
2317 client->opts |= OPT_LOCK;
2318 return 0;
2321 static gpg_error_t
2322 iscached_command (assuan_context_t ctx, char *line)
2324 struct client_s *client = assuan_get_pointer (ctx);
2325 gpg_error_t rc;
2326 struct argv_s *args[] = {
2327 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2328 NULL
2331 if (!line || !*line)
2332 return send_error (ctx, GPG_ERR_SYNTAX);
2334 rc = parse_options (&line, args, client);
2335 if (rc)
2336 return send_error (ctx, rc);
2337 else if (!valid_filename (line))
2338 return send_error (ctx, GPG_ERR_INV_VALUE);
2340 rc = cache_iscached (line, NULL);
2341 if (client->opts & OPT_LOCK
2342 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2344 unsigned char md5file[16];
2345 gpg_error_t trc = rc;
2347 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2348 if (memcmp (md5file, client->md5file, 16))
2349 cleanup_client (client);
2351 memcpy (client->md5file, md5file, 16);
2352 rc = do_lock (client, 1);
2353 if (!rc)
2354 rc = trc;
2357 return send_error (ctx, rc);
2360 static gpg_error_t
2361 clearcache_command (assuan_context_t ctx, char *line)
2363 gpg_error_t rc = 0, all_rc = 0;
2364 unsigned char md5file[16];
2365 int i;
2366 int t;
2367 int all = 0;
2368 struct client_thread_s *once = NULL;
2370 cache_lock ();
2371 MUTEX_LOCK (&cn_mutex);
2372 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
2374 if (!line || !*line)
2375 all = 1;
2377 t = slist_length (cn_thread_list);
2379 for (i = 0; i < t; i++)
2381 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2382 assuan_peercred_t peer;
2384 if (!thd->cl)
2385 continue;
2387 /* Lock each connected clients' file mutex to prevent any other client
2388 * from accessing the cache entry (the file mutex is locked upon
2389 * command startup). The cache for the entry is not cleared if the
2390 * file mutex is locked by another client to prevent this function
2391 * from blocking.
2393 if (all)
2395 if (thd->cl->filename)
2397 rc = do_validate_peer (ctx, thd->cl->filename, &peer);
2398 all_rc = !all_rc ? rc : all_rc;
2399 if (rc)
2401 rc = 0;
2402 continue;
2406 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
2407 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2409 if (pthread_equal (pthread_self (), thd->tid))
2410 rc = 0;
2411 else
2413 if (!thd->cl->filename ||
2414 cache_iscached (thd->cl->filename,
2415 NULL) == GPG_ERR_NO_DATA)
2417 rc = 0;
2418 continue;
2421 cache_defer_clear (thd->cl->md5file);
2424 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2426 rc = 0;
2427 continue;
2430 if (!rc)
2432 rc = cache_clear (thd->cl->md5file);
2433 cache_unlock_mutex (thd->cl->md5file, 0);
2436 if (rc)
2437 all_rc = rc;
2439 rc = 0;
2441 /* A single data filename was specified. Lock only this data file
2442 * mutex and free the cache entry. */
2443 else
2445 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2446 rc = do_validate_peer (ctx, line, &peer);
2448 if (!rc && !memcmp (thd->cl->md5file, md5file, sizeof (md5file)))
2450 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0,
2451 -1);
2452 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2454 if (pthread_equal (pthread_self (), thd->tid))
2455 rc = 0;
2458 if (!rc)
2460 once = thd;
2461 rc = cache_clear (thd->cl->md5file);
2462 cache_unlock_mutex (thd->cl->md5file, 0);
2464 else
2466 cache_defer_clear (thd->cl->md5file);
2469 break;
2474 /* Only connected clients' cache entries have been cleared. Now clear any
2475 * remaining cache entries without clients but only if there wasn't an
2476 * error from above since this would defeat the locking check of the
2477 * remaining entries. */
2478 if (!all_rc && all)
2480 cache_clear (NULL);
2483 /* No clients are using the specified file. */
2484 else if (!all_rc && !rc && !once)
2486 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2487 rc = cache_clear (md5file);
2490 /* Release the connection mutex. */
2491 pthread_cleanup_pop (1);
2492 cache_unlock ();
2494 if (!rc)
2495 send_status_all (STATUS_CACHE, NULL);
2497 /* One or more files were locked while clearing all cache entries. */
2498 if (all_rc)
2499 rc = all_rc;
2501 return send_error (ctx, rc);
2504 static gpg_error_t
2505 cachetimeout_command (assuan_context_t ctx, char *line)
2507 int timeout;
2508 char **req = str_split (line, " ", 0);
2509 char *p;
2510 gpg_error_t rc = 0;
2511 assuan_peercred_t peer;
2513 if (!req || !*req || !req[1])
2515 strv_free (req);
2516 return send_error (ctx, GPG_ERR_SYNTAX);
2519 errno = 0;
2520 timeout = (int) strtol (req[1], &p, 10);
2521 if (errno != 0 || *p || timeout < -1)
2523 strv_free (req);
2524 return send_error (ctx, GPG_ERR_SYNTAX);
2527 rc = do_validate_peer (ctx, req[0], &peer);
2528 if (!rc)
2530 unsigned char md5file[16];
2532 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, req[0], strlen (req[0]));
2533 rc = cache_set_timeout (md5file, timeout);
2534 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2536 rc = 0;
2537 MUTEX_LOCK (&rcfile_mutex);
2538 config_set_int_param (&global_config, req[0], "cache_timeout",
2539 req[1]);
2540 MUTEX_UNLOCK (&rcfile_mutex);
2544 strv_free (req);
2545 return send_error (ctx, rc);
2548 static gpg_error_t
2549 dump_command (assuan_context_t ctx, char *line)
2551 xmlChar *xml;
2552 int len;
2553 struct client_s *client = assuan_get_pointer (ctx);
2554 gpg_error_t rc;
2556 if (disable_list_and_dump == 1)
2557 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2559 rc = peer_is_invoker(client);
2560 if (rc)
2561 return send_error (ctx, rc);
2563 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2565 if (!xml)
2567 log_write ("%s(%i): %s", __FILE__, __LINE__,
2568 pwmd_strerror (GPG_ERR_ENOMEM));
2569 return send_error (ctx, GPG_ERR_ENOMEM);
2572 pthread_cleanup_push (xmlFree, xml);
2573 rc = xfer_data (ctx, (char *) xml, len);
2574 pthread_cleanup_pop (1);
2575 return send_error (ctx, rc);
2578 static gpg_error_t
2579 getconfig_command (assuan_context_t ctx, char *line)
2581 struct client_s *client = assuan_get_pointer (ctx);
2582 gpg_error_t rc = 0;
2583 char filename[255] = { 0 }, param[747] = { 0 };
2584 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2586 if (!line || !*line)
2587 return send_error (ctx, GPG_ERR_SYNTAX);
2589 if (strchr (line, ' '))
2591 sscanf (line, " %254[^ ] %746c", filename, param);
2592 paramp = param;
2593 fp = filename;
2596 if (fp && !valid_filename (fp))
2597 return send_error (ctx, GPG_ERR_INV_VALUE);
2599 paramp = str_down (paramp);
2600 if (!strcmp (paramp, "cipher") && fp)
2602 struct crypto_s *crypto = NULL;
2604 rc = init_client_crypto (&crypto);
2605 if (!rc)
2607 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2608 if (!rc)
2610 const char *t =
2611 gcry_cipher_algo_name (cipher_to_gcrypt (crypto->hdr.flags));
2612 if (t)
2614 tmp = str_dup (t);
2615 if (tmp)
2616 str_down (tmp);
2621 UPDATE_AGENT_CTX (client, crypto);
2622 cleanup_crypto (&crypto);
2623 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2624 return send_error (ctx, rc);
2626 if (!rc && tmp)
2627 goto done;
2629 else if (!strcmp (paramp, "cipher_iterations") && fp)
2631 struct crypto_s *crypto = NULL;
2633 rc = init_client_crypto (&crypto);
2634 if (!rc)
2636 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2637 if (!rc)
2639 tmp = str_asprintf ("%llu",
2640 (unsigned long long) crypto->hdr.
2641 iterations);
2642 if (!tmp)
2643 rc = GPG_ERR_ENOMEM;
2647 UPDATE_AGENT_CTX (client, crypto);
2648 cleanup_crypto (&crypto);
2649 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2650 return send_error (ctx, rc);
2652 if (!rc && tmp)
2653 goto done;
2655 else if (!strcmp (paramp, "passphrase"))
2656 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2658 p = config_get_value (fp ? fp : "global", paramp);
2659 if (!p)
2660 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2662 tmp = expand_homedir (p);
2663 xfree (p);
2664 if (!tmp)
2666 log_write ("%s(%i): %s", __FILE__, __LINE__,
2667 pwmd_strerror (GPG_ERR_ENOMEM));
2668 return send_error (ctx, GPG_ERR_ENOMEM);
2671 done:
2672 p = tmp;
2673 pthread_cleanup_push (xfree, p);
2674 rc = xfer_data (ctx, p, strlen (p));
2675 pthread_cleanup_pop (1);
2676 return send_error (ctx, rc);
2679 struct xpath_s
2681 xmlXPathContextPtr xp;
2682 xmlXPathObjectPtr result;
2683 xmlBufferPtr buf;
2684 char **req;
2687 static void
2688 xpath_command_cleanup (void *arg)
2690 struct xpath_s *xpath = arg;
2692 if (!xpath)
2693 return;
2695 req_cleanup (xpath->req);
2697 if (xpath->buf)
2698 xmlBufferFree (xpath->buf);
2700 if (xpath->result)
2701 xmlXPathFreeObject (xpath->result);
2703 if (xpath->xp)
2704 xmlXPathFreeContext (xpath->xp);
2707 static gpg_error_t
2708 do_xpath (assuan_context_t ctx, char *line)
2710 gpg_error_t rc;
2711 struct client_s *client = assuan_get_pointer (ctx);
2712 struct xpath_s _x = { 0 };
2713 struct xpath_s *xpath = &_x;
2715 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2717 if (strv_printf (&xpath->req, "%s", line) == 0)
2718 return GPG_ERR_ENOMEM;
2721 xpath->xp = xmlXPathNewContext (client->doc);
2722 if (!xpath->xp)
2724 rc = GPG_ERR_BAD_DATA;
2725 goto fail;
2728 xpath->result =
2729 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2730 if (!xpath->result)
2732 rc = GPG_ERR_BAD_DATA;
2733 goto fail;
2736 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2738 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2739 goto fail;
2742 rc = recurse_xpath_nodeset (client, client->doc, xpath->result->nodesetval,
2743 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2744 NULL);
2745 if (rc)
2746 goto fail;
2747 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2749 rc = GPG_ERR_NO_DATA;
2750 goto fail;
2752 else if (xpath->req[1])
2754 rc = 0;
2755 goto fail;
2758 pthread_cleanup_push (xpath_command_cleanup, &xpath);
2759 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2760 xmlBufferLength (xpath->buf));
2761 pthread_cleanup_pop (0);
2762 fail:
2763 xpath_command_cleanup (xpath);
2764 return rc;
2767 static gpg_error_t
2768 xpath_command (assuan_context_t ctx, char *line)
2770 struct client_s *client = assuan_get_pointer (ctx);
2771 gpg_error_t rc;
2772 struct argv_s *args[] = {
2773 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2774 NULL
2777 if (disable_list_and_dump == 1)
2778 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2780 rc = peer_is_invoker(client);
2781 if (rc)
2782 return send_error (ctx, rc);
2784 rc = parse_options (&line, args, client);
2785 if (rc)
2786 return send_error (ctx, rc);
2788 if (client->opts & OPT_INQUIRE)
2790 unsigned char *result;
2791 size_t len;
2793 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2794 if (rc)
2795 return send_error (ctx, rc);
2797 line = (char *) result;
2800 if (!line || !*line)
2801 rc = GPG_ERR_SYNTAX;
2803 if (!rc)
2804 rc = do_xpath (ctx, line);
2806 if (client->opts & OPT_INQUIRE)
2807 xfree (line);
2809 return send_error (ctx, rc);
2812 static gpg_error_t
2813 do_xpathattr (assuan_context_t ctx, char *line)
2815 struct client_s *client = assuan_get_pointer (ctx);
2816 gpg_error_t rc;
2817 char **req = NULL;
2818 int cmd = 0; //SET
2819 struct xpath_s _x = { 0 };
2820 struct xpath_s *xpath = &_x;
2822 if ((req = str_split (line, " ", 3)) == NULL)
2823 return GPG_ERR_ENOMEM;
2825 if (!req[0])
2827 rc = GPG_ERR_SYNTAX;
2828 goto fail;
2831 if (!strcasecmp (req[0], "SET"))
2832 cmd = 0;
2833 else if (!strcasecmp (req[0], "DELETE"))
2834 cmd = 1;
2835 else
2837 rc = GPG_ERR_SYNTAX;
2838 goto fail;
2841 if (!req[1] || !req[2])
2843 rc = GPG_ERR_SYNTAX;
2844 goto fail;
2847 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2849 rc = GPG_ERR_ENOMEM;
2850 goto fail;
2853 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2855 rc = GPG_ERR_SYNTAX;
2856 goto fail;
2859 xpath->xp = xmlXPathNewContext (client->doc);
2860 if (!xpath->xp)
2862 rc = GPG_ERR_BAD_DATA;
2863 goto fail;
2866 xpath->result = xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2867 if (!xpath->result)
2869 rc = GPG_ERR_BAD_DATA;
2870 goto fail;
2873 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2875 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2876 goto fail;
2879 rc = recurse_xpath_nodeset (client, client->doc, xpath->result->nodesetval,
2880 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2881 (xmlChar *) req[1]);
2883 fail:
2884 xpath_command_cleanup (xpath);
2885 strv_free (req);
2886 return rc;
2889 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2890 static gpg_error_t
2891 xpathattr_command (assuan_context_t ctx, char *line)
2893 struct client_s *client = assuan_get_pointer (ctx);
2894 gpg_error_t rc;
2895 struct argv_s *args[] = {
2896 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2897 NULL
2900 if (disable_list_and_dump == 1)
2901 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2903 rc = peer_is_invoker(client);
2904 if (rc)
2905 return send_error (ctx, rc);
2907 rc = parse_options (&line, args, client);
2908 if (rc)
2909 return send_error (ctx, rc);
2911 if (client->opts & OPT_INQUIRE)
2913 unsigned char *result;
2914 size_t len;
2916 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2917 if (rc)
2918 return send_error (ctx, rc);
2920 line = (char *) result;
2923 if (!line || !*line)
2924 rc = GPG_ERR_SYNTAX;
2926 if (!rc)
2927 rc = do_xpathattr (ctx, line);
2929 if (client->opts & OPT_INQUIRE)
2930 xfree (line);
2932 return send_error (ctx, rc);
2935 static gpg_error_t
2936 do_import (struct client_s *client, const char *root_element,
2937 unsigned char *content)
2939 char **dst_path = NULL;
2940 xmlDocPtr doc = NULL;
2941 xmlNodePtr n, root, copy;
2942 gpg_error_t rc;
2944 if (!content || !*content)
2946 xfree (content);
2947 return GPG_ERR_SYNTAX;
2950 if (root_element)
2951 dst_path = str_split (root_element, "\t", 0);
2953 if (dst_path && !valid_element_path (dst_path, 0))
2955 if (dst_path)
2956 strv_free (dst_path);
2958 return GPG_ERR_INV_VALUE;
2961 struct string_s *str = string_new_content ((char *)content);
2962 str = string_prepend (str, "<pwmd>");
2963 str = string_append (str, "</pwmd>");
2964 doc = xmlReadDoc ((xmlChar *) str->str, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2965 string_free (str, 1);
2966 if (!doc)
2968 rc = GPG_ERR_BAD_DATA;
2969 goto fail;
2972 root = xmlDocGetRootElement (doc);
2973 xmlNodePtr root_orig = root->children;
2974 root = root->children;
2975 rc = validate_import (client, root);
2976 if (rc)
2977 goto fail;
2981 again:
2982 if (dst_path)
2984 char **path = strv_dup (dst_path);
2985 if (!path)
2987 log_write ("%s(%i): %s", __FILE__, __LINE__,
2988 pwmd_strerror (GPG_ERR_ENOMEM));
2989 rc = GPG_ERR_ENOMEM;
2990 goto fail;
2993 xmlChar *a = xmlGetProp (root, (xmlChar *) "_name");
2994 if (!a)
2996 strv_free (path);
2997 rc = GPG_ERR_INV_VALUE;
2998 goto fail;
3001 if (strv_printf (&path, "%s", (char *) a) == 0)
3003 xmlFree (a);
3004 rc = GPG_ERR_ENOMEM;
3005 goto fail;
3008 xmlFree (a);
3009 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
3010 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3012 strv_free (path);
3013 goto fail;
3016 if (!rc)
3018 n = find_elements (client, client->doc, n->children, path + 1, &rc,
3019 NULL, NULL, NULL, 0, 0, NULL, 1);
3020 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3022 strv_free (path);
3023 goto fail;
3025 else if (!rc)
3027 xmlUnlinkNode (n);
3028 xmlFreeNode (n);
3029 strv_free (path);
3030 path = NULL;
3031 goto again;
3035 if (rc == GPG_ERR_ELEMENT_NOT_FOUND)
3037 n = create_element_path (client, &path, &rc, NULL);
3038 if (rc)
3039 goto fail;
3042 if (root->children)
3044 copy = xmlCopyNodeList (root->children);
3045 n = xmlAddChildList (n, copy);
3046 if (!n)
3047 rc = GPG_ERR_ENOMEM;
3050 strv_free (path);
3052 else
3054 char **path = NULL;
3056 /* Check if the content root element can create a DTD root element. */
3057 if (!xmlStrEqual ((xmlChar *) "element", root->name))
3059 rc = GPG_ERR_SYNTAX;
3060 goto fail;
3063 xmlChar *a;
3065 if ((a = xmlGetProp (root, (xmlChar *) "_name")) == NULL)
3067 rc = GPG_ERR_SYNTAX;
3068 goto fail;
3071 char *tmp = str_dup ((char *) a);
3072 xmlFree (a);
3073 int literal = is_literal_element (&tmp);
3075 if (!valid_xml_element ((xmlChar *) tmp) || literal)
3077 xfree (tmp);
3078 rc = GPG_ERR_INV_VALUE;
3079 goto fail;
3082 if (strv_printf (&path, "%s", tmp) == 0)
3084 xfree (tmp);
3085 rc = GPG_ERR_ENOMEM;
3086 goto fail;
3089 xfree (tmp);
3090 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 1);
3091 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3093 rc = GPG_ERR_BAD_DATA;
3094 goto fail;
3097 /* Overwriting the existing tree. */
3098 if (!rc)
3100 xmlUnlinkNode (n);
3101 xmlFreeNodeList (n);
3104 rc = 0;
3105 xmlSetProp (root, (xmlChar *) "_name", (xmlChar *) path[0]);
3106 n = xmlCopyNode (root, 1);
3107 n = xmlAddChildList (xmlDocGetRootElement (client->doc), n);
3108 strv_free (path);
3111 if (n && !rc)
3112 rc = update_element_mtime (client, n->parent);
3114 for (root = root_orig->next; root; root = root->next)
3116 if (root->type == XML_ELEMENT_NODE)
3117 break;
3120 root_orig = root;
3122 while (root);
3124 fail:
3125 if (doc)
3126 xmlFreeDoc (doc);
3128 if (dst_path)
3129 strv_free (dst_path);
3131 return rc;
3134 static gpg_error_t
3135 parse_import_opt_root (void *data, void *value)
3137 struct client_s *client = data;
3139 client->import_root = str_dup (value);
3140 return client->import_root ? 0 : GPG_ERR_ENOMEM;
3143 static gpg_error_t
3144 import_command (assuan_context_t ctx, char *line)
3146 gpg_error_t rc;
3147 struct client_s *client = assuan_get_pointer (ctx);
3148 unsigned char *result;
3149 size_t len;
3150 struct argv_s *args[] = {
3151 &(struct argv_s) {"root", OPTION_TYPE_ARG, parse_import_opt_root},
3152 NULL
3155 xfree (client->import_root);
3156 client->import_root = NULL;
3157 rc = parse_options (&line, args, client);
3158 if (rc)
3159 return send_error (ctx, rc);
3161 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3162 if (rc)
3164 xfree (client->import_root);
3165 client->import_root = NULL;
3166 return send_error (ctx, rc);
3169 rc = do_import (client, client->import_root, result);
3170 xfree (client->import_root);
3171 client->import_root = NULL;
3172 return send_error (ctx, rc);
3175 static gpg_error_t
3176 do_lock (struct client_s *client, int add)
3178 gpg_error_t rc = lock_file_mutex (client, add);
3180 if (!rc)
3181 client->flags |= FLAG_LOCK_CMD;
3183 return rc;
3186 static gpg_error_t
3187 lock_command (assuan_context_t ctx, char *line)
3189 struct client_s *client = assuan_get_pointer (ctx);
3190 gpg_error_t rc = do_lock (client, 0);
3192 return send_error (ctx, rc);
3195 static gpg_error_t
3196 unlock_command (assuan_context_t ctx, char *line)
3198 struct client_s *client = assuan_get_pointer (ctx);
3199 gpg_error_t rc;
3201 rc = unlock_file_mutex (client, 0);
3202 return send_error (ctx, rc);
3205 static gpg_error_t
3206 option_command (assuan_context_t ctx, char *line)
3208 struct client_s *client = assuan_get_pointer (ctx);
3209 gpg_error_t rc = 0;
3210 struct pinentry_option_s *pin_opts = &client->pinentry_opts;
3211 #ifdef WITH_AGENT
3212 struct agent_s *agent = client->crypto->agent;
3213 #endif
3214 char namebuf[255] = { 0 };
3215 char *name = namebuf;
3216 char *value = NULL, *p;
3218 p = strchr (line, '=');
3219 if (!p)
3220 strncpy (namebuf, line, sizeof(namebuf));
3221 else
3223 strncpy (namebuf, line, strlen (line)-strlen (p));
3224 value = p+1;
3227 log_write1 ("OPTION name='%s' value='%s'", name, value);
3229 if (strcasecmp (name, (char *) "log_level") == 0)
3231 long l = 0;
3233 if (value)
3235 l = strtol (value, NULL, 10);
3237 if (l < 0 || l > 2)
3238 return send_error (ctx, GPG_ERR_INV_VALUE);
3241 MUTEX_LOCK (&rcfile_mutex);
3242 config_set_int_param (&global_config, "global", "log_level", value);
3243 MUTEX_UNLOCK (&rcfile_mutex);
3244 goto done;
3246 else if (strcasecmp (name, (char *) "lock-timeout") == 0)
3248 long n = 0;
3249 char *p = NULL;
3251 if (value)
3253 n = strtol (value, &p, 10);
3254 if (p && *p)
3255 return send_error (ctx, GPG_ERR_INV_VALUE);
3258 client->lock_timeout = n;
3259 goto done;
3261 else if (strcasecmp (name, (char *) "NAME") == 0)
3263 char *tmp = pthread_getspecific (thread_name_key);
3265 if (tmp)
3266 xfree (tmp);
3268 if (!value || !*value)
3270 pthread_setspecific (thread_name_key, str_dup (""));
3271 goto done;
3274 pthread_setspecific (thread_name_key, str_dup (value));
3275 goto done;
3277 else if (strcasecmp (name, (char *) "lc-messages") == 0)
3279 xfree (pin_opts->lc_messages);
3280 pin_opts->lc_messages = NULL;
3281 if (value && *value)
3282 pin_opts->lc_messages = str_dup (value);
3283 #ifdef WITH_AGENT
3284 if (use_agent)
3285 rc = set_agent_option (client->crypto->agent, "lc-messages", value);
3286 #endif
3288 else if (strcasecmp (name, (char *) "lc-ctype") == 0)
3290 xfree (pin_opts->lc_ctype);
3291 pin_opts->lc_ctype = NULL;
3292 if (value && *value)
3293 pin_opts->lc_ctype = str_dup (value);
3294 #ifdef WITH_AGENT
3295 if (use_agent)
3296 rc = set_agent_option (client->crypto->agent, "lc-ctype", value);
3297 #endif
3299 else if (strcasecmp (name, (char *) "ttyname") == 0)
3301 xfree (pin_opts->ttyname);
3302 pin_opts->ttyname = NULL;
3303 if (value && *value)
3304 pin_opts->ttyname = str_dup (value);
3305 #ifdef WITH_AGENT
3306 if (use_agent)
3307 rc = set_agent_option (client->crypto->agent, "ttyname", value);
3308 #endif
3310 else if (strcasecmp (name, (char *) "ttytype") == 0)
3312 xfree (pin_opts->ttytype);
3313 pin_opts->ttytype = NULL;
3314 if (value && *value)
3315 pin_opts->ttytype = str_dup (value);
3316 #ifdef WITH_AGENT
3317 if (use_agent)
3318 rc = set_agent_option (client->crypto->agent, "ttytype", value);
3319 #endif
3321 else if (strcasecmp (name, (char *) "display") == 0)
3323 xfree (pin_opts->display);
3324 pin_opts->display = NULL;
3325 if (value && *value)
3326 pin_opts->display = str_dup (value);
3327 #ifdef WITH_AGENT
3328 if (use_agent)
3329 rc = set_agent_option (client->crypto->agent, "display", value);
3330 #endif
3332 else if (strcasecmp (name, (char *) "pinentry-desc") == 0)
3334 xfree (pin_opts->desc);
3335 pin_opts->desc = NULL;
3336 if (value && *value)
3337 pin_opts->desc = str_dup (value);
3339 else if (strcasecmp (name, (char *) "pinentry-title") == 0)
3341 xfree (pin_opts->title);
3342 pin_opts->title = NULL;
3343 if (value && *value)
3344 pin_opts->title = str_dup (value);
3346 else if (strcasecmp (name, (char *) "pinentry-prompt") == 0)
3348 xfree (pin_opts->prompt);
3349 pin_opts->prompt = NULL;
3350 if (value && *value)
3351 pin_opts->prompt = str_dup (value);
3354 else if (strcasecmp (name, "pinentry-timeout") == 0)
3356 char *p = NULL;
3357 int n;
3359 if (!value)
3360 goto done;
3362 n = (int) strtol (value, &p, 10);
3364 if (*p || n < 0)
3365 return send_error (ctx, GPG_ERR_INV_VALUE);
3367 pin_opts->timeout = n;
3368 MUTEX_LOCK (&rcfile_mutex);
3369 config_set_int_param (&global_config,
3370 client->filename ? client->filename : "global",
3371 "pinentry_timeout", value);
3372 MUTEX_UNLOCK (&rcfile_mutex);
3373 goto done;
3375 else if (strcasecmp (name, "disable-pinentry") == 0)
3377 char *p = NULL;
3378 int n = 1;
3380 if (value && *value)
3382 n = (int) strtol (value, &p, 10);
3383 if (*p || n < 0 || n > 1)
3384 return send_error (ctx, GPG_ERR_INV_VALUE);
3387 if (n)
3388 client->flags |= FLAG_NO_PINENTRY;
3389 else
3390 client->flags &= ~FLAG_NO_PINENTRY;
3392 #ifdef WITH_AGENT
3393 if (use_agent)
3395 if (client->flags & FLAG_NO_PINENTRY)
3396 rc = set_agent_option (client->crypto->agent, "pinentry-mode",
3397 "loopback");
3398 else
3399 rc = set_agent_option (client->crypto->agent, "pinentry-mode",
3400 "ask");
3402 if (rc)
3403 return send_error (ctx, rc);
3405 #endif
3407 else
3408 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
3410 done:
3411 #ifdef WITH_AGENT
3412 if (!rc && use_agent && agent)
3414 rc = pinentry_merge_options (&client->crypto->agent->pinentry_opts,
3415 pin_opts);
3417 #endif
3419 return send_error (ctx, rc);
3422 static gpg_error_t
3423 do_rename (assuan_context_t ctx, char *line)
3425 struct client_s *client = assuan_get_pointer (ctx);
3426 gpg_error_t rc;
3427 char **req, **src, *dst;
3428 xmlNodePtr n, ndst;
3430 req = str_split (line, " ", 0);
3432 if (!req || !req[0] || !req[1])
3434 strv_free (req);
3435 return GPG_ERR_SYNTAX;
3438 dst = req[1];
3439 is_literal_element (&dst);
3441 if (!valid_xml_element ((xmlChar *) dst))
3443 strv_free (req);
3444 return GPG_ERR_INV_VALUE;
3447 if (strchr (req[0], '\t'))
3448 src = str_split (req[0], "\t", 0);
3449 else
3450 src = str_split (req[0], " ", 0);
3452 if (!src || !*src)
3454 rc = GPG_ERR_SYNTAX;
3455 goto fail;
3458 n = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3459 if (src[1] && n)
3460 n = find_elements (client, client->doc, n->children, src + 1, &rc, NULL, NULL,
3461 NULL, 0, 0, NULL, 0);
3463 if (!n)
3464 goto fail;
3466 rc = is_element_owner (client, n);
3467 if (rc)
3468 goto fail;
3470 xmlChar *a = xmlGetProp (n, (xmlChar *) "_name");
3471 if (!a)
3473 rc = GPG_ERR_ENOMEM;
3474 goto fail;
3477 /* To prevent unwanted effects:
3479 * <root name="a"><b/></root>
3481 * RENAME a<TAB>b b
3483 if (xmlStrEqual (a, (xmlChar *) dst))
3485 xmlFree (a);
3486 rc = GPG_ERR_AMBIGUOUS_NAME;
3487 goto fail;
3490 xmlFree (a);
3491 char **tmp = NULL;
3492 if (src[1])
3494 char **p;
3496 for (p = src; *p; p++)
3498 if (!*(p + 1))
3499 break;
3500 strv_printf (&tmp, "%s", *p);
3504 strv_printf (&tmp, "!%s", dst);
3505 ndst = find_root_element (client, client->doc, &tmp, &rc, NULL, 0, 0);
3506 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3508 strv_free (tmp);
3509 goto fail;
3512 if (tmp[1] && ndst)
3513 ndst = find_elements (client, client->doc, ndst->children, tmp + 1, &rc, NULL,
3514 NULL, NULL, 0, 0, NULL, 0);
3516 strv_free (tmp);
3517 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3518 goto fail;
3520 rc = 0;
3522 /* Target may exist:
3524 * <root name="a"/>
3525 * <root name="b" target="a"/>
3527 * RENAME b a
3529 * Would need to do:
3530 * RENAME !b a
3532 if (ndst == n)
3534 rc = GPG_ERR_AMBIGUOUS_NAME;
3535 goto fail;
3538 if (ndst)
3540 rc = is_element_owner (client, ndst);
3541 if (rc)
3542 goto fail;
3544 unlink_node (client, ndst);
3545 xmlFreeNodeList (ndst);
3548 rc = add_attribute (client, n, "_name", dst);
3550 fail:
3551 strv_free (req);
3552 strv_free (src);
3553 return rc;
3556 static gpg_error_t
3557 rename_command (assuan_context_t ctx, char *line)
3559 struct client_s *client = assuan_get_pointer (ctx);
3560 gpg_error_t rc;
3561 struct argv_s *args[] = {
3562 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3563 NULL
3566 rc = parse_options (&line, args, client);
3567 if (rc)
3568 return send_error (ctx, rc);
3570 if (client->opts & OPT_INQUIRE)
3572 unsigned char *result;
3573 size_t len;
3575 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3576 if (rc)
3577 return send_error (ctx, rc);
3579 line = (char *) result;
3582 rc = do_rename (ctx, line);
3584 if (client->opts & OPT_INQUIRE)
3585 xfree (line);
3587 return send_error (ctx, rc);
3590 static gpg_error_t
3591 do_copy (assuan_context_t ctx, char *line)
3593 struct client_s *client = assuan_get_pointer (ctx);
3594 gpg_error_t rc;
3595 char **req, **src = NULL, **dst = NULL;
3596 xmlNodePtr nsrc, ndst, new = NULL;
3598 req = str_split (line, " ", 0);
3599 if (!req || !req[0] || !req[1])
3601 strv_free (req);
3602 return GPG_ERR_SYNTAX;
3605 if (strchr (req[0], '\t'))
3606 src = str_split (req[0], "\t", 0);
3607 else
3608 src = str_split (req[0], " ", 0);
3610 if (!src || !*src)
3612 rc = GPG_ERR_SYNTAX;
3613 goto fail;
3616 if (strchr (req[1], '\t'))
3617 dst = str_split (req[1], "\t", 0);
3618 else
3619 dst = str_split (req[1], " ", 0);
3621 if (!dst || !*dst)
3623 rc = GPG_ERR_SYNTAX;
3624 goto fail;
3627 if (!valid_element_path (dst, 0))
3629 rc = GPG_ERR_INV_VALUE;
3630 goto fail;
3633 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3634 if (nsrc && src[1])
3635 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc, NULL,
3636 NULL, NULL, 0, 0, NULL, 0);
3638 if (!nsrc)
3639 goto fail;
3641 new = xmlCopyNodeList (nsrc);
3642 if (!new)
3644 rc = GPG_ERR_ENOMEM;
3645 goto fail;
3648 int create = 0;
3649 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3650 if (ndst && dst[1])
3652 if (ndst->children)
3653 ndst = find_elements (client, client->doc, ndst->children, dst + 1, &rc, NULL,
3654 NULL, create_target_elements_cb, 0, 0, NULL, 0);
3655 else
3656 create = 1;
3658 else
3659 create = 1;
3661 if (!ndst && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3662 goto fail;
3663 else if (!ndst || create)
3665 ndst = create_element_path (client, &dst, &rc, NULL);
3666 if (!ndst)
3667 goto fail;
3670 rc = is_element_owner (client, ndst);
3671 if (rc)
3672 goto fail;
3674 /* Merge any attributes from the src node to the initial dst node. */
3675 for (xmlAttrPtr attr = new->properties; attr; attr = attr->next)
3677 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3678 continue;
3680 xmlAttrPtr a = xmlHasProp (ndst, attr->name);
3681 if (a)
3682 xmlRemoveProp (a);
3684 xmlChar *tmp = xmlNodeGetContent (attr->children);
3685 xmlNewProp (ndst, attr->name, tmp);
3686 xmlFree (tmp);
3687 rc = add_attribute (client, ndst, NULL, NULL);
3690 xmlNodePtr n = ndst->children;
3691 xmlUnlinkNode (n);
3692 xmlFreeNodeList (n);
3693 ndst->children = NULL;
3695 if (new->children)
3697 n = xmlCopyNodeList (new->children);
3698 if (!n)
3700 rc = GPG_ERR_ENOMEM;
3701 goto fail;
3704 n = xmlAddChildList (ndst, n);
3705 if (!n)
3707 rc = GPG_ERR_ENOMEM;
3708 goto fail;
3711 rc = update_element_mtime (client, xmlDocGetRootElement (client->doc) ==
3712 ndst->parent ? ndst : ndst->parent);
3715 fail:
3716 if (new)
3718 xmlUnlinkNode (new);
3719 xmlFreeNodeList (new);
3722 if (req)
3723 strv_free (req);
3725 if (src)
3726 strv_free (src);
3728 if (dst)
3729 strv_free (dst);
3731 return rc;
3734 static gpg_error_t
3735 copy_command (assuan_context_t ctx, char *line)
3737 struct client_s *client = assuan_get_pointer (ctx);
3738 gpg_error_t rc;
3739 struct argv_s *args[] = {
3740 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3741 NULL
3744 rc = parse_options (&line, args, client);
3745 if (rc)
3746 return send_error (ctx, rc);
3748 if (client->opts & OPT_INQUIRE)
3750 unsigned char *result;
3751 size_t len;
3753 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3754 if (rc)
3755 return send_error (ctx, rc);
3757 line = (char *) result;
3760 rc = do_copy (ctx, line);
3762 if (client->opts & OPT_INQUIRE)
3763 xfree (line);
3765 return send_error (ctx, rc);
3768 static gpg_error_t
3769 do_move (assuan_context_t ctx, char *line)
3771 struct client_s *client = assuan_get_pointer (ctx);
3772 gpg_error_t rc;
3773 char **req, **src = NULL, **dst = NULL;
3774 xmlNodePtr nsrc, ndst = NULL;
3776 req = str_split (line, " ", 0);
3778 if (!req || !req[0] || !req[1])
3780 strv_free (req);
3781 return GPG_ERR_SYNTAX;
3784 if (strchr (req[0], '\t'))
3785 src = str_split (req[0], "\t", 0);
3786 else
3787 src = str_split (req[0], " ", 0);
3789 if (!src || !*src)
3791 rc = GPG_ERR_SYNTAX;
3792 goto fail;
3795 if (strchr (req[1], '\t'))
3796 dst = str_split (req[1], "\t", 0);
3797 else
3798 dst = str_split (req[1], " ", 0);
3800 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3801 if (nsrc && src[1])
3802 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc,
3803 NULL, NULL, NULL, 0, 0, NULL, 0);
3805 if (!nsrc)
3806 goto fail;
3808 rc = is_element_owner (client, nsrc);
3809 if (rc)
3810 goto fail;
3812 if (dst)
3814 if (!valid_element_path (dst, 0))
3816 rc = GPG_ERR_INV_VALUE;
3817 goto fail;
3820 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3821 if (ndst && dst[1])
3822 ndst = find_elements (client, client->doc, ndst->children, dst + 1,
3823 &rc, NULL, NULL, NULL, 0, 0, NULL, 0);
3825 else
3826 ndst = xmlDocGetRootElement (client->doc);
3828 for (xmlNodePtr n = ndst; n; n = n->parent)
3830 if (n == nsrc)
3832 rc = GPG_ERR_CONFLICT;
3833 goto fail;
3837 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3838 goto fail;
3840 rc = 0;
3842 if (ndst)
3844 xmlChar *a = node_has_attribute (nsrc, (xmlChar *) "_name");
3846 xmlNodePtr dup = find_element (client, ndst->children, (char *) a,
3847 NULL, &rc);
3848 xmlFree (a);
3850 if (rc)
3851 goto fail;
3853 rc = is_element_owner (client, ndst);
3854 if (rc)
3855 goto fail;
3857 if (dup)
3859 if (dup == nsrc)
3860 goto fail;
3862 if (ndst == xmlDocGetRootElement (client->doc))
3864 xmlNodePtr n = nsrc;
3865 int match = 0;
3867 while (n->parent && n->parent != ndst)
3868 n = n->parent;
3870 xmlChar *a = node_has_attribute (n, (xmlChar *) "_name");
3871 xmlChar *b = node_has_attribute (nsrc, (xmlChar *) "_name");
3873 if (xmlStrEqual (a, b))
3875 match = 1;
3876 xmlUnlinkNode (nsrc);
3877 xmlUnlinkNode (n);
3878 xmlFreeNodeList (n);
3881 xmlFree (a);
3882 xmlFree (b);
3884 if (!match)
3886 xmlUnlinkNode (dup);
3887 xmlFreeNodeList (dup);
3890 else
3891 xmlUnlinkNode (dup);
3895 if (!ndst && dst)
3897 xmlChar *name = node_has_attribute (nsrc, (xmlChar *) "_name");
3899 if (nsrc->parent == xmlDocGetRootElement (client->doc)
3900 && !strcmp ((char *) name, *dst))
3902 xmlFree (name);
3903 rc = GPG_ERR_CONFLICT;
3904 goto fail;
3907 xmlFree (name);
3908 ndst = create_element_path (client, &dst, &rc, nsrc);
3911 if (!ndst)
3912 goto fail;
3914 update_element_mtime (client, nsrc->parent);
3915 xmlUnlinkNode (nsrc);
3916 ndst = xmlAddChildList (ndst, nsrc);
3918 if (!ndst)
3919 rc = GPG_ERR_ENOMEM;
3920 else
3921 update_element_mtime (client, ndst->parent);
3923 fail:
3924 if (req)
3925 strv_free (req);
3927 if (src)
3928 strv_free (src);
3930 if (dst)
3931 strv_free (dst);
3933 return rc;
3936 static gpg_error_t
3937 move_command (assuan_context_t ctx, char *line)
3939 struct client_s *client = assuan_get_pointer (ctx);
3940 gpg_error_t rc;
3941 struct argv_s *args[] = {
3942 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3943 NULL
3946 rc = parse_options (&line, args, client);
3947 if (rc)
3948 return send_error (ctx, rc);
3950 if (client->opts & OPT_INQUIRE)
3952 unsigned char *result;
3953 size_t len;
3955 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3956 if (rc)
3957 return send_error (ctx, rc);
3959 line = (char *) result;
3962 rc = do_move (ctx, line);
3964 if (client->opts & OPT_INQUIRE)
3965 xfree (line);
3967 return send_error (ctx, rc);
3970 static gpg_error_t
3971 ls_command (assuan_context_t ctx, char *line)
3973 gpg_error_t rc;
3974 char *tmp = str_asprintf ("%s/data", homedir);
3975 char *dir = expand_homedir (tmp);
3976 DIR *d = opendir (dir);
3978 rc = gpg_error_from_errno (errno);
3979 xfree (tmp);
3981 if (!d)
3983 xfree (dir);
3984 return send_error (ctx, rc);
3987 size_t len =
3988 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3989 struct dirent *p = xmalloc (len), *cur = NULL;
3990 char *list = NULL;
3992 xfree (dir);
3993 rc = 0;
3995 while (!readdir_r (d, p, &cur) && cur)
3997 if (cur->d_name[0] == '.' && cur->d_name[1] == '\0')
3998 continue;
3999 else if (cur->d_name[0] == '.' && cur->d_name[1] == '.'
4000 && cur->d_name[2] == '\0')
4001 continue;
4003 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
4005 if (!tmp)
4007 if (list)
4008 xfree (list);
4010 rc = GPG_ERR_ENOMEM;
4011 break;
4014 xfree (list);
4015 list = tmp;
4018 closedir (d);
4019 xfree (p);
4021 if (rc)
4022 return send_error (ctx, rc);
4024 if (!list)
4025 return send_error (ctx, GPG_ERR_NO_DATA);
4027 list[strlen (list) - 1] = 0;
4028 rc = xfer_data (ctx, list, strlen (list));
4029 xfree (list);
4030 return send_error (ctx, rc);
4033 static gpg_error_t
4034 bye_notify (assuan_context_t ctx, char *line)
4036 struct client_s *cl = assuan_get_pointer (ctx);
4038 #ifdef WITH_GNUTLS
4039 if (cl->thd->remote)
4041 int rc;
4045 struct timeval tv = { 0, 50000 };
4047 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
4048 if (rc == GNUTLS_E_AGAIN)
4049 select (0, NULL, NULL, NULL, &tv);
4051 while (rc == GNUTLS_E_AGAIN);
4053 #endif
4055 /* This will let assuan_process_next() return. */
4056 fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK);
4057 cl->last_rc = 0; // BYE command result
4058 return 0;
4061 static gpg_error_t
4062 reset_notify (assuan_context_t ctx, char *line)
4064 struct client_s *client = assuan_get_pointer (ctx);
4066 if (client)
4067 cleanup_client (client);
4069 return 0;
4073 * This is called before every Assuan command.
4075 static gpg_error_t
4076 command_startup (assuan_context_t ctx, const char *name)
4078 struct client_s *client = assuan_get_pointer (ctx);
4079 gpg_error_t rc;
4080 struct command_table_s *cmd = NULL;
4082 log_write1 ("command='%s'", name);
4083 client->last_rc = client->opts = 0;
4085 for (int i = 0; command_table[i]; i++)
4087 if (!strcasecmp (name, command_table[i]->name))
4089 if (command_table[i]->ignore_startup)
4090 return 0;
4091 cmd = command_table[i];
4092 break;
4096 client->last_rc = rc = gpg_error (file_modified (client, cmd));
4097 return rc;
4101 * This is called after every Assuan command.
4103 static void
4104 command_finalize (assuan_context_t ctx, gpg_error_t rc)
4106 struct client_s *client = assuan_get_pointer (ctx);
4108 if (!(client->flags & FLAG_LOCK_CMD))
4109 unlock_file_mutex (client, 0);
4111 log_write1 (_("command completed: rc=%u"), rc ? rc : client->last_rc);
4112 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
4113 #ifdef WITH_GNUTLS
4114 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
4115 #endif
4118 static gpg_error_t
4119 help_command (assuan_context_t ctx, char *line)
4121 gpg_error_t rc;
4122 int i;
4124 if (!line || !*line)
4126 char *tmp;
4127 char *help = str_dup (_("Usage: HELP [<COMMAND>]\n"
4128 "For commands that take an element path as an argument, each element is "
4129 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
4130 "\n" "COMMANDS:"));
4132 for (i = 0; command_table[i]; i++)
4134 if (!command_table[i]->help)
4135 continue;
4137 tmp = str_asprintf ("%s %s", help, command_table[i]->name);
4138 xfree (help);
4139 help = tmp;
4142 tmp = strip_texi_and_wrap (help);
4143 xfree (help);
4144 rc = xfer_data (ctx, tmp, strlen (tmp));
4145 xfree (tmp);
4146 return send_error (ctx, rc);
4149 for (i = 0; command_table[i]; i++)
4151 if (!strcasecmp (line, command_table[i]->name))
4153 char *help, *tmp;
4155 if (!command_table[i]->help)
4156 break;
4158 help = strip_texi_and_wrap (command_table[i]->help);
4159 tmp = str_asprintf (_("Usage: %s"), help);
4160 xfree (help);
4161 rc = xfer_data (ctx, tmp, strlen (tmp));
4162 xfree (tmp);
4163 return send_error (ctx, rc);
4167 return send_error (ctx, GPG_ERR_INV_NAME);
4170 static void
4171 new_command (const char *name, int ignore, int unlock,
4172 gpg_error_t (*handler) (assuan_context_t, char *),
4173 const char *help)
4175 int i = 0;
4177 if (command_table)
4178 for (i = 0; command_table[i]; i++);
4180 command_table =
4181 xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
4182 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
4183 command_table[i]->name = name;
4184 command_table[i]->handler = handler;
4185 command_table[i]->ignore_startup = ignore;
4186 command_table[i]->unlock = unlock;
4187 command_table[i++]->help = help;
4188 command_table[i] = NULL;
4191 void
4192 deinit_commands ()
4194 int i;
4196 for (i = 0; command_table[i]; i++)
4197 xfree (command_table[i]);
4199 xfree (command_table);
4202 static int
4203 sort_commands (const void *arg1, const void *arg2)
4205 struct command_table_s *const *a = arg1;
4206 struct command_table_s *const *b = arg2;
4208 if (!*a || !*b)
4209 return 0;
4210 else if (*a && !*b)
4211 return 1;
4212 else if (!*a && *b)
4213 return -1;
4215 return strcmp ((*a)->name, (*b)->name);
4218 static gpg_error_t
4219 passwd_command (assuan_context_t ctx, char *line)
4221 struct client_s *client = assuan_get_pointer (ctx);
4222 gpg_error_t rc;
4223 struct argv_s *args[] = {
4224 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
4225 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
4226 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG, parse_opt_no_passphrase},
4227 NULL
4230 rc = peer_is_invoker (client);
4231 if (rc == GPG_ERR_EACCES)
4232 return send_error (ctx, rc);
4234 if (client->flags & FLAG_NEW)
4235 return send_error (ctx, GPG_ERR_INV_STATE);
4237 client->crypto->save.s2k_count =
4238 config_get_ulong (client->filename, "s2k_count");
4239 rc = parse_options (&line, args, client);
4240 if (rc)
4241 return send_error (ctx, rc);
4243 if (!rc && client->opts & OPT_RESET)
4245 rc = cache_clear (client->md5file);
4246 if (!rc)
4247 send_status_all (STATUS_CACHE, NULL);
4250 if (!rc)
4252 if (!IS_PKI (client->crypto))
4254 struct crypto_s *crypto;
4256 xfree (client->crypto->filename);
4257 client->crypto->filename = str_dup (client->filename);
4258 rc = change_passwd (ctx, client->filename,
4259 client->flags & FLAG_NO_PINENTRY, &crypto,
4260 (client->opts & OPT_NO_PASSPHRASE));
4261 if (!rc)
4263 cleanup_crypto (&client->crypto);
4264 client->crypto = crypto;
4265 update_checksum (client);
4266 cleanup_crypto_stage1 (client->crypto);
4269 #ifdef WITH_AGENT
4270 else
4272 if (client->crypto->save.s2k_count)
4273 rc = send_to_agent (client->crypto->agent, NULL, NULL,
4274 "OPTION s2k-count=%lu",
4275 client->crypto->save.s2k_count);
4277 if (!rc)
4278 rc = agent_passwd (client->crypto);
4280 #endif
4283 return send_error (ctx, rc);
4286 static gpg_error_t
4287 parse_keygrip_opt_sign (void *data, void *value)
4289 struct client_s *client = data;
4291 (void) value;
4292 client->opts |= OPT_SIGN;
4293 return 0;
4296 static gpg_error_t
4297 keygrip_command (assuan_context_t ctx, char *line)
4299 struct client_s *client = assuan_get_pointer (ctx);
4300 gpg_error_t rc;
4301 struct crypto_s *crypto = NULL;
4302 struct argv_s *args[] = {
4303 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_keygrip_opt_sign},
4304 NULL
4307 if (!line || !*line)
4308 return send_error (ctx, GPG_ERR_SYNTAX);
4310 rc = parse_options (&line, args, client);
4311 if (rc)
4312 return send_error (ctx, rc);
4314 if (!valid_filename (line))
4315 return send_error (ctx, GPG_ERR_INV_VALUE);
4317 rc = init_client_crypto (&crypto);
4318 if (rc)
4319 return send_error (ctx, rc);
4321 rc = read_data_file (line, crypto);
4322 if (!rc)
4324 char *hexgrip = NULL;
4326 if (!IS_PKI (crypto))
4328 cleanup_crypto (&crypto);
4329 return send_error (ctx, GPG_ERR_NOT_SUPPORTED);
4332 if (client->opts & OPT_SIGN)
4334 if (valid_keygrip (crypto->sign_grip, sizeof (crypto->sign_grip)))
4335 hexgrip = bin2hex (crypto->sign_grip, sizeof (crypto->sign_grip));
4338 if (!hexgrip)
4339 hexgrip = bin2hex (crypto->grip, sizeof (crypto->grip));
4341 if (!hexgrip)
4342 rc = GPG_ERR_ENOMEM;
4343 else
4344 rc = xfer_data (ctx, hexgrip, strlen (hexgrip));
4346 xfree (hexgrip);
4349 UPDATE_AGENT_CTX (client, crypto);
4350 cleanup_crypto (&crypto);
4351 return send_error (ctx, rc);
4354 static gpg_error_t
4355 parse_opt_data (void *data, void *value)
4357 struct client_s *client = data;
4359 (void) value;
4360 client->opts |= OPT_DATA;
4361 return 0;
4364 static gpg_error_t
4365 getinfo_command (assuan_context_t ctx, char *line)
4367 struct client_s *client = assuan_get_pointer (ctx);
4368 gpg_error_t rc;
4369 char buf[ASSUAN_LINELENGTH];
4370 struct argv_s *args[] = {
4371 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4372 NULL
4375 rc = parse_options (&line, args, client);
4376 if (rc)
4377 return send_error (ctx, rc);
4379 if (!strcasecmp (line, "clients"))
4381 if (client->opts & OPT_DATA)
4383 MUTEX_LOCK (&cn_mutex);
4384 snprintf (buf, sizeof (buf), "%i", slist_length (cn_thread_list));
4385 MUTEX_UNLOCK (&cn_mutex);
4386 rc = xfer_data (ctx, buf, strlen (buf));
4388 else
4389 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4391 else if (!strcasecmp (line, "cache"))
4393 if (client->opts & OPT_DATA)
4395 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4396 rc = xfer_data (ctx, buf, strlen (buf));
4398 else
4399 rc = send_status (ctx, STATUS_CACHE, NULL);
4401 else if (!strcasecmp (line, "pid"))
4403 char buf[32];
4404 pid_t pid = getpid ();
4406 snprintf (buf, sizeof (buf), "%i", pid);
4407 rc = xfer_data (ctx, buf, strlen (buf));
4409 else if (!strcasecmp (line, "version"))
4411 char *buf = str_asprintf ("0x%06x %s%s", VERSION_HEX,
4412 #ifdef WITH_GNUTLS
4413 "GNUTLS "
4414 #endif
4415 #ifdef WITH_QUALITY
4416 "QUALITY "
4417 #endif
4418 "", use_agent ? "AGENT" : "");
4419 rc = xfer_data (ctx, buf, strlen (buf));
4420 xfree (buf);
4422 else if (!strcasecmp (line, "last_error"))
4424 if (client->last_error)
4425 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4426 else
4427 rc = GPG_ERR_NO_DATA;
4429 else
4430 rc = gpg_error (GPG_ERR_SYNTAX);
4432 return send_error (ctx, rc);
4435 #ifdef WITH_AGENT
4436 static gpg_error_t
4437 send_data_cb (void *user, const void *buf, size_t len)
4439 assuan_context_t ctx = user;
4441 return assuan_send_data (ctx, buf, len);
4444 static gpg_error_t
4445 send_status_cb (void *user, const char *line)
4447 assuan_context_t ctx = user;
4448 char keyword[200], *k;
4449 const char *p;
4451 for (p = line, k = keyword; *p; p++)
4453 if (isspace (*p))
4454 break;
4456 *k++ = *p;
4459 *k = 0;
4460 if (*p == '#')
4461 p++;
4463 while (isspace (*p))
4464 p++;
4466 return assuan_write_status (ctx, keyword, *p ? p : NULL);
4468 #endif
4470 static gpg_error_t
4471 agent_command (assuan_context_t ctx, char *line)
4473 gpg_error_t rc = 0;
4475 if (!use_agent)
4476 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
4478 #ifdef WITH_AGENT
4479 struct client_s *client = assuan_get_pointer (ctx);
4481 if (!line || !*line)
4482 return send_error (ctx, GPG_ERR_SYNTAX);
4484 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 1);
4485 rc = assuan_transact (client->crypto->agent->ctx, line, send_data_cb,
4486 client->ctx, agent_loopback_cb, client->crypto,
4487 send_status_cb, client->ctx);
4488 if (gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
4490 char *line;
4491 size_t len;
4493 rc = assuan_write_line (client->crypto->agent->ctx, "CAN");
4494 if (!rc)
4496 rc = assuan_read_line (client->crypto->agent->ctx, &line, &len);
4497 if (!rc)
4498 rc = gpg_error (GPG_ERR_ASS_CANCELED);
4502 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 0);
4503 #endif
4504 return send_error (ctx, rc);
4507 void
4508 init_commands ()
4510 /* !BEGIN-HELP-TEXT!
4512 * This comment is used as a marker to generate the offline documentation
4513 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4514 * script to determine where commands begin and end.
4516 new_command("HELP", 1, 1, help_command, _(
4517 "HELP [<COMMAND>]\n"
4518 "Show available commands or command specific help text."
4521 new_command("AGENT", 1, 1, agent_command, _(
4522 "AGENT <command>\n"
4523 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4524 "@command{gpg-agent}."
4527 new_command("GETINFO", 1, 1, getinfo_command, _(
4528 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4529 "Get server and other information: @var{cache} returns the number of cached "
4530 "documents via a status message. @var{clients} returns the number of "
4531 "connected clients via a status message. @var{pid} returns the process ID "
4532 "number of the server via a data response. @var{VERSION} returns the server "
4533 "version number and compile-time features with a data response with each "
4534 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4535 "the last failed command when available. @xref{Status Messages}. "
4536 "\n"
4537 "When the @option{--data} option is specified then the result will be sent "
4538 "via a data response rather than a status message."
4541 new_command("PASSWD", 0, 0, passwd_command, _(
4542 "PASSWD [--reset] [--s2k-count=N] [--no-passphrase]\n"
4543 "Changes the passphrase of the secret key required to open the current "
4544 "file or the passphrase of a symmetrically encrypted data file. When the "
4545 "@option{--reset} option is passed then the cache entry for the current "
4546 "file will be reset and the passphrase, if any, will be required during the "
4547 "next @code{OPEN} (@pxref{OPEN})."
4548 "\n"
4549 "The @option{--s2k-count} option sets or changes (@pxref{SAVE}) the number "
4550 "of hash iterations for a passphrase and must be either @code{0} to use "
4551 "the calibrated count of the machine (the default), or a value greater than "
4552 "or equal to @code{65536}. This option has no effect for symmetrically "
4553 "encrypted data files."
4554 "\n"
4555 "The @option{--no-passphrase} option will prevent requiring a passphrase for "
4556 "the data file, although a passphrase may be required when changing it."
4557 "\n"
4558 "This command is not available for non-invoking clients "
4559 "(@pxref{Access Control})."
4562 new_command("KEYGRIP", 1, 1, keygrip_command, _(
4563 "KEYGRIP [--sign] <filename>\n"
4564 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4565 "data response."
4566 "\n"
4567 "When the @option{--sign} option is specified then the key used for signing "
4568 "of the specified @var{filename} will be returned."
4569 "\n"
4570 "For symmetrically encrypted data files this command returns the error "
4571 "GPG_ERR_NOT_SUPPORTED."
4574 new_command("OPEN", 1, 1, open_command, _(
4575 "OPEN [--lock] <filename> [<passphrase>]\n"
4576 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4577 "found on the file-system then a new document will be created. If the file "
4578 "is found, it is looked for in the file cache. If cached and no "
4579 "@var{passphrase} was specified then the cached document is opened. When not "
4580 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4581 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4582 "specified."
4583 "\n"
4584 "When the @option{--lock} option is passed then the file mutex will be "
4585 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4586 "file has been opened."
4589 new_command("SAVE", 0, 0, save_command, _(
4590 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring] [--sign-keygrip=hexstring]\n"
4591 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4592 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4593 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4594 "keypair will be generated and a pinentry will be used to prompt for the "
4595 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4596 "passed in which case the data file will not be passphrase protected. "
4597 "\n"
4598 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4599 "passphrase retrieval and caching of new files when @command{gpg-agent} "
4600 "use is enabled. The datafile will be symmetrically encrypted and will not "
4601 "use or generate any keypair."
4602 "\n"
4603 "The @option{--reset} option will clear the cache entry for the current file "
4604 "and require a passphrase, if needed, before saving."
4605 "\n"
4606 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4607 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4608 "(@pxref{Configuration}) for available ciphers."
4609 "\n"
4610 "The @option{--cipher-iterations} option specifies the number of times to "
4611 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4612 "\n"
4613 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4614 "the client to obtain the key paramaters to use when generating a new "
4615 "keypair. The inquired data is expected to be an S-expression. If not "
4616 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4617 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4618 "that when this option is specified a new keypair will be generated "
4619 "reguardless if the file is a new one and that if the data file is protected "
4620 "the passphrase to open it will be required before generating the new "
4621 "keypair. This option is not available for non-invoking clients "
4622 "(@pxref{Access Control})."
4623 "\n"
4624 "You can encrypt the data file to a public key other than the one that it "
4625 "was originally encrypted with by passing the @option{--keygrip} option with "
4626 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4627 "be of any key that @command{gpg-agent} knows about. The "
4628 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4629 "secret key. Use the @code{KEYGRIP} (@pxref{KEYGRIP}) command to obtain the "
4630 "keygrip of an existing data file. This option may be needed when using a "
4631 "smartcard. This option has no effect with symmetrically encrypted data "
4632 "files. These options are not available for non-invoking clients "
4633 "(@pxref{Access Control})."
4634 "\n"
4635 "The @option{--s2k-count} option sets number of hash iterations for a "
4636 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4637 "value and is the default. This setting only affects new files. To change "
4638 "the setting use the @code{PASSWD} command (@pxref{PASSWD}). This option "
4639 "has no effect with symmetrically encrypted data files."
4642 new_command("ISCACHED", 1, 0, iscached_command, _(
4643 "ISCACHED [--lock] <filename>\n"
4644 "An @emph{OK} response is returned if the specified @var{filename} is found "
4645 "in the file cache. If not found in the cache but exists on the filesystem "
4646 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4647 "returned."
4648 "\n"
4649 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4650 "file exists; it does not need to be opened nor cached. The lock will be "
4651 "released when the client exits or sends the @code{UNLOCK} (@pxref{UNLOCK}) "
4652 "command."
4655 new_command("CLEARCACHE", 1, 1, clearcache_command, _(
4656 "CLEARCACHE [<filename>]\n"
4657 "Clears a file cache entry for all or the specified @var{filename}."
4660 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command, _(
4661 "CACHETIMEOUT <filename> <seconds>\n"
4662 "The time in @var{seconds} until @var{filename} will be removed from the "
4663 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4664 "the passphrase for each @code{OPEN} or @code{SAVE} command (@pxref{OPEN}, "
4665 "@pxref{SAVE}). @xref{Configuration}, and the @code{cache_timeout} "
4666 "parameter."
4669 new_command("LIST", 0, 1, list_command, _(
4670 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4671 "If no element path is given then a newline separated list of root elements "
4672 "is returned with a data response. If given, then all reachable elements "
4673 "of the specified element path are returned unless the @option{--no-recurse} "
4674 "option is specified. If specified, only the child elements of the element "
4675 "path are returned without recursing into grandchildren. Each resulting "
4676 "element is prefixed with the literal @code{!} character when the element "
4677 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4678 "\n"
4679 "When the @option{--verbose} option is passed then each element path "
4680 "returned will have zero or more flags appened to it. These flags are "
4681 "delimited from the element path by a single space character. A flag itself "
4682 "is a single character. Flag @code{P} indicates that access to the element "
4683 "is denied. Flag @code{+} indicates that there are child nodes of "
4684 "the current element path. Flag @code{E} indicates that an element of an "
4685 "element path contained in a @var{target} attribute could not be found. Flag "
4686 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4687 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4688 "of the @var{target} attribute contained in the current element (see below)."
4689 "\n"
4690 "The @option{--with-target} option implies @option{--verbose} and will append "
4691 "an additional flag @code{T} followed by a single space then an element path. "
4692 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4693 "current element when it contains a @var{target} attribute. When no "
4694 "@var{target} attribute is found then no flag will be appended."
4695 "\n"
4696 "The @option{--no-recurse} option limits the amount of data returned to only "
4697 "the listing of children of the specified element path and not any "
4698 "grandchildren."
4699 "\n"
4700 "The @option{--all} option lists the entire element tree for each root "
4701 "element. This option also implies option @option{--verbose}."
4702 "\n"
4703 "When the @option{--inquire} option is passed then all remaining non-option "
4704 "arguments are retrieved via a server @emph{INQUIRE}."
4707 new_command("REALPATH", 0, 1, realpath_command, _(
4708 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4709 "Resolves all @code{target} attributes of the specified element path and "
4710 "returns the result with a data response. @xref{Target Attribute}, for details."
4711 "\n"
4712 "When the @option{--inquire} option is passed then all remaining non-option "
4713 "arguments are retrieved via a server @emph{INQUIRE}."
4716 new_command("STORE", 0, 1, store_command, _(
4717 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4718 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4719 "\n"
4720 "Creates a new element path or modifies the @var{content} of an existing "
4721 "element. If only a single element is specified then a new root element is "
4722 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4723 "set to the final @key{TAB} delimited element. If no @var{content} is "
4724 "specified after the final @key{TAB}, then the content of an existing "
4725 "element will be removed; or empty when creating a new element."
4726 "\n"
4727 "The only restriction of an element name is that it not contain whitespace "
4728 "or begin with the literal element character @code{!} unless specifying a "
4729 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4730 "the @key{TAB} delimited elements. It is recommended that the content of an "
4731 "element be base64 encoded when it contains control or @key{TAB} characters "
4732 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
4735 new_command("RENAME", 0, 1, rename_command, _(
4736 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4737 "Renames the specified @var{element} to the new @var{value}. If an element of "
4738 "the same name as the @var{value} already exists it will be overwritten."
4739 "\n"
4740 "When the @option{--inquire} option is passed then all remaining non-option "
4741 "arguments are retrieved via a server @emph{INQUIRE}."
4744 new_command("COPY", 0, 1, copy_command, _(
4745 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4746 "Copies the entire element tree starting from the child node of the source "
4747 "element, to the destination element path. If the destination element path "
4748 "does not exist then it will be created; otherwise it is overwritten."
4749 "\n"
4750 "Note that attributes from the source element are merged into the "
4751 "destination element when the destination element path exists. When an "
4752 "attribute of the same name exists in both the source and destination "
4753 "elements then the destination attribute will be updated to the source "
4754 "attribute value."
4755 "\n"
4756 "When the @option{--inquire} option is passed then all remaining non-option "
4757 "arguments are retrieved via a server @emph{INQUIRE}."
4760 new_command("MOVE", 0, 1, move_command, _(
4761 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4762 "Moves the source element path to the destination element path. If the "
4763 "destination is not specified then it will be moved to the root node of the "
4764 "document. If the destination is specified and exists then it will be "
4765 "overwritten; otherwise non-existing elements of the destination element "
4766 "path will be created."
4767 "\n"
4768 "When the @option{--inquire} option is passed then all remaining non-option "
4769 "arguments are retrieved via a server @emph{INQUIRE}."
4772 new_command("DELETE", 0, 1, delete_command, _(
4773 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4774 "Removes the specified element path and all of its children. This may break "
4775 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4776 "refers to this element or any of its children."
4777 "\n"
4778 "When the @option{--inquire} option is passed then all remaining non-option "
4779 "arguments are retrieved via a server @emph{INQUIRE}."
4782 new_command("GET", 0, 1, get_command, _(
4783 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4784 "Retrieves the content of the specified element. The content is returned "
4785 "with a data response."
4786 "\n"
4787 "When the @option{--inquire} option is passed then all remaining non-option "
4788 "arguments are retrieved via a server @emph{INQUIRE}."
4791 new_command("ATTR", 0, 1, attr_command, _(
4792 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4793 "@table @asis\n"
4794 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4795 "\n"
4796 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4797 "element. When no @var{value} is specified any existing value will be removed."
4798 "\n"
4799 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4800 "\n"
4801 " Removes an @var{attribute} from an element."
4802 "\n"
4803 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4804 "\n"
4805 " Retrieves a newline separated list of attributes names and values "
4806 "from the specified element. Each attribute name and value is space delimited."
4807 "\n"
4808 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4809 "\n"
4810 " Retrieves the value of an @var{attribute} from an element."
4811 "@end table\n"
4812 "\n"
4813 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4814 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4815 "commands instead."
4816 "\n"
4817 "The @code{_mtime} attribute is updated each time an element is modified by "
4818 "either storing content, editing attributes or by deleting a child element. "
4819 "The @code{_ctime} attribute is created for each new element in an element "
4820 "path."
4821 "\n"
4822 "When the @option{--inquire} option is passed then all remaining non-option "
4823 "arguments are retrieved via a server @emph{INQUIRE}."
4824 "\n"
4825 "@xref{Target Attribute}, for details about this special attribute."
4828 new_command("XPATH", 0, 1, xpath_command, _(
4829 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4830 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4831 "specified it is assumed the expression is a request to return a result. "
4832 "Otherwise, the result is set to the @var{value} argument and the document is "
4833 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4834 "is assumed to be empty and the document is updated. For example:"
4835 "@sp 1\n"
4836 "@example\n"
4837 "XPATH //element[@@_name='password']@key{TAB}\n"
4838 "@end example\n"
4839 "@sp 1"
4840 "would clear the content of all @code{password} elements in the data file "
4841 "while leaving off the trailing @key{TAB} would return all @code{password} "
4842 "elements in @abbr{XML} format."
4843 "\n"
4844 "When the @option{--inquire} option is passed then all remaining non-option "
4845 "arguments are retrieved via a server @emph{INQUIRE}."
4846 "\n"
4847 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4848 "expression syntax."
4851 new_command("XPATHATTR", 0, 1, xpathattr_command, _(
4852 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4853 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4854 "attributes and does not return a result. For the @var{SET} operation the "
4855 "@var{value} is optional but the field is required. If not specified then "
4856 "the attribute value will be empty. For example:"
4857 "@sp 1"
4858 "@example\n"
4859 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4860 "@end example\n"
4861 "@sp 1"
4862 "would create an @code{password} attribute for each @code{password} element "
4863 "found in the document. The attribute value will be empty but still exist."
4864 "\n"
4865 "When the @option{--inquire} option is passed then all remaining non-option "
4866 "arguments are retrieved via a server @emph{INQUIRE}."
4867 "\n"
4868 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4869 "expression syntax."
4872 new_command("IMPORT", 0, 1, import_command, _(
4873 "IMPORT [--root [!]element[<TAB>[!]child[..]]] <content>\n"
4874 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4875 "\n"
4876 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4877 "argument is raw @abbr{XML} data. The content is created as a child of "
4878 "the element path specified with the @option{--root} option or at the "
4879 "document root when not specified. Existing elements of the same name will "
4880 "be overwritten."
4881 "\n"
4882 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4883 "for details."
4886 new_command("DUMP", 0, 1, dump_command, _(
4887 "DUMP\n"
4888 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4889 "dumping a specific node."
4892 new_command("LOCK", 0, 0, lock_command, _(
4893 "LOCK\n"
4894 "Locks the mutex associated with the opened file. This prevents other clients "
4895 "from sending commands to the same opened file until the client "
4896 "that sent this command either disconnects or sends the @code{UNLOCK} "
4897 "command. @xref{UNLOCK}."
4900 new_command("UNLOCK", 1, 0, unlock_command, _(
4901 "UNLOCK\n"
4902 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4903 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4904 "@pxref{ISCACHED})."
4907 new_command("GETCONFIG", 1, 1, getconfig_command, _(
4908 "GETCONFIG [filename] <parameter>\n"
4909 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4910 "data response. If no file has been opened then the value for @var{filename} "
4911 "or the default from the @samp{global} section will be returned. If a file "
4912 "has been opened and no @var{filename} is specified, a value previously "
4913 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4916 new_command("OPTION", 1, 1, option_command, _(
4917 "OPTION <NAME>=<VALUE>\n"
4918 "Sets a client option @var{name} to @var{value}. The value for an option is "
4919 "kept for the duration of the connection."
4920 "\n"
4921 "@table @asis\n"
4922 "@item DISABLE-PINENTRY\n"
4923 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
4924 "server inquire is sent to the client to obtain the passphrase. This option "
4925 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4926 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
4927 "\n"
4928 "@item PINENTRY-TIMEOUT\n"
4929 "Sets the number of seconds before a pinentry prompt will return an error "
4930 "while waiting for user input."
4931 "\n"
4932 "@item TTYNAME\n"
4933 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4934 "\n"
4935 "@item TTYTYPE\n"
4936 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4937 "\n"
4938 "@item DISPLAY\n"
4939 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4940 "\n"
4941 "@item PINENTRY-DESC\n"
4942 "Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4943 "\n"
4944 "@item PINENTRY-TITLE\n"
4945 "Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
4946 "\n"
4947 "@item PINENTRY-PROMPT\n"
4948 "Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
4949 "\n"
4950 "@item LC-CTYPE\n"
4951 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4952 "\n"
4953 "@item LC-MESSAGES\n"
4954 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4955 "\n"
4956 "@item NAME\n"
4957 "Associates the thread ID of the connection with the specified textual "
4958 "representation. Useful for debugging log messages."
4959 "\n"
4960 "@item LOCK-TIMEOUT\n"
4961 "When not @code{0}, the duration in tenths of a second to wait for the file "
4962 "mutex which has been locked by another thread to be released before returning "
4963 "an error. When @code{-1}, then an error will be returned immediately."
4964 "\n"
4965 "@item LOG-LEVEL\n"
4966 "An integer specifiying the logging level."
4967 "@end table\n"
4970 new_command("LS", 1, 1, ls_command, _(
4971 "LS\n"
4972 "Lists the available data files stored in the data directory "
4973 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4976 new_command("RESET", 1, 1, NULL, _(
4977 "RESET\n"
4978 "Closes the currently opened file but keeps any previously set client options."
4981 new_command("NOP", 1, 1, NULL, _(
4982 "NOP\n"
4983 "Does nothing. Always returns successfully."
4986 /* !END-HELP-TEXT! */
4987 new_command ("CANCEL", 1, 1, NULL, NULL);
4988 new_command ("END", 1, 1, NULL, NULL);
4989 new_command ("BYE", 1, 1, NULL, NULL);
4991 int i;
4992 for (i = 0; command_table[i]; i++);
4993 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4994 sort_commands);
4997 gpg_error_t
4998 register_commands (assuan_context_t ctx)
5000 int i = 0, rc;
5002 for (; command_table[i]; i++)
5004 if (!command_table[i]->handler)
5005 continue;
5007 rc = assuan_register_command (ctx, command_table[i]->name,
5008 command_table[i]->handler,
5009 command_table[i]->help);
5010 if (rc)
5011 return rc;
5014 rc = assuan_register_bye_notify (ctx, bye_notify);
5015 if (rc)
5016 return rc;
5018 rc = assuan_register_reset_notify (ctx, reset_notify);
5019 if (rc)
5020 return rc;
5022 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
5023 if (rc)
5024 return rc;
5026 return assuan_register_post_cmd_notify (ctx, command_finalize);