Merge branch 'agent-cache-checking'
[pwmd.git] / src / commands.c
blobac8f3b3eec1159a17e5433d730edb052ad6af3ad
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 *,
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, 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, struct cache_data_s *cdata,
362 unsigned char **r_crc, size_t *r_crclen)
364 unsigned char *crc;
365 size_t len;
366 gpg_error_t rc;
367 int n = 0;
369 if (cdata && !cdata->crc)
370 return GPG_ERR_CHECKSUM;
372 rc = get_checksum (client->filename, &crc, &len);
373 if (rc)
374 return rc;
376 if (cdata)
377 n = memcmp (cdata->crc, crc, len);
378 else if (client->crc)
379 n = memcmp (client->crc, crc, len);
381 if (!n && r_crc)
383 *r_crc = crc;
384 *r_crclen = len;
386 else
387 xfree (crc);
389 return n ? GPG_ERR_CHECKSUM : 0;
392 static gpg_error_t
393 open_command (assuan_context_t ctx, char *line)
395 gpg_error_t rc;
396 struct client_s *client = assuan_get_pointer (ctx);
397 char **req, *filename;
398 unsigned char md5file[16];
399 int same_file = 0;
400 assuan_peercred_t peer;
401 struct cache_data_s *cdata = NULL;
402 int cached = 0;
403 unsigned char *crc = NULL;
404 size_t crclen = 0;
405 struct argv_s *args[] = {
406 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
407 NULL
410 rc = parse_options (&line, args, client, 1);
411 if (rc)
412 return send_error (ctx, rc);
414 req = str_split (line, " ", 2);
415 if (!req)
416 return send_error (ctx, GPG_ERR_SYNTAX);
418 rc = do_validate_peer (ctx, req[0], &peer);
419 if (rc)
421 strv_free (req);
422 return send_error (ctx, rc);
425 filename = req[0];
426 if (!valid_filename (filename))
428 strv_free (req);
429 return send_error (ctx, GPG_ERR_INV_VALUE);
432 pthread_cleanup_push ((void *)req_cleanup, req);
433 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, filename, strlen (filename));
434 /* This client may have locked a different file with ISCACHED --lock than
435 * the current filename. This will remove that lock. */
436 same_file = !memcmp (md5file, client->md5file, 16) ? 1 : 0;
437 if (client->flags & FLAG_OPEN ||
438 (client->flags & FLAG_HAS_LOCK && !same_file))
440 uint32_t opts = client->opts;
441 uint32_t flags = client->flags;
443 if (same_file)
444 client->flags |= FLAG_KEEP_LOCK;
445 else if (client->flags & FLAG_NEW)
446 cache_clear (client->md5file, 0);
448 cleanup_client (client);
449 client->opts = opts;
450 client->flags |= flags;
451 client->flags &= ~(FLAG_LOCK_CMD);
452 if (!same_file)
453 client->flags &= ~(FLAG_HAS_LOCK | FLAG_NEW);
456 memcpy (client->md5file, md5file, 16);
457 client->filename = str_dup (filename);
458 if (!client->filename)
460 strv_free (req);
461 return send_error(ctx, GPG_ERR_ENOMEM);
464 /* Need to lock the mutex here because file_modified() cannot without
465 * knowing the filename. */
466 rc = lock_file_mutex (client, 1);
467 if (rc)
468 client->flags &= ~FLAG_OPEN;
470 if (!rc)
472 rc = lock_flock (ctx, filename, LOCK_SH, &client->flock_fd);
473 if (gpg_err_code (rc) == GPG_ERR_ENOENT)
474 rc = 0;
477 if (!rc)
479 struct stat st;
481 rc = crypto_init (&client->crypto, client->ctx, client->filename,
482 client->flags & FLAG_NO_PINENTRY);
483 if (!rc && stat (client->filename, &st) == -1)
484 rc = gpg_error_from_errno (errno);
485 else if (!rc && !S_ISREG (st.st_mode)) // to match LS output
486 rc = gpg_error_from_errno (ENOANO);
489 if (!rc || gpg_err_code (rc) == GPG_ERR_ENOENT)
491 cdata = cache_get_data (client->md5file);
493 if (rc) // new file
495 rc = 0;
496 client->flags |= FLAG_NEW;
497 // data file disappeared. clear the cache entry.
498 cache_clear (client->md5file, 1);
499 cdata = NULL;
501 else if (cdata && cdata->doc) // cached document
503 int reload = 0;
504 int defer = 0;
506 if (!rc && !(client->flags & FLAG_NEW))
508 rc = validate_checksum (client, cdata, &crc, &crclen);
509 if (rc == GPG_ERR_CHECKSUM)
511 rc = 0;
512 reload = 1;
516 if (!rc)
518 rc = cache_iscached (client->filename, &defer, 0, 0);
519 if ((!rc || rc == GPG_ERR_ENOENT) && defer)
521 rc = 0;
522 reload = 2;
526 if (!rc && reload)
528 if (reload == 1)
529 log_write ("%s: %s", client->filename,
530 pwmd_strerror (GPG_ERR_CHECKSUM));
531 cache_clear (client->md5file, 1);
532 cdata = NULL;
533 rc = crypto_decrypt (client, client->crypto);
535 #ifdef WITH_GNUTLS
536 else if (!rc)
538 if (client->thd->remote
539 && config_get_boolean (client->filename, "tcp_require_key")
540 && !(client->flags & FLAG_NEW))
541 rc = crypto_try_decrypt (client,
542 (client->flags & FLAG_NO_PINENTRY));
544 #endif
546 if (!rc && cdata)
548 client->crypto->plaintext = cdata->doc;
549 client->crypto->plaintext_size = cdata->size;
550 rc = cache_decrypt (client->crypto);
551 if (!rc)
553 cdata->doc = NULL;
554 cdata->size = 0;
555 strv_free (client->crypto->pubkey);
556 strv_free (client->crypto->sigkey);
557 client->crypto->pubkey = strv_dup (cdata->pubkey);
558 client->crypto->sigkey = strv_dup (cdata->sigkey);
559 cached = 1;
561 else
563 client->crypto->plaintext = NULL;
564 client->crypto->plaintext_size = 0;
568 else // existing file
570 cached = cdata != NULL;
571 rc = crypto_decrypt (client, client->crypto);
575 if (!rc)
577 rc = parse_xml (ctx, client->flags & FLAG_NEW);
578 if (!rc)
580 rc = cache_encrypt (client->crypto);
581 if (rc)
582 cache_clear (client->md5file, 1);
583 else
585 int timeout = config_get_integer (client->filename,
586 "cache_timeout");
588 free_cache_data_once (cdata);
589 cdata = xcalloc (1, sizeof (struct cache_data_s));
590 cdata->doc = client->crypto->plaintext;
591 cdata->size = client->crypto->plaintext_size;
592 cdata->pubkey = strv_dup(client->crypto->pubkey);
593 cdata->sigkey = strv_dup(client->crypto->sigkey);
594 client->crypto->plaintext = NULL;
595 client->crypto->plaintext_size = 0;
597 if (cached) // wont increment the refcount
599 /* Prevent using another FD to update the checksum for a
600 * cached data file. The validity has already been
601 * verified. */
602 xfree (client->crc);
603 client->crc = xmalloc (crclen);
604 memcpy (client->crc, crc, crclen);
605 xfree (cdata->crc);
606 cdata->crc = xmalloc (crclen);
607 memcpy (cdata->crc, crc, crclen);
609 rc = cache_set_data (client->md5file, cdata);
610 /* The cache entry may have been removed and cache_set_data()
611 * already sent STATUS_CACHE. */
612 if (!cache_iscached (client->filename, NULL, 0, 0))
613 rc = send_status (ctx, STATUS_CACHE, NULL);
615 else
616 rc = cache_add_file (client->md5file, cdata, timeout);
621 pthread_cleanup_pop (1);
622 xfree (crc);
624 if (!rc && !(client->flags & FLAG_NEW) && !cached)
625 rc = update_checksum (client);
627 if (!rc)
629 client->flags |= FLAG_OPEN;
631 if (client->flags & FLAG_NEW)
632 rc = send_status (ctx, STATUS_NEWFILE, NULL);
634 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
635 rc = do_lock (client, 0);
638 if (rc)
640 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
642 if (client->flags & FLAG_NEW)
643 cache_clear (client->md5file, 1);
645 crypto_free (client->crypto);
646 client->crypto = NULL;
647 client->flags &= ~FLAG_OPEN;
650 crypto_free_non_keys (client->crypto);
651 return send_error (ctx, rc);
654 /* If not the invoking_user or is an existing file, check that the list of user
655 * supplied key ID's are in the list of current key ID's obtained when
656 * decrypting the data file. Both short and long form keyid fingerprints are
657 * valid:
659 * 8 byte with optional "0x" prefix
660 * 16 byte
661 * 40 byte
663 * Although, the key ID's are converted to the 8 byte form before calling this
664 * function in parse_save_opt_keyid_common() to keep consistancy with KEYINFO
665 * output.
667 static gpg_error_t
668 permitted_to_save (struct client_s *client, const char **keys,
669 const char **value)
671 gpg_error_t rc = 0;
672 const char **v;
674 if ((client->flags & FLAG_NEW) || (client->opts & OPT_SYMMETRIC))
675 return 0;
677 rc = peer_is_invoker (client);
678 if (!rc)
679 return 0;
681 /* Empty match. */
682 if ((!value && !keys) || ((value && !*value) && (keys && !*keys)))
683 return 0;
685 for (v = value; v && *v; v++)
687 const char **k, *pv = *v;
688 int match = 0;
690 if (*pv == '0' && *(pv+1) == 'x')
691 pv += 2;
693 for (k = keys; k && *k; k++)
695 const char *pk = *k;
697 if (*pk == '0' && *(pk+1) == 'x')
698 pk += 2;
700 rc = !strcmp (pv, pk) ? 0 : GPG_ERR_FORBIDDEN;
701 if (rc)
702 rc = !strcmp (pv, *k) ? 0 : GPG_ERR_FORBIDDEN;
704 if (!rc)
706 match = 1;
707 break;
711 if (!match)
712 return GPG_ERR_FORBIDDEN;
715 return rc;
718 static gpg_error_t
719 parse_save_opt_keyid_common (struct client_s *client, const char **list,
720 const char *value, char ***dst)
722 gpg_error_t rc;
723 char **p = NULL;
725 if (value && *value)
727 p = str_split (value, ",", 0);
728 if (!p)
729 return GPG_ERR_ENOMEM;
732 crypto_keyid_to_16b (p);
733 rc = permitted_to_save (client, list, (const char **)p);
734 if (!rc)
735 *dst = p;
736 else
737 strv_free (*dst);
739 return rc;
742 static gpg_error_t
743 parse_save_opt_keyid (void *data, void *value)
745 struct client_s *client = data;
746 const char *str = value;
747 char **dst = NULL;
748 gpg_error_t rc;
750 rc = parse_save_opt_keyid_common (client,
751 (const char **)client->crypto->pubkey,
752 str, &dst);
753 if (rc)
754 return rc;
756 client->crypto->save.pubkey = dst;
757 return 0;
760 static gpg_error_t
761 parse_save_opt_sign_keyid (void *data, void *value)
763 struct client_s *client = data;
764 const char *str = value;
765 char **dst = NULL;
766 gpg_error_t rc;
768 rc = parse_save_opt_keyid_common (client,
769 (const char **)client->crypto->sigkey,
770 str, &dst);
771 if (rc)
772 return rc;
774 if (!dst)
775 client->opts |= OPT_NO_SIGNER;
777 client->crypto->save.sigkey = dst;
778 return 0;
781 static gpg_error_t
782 parse_save_opt_inquire (struct client_s *client, uint32_t opt)
784 if (opt == OPT_INQUIRE && !(client->flags & FLAG_NEW))
786 gpg_error_t rc = peer_is_invoker (client);
788 if (rc)
789 return rc;
792 client->opts |= opt;
793 return 0;
796 static gpg_error_t
797 parse_save_opt_inquire_keyparam (void *data, void *value)
799 return parse_save_opt_inquire (data, OPT_INQUIRE);
802 static gpg_error_t
803 parse_save_opt_inquire_keyid (void *data, void *value)
805 return parse_save_opt_inquire (data, OPT_INQUIRE_KEYID);
808 static gpg_error_t
809 parse_save_opt_inquire_sign_keyid (void *data, void *value)
811 return parse_save_opt_inquire (data, OPT_INQUIRE_SIGN_KEYID);
814 static gpg_error_t
815 parse_save_opt_symmetric (void *data, void *value)
817 struct client_s *client = data;
819 client->opts |= OPT_SYMMETRIC;
820 return 0;
823 /* Tests that the keys in new_keys are also in old_keys. */
824 static gpg_error_t
825 compare_keys (char **new_keys, char **old_keys)
827 char **o;
829 if (!old_keys || !*old_keys)
830 return 0;
832 crypto_keyid_to_16b (new_keys);
834 for (o = old_keys; *o; o++)
836 char **n;
838 for (n = new_keys; *n; n++)
840 if (!strcmp (*n, *o))
841 break;
844 if (!*n)
845 return GPG_ERR_NOT_FOUND;
848 return 0;
851 static gpg_error_t
852 inquire_keyid (struct client_s *client, uint32_t opt)
854 gpg_error_t rc;
855 unsigned char *result = NULL;
856 size_t len;
857 char *s;
858 char ***orig;
859 char ***save;
861 if (opt == OPT_INQUIRE_KEYID)
863 s = "INQUIRE_KEYID";
864 orig = &client->crypto->pubkey;
865 save = &client->crypto->save.pubkey;
867 else
869 s = "INQUIRE_SIGN_KEYID";
870 orig = &client->crypto->sigkey;
871 save = &client->crypto->save.sigkey;
874 rc = assuan_inquire (client->ctx, s, &result, &len, 0);
875 if (!rc)
877 char **dst = NULL;
879 rc = parse_save_opt_keyid_common (client, (const char **)*orig,
880 (char *)result, &dst);
881 if (!rc)
883 if (!dst && opt == OPT_INQUIRE_SIGN_KEYID
884 && client->opts & OPT_SYMMETRIC)
885 client->opts |= OPT_NO_SIGNER;
887 *save = dst;
891 xfree (result);
892 return rc;
895 /* The caching test of gpg-agent is both a blessing and a curse. When a key
896 * lookup in its cache fails it tries the last successful key to be unlocked.
897 * This prevents pwmd from successfully clearing a signing key since the
898 * previous key, which more than likely belongs to the same keypair as the
899 * encryption/decryption key, will have the passphrase cached and therefore the
900 * signing key also cached. So we need to clear both the signing and encryption
901 * keys to get the effect of requiring a passphrase when generating a new
902 * keypair for an existing data file, or when the "require_save_key"
903 * configuration parameter is set. The "require_save_key" parameter is mostly a
904 * failsafe of the gpg-agent option --ignore-cache-for-signing since
905 * some/most/all users may fail to set it.
907 static gpg_error_t
908 save_command (assuan_context_t ctx, char *line)
910 struct client_s *client = assuan_get_pointer (ctx);
911 struct cache_data_s *cdata = NULL;
912 gpg_error_t rc = 0, cached = 0;
913 int defer = 0;
914 struct stat st;
915 struct argv_s *args[] = {
916 &(struct argv_s) {"keyid", OPTION_TYPE_ARG, parse_save_opt_keyid},
917 &(struct argv_s) {"sign-keyid", OPTION_TYPE_OPTARG,
918 parse_save_opt_sign_keyid},
919 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
920 parse_save_opt_inquire_keyparam },
921 &(struct argv_s) {"inquire-keyid", OPTION_TYPE_NOARG,
922 parse_save_opt_inquire_keyid },
923 &(struct argv_s) {"inquire-sign-keyid", OPTION_TYPE_NOARG,
924 parse_save_opt_inquire_sign_keyid },
925 &(struct argv_s) {"symmetric", OPTION_TYPE_NOARG,
926 parse_save_opt_symmetric },
927 NULL
930 crypto_free_save (&client->crypto->save);
932 if (!(client->flags & FLAG_NEW))
934 rc = crypto_is_symmetric (client->filename);
935 if (!rc || rc == GPG_ERR_BAD_DATA)
937 if (!rc)
938 client->opts |= OPT_SYMMETRIC;
940 rc = 0; // PKI
944 if (rc)
945 return send_error (ctx, rc);
947 rc = parse_options (&line, args, client, 0);
948 if (rc)
949 return send_error (ctx, rc);
951 if (config_get_boolean (client->filename, "require_save_key"))
952 client->opts |= OPT_ASK;
954 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
955 return send_error (ctx, gpg_error_from_errno (errno));
957 if (errno != ENOENT && !S_ISREG (st.st_mode))
959 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
960 return send_error (ctx, GPG_ERR_ENOANO);
963 if (!rc)
964 cached = rc = cache_iscached (client->filename, &defer, 0, 1);
966 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
967 rc = 0;
968 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
969 rc = 0;
971 if (rc)
972 return send_error (ctx, rc);
974 /* Specifying both a recipient and symmetric encryption is an error. */
975 if (client->opts & OPT_INQUIRE_KEYID && client->opts & OPT_SYMMETRIC)
977 return send_error (ctx, GPG_ERR_CONFLICT);
979 else if (client->opts & OPT_INQUIRE && client->opts & OPT_SYMMETRIC)
981 return send_error (ctx, GPG_ERR_CONFLICT);
983 /* Existing file with a recipient and wanting symmetric is an error. */
984 else if (client->opts & OPT_SYMMETRIC && client->crypto->save.pubkey)
986 return send_error (ctx, GPG_ERR_CONFLICT);
988 else if (((client->opts & OPT_INQUIRE_KEYID)
989 || (client->opts & OPT_INQUIRE_SIGN_KEYID)))
991 if (client->opts & OPT_INQUIRE)
992 return send_error (ctx, GPG_ERR_CONFLICT);
994 if (client->opts & OPT_INQUIRE_KEYID)
995 rc = inquire_keyid (client, OPT_INQUIRE_KEYID);
997 if (!rc && (client->opts & OPT_INQUIRE_SIGN_KEYID))
998 rc = inquire_keyid (client, OPT_INQUIRE_SIGN_KEYID);
1000 else if ((client->crypto->save.pubkey || client->crypto->save.sigkey)
1001 && client->opts & OPT_INQUIRE)
1002 return send_error (ctx, GPG_ERR_CONFLICT);
1004 if (!rc)
1005 rc = crypto_init_ctx (client->crypto, client->flags & FLAG_NO_PINENTRY);
1007 if (!rc && (client->opts & OPT_INQUIRE))
1009 /* Require a passphrase when generating a new key pair for an existing
1010 * file.
1012 if (!(client->flags & FLAG_NEW))
1014 rc = cache_clear_agent_keys (client->md5file, 1, 1);
1015 if (!rc)
1017 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
1018 client->opts &= ~OPT_ASK;
1022 if (!rc)
1024 unsigned char *params = NULL;
1025 size_t len;
1027 rc = assuan_inquire (client->ctx, "KEYPARAM", &params, &len, 0);
1028 if (!rc)
1030 client->crypto->flags |= CRYPTO_FLAG_NEWFILE;
1031 pthread_cleanup_push ((void *)xfree, params);
1032 rc = crypto_genkey (client, client->crypto, params);
1033 pthread_cleanup_pop (1);
1037 else if (!rc && (client->flags & FLAG_NEW))
1039 if (!client->crypto->save.pubkey && !client->crypto->save.sigkey
1040 && !(client->opts & OPT_SYMMETRIC))
1042 char *params = crypto_default_key_params ();
1044 if (!params)
1045 rc = GPG_ERR_ENOMEM;
1047 if (!rc)
1049 client->crypto->flags |= CRYPTO_FLAG_NEWFILE;
1050 pthread_cleanup_push ((void *)xfree, params);
1051 rc = crypto_genkey (client, client->crypto,
1052 (unsigned char *)params);
1053 pthread_cleanup_pop (1);
1056 else
1058 if (!client->crypto->save.pubkey && client->crypto->save.sigkey
1059 && !(client->opts & OPT_SYMMETRIC))
1060 client->crypto->save.pubkey = strv_dup (client->crypto->save.sigkey);
1062 if (client->crypto->save.pubkey && !client->crypto->save.sigkey)
1063 client->crypto->save.sigkey = strv_dup (client->crypto->save.pubkey);
1066 else if (!rc)
1068 cdata = cache_get_data (client->md5file);
1069 if (cdata)
1071 if (client->crypto->save.pubkey)
1072 rc = compare_keys (client->crypto->save.pubkey, cdata->pubkey);
1074 /* Always allow a signer for symmetric data files. */
1075 if (!rc && client->crypto->save.sigkey
1076 && !(client->opts & OPT_SYMMETRIC))
1077 rc = compare_keys (client->crypto->save.sigkey, cdata->sigkey);
1079 /* Prevent saving to a recipient who is not in the original recipient
1080 * list without a passphrase. */
1081 if (rc || (client->crypto->sigkey && client->opts & OPT_NO_SIGNER))
1082 client->opts |= OPT_ASK;
1084 if (rc == GPG_ERR_NOT_FOUND)
1086 rc = peer_is_invoker (client);
1087 if (rc == GPG_ERR_EACCES)
1088 rc = GPG_ERR_FORBIDDEN;
1091 if (!client->crypto->save.pubkey
1092 && !(client->opts & OPT_SYMMETRIC))
1093 client->crypto->save.pubkey = strv_dup (cdata->pubkey);
1095 if (!client->crypto->save.sigkey && cdata->sigkey
1096 && !(client->opts & OPT_NO_SIGNER))
1097 client->crypto->save.sigkey = strv_dup (cdata->sigkey);
1098 else if (!client->crypto->save.sigkey
1099 && (client->opts & OPT_NO_SIGNER)
1100 && !(client->opts & OPT_SYMMETRIC))
1101 rc = GPG_ERR_NO_SIGNATURE_SCHEME;
1103 else
1105 client->crypto->save.pubkey = strv_dup (client->crypto->pubkey);
1106 client->crypto->save.sigkey = strv_dup (client->crypto->sigkey);
1110 if (!rc && !(client->flags & FLAG_NEW))
1112 /* Fixes {NEW_,SIGN_}PASSPHRASE inquire keywords. See passphrase_cb(). */
1113 if (client->opts & OPT_SYMMETRIC)
1114 client->crypto->flags |= CRYPTO_FLAG_PASSWD;
1116 if (client->opts & OPT_ASK)
1118 rc = cache_clear_agent_keys (client->md5file, 1, 1);
1119 if (!rc)
1120 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
1124 if (!rc && client->opts & OPT_SYMMETRIC)
1125 client->crypto->flags |= CRYPTO_FLAG_PASSWD_NEW;
1127 if (!rc)
1128 rc = xml_update_element_mtime (client, xmlDocGetRootElement (client->doc));
1130 if (!rc)
1132 int size;
1134 xmlDocDumpFormatMemory (client->doc, &client->crypto->plaintext, &size,
1136 if (size > 0)
1137 client->crypto->plaintext_size = (size_t) size;
1138 else
1139 rc = GPG_ERR_ENOMEM;
1141 if (!rc)
1143 client->crypto->flags |= (client->opts & OPT_SYMMETRIC) ? CRYPTO_FLAG_SYMMETRIC : 0;
1144 client->crypto->flags |= (client->flags & FLAG_NEW) ? CRYPTO_FLAG_NEWFILE : 0;
1145 rc = crypto_encrypt (client, client->crypto);
1146 client->crypto->flags &= ~CRYPTO_FLAG_SYMMETRIC;
1149 if (!rc)
1151 rc = crypto_write_file (client->crypto);
1152 if (!rc)
1154 if (!cached) // no error
1155 cdata = cache_get_data (client->md5file);
1158 if (!rc)
1160 rc = cache_encrypt (client->crypto);
1161 if (rc)
1163 cache_clear (client->md5file, 1);
1164 strv_free (client->crypto->pubkey);
1165 client->crypto->pubkey = strv_dup (client->crypto->save.pubkey);
1166 strv_free (client->crypto->sigkey);
1167 client->crypto->sigkey = strv_dup (client->crypto->save.sigkey);
1168 client->flags &= ~(FLAG_NEW);
1172 if (!rc)
1174 int timeout = config_get_integer (client->filename,
1175 "cache_timeout");
1177 free_cache_data_once (cdata);
1178 cdata = xcalloc (1, sizeof (struct cache_data_s));
1179 cdata->doc = client->crypto->plaintext;
1180 client->crypto->plaintext = NULL;
1181 cdata->size = client->crypto->plaintext_size;
1182 client->crypto->plaintext_size = 0;
1183 cdata->pubkey = client->crypto->save.pubkey;
1184 client->crypto->save.pubkey = NULL;
1185 cdata->sigkey = client->crypto->save.sigkey;
1186 client->crypto->save.sigkey = NULL;
1188 /* Update in case the cache entry expires the next SAVE may not
1189 * have any known keys. */
1190 strv_free (client->crypto->pubkey);
1191 client->crypto->pubkey = strv_dup (cdata->pubkey);
1192 strv_free (client->crypto->sigkey);
1193 client->crypto->sigkey = strv_dup (cdata->sigkey);
1195 if (!cached) // no error and wont increment refcount
1196 rc = cache_set_data (client->md5file, cdata);
1197 else
1198 rc = cache_add_file (client->md5file, cdata, timeout);
1200 if (!rc && (cached || (client->flags & FLAG_NEW)))
1201 send_status_all (STATUS_CACHE, NULL);
1203 if (!rc)
1205 rc = update_checksum (client);
1206 client->flags &= ~(FLAG_NEW);
1212 crypto_free_non_keys (client->crypto);
1213 return send_error (ctx, rc);
1216 static gpg_error_t
1217 do_delete (assuan_context_t ctx, char *line)
1219 struct client_s *client = assuan_get_pointer (ctx);
1220 struct xml_request_s *req;
1221 xmlNodePtr n;
1222 gpg_error_t rc;
1224 rc = xml_new_request (client, line, XML_CMD_DELETE, &req);
1225 if (rc)
1226 return rc;
1228 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1229 xml_free_request (req);
1230 if (rc)
1231 return rc;
1233 rc = xml_is_element_owner (client, n);
1234 if (!rc)
1235 rc = xml_unlink_node (client, n);
1237 return rc;
1240 static gpg_error_t
1241 delete_command (assuan_context_t ctx, char *line)
1243 struct client_s *client = assuan_get_pointer (ctx);
1244 gpg_error_t rc;
1245 struct argv_s *args[] = {
1246 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1247 NULL
1250 rc = parse_options (&line, args, client, 1);
1251 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1252 rc = GPG_ERR_SYNTAX;
1253 if (rc)
1254 return send_error (ctx, rc);
1256 if (client->opts & OPT_INQUIRE)
1258 unsigned char *result;
1259 size_t len;
1261 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1262 if (rc)
1263 return send_error (ctx, rc);
1265 pthread_cleanup_push ((void *)xfree, result);
1266 rc = do_delete (ctx, (char *)result);
1267 pthread_cleanup_pop (1);
1269 else
1270 rc = do_delete (ctx, line);
1272 return send_error (ctx, rc);
1275 static gpg_error_t
1276 update_element_expirey (struct client_s *client, xmlNodePtr n)
1278 gpg_error_t rc = 0;
1279 xmlChar *expire, *incr;
1280 char *p;
1281 time_t e, e_orig, i;
1282 time_t now = time (NULL);
1284 expire = xml_attribute_value (n, (xmlChar *)"expire");
1285 if (!expire)
1286 return 0;
1288 errno = 0;
1289 e = strtoul ((char *)expire, &p, 10);
1290 if (errno || *p || e == ULONG_MAX || *expire == '-')
1292 xmlFree (expire);
1293 rc = GPG_ERR_ERANGE;
1294 goto fail;
1297 xmlFree (expire);
1298 incr = xml_attribute_value (n, (xmlChar *)"expire_increment");
1299 if (!incr)
1300 return 0;
1302 i = strtoul ((char *)incr, &p, 10);
1303 if (errno || *p || i == ULONG_MAX || *incr == '-')
1305 xmlFree (incr);
1306 rc = GPG_ERR_ERANGE;
1307 goto fail;
1310 xmlFree (incr);
1311 e_orig = e;
1312 e = now + i;
1313 p = str_asprintf ("%lu", e);
1314 if (!p)
1316 rc = GPG_ERR_ENOMEM;
1317 goto fail;
1320 rc = xml_add_attribute (client, n, "expire", p);
1321 xfree (p);
1322 if (!rc)
1323 rc = send_status (client->ctx, STATUS_EXPIRE, "%lu %lu", e_orig, e);
1325 fail:
1326 return rc;
1329 static gpg_error_t
1330 store_command (assuan_context_t ctx, char *line)
1332 struct client_s *client = assuan_get_pointer (ctx);
1333 gpg_error_t rc;
1334 size_t len;
1335 unsigned char *result;
1336 xmlNodePtr n, parent;
1337 int has_content;
1338 char *content = NULL;
1339 struct xml_request_s *req;
1341 if (line && *line)
1342 return send_error (ctx, GPG_ERR_SYNTAX);
1344 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1345 if (rc)
1346 return send_error (ctx, rc);
1348 rc = xml_new_request (client, (char *)result, XML_CMD_STORE, &req);
1349 xfree (result);
1350 if (rc)
1351 return send_error (ctx, rc);
1353 /* Prevent passing the element content around to save some memory. */
1354 len = strv_length (req->args);
1355 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1356 if (*(req->args+1) && !xml_valid_element_path (req->args, has_content))
1358 xml_free_request (req);
1359 return send_error (ctx, GPG_ERR_INV_VALUE);
1362 if (has_content || !*req->args[len-1])
1364 has_content = 1;
1365 content = req->args[len-1];
1366 req->args[len-1] = NULL;
1369 if (strv_length (req->args) > 1)
1371 rc = xml_check_recursion (client, req);
1372 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
1373 goto fail;
1376 parent = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1378 if (!rc && len > 1)
1380 rc = xml_is_element_owner (client, parent);
1381 if (!rc)
1383 n = xml_find_text_node (parent->children);
1384 if (n)
1385 xmlNodeSetContent (n, (xmlChar *) content);
1386 else
1387 xmlNodeAddContent (parent, (xmlChar *) content);
1389 (void)xml_update_element_mtime (client, parent);
1390 (void)update_element_expirey (client, parent);
1394 fail:
1395 xfree (content);
1396 xml_free_request (req);
1397 return send_error (ctx, rc);
1400 static gpg_error_t
1401 xfer_data (assuan_context_t ctx, const char *line, int total)
1403 struct client_s *client = assuan_get_pointer (ctx);
1404 int to_send;
1405 int sent = 0;
1406 gpg_error_t rc = 0;
1407 int progress = config_get_integer ("global", "xfer_progress");
1408 int flush = 0;
1410 if (!(client->flags & FLAG_LOCK_CMD))
1411 rc = unlock_file_mutex (client, 0);
1412 if (rc && rc != GPG_ERR_NOT_LOCKED)
1413 return rc;
1415 progress =
1416 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1417 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1418 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1420 if (rc)
1421 return rc;
1423 again:
1426 if (sent + to_send > total)
1427 to_send = total - sent;
1429 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1430 flush ? 0 : to_send);
1431 if (!rc)
1433 sent += flush ? 0 : to_send;
1435 if ((progress && !(sent % progress) && sent != total) ||
1436 (sent == total && flush))
1437 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1439 if (!flush && !rc && sent == total)
1441 flush = 1;
1442 goto again;
1446 while (!rc && sent < total);
1448 return rc;
1451 static gpg_error_t
1452 do_get (assuan_context_t ctx, char *line)
1454 struct client_s *client = assuan_get_pointer (ctx);
1455 gpg_error_t rc;
1456 struct xml_request_s *req;
1457 xmlNodePtr n;
1459 rc = xml_new_request (client, line, XML_CMD_NONE, &req);
1460 if (rc)
1461 return rc;
1463 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1464 if (!rc)
1466 if (n && n->children)
1468 xmlNodePtr tmp = n;
1470 n = xml_find_text_node (n->children);
1471 if (!n || !n->content || !*n->content)
1472 rc = GPG_ERR_NO_DATA;
1473 else
1475 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1476 if (!rc)
1478 xmlChar *expire = xml_attribute_value (tmp,
1479 (xmlChar *)"expire");
1480 if (expire)
1482 time_t now = time (NULL);
1483 time_t e;
1484 char *p;
1486 errno = 0;
1487 e = strtoul ((char *)expire, &p, 10);
1488 if (errno || *p || e == ULONG_MAX || *expire == '-')
1489 log_write (_("invalid expire attribute value: %s"),
1490 expire);
1491 else if (now >= e)
1492 rc = send_status (ctx, STATUS_EXPIRE, "%lu 0", e);
1494 xmlFree (expire);
1499 else
1500 rc = GPG_ERR_NO_DATA;
1503 xml_free_request (req);
1504 return rc;
1507 static gpg_error_t
1508 get_command (assuan_context_t ctx, char *line)
1510 struct client_s *client = assuan_get_pointer (ctx);
1511 gpg_error_t rc;
1512 struct argv_s *args[] = {
1513 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1514 NULL
1517 rc = parse_options (&line, args, client, 1);
1518 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1519 rc = GPG_ERR_SYNTAX;
1520 if (rc)
1521 return send_error (ctx, rc);
1523 if (client->opts & OPT_INQUIRE)
1525 unsigned char *result;
1526 size_t len;
1528 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1529 if (rc)
1530 return send_error (ctx, rc);
1532 pthread_cleanup_push ((void *)xfree, result);
1533 rc = do_get (ctx, (char *)result);
1534 pthread_cleanup_pop (1);
1536 else
1537 rc = do_get (ctx, line);
1539 return send_error (ctx, rc);
1542 static void list_command_cleanup1 (void *arg);
1543 static gpg_error_t
1544 realpath_command (assuan_context_t ctx, char *line)
1546 gpg_error_t rc;
1547 char **realpath = NULL;
1548 struct client_s *client = assuan_get_pointer (ctx);
1549 struct argv_s *args[] = {
1550 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1551 NULL
1554 rc = parse_options (&line, args, client, 1);
1555 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1556 rc = GPG_ERR_SYNTAX;
1557 if (rc)
1558 return send_error (ctx, rc);
1560 if (client->opts & OPT_INQUIRE)
1562 unsigned char *result;
1563 size_t len;
1565 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1566 if (rc)
1567 return send_error (ctx, rc);
1569 pthread_cleanup_push ((void *)xfree, result);
1570 (void)xml_resolve_path (client, client->doc, result, &realpath, &rc);
1571 pthread_cleanup_pop (1);
1573 else
1574 (void)xml_resolve_path (client, client->doc, (unsigned char *)line,
1575 &realpath, &rc);
1577 if (!rc)
1579 char *tmp = strv_join ("\t", realpath);
1581 strv_free (realpath);
1582 if (!tmp)
1583 return send_error (ctx, GPG_ERR_ENOMEM);
1585 pthread_cleanup_push ((void *)xfree, tmp);
1586 rc = xfer_data (ctx, tmp, strlen (tmp));
1587 pthread_cleanup_pop (1);
1590 return send_error (ctx, rc);
1593 static void
1594 list_command_cleanup1 (void *arg)
1596 if (arg)
1597 string_free ((struct string_s *) arg, 1);
1600 static gpg_error_t
1601 parse_list_opt_recurse (void *data, void *value)
1603 struct client_s *client = data;
1605 client->opts |= OPT_LIST_RECURSE;
1606 return 0;
1609 static gpg_error_t
1610 parse_opt_verbose (void *data, void *value)
1612 struct client_s *client = data;
1614 client->opts |= OPT_VERBOSE;
1615 return 0;
1618 static gpg_error_t
1619 list_path_once (struct client_s *client, char *line,
1620 struct element_list_s *elements, struct string_s *result)
1622 gpg_error_t rc;
1624 rc = xml_create_path_list (client, client->doc, NULL, elements, line, 0);
1625 if (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES)
1626 rc = 0;
1628 if (!rc)
1630 int total = slist_length (elements->list);
1632 if (total)
1634 int i;
1636 for (i = 0; i < total; i++)
1638 char *tmp = slist_nth_data (elements->list, i);
1640 string_append_printf (result, "%s%s", tmp,
1641 i + 1 == total ? "" : "\n");
1644 else
1645 rc = GPG_ERR_NO_DATA;
1648 return rc;
1651 static gpg_error_t
1652 do_list (assuan_context_t ctx, char *line)
1654 struct client_s *client = assuan_get_pointer (ctx);
1655 gpg_error_t rc = GPG_ERR_NO_DATA;
1656 struct element_list_s *elements = NULL;
1658 if (!line || !*line)
1660 struct string_s *str = string_new (NULL);
1661 xmlNodePtr n;
1663 if (!str)
1664 return GPG_ERR_ENOMEM;
1666 pthread_cleanup_push ((void *)list_command_cleanup1, str);
1667 n = xmlDocGetRootElement (client->doc);
1668 for (n = n->children; n; n = n->next)
1670 xmlChar *name;
1672 if (n->type != XML_ELEMENT_NODE)
1673 continue;
1675 name = xmlGetProp (n, (xmlChar *)"_name");
1676 if (!name)
1678 rc = GPG_ERR_ENOMEM;
1679 break;
1682 elements = xcalloc (1, sizeof (struct element_list_s));
1683 if (!elements)
1685 xmlFree (name);
1686 rc = GPG_ERR_ENOMEM;
1687 break;
1690 elements->prefix = string_new (NULL);
1691 if (!elements->prefix)
1693 xmlFree (name);
1694 xml_free_element_list (elements);
1695 rc = GPG_ERR_ENOMEM;
1696 break;
1699 elements->recurse = client->opts & OPT_LIST_RECURSE;
1700 elements->root_only = !elements->recurse;
1701 pthread_cleanup_push ((void *)xml_free_element_list, elements);
1702 rc = list_path_once (client, (char *)name, elements, str);
1703 xmlFree (name);
1704 pthread_cleanup_pop (1);
1705 if (rc)
1706 break;
1708 if (n->next)
1709 string_append (str, "\n");
1712 if (!rc)
1713 rc = xfer_data (ctx, str->str, str->len);
1715 pthread_cleanup_pop (1);
1716 return rc;
1719 elements = xcalloc (1, sizeof (struct element_list_s));
1720 if (!elements)
1721 return GPG_ERR_ENOMEM;
1723 elements->prefix = string_new (NULL);
1724 if (!elements->prefix)
1726 xml_free_element_list (elements);
1727 return GPG_ERR_ENOMEM;
1730 elements->recurse = client->opts & OPT_LIST_RECURSE;
1731 pthread_cleanup_push ((void *)xml_free_element_list, elements);
1732 struct string_s *str = string_new (NULL);
1733 pthread_cleanup_push ((void *)list_command_cleanup1, str);
1734 rc = list_path_once (client, line, elements, str);
1735 if (!rc)
1736 rc = xfer_data (ctx, str->str, str->len);
1738 pthread_cleanup_pop (1);
1739 pthread_cleanup_pop (1);
1740 return rc;
1743 static gpg_error_t
1744 list_command (assuan_context_t ctx, char *line)
1746 struct client_s *client = assuan_get_pointer (ctx);
1747 gpg_error_t rc;
1748 struct argv_s *args[] = {
1749 &(struct argv_s) {"recurse", OPTION_TYPE_NOARG, parse_list_opt_recurse},
1750 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1751 /* These are deprecated but kept for backward compatibility with older
1752 * clients. */
1753 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, NULL},
1754 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG, NULL},
1755 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_recurse},
1756 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG, NULL},
1757 NULL
1760 if (disable_list_and_dump == 1)
1761 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1763 rc = parse_options (&line, args, client, 1);
1764 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1765 rc = GPG_ERR_SYNTAX;
1766 if (rc)
1767 return send_error (ctx, rc);
1769 if (client->opts & OPT_INQUIRE)
1771 unsigned char *result;
1772 size_t len;
1774 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1775 if (rc)
1776 return send_error (ctx, rc);
1778 pthread_cleanup_push ((void *)xfree, result);
1779 rc = do_list (ctx, (char *)result);
1780 pthread_cleanup_pop (1);
1782 else
1783 rc = do_list (ctx, line);
1785 return send_error (ctx, rc);
1788 #define RESUMABLE_ERROR(rc) (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES \
1789 || rc == GPG_ERR_ELEMENT_NOT_FOUND || !rc)
1791 * args[0] - element path
1793 static gpg_error_t
1794 attribute_list (assuan_context_t ctx, char **args)
1796 struct client_s *client = assuan_get_pointer (ctx);
1797 char **attrlist = NULL;
1798 int i = 0;
1799 int target = 0;
1800 xmlAttrPtr a;
1801 xmlNodePtr n, an, r;
1802 char *line;
1803 gpg_error_t rc;
1804 struct xml_request_s *req;
1806 if (!args || !args[0] || !*args[0])
1807 return GPG_ERR_SYNTAX;
1809 rc = xml_new_request (client, args[0], XML_CMD_ATTR_LIST, &req);
1810 if (rc)
1811 return rc;
1813 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1814 xml_free_request (req);
1815 if (rc)
1816 return rc;
1818 again:
1819 for (a = n->properties; a; a = a->next)
1821 char **pa;
1823 if (target && xml_reserved_attribute ((char *)a->name))
1824 continue;
1826 pa = xrealloc (attrlist, (i + 2) * sizeof (char *));
1827 if (!pa)
1829 if (attrlist)
1830 strv_free (attrlist);
1832 log_write ("%s(%i): %s", __FILE__, __LINE__,
1833 pwmd_strerror (GPG_ERR_ENOMEM));
1834 return GPG_ERR_ENOMEM;
1837 attrlist = pa;
1838 an = a->children;
1839 attrlist[i] = str_asprintf ("%s %s", (char *) a->name, an && an->content
1840 ? (char *)an->content : "");
1842 if (!attrlist[i])
1844 strv_free (attrlist);
1845 log_write ("%s(%i): %s", __FILE__, __LINE__,
1846 pwmd_strerror (GPG_ERR_ENOMEM));
1847 return GPG_ERR_ENOMEM;
1850 attrlist[++i] = NULL;
1853 if (!attrlist)
1854 return GPG_ERR_NO_DATA;
1856 if (!target)
1858 r = xml_resolve_path (client, client->doc, (xmlChar *)args[0], NULL, &rc);
1859 if (!RESUMABLE_ERROR (rc))
1861 strv_free (attrlist);
1862 return rc;
1864 else if (!rc && r != n)
1866 target = 1;
1867 n = r;
1868 goto again;
1871 rc = 0;
1874 line = strv_join ("\n", attrlist);
1875 if (!line)
1877 log_write ("%s(%i): %s", __FILE__, __LINE__,
1878 pwmd_strerror (GPG_ERR_ENOMEM));
1879 strv_free (attrlist);
1880 return GPG_ERR_ENOMEM;
1883 pthread_cleanup_push ((void *)xfree, line);
1884 pthread_cleanup_push ((void *)req_cleanup, attrlist);
1885 rc = xfer_data (ctx, line, strlen (line));
1886 pthread_cleanup_pop (1);
1887 pthread_cleanup_pop (1);
1888 return rc;
1892 * args[0] - attribute
1893 * args[1] - element path
1895 static gpg_error_t
1896 attribute_delete (struct client_s *client, char **args)
1898 gpg_error_t rc;
1899 xmlNodePtr n;
1901 if (!args || !args[0] || !*args[0] || !args[1] || !*args[1])
1902 return GPG_ERR_SYNTAX;
1904 if (!strcmp (args[0], "_name") || !strcmp (args[0], "target"))
1905 return GPG_ERR_INV_ATTR;
1907 if (!xml_reserved_attribute (args[0]))
1908 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
1909 else
1911 struct xml_request_s *req;
1913 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
1914 if (rc)
1915 return rc;
1917 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1918 xml_free_request (req);
1921 if (!rc)
1922 rc = xml_is_element_owner (client, n);
1924 if (!rc)
1925 rc = xml_delete_attribute (client, n, (xmlChar *) args[0]);
1927 return rc;
1931 * Creates the "target" attribute. When other commands encounter an element
1932 * with this attribute they follow the "target" after appending remaining
1933 * elements from the original element path to the target. The exception is the
1934 * ATTR command that operates on the element itself (for reserved attribute
1935 * names) and not the target.
1937 * If the source element path doesn't exist when using 'ATTR SET target', it is
1938 * created, but the destination element path must exist. This is simliar to the
1939 * ls(1) command: ln -s src dst.
1941 * args[0] - source element path
1942 * args[1] - destination element path
1944 static gpg_error_t
1945 set_target_attribute (struct client_s *client, char **args)
1947 struct xml_request_s *req = NULL;
1948 char **src, **dst;
1949 gpg_error_t rc;
1950 xmlNodePtr n;
1952 if (!args || !args[0] || !args[1])
1953 return GPG_ERR_SYNTAX;
1955 src = str_split (args[0], "\t", 0);
1956 if (!src)
1957 return GPG_ERR_SYNTAX;
1959 if (!xml_valid_element_path (src, 0))
1961 strv_free (src);
1962 return GPG_ERR_INV_VALUE;
1965 dst = str_split (args[1], "\t", 0);
1966 if (!dst)
1968 strv_free (src);
1969 return GPG_ERR_SYNTAX;
1972 // Be sure the destination element path exists. */
1973 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
1974 if (rc)
1975 goto fail;
1977 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1978 if (rc && rc != GPG_ERR_EACCES)
1979 goto fail;
1981 xml_free_request (req);
1982 req = NULL;
1984 if (!strcmp (args[0], args[1]))
1986 rc = GPG_ERR_EEXIST;
1987 goto fail;
1990 rc = xml_new_request (client, args[0], XML_CMD_ATTR_TARGET, &req);
1991 if (rc)
1992 goto fail;
1994 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1995 if (rc && rc != GPG_ERR_EACCES)
1996 goto fail;
1998 if (!rc)
2000 rc = xml_add_attribute (client, n, "target", args[1]);
2001 if (!rc)
2002 rc = xml_unlink_node (client, n->children);
2005 fail:
2006 strv_free (src);
2007 strv_free (dst);
2008 xml_free_request (req);
2009 return rc;
2013 * args[0] - attribute
2014 * args[1] - element path
2016 static gpg_error_t
2017 attribute_get (assuan_context_t ctx, char **args)
2019 struct client_s *client = assuan_get_pointer (ctx);
2020 xmlNodePtr n;
2021 xmlChar *a;
2022 gpg_error_t rc;
2023 struct xml_request_s *req;
2025 if (!args || !args[0] || !*args[0] || !args[1] || !*args[1])
2026 return GPG_ERR_SYNTAX;
2028 if (!xml_reserved_attribute (args[0]))
2029 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
2030 else
2032 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
2033 if (rc)
2034 return rc;
2036 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2037 xml_free_request (req);
2040 if (rc)
2041 return rc;
2043 a = xmlGetProp (n, (xmlChar *) args[0]);
2044 if (!a)
2045 return GPG_ERR_NOT_FOUND;
2047 pthread_cleanup_push ((void *)xmlFree, a);
2049 if (*a)
2050 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
2051 else
2052 rc = GPG_ERR_NO_DATA;
2054 pthread_cleanup_pop (1);
2055 return rc;
2059 * args[0] - attribute
2060 * args[1] - element path
2061 * args[2] - value
2063 static gpg_error_t
2064 attribute_set (struct client_s *client, char **args)
2066 struct xml_request_s *req;
2067 gpg_error_t rc;
2068 xmlNodePtr n;
2070 if (!args || !args[0] || !args[1])
2071 return GPG_ERR_SYNTAX;
2074 * Reserved attribute names.
2076 if (!strcmp (args[0], "_name")) // Use the RENAME command instead
2077 return GPG_ERR_INV_ATTR;
2078 else if (!strcmp (args[0], "target"))
2079 return set_target_attribute (client, args + 1);
2080 else if (!xml_valid_attribute (args[0]))
2081 return GPG_ERR_INV_VALUE;
2083 if (!xml_valid_attribute_value (args[2]))
2084 return GPG_ERR_INV_VALUE;
2086 if (!xml_reserved_attribute (args[0]))
2088 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
2089 if (!rc)
2091 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
2092 if (!rc)
2094 rc = xml_check_recursion (client, req);
2095 xml_free_request (req);
2099 else
2101 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
2102 if (rc)
2103 return rc;
2105 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2106 xml_free_request (req);
2109 if (!rc)
2110 rc = xml_is_element_owner (client, n);
2112 if (!rc)
2113 rc = xml_add_attribute (client, n, args[0], args[2]);
2115 return rc;
2119 * req[0] - command
2120 * req[1] - attribute name or element path if command is LIST
2121 * req[2] - element path
2122 * req[2] - element path or value
2125 static gpg_error_t
2126 do_attr (assuan_context_t ctx, char *line)
2128 struct client_s *client = assuan_get_pointer (ctx);
2129 gpg_error_t rc = 0;
2130 char **req;
2132 req = str_split (line, " ", 4);
2133 if (!req || !req[0] || !req[1])
2135 strv_free (req);
2136 return GPG_ERR_SYNTAX;
2139 pthread_cleanup_push ((void *)req_cleanup, req);
2141 if (strcasecmp (req[0], "SET") == 0)
2142 rc = attribute_set (client, req + 1);
2143 else if (strcasecmp (req[0], "GET") == 0)
2144 rc = attribute_get (ctx, req + 1);
2145 else if (strcasecmp (req[0], "DELETE") == 0)
2146 rc = attribute_delete (client, req + 1);
2147 else if (strcasecmp (req[0], "LIST") == 0)
2148 rc = attribute_list (ctx, req + 1);
2149 else
2150 rc = GPG_ERR_SYNTAX;
2152 client->flags &= ~(FLAG_ACL_IGNORE|FLAG_ACL_ERROR);
2153 pthread_cleanup_pop (1);
2154 return rc;
2157 static gpg_error_t
2158 attr_command (assuan_context_t ctx, char *line)
2160 struct client_s *client = assuan_get_pointer (ctx);
2161 gpg_error_t rc;
2162 struct argv_s *args[] = {
2163 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2164 NULL
2167 rc = parse_options (&line, args, client, 1);
2168 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2169 rc = GPG_ERR_SYNTAX;
2170 if (rc)
2171 return send_error (ctx, rc);
2173 if (client->opts & OPT_INQUIRE)
2175 unsigned char *result;
2176 size_t len;
2178 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2179 if (rc)
2180 return send_error (ctx, rc);
2182 pthread_cleanup_push ((void *)xfree, result);
2183 rc = do_attr (ctx, (char *)result);
2184 pthread_cleanup_pop (1);
2186 else
2187 rc = do_attr (ctx, line);
2189 return send_error (ctx, rc);
2192 static gpg_error_t
2193 parse_iscached_opt_lock (void *data, void *value)
2195 struct client_s *client = data;
2197 (void) value;
2198 client->opts |= OPT_LOCK;
2199 return 0;
2202 static gpg_error_t
2203 parse_iscached_opt_agent (void *data, void *value)
2205 struct client_s *client = data;
2207 (void) value;
2208 client->opts |= OPT_CACHE_AGENT;
2209 return 0;
2212 static gpg_error_t
2213 parse_iscached_opt_sign (void *data, void *value)
2215 struct client_s *client = data;
2217 (void) value;
2218 client->opts |= OPT_CACHE_SIGN;
2219 return 0;
2222 // FIXME: read data file header to fetch keyid to test gpg-agent cache
2223 static gpg_error_t
2224 iscached_command (assuan_context_t ctx, char *line)
2226 struct client_s *client = assuan_get_pointer (ctx);
2227 gpg_error_t rc;
2228 struct argv_s *args[] = {
2229 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2230 &(struct argv_s) {"agent", OPTION_TYPE_NOARG, parse_iscached_opt_agent},
2231 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_iscached_opt_sign},
2232 NULL
2235 if (!line || !*line)
2236 return send_error (ctx, GPG_ERR_SYNTAX);
2238 rc = parse_options (&line, args, client, 1);
2239 if (rc)
2240 return send_error (ctx, rc);
2241 else if (!valid_filename (line))
2242 return send_error (ctx, GPG_ERR_INV_VALUE);
2244 rc = cache_iscached (line, NULL, (client->opts & OPT_CACHE_AGENT),
2245 (client->opts & OPT_CACHE_SIGN));
2246 if (client->opts & OPT_LOCK
2247 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2249 unsigned char md5file[16];
2250 gpg_error_t trc = rc;
2252 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2253 if (memcmp (md5file, client->md5file, 16))
2254 cleanup_client (client);
2256 memcpy (client->md5file, md5file, 16);
2257 rc = do_lock (client, 1);
2258 if (!rc)
2259 rc = trc;
2262 return send_error (ctx, rc);
2265 static gpg_error_t
2266 clearcache_command (assuan_context_t ctx, char *line)
2268 gpg_error_t rc = 0, all_rc = 0;
2269 unsigned char md5file[16];
2270 int i;
2271 int t;
2272 int all = 0;
2273 struct client_thread_s *once = NULL;
2275 cache_lock ();
2276 MUTEX_LOCK (&cn_mutex);
2277 pthread_cleanup_push ((void *)cleanup_mutex_cb, &cn_mutex);
2279 if (!line || !*line)
2280 all = 1;
2282 t = slist_length (cn_thread_list);
2284 for (i = 0; i < t; i++)
2286 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2287 assuan_peercred_t peer;
2289 if (!thd->cl)
2290 continue;
2292 /* Lock each connected clients' file mutex to prevent any other client
2293 * from accessing the cache entry (the file mutex is locked upon
2294 * command startup). The cache for the entry is not cleared if the
2295 * file mutex is locked by another client to prevent this function
2296 * from blocking.
2298 if (all)
2300 if (thd->cl->filename)
2302 rc = do_validate_peer (ctx, thd->cl->filename, &peer);
2303 all_rc = !all_rc ? rc : all_rc;
2304 if (rc)
2306 rc = 0;
2307 continue;
2311 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
2312 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2314 if (pthread_equal (pthread_self (), thd->tid))
2315 rc = 0;
2316 else
2318 if (!thd->cl->filename ||
2319 cache_iscached (thd->cl->filename, NULL, 0, 0) == GPG_ERR_NO_DATA)
2321 rc = 0;
2322 continue;
2325 cache_defer_clear (thd->cl->md5file);
2328 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2330 rc = 0;
2331 continue;
2334 if (!rc)
2336 rc = cache_clear (thd->cl->md5file, 1);
2337 cache_unlock_mutex (thd->cl->md5file, 0);
2340 if (rc)
2341 all_rc = rc;
2343 rc = 0;
2345 /* A single data filename was specified. Lock only this data file
2346 * mutex and free the cache entry. */
2347 else
2349 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2350 rc = do_validate_peer (ctx, line, &peer);
2352 if (!rc && !memcmp (thd->cl->md5file, md5file, sizeof (md5file)))
2354 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0,
2355 -1);
2356 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2358 if (pthread_equal (pthread_self (), thd->tid))
2359 rc = 0;
2362 if (!rc)
2364 once = thd;
2365 rc = cache_clear (thd->cl->md5file, 1);
2366 cache_unlock_mutex (thd->cl->md5file, 0);
2368 else
2370 cache_defer_clear (thd->cl->md5file);
2373 break;
2378 /* Only connected clients' cache entries have been cleared. Now clear any
2379 * remaining cache entries without clients but only if there wasn't an
2380 * error from above since this would defeat the locking check of the
2381 * remaining entries. */
2382 if (!all_rc && all)
2384 cache_clear (NULL, 1);
2387 /* No clients are using the specified file. */
2388 else if (!all_rc && !rc && !once)
2390 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2391 rc = cache_clear (md5file, 1);
2394 /* Release the connection mutex. */
2395 pthread_cleanup_pop (1);
2396 cache_unlock ();
2398 if (!rc)
2399 send_status_all (STATUS_CACHE, NULL);
2401 /* One or more files were locked while clearing all cache entries. */
2402 if (all_rc)
2403 rc = all_rc;
2405 return send_error (ctx, rc);
2408 static gpg_error_t
2409 cachetimeout_command (assuan_context_t ctx, char *line)
2411 int timeout;
2412 char **req = str_split (line, " ", 0);
2413 char *p;
2414 gpg_error_t rc = 0;
2415 assuan_peercred_t peer;
2417 if (!req || !*req || !req[1])
2419 strv_free (req);
2420 return send_error (ctx, GPG_ERR_SYNTAX);
2423 errno = 0;
2424 timeout = (int) strtol (req[1], &p, 10);
2425 if (errno != 0 || *p || timeout < -1)
2427 strv_free (req);
2428 return send_error (ctx, GPG_ERR_SYNTAX);
2431 rc = do_validate_peer (ctx, req[0], &peer);
2432 if (!rc)
2434 unsigned char md5file[16];
2436 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, req[0], strlen (req[0]));
2437 rc = cache_set_timeout (md5file, timeout);
2438 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2440 rc = 0;
2441 MUTEX_LOCK (&rcfile_mutex);
2442 config_set_int_param (&global_config, req[0], "cache_timeout",
2443 req[1]);
2444 MUTEX_UNLOCK (&rcfile_mutex);
2448 strv_free (req);
2449 return send_error (ctx, rc);
2452 static gpg_error_t
2453 dump_command (assuan_context_t ctx, char *line)
2455 xmlChar *xml;
2456 int len;
2457 struct client_s *client = assuan_get_pointer (ctx);
2458 gpg_error_t rc;
2460 if (disable_list_and_dump == 1)
2461 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2463 if (line && *line)
2464 return send_error (ctx, GPG_ERR_SYNTAX);
2466 rc = peer_is_invoker(client);
2467 if (rc)
2468 return send_error (ctx, rc);
2470 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2472 if (!xml)
2474 log_write ("%s(%i): %s", __FILE__, __LINE__,
2475 pwmd_strerror (GPG_ERR_ENOMEM));
2476 return send_error (ctx, GPG_ERR_ENOMEM);
2479 pthread_cleanup_push ((void *)xmlFree, xml);
2480 rc = xfer_data (ctx, (char *) xml, len);
2481 pthread_cleanup_pop (1);
2482 return send_error (ctx, rc);
2485 static gpg_error_t
2486 getconfig_command (assuan_context_t ctx, char *line)
2488 struct client_s *client = assuan_get_pointer (ctx);
2489 gpg_error_t rc = 0;
2490 char filename[255] = { 0 }, param[747] = { 0 };
2491 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2493 if (!line || !*line)
2494 return send_error (ctx, GPG_ERR_SYNTAX);
2496 if (strchr (line, ' '))
2498 sscanf (line, " %254[^ ] %746c", filename, param);
2499 paramp = param;
2500 fp = filename;
2503 if (fp && !valid_filename (fp))
2504 return send_error (ctx, GPG_ERR_INV_VALUE);
2506 paramp = str_down (paramp);
2507 if (!strcmp (paramp, "passphrase"))
2508 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2510 p = config_get_value (fp ? fp : "global", paramp);
2511 if (!p)
2512 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2514 tmp = expand_homedir (p);
2515 xfree (p);
2516 if (!tmp)
2518 log_write ("%s(%i): %s", __FILE__, __LINE__,
2519 pwmd_strerror (GPG_ERR_ENOMEM));
2520 return send_error (ctx, GPG_ERR_ENOMEM);
2523 p = tmp;
2524 pthread_cleanup_push ((void *)xfree, p);
2525 rc = xfer_data (ctx, p, strlen (p));
2526 pthread_cleanup_pop (1);
2527 return send_error (ctx, rc);
2530 struct xpath_s
2532 xmlXPathContextPtr xp;
2533 xmlXPathObjectPtr result;
2534 xmlBufferPtr buf;
2535 char **req;
2538 static void
2539 xpath_command_cleanup (void *arg)
2541 struct xpath_s *xpath = arg;
2543 if (!xpath)
2544 return;
2546 req_cleanup (xpath->req);
2548 if (xpath->buf)
2549 xmlBufferFree (xpath->buf);
2551 if (xpath->result)
2552 xmlXPathFreeObject (xpath->result);
2554 if (xpath->xp)
2555 xmlXPathFreeContext (xpath->xp);
2558 static gpg_error_t
2559 do_xpath (assuan_context_t ctx, char *line)
2561 gpg_error_t rc;
2562 struct client_s *client = assuan_get_pointer (ctx);
2563 struct xpath_s _x = { 0 };
2564 struct xpath_s *xpath = &_x;
2566 if (!line || !*line)
2567 return GPG_ERR_SYNTAX;
2569 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2571 if (strv_printf (&xpath->req, "%s", line) == 0)
2572 return GPG_ERR_ENOMEM;
2575 xpath->xp = xmlXPathNewContext (client->doc);
2576 if (!xpath->xp)
2578 rc = GPG_ERR_BAD_DATA;
2579 goto fail;
2582 xpath->result =
2583 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2584 if (!xpath->result)
2586 rc = GPG_ERR_BAD_DATA;
2587 goto fail;
2590 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2592 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2593 goto fail;
2596 rc = xml_recurse_xpath_nodeset (client, client->doc,
2597 xpath->result->nodesetval,
2598 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2599 NULL);
2600 if (rc)
2601 goto fail;
2602 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2604 rc = GPG_ERR_NO_DATA;
2605 goto fail;
2607 else if (xpath->req[1])
2609 rc = 0;
2610 goto fail;
2613 pthread_cleanup_push ((void *)xpath_command_cleanup, &xpath);
2614 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2615 xmlBufferLength (xpath->buf));
2616 pthread_cleanup_pop (0);
2617 fail:
2618 xpath_command_cleanup (xpath);
2619 return rc;
2622 static gpg_error_t
2623 xpath_command (assuan_context_t ctx, char *line)
2625 struct client_s *client = assuan_get_pointer (ctx);
2626 gpg_error_t rc;
2627 struct argv_s *args[] = {
2628 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2629 NULL
2632 if (disable_list_and_dump == 1)
2633 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2635 rc = peer_is_invoker(client);
2636 if (rc)
2637 return send_error (ctx, rc);
2639 rc = parse_options (&line, args, client, 1);
2640 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2641 rc = GPG_ERR_SYNTAX;
2642 if (rc)
2643 return send_error (ctx, rc);
2645 if (client->opts & OPT_INQUIRE)
2647 unsigned char *result;
2648 size_t len;
2650 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2651 if (rc)
2652 return send_error (ctx, rc);
2654 pthread_cleanup_push ((void *)xfree, result);
2655 rc = do_xpath (ctx, (char *)result);
2656 pthread_cleanup_pop (1);
2658 else
2659 rc = do_xpath (ctx, line);
2661 return send_error (ctx, rc);
2664 static gpg_error_t
2665 do_xpathattr (assuan_context_t ctx, char *line)
2667 struct client_s *client = assuan_get_pointer (ctx);
2668 gpg_error_t rc;
2669 char **req = NULL;
2670 int cmd = 0; //SET
2671 struct xpath_s _x = { 0 };
2672 struct xpath_s *xpath = &_x;
2674 if (!line || !*line)
2675 return GPG_ERR_SYNTAX;
2677 if ((req = str_split (line, " ", 3)) == NULL)
2678 return GPG_ERR_ENOMEM;
2680 if (!req[0])
2682 rc = GPG_ERR_SYNTAX;
2683 goto fail;
2686 if (!strcasecmp (req[0], "SET"))
2687 cmd = 0;
2688 else if (!strcasecmp (req[0], "DELETE"))
2689 cmd = 1;
2690 else
2692 rc = GPG_ERR_SYNTAX;
2693 goto fail;
2696 if (!req[1] || !req[2])
2698 rc = GPG_ERR_SYNTAX;
2699 goto fail;
2702 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2704 rc = GPG_ERR_ENOMEM;
2705 goto fail;
2708 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2710 rc = GPG_ERR_SYNTAX;
2711 goto fail;
2714 xpath->xp = xmlXPathNewContext (client->doc);
2715 if (!xpath->xp)
2717 rc = GPG_ERR_BAD_DATA;
2718 goto fail;
2721 xpath->result = xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2722 if (!xpath->result)
2724 rc = GPG_ERR_BAD_DATA;
2725 goto fail;
2728 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2730 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2731 goto fail;
2734 rc = xml_recurse_xpath_nodeset (client, client->doc,
2735 xpath->result->nodesetval,
2736 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2737 (xmlChar *) req[1]);
2739 fail:
2740 xpath_command_cleanup (xpath);
2741 strv_free (req);
2742 return rc;
2745 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2746 static gpg_error_t
2747 xpathattr_command (assuan_context_t ctx, char *line)
2749 struct client_s *client = assuan_get_pointer (ctx);
2750 gpg_error_t rc;
2751 struct argv_s *args[] = {
2752 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2753 NULL
2756 if (disable_list_and_dump == 1)
2757 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2759 rc = peer_is_invoker(client);
2760 if (rc)
2761 return send_error (ctx, rc);
2763 rc = parse_options (&line, args, client, 1);
2764 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2765 rc = GPG_ERR_SYNTAX;
2766 if (rc)
2767 return send_error (ctx, rc);
2769 if (client->opts & OPT_INQUIRE)
2771 unsigned char *result;
2772 size_t len;
2774 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2775 if (rc)
2776 return send_error (ctx, rc);
2778 pthread_cleanup_push ((void *)xfree, result);
2779 rc = do_xpathattr (ctx, (char *)result);
2780 pthread_cleanup_pop (1);
2782 else
2783 rc = do_xpathattr (ctx, line);
2785 return send_error (ctx, rc);
2788 static gpg_error_t
2789 do_import (struct client_s *client, const char *root_element,
2790 unsigned char *content)
2792 xmlDocPtr doc = NULL;
2793 xmlNodePtr n = NULL, root;
2794 gpg_error_t rc;
2795 struct string_s *str = NULL;
2796 struct xml_request_s *req = NULL;
2798 if (!content || !*content)
2799 return GPG_ERR_SYNTAX;
2801 if (root_element)
2803 rc = xml_new_request (client, root_element, XML_CMD_STORE, &req);
2804 if (rc)
2806 xfree (content);
2807 return rc;
2810 if (!xml_valid_element_path (req->args, 0))
2812 xml_free_request (req);
2813 xfree (content);
2814 return GPG_ERR_INV_VALUE;
2818 str = string_new_content ((char *)content);
2819 str = string_prepend (str, "<pwmd>");
2820 str = string_append (str, "</pwmd>");
2821 doc = xmlReadDoc ((xmlChar *) str->str, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2822 string_free (str, 1);
2823 if (!doc)
2825 rc = GPG_ERR_BAD_DATA;
2826 goto fail;
2829 root = xmlDocGetRootElement (doc);
2830 root = root->children;
2831 if (root->type != XML_ELEMENT_NODE)
2833 rc = GPG_ERR_BAD_DATA;
2834 goto fail;
2837 rc = xml_validate_import (client, root);
2838 if (rc)
2839 goto fail;
2841 if (req)
2843 /* Create the new specified root element path, if needed. */
2844 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc),
2845 &rc);
2846 if (rc)
2847 goto fail;
2849 /* Overwrite the children of the specified root element path. */
2850 (void)xml_unlink_node (client, n->children);
2851 n->children = NULL;
2853 else
2854 n = xmlDocGetRootElement (client->doc);
2856 if (n)
2858 xmlNodePtr copy;
2859 xmlChar *name = xml_attribute_value (root, (xmlChar *)"_name");
2861 if (!name)
2862 rc = GPG_ERR_BAD_DATA;
2863 else if (!req)
2865 /* No --root argument passed. Overwrite the current documents' root
2866 * element matching the root element of the imported data. */
2867 xml_free_request (req);
2868 req = NULL;
2869 rc = xml_new_request (client, (char *)name, XML_CMD_DELETE, &req);
2870 xmlFree (name);
2871 if (!rc)
2873 xmlNodePtr tmp;
2875 tmp = xml_find_elements (client, req,
2876 xmlDocGetRootElement (req->doc), &rc);
2877 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2878 goto fail;
2880 if (!rc)
2881 (void)xml_unlink_node (client, tmp);
2883 rc = 0;
2887 if (!rc)
2889 copy = xmlCopyNodeList (root);
2890 if (!copy)
2891 rc = GPG_ERR_ENOMEM;
2892 else
2894 n = xmlAddChildList (n, copy);
2895 if (!n)
2896 rc = GPG_ERR_ENOMEM;
2901 if (!rc && n && n->parent)
2902 rc = xml_update_element_mtime (client, n->parent);
2904 fail:
2905 xml_free_request (req);
2907 if (doc)
2908 xmlFreeDoc (doc);
2910 return rc;
2913 static gpg_error_t
2914 parse_import_opt_root (void *data, void *value)
2916 struct client_s *client = data;
2918 client->import_root = str_dup (value);
2919 return client->import_root ? 0 : GPG_ERR_ENOMEM;
2922 static gpg_error_t
2923 import_command (assuan_context_t ctx, char *line)
2925 gpg_error_t rc;
2926 struct client_s *client = assuan_get_pointer (ctx);
2927 unsigned char *result;
2928 size_t len;
2929 struct argv_s *args[] = {
2930 &(struct argv_s) {"root", OPTION_TYPE_ARG, parse_import_opt_root},
2931 NULL
2934 xfree (client->import_root);
2935 client->import_root = NULL;
2936 rc = parse_options (&line, args, client, 0);
2937 if (rc)
2938 return send_error (ctx, rc);
2940 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2941 if (rc)
2943 xfree (client->import_root);
2944 client->import_root = NULL;
2945 return send_error (ctx, rc);
2948 rc = do_import (client, client->import_root, result);
2949 xfree (client->import_root);
2950 client->import_root = NULL;
2951 return send_error (ctx, rc);
2954 static gpg_error_t
2955 do_lock (struct client_s *client, int add)
2957 gpg_error_t rc = lock_file_mutex (client, add);
2959 if (!rc)
2960 client->flags |= FLAG_LOCK_CMD;
2962 return rc;
2965 static gpg_error_t
2966 lock_command (assuan_context_t ctx, char *line)
2968 struct client_s *client = assuan_get_pointer (ctx);
2969 gpg_error_t rc;
2971 if (line && *line)
2972 return send_error (ctx, GPG_ERR_SYNTAX);
2974 rc = do_lock (client, 0);
2975 return send_error (ctx, rc);
2978 static gpg_error_t
2979 unlock_command (assuan_context_t ctx, char *line)
2981 struct client_s *client = assuan_get_pointer (ctx);
2982 gpg_error_t rc;
2984 if (line && *line)
2985 return send_error (ctx, GPG_ERR_SYNTAX);
2987 rc = unlock_file_mutex (client, 0);
2988 return send_error (ctx, rc);
2991 static void
2992 get_set_env (const char *name, const char *value)
2994 if (value && *value)
2995 setenv (name, value, 1);
2996 else
2997 unsetenv (name);
3000 static gpg_error_t
3001 option_command (assuan_context_t ctx, char *line)
3003 struct client_s *client = assuan_get_pointer (ctx);
3004 gpg_error_t rc = 0;
3005 char namebuf[255] = { 0 };
3006 char *name = namebuf;
3007 char *value = NULL, *p, *tmp = NULL;
3009 p = strchr (line, '=');
3010 if (!p)
3012 strncpy (namebuf, line, sizeof(namebuf));
3013 namebuf[sizeof(namebuf)-1] = 0;
3015 else
3017 strncpy (namebuf, line, strlen (line)-strlen (p));
3018 namebuf[sizeof(namebuf)-1] = 0;
3019 value = p+1;
3022 log_write2 ("OPTION name='%s' value='%s'", name, value);
3024 if (strcasecmp (name, (char *) "lock-timeout") == 0)
3026 long n = 0;
3028 if (value)
3030 n = strtol (value, &tmp, 10);
3031 if (tmp && *tmp)
3032 return send_error (ctx, GPG_ERR_INV_VALUE);
3035 client->lock_timeout = n;
3037 else if (strcasecmp (name, (char *) "NAME") == 0)
3039 if (value && strchr (value, ' '))
3040 rc = GPG_ERR_INV_VALUE;
3041 else
3043 tmp = pthread_getspecific (thread_name_key);
3044 xfree (tmp);
3045 MUTEX_LOCK (&cn_mutex);
3046 xfree (client->thd->name);
3047 client->thd->name = NULL;
3049 if (!value || !*value)
3050 pthread_setspecific (thread_name_key, str_dup (""));
3051 else
3053 client->thd->name = str_dup (value);
3054 pthread_setspecific (thread_name_key, str_dup (value));
3057 MUTEX_UNLOCK (&cn_mutex);
3060 else if (strcasecmp (name, "disable-pinentry") == 0)
3062 int n = 1;
3064 if (value && *value)
3066 n = (int) strtol (value, &tmp, 10);
3067 if (*tmp || n < 0 || n > 1)
3068 return send_error (ctx, GPG_ERR_INV_VALUE);
3071 if (n)
3072 client->flags |= FLAG_NO_PINENTRY;
3073 else
3074 client->flags &= ~FLAG_NO_PINENTRY;
3076 else if (strcasecmp (name, "ttyname") == 0)
3078 get_set_env ("GPG_TTY", value);
3080 else if (strcasecmp (name, "ttytype") == 0)
3082 get_set_env ("TERM", value);
3084 else if (strcasecmp (name, "display") == 0)
3086 get_set_env ("DISPLAY", value);
3088 else if (strcasecmp (name, "lc_messages") == 0)
3091 else if (strcasecmp (name, "lc_ctype") == 0)
3094 else if (strcasecmp (name, "desc") == 0)
3097 else if (strcasecmp (name, "pinentry-timeout") == 0)
3100 else
3101 rc = GPG_ERR_UNKNOWN_OPTION;
3103 return send_error (ctx, rc);
3106 static gpg_error_t
3107 do_rename (assuan_context_t ctx, char *line)
3109 struct client_s *client = assuan_get_pointer (ctx);
3110 char **args, *p;
3111 xmlNodePtr src, dst;
3112 struct string_s *str = NULL, *tstr;
3113 struct xml_request_s *req;
3114 gpg_error_t rc;
3116 args = str_split (line, " ", 0);
3117 if (!args || strv_length (args) != 2)
3119 strv_free (args);
3120 return GPG_ERR_SYNTAX;
3123 if (!xml_valid_element ((xmlChar *) args[1]))
3125 strv_free (args);
3126 return GPG_ERR_INV_VALUE;
3129 rc = xml_new_request (client, args[0], XML_CMD_RENAME, &req);
3130 if (rc)
3132 strv_free (args);
3133 return rc;
3136 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3137 if (rc)
3138 goto fail;
3140 rc = xml_is_element_owner (client, src);
3141 if (rc)
3142 goto fail;
3144 xmlChar *a = xmlGetProp (src, (xmlChar *) "_name");
3145 if (!a)
3147 rc = GPG_ERR_ENOMEM;
3148 goto fail;
3151 /* To prevent unwanted effects:
3153 * <element _name="a"><element _name="b"/></element>
3155 * RENAME a<TAB>b b
3157 if (xmlStrEqual (a, (xmlChar *) args[1]))
3159 xmlFree (a);
3160 rc = GPG_ERR_EEXIST;
3161 goto fail;
3164 xmlFree (a);
3165 str = string_new (args[0]);
3166 if (!str)
3168 rc = GPG_ERR_ENOMEM;
3169 goto fail;
3172 p = strrchr (str->str, '\t');
3173 if (p)
3174 tstr = string_truncate (str, strlen (str->str)-strlen (p));
3175 else
3176 tstr = string_truncate (str, 0);
3178 if (!tstr)
3180 string_free (str, 1);
3181 rc = GPG_ERR_ENOMEM;
3182 goto fail;
3185 str = tstr;
3186 tstr = string_append_printf (str, "%s%s", str->len ? "\t" : "", args[1]);
3187 if (!tstr)
3189 string_free (str, 1);
3190 rc = GPG_ERR_ENOMEM;
3191 goto fail;
3194 str = tstr;
3195 xml_free_request (req);
3196 rc = xml_new_request (client, str->str, XML_CMD_RENAME, &req);
3197 string_free (str, 1);
3198 if (rc)
3200 req = NULL;
3201 goto fail;
3204 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3205 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3206 goto fail;
3208 rc = 0;
3210 /* Target may exist:
3212 * <element _name="a"/>
3213 * <element _name="b" target="a"/>
3215 * RENAME b a
3217 if (src == dst)
3219 rc = GPG_ERR_EEXIST;
3220 goto fail;
3223 if (dst)
3225 rc = xml_is_element_owner (client, dst);
3226 if (rc)
3227 goto fail;
3229 rc = xml_add_attribute (client, src, "_name", args[1]);
3230 if (!rc)
3231 xml_unlink_node (client, dst);
3233 else
3234 rc = xml_add_attribute (client, src, "_name", args[1]);
3236 fail:
3237 xml_free_request (req);
3238 strv_free (args);
3239 return rc;
3242 static gpg_error_t
3243 rename_command (assuan_context_t ctx, char *line)
3245 struct client_s *client = assuan_get_pointer (ctx);
3246 gpg_error_t rc;
3247 struct argv_s *args[] = {
3248 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3249 NULL
3252 rc = parse_options (&line, args, client, 1);
3253 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3254 rc = GPG_ERR_SYNTAX;
3255 if (rc)
3256 return send_error (ctx, rc);
3258 if (client->opts & OPT_INQUIRE)
3260 unsigned char *result;
3261 size_t len;
3263 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3264 if (rc)
3265 return send_error (ctx, rc);
3267 pthread_cleanup_push ((void *)xfree, result);
3268 rc = do_rename (ctx, (char *)result);
3269 pthread_cleanup_pop (1);
3271 else
3272 rc = do_rename (ctx, line);
3274 return send_error (ctx, rc);
3277 static gpg_error_t
3278 do_copy (assuan_context_t ctx, char *line)
3280 struct client_s *client = assuan_get_pointer (ctx);
3281 char **args, **targs;
3282 xmlNodePtr src, dst, tree = NULL;
3283 struct xml_request_s *req;
3284 gpg_error_t rc;
3286 args = str_split (line, " ", 0);
3287 if (!args || strv_length (args) != 2)
3289 strv_free (args);
3290 return GPG_ERR_SYNTAX;
3293 targs = str_split (args[1], "\t", 0);
3294 if (!targs)
3296 strv_free (args);
3297 return GPG_ERR_SYNTAX;
3300 if (!xml_valid_element_path (targs, 0))
3302 strv_free (args);
3303 strv_free (targs);
3304 return GPG_ERR_INV_VALUE;
3306 strv_free (targs);
3308 rc = xml_new_request (client, args[0], XML_CMD_NONE, &req);
3309 if (rc)
3311 strv_free (args);
3312 return rc;
3315 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3316 if (rc)
3317 goto fail;
3319 tree = xmlCopyNodeList (src);
3320 if (!tree)
3322 rc = GPG_ERR_ENOMEM;
3323 goto fail;
3326 xml_free_request (req);
3327 /* Create the destination element path if it does not exist. */
3328 rc = xml_new_request (client, args[1], XML_CMD_STORE, &req);
3329 if (rc)
3331 req = NULL;
3332 goto fail;
3335 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3336 if (rc)
3337 goto fail;
3339 rc = xml_is_element_owner (client, dst);
3340 if (rc)
3341 goto fail;
3343 rc = xml_validate_target (client, req->doc, args[1], src);
3344 if (rc || src == dst)
3346 rc = rc ? rc : GPG_ERR_EEXIST;
3347 goto fail;
3350 /* Merge any attributes from the src node to the initial dst node. */
3351 for (xmlAttrPtr attr = tree->properties; attr; attr = attr->next)
3353 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3354 continue;
3356 xmlAttrPtr a = xmlHasProp (dst, attr->name);
3357 if (a)
3358 xmlRemoveProp (a);
3360 xmlChar *tmp = xmlNodeGetContent (attr->children);
3361 xmlNewProp (dst, attr->name, tmp);
3362 xmlFree (tmp);
3363 /* Create the default attributes. */
3364 rc = xml_add_attribute (client, dst, NULL, NULL);
3367 xmlNodePtr n = dst->children;
3368 (void)xml_unlink_node (client, n);
3369 dst->children = NULL;
3371 if (tree->children)
3373 n = xmlCopyNodeList (tree->children);
3374 if (!n)
3376 rc = GPG_ERR_ENOMEM;
3377 goto fail;
3380 n = xmlAddChildList (dst, n);
3381 if (!n)
3383 rc = GPG_ERR_ENOMEM;
3384 goto fail;
3387 rc = xml_update_element_mtime (client, xmlDocGetRootElement (client->doc)
3388 == dst->parent ? dst : dst->parent);
3391 fail:
3392 if (tree)
3393 (void)xml_unlink_node (client, tree);
3395 xml_free_request (req);
3396 strv_free (args);
3397 return rc;
3400 static gpg_error_t
3401 copy_command (assuan_context_t ctx, char *line)
3403 struct client_s *client = assuan_get_pointer (ctx);
3404 gpg_error_t rc;
3405 struct argv_s *args[] = {
3406 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3407 NULL
3410 rc = parse_options (&line, args, client, 1);
3411 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3412 rc = GPG_ERR_SYNTAX;
3413 if (rc)
3414 return send_error (ctx, rc);
3416 if (client->opts & OPT_INQUIRE)
3418 unsigned char *result;
3419 size_t len;
3421 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3422 if (rc)
3423 return send_error (ctx, rc);
3425 pthread_cleanup_push ((void *)xfree, result);
3426 rc = do_copy (ctx, (char *)result);
3427 pthread_cleanup_pop (1);
3429 else
3430 rc = do_copy (ctx, line);
3432 return send_error (ctx, rc);
3435 static gpg_error_t
3436 do_move (assuan_context_t ctx, char *line)
3438 struct client_s *client = assuan_get_pointer (ctx);
3439 char **args;
3440 xmlNodePtr src, dst;
3441 struct xml_request_s *req;
3442 gpg_error_t rc;
3444 args = str_split (line, " ", 0);
3445 if (!args || strv_length (args) != 2)
3447 strv_free (args);
3448 return GPG_ERR_SYNTAX;
3451 if (*args[1])
3453 char **targs = str_split (args[1], "\t", 0);
3454 if (!targs)
3456 strv_free (args);
3457 return GPG_ERR_ENOMEM;
3460 if (!xml_valid_element_path (targs, 0))
3462 strv_free (targs);
3463 strv_free (args);
3464 return GPG_ERR_INV_VALUE;
3467 strv_free (targs);
3470 rc = xml_new_request (client, args[0], XML_CMD_MOVE, &req);
3471 if (rc)
3473 strv_free (args);
3474 return rc;
3477 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3478 if (rc)
3479 goto fail;
3481 rc = xml_is_element_owner (client, src);
3482 if (rc)
3483 goto fail;
3485 xml_free_request (req);
3486 req = NULL;
3487 if (*args[1])
3489 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
3490 if (rc)
3491 goto fail;
3493 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc),
3494 &rc);
3496 else
3497 dst = xmlDocGetRootElement (client->doc);
3499 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3500 goto fail;
3502 rc = 0;
3504 for (xmlNodePtr n = dst; n; n = n->parent)
3506 if (n == src)
3508 rc = GPG_ERR_EEXIST;
3509 goto fail;
3513 if (dst)
3515 xmlChar *a = xml_attribute_value (src, (xmlChar *) "_name");
3516 xmlNodePtr dup = xml_find_element (client, dst->children, (char *) a, &rc);
3518 xmlFree (a);
3519 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3520 goto fail;
3522 rc = 0;
3524 if (dup)
3526 if (dup == src)
3528 rc = GPG_ERR_EEXIST;
3529 goto fail;
3532 if (dst == xmlDocGetRootElement (client->doc))
3534 xmlNodePtr n = src;
3535 int match = 0;
3537 while (n->parent && n->parent != dst)
3538 n = n->parent;
3540 xmlChar *a = xml_attribute_value (n, (xmlChar *) "_name");
3541 xmlChar *b = xml_attribute_value (src, (xmlChar *) "_name");
3543 if (xmlStrEqual (a, b))
3545 match = 1;
3546 xmlUnlinkNode (src);
3547 (void)xml_unlink_node (client, n);
3550 xmlFree (a);
3551 xmlFree (b);
3553 if (!match)
3554 (void)xml_unlink_node (client, dup);
3556 else
3557 xmlUnlinkNode (dup);
3561 if (!dst && *req->args)
3563 struct xml_request_s *nreq = NULL;
3564 xmlChar *name = xml_attribute_value (src, (xmlChar *) "_name");
3566 if (src->parent == xmlDocGetRootElement (client->doc)
3567 && !strcmp ((char *) name, *req->args))
3569 xmlFree (name);
3570 rc = GPG_ERR_EEXIST;
3571 goto fail;
3574 xmlFree (name);
3575 rc = xml_new_request (client, args[1], XML_CMD_STORE, &nreq);
3576 if (!rc)
3578 dst = xml_find_elements (client, nreq,
3579 xmlDocGetRootElement (nreq->doc), &rc);
3580 xml_free_request (nreq);
3583 if (rc)
3584 goto fail;
3587 xml_update_element_mtime (client, src->parent);
3588 xmlUnlinkNode (src);
3590 dst = xmlAddChildList (dst, src);
3591 if (!dst)
3592 rc = GPG_ERR_ENOMEM;
3593 else
3594 xml_update_element_mtime (client, dst->parent);
3596 fail:
3597 xml_free_request (req);
3598 strv_free (args);
3599 return rc;
3602 static gpg_error_t
3603 move_command (assuan_context_t ctx, char *line)
3605 struct client_s *client = assuan_get_pointer (ctx);
3606 gpg_error_t rc;
3607 struct argv_s *args[] = {
3608 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3609 NULL
3612 rc = parse_options (&line, args, client, 1);
3613 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3614 rc = GPG_ERR_SYNTAX;
3615 if (rc)
3616 return send_error (ctx, rc);
3618 if (client->opts & OPT_INQUIRE)
3620 unsigned char *result;
3621 size_t len;
3623 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3624 if (rc)
3625 return send_error (ctx, rc);
3627 pthread_cleanup_push ((void *)xfree, result);
3628 rc = do_move (ctx, (char *)result);
3629 pthread_cleanup_pop (1);
3631 else
3632 rc = do_move (ctx, line);
3634 return send_error (ctx, rc);
3637 static gpg_error_t
3638 ls_command (assuan_context_t ctx, char *line)
3640 gpg_error_t rc;
3641 char *tmp;
3642 char *dir;
3643 DIR *d;
3645 if (line && *line)
3646 return send_error (ctx, GPG_ERR_SYNTAX);
3648 tmp = str_asprintf ("%s/data", homedir);
3649 dir = expand_homedir (tmp);
3650 xfree (tmp);
3651 d = opendir (dir);
3652 rc = gpg_error_from_errno (errno);
3654 if (!d)
3656 xfree (dir);
3657 return send_error (ctx, rc);
3660 size_t len =
3661 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3662 struct dirent *p = xmalloc (len), *cur = NULL;
3663 char *list = NULL;
3665 xfree (dir);
3666 pthread_cleanup_push ((void *)xfree, p);
3667 pthread_cleanup_push ((void *)(void *)(void *)closedir, d);
3668 rc = 0;
3670 while (!readdir_r (d, p, &cur) && cur)
3672 struct stat st;
3674 if (stat (cur->d_name, &st) == -1 || !S_ISREG (st.st_mode))
3675 continue;
3677 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3679 if (!tmp)
3681 if (list)
3682 xfree (list);
3684 rc = GPG_ERR_ENOMEM;
3685 break;
3688 xfree (list);
3689 list = tmp;
3692 pthread_cleanup_pop (1); // closedir (d)
3693 pthread_cleanup_pop (1); // xfree (p)
3695 if (rc)
3696 return send_error (ctx, rc);
3698 if (!list)
3699 return send_error (ctx, GPG_ERR_NO_DATA);
3701 list[strlen (list) - 1] = 0;
3702 pthread_cleanup_push ((void *)xfree, list);
3703 rc = xfer_data (ctx, list, strlen (list));
3704 pthread_cleanup_pop (1);
3705 return send_error (ctx, rc);
3708 static gpg_error_t
3709 bye_notify (assuan_context_t ctx, char *line)
3711 struct client_s *cl = assuan_get_pointer (ctx);
3712 gpg_error_t ret = 0;
3714 update_client_state (cl, CLIENT_STATE_DISCON);
3716 #ifdef WITH_GNUTLS
3717 if (cl->thd->remote)
3719 int rc;
3723 struct timeval tv = { 0, 50000 };
3725 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3726 if (rc == GNUTLS_E_AGAIN)
3727 select (0, NULL, NULL, NULL, &tv);
3729 while (rc == GNUTLS_E_AGAIN);
3731 #endif
3733 /* This will let assuan_process_next() return. */
3734 if (fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK) == -1)
3736 cl->last_rc = gpg_error_from_errno (errno);
3737 ret = cl->last_rc;
3740 cl->last_rc = 0; // BYE command result
3741 return ret;
3744 static gpg_error_t
3745 reset_notify (assuan_context_t ctx, char *line)
3747 struct client_s *client = assuan_get_pointer (ctx);
3749 if (client)
3750 cleanup_client (client);
3752 return 0;
3756 * This is called before every Assuan command.
3758 static gpg_error_t
3759 command_startup (assuan_context_t ctx, const char *name)
3761 struct client_s *client = assuan_get_pointer (ctx);
3762 gpg_error_t rc;
3763 struct command_table_s *cmd = NULL;
3765 log_write2 ("command='%s'", name);
3766 client->last_rc = client->opts = 0;
3768 for (int i = 0; command_table[i]; i++)
3770 if (!strcasecmp (name, command_table[i]->name))
3772 if (command_table[i]->ignore_startup)
3773 return 0;
3774 cmd = command_table[i];
3775 break;
3779 if (!cmd)
3780 return GPG_ERR_UNKNOWN_COMMAND;
3782 client->last_rc = rc = gpg_error (file_modified (client, cmd));
3783 if (!rc)
3784 update_client_state (client, CLIENT_STATE_COMMAND);
3786 return rc;
3790 * This is called after every Assuan command.
3792 static void
3793 command_finalize (assuan_context_t ctx, gpg_error_t rc)
3795 struct client_s *client = assuan_get_pointer (ctx);
3797 if (!(client->flags & FLAG_LOCK_CMD))
3798 unlock_file_mutex (client, 0);
3800 unlock_flock (&client->flock_fd);
3801 log_write2 (_("command completed: rc=%u"), rc ? rc : client->last_rc);
3802 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
3803 #ifdef WITH_GNUTLS
3804 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
3805 #endif
3806 if (client->thd->state != CLIENT_STATE_DISCON)
3807 update_client_state (client, CLIENT_STATE_IDLE);
3810 static gpg_error_t
3811 parse_help_opt_html (void *data, void *value)
3813 struct client_s *client = data;
3815 (void) value;
3816 client->opts |= OPT_HTML;
3817 return 0;
3820 static gpg_error_t
3821 help_command (assuan_context_t ctx, char *line)
3823 gpg_error_t rc;
3824 int i;
3825 struct client_s *client = assuan_get_pointer (ctx);
3826 struct argv_s *args[] = {
3827 &(struct argv_s) {"html", OPTION_TYPE_NOARG, parse_help_opt_html},
3828 NULL
3831 rc = parse_options (&line, args, client, 1);
3832 if (rc)
3833 return send_error (ctx, rc);
3835 if (!line || !*line)
3837 char *tmp;
3838 char *help = str_dup (_("Usage: HELP [--html] [<COMMAND>]\n"
3839 "For commands that take an element path as an argument, each element is "
3840 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3841 "@*@*COMMANDS:"));
3843 for (i = 0; command_table[i]; i++)
3845 if (!command_table[i]->help)
3846 continue;
3848 /* @npxref{} won't put a "See " or "see " in front of the command.
3849 * It's not a texinfo command but needed for --html. */
3850 tmp = str_asprintf ("%s %s%s%s", help,
3851 client->opts & OPT_HTML ? "@npxref{" : "",
3852 command_table[i]->name,
3853 client->opts & OPT_HTML ? "}" : "");
3854 xfree (help);
3855 help = tmp;
3858 tmp = strip_texi_and_wrap (help, client->opts & OPT_HTML);
3859 xfree (help);
3860 pthread_cleanup_push ((void *)xfree, tmp);
3861 rc = xfer_data (ctx, tmp, strlen (tmp));
3862 pthread_cleanup_pop (1);
3863 return send_error (ctx, rc);
3866 for (i = 0; command_table[i]; i++)
3868 if (!strcasecmp (line, command_table[i]->name))
3870 char *help, *tmp;
3872 if (!command_table[i]->help)
3873 break;
3875 help = strip_texi_and_wrap (command_table[i]->help,
3876 client->opts & OPT_HTML);
3877 tmp = str_asprintf ("%s%s",
3878 (client->opts & OPT_HTML) ? "" : _("Usage: "),
3879 help);
3880 xfree (help);
3881 pthread_cleanup_push ((void *)xfree, tmp);
3882 rc = xfer_data (ctx, tmp, strlen (tmp));
3883 pthread_cleanup_pop (1);
3884 return send_error (ctx, rc);
3888 return send_error (ctx, GPG_ERR_INV_NAME);
3891 static void
3892 new_command (const char *name, int ignore, int unlock, int flock_type,
3893 gpg_error_t (*handler) (assuan_context_t, char *),
3894 const char *help)
3896 int i = 0;
3897 struct command_table_s **tmp;
3899 if (command_table)
3900 for (i = 0; command_table[i]; i++);
3902 tmp = xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
3903 assert (tmp);
3904 command_table = tmp;
3905 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
3906 command_table[i]->name = name;
3907 command_table[i]->handler = handler;
3908 command_table[i]->ignore_startup = ignore;
3909 command_table[i]->unlock = unlock;
3910 command_table[i]->flock_type = flock_type;
3911 command_table[i++]->help = help;
3912 command_table[i] = NULL;
3915 void
3916 deinit_commands ()
3918 int i;
3920 for (i = 0; command_table[i]; i++)
3921 xfree (command_table[i]);
3923 xfree (command_table);
3926 static int
3927 sort_commands (const void *arg1, const void *arg2)
3929 struct command_table_s *const *a = arg1;
3930 struct command_table_s *const *b = arg2;
3932 if (!*a || !*b)
3933 return 0;
3934 else if (*a && !*b)
3935 return 1;
3936 else if (!*a && *b)
3937 return -1;
3939 return strcmp ((*a)->name, (*b)->name);
3942 static gpg_error_t
3943 passwd_command (assuan_context_t ctx, char *line)
3945 struct client_s *client = assuan_get_pointer (ctx);
3946 gpg_error_t rc;
3948 rc = peer_is_invoker (client);
3949 if (rc == GPG_ERR_EACCES)
3950 return send_error (ctx, GPG_ERR_FORBIDDEN);
3951 else if (rc)
3952 return send_error (ctx, rc);
3954 if (client->flags & FLAG_NEW)
3955 return send_error (ctx, GPG_ERR_INV_STATE);
3957 rc = crypto_init_ctx (client->crypto, client->flags & FLAG_NO_PINENTRY);
3958 if (rc)
3959 return send_error (ctx, rc);
3961 if (!rc)
3962 rc = crypto_passwd (client, client->crypto);
3964 crypto_free_non_keys (client->crypto);
3965 return send_error (ctx, rc);
3968 static gpg_error_t
3969 parse_opt_data (void *data, void *value)
3971 struct client_s *client = data;
3973 (void) value;
3974 client->opts |= OPT_DATA;
3975 return 0;
3978 static gpg_error_t
3979 send_client_list (assuan_context_t ctx)
3981 struct client_s *client = assuan_get_pointer (ctx);
3982 gpg_error_t rc = 0;
3984 if (client->opts & OPT_VERBOSE)
3986 unsigned i, t;
3987 char **list = NULL;
3988 char *line;
3990 MUTEX_LOCK (&cn_mutex);
3991 pthread_cleanup_push ((void *)cleanup_mutex_cb, &cn_mutex);
3992 t = slist_length (cn_thread_list);
3994 for (i = 0; i < t; i++)
3996 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
3997 char *tmp;
3999 if (thd->state == CLIENT_STATE_UNKNOWN)
4000 continue;
4002 tmp = build_client_info_line (thd, 0);
4003 if (tmp)
4005 char **l = strv_cat (list, tmp);
4006 if (!l)
4007 rc = GPG_ERR_ENOMEM;
4008 else
4009 list = l;
4011 else
4012 rc = GPG_ERR_ENOMEM;
4014 if (rc)
4016 strv_free (list);
4017 break;
4021 pthread_cleanup_pop (1);
4022 if (rc)
4023 return rc;
4025 line = strv_join ("\n", list);
4026 strv_free (list);
4027 pthread_cleanup_push ((void *)xfree, line);
4028 rc = xfer_data (ctx, line, strlen (line));
4029 pthread_cleanup_pop (1);
4030 return rc;
4033 if (client->opts & OPT_DATA)
4035 char buf[ASSUAN_LINELENGTH];
4037 MUTEX_LOCK (&cn_mutex);
4038 snprintf (buf, sizeof (buf), "%u", slist_length (cn_thread_list));
4039 MUTEX_UNLOCK (&cn_mutex);
4040 rc = xfer_data (ctx, buf, strlen (buf));
4042 else
4043 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4045 return rc;
4048 static gpg_error_t
4049 getinfo_command (assuan_context_t ctx, char *line)
4051 struct client_s *client = assuan_get_pointer (ctx);
4052 gpg_error_t rc;
4053 char buf[ASSUAN_LINELENGTH];
4054 struct argv_s *args[] = {
4055 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4056 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_opt_verbose},
4057 NULL
4060 rc = parse_options (&line, args, client, 1);
4061 if (rc)
4062 return send_error (ctx, rc);
4064 if (!strcasecmp (line, "clients"))
4066 rc = send_client_list (ctx);
4068 else if (!strcasecmp (line, "cache"))
4070 if (client->opts & OPT_DATA)
4072 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4073 rc = xfer_data (ctx, buf, strlen (buf));
4075 else
4076 rc = send_status (ctx, STATUS_CACHE, NULL);
4078 else if (!strcasecmp (line, "pid"))
4080 char buf[32];
4081 pid_t pid = getpid ();
4083 snprintf (buf, sizeof (buf), "%i", pid);
4084 rc = xfer_data (ctx, buf, strlen (buf));
4086 else if (!strcasecmp (line, "version"))
4088 char *buf = str_asprintf ("0x%06x %s", VERSION_HEX,
4089 #ifdef WITH_GNUTLS
4090 "GNUTLS "
4091 #endif
4092 #ifdef WITH_LIBACL
4093 "ACL "
4094 #endif
4095 "");
4096 rc = xfer_data (ctx, buf, strlen (buf));
4097 xfree (buf);
4099 else if (!strcasecmp (line, "last_error"))
4101 if (client->last_error)
4102 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4103 else
4104 rc = GPG_ERR_NO_DATA;
4106 else if (!strcasecmp (line, "user"))
4108 char *user = NULL;
4110 #ifdef WITH_GNUTLS
4111 if (client->thd->remote)
4112 user = str_asprintf ("#%s", client->thd->tls->fp);
4113 else
4114 user = get_username (client->thd->peer->uid);
4115 #else
4116 user = get_username (client->thd->peer->uid);
4117 #endif
4118 if (user)
4120 pthread_cleanup_push ((void *)xfree, user);
4121 rc = xfer_data (ctx, user, strlen (user));
4122 pthread_cleanup_pop (1);
4124 else
4125 rc = GPG_ERR_NO_DATA;
4127 else
4128 rc = gpg_error (GPG_ERR_SYNTAX);
4130 return send_error (ctx, rc);
4133 static gpg_error_t
4134 parse_listkeys_opt_secret_only (void *data, void *value)
4136 struct client_s *client = data;
4138 (void) value;
4139 client->opts |= OPT_SECRET_ONLY;
4140 return 0;
4143 static gpg_error_t
4144 keyinfo_command (assuan_context_t ctx, char *line)
4146 struct client_s *client = assuan_get_pointer (ctx);
4147 gpg_error_t rc = 0;
4148 char **keys = NULL, **p = NULL;
4149 int sym = 0;
4151 if (line && *line)
4152 return send_error (ctx, GPG_ERR_SYNTAX);
4154 if (!(client->flags & FLAG_OPEN))
4155 return send_error (ctx, GPG_ERR_INV_STATE);
4157 if (client->flags & FLAG_NEW)
4158 return send_error (ctx, GPG_ERR_NO_DATA);
4160 rc = lock_flock (ctx, client->filename, FLOCK_TYPE_SH, &client->flock_fd);
4161 if (rc)
4162 return send_error (ctx, rc);
4164 rc = crypto_is_symmetric (client->filename);
4165 unlock_flock (&client->flock_fd);
4166 if (!rc)
4167 sym = 1;
4168 else if (rc != GPG_ERR_BAD_DATA)
4169 return send_error (ctx, rc);
4171 rc = 0;
4172 if (!sym)
4174 p = strv_catv(keys, client->crypto->pubkey);
4175 if (!p)
4176 rc = GPG_ERR_ENOMEM;
4179 if (!rc)
4181 keys = p;
4182 for (p = client->crypto->sigkey; p && *p; p++)
4184 if (!strv_printf(&keys, "S%s", *p))
4186 strv_free (keys);
4187 return send_error (ctx, GPG_ERR_ENOMEM);
4191 else
4192 rc = GPG_ERR_ENOMEM;
4194 if (!rc && keys)
4196 line = strv_join ("\n", keys);
4197 strv_free (keys);
4198 pthread_cleanup_push ((void *)xfree, line);
4199 if (line)
4200 rc = xfer_data (ctx, line, strlen (line));
4201 else
4202 rc = GPG_ERR_ENOMEM;
4204 pthread_cleanup_pop (1);
4206 else if (!keys)
4207 rc = GPG_ERR_NO_DATA;
4208 else
4209 strv_free (keys);
4211 return send_error (ctx, rc);
4214 static gpg_error_t
4215 kill_command (assuan_context_t ctx, char *line)
4217 struct client_s *client = assuan_get_pointer (ctx);
4218 gpg_error_t rc;
4219 unsigned i, t;
4221 if (!line || !*line)
4222 return send_error (ctx, GPG_ERR_SYNTAX);
4224 MUTEX_LOCK (&cn_mutex);
4225 pthread_cleanup_push ((void *)cleanup_mutex_cb, &cn_mutex);
4226 t = slist_length (cn_thread_list);
4227 rc = GPG_ERR_ESRCH;
4229 for (i = 0; i < t; i++)
4231 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
4232 char *tmp = str_asprintf ("%p", thd->tid);
4234 if (strcmp (line, tmp))
4236 xfree (tmp);
4237 continue;
4240 xfree (tmp);
4241 rc = peer_is_invoker (client);
4242 if (!rc)
4244 #ifdef HAVE_PTHREAD_CANCEL
4245 rc = pthread_cancel (thd->tid);
4246 #else
4247 close (thd->fd);
4248 thd->fd = -1;
4249 #endif
4250 break;
4253 rc = GPG_ERR_FORBIDDEN;
4254 if (config_get_boolean ("global", "strict_kill"))
4255 break;
4257 #ifdef WITH_GNUTLS
4258 if (client->thd->remote && thd->remote)
4260 if (!strcmp (client->thd->tls->fp, thd->tls->fp))
4262 #ifdef HAVE_PTHREAD_CANCEL
4263 rc = pthread_cancel (thd->tid);
4264 #else
4265 close (thd->fd);
4266 thd->fd = -1;
4267 #endif
4268 break;
4271 else if (!client->thd->remote && !thd->remote)
4272 #endif
4274 if (client->thd->peer->uid == thd->peer->uid)
4276 #ifdef HAVE_PTHREAD_CANCEL
4277 rc = pthread_cancel (thd->tid);
4278 #else
4279 close (thd->fd);
4280 thd->fd = -1;
4281 #endif
4284 break;
4287 pthread_cleanup_pop (1);
4288 return send_error (ctx, rc);
4291 static gpg_error_t
4292 listkeys_command (assuan_context_t ctx, char *line)
4294 struct client_s *client = assuan_get_pointer (ctx);
4295 struct crypto_s *crypto = NULL;
4296 char **pattern = NULL;
4297 gpgme_key_t *keys = NULL;
4298 char **result = NULL;
4299 gpg_error_t rc;
4300 struct argv_s *args[] = {
4301 &(struct argv_s) {"secret-only", OPTION_TYPE_NOARG,
4302 parse_listkeys_opt_secret_only },
4303 NULL
4306 rc = parse_options (&line, args, client, 1);
4307 if (rc)
4308 return send_error (ctx, rc);
4310 rc = crypto_init (&crypto, client->ctx, client->filename,
4311 client->flags & FLAG_NO_PINENTRY);
4312 if (rc)
4313 return send_error (ctx, rc);
4315 pthread_cleanup_push ((void *)(void *)crypto_free, crypto);
4316 pattern = str_split (line, ",", 0);
4317 pthread_cleanup_push ((void *)(void *)strv_free, pattern);
4318 rc = crypto_list_keys (crypto, pattern, client->opts & OPT_SECRET_ONLY,
4319 &keys);
4320 pthread_cleanup_pop (1);
4321 pthread_cleanup_pop (1);
4322 if (!rc)
4324 int i;
4326 for (i = 0; keys[i]; i++)
4328 char *p = crypto_key_info (keys[i]);
4329 char **r;
4331 if (!p)
4333 rc = GPG_ERR_ENOMEM;
4334 break;
4337 r = strv_cat (result, p);
4338 if (!r)
4340 rc = GPG_ERR_ENOMEM;
4341 break;
4344 result = r;
4347 if (!i)
4348 rc = GPG_ERR_NO_DATA;
4350 crypto_free_key_list (keys);
4353 if (!rc)
4355 line = strv_join ("\n", result);
4356 strv_free (result);
4357 pthread_cleanup_push ((void *)xfree, line);
4358 if (!line)
4359 rc = GPG_ERR_ENOMEM;
4360 else
4361 rc = xfer_data (ctx, line, strlen (line));
4363 pthread_cleanup_pop (1);
4365 else
4366 strv_free (result);
4368 return send_error (ctx, rc);
4371 void
4372 init_commands ()
4374 /* !BEGIN-HELP-TEXT!
4376 * This comment is used as a marker to generate the offline documentation
4377 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4378 * script to determine where commands begin and end.
4380 new_command("HELP", 1, 1, 0, help_command, _(
4381 "HELP [--html] [<COMMAND>]\n"
4382 "Show available commands or command specific help text."
4383 "@*@*"
4384 "The @option{--html} option will output the help text in HTML format."
4387 new_command("KILL", 1, 0, 0, kill_command, _(
4388 "KILL <thread_id>\n"
4389 "Terminates the client identified by @var{thread_id} and releases any file "
4390 "lock or other resources it has held. See @code{GETINFO} (@pxref{GETINFO}) "
4391 "for details about listing connected clients. An @code{invoking_user} "
4392 "(@pxref{Configuration}) may kill any client while others may only kill "
4393 "clients of the same @code{UID} or @abbr{TLS} fingerprint.\n"
4396 new_command("LISTKEYS", 0, 0, 0, listkeys_command, _(
4397 "LISTKEYS [--secret-only] [pattern[,<pattern>]]\n"
4398 "Returns a new line separated list of key information matching a comma "
4399 "separated list of @var{pattern}'s. When option @option{--secret-only} is "
4400 "specified, only keys matching @var{pattern} that also have a secret key "
4401 "available will be returned."
4404 new_command("KEYINFO", 1, 0, 0, keyinfo_command, _(
4405 "KEYINFO\n"
4406 "Returns a new line separated list of key ID's that the currently opened "
4407 "data file has recipients and signers for. If the key is a signing key it "
4408 "will be prefixed with an @code{S}. If the file is a new one, or has no "
4409 "signers in the case of being symmetrically encrypted, the error code "
4410 "@code{GPG_ERR_NO_DATA} is returned."
4413 new_command("GETINFO", 1, 1, 0, getinfo_command, _(
4414 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4415 "Get server and other information. The information is returned via a status "
4416 "message (@pxref{Status Messages}) unless otherwise noted or @option{--data} "
4417 "is specified."
4418 "@*@*"
4419 "@var{CACHE} returns the number of cached documents."
4420 "@*@*"
4421 "@var{CLIENTS} returns the number of "
4422 "connected clients via a status message or a list of connected clients when "
4423 "the @option{--verbose} parameter is used (implies @option{--data}). A "
4424 "verbose line of a client list contains "
4425 "space delimited "
4426 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4427 "IP address if remote, file lock status, whether the current client is self "
4428 "or not, client state (see below), "
4429 "user ID or TLS fingerprint of the connected client, username if the "
4430 "client is a local one else @code{-}, and finally the time stamp of when the "
4431 "client connected."
4432 "@*@*"
4433 "Client state @code{0} is an unknown client state, state @code{1} indicates "
4434 "the client has connected but hasn't completed initializing, state @code{2} "
4435 "indicates that the client is idle, state @code{3} means the "
4436 "client is in a command and state @code{4} means the client is disconnecting. "
4437 "@*@*"
4438 "@var{PID} returns the process ID number of the server via a data response."
4439 "@*@*"
4440 "@var{VERSION} returns the server version number and compile-time features "
4441 "via a data response with each being space delimited."
4442 "@*@*"
4443 "@var{LAST_ERROR} returns a detailed description of the last failed command "
4444 "via a data response, when available."
4445 "@*@*"
4446 "@var{USER} returns the username or @abbr{TLS} hash of the connected client "
4447 "via a data response."
4450 new_command("PASSWD", 0, 0, FLOCK_TYPE_EX|FLOCK_TYPE_KEEP, passwd_command, _(
4451 "PASSWD\n"
4452 "Changes the passphrase of the secret key required to open the current "
4453 "data file. If the data file is symmetrically encrypted, the error "
4454 "@code{GPG_ERR_NOT_SUPPORTED} is returned. When symmetrically encrypted, "
4455 "the @code{SAVE} command (@pxref{SAVE}) should be used instead to prevent "
4456 "this command saving any unwanted changes to the @abbr{XML} document."
4457 "@*@*"
4458 "This command is not available to non-invoking clients "
4459 "(@pxref{Access Control})."
4462 new_command("OPEN", 1, 1, FLOCK_TYPE_SH|FLOCK_TYPE_KEEP, open_command, _(
4463 "OPEN [--lock] <filename>\n"
4464 "Opens @var{filename}. When the @var{filename} is not found on the "
4465 "file-system then a new in-memory document will be created. If the file is "
4466 "found, it is looked for in the file cache and when found no passphrase will "
4467 "be required to open it. When not cached, @cite{pinentry(1)} will be used to "
4468 "retrieve the passphrase for decryption unless @option{disable-pinentry} "
4469 "(@pxref{OPTION}) was specified in which case @command{pwmd} will "
4470 "@emph{INQUIRE} the client for the passphrase."
4471 "@*@*"
4472 "When the @option{--lock} option is passed then the file mutex will be "
4473 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4474 "file had been opened."
4477 new_command("SAVE", 0, 0, FLOCK_TYPE_EX|FLOCK_TYPE_KEEP, save_command, _(
4478 "SAVE [--symmetric] [--inquire-keyparam] [--keyid=<keyid>[,..] | [--inquire-keyid]] [--sign-keyid=<fingerprint>[,..] | [--inquire-sign-keyid]]\n"
4479 "Writes the in-memory @abbr{XML} document to disk. The file written to is the "
4480 "file that was opened using the @code{OPEN} command (@pxref{OPEN}). If the "
4481 "file is a new one or the option @option{--inquire-keyparam} was passed, a "
4482 "new keypair will be generated and @cite{pinentry(1)} will be used to prompt "
4483 "for the passphrase of the secret key used for signing."
4484 "@*@*"
4485 "The @option{--inquire-keyparam} option will send an "
4486 "@emph{INQUIRE} to the client to obtain the key parameters to use for "
4487 "generating the new keypair. The inquired data is expected to be in "
4488 "@cite{gnupg} @abbr{XML} format. See the @cite{gnupg} documentation for "
4489 "details. Note that when this option is specified a new keypair will be "
4490 "generated reguardless if the file is a new one and that the passphrase for "
4491 "the current file will be required before generating the new keypair. This "
4492 "option is available to non-invoking clients (@pxref{Access Control}) only "
4493 "when the file is a new one."
4494 "@*@*"
4495 "You can encrypt the data file to a recipient other than the one that it "
4496 "was encrypted with by passing the @option{--keyid} or "
4497 "@option{--inquire-keyid} option with "
4498 "the key ID of a public encryption key as its argument. Use the "
4499 "@command{LISTKEYS} command (@pxref{LISTKEYS}) to show key information by "
4500 "pattern. The @option{--sign-keyid} or @option{--inquire-sign-keyid} option "
4501 "may also be used to sign the data "
4502 "file with an alternate key by specifying the key ID of a secret key. "
4503 "A passphrase to decrypt the data file "
4504 "will be required if one or more of the original encryption or signing keys "
4505 "are not found in either of these two options' arguments. The original "
4506 "encryption or signing keys will be used when either of these options are "
4507 "not specified."
4508 "@*@*"
4509 "The @option{--keyid} and @option{--sign-keyid} options are not available "
4510 "for non-invoking clients "
4511 "(@pxref{Access Control}) when the recipients or signers do not match those "
4512 "that were used when the file was @code{OPEN}'ed."
4513 "@*@*"
4514 "The @option{--symmetric} option specifies that a new data file be "
4515 "conventionally encrypted. These types of data files do not use a recipient "
4516 "public key but may be signed by using the @option{--sign-keyid} or "
4517 "@option{--inquire-sign-keyid} options. To remove all signers from a "
4518 "symmtrically encrypted data file, leave the option value empty. Note that "
4519 "you cannot change encryption schemes once a data file has been saved."
4522 new_command("ISCACHED", 1, 0, 0, iscached_command, _(
4523 "ISCACHED [--lock] [--agent [--sign]] <filename>\n"
4524 "Determines the file cache status of the specified @var{filename}. "
4525 "The default is to test whether the filename is cached in memory. Passing "
4526 "option @option{--agent} will test the @command{gpg-agent} cache for at most "
4527 "one cached key used for opening the data file (@pxref{OPEN}). To test if "
4528 "a signing key is cached, pass @option{--sign} along with @option{--agent}."
4529 "*@*@"
4530 "An @emph{OK} response is returned if the specified @var{filename} is found "
4531 "in the cache. If not found in the cache but exists on the filesystem "
4532 "then @code{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4533 "returned."
4534 "@*@*"
4535 "The @option{--lock} option will lock the file mutex of @var{filename} when "
4536 "the file exists; it does not need to be opened nor cached. The lock will be "
4537 "released when the client exits or sends the @code{UNLOCK} command "
4538 "(@pxref{UNLOCK})."
4541 new_command("CLEARCACHE", 1, 1, 0, clearcache_command, _(
4542 "CLEARCACHE [<filename>]\n"
4543 "Clears a file cache entry for all or the specified @var{filename}. Note that "
4544 "this will also clear any @command{gpg-agent} cached keys which may cause "
4545 "problems if another data file shares the same keys as @var{filename}."
4548 new_command("CACHETIMEOUT", 1, 1, 0, cachetimeout_command, _(
4549 "CACHETIMEOUT <filename> <seconds>\n"
4550 "The time in @var{seconds} until @var{filename} will be removed from the "
4551 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4552 "the passphrase for each @code{OPEN} command (@pxref{OPEN}) or @code{SAVE} "
4553 "(@pxref{SAVE}) command. @xref{Configuration}, and the @code{cache_timeout} "
4554 "parameter."
4557 new_command("LIST", 0, 1, 0, list_command, _(
4558 "LIST [--inquire] [--recurse] [element[<TAB>child[..]]]\n"
4559 "If no element path is given then a newline separated list of root elements "
4560 "is returned with a data response. If given, then children of the specified "
4561 "element path are returned."
4562 "@*@*"
4563 "Each element path "
4564 "returned will have zero or more flags appened to it. These flags are "
4565 "delimited from the element path by a single space character. A flag itself "
4566 "is a single character. Flag @code{P} indicates that access to the element "
4567 "is denied. Flag @code{+} indicates that there are child nodes of "
4568 "the current element path. Flag @code{E} indicates that an element of the "
4569 "element path contained in a @var{target} attribute could not be found. Flag "
4570 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4571 "(@pxref{Configuration}). Flag @code{T}, followed by a single space character, "
4572 "then an element path, is the element path of the @var{target} attribute "
4573 "contained in the current element."
4574 "@*@*"
4575 "When a specified element path contains an error, beit from the final "
4576 "element in the path or any previous element, the path is still shown but "
4577 "will contain the error flag for the element with the error. Determining "
4578 "the actual element which contains the error is up to the client. This can be "
4579 "done by traversing the final element up to parent elements that contain the "
4580 "same error flag."
4581 "@*@*"
4582 "The option @option{--recurse} may be used to list the entire element tree "
4583 "for a specified element path or the entire tree for all root elements."
4584 "@*@*"
4585 "When the @option{--inquire} option is passed then all remaining non-option "
4586 "arguments are retrieved via a server @emph{INQUIRE}."
4589 new_command("REALPATH", 0, 1, 0, realpath_command, _(
4590 "REALPATH [--inquire] element[<TAB>child[..]]\n"
4591 "Resolves all @code{target} attributes of the specified element path and "
4592 "returns the result with a data response. @xref{Target Attribute}, for details."
4593 "@*@*"
4594 "When the @option{--inquire} option is passed then all remaining non-option "
4595 "arguments are retrieved via a server @emph{INQUIRE}."
4598 new_command("STORE", 0, 1, 0, store_command, _(
4599 "STORE element[<TAB>child[..]]<TAB>[content]\n"
4600 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4601 "@*@*"
4602 "Creates a new element path or modifies the @var{content} of an existing "
4603 "element. If only a single element is specified then a new root element is "
4604 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4605 "set to the final @key{TAB} delimited element. If no @var{content} is "
4606 "specified after the final @key{TAB}, then the content of the existing "
4607 "element will be removed; or will be empty if creating a new element."
4608 "@*@*"
4609 "The only restriction of an element name is that it not contain whitespace "
4610 "characters. There is no other whitespace between the @key{TAB} delimited "
4611 "elements. It is recommended that the content of an element be base64 encoded "
4612 "when it contains control or @key{TAB} characters to prevent @abbr{XML} "
4613 "parsing and @command{pwmd} syntax errors."
4616 new_command("RENAME", 0, 1, 0, rename_command, _(
4617 "RENAME [--inquire] element[<TAB>child[..]] <value>\n"
4618 "Renames the specified @var{element} to the new @var{value}. If an element of "
4619 "the same name as the @var{value} already exists it will be overwritten."
4620 "@*@*"
4621 "When the @option{--inquire} option is passed then all remaining non-option "
4622 "arguments are retrieved via a server @emph{INQUIRE}."
4625 new_command("COPY", 0, 1, 0, copy_command, _(
4626 "COPY [--inquire] source[<TAB>child[..]] dest[<TAB>child[..]]\n"
4627 "Copies the entire element tree starting from the child node of the source "
4628 "element, to the destination element path. If the destination element path "
4629 "does not exist then it will be created; otherwise it is overwritten."
4630 "@*@*"
4631 "Note that attributes from the source element are merged into the "
4632 "destination element when the destination element path exists. When an "
4633 "attribute of the same name exists in both the source and destination "
4634 "elements then the destination attribute will be updated to the source "
4635 "attribute value."
4636 "@*@*"
4637 "When the @option{--inquire} option is passed then all remaining non-option "
4638 "arguments are retrieved via a server @emph{INQUIRE}."
4641 new_command("MOVE", 0, 1, 0, move_command, _(
4642 "MOVE [--inquire] source[<TAB>child[..]] [dest[<TAB>child[..]]]\n"
4643 "Moves the source element path to the destination element path. If the "
4644 "destination is not specified then it will be moved to the root node of the "
4645 "document. If the destination is specified and exists then it will be "
4646 "overwritten; otherwise non-existing elements of the destination element "
4647 "path will be created."
4648 "@*@*"
4649 "When the @option{--inquire} option is passed then all remaining non-option "
4650 "arguments are retrieved via a server @emph{INQUIRE}."
4653 new_command("DELETE", 0, 1, 0, delete_command, _(
4654 "DELETE [--inquire] element[<TAB>child[..]]\n"
4655 "Removes the specified element path and all of its children. This may break "
4656 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4657 "refers to this element or any of its children."
4658 "@*@*"
4659 "When the @option{--inquire} option is passed then all remaining non-option "
4660 "arguments are retrieved via a server @emph{INQUIRE}."
4663 new_command("GET", 0, 1, 0, get_command, _(
4664 "GET [--inquire] element[<TAB>child[..]]\n"
4665 "Retrieves the content of the specified element. The content is returned "
4666 "with a data response."
4667 "@*@*"
4668 "When the @option{--inquire} option is passed then all remaining non-option "
4669 "arguments are retrieved via a server @emph{INQUIRE}."
4672 new_command("ATTR", 0, 1, 0, attr_command, _(
4673 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] element[<TAB>child[..]] ..\n"
4674 "@table @asis\n"
4675 "@item ATTR SET attribute element[<TAB>child[..]] [value]\n"
4676 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4677 "element. When no @var{value} is specified any existing value will be removed."
4678 "@*@*"
4679 "@item ATTR DELETE attribute element[<TAB>child[..]]\n"
4680 " Removes an attribute from an element. If @var{attribute} is @code{_name} "
4681 "or @code{target} an error is returned. Use the @command{DELETE} command "
4682 "(@pxref{DELETE}) instead."
4683 "@*@*"
4684 "@item ATTR LIST element[<TAB>child[..]]\n"
4685 " Retrieves a newline separated list of attributes names and values "
4686 "from the specified element. Each attribute name and value is space delimited."
4687 "@*@*"
4688 "@item ATTR GET attribute element[<TAB>child[..]]\n"
4689 " Retrieves the value of an @var{attribute} from an element."
4690 "@end table\n"
4691 "@*@*"
4692 "When the @option{--inquire} option is passed then all remaining non-option "
4693 "arguments are retrieved via a server @emph{INQUIRE}."
4694 "@*@*"
4695 "@xref{Target Attribute}, for details about this special attribute and also "
4696 "@pxref{Other Attributes} for other attributes that are handled specially "
4697 "by @command{pwmd}."
4700 new_command("XPATH", 0, 1, 0, xpath_command, _(
4701 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4702 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4703 "specified it is assumed the expression is a request to return a result. "
4704 "Otherwise, the result is set to the @var{value} argument and the document is "
4705 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4706 "is assumed to be empty and the document is updated. For example:"
4707 "@sp 1\n"
4708 "@example\n"
4709 "XPATH //element[@@_name='password']@key{TAB}\n"
4710 "@end example\n"
4711 "@sp 1\n"
4712 "would clear the content of all @var{password} elements in the data file "
4713 "while leaving off the trailing @key{TAB} would return all @var{password} "
4714 "elements in @abbr{XML} format."
4715 "@*@*"
4716 "When the @option{--inquire} option is passed then all remaining non-option "
4717 "arguments are retrieved via a server @emph{INQUIRE}."
4718 "@*@*"
4719 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4720 "expression syntax."
4723 new_command("XPATHATTR", 0, 1, 0, xpathattr_command, _(
4724 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4725 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4726 "attributes and does not return a result. For the @var{SET} operation the "
4727 "@var{value} is optional but the field is required. If not specified then "
4728 "the attribute value will be empty. For example:"
4729 "@sp 1\n"
4730 "@example\n"
4731 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4732 "@end example\n"
4733 "@sp 1\n"
4734 "would create a @var{password} attribute for each @var{password} element "
4735 "found in the document. The attribute value will be empty but still exist."
4736 "@*@*"
4737 "When the @option{--inquire} option is passed then all remaining non-option "
4738 "arguments are retrieved via a server @emph{INQUIRE}."
4739 "@*@*"
4740 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4741 "expression syntax."
4744 new_command("IMPORT", 0, 1, 0, import_command, _(
4745 "IMPORT [--root=element[<TAB>child[..]]]\n"
4746 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4747 "@*@*"
4748 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4749 "argument is raw @abbr{XML} data. The content is created as a child of "
4750 "the element path specified with the @option{--root} option or at the "
4751 "document root when not specified. Existing elements of the same name will "
4752 "be overwritten."
4753 "@*@*"
4754 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4755 "for details."
4758 new_command("DUMP", 0, 1, 0, dump_command, _(
4759 "DUMP\n"
4760 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4761 "dumping a specific node."
4764 new_command("LOCK", 0, 0, 0, lock_command, _(
4765 "LOCK\n"
4766 "Locks the mutex associated with the opened file. This prevents other clients "
4767 "from sending commands to the same opened file until the client "
4768 "that sent this command either disconnects or sends the @code{UNLOCK} "
4769 "command. @xref{UNLOCK}."
4772 new_command("UNLOCK", 1, 0, 0, unlock_command, _(
4773 "UNLOCK\n"
4774 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4775 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4776 "@pxref{ISCACHED})."
4779 new_command("GETCONFIG", 1, 1, 0, getconfig_command, _(
4780 "GETCONFIG [filename] <parameter>\n"
4781 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4782 "data response. If no file has been opened then the value for @var{filename} "
4783 "or the default from the @var{global} section will be returned. If a file "
4784 "has been opened and no @var{filename} is specified, the value previously "
4785 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4788 new_command("OPTION", 1, 1, 0, option_command, _(
4789 "OPTION <NAME>=[<VALUE>]\n"
4790 "Sets a client option @var{name} to @var{value}. The value for an option is "
4791 "kept for the duration of the connection with the exception of the "
4792 "@command{pinentry} options which are defaults for all future connections "
4793 "(@pxref{Pinentry}). When @var{value} is empty the option is unset."
4794 "@*@*"
4795 "@table @asis\n"
4796 "@item DISABLE-PINENTRY\n"
4797 "Disable use of @command{pinentry} for passphrase retrieval. When @code{1}, a "
4798 "server inquire is sent to the client to obtain the passphrase. This option "
4799 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4800 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands. Set to @code{0} "
4801 "to use a @command{pinentry}."
4802 "@*@*"
4803 "@item DISPLAY\n"
4804 "Set or unset the X11 display to use when prompting for a passphrase."
4805 "@*@*"
4806 "@item TTYNAME\n"
4807 "Set the terminal device path to use when prompting for a passphrase."
4808 "@*@*"
4809 "@item TTYTYPE\n"
4810 "Set the terminal type for use with @option{TTYNAME}."
4811 "@*@*"
4812 "@item NAME\n"
4813 "Associates the thread ID of the connection with the specified textual "
4814 "representation. Useful for debugging log messages. May not contain whitespace."
4815 "@*@*"
4816 "@item LOCK-TIMEOUT\n"
4817 "When not @code{0}, the duration in tenths of a second to wait for the file "
4818 "mutex which has been locked by another thread to be released before returning "
4819 "an error. When @code{-1} the error will be returned immediately."
4820 "@end table\n"
4823 new_command("LS", 1, 1, 0, ls_command, _(
4824 "LS\n"
4825 "Returns a newline separated list of data files stored in the data directory "
4826 "@file{HOMEDIR/data} (@pxref{Invoking}) with a data response."
4829 new_command("RESET", 1, 1, 0, NULL, _(
4830 "RESET\n"
4831 "Closes the currently opened file but keeps any previously set client options "
4832 "(@pxref{OPTION})."
4835 new_command("NOP", 1, 1, 0, NULL, _(
4836 "NOP\n"
4837 "Does nothing. Always returns successfully."
4840 /* !END-HELP-TEXT! */
4841 new_command ("CANCEL", 1, 1, 0, NULL, NULL);
4842 new_command ("END", 1, 1, 0, NULL, NULL);
4843 new_command ("BYE", 1, 1, 0, NULL, NULL);
4845 int i;
4846 for (i = 0; command_table[i]; i++);
4847 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4848 sort_commands);
4851 gpg_error_t
4852 register_commands (assuan_context_t ctx)
4854 int i = 0, rc;
4856 for (; command_table[i]; i++)
4858 if (!command_table[i]->handler)
4859 continue;
4861 rc = assuan_register_command (ctx, command_table[i]->name,
4862 command_table[i]->handler,
4863 command_table[i]->help);
4864 if (rc)
4865 return rc;
4868 rc = assuan_register_bye_notify (ctx, bye_notify);
4869 if (rc)
4870 return rc;
4872 rc = assuan_register_reset_notify (ctx, reset_notify);
4873 if (rc)
4874 return rc;
4876 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4877 if (rc)
4878 return rc;
4880 return assuan_register_post_cmd_notify (ctx, command_finalize);