Return error on no valid signatures.
[pwmd.git] / src / commands.c
blobebdc23c32f058efe5b40632030e3d76f750dfba2
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
3 2016
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of pwmd.
8 Pwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Pwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <err.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <ctype.h>
34 #include <dirent.h>
35 #include <pthread.h>
36 #include <stdint.h>
37 #include <assert.h>
39 #include "pwmd-error.h"
40 #include <gcrypt.h>
42 #include "mem.h"
43 #include "xml.h"
44 #include "util-misc.h"
45 #include "common.h"
46 #include "rcfile.h"
47 #include "cache.h"
48 #include "commands.h"
49 #include "mutex.h"
50 #include "crypto.h"
52 /* These are command option flags. */
53 #define OPT_INQUIRE 0x0001
54 #define OPT_NO_PASSPHRASE 0x0002
55 #define OPT_ASK 0x0004
56 #define OPT_LIST_RECURSE 0x0008
57 #define OPT_VERBOSE 0x0010
58 #define OPT_LOCK 0x0020
59 #define OPT_LOCK_ON_OPEN 0x0040
60 #define OPT_SIGN 0x0080
61 #define OPT_LIST_ALL 0x0100
62 #define OPT_DATA 0x0200
63 #define OPT_NO_AGENT 0x0400
64 #define OPT_SECRET_ONLY 0x0800
65 #define OPT_INQUIRE_KEYID 0x1000
66 #define OPT_INQUIRE_SIGN_KEYID 0x2000
67 #define OPT_SYMMETRIC 0x4000
68 #define OPT_NO_SIGNER 0x8000
69 #define OPT_HTML 0x10000
70 #define OPT_CACHE_AGENT 0x20000
71 #define OPT_CACHE_SIGN 0x40000
73 #define FLOCK_TYPE_NONE 0
74 #define FLOCK_TYPE_SH 0x0001
75 #define FLOCK_TYPE_EX 0x0002
76 #define FLOCK_TYPE_KEEP 0x0004
78 struct command_table_s
80 const char *name;
81 gpg_error_t (*handler) (assuan_context_t, char *line);
82 const char *help;
83 int ignore_startup;
84 int unlock; // unlock the file mutex after validating the checksum
85 uint32_t flock_type;
88 static struct command_table_s **command_table;
90 static gpg_error_t do_lock (struct client_s *client, int add);
91 static gpg_error_t validate_checksum (struct client_s *, const char *filename,
92 struct cache_data_s *, unsigned char **,
93 size_t *);
94 static gpg_error_t update_checksum (struct client_s *client);
96 /* When 'status' is true the 'self' field of the status line will be false
97 * because we never send the STATE status message to the same client that
98 * initiated it. */
99 static char *
100 build_client_info_line (struct client_thread_s *thd, int status)
102 int with_state = config_get_boolean ("global", "send_state");
103 char *uid, *username = NULL;
104 char *line;
106 #ifdef WITH_GNUTLS
107 if (thd->remote)
108 uid = str_asprintf("#%s", thd->tls->fp);
109 else
111 uid = str_asprintf("%u", thd->peer->uid);
112 username = get_username (thd->peer->uid);
114 #else
115 uid = str_asprintf("%u", thd->peer->uid);
116 username = get_username (thd->peer->uid);
117 #endif
118 line = str_asprintf ("%p %s %s %s %u %u %u %s %s %u",
119 thd->tid,
120 thd->name ? thd->name : "-",
121 thd->cl && thd->cl->filename
122 && (thd->cl->flags & FLAG_OPEN)
123 ? thd->cl->filename : "/",
124 #ifdef WITH_GNUTLS
125 thd->remote ? thd->peeraddr : "-",
126 #else
127 "-",
128 #endif
129 thd->cl && thd->cl->flags & FLAG_HAS_LOCK ? 1 : 0,
130 !status && pthread_equal (pthread_self (), thd->tid) ? 1 : 0,
131 with_state ? thd->state : CLIENT_STATE_UNKNOWN, uid,
132 #ifdef WITH_GNUTLS
133 thd->remote ? "-" : username,
134 #else
135 username,
136 #endif
137 thd->conntime
140 xfree (username);
141 xfree (uid);
142 return line;
145 void
146 update_client_state (struct client_s *client, unsigned s)
148 MUTEX_LOCK (&cn_mutex);
149 client->thd->state = s;
150 MUTEX_UNLOCK (&cn_mutex);
151 int n = config_get_boolean ("global", "send_state");
153 if (n && client->thd->state != CLIENT_STATE_UNKNOWN)
155 char *line = build_client_info_line (client->thd, 1);
157 if (line)
158 send_status_all_not_self (n == 2, STATUS_STATE, "%s", line);
160 xfree (line);
164 static gpg_error_t
165 unlock_file_mutex (struct client_s *client, int remove)
167 gpg_error_t rc = 0;
169 // OPEN: keep the lock for the same file being reopened.
170 if (client->flags & FLAG_KEEP_LOCK && client->flags & FLAG_HAS_LOCK)
171 return 0;
173 if (!(client->flags & FLAG_HAS_LOCK))
174 return GPG_ERR_NOT_LOCKED;
176 rc = cache_unlock_mutex (client->md5file, remove);
177 if (rc)
178 rc = GPG_ERR_INV_STATE;
179 else
180 client->flags &= ~(FLAG_HAS_LOCK | FLAG_LOCK_CMD);
182 return rc;
185 static gpg_error_t
186 lock_file_mutex (struct client_s *client, int add)
188 gpg_error_t rc = 0;
189 int timeout = config_get_integer (client->filename, "cache_timeout");
191 if (client->flags & FLAG_HAS_LOCK)
192 return 0;
194 rc = cache_lock_mutex (client->ctx, client->md5file,
195 client->lock_timeout, add, timeout);
196 if (!rc)
197 client->flags |= FLAG_HAS_LOCK;
199 return rc;
202 static gpg_error_t
203 file_modified (struct client_s *client, struct command_table_s *cmd)
205 gpg_error_t rc = 0;
206 int type = !cmd->flock_type || (cmd->flock_type & FLOCK_TYPE_SH)
207 ? LOCK_SH : LOCK_EX;
209 if (!(client->flags & FLAG_OPEN))
210 return GPG_ERR_INV_STATE;
212 rc = lock_file_mutex (client, 0);
213 if (rc && rc != GPG_ERR_NO_DATA)
214 return rc;
216 rc = lock_flock (client->ctx, client->filename, type, &client->flock_fd);
217 if (!rc)
219 rc = validate_checksum (client, client->filename, NULL, NULL, NULL);
220 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
221 rc = 0;
222 else if (rc)
223 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
225 else if (gpg_err_code (rc) == GPG_ERR_ENOENT)
226 rc = 0;
228 /* FLAG_HAS_LOCK may have been set by the LOCK command or OPEN --lock. */
229 if ((cmd->unlock && !(client->flags & FLAG_HAS_LOCK)) || rc)
230 unlock_file_mutex (client, 0);
232 if (rc || !(cmd->flock_type & FLOCK_TYPE_KEEP))
233 unlock_flock (&client->flock_fd);
235 return rc;
238 static gpg_error_t
239 parse_xml (assuan_context_t ctx, int new)
241 struct client_s *client = assuan_get_pointer (ctx);
242 int cached = client->doc != NULL;
243 gpg_error_t rc = 0;
245 if (new)
247 client->doc = xml_new_document ();
248 if (client->doc)
250 xmlChar *result;
251 int len;
253 xmlDocDumpFormatMemory (client->doc, &result, &len, 0);
254 client->crypto->plaintext = result;
255 client->crypto->plaintext_size = len;
256 if (!client->crypto->plaintext)
258 xmlFreeDoc (client->doc);
259 client->doc = NULL;
260 rc = GPG_ERR_ENOMEM;
263 else
264 rc = GPG_ERR_ENOMEM;
266 else if (!cached)
267 rc = xml_parse_doc ((char *) client->crypto->plaintext,
268 client->crypto->plaintext_size,
269 (xmlDocPtr *)&client->doc);
271 return rc;
274 static void
275 free_client (struct client_s *client)
277 if (client->doc)
278 xmlFreeDoc (client->doc);
280 xfree (client->crc);
281 xfree (client->filename);
282 xfree (client->last_error);
283 crypto_free (client->crypto);
284 client->crypto = NULL;
287 void
288 cleanup_client (struct client_s *client)
290 assuan_context_t ctx = client->ctx;
291 struct client_thread_s *thd = client->thd;
292 long lock_timeout = client->lock_timeout;
293 xmlErrorPtr xml_error = client->xml_error;
294 int no_pinentry = (client->flags & FLAG_NO_PINENTRY);
295 int flock_fd = client->flock_fd;
297 unlock_file_mutex (client, client->flags & FLAG_NEW);
298 free_client (client);
299 memset (client, 0, sizeof (struct client_s));
300 client->flock_fd = flock_fd;
301 client->xml_error = xml_error;
302 client->ctx = ctx;
303 client->thd = thd;
304 client->lock_timeout = lock_timeout;
305 client->flags |= no_pinentry ? FLAG_NO_PINENTRY : 0;
308 static void
309 req_cleanup (void *arg)
311 if (!arg)
312 return;
314 strv_free ((char **) arg);
317 static gpg_error_t
318 parse_open_opt_lock (void *data, void *value)
320 struct client_s *client = data;
322 client->opts |= OPT_LOCK_ON_OPEN;
323 return 0;
326 static gpg_error_t
327 parse_opt_inquire (void *data, void *value)
329 struct client_s *client = data;
331 (void) value;
332 client->opts |= OPT_INQUIRE;
333 return 0;
336 static gpg_error_t
337 update_checksum (struct client_s *client)
339 unsigned char *crc;
340 size_t len;
341 struct cache_data_s *cdata;
342 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
344 if (rc)
345 return rc;
347 xfree (client->crc);
348 client->crc = crc;
349 cdata = cache_get_data (client->md5file);
350 if (cdata)
352 xfree (cdata->crc);
353 cdata->crc = xmalloc (len);
354 memcpy (cdata->crc, crc, len);
357 return 0;
360 static gpg_error_t
361 validate_checksum (struct client_s *client, const char *filename,
362 struct cache_data_s *cdata, unsigned char **r_crc,
363 size_t *r_crclen)
365 unsigned char *crc;
366 size_t len;
367 gpg_error_t rc;
368 int n = 0;
370 if (cdata && !cdata->crc)
371 return GPG_ERR_CHECKSUM;
373 rc = get_checksum (filename, &crc, &len);
374 if (rc)
375 return rc;
377 if (cdata)
378 n = memcmp (cdata->crc, crc, len);
379 else if (client->crc)
380 n = memcmp (client->crc, crc, len);
382 if (!n && r_crc)
384 *r_crc = crc;
385 *r_crclen = len;
387 else
388 xfree (crc);
390 return n ? GPG_ERR_CHECKSUM : 0;
393 static gpg_error_t
394 open_command (assuan_context_t ctx, char *line)
396 gpg_error_t rc;
397 struct client_s *client = assuan_get_pointer (ctx);
398 char **req, *filename;
399 unsigned char md5file[16];
400 int same_file = 0;
401 assuan_peercred_t peer;
402 struct cache_data_s *cdata = NULL;
403 int cached = 0;
404 unsigned char *crc = NULL;
405 size_t crclen = 0;
406 struct argv_s *args[] = {
407 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
408 NULL
411 rc = parse_options (&line, args, client, 1);
412 if (rc)
413 return send_error (ctx, rc);
415 req = str_split (line, " ", 2);
416 if (!req)
417 return send_error (ctx, GPG_ERR_SYNTAX);
419 rc = do_validate_peer (ctx, req[0], &peer);
420 if (rc)
422 strv_free (req);
423 return send_error (ctx, rc);
426 filename = req[0];
427 if (!valid_filename (filename))
429 strv_free (req);
430 return send_error (ctx, GPG_ERR_INV_VALUE);
433 pthread_cleanup_push ((void *)req_cleanup, req);
434 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, filename, strlen (filename));
435 /* This client may have locked a different file with ISCACHED --lock than
436 * the current filename. This will remove that lock. */
437 same_file = !memcmp (md5file, client->md5file, 16) ? 1 : 0;
438 if (client->flags & FLAG_OPEN ||
439 (client->flags & FLAG_HAS_LOCK && !same_file))
441 uint32_t opts = client->opts;
442 uint32_t flags = client->flags;
444 if (same_file)
445 client->flags |= FLAG_KEEP_LOCK;
446 else if (client->flags & FLAG_NEW)
447 cache_clear (client->md5file, 0);
449 cleanup_client (client);
450 client->opts = opts;
451 client->flags |= flags;
452 client->flags &= ~(FLAG_LOCK_CMD);
453 if (!same_file)
454 client->flags &= ~(FLAG_HAS_LOCK | FLAG_NEW);
457 memcpy (client->md5file, md5file, 16);
458 client->filename = str_dup (filename);
459 if (!client->filename)
461 strv_free (req);
462 return send_error(ctx, GPG_ERR_ENOMEM);
465 /* Need to lock the mutex here because file_modified() cannot without
466 * knowing the filename. */
467 rc = lock_file_mutex (client, 1);
468 if (rc)
469 client->flags &= ~FLAG_OPEN;
471 if (!rc)
473 rc = lock_flock (ctx, filename, LOCK_SH, &client->flock_fd);
474 if (gpg_err_code (rc) == GPG_ERR_ENOENT)
475 rc = 0;
478 if (!rc)
480 struct stat st;
481 char *keyfile = config_get_string (client->filename, "passphrase_file");
483 rc = crypto_init (&client->crypto, client->ctx, client->filename,
484 client->flags & FLAG_NO_PINENTRY, keyfile);
485 if (rc)
486 xfree (keyfile);
488 if (!rc && stat (client->filename, &st) == -1)
489 rc = gpg_error_from_errno (errno);
490 else if (!rc && !S_ISREG (st.st_mode)) // to match LS output
491 rc = gpg_error_from_errno (ENOANO);
494 if (!rc || gpg_err_code (rc) == GPG_ERR_ENOENT)
496 cdata = cache_get_data (client->md5file);
498 if (rc) // new file
500 rc = 0;
501 client->flags |= FLAG_NEW;
502 // data file disappeared. clear the cache entry.
503 cache_clear (client->md5file, 1);
504 cdata = NULL;
506 else if (cdata && cdata->doc) // cached document
508 int reload = 0;
509 int defer = 0;
511 if (!rc && !(client->flags & FLAG_NEW))
513 rc = validate_checksum (client, client->filename, cdata, &crc,
514 &crclen);
515 if (rc == GPG_ERR_CHECKSUM)
517 rc = 0;
518 reload = 1;
522 if (!rc)
524 rc = cache_iscached (client->filename, &defer, 0, 0);
525 if ((!rc || rc == GPG_ERR_ENOENT) && defer)
527 rc = 0;
528 reload = 2;
532 if (!rc && reload)
534 if (reload == 1)
535 log_write ("%s: %s", client->filename,
536 pwmd_strerror (GPG_ERR_CHECKSUM));
537 cache_clear (client->md5file, 1);
538 cdata = NULL;
539 rc = crypto_decrypt (client, client->crypto);
541 #ifdef WITH_GNUTLS
542 else if (!rc)
544 if (client->thd->remote
545 && config_get_boolean (client->filename, "tcp_require_key")
546 && !(client->flags & FLAG_NEW))
547 rc = crypto_try_decrypt (client,
548 (client->flags & FLAG_NO_PINENTRY));
550 #endif
552 if (!rc && cdata)
554 client->crypto->plaintext = cdata->doc;
555 client->crypto->plaintext_size = cdata->size;
556 rc = cache_decrypt (client->crypto);
557 if (!rc)
559 cdata->doc = NULL;
560 cdata->size = 0;
561 strv_free (client->crypto->pubkey);
562 strv_free (client->crypto->sigkey);
563 client->crypto->pubkey = strv_dup (cdata->pubkey);
564 client->crypto->sigkey = strv_dup (cdata->sigkey);
565 cached = 1;
567 else
569 client->crypto->plaintext = NULL;
570 client->crypto->plaintext_size = 0;
574 else // existing file
576 cached = cdata != NULL;
577 rc = crypto_decrypt (client, client->crypto);
581 if (!rc)
583 rc = parse_xml (ctx, client->flags & FLAG_NEW);
584 if (!rc)
586 rc = cache_encrypt (client->crypto);
587 if (rc)
588 cache_clear (client->md5file, 1);
589 else
591 int timeout = config_get_integer (client->filename,
592 "cache_timeout");
594 free_cache_data_once (cdata);
595 cdata = xcalloc (1, sizeof (struct cache_data_s));
596 cdata->doc = client->crypto->plaintext;
597 cdata->size = client->crypto->plaintext_size;
598 cdata->pubkey = strv_dup(client->crypto->pubkey);
599 cdata->sigkey = strv_dup(client->crypto->sigkey);
600 client->crypto->plaintext = NULL;
601 client->crypto->plaintext_size = 0;
603 if (cached) // wont increment the refcount
605 /* Prevent using another FD to update the checksum for a
606 * cached data file. The validity has already been
607 * verified. */
608 xfree (client->crc);
609 client->crc = xmalloc (crclen);
610 memcpy (client->crc, crc, crclen);
611 xfree (cdata->crc);
612 cdata->crc = xmalloc (crclen);
613 memcpy (cdata->crc, crc, crclen);
615 rc = cache_set_data (client->md5file, cdata);
616 /* The cache entry may have been removed and cache_set_data()
617 * already sent STATUS_CACHE. */
618 if (!cache_iscached (client->filename, NULL, 0, 0))
619 rc = send_status (ctx, STATUS_CACHE, NULL);
621 else
622 rc = cache_add_file (client->md5file, cdata, timeout);
627 pthread_cleanup_pop (1);
628 xfree (crc);
630 if (!rc && !(client->flags & FLAG_NEW) && !cached)
631 rc = update_checksum (client);
633 if (!rc)
635 client->flags |= FLAG_OPEN;
637 if (client->flags & FLAG_NEW)
638 rc = send_status (ctx, STATUS_NEWFILE, NULL);
640 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
641 rc = do_lock (client, 0);
644 if (rc)
646 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
648 if (client->flags & FLAG_NEW)
649 cache_clear (client->md5file, 1);
651 crypto_free (client->crypto);
652 client->crypto = NULL;
653 client->flags &= ~FLAG_OPEN;
655 else
656 crypto_free_non_keys (client->crypto);
658 return send_error (ctx, rc);
661 /* If not the invoking_user or is an existing file, check that the list of user
662 * supplied key ID's are in the list of current key ID's obtained when
663 * decrypting the data file. Both short and long form keyid fingerprints are
664 * valid:
666 * 8 byte with optional "0x" prefix
667 * 16 byte
668 * 40 byte
670 * Although, the key ID's are converted to the 8 byte form before calling this
671 * function in parse_save_opt_keyid_common() to keep consistancy with KEYINFO
672 * output.
674 static gpg_error_t
675 permitted_to_save (struct client_s *client, const char **keys,
676 const char **value)
678 gpg_error_t rc = 0;
679 const char **v;
681 if ((client->flags & FLAG_NEW) || (client->opts & OPT_SYMMETRIC))
682 return 0;
684 rc = peer_is_invoker (client);
685 if (!rc)
686 return 0;
688 /* Empty match. */
689 if ((!value && !keys) || ((value && !*value) && (keys && !*keys)))
690 return 0;
692 for (v = value; v && *v; v++)
694 const char **k, *pv = *v;
695 int match = 0;
697 if (*pv == '0' && *(pv+1) == 'x')
698 pv += 2;
700 for (k = keys; k && *k; k++)
702 const char *pk = *k;
704 if (*pk == '0' && *(pk+1) == 'x')
705 pk += 2;
707 rc = !strcmp (pv, pk) ? 0 : GPG_ERR_FORBIDDEN;
708 if (rc)
709 rc = !strcmp (pv, *k) ? 0 : GPG_ERR_FORBIDDEN;
711 if (!rc)
713 match = 1;
714 break;
718 if (!match)
719 return GPG_ERR_FORBIDDEN;
722 return rc;
725 static gpg_error_t
726 parse_save_opt_keyid_common (struct client_s *client, const char **list,
727 const char *value, char ***dst)
729 gpg_error_t rc;
730 char **p = NULL;
732 if (value && *value)
734 p = str_split (value, ",", 0);
735 if (!p)
736 return GPG_ERR_ENOMEM;
739 crypto_keyid_to_16b (p);
740 rc = permitted_to_save (client, list, (const char **)p);
741 if (!rc)
742 *dst = p;
743 else
744 strv_free (*dst);
746 return rc;
749 static gpg_error_t
750 parse_save_opt_keyid (void *data, void *value)
752 struct client_s *client = data;
753 const char *str = value;
754 char **dst = NULL;
755 gpg_error_t rc;
757 rc = parse_save_opt_keyid_common (client,
758 (const char **)client->crypto->pubkey,
759 str, &dst);
760 if (rc)
761 return rc;
763 client->crypto->save.pubkey = dst;
764 return 0;
767 static gpg_error_t
768 parse_save_opt_sign_keyid (void *data, void *value)
770 struct client_s *client = data;
771 const char *str = value;
772 char **dst = NULL;
773 gpg_error_t rc;
775 rc = parse_save_opt_keyid_common (client,
776 (const char **)client->crypto->sigkey,
777 str, &dst);
778 if (rc)
779 return rc;
781 if (!dst)
782 client->opts |= OPT_NO_SIGNER;
784 client->crypto->save.sigkey = dst;
785 return 0;
788 static gpg_error_t
789 parse_save_opt_inquire (struct client_s *client, uint32_t opt)
791 if (opt == OPT_INQUIRE && !(client->flags & FLAG_NEW))
793 gpg_error_t rc = peer_is_invoker (client);
795 if (rc)
796 return rc;
799 client->opts |= opt;
800 return 0;
803 static gpg_error_t
804 parse_save_opt_inquire_keyparam (void *data, void *value)
806 return parse_save_opt_inquire (data, OPT_INQUIRE);
809 static gpg_error_t
810 parse_save_opt_inquire_keyid (void *data, void *value)
812 return parse_save_opt_inquire (data, OPT_INQUIRE_KEYID);
815 static gpg_error_t
816 parse_save_opt_inquire_sign_keyid (void *data, void *value)
818 return parse_save_opt_inquire (data, OPT_INQUIRE_SIGN_KEYID);
821 static gpg_error_t
822 parse_save_opt_symmetric (void *data, void *value)
824 struct client_s *client = data;
826 client->opts |= OPT_SYMMETRIC;
827 return 0;
830 /* Tests that the keys in new_keys are also in old_keys. */
831 static gpg_error_t
832 compare_keys (char **new_keys, char **old_keys)
834 char **o;
836 if (!old_keys || !*old_keys)
837 return 0;
839 crypto_keyid_to_16b (new_keys);
841 for (o = old_keys; *o; o++)
843 char **n;
845 for (n = new_keys; *n; n++)
847 if (!strcmp (*n, *o))
848 break;
851 if (!*n)
852 return GPG_ERR_NOT_FOUND;
855 return 0;
858 static gpg_error_t
859 inquire_keyid (struct client_s *client, uint32_t opt)
861 gpg_error_t rc;
862 unsigned char *result = NULL;
863 size_t len;
864 char *s;
865 char ***orig;
866 char ***save;
868 if (opt == OPT_INQUIRE_KEYID)
870 s = "INQUIRE_KEYID";
871 orig = &client->crypto->pubkey;
872 save = &client->crypto->save.pubkey;
874 else
876 s = "INQUIRE_SIGN_KEYID";
877 orig = &client->crypto->sigkey;
878 save = &client->crypto->save.sigkey;
881 rc = assuan_inquire (client->ctx, s, &result, &len, 0);
882 if (!rc)
884 char **dst = NULL;
886 rc = parse_save_opt_keyid_common (client, (const char **)*orig,
887 (char *)result, &dst);
888 if (!rc)
890 if (!dst && opt == OPT_INQUIRE_SIGN_KEYID
891 && client->opts & OPT_SYMMETRIC)
892 client->opts |= OPT_NO_SIGNER;
894 *save = dst;
898 xfree (result);
899 return rc;
902 /* The caching test of gpg-agent is both a blessing and a curse. When a key
903 * lookup in its cache fails it tries the last successful key to be unlocked.
904 * This prevents pwmd from successfully clearing a signing key since the
905 * previous key, which more than likely belongs to the same keypair as the
906 * encryption/decryption key, will have the passphrase cached and therefore the
907 * signing key also cached. So we need to clear both the signing and encryption
908 * keys to get the effect of requiring a passphrase when generating a new
909 * keypair for an existing data file, or when the "require_save_key"
910 * configuration parameter is set. The "require_save_key" parameter is mostly a
911 * failsafe of the gpg-agent option --ignore-cache-for-signing since
912 * some/most/all users may fail to set it.
914 static gpg_error_t
915 save_command (assuan_context_t ctx, char *line)
917 struct client_s *client = assuan_get_pointer (ctx);
918 struct cache_data_s *cdata = NULL;
919 gpg_error_t rc = 0, cached = 0;
920 int defer = 0;
921 struct stat st;
922 struct argv_s *args[] = {
923 &(struct argv_s) {"keyid", OPTION_TYPE_ARG, parse_save_opt_keyid},
924 &(struct argv_s) {"sign-keyid", OPTION_TYPE_OPTARG,
925 parse_save_opt_sign_keyid},
926 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
927 parse_save_opt_inquire_keyparam },
928 &(struct argv_s) {"inquire-keyid", OPTION_TYPE_NOARG,
929 parse_save_opt_inquire_keyid },
930 &(struct argv_s) {"inquire-sign-keyid", OPTION_TYPE_NOARG,
931 parse_save_opt_inquire_sign_keyid },
932 &(struct argv_s) {"symmetric", OPTION_TYPE_NOARG,
933 parse_save_opt_symmetric },
934 NULL
937 crypto_free_save (&client->crypto->save);
939 if (!(client->flags & FLAG_NEW))
941 rc = crypto_is_symmetric (client->filename);
942 if (!rc || rc == GPG_ERR_BAD_DATA)
944 if (!rc)
945 client->opts |= OPT_SYMMETRIC;
947 rc = 0; // PKI
951 if (rc)
952 return send_error (ctx, rc);
954 rc = parse_options (&line, args, client, 0);
955 if (rc)
956 return send_error (ctx, rc);
958 if (config_get_boolean (client->filename, "require_save_key"))
959 client->opts |= OPT_ASK;
961 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
962 return send_error (ctx, gpg_error_from_errno (errno));
964 if (errno != ENOENT && !S_ISREG (st.st_mode))
966 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
967 return send_error (ctx, GPG_ERR_ENOANO);
970 if (!rc)
971 cached = rc = cache_iscached (client->filename, &defer, 0, 1);
973 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
974 rc = 0;
975 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
976 rc = 0;
978 if (rc)
979 return send_error (ctx, rc);
981 /* Specifying both a recipient and symmetric encryption is an error. */
982 if (client->opts & OPT_INQUIRE_KEYID && client->opts & OPT_SYMMETRIC)
984 return send_error (ctx, GPG_ERR_CONFLICT);
986 else if (client->opts & OPT_INQUIRE && client->opts & OPT_SYMMETRIC)
988 return send_error (ctx, GPG_ERR_CONFLICT);
990 /* Existing file with a recipient and wanting symmetric is an error. */
991 else if (client->opts & OPT_SYMMETRIC && client->crypto->save.pubkey)
993 return send_error (ctx, GPG_ERR_CONFLICT);
995 else if (((client->opts & OPT_INQUIRE_KEYID)
996 || (client->opts & OPT_INQUIRE_SIGN_KEYID)))
998 if (client->opts & OPT_INQUIRE)
999 return send_error (ctx, GPG_ERR_CONFLICT);
1001 if (client->opts & OPT_INQUIRE_KEYID)
1002 rc = inquire_keyid (client, OPT_INQUIRE_KEYID);
1004 if (!rc && (client->opts & OPT_INQUIRE_SIGN_KEYID))
1005 rc = inquire_keyid (client, OPT_INQUIRE_SIGN_KEYID);
1007 else if ((client->crypto->save.pubkey || client->crypto->save.sigkey)
1008 && client->opts & OPT_INQUIRE)
1009 return send_error (ctx, GPG_ERR_CONFLICT);
1011 if (!rc)
1013 client->crypto->keyfile = config_get_string (client->filename,
1014 "passphrase_file");
1015 rc = crypto_init_ctx (client->crypto, (client->flags & FLAG_NO_PINENTRY),
1016 client->crypto->keyfile);
1019 if (!rc && (client->opts & OPT_INQUIRE))
1021 /* Require a passphrase when generating a new key pair for an existing
1022 * file.
1024 if (!(client->flags & FLAG_NEW))
1026 rc = cache_clear_agent_keys (client->md5file, 1, 1);
1027 if (!rc)
1029 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
1030 client->opts &= ~OPT_ASK;
1034 if (!rc)
1036 unsigned char *params = NULL;
1037 size_t len;
1039 rc = assuan_inquire (client->ctx, "KEYPARAM", &params, &len, 0);
1040 if (!rc)
1042 client->crypto->flags |= CRYPTO_FLAG_NEWFILE;
1043 pthread_cleanup_push ((void *)xfree, params);
1044 rc = crypto_genkey (client, client->crypto, params);
1045 pthread_cleanup_pop (1);
1049 else if (!rc && (client->flags & FLAG_NEW))
1051 if (!client->crypto->save.pubkey && !client->crypto->save.sigkey
1052 && !(client->opts & OPT_SYMMETRIC))
1054 char *params = crypto_default_key_params ();
1056 if (!params)
1057 rc = GPG_ERR_ENOMEM;
1059 if (!rc)
1061 client->crypto->flags |= CRYPTO_FLAG_NEWFILE;
1062 pthread_cleanup_push ((void *)xfree, params);
1063 rc = crypto_genkey (client, client->crypto,
1064 (unsigned char *)params);
1065 pthread_cleanup_pop (1);
1068 else
1070 if (!client->crypto->save.pubkey && client->crypto->save.sigkey
1071 && !(client->opts & OPT_SYMMETRIC))
1072 client->crypto->save.pubkey = strv_dup (client->crypto->save.sigkey);
1074 if (client->crypto->save.pubkey && !client->crypto->save.sigkey)
1075 client->crypto->save.sigkey = strv_dup (client->crypto->save.pubkey);
1078 else if (!rc)
1080 cdata = cache_get_data (client->md5file);
1081 if (cdata)
1083 if (client->crypto->save.pubkey)
1084 rc = compare_keys (client->crypto->save.pubkey, cdata->pubkey);
1086 /* Always allow a signer for symmetric data files. */
1087 if (!rc && client->crypto->save.sigkey
1088 && !(client->opts & OPT_SYMMETRIC))
1089 rc = compare_keys (client->crypto->save.sigkey, cdata->sigkey);
1091 /* Prevent saving to a recipient who is not in the original recipient
1092 * list without a passphrase. */
1093 if (rc || (client->crypto->sigkey && client->opts & OPT_NO_SIGNER))
1094 client->opts |= OPT_ASK;
1096 if (rc == GPG_ERR_NOT_FOUND)
1098 rc = peer_is_invoker (client);
1099 if (rc == GPG_ERR_EACCES)
1100 rc = GPG_ERR_FORBIDDEN;
1103 if (!client->crypto->save.pubkey
1104 && !(client->opts & OPT_SYMMETRIC))
1105 client->crypto->save.pubkey = strv_dup (cdata->pubkey);
1107 if (!rc && !client->crypto->save.sigkey && cdata->sigkey
1108 && !(client->opts & OPT_NO_SIGNER))
1109 client->crypto->save.sigkey = strv_dup (cdata->sigkey);
1110 else if (!rc && !client->crypto->save.sigkey
1111 && (client->opts & OPT_NO_SIGNER)
1112 && !(client->opts & OPT_SYMMETRIC))
1113 rc = GPG_ERR_NO_SIGNATURE_SCHEME;
1115 else
1117 client->crypto->save.pubkey = strv_dup (client->crypto->pubkey);
1118 client->crypto->save.sigkey = strv_dup (client->crypto->sigkey);
1122 if (!rc && !(client->flags & FLAG_NEW))
1124 /* Fixes {NEW_,SIGN_}PASSPHRASE inquire keywords. See passphrase_cb(). */
1125 if (client->opts & OPT_SYMMETRIC)
1126 client->crypto->flags |= CRYPTO_FLAG_PASSWD;
1128 if (client->opts & OPT_ASK)
1130 rc = cache_clear_agent_keys (client->md5file, 1, 1);
1131 if (!rc)
1132 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
1136 if (!rc && client->opts & OPT_SYMMETRIC)
1137 client->crypto->flags |= CRYPTO_FLAG_PASSWD_NEW;
1139 if (!rc)
1140 rc = xml_update_element_mtime (client, xmlDocGetRootElement (client->doc));
1142 if (!rc)
1144 int size;
1146 xmlDocDumpFormatMemory (client->doc, &client->crypto->plaintext, &size,
1148 if (size > 0)
1149 client->crypto->plaintext_size = (size_t) size;
1150 else
1151 rc = GPG_ERR_ENOMEM;
1153 if (!rc)
1155 client->crypto->flags |= (client->opts & OPT_SYMMETRIC) ? CRYPTO_FLAG_SYMMETRIC : 0;
1156 client->crypto->flags |= (client->flags & FLAG_NEW) ? CRYPTO_FLAG_NEWFILE : 0;
1157 rc = crypto_encrypt (client, client->crypto);
1158 client->crypto->flags &= ~CRYPTO_FLAG_SYMMETRIC;
1161 if (!rc)
1163 rc = crypto_write_file (client->crypto);
1164 if (!rc)
1166 if (!cached) // no error
1167 cdata = cache_get_data (client->md5file);
1170 if (!rc)
1172 rc = cache_encrypt (client->crypto);
1173 if (rc)
1175 cache_clear (client->md5file, 1);
1176 strv_free (client->crypto->pubkey);
1177 client->crypto->pubkey = strv_dup (client->crypto->save.pubkey);
1178 strv_free (client->crypto->sigkey);
1179 client->crypto->sigkey = strv_dup (client->crypto->save.sigkey);
1180 client->flags &= ~(FLAG_NEW);
1184 if (!rc)
1186 int timeout = config_get_integer (client->filename,
1187 "cache_timeout");
1189 free_cache_data_once (cdata);
1190 cdata = xcalloc (1, sizeof (struct cache_data_s));
1191 cdata->doc = client->crypto->plaintext;
1192 client->crypto->plaintext = NULL;
1193 cdata->size = client->crypto->plaintext_size;
1194 client->crypto->plaintext_size = 0;
1195 cdata->pubkey = client->crypto->save.pubkey;
1196 client->crypto->save.pubkey = NULL;
1197 cdata->sigkey = client->crypto->save.sigkey;
1198 client->crypto->save.sigkey = NULL;
1200 /* Update in case the cache entry expires the next SAVE may not
1201 * have any known keys. */
1202 strv_free (client->crypto->pubkey);
1203 client->crypto->pubkey = strv_dup (cdata->pubkey);
1204 strv_free (client->crypto->sigkey);
1205 client->crypto->sigkey = strv_dup (cdata->sigkey);
1207 if (!cached) // no error and wont increment refcount
1208 rc = cache_set_data (client->md5file, cdata);
1209 else
1210 rc = cache_add_file (client->md5file, cdata, timeout);
1212 if (!rc && (cached || (client->flags & FLAG_NEW)))
1213 send_status_all (STATUS_CACHE, NULL);
1215 if (!rc)
1217 rc = update_checksum (client);
1218 client->flags &= ~(FLAG_NEW);
1224 crypto_free_non_keys (client->crypto);
1225 return send_error (ctx, rc);
1228 static gpg_error_t
1229 do_delete (assuan_context_t ctx, char *line)
1231 struct client_s *client = assuan_get_pointer (ctx);
1232 struct xml_request_s *req;
1233 xmlNodePtr n;
1234 gpg_error_t rc;
1236 rc = xml_new_request (client, line, XML_CMD_DELETE, &req);
1237 if (rc)
1238 return rc;
1240 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1241 xml_free_request (req);
1242 if (rc)
1243 return rc;
1245 rc = xml_is_element_owner (client, n);
1246 if (!rc)
1247 rc = xml_unlink_node (client, n);
1249 return rc;
1252 static gpg_error_t
1253 delete_command (assuan_context_t ctx, char *line)
1255 struct client_s *client = assuan_get_pointer (ctx);
1256 gpg_error_t rc;
1257 struct argv_s *args[] = {
1258 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1259 NULL
1262 rc = parse_options (&line, args, client, 1);
1263 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1264 rc = GPG_ERR_SYNTAX;
1265 if (rc)
1266 return send_error (ctx, rc);
1268 if (client->opts & OPT_INQUIRE)
1270 unsigned char *result;
1271 size_t len;
1273 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1274 if (rc)
1275 return send_error (ctx, rc);
1277 pthread_cleanup_push ((void *)xfree, result);
1278 rc = do_delete (ctx, (char *)result);
1279 pthread_cleanup_pop (1);
1281 else
1282 rc = do_delete (ctx, line);
1284 return send_error (ctx, rc);
1287 static gpg_error_t
1288 update_element_expirey (struct client_s *client, xmlNodePtr n)
1290 gpg_error_t rc = 0;
1291 xmlChar *expire, *incr;
1292 char *p;
1293 time_t e, e_orig, i;
1294 time_t now = time (NULL);
1296 expire = xml_attribute_value (n, (xmlChar *)"expire");
1297 if (!expire)
1298 return 0;
1300 errno = 0;
1301 e = strtoul ((char *)expire, &p, 10);
1302 if (errno || *p || e == ULONG_MAX || *expire == '-')
1304 xmlFree (expire);
1305 rc = GPG_ERR_ERANGE;
1306 goto fail;
1309 xmlFree (expire);
1310 incr = xml_attribute_value (n, (xmlChar *)"expire_increment");
1311 if (!incr)
1312 return 0;
1314 i = strtoul ((char *)incr, &p, 10);
1315 if (errno || *p || i == ULONG_MAX || *incr == '-')
1317 xmlFree (incr);
1318 rc = GPG_ERR_ERANGE;
1319 goto fail;
1322 xmlFree (incr);
1323 e_orig = e;
1324 e = now + i;
1325 p = str_asprintf ("%lu", e);
1326 if (!p)
1328 rc = GPG_ERR_ENOMEM;
1329 goto fail;
1332 rc = xml_add_attribute (client, n, "expire", p);
1333 xfree (p);
1334 if (!rc)
1335 rc = send_status (client->ctx, STATUS_EXPIRE, "%lu %lu", e_orig, e);
1337 fail:
1338 return rc;
1341 static gpg_error_t
1342 store_command (assuan_context_t ctx, char *line)
1344 struct client_s *client = assuan_get_pointer (ctx);
1345 gpg_error_t rc;
1346 size_t len;
1347 unsigned char *result;
1348 xmlNodePtr n, parent;
1349 int has_content;
1350 char *content = NULL;
1351 struct xml_request_s *req;
1353 if (line && *line)
1354 return send_error (ctx, GPG_ERR_SYNTAX);
1356 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1357 if (rc)
1358 return send_error (ctx, rc);
1360 rc = xml_new_request (client, (char *)result, XML_CMD_STORE, &req);
1361 xfree (result);
1362 if (rc)
1363 return send_error (ctx, rc);
1365 /* Prevent passing the element content around to save some memory. */
1366 len = strv_length (req->args);
1367 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1368 if (*(req->args+1) && !xml_valid_element_path (req->args, has_content))
1370 xml_free_request (req);
1371 return send_error (ctx, GPG_ERR_INV_VALUE);
1374 if (has_content || !*req->args[len-1])
1376 has_content = 1;
1377 content = req->args[len-1];
1378 req->args[len-1] = NULL;
1381 if (strv_length (req->args) > 1)
1383 rc = xml_check_recursion (client, req);
1384 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
1385 goto fail;
1388 parent = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1390 if (!rc && len > 1)
1392 rc = xml_is_element_owner (client, parent);
1393 if (!rc)
1395 n = xml_find_text_node (parent->children);
1396 if (n)
1397 xmlNodeSetContent (n, (xmlChar *) content);
1398 else
1399 xmlNodeAddContent (parent, (xmlChar *) content);
1401 (void)xml_update_element_mtime (client, parent);
1402 (void)update_element_expirey (client, parent);
1406 fail:
1407 xfree (content);
1408 xml_free_request (req);
1409 return send_error (ctx, rc);
1412 static gpg_error_t
1413 xfer_data (assuan_context_t ctx, const char *line, int total)
1415 struct client_s *client = assuan_get_pointer (ctx);
1416 int to_send;
1417 int sent = 0;
1418 gpg_error_t rc = 0;
1419 int progress = config_get_integer ("global", "xfer_progress");
1420 int flush = 0;
1422 if (!(client->flags & FLAG_LOCK_CMD))
1423 rc = unlock_file_mutex (client, 0);
1424 if (rc && rc != GPG_ERR_NOT_LOCKED)
1425 return rc;
1427 progress =
1428 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1429 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1430 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1432 if (rc)
1433 return rc;
1435 again:
1438 if (sent + to_send > total)
1439 to_send = total - sent;
1441 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1442 flush ? 0 : to_send);
1443 if (!rc)
1445 sent += flush ? 0 : to_send;
1447 if ((progress && !(sent % progress) && sent != total) ||
1448 (sent == total && flush))
1449 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1451 if (!flush && !rc && sent == total)
1453 flush = 1;
1454 goto again;
1458 while (!rc && sent < total);
1460 return rc;
1463 static gpg_error_t
1464 do_get (assuan_context_t ctx, char *line)
1466 struct client_s *client = assuan_get_pointer (ctx);
1467 gpg_error_t rc;
1468 struct xml_request_s *req;
1469 xmlNodePtr n;
1471 rc = xml_new_request (client, line, XML_CMD_NONE, &req);
1472 if (rc)
1473 return rc;
1475 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1476 if (!rc)
1478 if (n && n->children)
1480 xmlNodePtr tmp = n;
1482 n = xml_find_text_node (n->children);
1483 if (!n || !n->content || !*n->content)
1484 rc = GPG_ERR_NO_DATA;
1485 else
1487 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1488 if (!rc)
1490 xmlChar *expire = xml_attribute_value (tmp,
1491 (xmlChar *)"expire");
1492 if (expire)
1494 time_t now = time (NULL);
1495 time_t e;
1496 char *p;
1498 errno = 0;
1499 e = strtoul ((char *)expire, &p, 10);
1500 if (errno || *p || e == ULONG_MAX || *expire == '-')
1501 log_write (_("invalid expire attribute value: %s"),
1502 expire);
1503 else if (now >= e)
1504 rc = send_status (ctx, STATUS_EXPIRE, "%lu 0", e);
1506 xmlFree (expire);
1511 else
1512 rc = GPG_ERR_NO_DATA;
1515 xml_free_request (req);
1516 return rc;
1519 static gpg_error_t
1520 get_command (assuan_context_t ctx, char *line)
1522 struct client_s *client = assuan_get_pointer (ctx);
1523 gpg_error_t rc;
1524 struct argv_s *args[] = {
1525 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1526 NULL
1529 rc = parse_options (&line, args, client, 1);
1530 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1531 rc = GPG_ERR_SYNTAX;
1532 if (rc)
1533 return send_error (ctx, rc);
1535 if (client->opts & OPT_INQUIRE)
1537 unsigned char *result;
1538 size_t len;
1540 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1541 if (rc)
1542 return send_error (ctx, rc);
1544 pthread_cleanup_push ((void *)xfree, result);
1545 rc = do_get (ctx, (char *)result);
1546 pthread_cleanup_pop (1);
1548 else
1549 rc = do_get (ctx, line);
1551 return send_error (ctx, rc);
1554 static void list_command_cleanup1 (void *arg);
1555 static gpg_error_t
1556 realpath_command (assuan_context_t ctx, char *line)
1558 gpg_error_t rc;
1559 char **realpath = NULL;
1560 struct client_s *client = assuan_get_pointer (ctx);
1561 struct argv_s *args[] = {
1562 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1563 NULL
1566 rc = parse_options (&line, args, client, 1);
1567 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1568 rc = GPG_ERR_SYNTAX;
1569 if (rc)
1570 return send_error (ctx, rc);
1572 if (client->opts & OPT_INQUIRE)
1574 unsigned char *result;
1575 size_t len;
1577 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1578 if (rc)
1579 return send_error (ctx, rc);
1581 pthread_cleanup_push ((void *)xfree, result);
1582 (void)xml_resolve_path (client, client->doc, result, &realpath, &rc);
1583 pthread_cleanup_pop (1);
1585 else
1586 (void)xml_resolve_path (client, client->doc, (unsigned char *)line,
1587 &realpath, &rc);
1589 if (!rc)
1591 char *tmp = strv_join ("\t", realpath);
1593 strv_free (realpath);
1594 if (!tmp)
1595 return send_error (ctx, GPG_ERR_ENOMEM);
1597 pthread_cleanup_push ((void *)xfree, tmp);
1598 rc = xfer_data (ctx, tmp, strlen (tmp));
1599 pthread_cleanup_pop (1);
1602 return send_error (ctx, rc);
1605 static void
1606 list_command_cleanup1 (void *arg)
1608 if (arg)
1609 string_free ((struct string_s *) arg, 1);
1612 static gpg_error_t
1613 parse_list_opt_recurse (void *data, void *value)
1615 struct client_s *client = data;
1617 client->opts |= OPT_LIST_RECURSE;
1618 return 0;
1621 static gpg_error_t
1622 parse_opt_verbose (void *data, void *value)
1624 struct client_s *client = data;
1626 client->opts |= OPT_VERBOSE;
1627 return 0;
1630 static gpg_error_t
1631 list_path_once (struct client_s *client, char *line,
1632 struct element_list_s *elements, struct string_s *result)
1634 gpg_error_t rc;
1636 rc = xml_create_path_list (client, client->doc, NULL, elements, line, 0);
1637 if (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES)
1638 rc = 0;
1640 if (!rc)
1642 int total = slist_length (elements->list);
1644 if (total)
1646 int i;
1648 for (i = 0; i < total; i++)
1650 char *tmp = slist_nth_data (elements->list, i);
1652 string_append_printf (result, "%s%s", tmp,
1653 i + 1 == total ? "" : "\n");
1656 else
1657 rc = GPG_ERR_NO_DATA;
1660 return rc;
1663 static gpg_error_t
1664 do_list (assuan_context_t ctx, char *line)
1666 struct client_s *client = assuan_get_pointer (ctx);
1667 gpg_error_t rc = GPG_ERR_NO_DATA;
1668 struct element_list_s *elements = NULL;
1670 if (!line || !*line)
1672 struct string_s *str = string_new (NULL);
1673 xmlNodePtr n;
1675 if (!str)
1676 return GPG_ERR_ENOMEM;
1678 pthread_cleanup_push ((void *)list_command_cleanup1, str);
1679 n = xmlDocGetRootElement (client->doc);
1680 for (n = n->children; n; n = n->next)
1682 xmlChar *name;
1684 if (n->type != XML_ELEMENT_NODE)
1685 continue;
1687 name = xmlGetProp (n, (xmlChar *)"_name");
1688 if (!name)
1690 rc = GPG_ERR_ENOMEM;
1691 break;
1694 elements = xcalloc (1, sizeof (struct element_list_s));
1695 if (!elements)
1697 xmlFree (name);
1698 rc = GPG_ERR_ENOMEM;
1699 break;
1702 elements->prefix = string_new (NULL);
1703 if (!elements->prefix)
1705 xmlFree (name);
1706 xml_free_element_list (elements);
1707 rc = GPG_ERR_ENOMEM;
1708 break;
1711 elements->recurse = client->opts & OPT_LIST_RECURSE;
1712 elements->root_only = !elements->recurse;
1713 pthread_cleanup_push ((void *)xml_free_element_list, elements);
1714 rc = list_path_once (client, (char *)name, elements, str);
1715 xmlFree (name);
1716 pthread_cleanup_pop (1);
1717 if (rc)
1718 break;
1720 if (n->next)
1721 string_append (str, "\n");
1724 if (!rc)
1725 rc = xfer_data (ctx, str->str, str->len);
1727 pthread_cleanup_pop (1);
1728 return rc;
1731 elements = xcalloc (1, sizeof (struct element_list_s));
1732 if (!elements)
1733 return GPG_ERR_ENOMEM;
1735 elements->prefix = string_new (NULL);
1736 if (!elements->prefix)
1738 xml_free_element_list (elements);
1739 return GPG_ERR_ENOMEM;
1742 elements->recurse = client->opts & OPT_LIST_RECURSE;
1743 pthread_cleanup_push ((void *)xml_free_element_list, elements);
1744 struct string_s *str = string_new (NULL);
1745 pthread_cleanup_push ((void *)list_command_cleanup1, str);
1746 rc = list_path_once (client, line, elements, str);
1747 if (!rc)
1748 rc = xfer_data (ctx, str->str, str->len);
1750 pthread_cleanup_pop (1);
1751 pthread_cleanup_pop (1);
1752 return rc;
1755 static gpg_error_t
1756 list_command (assuan_context_t ctx, char *line)
1758 struct client_s *client = assuan_get_pointer (ctx);
1759 gpg_error_t rc;
1760 struct argv_s *args[] = {
1761 &(struct argv_s) {"recurse", OPTION_TYPE_NOARG, parse_list_opt_recurse},
1762 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1763 /* These are deprecated but kept for backward compatibility with older
1764 * clients. */
1765 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, NULL},
1766 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG, NULL},
1767 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_recurse},
1768 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG, NULL},
1769 NULL
1772 if (disable_list_and_dump == 1)
1773 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1775 rc = parse_options (&line, args, client, 1);
1776 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1777 rc = GPG_ERR_SYNTAX;
1778 if (rc)
1779 return send_error (ctx, rc);
1781 if (client->opts & OPT_INQUIRE)
1783 unsigned char *result;
1784 size_t len;
1786 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1787 if (rc)
1788 return send_error (ctx, rc);
1790 pthread_cleanup_push ((void *)xfree, result);
1791 rc = do_list (ctx, (char *)result);
1792 pthread_cleanup_pop (1);
1794 else
1795 rc = do_list (ctx, line);
1797 return send_error (ctx, rc);
1800 #define RESUMABLE_ERROR(rc) (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES \
1801 || rc == GPG_ERR_ELEMENT_NOT_FOUND || !rc)
1803 * args[0] - element path
1805 static gpg_error_t
1806 attribute_list (assuan_context_t ctx, char **args)
1808 struct client_s *client = assuan_get_pointer (ctx);
1809 char **attrlist = NULL;
1810 int i = 0;
1811 int target = 0;
1812 xmlAttrPtr a;
1813 xmlNodePtr n, an, r;
1814 char *line;
1815 gpg_error_t rc;
1816 struct xml_request_s *req;
1818 if (!args || !args[0] || !*args[0])
1819 return GPG_ERR_SYNTAX;
1821 rc = xml_new_request (client, args[0], XML_CMD_ATTR_LIST, &req);
1822 if (rc)
1823 return rc;
1825 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1826 xml_free_request (req);
1827 if (rc)
1828 return rc;
1830 again:
1831 for (a = n->properties; a; a = a->next)
1833 char **pa;
1835 if (target && xml_reserved_attribute ((char *)a->name))
1836 continue;
1838 pa = xrealloc (attrlist, (i + 2) * sizeof (char *));
1839 if (!pa)
1841 if (attrlist)
1842 strv_free (attrlist);
1844 log_write ("%s(%i): %s", __FILE__, __LINE__,
1845 pwmd_strerror (GPG_ERR_ENOMEM));
1846 return GPG_ERR_ENOMEM;
1849 attrlist = pa;
1850 an = a->children;
1851 attrlist[i] = str_asprintf ("%s %s", (char *) a->name, an && an->content
1852 ? (char *)an->content : "");
1854 if (!attrlist[i])
1856 strv_free (attrlist);
1857 log_write ("%s(%i): %s", __FILE__, __LINE__,
1858 pwmd_strerror (GPG_ERR_ENOMEM));
1859 return GPG_ERR_ENOMEM;
1862 attrlist[++i] = NULL;
1865 if (!attrlist)
1866 return GPG_ERR_NO_DATA;
1868 if (!target)
1870 r = xml_resolve_path (client, client->doc, (xmlChar *)args[0], NULL, &rc);
1871 if (!RESUMABLE_ERROR (rc))
1873 strv_free (attrlist);
1874 return rc;
1876 else if (!rc && r != n)
1878 target = 1;
1879 n = r;
1880 goto again;
1883 rc = 0;
1886 line = strv_join ("\n", attrlist);
1887 if (!line)
1889 log_write ("%s(%i): %s", __FILE__, __LINE__,
1890 pwmd_strerror (GPG_ERR_ENOMEM));
1891 strv_free (attrlist);
1892 return GPG_ERR_ENOMEM;
1895 pthread_cleanup_push ((void *)xfree, line);
1896 pthread_cleanup_push ((void *)req_cleanup, attrlist);
1897 rc = xfer_data (ctx, line, strlen (line));
1898 pthread_cleanup_pop (1);
1899 pthread_cleanup_pop (1);
1900 return rc;
1904 * args[0] - attribute
1905 * args[1] - element path
1907 static gpg_error_t
1908 attribute_delete (struct client_s *client, char **args)
1910 gpg_error_t rc;
1911 xmlNodePtr n;
1913 if (!args || !args[0] || !*args[0] || !args[1] || !*args[1])
1914 return GPG_ERR_SYNTAX;
1916 if (!strcmp (args[0], "_name") || !strcmp (args[0], "target"))
1917 return GPG_ERR_INV_ATTR;
1919 if (!xml_reserved_attribute (args[0]))
1920 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
1921 else
1923 struct xml_request_s *req;
1925 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
1926 if (rc)
1927 return rc;
1929 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1930 xml_free_request (req);
1933 if (!rc)
1934 rc = xml_is_element_owner (client, n);
1936 if (!rc)
1937 rc = xml_delete_attribute (client, n, (xmlChar *) args[0]);
1939 return rc;
1943 * Creates the "target" attribute. When other commands encounter an element
1944 * with this attribute they follow the "target" after appending remaining
1945 * elements from the original element path to the target. The exception is the
1946 * ATTR command that operates on the element itself (for reserved attribute
1947 * names) and not the target.
1949 * If the source element path doesn't exist when using 'ATTR SET target', it is
1950 * created, but the destination element path must exist. This is simliar to the
1951 * ls(1) command: ln -s src dst.
1953 * args[0] - source element path
1954 * args[1] - destination element path
1956 static gpg_error_t
1957 set_target_attribute (struct client_s *client, char **args)
1959 struct xml_request_s *req = NULL;
1960 char **src, **dst;
1961 gpg_error_t rc;
1962 xmlNodePtr n;
1964 if (!args || !args[0] || !args[1])
1965 return GPG_ERR_SYNTAX;
1967 src = str_split (args[0], "\t", 0);
1968 if (!src)
1969 return GPG_ERR_SYNTAX;
1971 if (!xml_valid_element_path (src, 0))
1973 strv_free (src);
1974 return GPG_ERR_INV_VALUE;
1977 dst = str_split (args[1], "\t", 0);
1978 if (!dst)
1980 strv_free (src);
1981 return GPG_ERR_SYNTAX;
1984 // Be sure the destination element path exists. */
1985 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
1986 if (rc)
1987 goto fail;
1989 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1990 if (rc && rc != GPG_ERR_EACCES)
1991 goto fail;
1993 xml_free_request (req);
1994 req = NULL;
1996 if (!strcmp (args[0], args[1]))
1998 rc = GPG_ERR_EEXIST;
1999 goto fail;
2002 rc = xml_new_request (client, args[0], XML_CMD_ATTR_TARGET, &req);
2003 if (rc)
2004 goto fail;
2006 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2007 if (rc && rc != GPG_ERR_EACCES)
2008 goto fail;
2010 if (!rc)
2012 rc = xml_add_attribute (client, n, "target", args[1]);
2013 if (!rc)
2014 rc = xml_unlink_node (client, n->children);
2017 fail:
2018 strv_free (src);
2019 strv_free (dst);
2020 xml_free_request (req);
2021 return rc;
2025 * args[0] - attribute
2026 * args[1] - element path
2028 static gpg_error_t
2029 attribute_get (assuan_context_t ctx, char **args)
2031 struct client_s *client = assuan_get_pointer (ctx);
2032 xmlNodePtr n;
2033 xmlChar *a;
2034 gpg_error_t rc;
2035 struct xml_request_s *req;
2037 if (!args || !args[0] || !*args[0] || !args[1] || !*args[1])
2038 return GPG_ERR_SYNTAX;
2040 if (!xml_reserved_attribute (args[0]))
2041 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
2042 else
2044 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
2045 if (rc)
2046 return rc;
2048 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2049 xml_free_request (req);
2052 if (rc)
2053 return rc;
2055 a = xmlGetProp (n, (xmlChar *) args[0]);
2056 if (!a)
2057 return GPG_ERR_NOT_FOUND;
2059 pthread_cleanup_push ((void *)xmlFree, a);
2061 if (*a)
2062 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
2063 else
2064 rc = GPG_ERR_NO_DATA;
2066 pthread_cleanup_pop (1);
2067 return rc;
2071 * args[0] - attribute
2072 * args[1] - element path
2073 * args[2] - value
2075 static gpg_error_t
2076 attribute_set (struct client_s *client, char **args)
2078 struct xml_request_s *req;
2079 gpg_error_t rc;
2080 xmlNodePtr n;
2082 if (!args || !args[0] || !args[1])
2083 return GPG_ERR_SYNTAX;
2086 * Reserved attribute names.
2088 if (!strcmp (args[0], "_name")) // Use the RENAME command instead
2089 return GPG_ERR_INV_ATTR;
2090 else if (!strcmp (args[0], "target"))
2091 return set_target_attribute (client, args + 1);
2092 else if (!xml_valid_attribute (args[0]))
2093 return GPG_ERR_INV_VALUE;
2095 if (!xml_valid_attribute_value (args[2]))
2096 return GPG_ERR_INV_VALUE;
2098 if (!xml_reserved_attribute (args[0]))
2100 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
2101 if (!rc)
2103 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
2104 if (!rc)
2106 rc = xml_check_recursion (client, req);
2107 xml_free_request (req);
2111 else
2113 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
2114 if (rc)
2115 return rc;
2117 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2118 xml_free_request (req);
2121 if (!rc)
2122 rc = xml_is_element_owner (client, n);
2124 if (!rc)
2125 rc = xml_add_attribute (client, n, args[0], args[2]);
2127 return rc;
2131 * req[0] - command
2132 * req[1] - attribute name or element path if command is LIST
2133 * req[2] - element path
2134 * req[2] - element path or value
2137 static gpg_error_t
2138 do_attr (assuan_context_t ctx, char *line)
2140 struct client_s *client = assuan_get_pointer (ctx);
2141 gpg_error_t rc = 0;
2142 char **req;
2144 req = str_split (line, " ", 4);
2145 if (!req || !req[0] || !req[1])
2147 strv_free (req);
2148 return GPG_ERR_SYNTAX;
2151 pthread_cleanup_push ((void *)req_cleanup, req);
2153 if (strcasecmp (req[0], "SET") == 0)
2154 rc = attribute_set (client, req + 1);
2155 else if (strcasecmp (req[0], "GET") == 0)
2156 rc = attribute_get (ctx, req + 1);
2157 else if (strcasecmp (req[0], "DELETE") == 0)
2158 rc = attribute_delete (client, req + 1);
2159 else if (strcasecmp (req[0], "LIST") == 0)
2160 rc = attribute_list (ctx, req + 1);
2161 else
2162 rc = GPG_ERR_SYNTAX;
2164 client->flags &= ~(FLAG_ACL_IGNORE|FLAG_ACL_ERROR);
2165 pthread_cleanup_pop (1);
2166 return rc;
2169 static gpg_error_t
2170 attr_command (assuan_context_t ctx, char *line)
2172 struct client_s *client = assuan_get_pointer (ctx);
2173 gpg_error_t rc;
2174 struct argv_s *args[] = {
2175 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2176 NULL
2179 rc = parse_options (&line, args, client, 1);
2180 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2181 rc = GPG_ERR_SYNTAX;
2182 if (rc)
2183 return send_error (ctx, rc);
2185 if (client->opts & OPT_INQUIRE)
2187 unsigned char *result;
2188 size_t len;
2190 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2191 if (rc)
2192 return send_error (ctx, rc);
2194 pthread_cleanup_push ((void *)xfree, result);
2195 rc = do_attr (ctx, (char *)result);
2196 pthread_cleanup_pop (1);
2198 else
2199 rc = do_attr (ctx, line);
2201 return send_error (ctx, rc);
2204 static gpg_error_t
2205 parse_iscached_opt_lock (void *data, void *value)
2207 struct client_s *client = data;
2209 (void) value;
2210 client->opts |= OPT_LOCK;
2211 return 0;
2214 static gpg_error_t
2215 parse_iscached_opt_agent (void *data, void *value)
2217 struct client_s *client = data;
2219 (void) value;
2220 client->opts |= OPT_CACHE_AGENT;
2221 return 0;
2224 static gpg_error_t
2225 parse_iscached_opt_sign (void *data, void *value)
2227 struct client_s *client = data;
2229 (void) value;
2230 client->opts |= OPT_CACHE_SIGN;
2231 return 0;
2234 static gpg_error_t
2235 iscached_command (assuan_context_t ctx, char *line)
2237 struct client_s *client = assuan_get_pointer (ctx);
2238 gpg_error_t rc;
2239 unsigned char md5file[16];
2240 struct argv_s *args[] = {
2241 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2242 &(struct argv_s) {"agent", OPTION_TYPE_NOARG, parse_iscached_opt_agent},
2243 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_iscached_opt_sign},
2244 NULL
2247 if (!line || !*line)
2248 return send_error (ctx, GPG_ERR_SYNTAX);
2250 rc = parse_options (&line, args, client, 1);
2251 if (rc)
2252 return send_error (ctx, rc);
2253 else if (!valid_filename (line))
2254 return send_error (ctx, GPG_ERR_INV_VALUE);
2256 if (!(client->flags & FLAG_OPEN)
2257 && ((client->opts & OPT_CACHE_AGENT) || (client->opts & OPT_CACHE_SIGN)))
2258 return send_error (ctx, GPG_ERR_INV_STATE);
2260 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2261 rc = cache_iscached (line, NULL, (client->opts & OPT_CACHE_AGENT),
2262 (client->opts & OPT_CACHE_SIGN));
2263 if (!rc)
2265 struct cache_data_s *cdata = cache_get_data (md5file);
2267 if (cdata)
2269 rc = validate_checksum (client, line, cdata, NULL, NULL);
2270 if (rc == GPG_ERR_CHECKSUM)
2271 rc = GPG_ERR_NO_DATA;
2275 if (client->opts & OPT_LOCK
2276 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2278 gpg_error_t trc = rc;
2280 if (memcmp (md5file, client->md5file, 16))
2281 cleanup_client (client);
2283 memcpy (client->md5file, md5file, 16);
2284 rc = do_lock (client, 1);
2285 if (!rc)
2286 rc = trc;
2289 return send_error (ctx, rc);
2292 static gpg_error_t
2293 clearcache_command (assuan_context_t ctx, char *line)
2295 gpg_error_t rc = 0, all_rc = 0;
2296 unsigned char md5file[16];
2297 int i;
2298 int t;
2299 int all = 0;
2300 struct client_thread_s *once = NULL;
2302 cache_lock ();
2303 MUTEX_LOCK (&cn_mutex);
2304 pthread_cleanup_push ((void *)cleanup_mutex_cb, &cn_mutex);
2306 if (!line || !*line)
2307 all = 1;
2309 t = slist_length (cn_thread_list);
2311 for (i = 0; i < t; i++)
2313 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2314 assuan_peercred_t peer;
2316 if (!thd->cl)
2317 continue;
2319 /* Lock each connected clients' file mutex to prevent any other client
2320 * from accessing the cache entry (the file mutex is locked upon
2321 * command startup). The cache for the entry is not cleared if the
2322 * file mutex is locked by another client to prevent this function
2323 * from blocking.
2325 if (all)
2327 if (thd->cl->filename)
2329 rc = do_validate_peer (ctx, thd->cl->filename, &peer);
2330 all_rc = !all_rc ? rc : all_rc;
2331 if (rc)
2333 rc = 0;
2334 continue;
2338 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
2339 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2341 if (pthread_equal (pthread_self (), thd->tid))
2342 rc = 0;
2343 else
2345 if (!thd->cl->filename ||
2346 cache_iscached (thd->cl->filename, NULL, 0, 0) == GPG_ERR_NO_DATA)
2348 rc = 0;
2349 continue;
2352 cache_defer_clear (thd->cl->md5file);
2355 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2357 rc = 0;
2358 continue;
2361 if (!rc)
2363 rc = cache_clear (thd->cl->md5file, 1);
2364 cache_unlock_mutex (thd->cl->md5file, 0);
2367 if (rc)
2368 all_rc = rc;
2370 rc = 0;
2372 /* A single data filename was specified. Lock only this data file
2373 * mutex and free the cache entry. */
2374 else
2376 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2377 rc = do_validate_peer (ctx, line, &peer);
2379 if (!rc && !memcmp (thd->cl->md5file, md5file, sizeof (md5file)))
2381 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0,
2382 -1);
2383 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2385 if (pthread_equal (pthread_self (), thd->tid))
2386 rc = 0;
2389 if (!rc)
2391 once = thd;
2392 rc = cache_clear (thd->cl->md5file, 1);
2393 cache_unlock_mutex (thd->cl->md5file, 0);
2395 else
2397 cache_defer_clear (thd->cl->md5file);
2400 break;
2405 /* Only connected clients' cache entries have been cleared. Now clear any
2406 * remaining cache entries without clients but only if there wasn't an
2407 * error from above since this would defeat the locking check of the
2408 * remaining entries. */
2409 if (!all_rc && all)
2411 cache_clear (NULL, 1);
2414 /* No clients are using the specified file. */
2415 else if (!all_rc && !rc && !once)
2417 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2418 rc = cache_clear (md5file, 1);
2421 /* Release the connection mutex. */
2422 pthread_cleanup_pop (1);
2423 cache_unlock ();
2425 if (!rc)
2426 send_status_all (STATUS_CACHE, NULL);
2428 /* One or more files were locked while clearing all cache entries. */
2429 if (all_rc)
2430 rc = all_rc;
2432 return send_error (ctx, rc);
2435 static gpg_error_t
2436 cachetimeout_command (assuan_context_t ctx, char *line)
2438 int timeout;
2439 char **req = str_split (line, " ", 0);
2440 char *p;
2441 gpg_error_t rc = 0;
2442 assuan_peercred_t peer;
2444 if (!req || !*req || !req[1])
2446 strv_free (req);
2447 return send_error (ctx, GPG_ERR_SYNTAX);
2450 errno = 0;
2451 timeout = (int) strtol (req[1], &p, 10);
2452 if (errno != 0 || *p || timeout < -1)
2454 strv_free (req);
2455 return send_error (ctx, GPG_ERR_SYNTAX);
2458 rc = do_validate_peer (ctx, req[0], &peer);
2459 if (!rc)
2461 unsigned char md5file[16];
2463 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, req[0], strlen (req[0]));
2464 rc = cache_set_timeout (md5file, timeout);
2465 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2467 rc = 0;
2468 MUTEX_LOCK (&rcfile_mutex);
2469 config_set_int_param (&global_config, req[0], "cache_timeout",
2470 req[1]);
2471 MUTEX_UNLOCK (&rcfile_mutex);
2475 strv_free (req);
2476 return send_error (ctx, rc);
2479 static gpg_error_t
2480 dump_command (assuan_context_t ctx, char *line)
2482 xmlChar *xml;
2483 int len;
2484 struct client_s *client = assuan_get_pointer (ctx);
2485 gpg_error_t rc;
2487 if (disable_list_and_dump == 1)
2488 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2490 if (line && *line)
2491 return send_error (ctx, GPG_ERR_SYNTAX);
2493 rc = peer_is_invoker(client);
2494 if (rc)
2495 return send_error (ctx, rc);
2497 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2499 if (!xml)
2501 log_write ("%s(%i): %s", __FILE__, __LINE__,
2502 pwmd_strerror (GPG_ERR_ENOMEM));
2503 return send_error (ctx, GPG_ERR_ENOMEM);
2506 pthread_cleanup_push ((void *)xmlFree, xml);
2507 rc = xfer_data (ctx, (char *) xml, len);
2508 pthread_cleanup_pop (1);
2509 return send_error (ctx, rc);
2512 static gpg_error_t
2513 getconfig_command (assuan_context_t ctx, char *line)
2515 struct client_s *client = assuan_get_pointer (ctx);
2516 gpg_error_t rc = 0;
2517 char filename[255] = { 0 }, param[747] = { 0 };
2518 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2520 if (!line || !*line)
2521 return send_error (ctx, GPG_ERR_SYNTAX);
2523 if (strchr (line, ' '))
2525 sscanf (line, " %254[^ ] %746c", filename, param);
2526 paramp = param;
2527 fp = filename;
2530 if (fp && !valid_filename (fp))
2531 return send_error (ctx, GPG_ERR_INV_VALUE);
2533 paramp = str_down (paramp);
2534 if (!strcmp (paramp, "passphrase"))
2535 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2537 p = config_get_value (fp ? fp : "global", paramp);
2538 if (!p)
2539 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2541 tmp = expand_homedir (p);
2542 xfree (p);
2543 if (!tmp)
2545 log_write ("%s(%i): %s", __FILE__, __LINE__,
2546 pwmd_strerror (GPG_ERR_ENOMEM));
2547 return send_error (ctx, GPG_ERR_ENOMEM);
2550 p = tmp;
2551 pthread_cleanup_push ((void *)xfree, p);
2552 rc = xfer_data (ctx, p, strlen (p));
2553 pthread_cleanup_pop (1);
2554 return send_error (ctx, rc);
2557 struct xpath_s
2559 xmlXPathContextPtr xp;
2560 xmlXPathObjectPtr result;
2561 xmlBufferPtr buf;
2562 char **req;
2565 static void
2566 xpath_command_cleanup (void *arg)
2568 struct xpath_s *xpath = arg;
2570 if (!xpath)
2571 return;
2573 req_cleanup (xpath->req);
2575 if (xpath->buf)
2576 xmlBufferFree (xpath->buf);
2578 if (xpath->result)
2579 xmlXPathFreeObject (xpath->result);
2581 if (xpath->xp)
2582 xmlXPathFreeContext (xpath->xp);
2585 static gpg_error_t
2586 do_xpath (assuan_context_t ctx, char *line)
2588 gpg_error_t rc;
2589 struct client_s *client = assuan_get_pointer (ctx);
2590 struct xpath_s _x = { 0 };
2591 struct xpath_s *xpath = &_x;
2593 if (!line || !*line)
2594 return GPG_ERR_SYNTAX;
2596 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2598 if (strv_printf (&xpath->req, "%s", line) == 0)
2599 return GPG_ERR_ENOMEM;
2602 xpath->xp = xmlXPathNewContext (client->doc);
2603 if (!xpath->xp)
2605 rc = GPG_ERR_BAD_DATA;
2606 goto fail;
2609 xpath->result =
2610 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2611 if (!xpath->result)
2613 rc = GPG_ERR_BAD_DATA;
2614 goto fail;
2617 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2619 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2620 goto fail;
2623 rc = xml_recurse_xpath_nodeset (client, client->doc,
2624 xpath->result->nodesetval,
2625 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2626 NULL);
2627 if (rc)
2628 goto fail;
2629 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2631 rc = GPG_ERR_NO_DATA;
2632 goto fail;
2634 else if (xpath->req[1])
2636 rc = 0;
2637 goto fail;
2640 pthread_cleanup_push ((void *)xpath_command_cleanup, &xpath);
2641 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2642 xmlBufferLength (xpath->buf));
2643 pthread_cleanup_pop (0);
2644 fail:
2645 xpath_command_cleanup (xpath);
2646 return rc;
2649 static gpg_error_t
2650 xpath_command (assuan_context_t ctx, char *line)
2652 struct client_s *client = assuan_get_pointer (ctx);
2653 gpg_error_t rc;
2654 struct argv_s *args[] = {
2655 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2656 NULL
2659 if (disable_list_and_dump == 1)
2660 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2662 rc = peer_is_invoker(client);
2663 if (rc)
2664 return send_error (ctx, rc);
2666 rc = parse_options (&line, args, client, 1);
2667 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2668 rc = GPG_ERR_SYNTAX;
2669 if (rc)
2670 return send_error (ctx, rc);
2672 if (client->opts & OPT_INQUIRE)
2674 unsigned char *result;
2675 size_t len;
2677 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2678 if (rc)
2679 return send_error (ctx, rc);
2681 pthread_cleanup_push ((void *)xfree, result);
2682 rc = do_xpath (ctx, (char *)result);
2683 pthread_cleanup_pop (1);
2685 else
2686 rc = do_xpath (ctx, line);
2688 return send_error (ctx, rc);
2691 static gpg_error_t
2692 do_xpathattr (assuan_context_t ctx, char *line)
2694 struct client_s *client = assuan_get_pointer (ctx);
2695 gpg_error_t rc;
2696 char **req = NULL;
2697 int cmd = 0; //SET
2698 struct xpath_s _x = { 0 };
2699 struct xpath_s *xpath = &_x;
2701 if (!line || !*line)
2702 return GPG_ERR_SYNTAX;
2704 if ((req = str_split (line, " ", 3)) == NULL)
2705 return GPG_ERR_ENOMEM;
2707 if (!req[0])
2709 rc = GPG_ERR_SYNTAX;
2710 goto fail;
2713 if (!strcasecmp (req[0], "SET"))
2714 cmd = 0;
2715 else if (!strcasecmp (req[0], "DELETE"))
2716 cmd = 1;
2717 else
2719 rc = GPG_ERR_SYNTAX;
2720 goto fail;
2723 if (!req[1] || !req[2])
2725 rc = GPG_ERR_SYNTAX;
2726 goto fail;
2729 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2731 rc = GPG_ERR_ENOMEM;
2732 goto fail;
2735 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2737 rc = GPG_ERR_SYNTAX;
2738 goto fail;
2741 xpath->xp = xmlXPathNewContext (client->doc);
2742 if (!xpath->xp)
2744 rc = GPG_ERR_BAD_DATA;
2745 goto fail;
2748 xpath->result = xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2749 if (!xpath->result)
2751 rc = GPG_ERR_BAD_DATA;
2752 goto fail;
2755 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2757 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2758 goto fail;
2761 rc = xml_recurse_xpath_nodeset (client, client->doc,
2762 xpath->result->nodesetval,
2763 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2764 (xmlChar *) req[1]);
2766 fail:
2767 xpath_command_cleanup (xpath);
2768 strv_free (req);
2769 return rc;
2772 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2773 static gpg_error_t
2774 xpathattr_command (assuan_context_t ctx, char *line)
2776 struct client_s *client = assuan_get_pointer (ctx);
2777 gpg_error_t rc;
2778 struct argv_s *args[] = {
2779 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2780 NULL
2783 if (disable_list_and_dump == 1)
2784 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2786 rc = peer_is_invoker(client);
2787 if (rc)
2788 return send_error (ctx, rc);
2790 rc = parse_options (&line, args, client, 1);
2791 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2792 rc = GPG_ERR_SYNTAX;
2793 if (rc)
2794 return send_error (ctx, rc);
2796 if (client->opts & OPT_INQUIRE)
2798 unsigned char *result;
2799 size_t len;
2801 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2802 if (rc)
2803 return send_error (ctx, rc);
2805 pthread_cleanup_push ((void *)xfree, result);
2806 rc = do_xpathattr (ctx, (char *)result);
2807 pthread_cleanup_pop (1);
2809 else
2810 rc = do_xpathattr (ctx, line);
2812 return send_error (ctx, rc);
2815 static gpg_error_t
2816 do_import (struct client_s *client, const char *root_element,
2817 unsigned char *content)
2819 xmlDocPtr doc = NULL;
2820 xmlNodePtr n = NULL, root;
2821 gpg_error_t rc;
2822 struct string_s *str = NULL;
2823 struct xml_request_s *req = NULL;
2825 if (!content || !*content)
2826 return GPG_ERR_SYNTAX;
2828 if (root_element)
2830 rc = xml_new_request (client, root_element, XML_CMD_STORE, &req);
2831 if (rc)
2833 xfree (content);
2834 return rc;
2837 if (!xml_valid_element_path (req->args, 0))
2839 xml_free_request (req);
2840 xfree (content);
2841 return GPG_ERR_INV_VALUE;
2845 str = string_new_content ((char *)content);
2846 str = string_prepend (str, "<pwmd>");
2847 str = string_append (str, "</pwmd>");
2848 doc = xmlReadDoc ((xmlChar *) str->str, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2849 string_free (str, 1);
2850 if (!doc)
2852 rc = GPG_ERR_BAD_DATA;
2853 goto fail;
2856 root = xmlDocGetRootElement (doc);
2857 root = root->children;
2858 if (root->type != XML_ELEMENT_NODE)
2860 rc = GPG_ERR_BAD_DATA;
2861 goto fail;
2864 rc = xml_validate_import (client, root);
2865 if (rc)
2866 goto fail;
2868 if (req)
2870 /* Create the new specified root element path, if needed. */
2871 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc),
2872 &rc);
2873 if (rc)
2874 goto fail;
2876 /* Overwrite the children of the specified root element path. */
2877 (void)xml_unlink_node (client, n->children);
2878 n->children = NULL;
2880 else
2881 n = xmlDocGetRootElement (client->doc);
2883 if (n)
2885 xmlNodePtr copy;
2886 xmlChar *name = xml_attribute_value (root, (xmlChar *)"_name");
2888 if (!name)
2889 rc = GPG_ERR_BAD_DATA;
2890 else if (!req)
2892 /* No --root argument passed. Overwrite the current documents' root
2893 * element matching the root element of the imported data. */
2894 xml_free_request (req);
2895 req = NULL;
2896 rc = xml_new_request (client, (char *)name, XML_CMD_DELETE, &req);
2897 xmlFree (name);
2898 if (!rc)
2900 xmlNodePtr tmp;
2902 tmp = xml_find_elements (client, req,
2903 xmlDocGetRootElement (req->doc), &rc);
2904 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2905 goto fail;
2907 if (!rc)
2908 (void)xml_unlink_node (client, tmp);
2910 rc = 0;
2914 if (!rc)
2916 copy = xmlCopyNodeList (root);
2917 if (!copy)
2918 rc = GPG_ERR_ENOMEM;
2919 else
2921 n = xmlAddChildList (n, copy);
2922 if (!n)
2923 rc = GPG_ERR_ENOMEM;
2928 if (!rc && n && n->parent)
2929 rc = xml_update_element_mtime (client, n->parent);
2931 fail:
2932 xml_free_request (req);
2934 if (doc)
2935 xmlFreeDoc (doc);
2937 return rc;
2940 static gpg_error_t
2941 parse_import_opt_root (void *data, void *value)
2943 struct client_s *client = data;
2945 client->import_root = str_dup (value);
2946 return client->import_root ? 0 : GPG_ERR_ENOMEM;
2949 static gpg_error_t
2950 import_command (assuan_context_t ctx, char *line)
2952 gpg_error_t rc;
2953 struct client_s *client = assuan_get_pointer (ctx);
2954 unsigned char *result;
2955 size_t len;
2956 struct argv_s *args[] = {
2957 &(struct argv_s) {"root", OPTION_TYPE_ARG, parse_import_opt_root},
2958 NULL
2961 xfree (client->import_root);
2962 client->import_root = NULL;
2963 rc = parse_options (&line, args, client, 0);
2964 if (rc)
2965 return send_error (ctx, rc);
2967 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2968 if (rc)
2970 xfree (client->import_root);
2971 client->import_root = NULL;
2972 return send_error (ctx, rc);
2975 rc = do_import (client, client->import_root, result);
2976 xfree (client->import_root);
2977 client->import_root = NULL;
2978 return send_error (ctx, rc);
2981 static gpg_error_t
2982 do_lock (struct client_s *client, int add)
2984 gpg_error_t rc = lock_file_mutex (client, add);
2986 if (!rc)
2987 client->flags |= FLAG_LOCK_CMD;
2989 return rc;
2992 static gpg_error_t
2993 lock_command (assuan_context_t ctx, char *line)
2995 struct client_s *client = assuan_get_pointer (ctx);
2996 gpg_error_t rc;
2998 if (line && *line)
2999 return send_error (ctx, GPG_ERR_SYNTAX);
3001 rc = do_lock (client, 0);
3002 return send_error (ctx, rc);
3005 static gpg_error_t
3006 unlock_command (assuan_context_t ctx, char *line)
3008 struct client_s *client = assuan_get_pointer (ctx);
3009 gpg_error_t rc;
3011 if (line && *line)
3012 return send_error (ctx, GPG_ERR_SYNTAX);
3014 rc = unlock_file_mutex (client, 0);
3015 return send_error (ctx, rc);
3018 static void
3019 get_set_env (const char *name, const char *value)
3021 if (value && *value)
3022 setenv (name, value, 1);
3023 else
3024 unsetenv (name);
3027 static gpg_error_t
3028 option_command (assuan_context_t ctx, char *line)
3030 struct client_s *client = assuan_get_pointer (ctx);
3031 gpg_error_t rc = 0;
3032 char namebuf[255] = { 0 };
3033 char *name = namebuf;
3034 char *value = NULL, *p, *tmp = NULL;
3036 p = strchr (line, '=');
3037 if (!p)
3039 strncpy (namebuf, line, sizeof(namebuf));
3040 namebuf[sizeof(namebuf)-1] = 0;
3042 else
3044 strncpy (namebuf, line, strlen (line)-strlen (p));
3045 namebuf[sizeof(namebuf)-1] = 0;
3046 value = p+1;
3049 log_write2 ("OPTION name='%s' value='%s'", name, value);
3051 if (strcasecmp (name, (char *) "lock-timeout") == 0)
3053 long n = 0;
3055 if (value)
3057 n = strtol (value, &tmp, 10);
3058 if (tmp && *tmp)
3059 return send_error (ctx, GPG_ERR_INV_VALUE);
3062 client->lock_timeout = n;
3064 else if (strcasecmp (name, (char *) "NAME") == 0)
3066 if (value && strchr (value, ' '))
3067 rc = GPG_ERR_INV_VALUE;
3068 else
3070 tmp = pthread_getspecific (thread_name_key);
3071 xfree (tmp);
3072 MUTEX_LOCK (&cn_mutex);
3073 xfree (client->thd->name);
3074 client->thd->name = NULL;
3076 if (!value || !*value)
3077 pthread_setspecific (thread_name_key, str_dup (""));
3078 else
3080 client->thd->name = str_dup (value);
3081 pthread_setspecific (thread_name_key, str_dup (value));
3084 MUTEX_UNLOCK (&cn_mutex);
3087 else if (strcasecmp (name, "disable-pinentry") == 0)
3089 int n = 1;
3091 if (value && *value)
3093 n = (int) strtol (value, &tmp, 10);
3094 if (*tmp || n < 0 || n > 1)
3095 return send_error (ctx, GPG_ERR_INV_VALUE);
3098 if (n)
3099 client->flags |= FLAG_NO_PINENTRY;
3100 else
3101 client->flags &= ~FLAG_NO_PINENTRY;
3103 else if (strcasecmp (name, "ttyname") == 0)
3105 get_set_env ("GPG_TTY", value);
3107 else if (strcasecmp (name, "ttytype") == 0)
3109 get_set_env ("TERM", value);
3111 else if (strcasecmp (name, "display") == 0)
3113 get_set_env ("DISPLAY", value);
3115 else if (strcasecmp (name, "lc_messages") == 0)
3118 else if (strcasecmp (name, "lc_ctype") == 0)
3121 else if (strcasecmp (name, "desc") == 0)
3124 else if (strcasecmp (name, "pinentry-timeout") == 0)
3127 else
3128 rc = GPG_ERR_UNKNOWN_OPTION;
3130 return send_error (ctx, rc);
3133 static gpg_error_t
3134 do_rename (assuan_context_t ctx, char *line)
3136 struct client_s *client = assuan_get_pointer (ctx);
3137 char **args, *p;
3138 xmlNodePtr src, dst;
3139 struct string_s *str = NULL, *tstr;
3140 struct xml_request_s *req;
3141 gpg_error_t rc;
3143 args = str_split (line, " ", 0);
3144 if (!args || strv_length (args) != 2)
3146 strv_free (args);
3147 return GPG_ERR_SYNTAX;
3150 if (!xml_valid_element ((xmlChar *) args[1]))
3152 strv_free (args);
3153 return GPG_ERR_INV_VALUE;
3156 rc = xml_new_request (client, args[0], XML_CMD_RENAME, &req);
3157 if (rc)
3159 strv_free (args);
3160 return rc;
3163 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3164 if (rc)
3165 goto fail;
3167 rc = xml_is_element_owner (client, src);
3168 if (rc)
3169 goto fail;
3171 xmlChar *a = xmlGetProp (src, (xmlChar *) "_name");
3172 if (!a)
3174 rc = GPG_ERR_ENOMEM;
3175 goto fail;
3178 /* To prevent unwanted effects:
3180 * <element _name="a"><element _name="b"/></element>
3182 * RENAME a<TAB>b b
3184 if (xmlStrEqual (a, (xmlChar *) args[1]))
3186 xmlFree (a);
3187 rc = GPG_ERR_EEXIST;
3188 goto fail;
3191 xmlFree (a);
3192 str = string_new (args[0]);
3193 if (!str)
3195 rc = GPG_ERR_ENOMEM;
3196 goto fail;
3199 p = strrchr (str->str, '\t');
3200 if (p)
3201 tstr = string_truncate (str, strlen (str->str)-strlen (p));
3202 else
3203 tstr = string_truncate (str, 0);
3205 if (!tstr)
3207 string_free (str, 1);
3208 rc = GPG_ERR_ENOMEM;
3209 goto fail;
3212 str = tstr;
3213 tstr = string_append_printf (str, "%s%s", str->len ? "\t" : "", args[1]);
3214 if (!tstr)
3216 string_free (str, 1);
3217 rc = GPG_ERR_ENOMEM;
3218 goto fail;
3221 str = tstr;
3222 xml_free_request (req);
3223 rc = xml_new_request (client, str->str, XML_CMD_RENAME, &req);
3224 string_free (str, 1);
3225 if (rc)
3227 req = NULL;
3228 goto fail;
3231 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3232 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3233 goto fail;
3235 rc = 0;
3237 /* Target may exist:
3239 * <element _name="a"/>
3240 * <element _name="b" target="a"/>
3242 * RENAME b a
3244 if (src == dst)
3246 rc = GPG_ERR_EEXIST;
3247 goto fail;
3250 if (dst)
3252 rc = xml_is_element_owner (client, dst);
3253 if (rc)
3254 goto fail;
3256 rc = xml_add_attribute (client, src, "_name", args[1]);
3257 if (!rc)
3258 xml_unlink_node (client, dst);
3260 else
3261 rc = xml_add_attribute (client, src, "_name", args[1]);
3263 fail:
3264 xml_free_request (req);
3265 strv_free (args);
3266 return rc;
3269 static gpg_error_t
3270 rename_command (assuan_context_t ctx, char *line)
3272 struct client_s *client = assuan_get_pointer (ctx);
3273 gpg_error_t rc;
3274 struct argv_s *args[] = {
3275 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3276 NULL
3279 rc = parse_options (&line, args, client, 1);
3280 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3281 rc = GPG_ERR_SYNTAX;
3282 if (rc)
3283 return send_error (ctx, rc);
3285 if (client->opts & OPT_INQUIRE)
3287 unsigned char *result;
3288 size_t len;
3290 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3291 if (rc)
3292 return send_error (ctx, rc);
3294 pthread_cleanup_push ((void *)xfree, result);
3295 rc = do_rename (ctx, (char *)result);
3296 pthread_cleanup_pop (1);
3298 else
3299 rc = do_rename (ctx, line);
3301 return send_error (ctx, rc);
3304 static gpg_error_t
3305 do_copy (assuan_context_t ctx, char *line)
3307 struct client_s *client = assuan_get_pointer (ctx);
3308 char **args, **targs;
3309 xmlNodePtr src, dst, tree = NULL;
3310 struct xml_request_s *req;
3311 gpg_error_t rc;
3313 args = str_split (line, " ", 0);
3314 if (!args || strv_length (args) != 2)
3316 strv_free (args);
3317 return GPG_ERR_SYNTAX;
3320 targs = str_split (args[1], "\t", 0);
3321 if (!targs)
3323 strv_free (args);
3324 return GPG_ERR_SYNTAX;
3327 if (!xml_valid_element_path (targs, 0))
3329 strv_free (args);
3330 strv_free (targs);
3331 return GPG_ERR_INV_VALUE;
3333 strv_free (targs);
3335 rc = xml_new_request (client, args[0], XML_CMD_NONE, &req);
3336 if (rc)
3338 strv_free (args);
3339 return rc;
3342 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3343 if (rc)
3344 goto fail;
3346 tree = xmlCopyNodeList (src);
3347 if (!tree)
3349 rc = GPG_ERR_ENOMEM;
3350 goto fail;
3353 xml_free_request (req);
3354 /* Create the destination element path if it does not exist. */
3355 rc = xml_new_request (client, args[1], XML_CMD_STORE, &req);
3356 if (rc)
3358 req = NULL;
3359 goto fail;
3362 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3363 if (rc)
3364 goto fail;
3366 rc = xml_is_element_owner (client, dst);
3367 if (rc)
3368 goto fail;
3370 rc = xml_validate_target (client, req->doc, args[1], src);
3371 if (rc || src == dst)
3373 rc = rc ? rc : GPG_ERR_EEXIST;
3374 goto fail;
3377 /* Merge any attributes from the src node to the initial dst node. */
3378 for (xmlAttrPtr attr = tree->properties; attr; attr = attr->next)
3380 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3381 continue;
3383 xmlAttrPtr a = xmlHasProp (dst, attr->name);
3384 if (a)
3385 xmlRemoveProp (a);
3387 xmlChar *tmp = xmlNodeGetContent (attr->children);
3388 xmlNewProp (dst, attr->name, tmp);
3389 xmlFree (tmp);
3390 /* Create the default attributes. */
3391 rc = xml_add_attribute (client, dst, NULL, NULL);
3394 xmlNodePtr n = dst->children;
3395 (void)xml_unlink_node (client, n);
3396 dst->children = NULL;
3398 if (tree->children)
3400 n = xmlCopyNodeList (tree->children);
3401 if (!n)
3403 rc = GPG_ERR_ENOMEM;
3404 goto fail;
3407 n = xmlAddChildList (dst, n);
3408 if (!n)
3410 rc = GPG_ERR_ENOMEM;
3411 goto fail;
3414 rc = xml_update_element_mtime (client, xmlDocGetRootElement (client->doc)
3415 == dst->parent ? dst : dst->parent);
3418 fail:
3419 if (tree)
3420 (void)xml_unlink_node (client, tree);
3422 xml_free_request (req);
3423 strv_free (args);
3424 return rc;
3427 static gpg_error_t
3428 copy_command (assuan_context_t ctx, char *line)
3430 struct client_s *client = assuan_get_pointer (ctx);
3431 gpg_error_t rc;
3432 struct argv_s *args[] = {
3433 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3434 NULL
3437 rc = parse_options (&line, args, client, 1);
3438 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3439 rc = GPG_ERR_SYNTAX;
3440 if (rc)
3441 return send_error (ctx, rc);
3443 if (client->opts & OPT_INQUIRE)
3445 unsigned char *result;
3446 size_t len;
3448 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3449 if (rc)
3450 return send_error (ctx, rc);
3452 pthread_cleanup_push ((void *)xfree, result);
3453 rc = do_copy (ctx, (char *)result);
3454 pthread_cleanup_pop (1);
3456 else
3457 rc = do_copy (ctx, line);
3459 return send_error (ctx, rc);
3462 static gpg_error_t
3463 do_move (assuan_context_t ctx, char *line)
3465 struct client_s *client = assuan_get_pointer (ctx);
3466 char **args;
3467 xmlNodePtr src, dst;
3468 struct xml_request_s *req;
3469 gpg_error_t rc;
3471 args = str_split (line, " ", 0);
3472 if (!args || strv_length (args) != 2)
3474 strv_free (args);
3475 return GPG_ERR_SYNTAX;
3478 if (*args[1])
3480 char **targs = str_split (args[1], "\t", 0);
3481 if (!targs)
3483 strv_free (args);
3484 return GPG_ERR_ENOMEM;
3487 if (!xml_valid_element_path (targs, 0))
3489 strv_free (targs);
3490 strv_free (args);
3491 return GPG_ERR_INV_VALUE;
3494 strv_free (targs);
3497 rc = xml_new_request (client, args[0], XML_CMD_MOVE, &req);
3498 if (rc)
3500 strv_free (args);
3501 return rc;
3504 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3505 if (rc)
3506 goto fail;
3508 rc = xml_is_element_owner (client, src);
3509 if (rc)
3510 goto fail;
3512 xml_free_request (req);
3513 req = NULL;
3514 if (*args[1])
3516 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
3517 if (rc)
3518 goto fail;
3520 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc),
3521 &rc);
3523 else
3524 dst = xmlDocGetRootElement (client->doc);
3526 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3527 goto fail;
3529 rc = 0;
3531 for (xmlNodePtr n = dst; n; n = n->parent)
3533 if (n == src)
3535 rc = GPG_ERR_EEXIST;
3536 goto fail;
3540 if (dst)
3542 xmlChar *a = xml_attribute_value (src, (xmlChar *) "_name");
3543 xmlNodePtr dup = xml_find_element (client, dst->children, (char *) a, &rc);
3545 xmlFree (a);
3546 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3547 goto fail;
3549 rc = 0;
3551 if (dup)
3553 if (dup == src)
3555 rc = GPG_ERR_EEXIST;
3556 goto fail;
3559 if (dst == xmlDocGetRootElement (client->doc))
3561 xmlNodePtr n = src;
3562 int match = 0;
3564 while (n->parent && n->parent != dst)
3565 n = n->parent;
3567 xmlChar *a = xml_attribute_value (n, (xmlChar *) "_name");
3568 xmlChar *b = xml_attribute_value (src, (xmlChar *) "_name");
3570 if (xmlStrEqual (a, b))
3572 match = 1;
3573 xmlUnlinkNode (src);
3574 (void)xml_unlink_node (client, n);
3577 xmlFree (a);
3578 xmlFree (b);
3580 if (!match)
3581 (void)xml_unlink_node (client, dup);
3583 else
3584 xmlUnlinkNode (dup);
3588 if (!dst && *req->args)
3590 struct xml_request_s *nreq = NULL;
3591 xmlChar *name = xml_attribute_value (src, (xmlChar *) "_name");
3593 if (src->parent == xmlDocGetRootElement (client->doc)
3594 && !strcmp ((char *) name, *req->args))
3596 xmlFree (name);
3597 rc = GPG_ERR_EEXIST;
3598 goto fail;
3601 xmlFree (name);
3602 rc = xml_new_request (client, args[1], XML_CMD_STORE, &nreq);
3603 if (!rc)
3605 dst = xml_find_elements (client, nreq,
3606 xmlDocGetRootElement (nreq->doc), &rc);
3607 xml_free_request (nreq);
3610 if (rc)
3611 goto fail;
3614 xml_update_element_mtime (client, src->parent);
3615 xmlUnlinkNode (src);
3617 dst = xmlAddChildList (dst, src);
3618 if (!dst)
3619 rc = GPG_ERR_ENOMEM;
3620 else
3621 xml_update_element_mtime (client, dst->parent);
3623 fail:
3624 xml_free_request (req);
3625 strv_free (args);
3626 return rc;
3629 static gpg_error_t
3630 move_command (assuan_context_t ctx, char *line)
3632 struct client_s *client = assuan_get_pointer (ctx);
3633 gpg_error_t rc;
3634 struct argv_s *args[] = {
3635 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3636 NULL
3639 rc = parse_options (&line, args, client, 1);
3640 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3641 rc = GPG_ERR_SYNTAX;
3642 if (rc)
3643 return send_error (ctx, rc);
3645 if (client->opts & OPT_INQUIRE)
3647 unsigned char *result;
3648 size_t len;
3650 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3651 if (rc)
3652 return send_error (ctx, rc);
3654 pthread_cleanup_push ((void *)xfree, result);
3655 rc = do_move (ctx, (char *)result);
3656 pthread_cleanup_pop (1);
3658 else
3659 rc = do_move (ctx, line);
3661 return send_error (ctx, rc);
3664 static gpg_error_t
3665 ls_command (assuan_context_t ctx, char *line)
3667 gpg_error_t rc;
3668 char *tmp;
3669 char *dir;
3670 DIR *d;
3672 if (line && *line)
3673 return send_error (ctx, GPG_ERR_SYNTAX);
3675 tmp = str_asprintf ("%s/data", homedir);
3676 dir = expand_homedir (tmp);
3677 xfree (tmp);
3678 d = opendir (dir);
3679 rc = gpg_error_from_errno (errno);
3681 if (!d)
3683 xfree (dir);
3684 return send_error (ctx, rc);
3687 size_t len =
3688 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3689 struct dirent *p = xmalloc (len), *cur = NULL;
3690 char *list = NULL;
3692 xfree (dir);
3693 pthread_cleanup_push ((void *)xfree, p);
3694 pthread_cleanup_push ((void *)(void *)(void *)closedir, d);
3695 rc = 0;
3697 while (!readdir_r (d, p, &cur) && cur)
3699 struct stat st;
3701 if (stat (cur->d_name, &st) == -1 || !S_ISREG (st.st_mode))
3702 continue;
3704 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3706 if (!tmp)
3708 if (list)
3709 xfree (list);
3711 rc = GPG_ERR_ENOMEM;
3712 break;
3715 xfree (list);
3716 list = tmp;
3719 pthread_cleanup_pop (1); // closedir (d)
3720 pthread_cleanup_pop (1); // xfree (p)
3722 if (rc)
3723 return send_error (ctx, rc);
3725 if (!list)
3726 return send_error (ctx, GPG_ERR_NO_DATA);
3728 list[strlen (list) - 1] = 0;
3729 pthread_cleanup_push ((void *)xfree, list);
3730 rc = xfer_data (ctx, list, strlen (list));
3731 pthread_cleanup_pop (1);
3732 return send_error (ctx, rc);
3735 static gpg_error_t
3736 bye_notify (assuan_context_t ctx, char *line)
3738 struct client_s *cl = assuan_get_pointer (ctx);
3739 gpg_error_t ret = 0;
3741 update_client_state (cl, CLIENT_STATE_DISCON);
3743 #ifdef WITH_GNUTLS
3744 if (cl->thd->remote)
3746 int rc;
3750 struct timeval tv = { 0, 50000 };
3752 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3753 if (rc == GNUTLS_E_AGAIN)
3754 select (0, NULL, NULL, NULL, &tv);
3756 while (rc == GNUTLS_E_AGAIN);
3758 #endif
3760 /* This will let assuan_process_next() return. */
3761 if (fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK) == -1)
3763 cl->last_rc = gpg_error_from_errno (errno);
3764 ret = cl->last_rc;
3767 cl->last_rc = 0; // BYE command result
3768 return ret;
3771 static gpg_error_t
3772 reset_notify (assuan_context_t ctx, char *line)
3774 struct client_s *client = assuan_get_pointer (ctx);
3776 if (client)
3777 cleanup_client (client);
3779 return 0;
3783 * This is called before every Assuan command.
3785 static gpg_error_t
3786 command_startup (assuan_context_t ctx, const char *name)
3788 struct client_s *client = assuan_get_pointer (ctx);
3789 gpg_error_t rc;
3790 struct command_table_s *cmd = NULL;
3792 log_write2 ("command='%s'", name);
3793 client->last_rc = client->opts = 0;
3795 for (int i = 0; command_table[i]; i++)
3797 if (!strcasecmp (name, command_table[i]->name))
3799 if (command_table[i]->ignore_startup)
3800 return 0;
3801 cmd = command_table[i];
3802 break;
3806 if (!cmd)
3807 return GPG_ERR_UNKNOWN_COMMAND;
3809 client->last_rc = rc = gpg_error (file_modified (client, cmd));
3810 if (!rc)
3811 update_client_state (client, CLIENT_STATE_COMMAND);
3813 return rc;
3817 * This is called after every Assuan command.
3819 static void
3820 command_finalize (assuan_context_t ctx, gpg_error_t rc)
3822 struct client_s *client = assuan_get_pointer (ctx);
3824 if (!(client->flags & FLAG_LOCK_CMD))
3825 unlock_file_mutex (client, 0);
3827 unlock_flock (&client->flock_fd);
3828 log_write2 (_("command completed: rc=%u"), rc ? rc : client->last_rc);
3829 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
3830 #ifdef WITH_GNUTLS
3831 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
3832 #endif
3833 if (client->thd->state != CLIENT_STATE_DISCON)
3834 update_client_state (client, CLIENT_STATE_IDLE);
3837 static gpg_error_t
3838 parse_help_opt_html (void *data, void *value)
3840 struct client_s *client = data;
3842 (void) value;
3843 client->opts |= OPT_HTML;
3844 return 0;
3847 static gpg_error_t
3848 help_command (assuan_context_t ctx, char *line)
3850 gpg_error_t rc;
3851 int i;
3852 struct client_s *client = assuan_get_pointer (ctx);
3853 struct argv_s *args[] = {
3854 &(struct argv_s) {"html", OPTION_TYPE_NOARG, parse_help_opt_html},
3855 NULL
3858 rc = parse_options (&line, args, client, 1);
3859 if (rc)
3860 return send_error (ctx, rc);
3862 if (!line || !*line)
3864 char *tmp;
3865 char *help = str_dup (_("Usage: HELP [--html] [<COMMAND>]\n"
3866 "For commands that take an element path as an argument, each element is "
3867 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3868 "@*@*COMMANDS:"));
3870 for (i = 0; command_table[i]; i++)
3872 if (!command_table[i]->help)
3873 continue;
3875 /* @npxref{} won't put a "See " or "see " in front of the command.
3876 * It's not a texinfo command but needed for --html. */
3877 tmp = str_asprintf ("%s %s%s%s", help,
3878 client->opts & OPT_HTML ? "@npxref{" : "",
3879 command_table[i]->name,
3880 client->opts & OPT_HTML ? "}" : "");
3881 xfree (help);
3882 help = tmp;
3885 tmp = strip_texi_and_wrap (help, client->opts & OPT_HTML);
3886 xfree (help);
3887 pthread_cleanup_push ((void *)xfree, tmp);
3888 rc = xfer_data (ctx, tmp, strlen (tmp));
3889 pthread_cleanup_pop (1);
3890 return send_error (ctx, rc);
3893 for (i = 0; command_table[i]; i++)
3895 if (!strcasecmp (line, command_table[i]->name))
3897 char *help, *tmp;
3899 if (!command_table[i]->help)
3900 break;
3902 help = strip_texi_and_wrap (command_table[i]->help,
3903 client->opts & OPT_HTML);
3904 tmp = str_asprintf ("%s%s",
3905 (client->opts & OPT_HTML) ? "" : _("Usage: "),
3906 help);
3907 xfree (help);
3908 pthread_cleanup_push ((void *)xfree, tmp);
3909 rc = xfer_data (ctx, tmp, strlen (tmp));
3910 pthread_cleanup_pop (1);
3911 return send_error (ctx, rc);
3915 return send_error (ctx, GPG_ERR_INV_NAME);
3918 static void
3919 new_command (const char *name, int ignore, int unlock, int flock_type,
3920 gpg_error_t (*handler) (assuan_context_t, char *),
3921 const char *help)
3923 int i = 0;
3924 struct command_table_s **tmp;
3926 if (command_table)
3927 for (i = 0; command_table[i]; i++);
3929 tmp = xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
3930 assert (tmp);
3931 command_table = tmp;
3932 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
3933 command_table[i]->name = name;
3934 command_table[i]->handler = handler;
3935 command_table[i]->ignore_startup = ignore;
3936 command_table[i]->unlock = unlock;
3937 command_table[i]->flock_type = flock_type;
3938 command_table[i++]->help = help;
3939 command_table[i] = NULL;
3942 void
3943 deinit_commands ()
3945 int i;
3947 for (i = 0; command_table[i]; i++)
3948 xfree (command_table[i]);
3950 xfree (command_table);
3953 static int
3954 sort_commands (const void *arg1, const void *arg2)
3956 struct command_table_s *const *a = arg1;
3957 struct command_table_s *const *b = arg2;
3959 if (!*a || !*b)
3960 return 0;
3961 else if (*a && !*b)
3962 return 1;
3963 else if (!*a && *b)
3964 return -1;
3966 return strcmp ((*a)->name, (*b)->name);
3969 static gpg_error_t
3970 passwd_command (assuan_context_t ctx, char *line)
3972 struct client_s *client = assuan_get_pointer (ctx);
3973 gpg_error_t rc;
3975 rc = peer_is_invoker (client);
3976 if (rc == GPG_ERR_EACCES)
3977 return send_error (ctx, GPG_ERR_FORBIDDEN);
3978 else if (rc)
3979 return send_error (ctx, rc);
3981 if (client->flags & FLAG_NEW)
3982 return send_error (ctx, GPG_ERR_INV_STATE);
3984 client->crypto->keyfile = config_get_string (client->filename,
3985 "passphrase_file");
3986 rc = crypto_init_ctx (client->crypto, client->flags & FLAG_NO_PINENTRY,
3987 client->crypto->keyfile);
3988 if (rc)
3989 return send_error (ctx, rc);
3991 if (!rc)
3992 rc = crypto_passwd (client, client->crypto);
3994 crypto_free_non_keys (client->crypto);
3995 return send_error (ctx, rc);
3998 static gpg_error_t
3999 parse_opt_data (void *data, void *value)
4001 struct client_s *client = data;
4003 (void) value;
4004 client->opts |= OPT_DATA;
4005 return 0;
4008 static gpg_error_t
4009 send_client_list (assuan_context_t ctx)
4011 struct client_s *client = assuan_get_pointer (ctx);
4012 gpg_error_t rc = 0;
4014 if (client->opts & OPT_VERBOSE)
4016 unsigned i, t;
4017 char **list = NULL;
4018 char *line;
4020 MUTEX_LOCK (&cn_mutex);
4021 pthread_cleanup_push ((void *)cleanup_mutex_cb, &cn_mutex);
4022 t = slist_length (cn_thread_list);
4024 for (i = 0; i < t; i++)
4026 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
4027 char *tmp;
4029 if (thd->state == CLIENT_STATE_UNKNOWN)
4030 continue;
4032 tmp = build_client_info_line (thd, 0);
4033 if (tmp)
4035 char **l = strv_cat (list, tmp);
4036 if (!l)
4037 rc = GPG_ERR_ENOMEM;
4038 else
4039 list = l;
4041 else
4042 rc = GPG_ERR_ENOMEM;
4044 if (rc)
4046 strv_free (list);
4047 break;
4051 pthread_cleanup_pop (1);
4052 if (rc)
4053 return rc;
4055 line = strv_join ("\n", list);
4056 strv_free (list);
4057 pthread_cleanup_push ((void *)xfree, line);
4058 rc = xfer_data (ctx, line, strlen (line));
4059 pthread_cleanup_pop (1);
4060 return rc;
4063 if (client->opts & OPT_DATA)
4065 char buf[ASSUAN_LINELENGTH];
4067 MUTEX_LOCK (&cn_mutex);
4068 snprintf (buf, sizeof (buf), "%u", slist_length (cn_thread_list));
4069 MUTEX_UNLOCK (&cn_mutex);
4070 rc = xfer_data (ctx, buf, strlen (buf));
4072 else
4073 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4075 return rc;
4078 static gpg_error_t
4079 getinfo_command (assuan_context_t ctx, char *line)
4081 struct client_s *client = assuan_get_pointer (ctx);
4082 gpg_error_t rc;
4083 char buf[ASSUAN_LINELENGTH];
4084 struct argv_s *args[] = {
4085 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4086 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_opt_verbose},
4087 NULL
4090 rc = parse_options (&line, args, client, 1);
4091 if (rc)
4092 return send_error (ctx, rc);
4094 if (!strcasecmp (line, "clients"))
4096 rc = send_client_list (ctx);
4098 else if (!strcasecmp (line, "cache"))
4100 if (client->opts & OPT_DATA)
4102 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4103 rc = xfer_data (ctx, buf, strlen (buf));
4105 else
4106 rc = send_status (ctx, STATUS_CACHE, NULL);
4108 else if (!strcasecmp (line, "pid"))
4110 char buf[32];
4111 pid_t pid = getpid ();
4113 snprintf (buf, sizeof (buf), "%i", pid);
4114 rc = xfer_data (ctx, buf, strlen (buf));
4116 else if (!strcasecmp (line, "version"))
4118 char *buf = str_asprintf ("0x%06x %s", VERSION_HEX,
4119 #ifdef WITH_GNUTLS
4120 "GNUTLS "
4121 #endif
4122 #ifdef WITH_LIBACL
4123 "ACL "
4124 #endif
4125 "");
4126 rc = xfer_data (ctx, buf, strlen (buf));
4127 xfree (buf);
4129 else if (!strcasecmp (line, "last_error"))
4131 if (client->last_error)
4132 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4133 else
4134 rc = GPG_ERR_NO_DATA;
4136 else if (!strcasecmp (line, "user"))
4138 char *user = NULL;
4140 #ifdef WITH_GNUTLS
4141 if (client->thd->remote)
4142 user = str_asprintf ("#%s", client->thd->tls->fp);
4143 else
4144 user = get_username (client->thd->peer->uid);
4145 #else
4146 user = get_username (client->thd->peer->uid);
4147 #endif
4148 if (user)
4150 pthread_cleanup_push ((void *)xfree, user);
4151 rc = xfer_data (ctx, user, strlen (user));
4152 pthread_cleanup_pop (1);
4154 else
4155 rc = GPG_ERR_NO_DATA;
4157 else
4158 rc = gpg_error (GPG_ERR_SYNTAX);
4160 return send_error (ctx, rc);
4163 static gpg_error_t
4164 parse_listkeys_opt_secret_only (void *data, void *value)
4166 struct client_s *client = data;
4168 (void) value;
4169 client->opts |= OPT_SECRET_ONLY;
4170 return 0;
4173 static gpg_error_t
4174 keyinfo_command (assuan_context_t ctx, char *line)
4176 struct client_s *client = assuan_get_pointer (ctx);
4177 gpg_error_t rc = 0;
4178 char **keys = NULL, **p = NULL;
4179 int sym = 0;
4181 if (line && *line)
4182 return send_error (ctx, GPG_ERR_SYNTAX);
4184 if (!(client->flags & FLAG_OPEN))
4185 return send_error (ctx, GPG_ERR_INV_STATE);
4187 if (client->flags & FLAG_NEW)
4188 return send_error (ctx, GPG_ERR_NO_DATA);
4190 rc = lock_flock (ctx, client->filename, FLOCK_TYPE_SH, &client->flock_fd);
4191 if (rc)
4192 return send_error (ctx, rc);
4194 rc = crypto_is_symmetric (client->filename);
4195 unlock_flock (&client->flock_fd);
4196 if (!rc)
4197 sym = 1;
4198 else if (rc != GPG_ERR_BAD_DATA)
4199 return send_error (ctx, rc);
4201 rc = 0;
4202 if (!sym)
4204 p = strv_catv(keys, client->crypto->pubkey);
4205 if (!p)
4206 rc = GPG_ERR_ENOMEM;
4209 if (!rc)
4211 keys = p;
4212 for (p = client->crypto->sigkey; p && *p; p++)
4214 if (!strv_printf(&keys, "S%s", *p))
4216 strv_free (keys);
4217 return send_error (ctx, GPG_ERR_ENOMEM);
4221 else
4222 rc = GPG_ERR_ENOMEM;
4224 if (!rc && keys)
4226 line = strv_join ("\n", keys);
4227 strv_free (keys);
4228 pthread_cleanup_push ((void *)xfree, line);
4229 if (line)
4230 rc = xfer_data (ctx, line, strlen (line));
4231 else
4232 rc = GPG_ERR_ENOMEM;
4234 pthread_cleanup_pop (1);
4236 else if (!keys)
4237 rc = GPG_ERR_NO_DATA;
4238 else
4239 strv_free (keys);
4241 return send_error (ctx, rc);
4244 static gpg_error_t
4245 kill_command (assuan_context_t ctx, char *line)
4247 struct client_s *client = assuan_get_pointer (ctx);
4248 gpg_error_t rc;
4249 unsigned i, t;
4251 if (!line || !*line)
4252 return send_error (ctx, GPG_ERR_SYNTAX);
4254 MUTEX_LOCK (&cn_mutex);
4255 pthread_cleanup_push ((void *)cleanup_mutex_cb, &cn_mutex);
4256 t = slist_length (cn_thread_list);
4257 rc = GPG_ERR_ESRCH;
4259 for (i = 0; i < t; i++)
4261 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
4262 char *tmp = str_asprintf ("%p", thd->tid);
4264 if (strcmp (line, tmp))
4266 xfree (tmp);
4267 continue;
4270 xfree (tmp);
4271 rc = peer_is_invoker (client);
4272 if (!rc)
4274 #ifdef HAVE_PTHREAD_CANCEL
4275 rc = pthread_cancel (thd->tid);
4276 #else
4277 close (thd->fd);
4278 thd->fd = -1;
4279 #endif
4280 break;
4283 rc = GPG_ERR_FORBIDDEN;
4284 if (config_get_boolean ("global", "strict_kill"))
4285 break;
4287 #ifdef WITH_GNUTLS
4288 if (client->thd->remote && thd->remote)
4290 if (!strcmp (client->thd->tls->fp, thd->tls->fp))
4292 #ifdef HAVE_PTHREAD_CANCEL
4293 rc = pthread_cancel (thd->tid);
4294 #else
4295 close (thd->fd);
4296 thd->fd = -1;
4297 #endif
4298 break;
4301 else if (!client->thd->remote && !thd->remote)
4302 #endif
4304 if (client->thd->peer->uid == thd->peer->uid)
4306 #ifdef HAVE_PTHREAD_CANCEL
4307 rc = pthread_cancel (thd->tid);
4308 #else
4309 close (thd->fd);
4310 thd->fd = -1;
4311 #endif
4314 break;
4317 pthread_cleanup_pop (1);
4318 return send_error (ctx, rc);
4321 static gpg_error_t
4322 listkeys_command (assuan_context_t ctx, char *line)
4324 struct client_s *client = assuan_get_pointer (ctx);
4325 struct crypto_s *crypto = NULL;
4326 char **pattern = NULL;
4327 gpgme_key_t *keys = NULL;
4328 char **result = NULL;
4329 gpg_error_t rc;
4330 struct argv_s *args[] = {
4331 &(struct argv_s) {"secret-only", OPTION_TYPE_NOARG,
4332 parse_listkeys_opt_secret_only },
4333 NULL
4336 rc = parse_options (&line, args, client, 1);
4337 if (rc)
4338 return send_error (ctx, rc);
4340 rc = crypto_init (&crypto, client->ctx, client->filename,
4341 client->flags & FLAG_NO_PINENTRY, NULL);
4342 if (rc)
4343 return send_error (ctx, rc);
4345 pthread_cleanup_push ((void *)crypto_free, crypto);
4346 pattern = str_split (line, ",", 0);
4347 pthread_cleanup_push ((void *)(void *)strv_free, pattern);
4348 rc = crypto_list_keys (crypto, pattern, client->opts & OPT_SECRET_ONLY,
4349 &keys);
4350 pthread_cleanup_pop (1);
4351 pthread_cleanup_pop (1);
4352 if (!rc)
4354 int i;
4356 for (i = 0; keys[i]; i++)
4358 char *p = crypto_key_info (keys[i]);
4359 char **r;
4361 if (!p)
4363 rc = GPG_ERR_ENOMEM;
4364 break;
4367 r = strv_cat (result, p);
4368 if (!r)
4370 rc = GPG_ERR_ENOMEM;
4371 break;
4374 result = r;
4377 if (!i)
4378 rc = GPG_ERR_NO_DATA;
4380 crypto_free_key_list (keys);
4383 if (!rc)
4385 line = strv_join ("\n", result);
4386 strv_free (result);
4387 pthread_cleanup_push ((void *)xfree, line);
4388 if (!line)
4389 rc = GPG_ERR_ENOMEM;
4390 else
4391 rc = xfer_data (ctx, line, strlen (line));
4393 pthread_cleanup_pop (1);
4395 else
4396 strv_free (result);
4398 return send_error (ctx, rc);
4401 void
4402 init_commands ()
4404 /* !BEGIN-HELP-TEXT!
4406 * This comment is used as a marker to generate the offline documentation
4407 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4408 * script to determine where commands begin and end.
4410 new_command("HELP", 1, 1, 0, help_command, _(
4411 "HELP [--html] [<COMMAND>]\n"
4412 "Show available commands or command specific help text."
4413 "@*@*"
4414 "The @option{--html} option will output the help text in HTML format."
4417 new_command("KILL", 1, 0, 0, kill_command, _(
4418 "KILL <thread_id>\n"
4419 "Terminates the client identified by @var{thread_id} and releases any file "
4420 "lock or other resources it has held. See @code{GETINFO} (@pxref{GETINFO}) "
4421 "for details about listing connected clients. An @code{invoking_user} "
4422 "(@pxref{Configuration}) may kill any client while others may only kill "
4423 "clients of the same @code{UID} or @abbr{TLS} fingerprint.\n"
4426 new_command("LISTKEYS", 0, 0, 0, listkeys_command, _(
4427 "LISTKEYS [--secret-only] [pattern[,<pattern>]]\n"
4428 "Returns a new line separated list of key information matching a comma "
4429 "separated list of @var{pattern}'s. When option @option{--secret-only} is "
4430 "specified, only keys matching @var{pattern} that also have a secret key "
4431 "available will be returned."
4434 new_command("KEYINFO", 1, 0, 0, keyinfo_command, _(
4435 "KEYINFO\n"
4436 "Returns a new line separated list of key ID's that the currently opened "
4437 "data file has recipients and signers for. If the key is a signing key it "
4438 "will be prefixed with an @code{S}. If the file is a new one, or has no "
4439 "signers in the case of being symmetrically encrypted, the error code "
4440 "@code{GPG_ERR_NO_DATA} is returned."
4443 new_command("GETINFO", 1, 1, 0, getinfo_command, _(
4444 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4445 "Get server and other information. The information is returned via a status "
4446 "message (@pxref{Status Messages}) unless otherwise noted or @option{--data} "
4447 "is specified."
4448 "@*@*"
4449 "@var{CACHE} returns the number of cached documents."
4450 "@*@*"
4451 "@var{CLIENTS} returns the number of "
4452 "connected clients via a status message or a list of connected clients when "
4453 "the @option{--verbose} parameter is used (implies @option{--data}). A "
4454 "verbose line of a client list contains "
4455 "space delimited "
4456 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4457 "IP address if remote, file lock status, whether the current client is self "
4458 "or not, client state (see below), "
4459 "user ID or TLS fingerprint of the connected client, username if the "
4460 "client is a local one else @code{-}, and finally the time stamp of when the "
4461 "client connected."
4462 "@*@*"
4463 "Client state @code{0} is an unknown client state, state @code{1} indicates "
4464 "the client has connected but hasn't completed initializing, state @code{2} "
4465 "indicates that the client is idle, state @code{3} means the "
4466 "client is in a command and state @code{4} means the client is disconnecting. "
4467 "@*@*"
4468 "@var{PID} returns the process ID number of the server via a data response."
4469 "@*@*"
4470 "@var{VERSION} returns the server version number and compile-time features "
4471 "via a data response with each being space delimited."
4472 "@*@*"
4473 "@var{LAST_ERROR} returns a detailed description of the last failed command "
4474 "via a data response, when available."
4475 "@*@*"
4476 "@var{USER} returns the username or @abbr{TLS} hash of the connected client "
4477 "via a data response."
4480 new_command("PASSWD", 0, 0, FLOCK_TYPE_EX|FLOCK_TYPE_KEEP, passwd_command, _(
4481 "PASSWD\n"
4482 "Changes the passphrase of the secret key required to open the current "
4483 "data file. If the data file is symmetrically encrypted, the error "
4484 "@code{GPG_ERR_NOT_SUPPORTED} is returned. When symmetrically encrypted, "
4485 "the @code{SAVE} command (@pxref{SAVE}) should be used instead to prevent "
4486 "this command saving any unwanted changes to the @abbr{XML} document."
4487 "@*@*"
4488 "This command is not available to non-invoking clients "
4489 "(@pxref{Access Control})."
4492 new_command("OPEN", 1, 1, FLOCK_TYPE_SH|FLOCK_TYPE_KEEP, open_command, _(
4493 "OPEN [--lock] <filename>\n"
4494 "Opens @var{filename}. When the @var{filename} is not found on the "
4495 "file-system then a new in-memory document will be created. If the file is "
4496 "found, it is looked for in the file cache and when found no passphrase will "
4497 "be required to open it. When not cached, @cite{pinentry(1)} will be used to "
4498 "retrieve the passphrase for decryption unless @option{disable-pinentry} "
4499 "(@pxref{OPTION}) was specified in which case @command{pwmd} will "
4500 "@emph{INQUIRE} the client for the passphrase."
4501 "@*@*"
4502 "When the @option{--lock} option is passed then the file mutex will be "
4503 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4504 "file had been opened."
4507 new_command("SAVE", 0, 0, FLOCK_TYPE_EX|FLOCK_TYPE_KEEP, save_command, _(
4508 "SAVE [--symmetric] [--inquire-keyparam] [--keyid=<keyid>[,..] | [--inquire-keyid]] [--sign-keyid=<fingerprint>[,..] | [--inquire-sign-keyid]]\n"
4509 "Writes the in-memory @abbr{XML} document to disk. The file written to is the "
4510 "file that was opened using the @code{OPEN} command (@pxref{OPEN}). If the "
4511 "file is a new one or the option @option{--inquire-keyparam} was passed, a "
4512 "new keypair will be generated and @cite{pinentry(1)} will be used to prompt "
4513 "for the passphrase of the secret key used for signing."
4514 "@*@*"
4515 "The @option{--inquire-keyparam} option will send an "
4516 "@emph{INQUIRE} to the client to obtain the key parameters to use for "
4517 "generating the new keypair. The inquired data is expected to be in "
4518 "@cite{gnupg} @abbr{XML} format. See the @cite{gnupg} documentation for "
4519 "details. Note that when this option is specified a new keypair will be "
4520 "generated reguardless if the file is a new one and that the passphrase for "
4521 "the current file will be required before generating the new keypair. This "
4522 "option is available to non-invoking clients (@pxref{Access Control}) only "
4523 "when the file is a new one."
4524 "@*@*"
4525 "You can encrypt the data file to a recipient other than the one that it "
4526 "was encrypted with by passing the @option{--keyid} or "
4527 "@option{--inquire-keyid} option with "
4528 "the key ID of a public encryption key as its argument. Use the "
4529 "@command{LISTKEYS} command (@pxref{LISTKEYS}) to show key information by "
4530 "pattern. The @option{--sign-keyid} or @option{--inquire-sign-keyid} option "
4531 "may also be used to sign the data "
4532 "file with an alternate key by specifying the key ID of a secret key. "
4533 "A passphrase to decrypt the data file "
4534 "will be required if one or more of the original encryption or signing keys "
4535 "are not found in either of these two options' arguments. The original "
4536 "encryption or signing keys will be used when either of these options are "
4537 "not specified."
4538 "@*@*"
4539 "The @option{--keyid} and @option{--sign-keyid} options are not available "
4540 "for non-invoking clients "
4541 "(@pxref{Access Control}) when the recipients or signers do not match those "
4542 "that were used when the file was @code{OPEN}'ed."
4543 "@*@*"
4544 "The @option{--symmetric} option specifies that a new data file be "
4545 "conventionally encrypted. These types of data files do not use a recipient "
4546 "public key but may be signed by using the @option{--sign-keyid} or "
4547 "@option{--inquire-sign-keyid} options. To remove all signers from a "
4548 "symmtrically encrypted data file, leave the option value empty. Note that "
4549 "you cannot change encryption schemes once a data file has been saved."
4552 new_command("ISCACHED", 1, 0, 0, iscached_command, _(
4553 "ISCACHED [--lock] [--agent [--sign]] <filename>\n"
4554 "Determines the file cache status of the specified @var{filename}. "
4555 "The default is to test whether the filename is cached in memory. Passing "
4556 "option @option{--agent} will test the @command{gpg-agent} cache for at most "
4557 "one cached key used for opening the data file (@pxref{OPEN}). To test if "
4558 "a signing key is cached, pass @option{--sign} along with @option{--agent}. "
4559 "Both the @option{--agent} and @option{--sign} options require an opened data "
4560 "file."
4561 "@*@*"
4562 "An @emph{OK} response is returned if the specified @var{filename} is found "
4563 "in the cache. If not found in the cache but exists on the filesystem "
4564 "then @code{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4565 "returned."
4566 "@*@*"
4567 "The @option{--lock} option will lock the file mutex of @var{filename} when "
4568 "the file exists; it does not need to be opened nor cached. The lock will be "
4569 "released when the client exits or sends the @code{UNLOCK} command "
4570 "(@pxref{UNLOCK})."
4573 new_command("CLEARCACHE", 1, 1, 0, clearcache_command, _(
4574 "CLEARCACHE [<filename>]\n"
4575 "Clears a file cache entry for all or the specified @var{filename}. Note that "
4576 "this will also clear any @command{gpg-agent} cached keys which may cause "
4577 "problems if another data file shares the same keys as @var{filename}."
4580 new_command("CACHETIMEOUT", 1, 1, 0, cachetimeout_command, _(
4581 "CACHETIMEOUT <filename> <seconds>\n"
4582 "The time in @var{seconds} until @var{filename} will be removed from the "
4583 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4584 "the passphrase for each @code{OPEN} command (@pxref{OPEN}) or @code{SAVE} "
4585 "(@pxref{SAVE}) command. @xref{Configuration}, and the @code{cache_timeout} "
4586 "parameter."
4589 new_command("LIST", 0, 1, 0, list_command, _(
4590 "LIST [--inquire] [--recurse] [element[<TAB>child[..]]]\n"
4591 "If no element path is given then a newline separated list of root elements "
4592 "is returned with a data response. If given, then children of the specified "
4593 "element path are returned."
4594 "@*@*"
4595 "Each element path "
4596 "returned will have zero or more flags appened to it. These flags are "
4597 "delimited from the element path by a single space character. A flag itself "
4598 "is a single character. Flag @code{P} indicates that access to the element "
4599 "is denied. Flag @code{+} indicates that there are child nodes of "
4600 "the current element path. Flag @code{E} indicates that an element of the "
4601 "element path contained in a @var{target} attribute could not be found. Flag "
4602 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4603 "(@pxref{Configuration}). Flag @code{T}, followed by a single space character, "
4604 "then an element path, is the element path of the @var{target} attribute "
4605 "contained in the current element."
4606 "@*@*"
4607 "When a specified element path contains an error, beit from the final "
4608 "element in the path or any previous element, the path is still shown but "
4609 "will contain the error flag for the element with the error. Determining "
4610 "the actual element which contains the error is up to the client. This can be "
4611 "done by traversing the final element up to parent elements that contain the "
4612 "same error flag."
4613 "@*@*"
4614 "The option @option{--recurse} may be used to list the entire element tree "
4615 "for a specified element path or the entire tree for all root elements."
4616 "@*@*"
4617 "When the @option{--inquire} option is passed then all remaining non-option "
4618 "arguments are retrieved via a server @emph{INQUIRE}."
4621 new_command("REALPATH", 0, 1, 0, realpath_command, _(
4622 "REALPATH [--inquire] element[<TAB>child[..]]\n"
4623 "Resolves all @code{target} attributes of the specified element path and "
4624 "returns the result with a data response. @xref{Target Attribute}, for details."
4625 "@*@*"
4626 "When the @option{--inquire} option is passed then all remaining non-option "
4627 "arguments are retrieved via a server @emph{INQUIRE}."
4630 new_command("STORE", 0, 1, 0, store_command, _(
4631 "STORE element[<TAB>child[..]]<TAB>[content]\n"
4632 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4633 "@*@*"
4634 "Creates a new element path or modifies the @var{content} of an existing "
4635 "element. If only a single element is specified then a new root element is "
4636 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4637 "set to the final @key{TAB} delimited element. If no @var{content} is "
4638 "specified after the final @key{TAB}, then the content of the existing "
4639 "element will be removed; or will be empty if creating a new element."
4640 "@*@*"
4641 "The only restriction of an element name is that it not contain whitespace "
4642 "characters. There is no other whitespace between the @key{TAB} delimited "
4643 "elements. It is recommended that the content of an element be base64 encoded "
4644 "when it contains control or @key{TAB} characters to prevent @abbr{XML} "
4645 "parsing and @command{pwmd} syntax errors."
4648 new_command("RENAME", 0, 1, 0, rename_command, _(
4649 "RENAME [--inquire] element[<TAB>child[..]] <value>\n"
4650 "Renames the specified @var{element} to the new @var{value}. If an element of "
4651 "the same name as the @var{value} already exists it will be overwritten."
4652 "@*@*"
4653 "When the @option{--inquire} option is passed then all remaining non-option "
4654 "arguments are retrieved via a server @emph{INQUIRE}."
4657 new_command("COPY", 0, 1, 0, copy_command, _(
4658 "COPY [--inquire] source[<TAB>child[..]] dest[<TAB>child[..]]\n"
4659 "Copies the entire element tree starting from the child node of the source "
4660 "element, to the destination element path. If the destination element path "
4661 "does not exist then it will be created; otherwise it is overwritten."
4662 "@*@*"
4663 "Note that attributes from the source element are merged into the "
4664 "destination element when the destination element path exists. When an "
4665 "attribute of the same name exists in both the source and destination "
4666 "elements then the destination attribute will be updated to the source "
4667 "attribute value."
4668 "@*@*"
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("MOVE", 0, 1, 0, move_command, _(
4674 "MOVE [--inquire] source[<TAB>child[..]] [dest[<TAB>child[..]]]\n"
4675 "Moves the source element path to the destination element path. If the "
4676 "destination is not specified then it will be moved to the root node of the "
4677 "document. If the destination is specified and exists then it will be "
4678 "overwritten; otherwise non-existing elements of the destination element "
4679 "path will be created."
4680 "@*@*"
4681 "When the @option{--inquire} option is passed then all remaining non-option "
4682 "arguments are retrieved via a server @emph{INQUIRE}."
4685 new_command("DELETE", 0, 1, 0, delete_command, _(
4686 "DELETE [--inquire] element[<TAB>child[..]]\n"
4687 "Removes the specified element path and all of its children. This may break "
4688 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4689 "refers to this element or any of its children."
4690 "@*@*"
4691 "When the @option{--inquire} option is passed then all remaining non-option "
4692 "arguments are retrieved via a server @emph{INQUIRE}."
4695 new_command("GET", 0, 1, 0, get_command, _(
4696 "GET [--inquire] element[<TAB>child[..]]\n"
4697 "Retrieves the content of the specified element. The content is returned "
4698 "with a data response."
4699 "@*@*"
4700 "When the @option{--inquire} option is passed then all remaining non-option "
4701 "arguments are retrieved via a server @emph{INQUIRE}."
4704 new_command("ATTR", 0, 1, 0, attr_command, _(
4705 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] element[<TAB>child[..]] ..\n"
4706 "@table @asis\n"
4707 "@item ATTR SET attribute element[<TAB>child[..]] [value]\n"
4708 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4709 "element. When no @var{value} is specified any existing value will be removed."
4710 "@*@*"
4711 "@item ATTR DELETE attribute element[<TAB>child[..]]\n"
4712 " Removes an attribute from an element. If @var{attribute} is @code{_name} "
4713 "or @code{target} an error is returned. Use the @command{DELETE} command "
4714 "(@pxref{DELETE}) instead."
4715 "@*@*"
4716 "@item ATTR LIST element[<TAB>child[..]]\n"
4717 " Retrieves a newline separated list of attributes names and values "
4718 "from the specified element. Each attribute name and value is space delimited."
4719 "@*@*"
4720 "@item ATTR GET attribute element[<TAB>child[..]]\n"
4721 " Retrieves the value of an @var{attribute} from an element."
4722 "@end table\n"
4723 "@*@*"
4724 "When the @option{--inquire} option is passed then all remaining non-option "
4725 "arguments are retrieved via a server @emph{INQUIRE}."
4726 "@*@*"
4727 "@xref{Target Attribute}, for details about this special attribute and also "
4728 "@pxref{Other Attributes} for other attributes that are handled specially "
4729 "by @command{pwmd}."
4732 new_command("XPATH", 0, 1, 0, xpath_command, _(
4733 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4734 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4735 "specified it is assumed the expression is a request to return a result. "
4736 "Otherwise, the result is set to the @var{value} argument and the document is "
4737 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4738 "is assumed to be empty and the document is updated. For example:"
4739 "@sp 1\n"
4740 "@example\n"
4741 "XPATH //element[@@_name='password']@key{TAB}\n"
4742 "@end example\n"
4743 "@sp 1\n"
4744 "would clear the content of all @var{password} elements in the data file "
4745 "while leaving off the trailing @key{TAB} would return all @var{password} "
4746 "elements in @abbr{XML} format."
4747 "@*@*"
4748 "When the @option{--inquire} option is passed then all remaining non-option "
4749 "arguments are retrieved via a server @emph{INQUIRE}."
4750 "@*@*"
4751 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4752 "expression syntax."
4755 new_command("XPATHATTR", 0, 1, 0, xpathattr_command, _(
4756 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4757 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4758 "attributes and does not return a result. For the @var{SET} operation the "
4759 "@var{value} is optional but the field is required. If not specified then "
4760 "the attribute value will be empty. For example:"
4761 "@sp 1\n"
4762 "@example\n"
4763 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4764 "@end example\n"
4765 "@sp 1\n"
4766 "would create a @var{password} attribute for each @var{password} element "
4767 "found in the document. The attribute value will be empty but still exist."
4768 "@*@*"
4769 "When the @option{--inquire} option is passed then all remaining non-option "
4770 "arguments are retrieved via a server @emph{INQUIRE}."
4771 "@*@*"
4772 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4773 "expression syntax."
4776 new_command("IMPORT", 0, 1, 0, import_command, _(
4777 "IMPORT [--root=element[<TAB>child[..]]]\n"
4778 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4779 "@*@*"
4780 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4781 "argument is raw @abbr{XML} data. The content is created as a child of "
4782 "the element path specified with the @option{--root} option or at the "
4783 "document root when not specified. Existing elements of the same name will "
4784 "be overwritten."
4785 "@*@*"
4786 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4787 "for details."
4790 new_command("DUMP", 0, 1, 0, dump_command, _(
4791 "DUMP\n"
4792 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4793 "dumping a specific node."
4796 new_command("LOCK", 0, 0, 0, lock_command, _(
4797 "LOCK\n"
4798 "Locks the mutex associated with the opened file. This prevents other clients "
4799 "from sending commands to the same opened file until the client "
4800 "that sent this command either disconnects or sends the @code{UNLOCK} "
4801 "command. @xref{UNLOCK}."
4804 new_command("UNLOCK", 1, 0, 0, unlock_command, _(
4805 "UNLOCK\n"
4806 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4807 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4808 "@pxref{ISCACHED})."
4811 new_command("GETCONFIG", 1, 1, 0, getconfig_command, _(
4812 "GETCONFIG [filename] <parameter>\n"
4813 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4814 "data response. If no file has been opened then the value for @var{filename} "
4815 "or the default from the @var{global} section will be returned. If a file "
4816 "has been opened and no @var{filename} is specified, the value previously "
4817 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4820 new_command("OPTION", 1, 1, 0, option_command, _(
4821 "OPTION <NAME>=[<VALUE>]\n"
4822 "Sets a client option @var{name} to @var{value}. The value for an option is "
4823 "kept for the duration of the connection with the exception of the "
4824 "@command{pinentry} options which are defaults for all future connections "
4825 "(@pxref{Pinentry}). When @var{value} is empty the option is unset."
4826 "@*@*"
4827 "@table @asis\n"
4828 "@item DISABLE-PINENTRY\n"
4829 "Disable use of @command{pinentry} for passphrase retrieval. When @code{1}, a "
4830 "server inquire is sent to the client to obtain the passphrase. This option "
4831 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4832 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands. Set to @code{0} "
4833 "to use a @command{pinentry}."
4834 "@*@*"
4835 "@item DISPLAY\n"
4836 "Set or unset the X11 display to use when prompting for a passphrase."
4837 "@*@*"
4838 "@item TTYNAME\n"
4839 "Set the terminal device path to use when prompting for a passphrase."
4840 "@*@*"
4841 "@item TTYTYPE\n"
4842 "Set the terminal type for use with @option{TTYNAME}."
4843 "@*@*"
4844 "@item NAME\n"
4845 "Associates the thread ID of the connection with the specified textual "
4846 "representation. Useful for debugging log messages. May not contain whitespace."
4847 "@*@*"
4848 "@item LOCK-TIMEOUT\n"
4849 "When not @code{0}, the duration in tenths of a second to wait for the file "
4850 "mutex which has been locked by another thread to be released before returning "
4851 "an error. When @code{-1} the error will be returned immediately."
4852 "@end table\n"
4855 new_command("LS", 1, 1, 0, ls_command, _(
4856 "LS\n"
4857 "Returns a newline separated list of data files stored in the data directory "
4858 "@file{HOMEDIR/data} (@pxref{Invoking}) with a data response."
4861 new_command("RESET", 1, 1, 0, NULL, _(
4862 "RESET\n"
4863 "Closes the currently opened file but keeps any previously set client options "
4864 "(@pxref{OPTION})."
4867 new_command("NOP", 1, 1, 0, NULL, _(
4868 "NOP\n"
4869 "Does nothing. Always returns successfully."
4872 /* !END-HELP-TEXT! */
4873 new_command ("CANCEL", 1, 1, 0, NULL, NULL);
4874 new_command ("END", 1, 1, 0, NULL, NULL);
4875 new_command ("BYE", 1, 1, 0, NULL, NULL);
4877 int i;
4878 for (i = 0; command_table[i]; i++);
4879 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4880 sort_commands);
4883 gpg_error_t
4884 register_commands (assuan_context_t ctx)
4886 int i = 0, rc;
4888 for (; command_table[i]; i++)
4890 if (!command_table[i]->handler)
4891 continue;
4893 rc = assuan_register_command (ctx, command_table[i]->name,
4894 command_table[i]->handler,
4895 command_table[i]->help);
4896 if (rc)
4897 return rc;
4900 rc = assuan_register_bye_notify (ctx, bye_notify);
4901 if (rc)
4902 return rc;
4904 rc = assuan_register_reset_notify (ctx, reset_notify);
4905 if (rc)
4906 return rc;
4908 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4909 if (rc)
4910 return rc;
4912 return assuan_register_post_cmd_notify (ctx, command_finalize);