s/peer_is_self/peer_is_invoker/.
[libpwmd.git] / src / commands.c
blob3af2908e2fad17792aa57a6cca3d72de0cd21d8d
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_opt_inquire (void *data, void *value)
449 struct client_s *client = data;
451 (void) value;
452 client->opts |= OPT_INQUIRE;
453 return 0;
456 static gpg_error_t
457 update_checksum (struct client_s *client)
459 unsigned char *crc;
460 size_t len;
461 struct cache_data_s *cdata;
462 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
464 if (rc)
465 return rc;
467 xfree (client->crc);
468 client->crc = crc;
469 cdata = cache_get_data (client->md5file);
470 if (cdata)
472 xfree (cdata->crc);
473 cdata->crc = xmalloc (len);
474 memcpy (cdata->crc, crc, len);
477 return 0;
480 static gpg_error_t
481 validate_checksum (struct client_s *client, struct cache_data_s *cdata)
483 unsigned char *crc;
484 size_t len;
485 gpg_error_t rc;
486 int n = 0;
488 if (cdata && !cdata->crc)
489 return GPG_ERR_CHECKSUM;
491 rc = get_checksum (client->filename, &crc, &len);
492 if (rc)
493 return rc;
495 if (cdata)
496 n = memcmp (cdata->crc, crc, len);
497 else if (client->crc)
498 n = memcmp (client->crc, crc, len);
500 xfree (crc);
501 return n ? GPG_ERR_CHECKSUM : 0;
504 static gpg_error_t
505 do_open (assuan_context_t ctx, const char *filename, const char *password)
507 struct client_s *client = assuan_get_pointer (ctx);
508 struct cache_data_s *cdata;
509 gpg_error_t rc = 0;
510 int done = 0;
512 if (!valid_filename (filename))
513 return GPG_ERR_INV_VALUE;
515 gcry_md_hash_buffer (GCRY_MD_MD5, client->md5file, filename,
516 strlen (filename));
517 client->filename = str_dup (filename);
518 if (!client->filename)
519 return GPG_ERR_ENOMEM;
521 // Cached document?
522 cdata = cache_get_data (client->md5file);
523 if (cdata && cdata->doc)
525 int defer = 0;
527 /* This will check that the key is cached in the agent which needs to
528 * be determined for files that share a keygrip. */
529 if (!rc)
531 rc = cache_iscached (client->filename, &defer);
532 if ((!rc || rc == GPG_ERR_ENOENT) && defer)
533 rc = GPG_ERR_KEY_EXPIRED;
536 if (!rc && !(client->flags & FLAG_NEW))
537 rc = validate_checksum (client, cdata);
539 #ifdef WITH_GNUTLS
540 if (!rc && client->thd->remote
541 && config_get_boolean (client->filename, "tcp_require_key"))
542 rc = GPG_ERR_KEY_EXPIRED;
543 #endif
545 if (!rc && !password)
547 rc = read_data_header (client->filename, &client->crypto->hdr,
548 NULL, NULL);
549 if (!rc)
551 if (IS_PKI (client->crypto))
553 gcry_sexp_build (&client->crypto->pkey_sexp, NULL, "%S",
554 cdata->pubkey);
555 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
556 client->crypto->grip);
557 gcry_sexp_build (&client->crypto->sigpkey_sexp, NULL, "%S",
558 cdata->sigkey);
559 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
560 client->crypto->sign_grip);
563 if (!rc)
565 rc = open_finalize (ctx, NULL, 0);
566 done = 1;
571 /* There was an error accessing the file so clear the cache entry. The
572 * real error will be returned from read_data_file() since the file
573 * may have only disappeared. */
574 if (!done)
576 log_write ("%s: %s", filename, pwmd_strerror (rc));
577 rc = cache_clear (client->md5file);
578 send_status_all (STATUS_CACHE, NULL);
582 if (done || rc)
583 return rc;
585 rc = read_data_file (client->filename, client->crypto);
586 if (rc)
588 if (gpg_err_source (rc) != GPG_ERR_SOURCE_DEFAULT ||
589 gpg_err_code (rc) != GPG_ERR_ENOENT)
591 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
592 return rc;
595 client->flags |= FLAG_NEW;
596 memset (client->crypto->grip, 0, sizeof (client->crypto->grip));
597 memset (client->crypto->sign_grip, 0,
598 sizeof (client->crypto->sign_grip));
599 rc = open_finalize (ctx, NULL, 0);
600 return rc;
603 if (password && IS_PKI (client->crypto))
605 #ifdef WITH_AGENT
606 rc = set_agent_passphrase (client->crypto, password, strlen (password));
607 if (rc)
608 return rc;
609 #endif
612 rc = open_finalize (ctx, (char *)password, password ? strlen (password) : 0);
613 return rc;
616 static gpg_error_t
617 open_command (assuan_context_t ctx, char *line)
619 gpg_error_t rc;
620 struct client_s *client = assuan_get_pointer (ctx);
621 char **req, *filename;
622 unsigned char md5file[16];
623 int same_file = 0;
624 assuan_peercred_t peer;
625 struct argv_s *args[] = {
626 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
627 NULL
630 rc = parse_options (&line, args, client);
631 if (rc)
632 return send_error (ctx, rc);
634 req = str_split (line, " ", 2);
635 if (!req)
636 return send_error (ctx, GPG_ERR_SYNTAX);
638 rc = do_validate_peer (ctx, req[0], &peer);
639 if (rc)
641 strv_free (req);
642 return send_error (ctx, rc);
645 pthread_cleanup_push (req_cleanup, req);
646 filename = req[0];
647 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, filename, strlen (filename));
648 /* This client may have locked a different file with ISCACHED --lock than
649 * the current filename. This will remove that lock. */
650 same_file = !memcmp (md5file, client->md5file, 16) ? 1 : 0;
651 if (client->flags & FLAG_OPEN ||
652 (client->flags & FLAG_HAS_LOCK && !same_file))
654 typeof (client->opts) opts = client->opts;
655 typeof (client->flags) flags = client->flags;
657 if (same_file)
658 client->flags |= FLAG_KEEP_LOCK;
660 cleanup_client (client);
661 client->opts = opts;
662 client->flags |= flags;
663 client->flags &= ~(FLAG_LOCK_CMD);
664 if (!same_file)
665 client->flags &= ~(FLAG_HAS_LOCK | FLAG_NEW);
668 /* Need to lock the mutex here because file_modified() cannot without
669 * knowing the filename. */
670 memcpy (client->md5file, md5file, 16);
671 rc = lock_file_mutex (client, 1);
672 if (!rc)
674 char *password = req[1] && *req[1] ? req[1] : NULL;
676 #ifdef WITH_AGENT
677 if (IS_PKI (client->crypto))
678 rc = set_pinentry_mode (client->crypto->agent,
679 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
680 : "ask");
681 #endif
682 if (!rc)
684 rc = do_open (ctx, filename, password);
685 if (rc)
686 cleanup_client (client);
688 cleanup_crypto_stage1 (client->crypto);
692 pthread_cleanup_pop (1);
694 if (!rc && client->flags & FLAG_NEW)
695 rc = send_status (ctx, STATUS_NEWFILE, NULL);
697 #ifdef WITH_AGENT
698 (void) kill_scd (client->crypto->agent);
699 #endif
700 return send_error (ctx, rc);
703 static gpg_error_t
704 parse_opt_no_passphrase (void *data, void *value)
706 struct client_s *client = data;
708 (void) value;
709 client->opts |= OPT_NO_PASSPHRASE;
710 return 0;
713 static gpg_error_t
714 parse_save_opt_no_agent (void *data, void *value)
716 struct client_s *client = data;
718 client->opts |= OPT_NO_AGENT;
719 return 0;
722 static gpg_error_t
723 parse_save_opt_cipher (void *data, void *value)
725 struct client_s *client = data;
726 int algo = cipher_string_to_gcrypt ((char *) value);
727 file_header_t *hdr = &client->crypto->save.hdr;
729 if (algo == -1)
730 return GPG_ERR_INV_VALUE;
732 hdr->flags = set_cipher_flag (hdr->flags, algo);
733 return 0;
736 static gpg_error_t
737 parse_save_opt_keygrip (void *data, void *value)
739 struct client_s *client = data;
741 if (!IS_PKI (client->crypto))
742 return GPG_ERR_INV_ARG;
744 #ifdef WITH_AGENT
745 if (client->crypto->save.pkey)
746 gcry_sexp_release (client->crypto->save.pkey);
748 client->crypto->save.pkey = NULL;
749 return get_pubkey (client->crypto, value, &client->crypto->save.pkey);
750 #else
751 return GPG_ERR_INV_ARG;
752 #endif
755 static gpg_error_t
756 parse_save_opt_sign_keygrip (void *data, void *value)
758 struct client_s *client = data;
760 if (!IS_PKI (client->crypto))
761 return GPG_ERR_INV_ARG;
763 #ifdef WITH_AGENT
764 if (client->crypto->save.sigpkey)
765 gcry_sexp_release (client->crypto->save.sigpkey);
767 client->crypto->save.sigpkey = NULL;
768 return get_pubkey (client->crypto, value, &client->crypto->save.sigpkey);
769 #else
770 return GPG_ERR_INV_ARG;
771 #endif
774 static gpg_error_t
775 parse_opt_s2k_count (void *data, void *value)
777 struct client_s *client = data;
778 char *v = value;
780 if (!v || !*v)
781 return GPG_ERR_INV_VALUE;
783 client->crypto->save.s2k_count = strtoul (v, NULL, 10);
784 return 0;
787 static gpg_error_t
788 parse_save_opt_iterations (void *data, void *value)
790 struct client_s *client = data;
791 char *v = value, *p;
792 uint64_t n;
794 if (!v || !*v)
795 return GPG_ERR_INV_VALUE;
797 errno = 0;
798 n = strtoull (v, &p, 10);
799 if (n == UINT64_MAX && errno)
800 return gpg_error_from_errno (errno);
801 else if (p && *p)
802 return GPG_ERR_INV_VALUE;
804 client->crypto->save.hdr.iterations = n;
805 return 0;
808 static gpg_error_t
809 save_finalize (assuan_context_t ctx)
811 struct client_s *client = assuan_get_pointer (ctx);
812 gpg_error_t rc = 0;
813 xmlChar *xmlbuf = NULL;
814 int xmlbuflen;
815 void *key = NULL;
816 size_t keylen = 0;
818 xmlDocDumpFormatMemory (client->doc, &xmlbuf, &xmlbuflen, 0);
819 if (!xmlbuf)
820 return GPG_ERR_ENOMEM;
822 pthread_cleanup_push (xmlFree, xmlbuf);
824 if (!use_agent || ((client->flags & FLAG_NEW)
825 && (client->opts & OPT_NO_AGENT))
826 || !(client->crypto->hdr.flags & PWMD_FLAG_PKI))
828 rc = export_common (ctx, client->flags & FLAG_NO_PINENTRY,
829 client->crypto, xmlbuf, xmlbuflen, client->filename,
830 NULL, &key, &keylen, 1, 1,
831 (client->opts & OPT_NO_PASSPHRASE));
833 #ifdef WITH_AGENT
834 else
836 gcry_sexp_t pubkey = client->crypto->save.pkey;
837 gcry_sexp_t sigkey = client->crypto->save.sigpkey;
839 if (!pubkey)
840 pubkey = client->crypto->pkey_sexp;
842 if (!sigkey)
844 sigkey = client->crypto->sigpkey_sexp;
845 if (!sigkey)
847 gcry_sexp_build (&client->crypto->save.sigpkey, 0, "%S", pubkey);
848 sigkey = client->crypto->save.sigpkey;
852 rc = encrypt_data_file (client->ctx, client->crypto, pubkey, sigkey,
853 client->filename, xmlbuf, xmlbuflen);
854 if (pubkey == client->crypto->save.pkey)
856 if (!rc)
858 gcry_sexp_release (client->crypto->pkey_sexp);
859 client->crypto->pkey_sexp = client->crypto->save.pkey;
860 gcry_pk_get_keygrip (client->crypto->pkey_sexp,
861 client->crypto->grip);
863 else
864 gcry_sexp_release (pubkey);
866 client->crypto->save.pkey = NULL;
869 if (!rc)
870 gcry_pk_get_keygrip (sigkey, client->crypto->sign_grip);
872 if (sigkey == client->crypto->save.sigpkey)
874 if (!rc)
876 if (client->crypto->sigpkey_sexp)
877 gcry_sexp_release (client->crypto->sigpkey_sexp);
879 client->crypto->sigpkey_sexp = client->crypto->save.sigpkey;
880 gcry_pk_get_keygrip (client->crypto->sigpkey_sexp,
881 client->crypto->sign_grip);
883 else
884 gcry_sexp_release (sigkey);
886 client->crypto->save.sigpkey = NULL;
889 #endif
891 if (!rc)
893 int cached;
895 rc = save_common (client->filename, client->crypto, xmlbuf, xmlbuflen,
896 key, keylen, &cached, client->opts & OPT_NO_AGENT);
897 if (rc)
898 gcry_free (key);
900 if (!rc && (!cached || (client->flags & FLAG_NEW)))
901 send_status_all (STATUS_CACHE, NULL);
903 if (!rc)
905 rc = update_checksum (client);
906 client->flags &= ~(FLAG_NEW);
910 pthread_cleanup_pop (1); // xmlFree
911 return rc;
914 static gpg_error_t
915 parse_opt_reset (void *data, void *value)
917 struct client_s *client = data;
919 (void) value;
920 client->opts |= OPT_RESET;
921 return 0;
924 static gpg_error_t
925 save_command (assuan_context_t ctx, char *line)
927 struct client_s *client = assuan_get_pointer (ctx);
928 gpg_error_t rc;
929 struct stat st;
930 struct argv_s *args[] = {
931 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG,
932 parse_opt_no_passphrase},
933 &(struct argv_s) {"cipher", OPTION_TYPE_ARG, parse_save_opt_cipher},
934 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
935 parse_opt_inquire},
936 &(struct argv_s) {"keygrip", OPTION_TYPE_ARG, parse_save_opt_keygrip},
937 &(struct argv_s) {"sign-keygrip", OPTION_TYPE_ARG,
938 parse_save_opt_sign_keygrip},
939 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
940 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
941 &(struct argv_s) {"cipher-iterations", OPTION_TYPE_ARG,
942 parse_save_opt_iterations},
943 &(struct argv_s) {"no-agent", OPTION_TYPE_NOARG,
944 parse_save_opt_no_agent},
945 NULL
948 cleanup_save (&client->crypto->save);
949 memcpy (&client->crypto->save.hdr, &client->crypto->hdr,
950 sizeof (file_header_t));
951 client->crypto->save.s2k_count =
952 config_get_ulong (client->filename, "s2k_count");
954 if (client->flags & FLAG_NEW)
955 client->crypto->save.hdr.iterations =
956 config_get_ulonglong (client->filename, "cipher_iterations");
958 rc = parse_options (&line, args, client);
959 if (rc)
960 return send_error (ctx, rc);
962 if (!(client->flags & FLAG_NEW))
963 client->opts &= ~OPT_NO_AGENT;
964 else if (client->opts & OPT_NO_AGENT)
966 client->crypto->save.hdr.flags &= ~PWMD_FLAG_PKI;
967 client->crypto->hdr.flags &= ~PWMD_FLAG_PKI;
970 if ((client->opts & OPT_NO_PASSPHRASE) && !(client->flags & FLAG_NEW)
971 && !(client->opts & OPT_INQUIRE))
972 return send_error (ctx, GPG_ERR_WRONG_KEY_USAGE);
974 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
975 return send_error (ctx, gpg_error_from_errno (errno));
977 if (errno != ENOENT && !S_ISREG (st.st_mode))
979 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
980 return send_error (ctx, GPG_ERR_ENOANO);
983 int defer = 0;
984 rc = cache_iscached (client->filename, &defer);
985 if (gpg_err_code (rc) == GPG_ERR_ENOENT && client->flags & FLAG_NEW)
986 rc = 0;
987 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
988 rc = 0;
990 if ((!rc && defer) || config_get_boolean ("global", "require_save_key"))
991 client->opts |= OPT_RESET;
993 if (!rc && (client->opts & OPT_RESET))
995 rc = cache_clear (client->md5file);
996 if (rc)
997 return send_error (ctx, rc);
999 log_write ("%s: %s", client->filename,
1000 pwmd_strerror (GPG_ERR_KEY_EXPIRED));
1001 send_status_all (STATUS_CACHE, NULL);
1004 if (!rc)
1005 rc = update_element_mtime (xmlDocGetRootElement (client->doc));
1007 if (rc)
1008 return send_error (ctx, rc);
1010 #ifdef WITH_AGENT
1011 if (!rc && use_agent && !client->crypto->save.pkey &&
1012 (client->flags & FLAG_NEW || client->opts & OPT_INQUIRE)
1013 && !(client->opts & OPT_NO_AGENT))
1015 rc = set_pinentry_mode (client->crypto->agent,
1016 (client->flags & FLAG_NO_PINENTRY) ? "loopback"
1017 : "ask");
1019 if (!(client->flags & FLAG_NEW))
1021 struct crypto_s *crypto;
1022 char *key = NULL;
1023 size_t keylen = 0;
1025 /* Wanting to generate a new key. Require the key to open the
1026 current file before proceeding reguardless of the
1027 require_save_key configuration parameter. */
1028 rc = cache_clear (client->md5file);
1029 if (!rc)
1031 rc = init_client_crypto (&crypto);
1032 if (!rc)
1033 crypto->client_ctx = client->ctx;
1036 if (!rc)
1037 rc = decrypt_common (client->ctx, client->opts&OPT_INQUIRE, crypto,
1038 client->filename, &key, &keylen);
1040 xfree (key);
1041 cleanup_crypto (&crypto);
1044 if (!rc)
1045 rc = send_status (client->ctx, STATUS_GENKEY, NULL);
1047 if (!rc)
1049 struct inquire_data_s idata = { 0 };
1050 char *params = client->opts & OPT_INQUIRE
1051 ? NULL : default_key_params (client->crypto);
1053 pthread_cleanup_push (xfree, params);
1054 idata.crypto = client->crypto;
1056 if (params)
1058 idata.line = params;
1059 idata.len = strlen (params);
1061 else
1062 idata.preset = 1;
1064 client->crypto->agent->inquire_data = &idata;
1065 client->crypto->agent->inquire_cb = NULL;
1066 rc = generate_key (client->crypto, params,
1067 (client->opts & OPT_NO_PASSPHRASE), 1);
1068 pthread_cleanup_pop (1);
1071 #endif
1073 if (!rc)
1074 rc = save_finalize (ctx);
1076 cleanup_crypto_stage1 (client->crypto);
1077 #ifdef WITH_AGENT
1078 (void) kill_scd (client->crypto->agent);
1079 #endif
1080 return send_error (ctx, rc);
1083 static gpg_error_t
1084 do_delete (assuan_context_t ctx, char *line)
1086 struct client_s *client = assuan_get_pointer (ctx);
1087 gpg_error_t rc;
1088 char **req;
1089 xmlNodePtr n;
1091 if (strchr (line, '\t'))
1092 req = str_split (line, "\t", 0);
1093 else
1094 req = str_split (line, " ", 0);
1096 if (!req || !*req)
1097 return GPG_ERR_SYNTAX;
1099 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1100 if (!n)
1102 strv_free (req);
1103 return rc;
1107 * No sub-node defined. Remove the entire node (root element).
1109 if (!req[1])
1111 if (n)
1113 rc = peer_is_invoker (client);
1114 if (rc == GPG_ERR_EPERM)
1115 rc = is_element_owner (client, n);
1117 if (!rc)
1119 rc = unlink_node (n);
1120 xmlFreeNode (n);
1124 strv_free (req);
1125 return rc;
1128 n = find_elements (client, client->doc, n->children, req + 1, &rc, NULL,
1129 NULL, NULL, 0, 0, NULL, 0);
1130 strv_free (req);
1131 if (!n)
1132 return rc;
1134 rc = peer_is_invoker (client);
1135 if (rc == GPG_ERR_EPERM)
1136 rc = is_element_owner (client, n);
1138 if (!rc)
1140 rc = unlink_node (n);
1141 xmlFreeNode (n);
1144 return rc;
1147 static gpg_error_t
1148 delete_command (assuan_context_t ctx, char *line)
1150 struct client_s *client = assuan_get_pointer (ctx);
1151 gpg_error_t rc;
1152 struct argv_s *args[] = {
1153 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1154 NULL
1157 rc = parse_options (&line, args, client);
1158 if (rc)
1159 return send_error (ctx, rc);
1161 if (client->opts & OPT_INQUIRE)
1163 unsigned char *result;
1164 size_t len;
1166 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1167 if (rc)
1168 return send_error (ctx, rc);
1170 line = (char *) result;
1173 rc = do_delete (ctx, line);
1175 if (client->opts & OPT_INQUIRE)
1176 xfree (line);
1178 return send_error (ctx, rc);
1181 static gpg_error_t
1182 store_command (assuan_context_t ctx, char *line)
1184 struct client_s *client = assuan_get_pointer (ctx);
1185 gpg_error_t rc;
1186 size_t len;
1187 unsigned char *result;
1188 char **req;
1189 xmlNodePtr n, parent;
1190 int has_content;
1191 char *content = NULL;
1193 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1194 if (rc)
1195 return send_error (ctx, rc);
1197 req = str_split ((char *) result, "\t", 0);
1198 xfree (result);
1200 if (!req || !*req)
1201 return send_error (ctx, GPG_ERR_SYNTAX);
1203 len = strv_length (req);
1204 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1205 if (*(req + 1) && !valid_element_path (req, has_content))
1207 strv_free (req);
1208 return send_error (ctx, GPG_ERR_INV_VALUE);
1211 if (has_content || !*req[len - 1])
1213 has_content = 1;
1214 content = req[len - 1];
1215 req[len - 1] = NULL;
1218 again:
1219 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1220 if (rc && rc == GPG_ERR_ELEMENT_NOT_FOUND)
1222 rc = new_root_element (client, client->doc, *req);
1223 if (rc)
1225 strv_free (req);
1226 return send_error (ctx, rc);
1229 goto again;
1232 if (!n)
1234 strv_free (req);
1235 return send_error (ctx, rc);
1238 parent = n;
1240 if (req[1] && *req[1])
1242 if (!n->children)
1243 parent = create_elements_cb (client, 1, n, req + 1, &rc, NULL);
1244 else
1245 parent = find_elements (client, client->doc, n->children, req + 1, &rc,
1246 NULL, NULL, create_elements_cb, 0, 0, NULL,
1250 if (!rc && len > 1)
1252 rc = peer_is_invoker (client);
1253 if (rc == GPG_ERR_EPERM)
1254 rc = is_element_owner (client, parent);
1256 if (!rc)
1258 n = find_text_node (parent->children);
1259 if (n)
1260 xmlNodeSetContent (n, (xmlChar *) content);
1261 else
1262 xmlNodeAddContent (parent, (xmlChar *) content);
1264 update_element_mtime (parent);
1268 xfree (content);
1269 strv_free (req);
1270 return send_error (ctx, rc);
1273 static gpg_error_t
1274 xfer_data (assuan_context_t ctx, const char *line, int total)
1276 int to_send;
1277 int sent = 0;
1278 gpg_error_t rc;
1279 int progress = config_get_integer ("global", "xfer_progress");
1280 int flush = 0;
1282 progress =
1283 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1284 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1285 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1287 if (rc)
1288 return rc;
1290 again:
1293 if (sent + to_send > total)
1294 to_send = total - sent;
1296 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1297 flush ? 0 : to_send);
1298 if (!rc)
1300 sent += flush ? 0 : to_send;
1302 if ((progress && !(sent % progress) && sent != total) ||
1303 (sent == total && flush))
1304 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1306 if (!flush && !rc && sent == total)
1308 flush = 1;
1309 goto again;
1313 while (!rc && sent < total);
1315 return rc;
1318 static gpg_error_t
1319 do_get (assuan_context_t ctx, char *line)
1321 struct client_s *client = assuan_get_pointer (ctx);
1322 gpg_error_t rc;
1323 char **req;
1324 xmlNodePtr n;
1326 req = str_split (line, "\t", 0);
1328 if (!req || !*req)
1330 strv_free (req);
1331 return GPG_ERR_SYNTAX;
1334 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1335 if (!n)
1337 strv_free (req);
1338 return rc;
1341 if (req[1])
1343 find_elements (client, client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1344 0, 0, NULL, 0);
1346 strv_free (req);
1347 if (rc)
1348 return rc;
1350 if (!n || !n->children)
1351 return GPG_ERR_NO_DATA;
1353 n = find_text_node (n->children);
1354 if (!n || !n->content || !*n->content)
1355 return GPG_ERR_NO_DATA;
1357 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1358 return rc;
1361 static gpg_error_t
1362 get_command (assuan_context_t ctx, char *line)
1364 struct client_s *client = assuan_get_pointer (ctx);
1365 gpg_error_t rc;
1366 struct argv_s *args[] = {
1367 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1368 NULL
1371 rc = parse_options (&line, args, client);
1372 if (rc)
1373 return send_error (ctx, rc);
1375 if (client->opts & OPT_INQUIRE)
1377 unsigned char *result;
1378 size_t len;
1380 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1381 if (rc)
1382 return send_error (ctx, rc);
1384 line = (char *) result;
1387 rc = do_get (ctx, line);
1389 if (client->opts & OPT_INQUIRE)
1390 xfree (line);
1392 return send_error (ctx, rc);
1395 static void list_command_cleanup1 (void *arg);
1396 static gpg_error_t
1397 realpath_command (assuan_context_t ctx, char *line)
1399 struct string_s *string = NULL;
1400 gpg_error_t rc;
1401 struct client_s *client = assuan_get_pointer (ctx);
1402 struct argv_s *args[] = {
1403 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1404 NULL
1407 rc = parse_options (&line, args, client);
1408 if (rc)
1409 return send_error (ctx, rc);
1411 if (client->opts & OPT_INQUIRE)
1413 unsigned char *result;
1414 size_t len;
1416 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1417 if (rc)
1418 return send_error (ctx, rc);
1420 line = (char *) result;
1423 rc = build_realpath (client, client->doc, line, &string);
1424 if (!rc)
1426 pthread_cleanup_push (list_command_cleanup1, string);
1427 rc = xfer_data (ctx, string->str, string->len);
1428 pthread_cleanup_pop (1);
1431 if (client->opts & OPT_INQUIRE)
1432 xfree (line);
1434 return send_error (ctx, rc);
1437 static void
1438 list_command_cleanup1 (void *arg)
1440 if (arg)
1441 string_free ((struct string_s *) arg, 1);
1444 static void
1445 list_command_cleanup2 (void *arg)
1447 struct element_list_s *elements = arg;
1449 if (elements)
1451 if (elements->list)
1453 int total = slist_length (elements->list);
1454 int i;
1456 for (i = 0; i < total; i++)
1458 char *tmp = slist_nth_data (elements->list, i);
1459 xfree (tmp);
1462 slist_free (elements->list);
1465 if (elements->prefix)
1466 xfree (elements->prefix);
1468 if (elements->req)
1469 strv_free (elements->req);
1471 xfree (elements);
1475 static gpg_error_t
1476 parse_list_opt_norecurse (void *data, void *value)
1478 struct client_s *client = data;
1480 client->opts &= ~(OPT_LIST_RECURSE);
1481 return 0;
1484 static gpg_error_t
1485 parse_list_opt_verbose (void *data, void *value)
1487 struct client_s *client = data;
1489 client->opts |= OPT_LIST_VERBOSE;
1490 return 0;
1493 static gpg_error_t
1494 parse_list_opt_target (void *data, void *value)
1496 struct client_s *client = data;
1498 client->opts |= OPT_LIST_WITH_TARGET;
1499 return 0;
1502 static gpg_error_t
1503 parse_list_opt_all (void *data, void *value)
1505 struct client_s *client = data;
1507 client->opts |= OPT_LIST_ALL;
1508 return 0;
1511 static gpg_error_t
1512 list_path_once (struct client_s *client, char *line,
1513 struct element_list_s *elements, struct string_s *result)
1515 gpg_error_t rc;
1517 elements->req = str_split (line, " ", 0);
1518 if (!elements->req)
1519 strv_printf (&elements->req, "%s", line);
1521 rc = create_path_list (client, client->doc, elements, *elements->req);
1522 if ((rc == GPG_ERR_ELOOP || rc == GPG_ERR_EPERM) && elements->verbose)
1523 rc = 0;
1525 if (!rc)
1527 int total = slist_length (elements->list);
1529 if (!total)
1530 rc = GPG_ERR_NO_DATA;
1532 if (!rc)
1534 if (!rc)
1536 int i;
1538 for (i = 0; i < total; i++)
1540 char *tmp = slist_nth_data (elements->list, i);
1542 string_append_printf (result, "%s%s", tmp,
1543 i + 1 == total ? "" : "\n");
1547 else
1548 rc = GPG_ERR_NO_DATA;
1551 return rc;
1554 static int
1555 has_list_flag (char *path, char *flags)
1557 char *p = path;
1559 while (*p && *++p != ' ');
1561 if (!*p)
1562 return 0;
1564 for (; *p; p++)
1566 char *f;
1568 for (f = flags; *f && *f != ' '; f++)
1570 if (*p == *f)
1571 return 1;
1575 return 0;
1578 static gpg_error_t
1579 do_list (assuan_context_t ctx, char *line)
1581 struct client_s *client = assuan_get_pointer (ctx);
1582 gpg_error_t rc;
1583 struct element_list_s *elements = NULL;
1585 elements = xcalloc (1, sizeof (struct element_list_s));
1586 if (!elements)
1587 return GPG_ERR_ENOMEM;
1589 elements->recurse = client->opts & OPT_LIST_RECURSE;
1590 elements->verbose =
1591 (client->opts & OPT_LIST_VERBOSE) | (client->opts & OPT_LIST_WITH_TARGET);
1592 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1594 if (!line || !*line)
1596 struct string_s *str = NULL;
1598 pthread_cleanup_push (list_command_cleanup2, elements);
1599 rc = list_root_elements (client, client->doc, &str, elements->verbose,
1600 elements->with_target);
1601 pthread_cleanup_pop (1);
1602 pthread_cleanup_push (list_command_cleanup1, str);
1604 if (!rc)
1606 if (client->opts & OPT_LIST_ALL)
1608 char **roots = str_split (str->str, "\n", 0);
1609 char **p;
1611 pthread_cleanup_push (req_cleanup, roots);
1612 string_truncate (str, 0);
1614 for (p = roots; *p; p++)
1616 if (strchr (*p, ' '))
1618 if (has_list_flag (*p, "EOP"))
1620 string_append_printf (str, "%s%s", *p,
1621 *(p + 1) ? "\n" : "");
1622 continue;
1626 elements = xcalloc (1, sizeof (struct element_list_s));
1627 if (!elements)
1629 rc = GPG_ERR_ENOMEM;
1630 break;
1633 elements->recurse = client->opts & OPT_LIST_RECURSE;
1634 elements->verbose =
1635 (client->opts & OPT_LIST_VERBOSE) | (client->opts &
1636 OPT_LIST_WITH_TARGET);
1637 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1638 pthread_cleanup_push (list_command_cleanup2, elements);
1639 rc = list_path_once (client, *p, elements, str);
1640 pthread_cleanup_pop (1);
1641 if (rc)
1642 break;
1644 if (*(p + 1))
1645 string_append (str, "\n");
1648 pthread_cleanup_pop (1);
1651 if (!rc)
1652 rc = xfer_data (ctx, str->str, str->len);
1655 pthread_cleanup_pop (1);
1656 return rc;
1659 pthread_cleanup_push (list_command_cleanup2, elements);
1660 struct string_s *str = string_new (NULL);
1661 pthread_cleanup_push (list_command_cleanup1, str);
1662 rc = list_path_once (client, line, elements, str);
1663 if (!rc)
1664 rc = xfer_data (ctx, str->str, str->len);
1666 pthread_cleanup_pop (1);
1667 pthread_cleanup_pop (1);
1668 return rc;
1671 static gpg_error_t
1672 list_command (assuan_context_t ctx, char *line)
1674 struct client_s *client = assuan_get_pointer (ctx);
1675 gpg_error_t rc;
1676 struct argv_s *args[] = {
1677 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG,
1678 parse_list_opt_norecurse},
1679 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_list_opt_verbose},
1680 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1681 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG,
1682 parse_list_opt_target},
1683 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_all},
1684 NULL
1687 if (disable_list_and_dump == 1)
1688 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1690 client->opts |= OPT_LIST_RECURSE;
1691 rc = parse_options (&line, args, client);
1692 if (rc)
1693 return send_error (ctx, rc);
1695 if (client->opts & OPT_LIST_ALL)
1696 client->opts |= OPT_LIST_RECURSE | OPT_LIST_VERBOSE;
1698 if (client->opts & OPT_INQUIRE)
1700 unsigned char *result;
1701 size_t len;
1703 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1704 if (rc)
1705 return send_error (ctx, rc);
1707 line = (char *) result;
1710 rc = do_list (ctx, line);
1712 if (client->opts & OPT_INQUIRE)
1713 xfree (line);
1715 return send_error (ctx, rc);
1719 * req[0] - element path
1721 static gpg_error_t
1722 attribute_list (assuan_context_t ctx, char **req)
1724 struct client_s *client = assuan_get_pointer (ctx);
1725 char **attrlist = NULL;
1726 int i = 0;
1727 char **path = NULL;
1728 xmlAttrPtr a;
1729 xmlNodePtr n, an;
1730 char *line;
1731 gpg_error_t rc;
1733 if (!req || !req[0])
1734 return GPG_ERR_SYNTAX;
1736 if ((path = str_split (req[0], "\t", 0)) == NULL)
1739 * The first argument may be only a root element.
1741 if ((path = str_split (req[0], " ", 0)) == NULL)
1742 return GPG_ERR_SYNTAX;
1745 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1747 if (!n)
1749 strv_free (path);
1750 return rc;
1753 if (path[1])
1755 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1756 NULL, NULL, NULL, 0, 0, NULL, 0);
1758 if (!n)
1760 strv_free (path);
1761 return rc;
1765 strv_free (path);
1767 for (a = n->properties; a; a = a->next)
1769 char **pa;
1771 if ((pa = xrealloc (attrlist, (i + 2) * sizeof (char *))) == NULL)
1773 if (attrlist)
1774 strv_free (attrlist);
1776 log_write ("%s(%i): %s", __FILE__, __LINE__,
1777 pwmd_strerror (GPG_ERR_ENOMEM));
1778 return GPG_ERR_ENOMEM;
1781 attrlist = pa;
1782 an = a->children;
1783 attrlist[i] = str_asprintf ("%s %s", (char *) a->name,
1785 && an->content ? (char *) an->content : "");
1787 if (!attrlist[i])
1789 strv_free (attrlist);
1790 log_write ("%s(%i): %s", __FILE__, __LINE__,
1791 pwmd_strerror (GPG_ERR_ENOMEM));
1792 return GPG_ERR_ENOMEM;
1795 attrlist[++i] = NULL;
1798 if (!attrlist)
1799 return GPG_ERR_NO_DATA;
1801 line = strv_join ("\n", attrlist);
1803 if (!line)
1805 log_write ("%s(%i): %s", __FILE__, __LINE__,
1806 pwmd_strerror (GPG_ERR_ENOMEM));
1807 strv_free (attrlist);
1808 return GPG_ERR_ENOMEM;
1811 pthread_cleanup_push (xfree, line);
1812 pthread_cleanup_push (req_cleanup, attrlist);
1813 rc = xfer_data (ctx, line, strlen (line));
1814 pthread_cleanup_pop (1);
1815 pthread_cleanup_pop (1);
1816 return rc;
1820 * req[0] - attribute
1821 * req[1] - element path
1823 static gpg_error_t
1824 attribute_delete (struct client_s *client, char **req)
1826 xmlNodePtr n;
1827 char **path = NULL;
1828 gpg_error_t rc;
1830 if (!req || !req[0] || !req[1])
1831 return GPG_ERR_SYNTAX;
1833 if (!strcmp (req[0], "_name"))
1834 return GPG_ERR_INV_ATTR;
1836 if ((path = str_split (req[1], "\t", 0)) == NULL)
1839 * The first argument may be only a root element.
1841 if ((path = str_split (req[1], " ", 0)) == NULL)
1842 return GPG_ERR_SYNTAX;
1845 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1846 if (!n)
1847 goto fail;
1849 if (path[1])
1851 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1852 NULL, NULL, NULL, 0, 0, NULL, 0);
1853 if (!n)
1854 goto fail;
1857 if (!strcmp (req[0], (char *) "_acl"))
1859 rc = peer_is_invoker (client);
1860 if (rc == GPG_ERR_EPERM)
1861 rc = is_element_owner (client, n);
1863 if (rc)
1864 goto fail;
1867 rc = delete_attribute (client, n, (xmlChar *) req[0]);
1869 fail:
1870 strv_free (path);
1871 return rc;
1874 static xmlNodePtr
1875 create_element_path (struct client_s *client,
1876 char ***elements, gpg_error_t * rc, xmlNodePtr parent)
1878 char **req = *elements;
1879 char **req_orig = strv_dup (req);
1880 xmlNodePtr n = NULL;
1882 *rc = 0;
1884 if (!req_orig)
1886 *rc = GPG_ERR_ENOMEM;
1887 log_write ("%s(%i): %s", __FILE__, __LINE__,
1888 pwmd_strerror (GPG_ERR_ENOMEM));
1889 goto fail;
1892 again:
1893 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
1894 if (!n)
1896 if (*rc != GPG_ERR_ELEMENT_NOT_FOUND)
1897 goto fail;
1899 *rc = new_root_element (client, client->doc, req[0]);
1900 if (*rc)
1901 goto fail;
1903 goto again;
1905 else if (n == parent)
1907 *rc = GPG_ERR_CONFLICT;
1908 goto fail;
1911 if (req[1])
1913 if (!n->children)
1914 n = create_target_elements_cb (client, 1, n, req + 1, rc, NULL);
1915 else
1916 n = find_elements (client, client->doc, n->children, req + 1, rc,
1917 NULL, NULL, create_target_elements_cb, 0, 0,
1918 parent, 0);
1920 if (!n)
1921 goto fail;
1924 * Reset the position of the element tree now that the elements
1925 * have been created.
1927 strv_free (req);
1928 req = req_orig;
1929 req_orig = NULL;
1930 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
1931 if (!n)
1932 goto fail;
1934 n = find_elements (client, client->doc, n->children, req + 1, rc,
1935 NULL, NULL, NULL, 0, 0, NULL, 0);
1936 if (!n)
1937 goto fail;
1940 fail:
1941 if (req_orig)
1942 strv_free (req_orig);
1944 *elements = req;
1945 return n;
1949 * Creates a "target" attribute. When other commands encounter an element with
1950 * this attribute, the element path is modified to the target value. If the
1951 * source element path doesn't exist when using 'ATTR SET target', it is
1952 * created, but the destination element path must exist.
1954 * req[0] - source element path
1955 * req[1] - destination element path
1957 static gpg_error_t
1958 target_attribute (struct client_s *client, char **req)
1960 char **src, **dst, *line = NULL, **odst = NULL;
1961 gpg_error_t rc;
1962 xmlNodePtr n;
1964 if (!req || !req[0] || !req[1])
1965 return GPG_ERR_SYNTAX;
1967 if ((src = str_split (req[0], "\t", 0)) == NULL)
1970 * The first argument may be only a root element.
1972 if ((src = str_split (req[0], " ", 0)) == NULL)
1973 return GPG_ERR_SYNTAX;
1976 if (!valid_element_path (src, 0))
1977 return GPG_ERR_INV_VALUE;
1979 if ((dst = str_split (req[1], "\t", 0)) == NULL)
1982 * The first argument may be only a root element.
1984 if ((dst = str_split (req[1], " ", 0)) == NULL)
1986 rc = GPG_ERR_SYNTAX;
1987 goto fail;
1991 odst = strv_dup (dst);
1992 if (!odst)
1994 rc = GPG_ERR_ENOMEM;
1995 goto fail;
1998 n = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
2000 * Make sure the destination element path exists.
2002 if (!n)
2003 goto fail;
2005 if (dst[1])
2007 n = find_elements (client, client->doc, n->children, dst + 1, &rc,
2008 NULL, NULL, NULL, 0, 0, NULL, 0);
2009 if (!n)
2010 goto fail;
2013 rc = validate_target_attribute (client, client->doc, req[0], n);
2014 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2015 goto fail;
2017 n = create_element_path (client, &src, &rc, NULL);
2018 if (rc)
2019 goto fail;
2021 line = strv_join ("\t", odst);
2022 if (!line)
2024 rc = GPG_ERR_ENOMEM;
2025 goto fail;
2028 rc = add_attribute (n, "target", line);
2030 fail:
2031 xfree (line);
2032 strv_free (src);
2033 strv_free (dst);
2034 strv_free (odst);
2035 return rc;
2039 * req[0] - attribute
2040 * req[1] - element path
2042 static gpg_error_t
2043 attribute_get (assuan_context_t ctx, char **req)
2045 struct client_s *client = assuan_get_pointer (ctx);
2046 xmlNodePtr n;
2047 xmlChar *a;
2048 char **path = NULL;
2049 gpg_error_t rc;
2051 if (!req || !req[0] || !req[1])
2052 return GPG_ERR_SYNTAX;
2054 if (strchr (req[1], '\t'))
2056 if ((path = str_split (req[1], "\t", 0)) == NULL)
2057 return GPG_ERR_SYNTAX;
2059 else
2061 if ((path = str_split (req[1], " ", 0)) == NULL)
2062 return GPG_ERR_SYNTAX;
2065 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2067 if (!n)
2068 goto fail;
2070 if (path[1])
2072 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2073 NULL, NULL, NULL, 0, 0, NULL, 0);
2075 if (!n)
2076 goto fail;
2079 strv_free (path);
2081 if ((a = xmlGetProp (n, (xmlChar *) req[0])) == NULL)
2082 return GPG_ERR_NOT_FOUND;
2084 pthread_cleanup_push (xmlFree, a);
2086 if (*a)
2087 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
2088 else
2089 rc = GPG_ERR_NO_DATA;
2091 pthread_cleanup_pop (1);
2092 return rc;
2094 fail:
2095 strv_free (path);
2096 return rc;
2100 * req[0] - attribute
2101 * req[1] - element path
2102 * req[2] - value
2104 static gpg_error_t
2105 attribute_set (struct client_s *client, char **req)
2107 char **path = NULL;
2108 gpg_error_t rc;
2109 xmlNodePtr n;
2111 if (!req || !req[0] || !req[1])
2112 return GPG_ERR_SYNTAX;
2115 * Reserved attribute names.
2117 if (!strcmp (req[0], "_name"))
2118 return GPG_ERR_INV_ATTR;
2119 else if (!strcmp (req[0], "target"))
2120 return target_attribute (client, req + 1);
2121 else if (!valid_xml_attribute (req[0]))
2122 return GPG_ERR_INV_VALUE;
2124 if ((path = str_split (req[1], "\t", 0)) == NULL)
2127 * The first argument may be only a root element.
2129 if ((path = str_split (req[1], " ", 0)) == NULL)
2130 return GPG_ERR_SYNTAX;
2133 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2135 if (!n)
2136 goto fail;
2138 if (path[1])
2140 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2141 NULL, NULL, NULL, 0, 0, NULL, 0);
2143 if (!n)
2144 goto fail;
2147 if (!strcmp (req[0], (char *) "_acl"))
2149 rc = peer_is_invoker (client);
2150 if (rc == GPG_ERR_EPERM)
2151 rc = is_element_owner (client, n);
2153 if (rc)
2154 goto fail;
2157 rc = add_attribute (n, req[0], req[2]);
2159 fail:
2160 strv_free (path);
2161 return rc;
2165 * req[0] - command
2166 * req[1] - attribute name or element path if command is LIST
2167 * req[2] - element path
2168 * req[2] - element path or value
2171 static gpg_error_t
2172 do_attr (assuan_context_t ctx, char *line)
2174 struct client_s *client = assuan_get_pointer (ctx);
2175 gpg_error_t rc = 0;
2176 char **req;
2178 req = str_split (line, " ", 4);
2179 if (!req || !req[0] || !req[1])
2181 strv_free (req);
2182 return GPG_ERR_SYNTAX;
2185 pthread_cleanup_push (req_cleanup, req);
2187 if (strcasecmp (req[0], "SET") == 0)
2188 rc = attribute_set (client, req + 1);
2189 else if (strcasecmp (req[0], "GET") == 0)
2190 rc = attribute_get (ctx, req + 1);
2191 else if (strcasecmp (req[0], "DELETE") == 0)
2192 rc = attribute_delete (client, req + 1);
2193 else if (strcasecmp (req[0], "LIST") == 0)
2194 rc = attribute_list (ctx, req + 1);
2195 else
2196 rc = GPG_ERR_SYNTAX;
2198 pthread_cleanup_pop (1);
2199 return rc;
2202 static gpg_error_t
2203 attr_command (assuan_context_t ctx, char *line)
2205 struct client_s *client = assuan_get_pointer (ctx);
2206 gpg_error_t rc;
2207 struct argv_s *args[] = {
2208 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2209 NULL
2212 rc = parse_options (&line, args, client);
2213 if (rc)
2214 return send_error (ctx, rc);
2216 if (client->opts & OPT_INQUIRE)
2218 unsigned char *result;
2219 size_t len;
2221 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2222 if (rc)
2223 return send_error (ctx, rc);
2225 line = (char *) result;
2228 rc = do_attr (ctx, line);
2230 if (client->opts & OPT_INQUIRE)
2231 xfree (line);
2233 return send_error (ctx, rc);
2236 static gpg_error_t
2237 parse_iscached_opt_lock (void *data, void *value)
2239 struct client_s *client = data;
2241 (void) value;
2242 client->opts |= OPT_LOCK;
2243 return 0;
2246 static gpg_error_t
2247 iscached_command (assuan_context_t ctx, char *line)
2249 struct client_s *client = assuan_get_pointer (ctx);
2250 gpg_error_t rc;
2251 struct argv_s *args[] = {
2252 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2253 NULL
2256 if (!line || !*line)
2257 return send_error (ctx, GPG_ERR_SYNTAX);
2259 rc = parse_options (&line, args, client);
2260 if (rc)
2261 return send_error (ctx, rc);
2262 else if (!valid_filename (line))
2263 return send_error (ctx, GPG_ERR_INV_VALUE);
2265 rc = cache_iscached (line, NULL);
2266 if (client->opts & OPT_LOCK
2267 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2269 unsigned char md5file[16];
2270 gpg_error_t trc = rc;
2272 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2273 if (memcmp (md5file, client->md5file, 16))
2274 cleanup_client (client);
2276 memcpy (client->md5file, md5file, 16);
2277 rc = do_lock (client, 1);
2278 if (!rc)
2279 rc = trc;
2282 return send_error (ctx, rc);
2285 static gpg_error_t
2286 clearcache_command (assuan_context_t ctx, char *line)
2288 gpg_error_t rc = 0, all_rc = 0;
2289 unsigned char md5file[16];
2290 int i;
2291 int t;
2292 int all = 0;
2293 struct client_thread_s *once = NULL;
2295 cache_lock ();
2296 MUTEX_LOCK (&cn_mutex);
2297 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
2299 if (!line || !*line)
2300 all = 1;
2302 t = slist_length (cn_thread_list);
2304 for (i = 0; i < t; i++)
2306 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2307 assuan_peercred_t peer;
2309 if (!thd->cl)
2310 continue;
2312 /* Lock each connected clients' file mutex to prevent any other client
2313 * from accessing the cache entry (the file mutex is locked upon
2314 * command startup). The cache for the entry is not cleared if the
2315 * file mutex is locked by another client to prevent this function
2316 * from blocking.
2318 if (all)
2320 if (thd->cl->filename)
2322 rc = do_validate_peer (ctx, thd->cl->filename, &peer);
2323 all_rc = !all_rc ? rc : all_rc;
2324 if (rc)
2326 rc = 0;
2327 continue;
2331 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
2332 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2334 if (pthread_equal (pthread_self (), thd->tid))
2335 rc = 0;
2336 else
2338 if (!thd->cl->filename ||
2339 cache_iscached (thd->cl->filename,
2340 NULL) == GPG_ERR_NO_DATA)
2342 rc = 0;
2343 continue;
2346 cache_defer_clear (thd->cl->md5file);
2349 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2351 rc = 0;
2352 continue;
2355 if (!rc)
2357 rc = cache_clear (thd->cl->md5file);
2358 cache_unlock_mutex (thd->cl->md5file, 0);
2361 if (rc)
2362 all_rc = rc;
2364 rc = 0;
2366 /* A single data filename was specified. Lock only this data file
2367 * mutex and free the cache entry. */
2368 else
2370 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2371 rc = do_validate_peer (ctx, line, &peer);
2373 if (!rc && !memcmp (thd->cl->md5file, md5file, sizeof (md5file)))
2375 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0,
2376 -1);
2377 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2379 if (pthread_equal (pthread_self (), thd->tid))
2380 rc = 0;
2383 if (!rc)
2385 once = thd;
2386 rc = cache_clear (thd->cl->md5file);
2387 cache_unlock_mutex (thd->cl->md5file, 0);
2389 else
2391 cache_defer_clear (thd->cl->md5file);
2394 break;
2399 /* Only connected clients' cache entries have been cleared. Now clear any
2400 * remaining cache entries without clients but only if there wasn't an
2401 * error from above since this would defeat the locking check of the
2402 * remaining entries. */
2403 if (!all_rc && all)
2405 cache_clear (NULL);
2408 /* No clients are using the specified file. */
2409 else if (!all_rc && !rc && !once)
2411 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2412 rc = cache_clear (md5file);
2415 /* Release the connection mutex. */
2416 pthread_cleanup_pop (1);
2417 cache_unlock ();
2419 if (!rc)
2420 send_status_all (STATUS_CACHE, NULL);
2422 /* One or more files were locked while clearing all cache entries. */
2423 if (all_rc)
2424 rc = all_rc;
2426 return send_error (ctx, rc);
2429 static gpg_error_t
2430 cachetimeout_command (assuan_context_t ctx, char *line)
2432 int timeout;
2433 char **req = str_split (line, " ", 0);
2434 char *p;
2435 gpg_error_t rc = 0;
2436 assuan_peercred_t peer;
2438 if (!req || !*req || !req[1])
2440 strv_free (req);
2441 return send_error (ctx, GPG_ERR_SYNTAX);
2444 errno = 0;
2445 timeout = (int) strtol (req[1], &p, 10);
2446 if (errno != 0 || *p || timeout < -1)
2448 strv_free (req);
2449 return send_error (ctx, GPG_ERR_SYNTAX);
2452 rc = do_validate_peer (ctx, req[0], &peer);
2453 if (!rc)
2455 unsigned char md5file[16];
2457 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, req[0], strlen (req[0]));
2458 rc = cache_set_timeout (md5file, timeout);
2459 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2461 rc = 0;
2462 MUTEX_LOCK (&rcfile_mutex);
2463 config_set_int_param (&global_config, req[0], "cache_timeout",
2464 req[1]);
2465 MUTEX_UNLOCK (&rcfile_mutex);
2469 strv_free (req);
2470 return send_error (ctx, rc);
2473 static gpg_error_t
2474 dump_command (assuan_context_t ctx, char *line)
2476 xmlChar *xml;
2477 int len;
2478 struct client_s *client = assuan_get_pointer (ctx);
2479 gpg_error_t rc;
2481 if (disable_list_and_dump == 1)
2482 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2484 rc = peer_is_invoker(client);
2485 if (rc)
2486 return send_error (ctx, rc);
2488 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2490 if (!xml)
2492 log_write ("%s(%i): %s", __FILE__, __LINE__,
2493 pwmd_strerror (GPG_ERR_ENOMEM));
2494 return send_error (ctx, GPG_ERR_ENOMEM);
2497 pthread_cleanup_push (xmlFree, xml);
2498 rc = xfer_data (ctx, (char *) xml, len);
2499 pthread_cleanup_pop (1);
2500 return send_error (ctx, rc);
2503 static gpg_error_t
2504 getconfig_command (assuan_context_t ctx, char *line)
2506 struct client_s *client = assuan_get_pointer (ctx);
2507 gpg_error_t rc = 0;
2508 char filename[255] = { 0 }, param[747] = { 0 };
2509 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2511 if (!line || !*line)
2512 return send_error (ctx, GPG_ERR_SYNTAX);
2514 if (strchr (line, ' '))
2516 sscanf (line, " %254[^ ] %746c", filename, param);
2517 paramp = param;
2518 fp = filename;
2521 if (fp && !valid_filename (fp))
2522 return send_error (ctx, GPG_ERR_INV_VALUE);
2524 paramp = str_down (paramp);
2525 if (!strcmp (paramp, "cipher") && fp)
2527 struct crypto_s *crypto = NULL;
2529 rc = init_client_crypto (&crypto);
2530 if (!rc)
2532 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2533 if (!rc)
2535 const char *t =
2536 gcry_cipher_algo_name (cipher_to_gcrypt (crypto->hdr.flags));
2537 if (t)
2539 tmp = str_dup (t);
2540 if (tmp)
2541 str_down (tmp);
2546 UPDATE_AGENT_CTX (client, crypto);
2547 cleanup_crypto (&crypto);
2548 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2549 return send_error (ctx, rc);
2551 if (!rc && tmp)
2552 goto done;
2554 else if (!strcmp (paramp, "cipher_iterations") && fp)
2556 struct crypto_s *crypto = NULL;
2558 rc = init_client_crypto (&crypto);
2559 if (!rc)
2561 rc = read_data_header (fp, &crypto->hdr, NULL, NULL);
2562 if (!rc)
2564 tmp = str_asprintf ("%llu",
2565 (unsigned long long) crypto->hdr.
2566 iterations);
2567 if (!tmp)
2568 rc = GPG_ERR_ENOMEM;
2572 UPDATE_AGENT_CTX (client, crypto);
2573 cleanup_crypto (&crypto);
2574 if (rc && gpg_err_code (rc) != GPG_ERR_ENOENT)
2575 return send_error (ctx, rc);
2577 if (!rc && tmp)
2578 goto done;
2580 else if (!strcmp (paramp, "passphrase"))
2581 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2583 p = config_get_value (fp ? fp : "global", paramp);
2584 if (!p)
2585 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2587 tmp = expand_homedir (p);
2588 xfree (p);
2589 if (!tmp)
2591 log_write ("%s(%i): %s", __FILE__, __LINE__,
2592 pwmd_strerror (GPG_ERR_ENOMEM));
2593 return send_error (ctx, GPG_ERR_ENOMEM);
2596 done:
2597 p = tmp;
2598 pthread_cleanup_push (xfree, p);
2599 rc = xfer_data (ctx, p, strlen (p));
2600 pthread_cleanup_pop (1);
2601 return send_error (ctx, rc);
2604 struct xpath_s
2606 xmlXPathContextPtr xp;
2607 xmlXPathObjectPtr result;
2608 xmlBufferPtr buf;
2609 char **req;
2612 static void
2613 xpath_command_cleanup (void *arg)
2615 struct xpath_s *xpath = arg;
2617 if (!xpath)
2618 return;
2620 req_cleanup (xpath->req);
2622 if (xpath->buf)
2623 xmlBufferFree (xpath->buf);
2625 if (xpath->result)
2626 xmlXPathFreeObject (xpath->result);
2628 if (xpath->xp)
2629 xmlXPathFreeContext (xpath->xp);
2632 static gpg_error_t
2633 do_xpath (assuan_context_t ctx, char *line)
2635 gpg_error_t rc;
2636 struct client_s *client = assuan_get_pointer (ctx);
2637 struct xpath_s _x = { 0 };
2638 struct xpath_s *xpath = &_x;
2640 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2642 if (strv_printf (&xpath->req, "%s", line) == 0)
2643 return GPG_ERR_ENOMEM;
2646 xpath->xp = xmlXPathNewContext (client->doc);
2647 if (!xpath->xp)
2649 rc = GPG_ERR_BAD_DATA;
2650 goto fail;
2653 xpath->result =
2654 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2655 if (!xpath->result)
2657 rc = GPG_ERR_BAD_DATA;
2658 goto fail;
2661 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2663 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2664 goto fail;
2667 rc = recurse_xpath_nodeset (client, client->doc, xpath->result->nodesetval,
2668 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2669 NULL);
2670 if (rc)
2671 goto fail;
2672 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2674 rc = GPG_ERR_NO_DATA;
2675 goto fail;
2677 else if (xpath->req[1])
2679 rc = 0;
2680 goto fail;
2683 pthread_cleanup_push (xpath_command_cleanup, &xpath);
2684 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2685 xmlBufferLength (xpath->buf));
2686 pthread_cleanup_pop (0);
2687 fail:
2688 xpath_command_cleanup (xpath);
2689 return rc;
2692 static gpg_error_t
2693 xpath_command (assuan_context_t ctx, char *line)
2695 struct client_s *client = assuan_get_pointer (ctx);
2696 gpg_error_t rc;
2697 struct argv_s *args[] = {
2698 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2699 NULL
2702 if (disable_list_and_dump == 1)
2703 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2705 rc = peer_is_invoker(client);
2706 if (rc)
2707 return send_error (ctx, rc);
2709 rc = parse_options (&line, args, client);
2710 if (rc)
2711 return send_error (ctx, rc);
2713 if (client->opts & OPT_INQUIRE)
2715 unsigned char *result;
2716 size_t len;
2718 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2719 if (rc)
2720 return send_error (ctx, rc);
2722 line = (char *) result;
2725 if (!line || !*line)
2726 rc = GPG_ERR_SYNTAX;
2728 if (!rc)
2729 rc = do_xpath (ctx, line);
2731 if (client->opts & OPT_INQUIRE)
2732 xfree (line);
2734 return send_error (ctx, rc);
2737 static gpg_error_t
2738 do_xpathattr (assuan_context_t ctx, char *line)
2740 struct client_s *client = assuan_get_pointer (ctx);
2741 gpg_error_t rc;
2742 char **req = NULL;
2743 int cmd = 0; //SET
2744 struct xpath_s _x = { 0 };
2745 struct xpath_s *xpath = &_x;
2747 if ((req = str_split (line, " ", 3)) == NULL)
2748 return GPG_ERR_ENOMEM;
2750 if (!req[0])
2752 rc = GPG_ERR_SYNTAX;
2753 goto fail;
2756 if (!strcasecmp (req[0], "SET"))
2757 cmd = 0;
2758 else if (!strcasecmp (req[0], "DELETE"))
2759 cmd = 1;
2760 else
2762 rc = GPG_ERR_SYNTAX;
2763 goto fail;
2766 if (!req[1] || !req[2])
2768 rc = GPG_ERR_SYNTAX;
2769 goto fail;
2772 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2774 rc = GPG_ERR_ENOMEM;
2775 goto fail;
2778 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2780 rc = GPG_ERR_SYNTAX;
2781 goto fail;
2784 xpath->xp = xmlXPathNewContext (client->doc);
2785 if (!xpath->xp)
2787 rc = GPG_ERR_BAD_DATA;
2788 goto fail;
2791 xpath->result = xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2792 if (!xpath->result)
2794 rc = GPG_ERR_BAD_DATA;
2795 goto fail;
2798 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2800 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2801 goto fail;
2804 rc = recurse_xpath_nodeset (client, client->doc, xpath->result->nodesetval,
2805 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2806 (xmlChar *) req[1]);
2808 fail:
2809 xpath_command_cleanup (xpath);
2810 strv_free (req);
2811 return rc;
2814 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2815 static gpg_error_t
2816 xpathattr_command (assuan_context_t ctx, char *line)
2818 struct client_s *client = assuan_get_pointer (ctx);
2819 gpg_error_t rc;
2820 struct argv_s *args[] = {
2821 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2822 NULL
2825 if (disable_list_and_dump == 1)
2826 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2828 rc = peer_is_invoker(client);
2829 if (rc)
2830 return send_error (ctx, rc);
2832 rc = parse_options (&line, args, client);
2833 if (rc)
2834 return send_error (ctx, rc);
2836 if (client->opts & OPT_INQUIRE)
2838 unsigned char *result;
2839 size_t len;
2841 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2842 if (rc)
2843 return send_error (ctx, rc);
2845 line = (char *) result;
2848 if (!line || !*line)
2849 rc = GPG_ERR_SYNTAX;
2851 if (!rc)
2852 rc = do_xpathattr (ctx, line);
2854 if (client->opts & OPT_INQUIRE)
2855 xfree (line);
2857 return send_error (ctx, rc);
2860 static gpg_error_t
2861 do_import (struct client_s *client, const char *root_element,
2862 unsigned char *content)
2864 char **dst_path = NULL;
2865 xmlDocPtr doc = NULL;
2866 xmlNodePtr n, root, copy;
2867 gpg_error_t rc;
2869 if (!content || !*content)
2871 xfree (content);
2872 return GPG_ERR_SYNTAX;
2875 if (root_element)
2876 dst_path = str_split (root_element, "\t", 0);
2878 if (dst_path && !valid_element_path (dst_path, 0))
2880 if (dst_path)
2881 strv_free (dst_path);
2883 return GPG_ERR_INV_VALUE;
2886 struct string_s *str = string_new_content ((char *)content);
2887 str = string_prepend (str, "<pwmd>");
2888 str = string_append (str, "</pwmd>");
2889 doc = xmlReadDoc ((xmlChar *) str->str, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2890 string_free (str, 1);
2891 if (!doc)
2893 rc = GPG_ERR_BAD_DATA;
2894 goto fail;
2897 root = xmlDocGetRootElement (doc);
2898 xmlNodePtr root_orig = root->children;
2899 root = root->children;
2900 rc = validate_import (root);
2901 if (rc)
2902 goto fail;
2906 again:
2907 if (dst_path)
2909 char **path = strv_dup (dst_path);
2910 if (!path)
2912 log_write ("%s(%i): %s", __FILE__, __LINE__,
2913 pwmd_strerror (GPG_ERR_ENOMEM));
2914 rc = GPG_ERR_ENOMEM;
2915 goto fail;
2918 xmlChar *a = xmlGetProp (root, (xmlChar *) "_name");
2919 if (!a)
2921 strv_free (path);
2922 rc = GPG_ERR_INV_VALUE;
2923 goto fail;
2926 if (strv_printf (&path, "%s", (char *) a) == 0)
2928 xmlFree (a);
2929 rc = GPG_ERR_ENOMEM;
2930 goto fail;
2933 xmlFree (a);
2934 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2935 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2937 strv_free (path);
2938 goto fail;
2941 if (!rc)
2943 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2944 NULL, NULL, NULL, 0, 0, NULL, 1);
2945 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2947 strv_free (path);
2948 goto fail;
2950 else if (!rc)
2952 xmlUnlinkNode (n);
2953 xmlFreeNode (n);
2954 strv_free (path);
2955 path = NULL;
2956 goto again;
2960 if (rc == GPG_ERR_ELEMENT_NOT_FOUND)
2962 n = create_element_path (client, &path, &rc, NULL);
2963 if (rc)
2964 goto fail;
2967 if (root->children)
2969 copy = xmlCopyNodeList (root->children);
2970 n = xmlAddChildList (n, copy);
2971 if (!n)
2972 rc = GPG_ERR_ENOMEM;
2975 strv_free (path);
2977 else
2979 char **path = NULL;
2981 /* Check if the content root element can create a DTD root element. */
2982 if (!xmlStrEqual ((xmlChar *) "element", root->name))
2984 rc = GPG_ERR_SYNTAX;
2985 goto fail;
2988 xmlChar *a;
2990 if ((a = xmlGetProp (root, (xmlChar *) "_name")) == NULL)
2992 rc = GPG_ERR_SYNTAX;
2993 goto fail;
2996 char *tmp = str_dup ((char *) a);
2997 xmlFree (a);
2998 int literal = is_literal_element (&tmp);
3000 if (!valid_xml_element ((xmlChar *) tmp) || literal)
3002 xfree (tmp);
3003 rc = GPG_ERR_INV_VALUE;
3004 goto fail;
3007 if (strv_printf (&path, "%s", tmp) == 0)
3009 xfree (tmp);
3010 rc = GPG_ERR_ENOMEM;
3011 goto fail;
3014 xfree (tmp);
3015 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 1);
3016 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3018 rc = GPG_ERR_BAD_DATA;
3019 goto fail;
3022 /* Overwriting the existing tree. */
3023 if (!rc)
3025 xmlUnlinkNode (n);
3026 xmlFreeNodeList (n);
3029 rc = 0;
3030 xmlSetProp (root, (xmlChar *) "_name", (xmlChar *) path[0]);
3031 n = xmlCopyNode (root, 1);
3032 n = xmlAddChildList (xmlDocGetRootElement (client->doc), n);
3033 strv_free (path);
3036 if (n && !rc)
3037 rc = update_element_mtime (n->parent);
3039 for (root = root_orig->next; root; root = root->next)
3041 if (root->type == XML_ELEMENT_NODE)
3042 break;
3045 root_orig = root;
3047 while (root);
3049 fail:
3050 if (doc)
3051 xmlFreeDoc (doc);
3053 if (dst_path)
3054 strv_free (dst_path);
3056 return rc;
3059 static gpg_error_t
3060 parse_import_opt_root (void *data, void *value)
3062 struct client_s *client = data;
3064 client->import_root = str_dup (value);
3065 return client->import_root ? 0 : GPG_ERR_ENOMEM;
3068 static gpg_error_t
3069 import_command (assuan_context_t ctx, char *line)
3071 gpg_error_t rc;
3072 struct client_s *client = assuan_get_pointer (ctx);
3073 unsigned char *result;
3074 size_t len;
3075 struct argv_s *args[] = {
3076 &(struct argv_s) {"root", OPTION_TYPE_ARG, parse_import_opt_root},
3077 NULL
3080 xfree (client->import_root);
3081 client->import_root = NULL;
3082 rc = parse_options (&line, args, client);
3083 if (rc)
3084 return send_error (ctx, rc);
3086 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3087 if (rc)
3089 xfree (client->import_root);
3090 client->import_root = NULL;
3091 return send_error (ctx, rc);
3094 rc = do_import (client, client->import_root, result);
3095 xfree (client->import_root);
3096 client->import_root = NULL;
3097 return send_error (ctx, rc);
3100 static gpg_error_t
3101 do_lock (struct client_s *client, int add)
3103 gpg_error_t rc = lock_file_mutex (client, add);
3105 if (!rc)
3106 client->flags |= FLAG_LOCK_CMD;
3108 return rc;
3111 static gpg_error_t
3112 lock_command (assuan_context_t ctx, char *line)
3114 struct client_s *client = assuan_get_pointer (ctx);
3115 gpg_error_t rc = do_lock (client, 0);
3117 return send_error (ctx, rc);
3120 static gpg_error_t
3121 unlock_command (assuan_context_t ctx, char *line)
3123 struct client_s *client = assuan_get_pointer (ctx);
3124 gpg_error_t rc;
3126 rc = unlock_file_mutex (client, 0);
3127 return send_error (ctx, rc);
3130 static gpg_error_t
3131 option_command (assuan_context_t ctx, char *line)
3133 struct client_s *client = assuan_get_pointer (ctx);
3134 gpg_error_t rc = 0;
3135 struct pinentry_option_s *pin_opts = &client->pinentry_opts;
3136 #ifdef WITH_AGENT
3137 struct agent_s *agent = client->crypto->agent;
3138 #endif
3139 char namebuf[255] = { 0 };
3140 char *name = namebuf;
3141 char *value = NULL, *p;
3143 p = strchr (line, '=');
3144 if (!p)
3145 strncpy (namebuf, line, sizeof(namebuf));
3146 else
3148 strncpy (namebuf, line, strlen (line)-strlen (p));
3149 value = p+1;
3152 log_write1 ("OPTION name='%s' value='%s'", name, value);
3154 if (strcasecmp (name, (char *) "log_level") == 0)
3156 long l = 0;
3158 if (value)
3160 l = strtol (value, NULL, 10);
3162 if (l < 0 || l > 2)
3163 return send_error (ctx, GPG_ERR_INV_VALUE);
3166 MUTEX_LOCK (&rcfile_mutex);
3167 config_set_int_param (&global_config, "global", "log_level", value);
3168 MUTEX_UNLOCK (&rcfile_mutex);
3169 goto done;
3171 else if (strcasecmp (name, (char *) "lock-timeout") == 0)
3173 long n = 0;
3174 char *p = NULL;
3176 if (value)
3178 n = strtol (value, &p, 10);
3179 if (p && *p)
3180 return send_error (ctx, GPG_ERR_INV_VALUE);
3183 client->lock_timeout = n;
3184 goto done;
3186 else if (strcasecmp (name, (char *) "NAME") == 0)
3188 char *tmp = pthread_getspecific (thread_name_key);
3190 if (tmp)
3191 xfree (tmp);
3193 if (!value || !*value)
3195 pthread_setspecific (thread_name_key, str_dup (""));
3196 goto done;
3199 pthread_setspecific (thread_name_key, str_dup (value));
3200 goto done;
3202 else if (strcasecmp (name, (char *) "lc-messages") == 0)
3204 xfree (pin_opts->lc_messages);
3205 pin_opts->lc_messages = NULL;
3206 if (value && *value)
3207 pin_opts->lc_messages = str_dup (value);
3208 #ifdef WITH_AGENT
3209 if (use_agent)
3210 rc = set_agent_option (client->crypto->agent, "lc-messages", value);
3211 #endif
3213 else if (strcasecmp (name, (char *) "lc-ctype") == 0)
3215 xfree (pin_opts->lc_ctype);
3216 pin_opts->lc_ctype = NULL;
3217 if (value && *value)
3218 pin_opts->lc_ctype = str_dup (value);
3219 #ifdef WITH_AGENT
3220 if (use_agent)
3221 rc = set_agent_option (client->crypto->agent, "lc-ctype", value);
3222 #endif
3224 else if (strcasecmp (name, (char *) "ttyname") == 0)
3226 xfree (pin_opts->ttyname);
3227 pin_opts->ttyname = NULL;
3228 if (value && *value)
3229 pin_opts->ttyname = str_dup (value);
3230 #ifdef WITH_AGENT
3231 if (use_agent)
3232 rc = set_agent_option (client->crypto->agent, "ttyname", value);
3233 #endif
3235 else if (strcasecmp (name, (char *) "ttytype") == 0)
3237 xfree (pin_opts->ttytype);
3238 pin_opts->ttytype = NULL;
3239 if (value && *value)
3240 pin_opts->ttytype = str_dup (value);
3241 #ifdef WITH_AGENT
3242 if (use_agent)
3243 rc = set_agent_option (client->crypto->agent, "ttytype", value);
3244 #endif
3246 else if (strcasecmp (name, (char *) "display") == 0)
3248 xfree (pin_opts->display);
3249 pin_opts->display = NULL;
3250 if (value && *value)
3251 pin_opts->display = str_dup (value);
3252 #ifdef WITH_AGENT
3253 if (use_agent)
3254 rc = set_agent_option (client->crypto->agent, "display", value);
3255 #endif
3257 else if (strcasecmp (name, (char *) "pinentry-desc") == 0)
3259 xfree (pin_opts->desc);
3260 pin_opts->desc = NULL;
3261 if (value && *value)
3262 pin_opts->desc = str_dup (value);
3264 else if (strcasecmp (name, (char *) "pinentry-title") == 0)
3266 xfree (pin_opts->title);
3267 pin_opts->title = NULL;
3268 if (value && *value)
3269 pin_opts->title = str_dup (value);
3271 else if (strcasecmp (name, (char *) "pinentry-prompt") == 0)
3273 xfree (pin_opts->prompt);
3274 pin_opts->prompt = NULL;
3275 if (value && *value)
3276 pin_opts->prompt = str_dup (value);
3279 else if (strcasecmp (name, "pinentry-timeout") == 0)
3281 char *p = NULL;
3282 int n;
3284 if (!value)
3285 goto done;
3287 n = (int) strtol (value, &p, 10);
3289 if (*p || n < 0)
3290 return send_error (ctx, GPG_ERR_INV_VALUE);
3292 pin_opts->timeout = n;
3293 MUTEX_LOCK (&rcfile_mutex);
3294 config_set_int_param (&global_config,
3295 client->filename ? client->filename : "global",
3296 "pinentry_timeout", value);
3297 MUTEX_UNLOCK (&rcfile_mutex);
3298 goto done;
3300 else if (strcasecmp (name, "disable-pinentry") == 0)
3302 char *p = NULL;
3303 int n = 1;
3305 if (value && *value)
3307 n = (int) strtol (value, &p, 10);
3308 if (*p || n < 0 || n > 1)
3309 return send_error (ctx, GPG_ERR_INV_VALUE);
3312 if (n)
3313 client->flags |= FLAG_NO_PINENTRY;
3314 else
3315 client->flags &= ~FLAG_NO_PINENTRY;
3317 #ifdef WITH_AGENT
3318 if (use_agent)
3320 if (client->flags & FLAG_NO_PINENTRY)
3321 rc = set_agent_option (client->crypto->agent, "pinentry-mode",
3322 "loopback");
3323 else
3324 rc = set_agent_option (client->crypto->agent, "pinentry-mode",
3325 "ask");
3327 if (rc)
3328 return send_error (ctx, rc);
3330 #endif
3332 else
3333 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
3335 done:
3336 #ifdef WITH_AGENT
3337 if (!rc && use_agent && agent)
3339 rc = pinentry_merge_options (&client->crypto->agent->pinentry_opts,
3340 pin_opts);
3342 #endif
3344 return send_error (ctx, rc);
3347 static gpg_error_t
3348 do_rename (assuan_context_t ctx, char *line)
3350 struct client_s *client = assuan_get_pointer (ctx);
3351 gpg_error_t rc;
3352 char **req, **src, *dst;
3353 xmlNodePtr n, ndst;
3355 req = str_split (line, " ", 0);
3357 if (!req || !req[0] || !req[1])
3359 strv_free (req);
3360 return GPG_ERR_SYNTAX;
3363 dst = req[1];
3364 is_literal_element (&dst);
3366 if (!valid_xml_element ((xmlChar *) dst))
3368 strv_free (req);
3369 return GPG_ERR_INV_VALUE;
3372 if (strchr (req[0], '\t'))
3373 src = str_split (req[0], "\t", 0);
3374 else
3375 src = str_split (req[0], " ", 0);
3377 if (!src || !*src)
3379 rc = GPG_ERR_SYNTAX;
3380 goto fail;
3383 n = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3384 if (src[1] && n)
3385 n = find_elements (client, client->doc, n->children, src + 1, &rc, NULL, NULL,
3386 NULL, 0, 0, NULL, 0);
3388 if (!n)
3389 goto fail;
3391 rc = peer_is_invoker (client);
3392 if (rc == GPG_ERR_EPERM)
3393 rc = is_element_owner (client, n);
3395 if (rc)
3396 goto fail;
3398 xmlChar *a = xmlGetProp (n, (xmlChar *) "_name");
3399 if (!a)
3401 rc = GPG_ERR_ENOMEM;
3402 goto fail;
3405 /* To prevent unwanted effects:
3407 * <root name="a"><b/></root>
3409 * RENAME a<TAB>b b
3411 if (xmlStrEqual (a, (xmlChar *) dst))
3413 xmlFree (a);
3414 rc = GPG_ERR_AMBIGUOUS_NAME;
3415 goto fail;
3418 xmlFree (a);
3419 char **tmp = NULL;
3420 if (src[1])
3422 char **p;
3424 for (p = src; *p; p++)
3426 if (!*(p + 1))
3427 break;
3428 strv_printf (&tmp, "%s", *p);
3432 strv_printf (&tmp, "!%s", dst);
3433 ndst = find_root_element (client, client->doc, &tmp, &rc, NULL, 0, 0);
3434 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3436 strv_free (tmp);
3437 goto fail;
3440 if (tmp[1] && ndst)
3441 ndst = find_elements (client, client->doc, ndst->children, tmp + 1, &rc, NULL,
3442 NULL, NULL, 0, 0, NULL, 0);
3444 strv_free (tmp);
3445 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3446 goto fail;
3448 rc = 0;
3450 /* Target may exist:
3452 * <root name="a"/>
3453 * <root name="b" target="a"/>
3455 * RENAME b a
3457 * Would need to do:
3458 * RENAME !b a
3460 if (ndst == n)
3462 rc = GPG_ERR_AMBIGUOUS_NAME;
3463 goto fail;
3466 if (ndst)
3468 rc = peer_is_invoker (client);
3469 if (rc == GPG_ERR_EPERM)
3470 rc = is_element_owner (client, ndst);
3472 if (rc)
3473 goto fail;
3475 unlink_node (ndst);
3476 xmlFreeNodeList (ndst);
3479 rc = add_attribute (n, "_name", dst);
3481 fail:
3482 strv_free (req);
3483 strv_free (src);
3484 return rc;
3487 static gpg_error_t
3488 rename_command (assuan_context_t ctx, char *line)
3490 struct client_s *client = assuan_get_pointer (ctx);
3491 gpg_error_t rc;
3492 struct argv_s *args[] = {
3493 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3494 NULL
3497 rc = parse_options (&line, args, client);
3498 if (rc)
3499 return send_error (ctx, rc);
3501 if (client->opts & OPT_INQUIRE)
3503 unsigned char *result;
3504 size_t len;
3506 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3507 if (rc)
3508 return send_error (ctx, rc);
3510 line = (char *) result;
3513 rc = do_rename (ctx, line);
3515 if (client->opts & OPT_INQUIRE)
3516 xfree (line);
3518 return send_error (ctx, rc);
3521 static gpg_error_t
3522 do_copy (assuan_context_t ctx, char *line)
3524 struct client_s *client = assuan_get_pointer (ctx);
3525 gpg_error_t rc;
3526 char **req, **src = NULL, **dst = NULL;
3527 xmlNodePtr nsrc, ndst, new = NULL;
3529 req = str_split (line, " ", 0);
3530 if (!req || !req[0] || !req[1])
3532 strv_free (req);
3533 return GPG_ERR_SYNTAX;
3536 if (strchr (req[0], '\t'))
3537 src = str_split (req[0], "\t", 0);
3538 else
3539 src = str_split (req[0], " ", 0);
3541 if (!src || !*src)
3543 rc = GPG_ERR_SYNTAX;
3544 goto fail;
3547 if (strchr (req[1], '\t'))
3548 dst = str_split (req[1], "\t", 0);
3549 else
3550 dst = str_split (req[1], " ", 0);
3552 if (!dst || !*dst)
3554 rc = GPG_ERR_SYNTAX;
3555 goto fail;
3558 if (!valid_element_path (dst, 0))
3560 rc = GPG_ERR_INV_VALUE;
3561 goto fail;
3564 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3565 if (nsrc && src[1])
3566 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc, NULL,
3567 NULL, NULL, 0, 0, NULL, 0);
3569 if (!nsrc)
3570 goto fail;
3572 new = xmlCopyNodeList (nsrc);
3573 if (!new)
3575 rc = GPG_ERR_ENOMEM;
3576 goto fail;
3579 int create = 0;
3580 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3581 if (ndst && dst[1])
3583 if (ndst->children)
3584 ndst = find_elements (client, client->doc, ndst->children, dst + 1, &rc, NULL,
3585 NULL, create_target_elements_cb, 0, 0, NULL, 0);
3586 else
3587 create = 1;
3589 else
3590 create = 1;
3592 if (!ndst && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3593 goto fail;
3594 else if (!ndst || create)
3596 ndst = create_element_path (client, &dst, &rc, NULL);
3597 if (!ndst)
3598 goto fail;
3601 rc = peer_is_invoker (client);
3602 if (rc == GPG_ERR_EPERM)
3603 rc = is_element_owner (client, ndst);
3605 if (rc)
3606 goto fail;
3608 /* Merge any attributes from the src node to the initial dst node. */
3609 for (xmlAttrPtr attr = new->properties; attr; attr = attr->next)
3611 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3612 continue;
3614 xmlAttrPtr a = xmlHasProp (ndst, attr->name);
3615 if (a)
3616 xmlRemoveProp (a);
3618 xmlChar *tmp = xmlNodeGetContent (attr->children);
3619 xmlNewProp (ndst, attr->name, tmp);
3620 xmlFree (tmp);
3621 rc = add_attribute (ndst, NULL, NULL);
3624 xmlNodePtr n = ndst->children;
3625 xmlUnlinkNode (n);
3626 xmlFreeNodeList (n);
3627 ndst->children = NULL;
3629 if (new->children)
3631 n = xmlCopyNodeList (new->children);
3632 if (!n)
3634 rc = GPG_ERR_ENOMEM;
3635 goto fail;
3638 n = xmlAddChildList (ndst, n);
3639 if (!n)
3641 rc = GPG_ERR_ENOMEM;
3642 goto fail;
3645 rc = update_element_mtime (xmlDocGetRootElement (client->doc) ==
3646 ndst->parent ? ndst : ndst->parent);
3649 fail:
3650 if (new)
3652 xmlUnlinkNode (new);
3653 xmlFreeNodeList (new);
3656 if (req)
3657 strv_free (req);
3659 if (src)
3660 strv_free (src);
3662 if (dst)
3663 strv_free (dst);
3665 return rc;
3668 static gpg_error_t
3669 copy_command (assuan_context_t ctx, char *line)
3671 struct client_s *client = assuan_get_pointer (ctx);
3672 gpg_error_t rc;
3673 struct argv_s *args[] = {
3674 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3675 NULL
3678 rc = parse_options (&line, args, client);
3679 if (rc)
3680 return send_error (ctx, rc);
3682 if (client->opts & OPT_INQUIRE)
3684 unsigned char *result;
3685 size_t len;
3687 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3688 if (rc)
3689 return send_error (ctx, rc);
3691 line = (char *) result;
3694 rc = do_copy (ctx, line);
3696 if (client->opts & OPT_INQUIRE)
3697 xfree (line);
3699 return send_error (ctx, rc);
3702 static gpg_error_t
3703 do_move (assuan_context_t ctx, char *line)
3705 struct client_s *client = assuan_get_pointer (ctx);
3706 gpg_error_t rc;
3707 char **req, **src = NULL, **dst = NULL;
3708 xmlNodePtr nsrc, ndst = NULL;
3710 req = str_split (line, " ", 0);
3712 if (!req || !req[0] || !req[1])
3714 strv_free (req);
3715 return GPG_ERR_SYNTAX;
3718 if (strchr (req[0], '\t'))
3719 src = str_split (req[0], "\t", 0);
3720 else
3721 src = str_split (req[0], " ", 0);
3723 if (!src || !*src)
3725 rc = GPG_ERR_SYNTAX;
3726 goto fail;
3729 if (strchr (req[1], '\t'))
3730 dst = str_split (req[1], "\t", 0);
3731 else
3732 dst = str_split (req[1], " ", 0);
3734 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3735 if (nsrc && src[1])
3736 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc,
3737 NULL, NULL, NULL, 0, 0, NULL, 0);
3739 if (!nsrc)
3740 goto fail;
3742 rc = peer_is_invoker (client);
3743 if (rc == GPG_ERR_EPERM)
3744 rc = is_element_owner (client, nsrc);
3746 if (rc)
3747 goto fail;
3749 if (dst)
3751 if (!valid_element_path (dst, 0))
3753 rc = GPG_ERR_INV_VALUE;
3754 goto fail;
3757 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3758 if (ndst && dst[1])
3759 ndst = find_elements (client, client->doc, ndst->children, dst + 1,
3760 &rc, NULL, NULL, NULL, 0, 0, NULL, 0);
3762 else
3763 ndst = xmlDocGetRootElement (client->doc);
3765 for (xmlNodePtr n = ndst; n; n = n->parent)
3767 if (n == nsrc)
3769 rc = GPG_ERR_CONFLICT;
3770 goto fail;
3774 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3775 goto fail;
3777 rc = 0;
3779 if (ndst)
3781 xmlChar *a = node_has_attribute (nsrc, (xmlChar *) "_name");
3783 xmlNodePtr dup = find_element (client, ndst->children, (char *) a,
3784 NULL, &rc);
3785 xmlFree (a);
3787 if (rc)
3788 goto fail;
3790 rc = peer_is_invoker (client);
3791 if (rc == GPG_ERR_EPERM)
3792 rc = is_element_owner (client, ndst);
3794 if (rc)
3795 goto fail;
3797 if (dup)
3799 if (dup == nsrc)
3800 goto fail;
3802 if (ndst == xmlDocGetRootElement (client->doc))
3804 xmlNodePtr n = nsrc;
3805 int match = 0;
3807 while (n->parent && n->parent != ndst)
3808 n = n->parent;
3810 xmlChar *a = node_has_attribute (n, (xmlChar *) "_name");
3811 xmlChar *b = node_has_attribute (nsrc, (xmlChar *) "_name");
3813 if (xmlStrEqual (a, b))
3815 match = 1;
3816 xmlUnlinkNode (nsrc);
3817 xmlUnlinkNode (n);
3818 xmlFreeNodeList (n);
3821 xmlFree (a);
3822 xmlFree (b);
3824 if (!match)
3826 xmlUnlinkNode (dup);
3827 xmlFreeNodeList (dup);
3830 else
3831 xmlUnlinkNode (dup);
3835 if (!ndst && dst)
3837 xmlChar *name = node_has_attribute (nsrc, (xmlChar *) "_name");
3839 if (nsrc->parent == xmlDocGetRootElement (client->doc)
3840 && !strcmp ((char *) name, *dst))
3842 xmlFree (name);
3843 rc = GPG_ERR_CONFLICT;
3844 goto fail;
3847 xmlFree (name);
3848 ndst = create_element_path (client, &dst, &rc, nsrc);
3851 if (!ndst)
3852 goto fail;
3854 update_element_mtime (nsrc->parent);
3855 xmlUnlinkNode (nsrc);
3856 ndst = xmlAddChildList (ndst, nsrc);
3858 if (!ndst)
3859 rc = GPG_ERR_ENOMEM;
3860 else
3861 update_element_mtime (ndst->parent);
3863 fail:
3864 if (req)
3865 strv_free (req);
3867 if (src)
3868 strv_free (src);
3870 if (dst)
3871 strv_free (dst);
3873 return rc;
3876 static gpg_error_t
3877 move_command (assuan_context_t ctx, char *line)
3879 struct client_s *client = assuan_get_pointer (ctx);
3880 gpg_error_t rc;
3881 struct argv_s *args[] = {
3882 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3883 NULL
3886 rc = parse_options (&line, args, client);
3887 if (rc)
3888 return send_error (ctx, rc);
3890 if (client->opts & OPT_INQUIRE)
3892 unsigned char *result;
3893 size_t len;
3895 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3896 if (rc)
3897 return send_error (ctx, rc);
3899 line = (char *) result;
3902 rc = do_move (ctx, line);
3904 if (client->opts & OPT_INQUIRE)
3905 xfree (line);
3907 return send_error (ctx, rc);
3910 static gpg_error_t
3911 ls_command (assuan_context_t ctx, char *line)
3913 gpg_error_t rc;
3914 char *tmp = str_asprintf ("%s/data", homedir);
3915 char *dir = expand_homedir (tmp);
3916 DIR *d = opendir (dir);
3918 rc = gpg_error_from_errno (errno);
3919 xfree (tmp);
3921 if (!d)
3923 xfree (dir);
3924 return send_error (ctx, rc);
3927 size_t len =
3928 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3929 struct dirent *p = xmalloc (len), *cur = NULL;
3930 char *list = NULL;
3932 xfree (dir);
3933 rc = 0;
3935 while (!readdir_r (d, p, &cur) && cur)
3937 if (cur->d_name[0] == '.' && cur->d_name[1] == '\0')
3938 continue;
3939 else if (cur->d_name[0] == '.' && cur->d_name[1] == '.'
3940 && cur->d_name[2] == '\0')
3941 continue;
3943 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3945 if (!tmp)
3947 if (list)
3948 xfree (list);
3950 rc = GPG_ERR_ENOMEM;
3951 break;
3954 xfree (list);
3955 list = tmp;
3958 closedir (d);
3959 xfree (p);
3961 if (rc)
3962 return send_error (ctx, rc);
3964 if (!list)
3965 return send_error (ctx, GPG_ERR_NO_DATA);
3967 list[strlen (list) - 1] = 0;
3968 rc = xfer_data (ctx, list, strlen (list));
3969 xfree (list);
3970 return send_error (ctx, rc);
3973 static gpg_error_t
3974 bye_notify (assuan_context_t ctx, char *line)
3976 struct client_s *cl = assuan_get_pointer (ctx);
3978 #ifdef WITH_GNUTLS
3979 if (cl->thd->remote)
3981 int rc;
3985 struct timeval tv = { 0, 50000 };
3987 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3988 if (rc == GNUTLS_E_AGAIN)
3989 select (0, NULL, NULL, NULL, &tv);
3991 while (rc == GNUTLS_E_AGAIN);
3993 #endif
3995 /* This will let assuan_process_next() return. */
3996 fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK);
3997 cl->last_rc = 0; // BYE command result
3998 return 0;
4001 static gpg_error_t
4002 reset_notify (assuan_context_t ctx, char *line)
4004 struct client_s *client = assuan_get_pointer (ctx);
4006 if (client)
4007 cleanup_client (client);
4009 return 0;
4013 * This is called before every Assuan command.
4015 static gpg_error_t
4016 command_startup (assuan_context_t ctx, const char *name)
4018 struct client_s *client = assuan_get_pointer (ctx);
4019 gpg_error_t rc;
4020 struct command_table_s *cmd = NULL;
4022 log_write1 ("command='%s'", name);
4023 client->last_rc = client->opts = 0;
4025 for (int i = 0; command_table[i]; i++)
4027 if (!strcasecmp (name, command_table[i]->name))
4029 if (command_table[i]->ignore_startup)
4030 return 0;
4031 cmd = command_table[i];
4032 break;
4036 client->last_rc = rc = gpg_error (file_modified (client, cmd));
4037 return rc;
4041 * This is called after every Assuan command.
4043 static void
4044 command_finalize (assuan_context_t ctx, gpg_error_t rc)
4046 struct client_s *client = assuan_get_pointer (ctx);
4048 if (!(client->flags & FLAG_LOCK_CMD))
4049 unlock_file_mutex (client, 0);
4051 log_write1 (_("command completed: rc=%u"), client->last_rc);
4052 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
4053 #ifdef WITH_GNUTLS
4054 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
4055 #endif
4058 static gpg_error_t
4059 help_command (assuan_context_t ctx, char *line)
4061 gpg_error_t rc;
4062 int i;
4064 if (!line || !*line)
4066 char *tmp;
4067 char *help = str_dup (_("Usage: HELP [<COMMAND>]\n"
4068 "For commands that take an element path as an argument, each element is "
4069 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
4070 "\n" "COMMANDS:"));
4072 for (i = 0; command_table[i]; i++)
4074 if (!command_table[i]->help)
4075 continue;
4077 tmp = str_asprintf ("%s %s", help, command_table[i]->name);
4078 xfree (help);
4079 help = tmp;
4082 tmp = strip_texi_and_wrap (help);
4083 xfree (help);
4084 rc = xfer_data (ctx, tmp, strlen (tmp));
4085 xfree (tmp);
4086 return send_error (ctx, rc);
4089 for (i = 0; command_table[i]; i++)
4091 if (!strcasecmp (line, command_table[i]->name))
4093 char *help, *tmp;
4095 if (!command_table[i]->help)
4096 break;
4098 help = strip_texi_and_wrap (command_table[i]->help);
4099 tmp = str_asprintf (_("Usage: %s"), help);
4100 xfree (help);
4101 rc = xfer_data (ctx, tmp, strlen (tmp));
4102 xfree (tmp);
4103 return send_error (ctx, rc);
4107 return send_error (ctx, GPG_ERR_INV_NAME);
4110 static void
4111 new_command (const char *name, int ignore, int unlock,
4112 gpg_error_t (*handler) (assuan_context_t, char *),
4113 const char *help)
4115 int i = 0;
4117 if (command_table)
4118 for (i = 0; command_table[i]; i++);
4120 command_table =
4121 xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
4122 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
4123 command_table[i]->name = name;
4124 command_table[i]->handler = handler;
4125 command_table[i]->ignore_startup = ignore;
4126 command_table[i]->unlock = unlock;
4127 command_table[i++]->help = help;
4128 command_table[i] = NULL;
4131 void
4132 deinit_commands ()
4134 int i;
4136 for (i = 0; command_table[i]; i++)
4137 xfree (command_table[i]);
4139 xfree (command_table);
4142 static int
4143 sort_commands (const void *arg1, const void *arg2)
4145 struct command_table_s *const *a = arg1;
4146 struct command_table_s *const *b = arg2;
4148 if (!*a || !*b)
4149 return 0;
4150 else if (*a && !*b)
4151 return 1;
4152 else if (!*a && *b)
4153 return -1;
4155 return strcmp ((*a)->name, (*b)->name);
4158 static gpg_error_t
4159 passwd_command (assuan_context_t ctx, char *line)
4161 struct client_s *client = assuan_get_pointer (ctx);
4162 gpg_error_t rc;
4163 struct argv_s *args[] = {
4164 &(struct argv_s) {"reset", OPTION_TYPE_NOARG, parse_opt_reset},
4165 &(struct argv_s) {"s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count},
4166 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG, parse_opt_no_passphrase},
4167 NULL
4170 if (client->flags & FLAG_NEW)
4171 return send_error (ctx, GPG_ERR_INV_STATE);
4173 client->crypto->save.s2k_count =
4174 config_get_ulong (client->filename, "s2k_count");
4175 rc = parse_options (&line, args, client);
4176 if (rc)
4177 return send_error (ctx, rc);
4179 if (!rc && client->opts & OPT_RESET)
4181 rc = cache_clear (client->md5file);
4182 if (!rc)
4183 send_status_all (STATUS_CACHE, NULL);
4186 if (!rc)
4188 if (!IS_PKI (client->crypto))
4190 struct crypto_s *crypto;
4192 xfree (client->crypto->filename);
4193 client->crypto->filename = str_dup (client->filename);
4194 rc = change_passwd (ctx, client->filename,
4195 client->flags & FLAG_NO_PINENTRY, &crypto,
4196 (client->opts & OPT_NO_PASSPHRASE));
4197 if (!rc)
4199 cleanup_crypto (&client->crypto);
4200 client->crypto = crypto;
4201 update_checksum (client);
4202 cleanup_crypto_stage1 (client->crypto);
4205 #ifdef WITH_AGENT
4206 else
4208 if (client->crypto->save.s2k_count)
4209 rc = send_to_agent (client->crypto->agent, NULL, NULL,
4210 "OPTION s2k-count=%lu",
4211 client->crypto->save.s2k_count);
4213 if (!rc)
4214 rc = agent_passwd (client->crypto);
4216 #endif
4219 return send_error (ctx, rc);
4222 static gpg_error_t
4223 parse_keygrip_opt_sign (void *data, void *value)
4225 struct client_s *client = data;
4227 (void) value;
4228 client->opts |= OPT_SIGN;
4229 return 0;
4232 static gpg_error_t
4233 keygrip_command (assuan_context_t ctx, char *line)
4235 struct client_s *client = assuan_get_pointer (ctx);
4236 gpg_error_t rc;
4237 struct crypto_s *crypto = NULL;
4238 struct argv_s *args[] = {
4239 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_keygrip_opt_sign},
4240 NULL
4243 if (!line || !*line)
4244 return send_error (ctx, GPG_ERR_SYNTAX);
4246 rc = parse_options (&line, args, client);
4247 if (rc)
4248 return send_error (ctx, rc);
4250 if (!valid_filename (line))
4251 return send_error (ctx, GPG_ERR_INV_VALUE);
4253 rc = init_client_crypto (&crypto);
4254 if (rc)
4255 return send_error (ctx, rc);
4257 rc = read_data_file (line, crypto);
4258 if (!rc)
4260 char *hexgrip = NULL;
4262 if (!IS_PKI (crypto))
4264 cleanup_crypto (&crypto);
4265 return send_error (ctx, GPG_ERR_NOT_SUPPORTED);
4268 if (client->opts & OPT_SIGN)
4270 if (valid_keygrip (crypto->sign_grip, sizeof (crypto->sign_grip)))
4271 hexgrip = bin2hex (crypto->sign_grip, sizeof (crypto->sign_grip));
4274 if (!hexgrip)
4275 hexgrip = bin2hex (crypto->grip, sizeof (crypto->grip));
4277 if (!hexgrip)
4278 rc = GPG_ERR_ENOMEM;
4279 else
4280 rc = xfer_data (ctx, hexgrip, strlen (hexgrip));
4282 xfree (hexgrip);
4285 UPDATE_AGENT_CTX (client, crypto);
4286 cleanup_crypto (&crypto);
4287 return send_error (ctx, rc);
4290 static gpg_error_t
4291 parse_opt_data (void *data, void *value)
4293 struct client_s *client = data;
4295 (void) value;
4296 client->opts |= OPT_DATA;
4297 return 0;
4300 static gpg_error_t
4301 getinfo_command (assuan_context_t ctx, char *line)
4303 struct client_s *client = assuan_get_pointer (ctx);
4304 gpg_error_t rc;
4305 char buf[ASSUAN_LINELENGTH];
4306 struct argv_s *args[] = {
4307 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4308 NULL
4311 rc = parse_options (&line, args, client);
4312 if (rc)
4313 return send_error (ctx, rc);
4315 if (!strcasecmp (line, "clients"))
4317 if (client->opts & OPT_DATA)
4319 MUTEX_LOCK (&cn_mutex);
4320 snprintf (buf, sizeof (buf), "%i", slist_length (cn_thread_list));
4321 MUTEX_UNLOCK (&cn_mutex);
4322 rc = xfer_data (ctx, buf, strlen (buf));
4324 else
4325 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4327 else if (!strcasecmp (line, "cache"))
4329 if (client->opts & OPT_DATA)
4331 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4332 rc = xfer_data (ctx, buf, strlen (buf));
4334 else
4335 rc = send_status (ctx, STATUS_CACHE, NULL);
4337 else if (!strcasecmp (line, "pid"))
4339 char buf[32];
4340 pid_t pid = getpid ();
4342 snprintf (buf, sizeof (buf), "%i", pid);
4343 rc = xfer_data (ctx, buf, strlen (buf));
4345 else if (!strcasecmp (line, "version"))
4347 char *buf = str_asprintf ("0x%06x %s%s", VERSION_HEX,
4348 #ifdef WITH_GNUTLS
4349 "GNUTLS "
4350 #endif
4351 #ifdef WITH_QUALITY
4352 "QUALITY "
4353 #endif
4354 "", use_agent ? "AGENT" : "");
4355 rc = xfer_data (ctx, buf, strlen (buf));
4356 xfree (buf);
4358 else if (!strcasecmp (line, "last_error"))
4360 if (client->last_error)
4361 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4362 else
4363 rc = GPG_ERR_NO_DATA;
4365 else
4366 rc = gpg_error (GPG_ERR_SYNTAX);
4368 return send_error (ctx, rc);
4371 #ifdef WITH_AGENT
4372 static gpg_error_t
4373 send_data_cb (void *user, const void *buf, size_t len)
4375 assuan_context_t ctx = user;
4377 return assuan_send_data (ctx, buf, len);
4380 static gpg_error_t
4381 send_status_cb (void *user, const char *line)
4383 assuan_context_t ctx = user;
4384 char keyword[200], *k;
4385 const char *p;
4387 for (p = line, k = keyword; *p; p++)
4389 if (isspace (*p))
4390 break;
4392 *k++ = *p;
4395 *k = 0;
4396 if (*p == '#')
4397 p++;
4399 while (isspace (*p))
4400 p++;
4402 return assuan_write_status (ctx, keyword, *p ? p : NULL);
4404 #endif
4406 static gpg_error_t
4407 agent_command (assuan_context_t ctx, char *line)
4409 gpg_error_t rc = 0;
4411 if (!use_agent)
4412 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
4414 #ifdef WITH_AGENT
4415 struct client_s *client = assuan_get_pointer (ctx);
4417 if (!line || !*line)
4418 return send_error (ctx, GPG_ERR_SYNTAX);
4420 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 1);
4421 rc = assuan_transact (client->crypto->agent->ctx, line, send_data_cb,
4422 client->ctx, agent_loopback_cb, client->crypto,
4423 send_status_cb, client->ctx);
4424 if (gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
4426 char *line;
4427 size_t len;
4429 rc = assuan_write_line (client->crypto->agent->ctx, "CAN");
4430 if (!rc)
4432 rc = assuan_read_line (client->crypto->agent->ctx, &line, &len);
4433 if (!rc)
4434 rc = gpg_error (GPG_ERR_ASS_CANCELED);
4438 assuan_set_flag (client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 0);
4439 #endif
4440 return send_error (ctx, rc);
4443 void
4444 init_commands ()
4446 /* !BEGIN-HELP-TEXT!
4448 * This comment is used as a marker to generate the offline documentation
4449 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4450 * script to determine where commands begin and end.
4452 new_command("HELP", 1, 1, help_command, _(
4453 "HELP [<COMMAND>]\n"
4454 "Show available commands or command specific help text."
4457 new_command("AGENT", 1, 1, agent_command, _(
4458 "AGENT <command>\n"
4459 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4460 "@command{gpg-agent}."
4463 new_command("GETINFO", 1, 1, getinfo_command, _(
4464 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4465 "Get server and other information: @var{cache} returns the number of cached "
4466 "documents via a status message. @var{clients} returns the number of "
4467 "connected clients via a status message. @var{pid} returns the process ID "
4468 "number of the server via a data response. @var{VERSION} returns the server "
4469 "version number and compile-time features with a data response with each "
4470 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4471 "the last failed command when available. @xref{Status Messages}. "
4472 "\n"
4473 "When the @option{--data} option is specified then the result will be sent "
4474 "via a data response rather than a status message."
4477 new_command("PASSWD", 0, 0, passwd_command, _(
4478 "PASSWD [--reset] [--s2k-count=N] [--no-passphrase]\n"
4479 "Changes the passphrase of the secret key required to open the current "
4480 "file or the passphrase of a symmetrically encrypted data file. When the "
4481 "@option{--reset} option is passed then the cache entry for the current "
4482 "file will be reset and the passphrase, if any, will be required during the "
4483 "next @code{OPEN} (@pxref{OPEN})."
4484 "\n"
4485 "The @option{--s2k-count} option sets or changes (@pxref{SAVE}) the number "
4486 "of hash iterations for a passphrase and must be either @code{0} to use "
4487 "the calibrated count of the machine (the default), or a value greater than "
4488 "or equal to @code{65536}. This option has no effect for symmetrically "
4489 "encrypted data files."
4490 "\n"
4491 "The @option{--no-passphrase} option will prevent requiring a passphrase for "
4492 "the data file, although a passphrase may be required when changing it."
4495 new_command("KEYGRIP", 1, 1, keygrip_command, _(
4496 "KEYGRIP [--sign] <filename>\n"
4497 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4498 "data response."
4499 "\n"
4500 "When the @option{--sign} option is specified then the key used for signing "
4501 "of the specified @var{filename} will be returned."
4502 "\n"
4503 "For symmetrically encrypted data files this command returns the error "
4504 "GPG_ERR_NOT_SUPPORTED."
4507 new_command("OPEN", 1, 1, open_command, _(
4508 "OPEN [--lock] <filename> [<passphrase>]\n"
4509 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4510 "found on the file-system then a new document will be created. If the file "
4511 "is found, it is looked for in the file cache. If cached and no "
4512 "@var{passphrase} was specified then the cached document is opened. When not "
4513 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4514 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4515 "specified."
4516 "\n"
4517 "When the @option{--lock} option is passed then the file mutex will be "
4518 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4519 "file has been opened."
4522 new_command("SAVE", 0, 0, save_command, _(
4523 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring] [--sign-keygrip=hexstring]\n"
4524 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4525 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4526 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4527 "keypair will be generated and a pinentry will be used to prompt for the "
4528 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4529 "passed in which case the data file will not be passphrase protected. "
4530 "\n"
4531 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4532 "passphrase retrieval and caching of new files when @command{gpg-agent} "
4533 "use is enabled. The datafile will be symmetrically encrypted and will not "
4534 "use or generate any keypair."
4535 "\n"
4536 "The @option{--reset} option will clear the cache entry for the current file "
4537 "and require a passphrase, if needed, before saving."
4538 "\n"
4539 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4540 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4541 "(@pxref{Configuration}) for available ciphers."
4542 "\n"
4543 "The @option{--cipher-iterations} option specifies the number of times to "
4544 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4545 "\n"
4546 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4547 "the client to obtain the key paramaters to use when generating a new "
4548 "keypair. The inquired data is expected to be an S-expression. If not "
4549 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4550 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4551 "that when this option is specified a new keypair will be generated "
4552 "reguardless if the file is a new one and that if the data file is protected "
4553 "the passphrase to open it will be required before generating the new keypair."
4554 "\n"
4555 "You can encrypt the data file to a public key other than the one that it "
4556 "was originally encrypted with by passing the @option{--keygrip} option with "
4557 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4558 "be of any key that @command{gpg-agent} knows about. The "
4559 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4560 "secret key. Use the @code{KEYGRIP} (@pxref{KEYGRIP}) command to obtain the "
4561 "keygrip of an existing data file. This option may be needed when using a "
4562 "smartcard. This option has no effect with symmetrically encrypted data files."
4563 "\n"
4564 "The @option{--s2k-count} option sets number of hash iterations for a "
4565 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4566 "value and is the default. This setting only affects new files. To change "
4567 "the setting use the @code{PASSWD} command (@pxref{PASSWD}). This option "
4568 "has no effect with symmetrically encrypted data files."
4571 new_command("ISCACHED", 1, 0, iscached_command, _(
4572 "ISCACHED [--lock] <filename>\n"
4573 "An @emph{OK} response is returned if the specified @var{filename} is found "
4574 "in the file cache. If not found in the cache but exists on the filesystem "
4575 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4576 "returned."
4577 "\n"
4578 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4579 "file exists; it does not need to be opened nor cached. The lock will be "
4580 "released when the client exits or sends the @code{UNLOCK} (@pxref{UNLOCK}) "
4581 "command."
4584 new_command("CLEARCACHE", 1, 1, clearcache_command, _(
4585 "CLEARCACHE [<filename>]\n"
4586 "Clears a file cache entry for all or the specified @var{filename}."
4589 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command, _(
4590 "CACHETIMEOUT <filename> <seconds>\n"
4591 "The time in @var{seconds} until @var{filename} will be removed from the "
4592 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4593 "the passphrase for each @code{OPEN} or @code{SAVE} command (@pxref{OPEN}, "
4594 "@pxref{SAVE}). @xref{Configuration}, and the @code{cache_timeout} "
4595 "parameter."
4598 new_command("LIST", 0, 1, list_command, _(
4599 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4600 "If no element path is given then a newline separated list of root elements "
4601 "is returned with a data response. If given, then all reachable elements "
4602 "of the specified element path are returned unless the @option{--no-recurse} "
4603 "option is specified. If specified, only the child elements of the element "
4604 "path are returned without recursing into grandchildren. Each resulting "
4605 "element is prefixed with the literal @code{!} character when the element "
4606 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4607 "\n"
4608 "When the @option{--verbose} option is passed then each element path "
4609 "returned will have zero or more flags appened to it. These flags are "
4610 "delimited from the element path by a single space character. A flag itself "
4611 "is a single character. Flag @code{P} indicates that access to the element "
4612 "is denied. Flag @code{+} indicates that there are child nodes of "
4613 "the current element path. Flag @code{E} indicates that an element of an "
4614 "element path contained in a @var{target} attribute could not be found. Flag "
4615 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4616 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4617 "of the @var{target} attribute contained in the current element (see below)."
4618 "\n"
4619 "The @option{--with-target} option implies @option{--verbose} and will append "
4620 "an additional flag @code{T} followed by a single space then an element path. "
4621 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4622 "current element when it contains a @var{target} attribute. When no "
4623 "@var{target} attribute is found then no flag will be appended."
4624 "\n"
4625 "The @option{--no-recurse} option limits the amount of data returned to only "
4626 "the listing of children of the specified element path and not any "
4627 "grandchildren."
4628 "\n"
4629 "The @option{--all} option lists the entire element tree for each root "
4630 "element. This option also implies option @option{--verbose}."
4631 "\n"
4632 "When the @option{--inquire} option is passed then all remaining non-option "
4633 "arguments are retrieved via a server @emph{INQUIRE}."
4636 new_command("REALPATH", 0, 1, realpath_command, _(
4637 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4638 "Resolves all @code{target} attributes of the specified element path and "
4639 "returns the result with a data response. @xref{Target Attribute}, for details."
4640 "\n"
4641 "When the @option{--inquire} option is passed then all remaining non-option "
4642 "arguments are retrieved via a server @emph{INQUIRE}."
4645 new_command("STORE", 0, 1, store_command, _(
4646 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4647 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4648 "\n"
4649 "Creates a new element path or modifies the @var{content} of an existing "
4650 "element. If only a single element is specified then a new root element is "
4651 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4652 "set to the final @key{TAB} delimited element. If no @var{content} is "
4653 "specified after the final @key{TAB}, then the content of an existing "
4654 "element will be removed; or empty when creating a new element."
4655 "\n"
4656 "The only restriction of an element name is that it not contain whitespace "
4657 "or begin with the literal element character @code{!} unless specifying a "
4658 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4659 "the @key{TAB} delimited elements. It is recommended that the content of an "
4660 "element be base64 encoded when it contains control or @key{TAB} characters "
4661 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
4664 new_command("RENAME", 0, 1, rename_command, _(
4665 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4666 "Renames the specified @var{element} to the new @var{value}. If an element of "
4667 "the same name as the @var{value} already exists it will be overwritten."
4668 "\n"
4669 "When the @option{--inquire} option is passed then all remaining non-option "
4670 "arguments are retrieved via a server @emph{INQUIRE}."
4673 new_command("COPY", 0, 1, copy_command, _(
4674 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4675 "Copies the entire element tree starting from the child node of the source "
4676 "element, to the destination element path. If the destination element path "
4677 "does not exist then it will be created; otherwise it is overwritten."
4678 "\n"
4679 "Note that attributes from the source element are merged into the "
4680 "destination element when the destination element path exists. When an "
4681 "attribute of the same name exists in both the source and destination "
4682 "elements then the destination attribute will be updated to the source "
4683 "attribute value."
4684 "\n"
4685 "When the @option{--inquire} option is passed then all remaining non-option "
4686 "arguments are retrieved via a server @emph{INQUIRE}."
4689 new_command("MOVE", 0, 1, move_command, _(
4690 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4691 "Moves the source element path to the destination element path. If the "
4692 "destination is not specified then it will be moved to the root node of the "
4693 "document. If the destination is specified and exists then it will be "
4694 "overwritten; otherwise non-existing elements of the destination element "
4695 "path will be created."
4696 "\n"
4697 "When the @option{--inquire} option is passed then all remaining non-option "
4698 "arguments are retrieved via a server @emph{INQUIRE}."
4701 new_command("DELETE", 0, 1, delete_command, _(
4702 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4703 "Removes the specified element path and all of its children. This may break "
4704 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4705 "refers to this element or any of its children."
4706 "\n"
4707 "When the @option{--inquire} option is passed then all remaining non-option "
4708 "arguments are retrieved via a server @emph{INQUIRE}."
4711 new_command("GET", 0, 1, get_command, _(
4712 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4713 "Retrieves the content of the specified element. The content is returned "
4714 "with a data response."
4715 "\n"
4716 "When the @option{--inquire} option is passed then all remaining non-option "
4717 "arguments are retrieved via a server @emph{INQUIRE}."
4720 new_command("ATTR", 0, 1, attr_command, _(
4721 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4722 "@table @asis\n"
4723 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4724 "\n"
4725 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4726 "element. When no @var{value} is specified any existing value will be removed."
4727 "\n"
4728 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4729 "\n"
4730 " Removes an @var{attribute} from an element."
4731 "\n"
4732 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4733 "\n"
4734 " Retrieves a newline separated list of attributes names and values "
4735 "from the specified element. Each attribute name and value is space delimited."
4736 "\n"
4737 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4738 "\n"
4739 " Retrieves the value of an @var{attribute} from an element."
4740 "@end table\n"
4741 "\n"
4742 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4743 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4744 "commands instead."
4745 "\n"
4746 "The @code{_mtime} attribute is updated each time an element is modified by "
4747 "either storing content, editing attributes or by deleting a child element. "
4748 "The @code{_ctime} attribute is created for each new element in an element "
4749 "path."
4750 "\n"
4751 "When the @option{--inquire} option is passed then all remaining non-option "
4752 "arguments are retrieved via a server @emph{INQUIRE}."
4753 "\n"
4754 "@xref{Target Attribute}, for details about this special attribute."
4757 new_command("XPATH", 0, 1, xpath_command, _(
4758 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4759 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4760 "specified it is assumed the expression is a request to return a result. "
4761 "Otherwise, the result is set to the @var{value} argument and the document is "
4762 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4763 "is assumed to be empty and the document is updated. For example:"
4764 "@sp 1\n"
4765 "@example\n"
4766 "XPATH //element[@@_name='password']@key{TAB}\n"
4767 "@end example\n"
4768 "@sp 1"
4769 "would clear the content of all @code{password} elements in the data file "
4770 "while leaving off the trailing @key{TAB} would return all @code{password} "
4771 "elements in @abbr{XML} format."
4772 "\n"
4773 "When the @option{--inquire} option is passed then all remaining non-option "
4774 "arguments are retrieved via a server @emph{INQUIRE}."
4775 "\n"
4776 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4777 "expression syntax."
4780 new_command("XPATHATTR", 0, 1, xpathattr_command, _(
4781 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4782 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4783 "attributes and does not return a result. For the @var{SET} operation the "
4784 "@var{value} is optional but the field is required. If not specified then "
4785 "the attribute value will be empty. For example:"
4786 "@sp 1"
4787 "@example\n"
4788 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4789 "@end example\n"
4790 "@sp 1"
4791 "would create an @code{password} attribute for each @code{password} element "
4792 "found in the document. The attribute value will be empty but still exist."
4793 "\n"
4794 "When the @option{--inquire} option is passed then all remaining non-option "
4795 "arguments are retrieved via a server @emph{INQUIRE}."
4796 "\n"
4797 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4798 "expression syntax."
4801 new_command("IMPORT", 0, 1, import_command, _(
4802 "IMPORT [--root [!]element[<TAB>[!]child[..]]] <content>\n"
4803 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4804 "\n"
4805 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4806 "argument is raw @abbr{XML} data. The content is created as a child of "
4807 "the element path specified with the @option{--root} option or at the "
4808 "document root when not specified. Existing elements of the same name will "
4809 "be overwritten."
4810 "\n"
4811 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4812 "for details."
4815 new_command("DUMP", 0, 1, dump_command, _(
4816 "DUMP\n"
4817 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4818 "dumping a specific node."
4821 new_command("LOCK", 0, 0, lock_command, _(
4822 "LOCK\n"
4823 "Locks the mutex associated with the opened file. This prevents other clients "
4824 "from sending commands to the same opened file until the client "
4825 "that sent this command either disconnects or sends the @code{UNLOCK} "
4826 "command. @xref{UNLOCK}."
4829 new_command("UNLOCK", 1, 0, unlock_command, _(
4830 "UNLOCK\n"
4831 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4832 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4833 "@pxref{ISCACHED})."
4836 new_command("GETCONFIG", 1, 1, getconfig_command, _(
4837 "GETCONFIG [filename] <parameter>\n"
4838 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4839 "data response. If no file has been opened then the value for @var{filename} "
4840 "or the default from the @samp{global} section will be returned. If a file "
4841 "has been opened and no @var{filename} is specified, a value previously "
4842 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4845 new_command("OPTION", 1, 1, option_command, _(
4846 "OPTION <NAME>=<VALUE>\n"
4847 "Sets a client option @var{name} to @var{value}. The value for an option is "
4848 "kept for the duration of the connection."
4849 "\n"
4850 "@table @asis\n"
4851 "@item DISABLE-PINENTRY\n"
4852 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
4853 "server inquire is sent to the client to obtain the passphrase. This option "
4854 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4855 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
4856 "\n"
4857 "@item PINENTRY-TIMEOUT\n"
4858 "Sets the number of seconds before a pinentry prompt will return an error "
4859 "while waiting for user input."
4860 "\n"
4861 "@item TTYNAME\n"
4862 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4863 "\n"
4864 "@item TTYTYPE\n"
4865 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4866 "\n"
4867 "@item DISPLAY\n"
4868 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4869 "\n"
4870 "@item PINENTRY-DESC\n"
4871 "Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4872 "\n"
4873 "@item PINENTRY-TITLE\n"
4874 "Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
4875 "\n"
4876 "@item PINENTRY-PROMPT\n"
4877 "Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
4878 "\n"
4879 "@item LC-CTYPE\n"
4880 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4881 "\n"
4882 "@item LC-MESSAGES\n"
4883 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4884 "\n"
4885 "@item NAME\n"
4886 "Associates the thread ID of the connection with the specified textual "
4887 "representation. Useful for debugging log messages."
4888 "\n"
4889 "@item LOCK-TIMEOUT\n"
4890 "When not @code{0}, the duration in tenths of a second to wait for the file "
4891 "mutex which has been locked by another thread to be released before returning "
4892 "an error. When @code{-1}, then an error will be returned immediately."
4893 "\n"
4894 "@item LOG-LEVEL\n"
4895 "An integer specifiying the logging level."
4896 "@end table\n"
4899 new_command("LS", 1, 1, ls_command, _(
4900 "LS\n"
4901 "Lists the available data files stored in the data directory "
4902 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4905 new_command("RESET", 1, 1, NULL, _(
4906 "RESET\n"
4907 "Closes the currently opened file but keeps any previously set client options."
4910 new_command("NOP", 1, 1, NULL, _(
4911 "NOP\n"
4912 "Does nothing. Always returns successfully."
4915 /* !END-HELP-TEXT! */
4916 new_command ("CANCEL", 1, 1, NULL, NULL);
4917 new_command ("END", 1, 1, NULL, NULL);
4918 new_command ("BYE", 1, 1, NULL, NULL);
4920 int i;
4921 for (i = 0; command_table[i]; i++);
4922 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4923 sort_commands);
4926 gpg_error_t
4927 register_commands (assuan_context_t ctx)
4929 int i = 0, rc;
4931 for (; command_table[i]; i++)
4933 if (!command_table[i]->handler)
4934 continue;
4936 rc = assuan_register_command (ctx, command_table[i]->name,
4937 command_table[i]->handler,
4938 command_table[i]->help);
4939 if (rc)
4940 return rc;
4943 rc = assuan_register_bye_notify (ctx, bye_notify);
4944 if (rc)
4945 return rc;
4947 rc = assuan_register_reset_notify (ctx, reset_notify);
4948 if (rc)
4949 return rc;
4951 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4952 if (rc)
4953 return rc;
4955 return assuan_register_post_cmd_notify (ctx, command_finalize);