CLEARCACHE: Reworked ACL's.
[libpwmd.git] / src / commands.c
blob4515f1ed2db36f353c47049994e3a6d7cfea8256
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
3 2016
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of pwmd.
8 Pwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Pwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <err.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <ctype.h>
34 #include <dirent.h>
35 #include <pthread.h>
36 #include <stdint.h>
37 #include <assert.h>
39 #include "pwmd-error.h"
40 #include <gcrypt.h>
42 #include "mem.h"
43 #include "xml.h"
44 #include "util-misc.h"
45 #include "common.h"
46 #include "rcfile.h"
47 #include "cache.h"
48 #include "commands.h"
49 #include "mutex.h"
50 #include "crypto.h"
52 /* These are command option flags. */
53 #define OPT_INQUIRE 0x0001
54 #define OPT_NO_PASSPHRASE 0x0002
55 #define OPT_ASK 0x0004
56 #define OPT_LIST_RECURSE 0x0008
57 #define OPT_VERBOSE 0x0010
58 #define OPT_LOCK 0x0020
59 #define OPT_LOCK_ON_OPEN 0x0040
60 #define OPT_SIGN 0x0080
61 #define OPT_LIST_ALL 0x0100
62 #define OPT_DATA 0x0200
63 #define OPT_NO_AGENT 0x0400
64 #define OPT_SECRET_ONLY 0x0800
65 #define OPT_INQUIRE_KEYID 0x1000
66 #define OPT_INQUIRE_SIGN_KEYID 0x2000
67 #define OPT_SYMMETRIC 0x4000
68 #define OPT_NO_SIGNER 0x8000
69 #define OPT_HTML 0x10000
70 #define OPT_CACHE_AGENT 0x20000
71 #define OPT_CACHE_SIGN 0x40000
73 #define FLOCK_TYPE_NONE 0
74 #define FLOCK_TYPE_SH 0x0001
75 #define FLOCK_TYPE_EX 0x0002
76 #define FLOCK_TYPE_KEEP 0x0004
78 struct command_table_s
80 const char *name;
81 gpg_error_t (*handler) (assuan_context_t, char *line);
82 const char *help;
83 int ignore_startup;
84 int unlock; // unlock the file mutex after validating the checksum
85 uint32_t flock_type;
88 static struct command_table_s **command_table;
90 static gpg_error_t do_lock (struct client_s *client, int add);
91 static gpg_error_t validate_checksum (struct client_s *, const char *filename,
92 struct cache_data_s *, unsigned char **,
93 size_t *);
94 static gpg_error_t update_checksum (struct client_s *client);
96 /* When 'status' is true the 'self' field of the status line will be false
97 * because we never send the STATE status message to the same client that
98 * initiated it. */
99 static char *
100 build_client_info_line (struct client_thread_s *thd, int status)
102 int with_state = config_get_boolean ("global", "send_state");
103 char *uid, *username = NULL;
104 char *line;
106 #ifdef WITH_GNUTLS
107 if (thd->remote)
108 uid = str_asprintf("#%s", thd->tls->fp);
109 else
111 uid = str_asprintf("%u", thd->peer->uid);
112 username = get_username (thd->peer->uid);
114 #else
115 uid = str_asprintf("%u", thd->peer->uid);
116 username = get_username (thd->peer->uid);
117 #endif
118 line = str_asprintf ("%p %s %s %s %u %u %u %s %s %u",
119 thd->tid,
120 thd->name ? thd->name : "-",
121 thd->cl && thd->cl->filename
122 && (thd->cl->flags & FLAG_OPEN)
123 ? thd->cl->filename : "/",
124 #ifdef WITH_GNUTLS
125 thd->remote ? thd->peeraddr : "-",
126 #else
127 "-",
128 #endif
129 thd->cl && thd->cl->flags & FLAG_HAS_LOCK ? 1 : 0,
130 !status && pthread_equal (pthread_self (), thd->tid) ? 1 : 0,
131 with_state ? thd->state : CLIENT_STATE_UNKNOWN, uid,
132 #ifdef WITH_GNUTLS
133 thd->remote ? "-" : username,
134 #else
135 username,
136 #endif
137 thd->conntime
140 xfree (username);
141 xfree (uid);
142 return line;
145 void
146 update_client_state (struct client_s *client, unsigned s)
148 MUTEX_LOCK (&cn_mutex);
149 client->thd->state = s;
150 MUTEX_UNLOCK (&cn_mutex);
151 int n = config_get_boolean ("global", "send_state");
153 if (n && client->thd->state != CLIENT_STATE_UNKNOWN)
155 char *line = build_client_info_line (client->thd, 1);
157 if (line)
158 send_status_all_not_self (n == 2, STATUS_STATE, "%s", line);
160 xfree (line);
164 static gpg_error_t
165 unlock_file_mutex (struct client_s *client, int remove)
167 gpg_error_t rc = 0;
169 // OPEN: keep the lock for the same file being reopened.
170 if (client->flags & FLAG_KEEP_LOCK && client->flags & FLAG_HAS_LOCK)
171 return 0;
173 if (!(client->flags & FLAG_HAS_LOCK))
174 return GPG_ERR_NOT_LOCKED;
176 rc = cache_unlock_mutex (client->filename, 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->filename,
195 client->lock_timeout, add, timeout);
196 if (!rc)
197 client->flags |= FLAG_HAS_LOCK;
199 return rc;
202 static gpg_error_t
203 file_modified (struct client_s *client, struct command_table_s *cmd)
205 gpg_error_t rc = 0;
206 int type = !cmd->flock_type || (cmd->flock_type & FLOCK_TYPE_SH)
207 ? LOCK_SH : LOCK_EX;
209 if (!(client->flags & FLAG_OPEN))
210 return GPG_ERR_INV_STATE;
212 rc = lock_file_mutex (client, 0);
213 if (rc && rc != GPG_ERR_NO_DATA)
214 return rc;
216 rc = lock_flock (client->ctx, client->filename, type, &client->flock_fd);
217 if (!rc)
219 rc = validate_checksum (client, client->filename, NULL, NULL, NULL);
220 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
221 rc = 0;
222 else if (rc)
223 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
225 else if (gpg_err_code (rc) == GPG_ERR_ENOENT)
226 rc = 0;
228 /* FLAG_HAS_LOCK may have been set by the LOCK command or OPEN --lock. */
229 if ((cmd->unlock && !(client->flags & FLAG_HAS_LOCK)) || rc)
230 unlock_file_mutex (client, 0);
232 if (rc || !(cmd->flock_type & FLOCK_TYPE_KEEP))
233 unlock_flock (&client->flock_fd);
235 return rc;
238 static gpg_error_t
239 parse_xml (assuan_context_t ctx, int new)
241 struct client_s *client = assuan_get_pointer (ctx);
242 int cached = client->doc != NULL;
243 gpg_error_t rc = 0;
245 if (new)
247 client->doc = xml_new_document ();
248 if (client->doc)
250 xmlChar *result;
251 int len;
253 xmlDocDumpFormatMemory (client->doc, &result, &len, 0);
254 client->crypto->plaintext = result;
255 client->crypto->plaintext_size = len;
256 if (!client->crypto->plaintext)
258 xmlFreeDoc (client->doc);
259 client->doc = NULL;
260 rc = GPG_ERR_ENOMEM;
263 else
264 rc = GPG_ERR_ENOMEM;
266 else if (!cached)
267 rc = xml_parse_doc ((char *) client->crypto->plaintext,
268 client->crypto->plaintext_size,
269 (xmlDocPtr *)&client->doc);
271 return rc;
274 static void
275 free_client (struct client_s *client)
277 if (client->doc)
278 xmlFreeDoc (client->doc);
280 xfree (client->crc);
281 xfree (client->filename);
282 xfree (client->last_error);
283 crypto_free (client->crypto);
284 client->crypto = NULL;
287 void
288 cleanup_client (struct client_s *client)
290 assuan_context_t ctx = client->ctx;
291 struct client_thread_s *thd = client->thd;
292 long lock_timeout = client->lock_timeout;
293 xmlErrorPtr xml_error = client->xml_error;
294 int no_pinentry = (client->flags & FLAG_NO_PINENTRY);
295 int flock_fd = client->flock_fd;
297 unlock_file_mutex (client, client->flags & FLAG_NEW);
298 free_client (client);
299 memset (client, 0, sizeof (struct client_s));
300 client->flock_fd = flock_fd;
301 client->xml_error = xml_error;
302 client->ctx = ctx;
303 client->thd = thd;
304 client->lock_timeout = lock_timeout;
305 client->flags |= no_pinentry ? FLAG_NO_PINENTRY : 0;
308 static void
309 req_cleanup (void *arg)
311 if (!arg)
312 return;
314 strv_free ((char **) arg);
317 static gpg_error_t
318 parse_open_opt_lock (void *data, void *value)
320 struct client_s *client = data;
322 client->opts |= OPT_LOCK_ON_OPEN;
323 return 0;
326 static gpg_error_t
327 parse_opt_inquire (void *data, void *value)
329 struct client_s *client = data;
331 (void) value;
332 client->opts |= OPT_INQUIRE;
333 return 0;
336 static gpg_error_t
337 update_checksum (struct client_s *client)
339 unsigned char *crc;
340 size_t len;
341 struct cache_data_s *cdata;
342 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
344 if (rc)
345 return rc;
347 xfree (client->crc);
348 client->crc = crc;
349 cdata = cache_get_data (client->filename);
350 if (cdata)
352 xfree (cdata->crc);
353 cdata->crc = xmalloc (len);
354 memcpy (cdata->crc, crc, len);
357 return 0;
360 static gpg_error_t
361 validate_checksum (struct client_s *client, const char *filename,
362 struct cache_data_s *cdata, unsigned char **r_crc,
363 size_t *r_crclen)
365 unsigned char *crc;
366 size_t len;
367 gpg_error_t rc;
368 int n = 0;
370 if (cdata && !cdata->crc)
371 return GPG_ERR_CHECKSUM;
373 rc = get_checksum (filename, &crc, &len);
374 if (rc)
375 return rc;
377 if (cdata)
378 n = memcmp (cdata->crc, crc, len);
379 else if (client->crc)
380 n = memcmp (client->crc, crc, len);
382 if (!n && r_crc)
384 *r_crc = crc;
385 *r_crclen = len;
387 else
388 xfree (crc);
390 return n ? GPG_ERR_CHECKSUM : 0;
393 static gpg_error_t
394 open_command (assuan_context_t ctx, char *line)
396 gpg_error_t rc;
397 struct client_s *client = assuan_get_pointer (ctx);
398 char **req, *filename;
399 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 /* This client may have locked a different file with ISCACHED --lock than
434 * the current filename. This will remove that lock. */
435 same_file = client->filename && !strcmp (filename, client->filename);
436 if (client->flags & FLAG_OPEN ||
437 (client->flags & FLAG_HAS_LOCK && !same_file))
439 uint32_t opts = client->opts;
440 uint32_t flags = client->flags;
442 if (same_file)
443 client->flags |= FLAG_KEEP_LOCK;
444 else if (client->flags & FLAG_NEW)
445 cache_clear (NULL, client->filename, 0);
447 cleanup_client (client);
448 client->opts = opts;
449 client->flags |= flags;
450 client->flags &= ~(FLAG_LOCK_CMD);
451 if (!same_file)
452 client->flags &= ~(FLAG_HAS_LOCK | FLAG_NEW);
455 client->filename = str_dup (filename);
456 if (!client->filename)
458 strv_free (req);
459 return send_error(ctx, GPG_ERR_ENOMEM);
462 /* Need to lock the mutex here because file_modified() cannot without
463 * knowing the filename. */
464 rc = lock_file_mutex (client, 1);
465 if (rc)
466 client->flags &= ~FLAG_OPEN;
468 if (!rc)
470 rc = lock_flock (ctx, filename, LOCK_SH, &client->flock_fd);
471 if (gpg_err_code (rc) == GPG_ERR_ENOENT)
472 rc = 0;
475 if (!rc)
477 struct stat st;
478 char *keyfile = config_get_string (client->filename, "passphrase_file");
480 rc = crypto_init (&client->crypto, client->ctx, client->filename,
481 client->flags & FLAG_NO_PINENTRY, keyfile);
482 if (rc)
483 xfree (keyfile);
485 if (!rc && stat (client->filename, &st) == -1)
486 rc = gpg_error_from_errno (errno);
487 else if (!rc && !S_ISREG (st.st_mode)) // to match LS output
488 rc = gpg_error_from_errno (ENOANO);
491 if (!rc || gpg_err_code (rc) == GPG_ERR_ENOENT)
493 cdata = cache_get_data (client->filename);
495 if (rc) // new file
497 rc = 0;
498 client->flags |= FLAG_NEW;
499 // data file disappeared. clear the cache entry.
500 cache_clear (NULL, client->filename, 1);
501 cdata = NULL;
503 else if (cdata && cdata->doc) // cached document
505 int reload = 0;
506 int defer = 0;
508 if (!rc && !(client->flags & FLAG_NEW))
510 rc = validate_checksum (client, client->filename, cdata, &crc,
511 &crclen);
512 if (rc == GPG_ERR_CHECKSUM)
514 rc = 0;
515 reload = 1;
519 if (!rc)
521 rc = cache_iscached (client->filename, &defer, 0, 0);
522 if ((!rc || rc == GPG_ERR_ENOENT) && defer)
524 rc = 0;
525 reload = 2;
529 if (!rc && reload)
531 if (reload == 1)
532 log_write ("%s: %s", client->filename,
533 pwmd_strerror (GPG_ERR_CHECKSUM));
534 cache_clear (NULL, client->filename, 1);
535 cdata = NULL;
536 rc = crypto_decrypt (client, client->crypto);
538 #ifdef WITH_GNUTLS
539 else if (!rc)
541 if (client->thd->remote
542 && config_get_boolean (client->filename, "tcp_require_key")
543 && !(client->flags & FLAG_NEW))
544 rc = crypto_try_decrypt (client,
545 (client->flags & FLAG_NO_PINENTRY));
547 #endif
549 if (!rc && cdata)
551 client->crypto->plaintext = cdata->doc;
552 client->crypto->plaintext_size = cdata->size;
553 rc = cache_decrypt (client->crypto);
554 if (!rc)
556 cdata->doc = NULL;
557 cdata->size = 0;
558 strv_free (client->crypto->pubkey);
559 strv_free (client->crypto->sigkey);
560 client->crypto->pubkey = strv_dup (cdata->pubkey);
561 client->crypto->sigkey = strv_dup (cdata->sigkey);
562 cached = 1;
564 else
566 client->crypto->plaintext = NULL;
567 client->crypto->plaintext_size = 0;
571 else // existing file
573 cached = cdata != NULL;
574 rc = crypto_decrypt (client, client->crypto);
578 if (!rc)
580 rc = parse_xml (ctx, client->flags & FLAG_NEW);
581 if (!rc)
583 rc = cache_encrypt (client->crypto);
584 if (rc)
585 cache_clear (NULL, client->filename, 1);
586 else
588 int timeout = config_get_integer (client->filename,
589 "cache_timeout");
591 free_cache_data_once (cdata);
592 cdata = xcalloc (1, sizeof (struct cache_data_s));
593 cdata->doc = client->crypto->plaintext;
594 cdata->size = client->crypto->plaintext_size;
595 cdata->pubkey = strv_dup(client->crypto->pubkey);
596 cdata->sigkey = strv_dup(client->crypto->sigkey);
597 client->crypto->plaintext = NULL;
598 client->crypto->plaintext_size = 0;
600 if (cached) // wont increment the refcount
602 /* Prevent using another FD to update the checksum for a
603 * cached data file. The validity has already been
604 * verified. */
605 xfree (client->crc);
606 client->crc = xmalloc (crclen);
607 memcpy (client->crc, crc, crclen);
608 xfree (cdata->crc);
609 cdata->crc = xmalloc (crclen);
610 memcpy (cdata->crc, crc, crclen);
612 rc = cache_set_data (client->filename, cdata);
613 /* The cache entry may have been removed and cache_set_data()
614 * already sent STATUS_CACHE. */
615 if (!cache_iscached (client->filename, NULL, 0, 0))
616 rc = send_status (ctx, STATUS_CACHE, NULL);
618 else
619 rc = cache_add_file (client->filename, cdata, timeout);
624 pthread_cleanup_pop (1);
625 xfree (crc);
627 if (!rc && !(client->flags & FLAG_NEW) && !cached)
628 rc = update_checksum (client);
630 if (!rc)
632 client->flags |= FLAG_OPEN;
634 if (client->flags & FLAG_NEW)
635 rc = send_status (ctx, STATUS_NEWFILE, NULL);
637 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
638 rc = do_lock (client, 0);
641 if (rc)
643 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
645 if (client->flags & FLAG_NEW)
646 cache_clear (NULL, client->filename, 1);
648 crypto_free (client->crypto);
649 client->crypto = NULL;
650 client->flags &= ~FLAG_OPEN;
652 else
653 crypto_free_non_keys (client->crypto);
655 return send_error (ctx, rc);
658 /* If not the invoking_user or is an existing file, check that the list of user
659 * supplied key ID's are in the list of current key ID's obtained when
660 * decrypting the data file. Both short and long form keyid fingerprints are
661 * valid:
663 * 8 byte with optional "0x" prefix
664 * 16 byte
665 * 40 byte
667 * Although, the key ID's are converted to the 8 byte form before calling this
668 * function in parse_save_opt_keyid_common() to keep consistancy with KEYINFO
669 * output.
671 static gpg_error_t
672 permitted_to_save (struct client_s *client, const char **keys,
673 const char **value)
675 gpg_error_t rc = 0;
676 const char **v;
678 if ((client->flags & FLAG_NEW) || (client->opts & OPT_SYMMETRIC))
679 return 0;
681 rc = peer_is_invoker (client);
682 if (!rc)
683 return 0;
685 /* Empty match. */
686 if ((!value && !keys) || ((value && !*value) && (keys && !*keys)))
687 return 0;
689 for (v = value; v && *v; v++)
691 const char **k, *pv = *v;
692 int match = 0;
694 if (*pv == '0' && *(pv+1) == 'x')
695 pv += 2;
697 for (k = keys; k && *k; k++)
699 const char *pk = *k;
701 if (*pk == '0' && *(pk+1) == 'x')
702 pk += 2;
704 rc = !strcmp (pv, pk) ? 0 : GPG_ERR_FORBIDDEN;
705 if (rc)
706 rc = !strcmp (pv, *k) ? 0 : GPG_ERR_FORBIDDEN;
708 if (!rc)
710 match = 1;
711 break;
715 if (!match)
716 return GPG_ERR_FORBIDDEN;
719 return rc;
722 static gpg_error_t
723 parse_save_opt_keyid_common (struct client_s *client, const char **list,
724 const char *value, char ***dst)
726 gpg_error_t rc;
727 char **p = NULL;
729 if (value && *value)
731 p = str_split (value, ",", 0);
732 if (!p)
733 return GPG_ERR_ENOMEM;
736 crypto_keyid_to_16b (p);
737 rc = permitted_to_save (client, list, (const char **)p);
738 if (!rc)
739 *dst = p;
740 else
741 strv_free (*dst);
743 return rc;
746 static gpg_error_t
747 parse_save_opt_keyid (void *data, void *value)
749 struct client_s *client = data;
750 const char *str = value;
751 char **dst = NULL;
752 gpg_error_t rc;
754 rc = parse_save_opt_keyid_common (client,
755 (const char **)client->crypto->pubkey,
756 str, &dst);
757 if (rc)
758 return rc;
760 client->crypto->save.pubkey = dst;
761 return 0;
764 static gpg_error_t
765 parse_save_opt_sign_keyid (void *data, void *value)
767 struct client_s *client = data;
768 const char *str = value;
769 char **dst = NULL;
770 gpg_error_t rc;
772 rc = parse_save_opt_keyid_common (client,
773 (const char **)client->crypto->sigkey,
774 str, &dst);
775 if (rc)
776 return rc;
778 if (!dst)
779 client->opts |= OPT_NO_SIGNER;
781 client->crypto->save.sigkey = dst;
782 return 0;
785 static gpg_error_t
786 parse_save_opt_inquire (struct client_s *client, uint32_t opt)
788 if (opt == OPT_INQUIRE && !(client->flags & FLAG_NEW))
790 gpg_error_t rc = peer_is_invoker (client);
792 if (rc)
793 return rc;
796 client->opts |= opt;
797 return 0;
800 static gpg_error_t
801 parse_save_opt_inquire_keyparam (void *data, void *value)
803 return parse_save_opt_inquire (data, OPT_INQUIRE);
806 static gpg_error_t
807 parse_save_opt_inquire_keyid (void *data, void *value)
809 return parse_save_opt_inquire (data, OPT_INQUIRE_KEYID);
812 static gpg_error_t
813 parse_save_opt_inquire_sign_keyid (void *data, void *value)
815 return parse_save_opt_inquire (data, OPT_INQUIRE_SIGN_KEYID);
818 static gpg_error_t
819 parse_save_opt_symmetric (void *data, void *value)
821 struct client_s *client = data;
823 client->opts |= OPT_SYMMETRIC;
824 return 0;
827 /* Tests that the keys in new_keys are also in old_keys. */
828 static gpg_error_t
829 compare_keys (char **new_keys, char **old_keys)
831 char **o;
833 if (!old_keys || !*old_keys)
834 return 0;
836 crypto_keyid_to_16b (new_keys);
838 for (o = old_keys; *o; o++)
840 char **n;
842 for (n = new_keys; *n; n++)
844 if (!strcmp (*n, *o))
845 break;
848 if (!*n)
849 return GPG_ERR_NOT_FOUND;
852 return 0;
855 static gpg_error_t
856 inquire_keyid (struct client_s *client, uint32_t opt)
858 gpg_error_t rc;
859 unsigned char *result = NULL;
860 size_t len;
861 char *s;
862 char ***orig;
863 char ***save;
865 if (opt == OPT_INQUIRE_KEYID)
867 s = "INQUIRE_KEYID";
868 orig = &client->crypto->pubkey;
869 save = &client->crypto->save.pubkey;
871 else
873 s = "INQUIRE_SIGN_KEYID";
874 orig = &client->crypto->sigkey;
875 save = &client->crypto->save.sigkey;
878 rc = assuan_inquire (client->ctx, s, &result, &len, 0);
879 if (!rc)
881 char **dst = NULL;
883 rc = parse_save_opt_keyid_common (client, (const char **)*orig,
884 (char *)result, &dst);
885 if (!rc)
887 if (!dst && opt == OPT_INQUIRE_SIGN_KEYID
888 && client->opts & OPT_SYMMETRIC)
889 client->opts |= OPT_NO_SIGNER;
891 *save = dst;
895 xfree (result);
896 return rc;
899 /* The caching test of gpg-agent is both a blessing and a curse. When a key
900 * lookup in its cache fails it tries the last successful key to be unlocked.
901 * This prevents pwmd from successfully clearing a signing key since the
902 * previous key, which more than likely belongs to the same keypair as the
903 * encryption/decryption key, will have the passphrase cached and therefore the
904 * signing key also cached. So we need to clear both the signing and encryption
905 * keys to get the effect of requiring a passphrase when generating a new
906 * keypair for an existing data file, or when the "require_save_key"
907 * configuration parameter is set. The "require_save_key" parameter is mostly a
908 * failsafe of the gpg-agent option --ignore-cache-for-signing since
909 * some/most/all users may fail to set it.
911 static gpg_error_t
912 save_command (assuan_context_t ctx, char *line)
914 struct client_s *client = assuan_get_pointer (ctx);
915 struct cache_data_s *cdata = NULL;
916 gpg_error_t rc = 0, cached = 0;
917 int defer = 0;
918 struct stat st;
919 struct argv_s *args[] = {
920 &(struct argv_s) {"keyid", OPTION_TYPE_ARG, parse_save_opt_keyid},
921 &(struct argv_s) {"sign-keyid", OPTION_TYPE_OPTARG,
922 parse_save_opt_sign_keyid},
923 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
924 parse_save_opt_inquire_keyparam },
925 &(struct argv_s) {"inquire-keyid", OPTION_TYPE_NOARG,
926 parse_save_opt_inquire_keyid },
927 &(struct argv_s) {"inquire-sign-keyid", OPTION_TYPE_NOARG,
928 parse_save_opt_inquire_sign_keyid },
929 &(struct argv_s) {"symmetric", OPTION_TYPE_NOARG,
930 parse_save_opt_symmetric },
931 NULL
934 crypto_free_save (&client->crypto->save);
936 if (!(client->flags & FLAG_NEW))
938 rc = crypto_is_symmetric (client->filename);
939 if (!rc || rc == GPG_ERR_BAD_DATA)
941 if (!rc)
942 client->opts |= OPT_SYMMETRIC;
944 rc = 0; // PKI
948 if (rc)
949 return send_error (ctx, rc);
951 rc = parse_options (&line, args, client, 0);
952 if (rc)
953 return send_error (ctx, rc);
955 if (config_get_boolean (client->filename, "require_save_key"))
956 client->opts |= OPT_ASK;
958 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
959 return send_error (ctx, gpg_error_from_errno (errno));
961 if (errno != ENOENT && !S_ISREG (st.st_mode))
963 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
964 return send_error (ctx, GPG_ERR_ENOANO);
967 if (!rc)
968 cached = rc = cache_iscached (client->filename, &defer, 0, 1);
970 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
971 rc = 0;
972 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
973 rc = 0;
975 if (rc)
976 return send_error (ctx, rc);
978 /* Specifying both a recipient and symmetric encryption is an error. */
979 if (client->opts & OPT_INQUIRE_KEYID && client->opts & OPT_SYMMETRIC)
981 return send_error (ctx, GPG_ERR_CONFLICT);
983 else if (client->opts & OPT_INQUIRE && client->opts & OPT_SYMMETRIC)
985 return send_error (ctx, GPG_ERR_CONFLICT);
987 /* Existing file with a recipient and wanting symmetric is an error. */
988 else if (client->opts & OPT_SYMMETRIC && client->crypto->save.pubkey)
990 return send_error (ctx, GPG_ERR_CONFLICT);
992 else if (((client->opts & OPT_INQUIRE_KEYID)
993 || (client->opts & OPT_INQUIRE_SIGN_KEYID)))
995 if (client->opts & OPT_INQUIRE)
996 return send_error (ctx, GPG_ERR_CONFLICT);
998 if (client->opts & OPT_INQUIRE_KEYID)
999 rc = inquire_keyid (client, OPT_INQUIRE_KEYID);
1001 if (!rc && (client->opts & OPT_INQUIRE_SIGN_KEYID))
1002 rc = inquire_keyid (client, OPT_INQUIRE_SIGN_KEYID);
1004 else if ((client->crypto->save.pubkey || client->crypto->save.sigkey)
1005 && client->opts & OPT_INQUIRE)
1006 return send_error (ctx, GPG_ERR_CONFLICT);
1008 if (!rc)
1010 client->crypto->keyfile = config_get_string (client->filename,
1011 "passphrase_file");
1012 rc = crypto_init_ctx (client->crypto, (client->flags & FLAG_NO_PINENTRY),
1013 client->crypto->keyfile);
1016 if (!rc && (client->opts & OPT_INQUIRE))
1018 /* Require a passphrase when generating a new key pair for an existing
1019 * file.
1021 if (!(client->flags & FLAG_NEW))
1023 rc = cache_clear_agent_keys (client->filename, 1, 1);
1024 if (!rc)
1026 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
1027 client->opts &= ~OPT_ASK;
1031 if (!rc)
1033 unsigned char *params = NULL;
1034 size_t len;
1036 rc = assuan_inquire (client->ctx, "KEYPARAM", &params, &len, 0);
1037 if (!rc)
1039 client->crypto->flags |= CRYPTO_FLAG_NEWFILE;
1040 pthread_cleanup_push ((void *)xfree, params);
1041 rc = crypto_genkey (client, client->crypto, params);
1042 pthread_cleanup_pop (1);
1046 else if (!rc && (client->flags & FLAG_NEW))
1048 if (!client->crypto->save.pubkey && !client->crypto->save.sigkey
1049 && !(client->opts & OPT_SYMMETRIC))
1051 char *params = crypto_default_key_params ();
1053 if (!params)
1054 rc = GPG_ERR_ENOMEM;
1056 if (!rc)
1058 client->crypto->flags |= CRYPTO_FLAG_NEWFILE;
1059 pthread_cleanup_push ((void *)xfree, params);
1060 rc = crypto_genkey (client, client->crypto,
1061 (unsigned char *)params);
1062 pthread_cleanup_pop (1);
1065 else
1067 if (!client->crypto->save.pubkey && client->crypto->save.sigkey
1068 && !(client->opts & OPT_SYMMETRIC))
1069 client->crypto->save.pubkey = strv_dup (client->crypto->save.sigkey);
1071 if (client->crypto->save.pubkey && !client->crypto->save.sigkey)
1072 client->crypto->save.sigkey = strv_dup (client->crypto->save.pubkey);
1075 else if (!rc)
1077 cdata = cache_get_data (client->filename);
1078 if (cdata)
1080 if (client->crypto->save.pubkey)
1081 rc = compare_keys (client->crypto->save.pubkey, cdata->pubkey);
1083 /* Always allow a signer for symmetric data files. */
1084 if (!rc && client->crypto->save.sigkey
1085 && !(client->opts & OPT_SYMMETRIC))
1086 rc = compare_keys (client->crypto->save.sigkey, cdata->sigkey);
1088 /* Prevent saving to a recipient who is not in the original recipient
1089 * list without a passphrase. */
1090 if (rc || (client->crypto->sigkey && client->opts & OPT_NO_SIGNER))
1091 client->opts |= OPT_ASK;
1093 if (rc == GPG_ERR_NOT_FOUND)
1095 rc = peer_is_invoker (client);
1096 if (rc == GPG_ERR_EACCES)
1097 rc = GPG_ERR_FORBIDDEN;
1100 if (!client->crypto->save.pubkey
1101 && !(client->opts & OPT_SYMMETRIC))
1102 client->crypto->save.pubkey = strv_dup (cdata->pubkey);
1104 if (!rc && !client->crypto->save.sigkey && cdata->sigkey
1105 && !(client->opts & OPT_NO_SIGNER))
1106 client->crypto->save.sigkey = strv_dup (cdata->sigkey);
1107 else if (!rc && !client->crypto->save.sigkey
1108 && (client->opts & OPT_NO_SIGNER)
1109 && !(client->opts & OPT_SYMMETRIC))
1110 rc = GPG_ERR_NO_SIGNATURE_SCHEME;
1112 else
1114 client->crypto->save.pubkey = strv_dup (client->crypto->pubkey);
1115 client->crypto->save.sigkey = strv_dup (client->crypto->sigkey);
1119 if (!rc && !(client->flags & FLAG_NEW))
1121 /* Fixes {NEW_,SIGN_}PASSPHRASE inquire keywords. See passphrase_cb(). */
1122 if (client->opts & OPT_SYMMETRIC)
1123 client->crypto->flags |= CRYPTO_FLAG_PASSWD;
1125 if (client->opts & OPT_ASK)
1127 rc = cache_clear_agent_keys (client->filename, 1, 1);
1128 if (!rc)
1129 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
1133 if (!rc && client->opts & OPT_SYMMETRIC)
1134 client->crypto->flags |= CRYPTO_FLAG_PASSWD_NEW;
1136 if (!rc)
1137 rc = xml_update_element_mtime (client, xmlDocGetRootElement (client->doc));
1139 if (!rc)
1141 int size;
1143 xmlDocDumpFormatMemory (client->doc, &client->crypto->plaintext, &size,
1145 if (size > 0)
1146 client->crypto->plaintext_size = (size_t) size;
1147 else
1148 rc = GPG_ERR_ENOMEM;
1150 if (!rc)
1152 client->crypto->flags |= (client->opts & OPT_SYMMETRIC) ? CRYPTO_FLAG_SYMMETRIC : 0;
1153 client->crypto->flags |= (client->flags & FLAG_NEW) ? CRYPTO_FLAG_NEWFILE : 0;
1154 client->crypto->flags |= !(client->opts & OPT_SYMMETRIC) ? CRYPTO_FLAG_PASSWD_SIGN : 0;
1155 rc = crypto_encrypt (client, client->crypto);
1156 client->crypto->flags &= ~CRYPTO_FLAG_SYMMETRIC;
1159 if (!rc)
1161 rc = crypto_write_file (client->crypto);
1162 if (!rc)
1164 if (!cached) // no error
1165 cdata = cache_get_data (client->filename);
1168 if (!rc)
1170 rc = cache_encrypt (client->crypto);
1171 if (rc)
1173 cache_clear (NULL, client->filename, 1);
1174 strv_free (client->crypto->pubkey);
1175 client->crypto->pubkey = strv_dup (client->crypto->save.pubkey);
1176 strv_free (client->crypto->sigkey);
1177 client->crypto->sigkey = strv_dup (client->crypto->save.sigkey);
1178 client->flags &= ~(FLAG_NEW);
1182 if (!rc)
1184 int timeout = config_get_integer (client->filename,
1185 "cache_timeout");
1187 free_cache_data_once (cdata);
1188 cdata = xcalloc (1, sizeof (struct cache_data_s));
1189 cdata->doc = client->crypto->plaintext;
1190 client->crypto->plaintext = NULL;
1191 cdata->size = client->crypto->plaintext_size;
1192 client->crypto->plaintext_size = 0;
1193 cdata->pubkey = client->crypto->save.pubkey;
1194 client->crypto->save.pubkey = NULL;
1195 cdata->sigkey = client->crypto->save.sigkey;
1196 client->crypto->save.sigkey = NULL;
1198 /* Update in case the cache entry expires the next SAVE may not
1199 * have any known keys. */
1200 strv_free (client->crypto->pubkey);
1201 client->crypto->pubkey = strv_dup (cdata->pubkey);
1202 strv_free (client->crypto->sigkey);
1203 client->crypto->sigkey = strv_dup (cdata->sigkey);
1205 if (!cached) // no error and wont increment refcount
1206 rc = cache_set_data (client->filename, cdata);
1207 else
1208 rc = cache_add_file (client->filename, cdata, timeout);
1210 if (!rc && (cached || (client->flags & FLAG_NEW)))
1211 send_status_all (STATUS_CACHE, NULL);
1213 if (!rc)
1215 rc = update_checksum (client);
1216 client->flags &= ~(FLAG_NEW);
1222 crypto_free_non_keys (client->crypto);
1223 return send_error (ctx, rc);
1226 static gpg_error_t
1227 do_delete (assuan_context_t ctx, char *line)
1229 struct client_s *client = assuan_get_pointer (ctx);
1230 struct xml_request_s *req;
1231 xmlNodePtr n;
1232 gpg_error_t rc;
1234 rc = xml_new_request (client, line, XML_CMD_DELETE, &req);
1235 if (rc)
1236 return rc;
1238 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1239 xml_free_request (req);
1240 if (rc)
1241 return rc;
1243 rc = xml_is_element_owner (client, n);
1244 if (!rc)
1245 rc = xml_unlink_node (client, n);
1247 return rc;
1250 static gpg_error_t
1251 delete_command (assuan_context_t ctx, char *line)
1253 struct client_s *client = assuan_get_pointer (ctx);
1254 gpg_error_t rc;
1255 struct argv_s *args[] = {
1256 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1257 NULL
1260 rc = parse_options (&line, args, client, 1);
1261 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1262 rc = GPG_ERR_SYNTAX;
1263 if (rc)
1264 return send_error (ctx, rc);
1266 if (client->opts & OPT_INQUIRE)
1268 unsigned char *result;
1269 size_t len;
1271 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1272 if (rc)
1273 return send_error (ctx, rc);
1275 pthread_cleanup_push ((void *)xfree, result);
1276 rc = do_delete (ctx, (char *)result);
1277 pthread_cleanup_pop (1);
1279 else
1280 rc = do_delete (ctx, line);
1282 return send_error (ctx, rc);
1285 static gpg_error_t
1286 update_element_expirey (struct client_s *client, xmlNodePtr n)
1288 gpg_error_t rc = 0;
1289 xmlChar *expire, *incr;
1290 char *p;
1291 time_t e, e_orig, i;
1292 time_t now = time (NULL);
1294 expire = xml_attribute_value (n, (xmlChar *)"expire");
1295 if (!expire)
1296 return 0;
1298 errno = 0;
1299 e = strtoul ((char *)expire, &p, 10);
1300 if (errno || *p || e == ULONG_MAX || *expire == '-')
1302 xmlFree (expire);
1303 rc = GPG_ERR_ERANGE;
1304 goto fail;
1307 xmlFree (expire);
1308 incr = xml_attribute_value (n, (xmlChar *)"expire_increment");
1309 if (!incr)
1310 return 0;
1312 i = strtoul ((char *)incr, &p, 10);
1313 if (errno || *p || i == ULONG_MAX || *incr == '-')
1315 xmlFree (incr);
1316 rc = GPG_ERR_ERANGE;
1317 goto fail;
1320 xmlFree (incr);
1321 e_orig = e;
1322 e = now + i;
1323 p = str_asprintf ("%lu", e);
1324 if (!p)
1326 rc = GPG_ERR_ENOMEM;
1327 goto fail;
1330 rc = xml_add_attribute (client, n, "expire", p);
1331 xfree (p);
1332 if (!rc)
1333 rc = send_status (client->ctx, STATUS_EXPIRE, "%lu %lu", e_orig, e);
1335 fail:
1336 return rc;
1339 static gpg_error_t
1340 store_command (assuan_context_t ctx, char *line)
1342 struct client_s *client = assuan_get_pointer (ctx);
1343 gpg_error_t rc;
1344 size_t len;
1345 unsigned char *result;
1346 xmlNodePtr n, parent;
1347 int has_content;
1348 char *content = NULL;
1349 struct xml_request_s *req;
1351 if (line && *line)
1352 return send_error (ctx, GPG_ERR_SYNTAX);
1354 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1355 if (rc)
1356 return send_error (ctx, rc);
1358 rc = xml_new_request (client, (char *)result, XML_CMD_STORE, &req);
1359 xfree (result);
1360 if (rc)
1361 return send_error (ctx, rc);
1363 /* Prevent passing the element content around to save some memory. */
1364 len = strv_length (req->args);
1365 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1366 if (*(req->args+1) && !xml_valid_element_path (req->args, has_content))
1368 xml_free_request (req);
1369 return send_error (ctx, GPG_ERR_INV_VALUE);
1372 if (has_content || !*req->args[len-1])
1374 has_content = 1;
1375 content = req->args[len-1];
1376 req->args[len-1] = NULL;
1379 if (strv_length (req->args) > 1)
1381 rc = xml_check_recursion (client, req);
1382 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
1383 goto fail;
1386 parent = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1388 if (!rc && len > 1)
1390 rc = xml_is_element_owner (client, parent);
1391 if (!rc)
1393 n = xml_find_text_node (parent->children);
1394 if (n)
1395 xmlNodeSetContent (n, (xmlChar *) content);
1396 else
1397 xmlNodeAddContent (parent, (xmlChar *) content);
1399 (void)xml_update_element_mtime (client, parent);
1400 (void)update_element_expirey (client, parent);
1404 fail:
1405 xfree (content);
1406 xml_free_request (req);
1407 return send_error (ctx, rc);
1410 static gpg_error_t
1411 xfer_data (assuan_context_t ctx, const char *line, int total)
1413 struct client_s *client = assuan_get_pointer (ctx);
1414 int to_send;
1415 int sent = 0;
1416 gpg_error_t rc = 0;
1417 int progress = config_get_integer ("global", "xfer_progress");
1418 int flush = 0;
1420 if (!(client->flags & FLAG_LOCK_CMD))
1421 rc = unlock_file_mutex (client, 0);
1422 if (rc && rc != GPG_ERR_NOT_LOCKED)
1423 return rc;
1425 progress =
1426 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1427 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1428 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1430 if (rc)
1431 return rc;
1433 again:
1436 if (sent + to_send > total)
1437 to_send = total - sent;
1439 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1440 flush ? 0 : to_send);
1441 if (!rc)
1443 sent += flush ? 0 : to_send;
1445 if ((progress && !(sent % progress) && sent != total) ||
1446 (sent == total && flush))
1447 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1449 if (!flush && !rc && sent == total)
1451 flush = 1;
1452 goto again;
1456 while (!rc && sent < total);
1458 return rc;
1461 static gpg_error_t
1462 do_get (assuan_context_t ctx, char *line)
1464 struct client_s *client = assuan_get_pointer (ctx);
1465 gpg_error_t rc;
1466 struct xml_request_s *req;
1467 xmlNodePtr n;
1469 rc = xml_new_request (client, line, XML_CMD_NONE, &req);
1470 if (rc)
1471 return rc;
1473 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1474 if (!rc)
1476 if (n && n->children)
1478 xmlNodePtr tmp = n;
1480 n = xml_find_text_node (n->children);
1481 if (!n || !n->content || !*n->content)
1482 rc = GPG_ERR_NO_DATA;
1483 else
1485 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1486 if (!rc)
1488 xmlChar *expire = xml_attribute_value (tmp,
1489 (xmlChar *)"expire");
1490 if (expire)
1492 time_t now = time (NULL);
1493 time_t e;
1494 char *p;
1496 errno = 0;
1497 e = strtoul ((char *)expire, &p, 10);
1498 if (errno || *p || e == ULONG_MAX || *expire == '-')
1499 log_write (_("invalid expire attribute value: %s"),
1500 expire);
1501 else if (now >= e)
1502 rc = send_status (ctx, STATUS_EXPIRE, "%lu 0", e);
1504 xmlFree (expire);
1509 else
1510 rc = GPG_ERR_NO_DATA;
1513 xml_free_request (req);
1514 return rc;
1517 static gpg_error_t
1518 get_command (assuan_context_t ctx, char *line)
1520 struct client_s *client = assuan_get_pointer (ctx);
1521 gpg_error_t rc;
1522 struct argv_s *args[] = {
1523 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1524 NULL
1527 rc = parse_options (&line, args, client, 1);
1528 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1529 rc = GPG_ERR_SYNTAX;
1530 if (rc)
1531 return send_error (ctx, rc);
1533 if (client->opts & OPT_INQUIRE)
1535 unsigned char *result;
1536 size_t len;
1538 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1539 if (rc)
1540 return send_error (ctx, rc);
1542 pthread_cleanup_push ((void *)xfree, result);
1543 rc = do_get (ctx, (char *)result);
1544 pthread_cleanup_pop (1);
1546 else
1547 rc = do_get (ctx, line);
1549 return send_error (ctx, rc);
1552 static void list_command_cleanup1 (void *arg);
1553 static gpg_error_t
1554 realpath_command (assuan_context_t ctx, char *line)
1556 gpg_error_t rc;
1557 char **realpath = NULL;
1558 struct client_s *client = assuan_get_pointer (ctx);
1559 struct argv_s *args[] = {
1560 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1561 NULL
1564 rc = parse_options (&line, args, client, 1);
1565 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1566 rc = GPG_ERR_SYNTAX;
1567 if (rc)
1568 return send_error (ctx, rc);
1570 if (client->opts & OPT_INQUIRE)
1572 unsigned char *result;
1573 size_t len;
1575 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1576 if (rc)
1577 return send_error (ctx, rc);
1579 pthread_cleanup_push ((void *)xfree, result);
1580 (void)xml_resolve_path (client, client->doc, result, &realpath, &rc);
1581 pthread_cleanup_pop (1);
1583 else
1584 (void)xml_resolve_path (client, client->doc, (unsigned char *)line,
1585 &realpath, &rc);
1587 if (!rc)
1589 char *tmp = strv_join ("\t", realpath);
1591 strv_free (realpath);
1592 if (!tmp)
1593 return send_error (ctx, GPG_ERR_ENOMEM);
1595 pthread_cleanup_push ((void *)xfree, tmp);
1596 rc = xfer_data (ctx, tmp, strlen (tmp));
1597 pthread_cleanup_pop (1);
1600 return send_error (ctx, rc);
1603 static void
1604 list_command_cleanup1 (void *arg)
1606 if (arg)
1607 string_free ((struct string_s *) arg, 1);
1610 static gpg_error_t
1611 parse_list_opt_recurse (void *data, void *value)
1613 struct client_s *client = data;
1615 client->opts |= OPT_LIST_RECURSE;
1616 return 0;
1619 static gpg_error_t
1620 parse_opt_verbose (void *data, void *value)
1622 struct client_s *client = data;
1624 client->opts |= OPT_VERBOSE;
1625 return 0;
1628 static gpg_error_t
1629 list_path_once (struct client_s *client, char *line,
1630 struct element_list_s *elements, struct string_s *result)
1632 gpg_error_t rc;
1634 rc = xml_create_path_list (client, client->doc, NULL, elements, line, 0);
1635 if (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES)
1636 rc = 0;
1638 if (!rc)
1640 int total = slist_length (elements->list);
1642 if (total)
1644 int i;
1646 for (i = 0; i < total; i++)
1648 char *tmp = slist_nth_data (elements->list, i);
1650 string_append_printf (result, "%s%s", tmp,
1651 i + 1 == total ? "" : "\n");
1654 else
1655 rc = GPG_ERR_NO_DATA;
1658 return rc;
1661 static gpg_error_t
1662 do_list (assuan_context_t ctx, char *line)
1664 struct client_s *client = assuan_get_pointer (ctx);
1665 gpg_error_t rc = GPG_ERR_NO_DATA;
1666 struct element_list_s *elements = NULL;
1668 if (!line || !*line)
1670 struct string_s *str = string_new (NULL);
1671 xmlNodePtr n;
1673 if (!str)
1674 return GPG_ERR_ENOMEM;
1676 pthread_cleanup_push ((void *)list_command_cleanup1, str);
1677 n = xmlDocGetRootElement (client->doc);
1678 for (n = n->children; n; n = n->next)
1680 xmlChar *name;
1682 if (n->type != XML_ELEMENT_NODE)
1683 continue;
1685 name = xmlGetProp (n, (xmlChar *)"_name");
1686 if (!name)
1688 rc = GPG_ERR_ENOMEM;
1689 break;
1692 elements = xcalloc (1, sizeof (struct element_list_s));
1693 if (!elements)
1695 xmlFree (name);
1696 rc = GPG_ERR_ENOMEM;
1697 break;
1700 elements->prefix = string_new (NULL);
1701 if (!elements->prefix)
1703 xmlFree (name);
1704 xml_free_element_list (elements);
1705 rc = GPG_ERR_ENOMEM;
1706 break;
1709 elements->recurse = client->opts & OPT_LIST_RECURSE;
1710 elements->root_only = !elements->recurse;
1711 pthread_cleanup_push ((void *)xml_free_element_list, elements);
1712 rc = list_path_once (client, (char *)name, elements, str);
1713 xmlFree (name);
1714 pthread_cleanup_pop (1);
1715 if (rc)
1716 break;
1718 if (n->next)
1719 string_append (str, "\n");
1722 if (!rc)
1723 rc = xfer_data (ctx, str->str, str->len);
1725 pthread_cleanup_pop (1);
1726 return rc;
1729 elements = xcalloc (1, sizeof (struct element_list_s));
1730 if (!elements)
1731 return GPG_ERR_ENOMEM;
1733 elements->prefix = string_new (NULL);
1734 if (!elements->prefix)
1736 xml_free_element_list (elements);
1737 return GPG_ERR_ENOMEM;
1740 elements->recurse = client->opts & OPT_LIST_RECURSE;
1741 pthread_cleanup_push ((void *)xml_free_element_list, elements);
1742 struct string_s *str = string_new (NULL);
1743 pthread_cleanup_push ((void *)list_command_cleanup1, str);
1744 rc = list_path_once (client, line, elements, str);
1745 if (!rc)
1746 rc = xfer_data (ctx, str->str, str->len);
1748 pthread_cleanup_pop (1);
1749 pthread_cleanup_pop (1);
1750 return rc;
1753 static gpg_error_t
1754 list_command (assuan_context_t ctx, char *line)
1756 struct client_s *client = assuan_get_pointer (ctx);
1757 gpg_error_t rc;
1758 struct argv_s *args[] = {
1759 &(struct argv_s) {"recurse", OPTION_TYPE_NOARG, parse_list_opt_recurse},
1760 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1761 /* These are deprecated but kept for backward compatibility with older
1762 * clients. */
1763 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, NULL},
1764 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG, NULL},
1765 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_recurse},
1766 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG, NULL},
1767 NULL
1770 if (disable_list_and_dump == 1)
1771 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1773 rc = parse_options (&line, args, client, 1);
1774 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1775 rc = GPG_ERR_SYNTAX;
1776 if (rc)
1777 return send_error (ctx, rc);
1779 if (client->opts & OPT_INQUIRE)
1781 unsigned char *result;
1782 size_t len;
1784 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1785 if (rc)
1786 return send_error (ctx, rc);
1788 pthread_cleanup_push ((void *)xfree, result);
1789 rc = do_list (ctx, (char *)result);
1790 pthread_cleanup_pop (1);
1792 else
1793 rc = do_list (ctx, line);
1795 return send_error (ctx, rc);
1798 #define RESUMABLE_ERROR(rc) (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES \
1799 || rc == GPG_ERR_ELEMENT_NOT_FOUND || !rc)
1801 * args[0] - element path
1803 static gpg_error_t
1804 attribute_list (assuan_context_t ctx, char **args)
1806 struct client_s *client = assuan_get_pointer (ctx);
1807 char **attrlist = NULL;
1808 int i = 0;
1809 int target = 0;
1810 xmlAttrPtr a;
1811 xmlNodePtr n, an, r;
1812 char *line;
1813 gpg_error_t rc;
1814 struct xml_request_s *req;
1816 if (!args || !args[0] || !*args[0])
1817 return GPG_ERR_SYNTAX;
1819 rc = xml_new_request (client, args[0], XML_CMD_ATTR_LIST, &req);
1820 if (rc)
1821 return rc;
1823 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1824 xml_free_request (req);
1825 if (rc)
1826 return rc;
1828 again:
1829 for (a = n->properties; a; a = a->next)
1831 char **pa;
1833 if (target && xml_reserved_attribute ((char *)a->name))
1834 continue;
1836 pa = xrealloc (attrlist, (i + 2) * sizeof (char *));
1837 if (!pa)
1839 if (attrlist)
1840 strv_free (attrlist);
1842 log_write ("%s(%i): %s", __FILE__, __LINE__,
1843 pwmd_strerror (GPG_ERR_ENOMEM));
1844 return GPG_ERR_ENOMEM;
1847 attrlist = pa;
1848 an = a->children;
1849 attrlist[i] = str_asprintf ("%s %s", (char *) a->name, an && an->content
1850 ? (char *)an->content : "");
1852 if (!attrlist[i])
1854 strv_free (attrlist);
1855 log_write ("%s(%i): %s", __FILE__, __LINE__,
1856 pwmd_strerror (GPG_ERR_ENOMEM));
1857 return GPG_ERR_ENOMEM;
1860 attrlist[++i] = NULL;
1863 if (!attrlist)
1864 return GPG_ERR_NO_DATA;
1866 if (!target)
1868 r = xml_resolve_path (client, client->doc, (xmlChar *)args[0], NULL, &rc);
1869 if (!RESUMABLE_ERROR (rc))
1871 strv_free (attrlist);
1872 return rc;
1874 else if (!rc && r != n)
1876 target = 1;
1877 n = r;
1878 goto again;
1881 rc = 0;
1884 line = strv_join ("\n", attrlist);
1885 if (!line)
1887 log_write ("%s(%i): %s", __FILE__, __LINE__,
1888 pwmd_strerror (GPG_ERR_ENOMEM));
1889 strv_free (attrlist);
1890 return GPG_ERR_ENOMEM;
1893 pthread_cleanup_push ((void *)xfree, line);
1894 pthread_cleanup_push ((void *)req_cleanup, attrlist);
1895 rc = xfer_data (ctx, line, strlen (line));
1896 pthread_cleanup_pop (1);
1897 pthread_cleanup_pop (1);
1898 return rc;
1902 * args[0] - attribute
1903 * args[1] - element path
1905 static gpg_error_t
1906 attribute_delete (struct client_s *client, char **args)
1908 gpg_error_t rc;
1909 xmlNodePtr n;
1911 if (!args || !args[0] || !*args[0] || !args[1] || !*args[1])
1912 return GPG_ERR_SYNTAX;
1914 if (!strcmp (args[0], "_name") || !strcmp (args[0], "target"))
1915 return GPG_ERR_INV_ATTR;
1917 if (!xml_reserved_attribute (args[0]))
1918 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
1919 else
1921 struct xml_request_s *req;
1923 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
1924 if (rc)
1925 return rc;
1927 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1928 xml_free_request (req);
1931 if (!rc)
1932 rc = xml_is_element_owner (client, n);
1934 if (!rc)
1935 rc = xml_delete_attribute (client, n, (xmlChar *) args[0]);
1937 return rc;
1941 * Creates the "target" attribute. When other commands encounter an element
1942 * with this attribute they follow the "target" after appending remaining
1943 * elements from the original element path to the target. The exception is the
1944 * ATTR command that operates on the element itself (for reserved attribute
1945 * names) and not the target.
1947 * If the source element path doesn't exist when using 'ATTR SET target', it is
1948 * created, but the destination element path must exist. This is simliar to the
1949 * ls(1) command: ln -s src dst.
1951 * args[0] - source element path
1952 * args[1] - destination element path
1954 static gpg_error_t
1955 set_target_attribute (struct client_s *client, char **args)
1957 struct xml_request_s *req = NULL;
1958 char **src, **dst;
1959 gpg_error_t rc;
1960 xmlNodePtr n;
1962 if (!args || !args[0] || !args[1])
1963 return GPG_ERR_SYNTAX;
1965 src = str_split (args[0], "\t", 0);
1966 if (!src)
1967 return GPG_ERR_SYNTAX;
1969 if (!xml_valid_element_path (src, 0))
1971 strv_free (src);
1972 return GPG_ERR_INV_VALUE;
1975 dst = str_split (args[1], "\t", 0);
1976 if (!dst)
1978 strv_free (src);
1979 return GPG_ERR_SYNTAX;
1982 // Be sure the destination element path exists. */
1983 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
1984 if (rc)
1985 goto fail;
1987 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1988 if (rc && rc != GPG_ERR_EACCES)
1989 goto fail;
1991 xml_free_request (req);
1992 req = NULL;
1994 if (!strcmp (args[0], args[1]))
1996 rc = GPG_ERR_EEXIST;
1997 goto fail;
2000 rc = xml_new_request (client, args[0], XML_CMD_ATTR_TARGET, &req);
2001 if (rc)
2002 goto fail;
2004 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2005 if (rc && rc != GPG_ERR_EACCES)
2006 goto fail;
2008 if (!rc)
2010 rc = xml_add_attribute (client, n, "target", args[1]);
2011 if (!rc)
2012 rc = xml_unlink_node (client, n->children);
2015 fail:
2016 strv_free (src);
2017 strv_free (dst);
2018 xml_free_request (req);
2019 return rc;
2023 * args[0] - attribute
2024 * args[1] - element path
2026 static gpg_error_t
2027 attribute_get (assuan_context_t ctx, char **args)
2029 struct client_s *client = assuan_get_pointer (ctx);
2030 xmlNodePtr n;
2031 xmlChar *a;
2032 gpg_error_t rc;
2033 struct xml_request_s *req;
2035 if (!args || !args[0] || !*args[0] || !args[1] || !*args[1])
2036 return GPG_ERR_SYNTAX;
2038 if (!xml_reserved_attribute (args[0]))
2039 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
2040 else
2042 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
2043 if (rc)
2044 return rc;
2046 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2047 xml_free_request (req);
2050 if (rc)
2051 return rc;
2053 a = xmlGetProp (n, (xmlChar *) args[0]);
2054 if (!a)
2055 return GPG_ERR_NOT_FOUND;
2057 pthread_cleanup_push ((void *)xmlFree, a);
2059 if (*a)
2060 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
2061 else
2062 rc = GPG_ERR_NO_DATA;
2064 pthread_cleanup_pop (1);
2065 return rc;
2069 * args[0] - attribute
2070 * args[1] - element path
2071 * args[2] - value
2073 static gpg_error_t
2074 attribute_set (struct client_s *client, char **args)
2076 struct xml_request_s *req;
2077 gpg_error_t rc;
2078 xmlNodePtr n;
2080 if (!args || !args[0] || !args[1])
2081 return GPG_ERR_SYNTAX;
2084 * Reserved attribute names.
2086 if (!strcmp (args[0], "_name")) // Use the RENAME command instead
2087 return GPG_ERR_INV_ATTR;
2088 else if (!strcmp (args[0], "target"))
2089 return set_target_attribute (client, args + 1);
2090 else if (!xml_valid_attribute (args[0]))
2091 return GPG_ERR_INV_VALUE;
2093 if (!xml_valid_attribute_value (args[2]))
2094 return GPG_ERR_INV_VALUE;
2096 if (!xml_reserved_attribute (args[0]))
2098 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
2099 if (!rc)
2101 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
2102 if (!rc)
2104 rc = xml_check_recursion (client, req);
2105 xml_free_request (req);
2109 else
2111 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
2112 if (rc)
2113 return rc;
2115 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2116 xml_free_request (req);
2119 if (!rc)
2120 rc = xml_is_element_owner (client, n);
2122 if (!rc)
2123 rc = xml_add_attribute (client, n, args[0], args[2]);
2125 return rc;
2129 * req[0] - command
2130 * req[1] - attribute name or element path if command is LIST
2131 * req[2] - element path
2132 * req[2] - element path or value
2135 static gpg_error_t
2136 do_attr (assuan_context_t ctx, char *line)
2138 struct client_s *client = assuan_get_pointer (ctx);
2139 gpg_error_t rc = 0;
2140 char **req;
2142 req = str_split (line, " ", 4);
2143 if (!req || !req[0] || !req[1])
2145 strv_free (req);
2146 return GPG_ERR_SYNTAX;
2149 pthread_cleanup_push ((void *)req_cleanup, req);
2151 if (strcasecmp (req[0], "SET") == 0)
2152 rc = attribute_set (client, req + 1);
2153 else if (strcasecmp (req[0], "GET") == 0)
2154 rc = attribute_get (ctx, req + 1);
2155 else if (strcasecmp (req[0], "DELETE") == 0)
2156 rc = attribute_delete (client, req + 1);
2157 else if (strcasecmp (req[0], "LIST") == 0)
2158 rc = attribute_list (ctx, req + 1);
2159 else
2160 rc = GPG_ERR_SYNTAX;
2162 client->flags &= ~(FLAG_ACL_IGNORE|FLAG_ACL_ERROR);
2163 pthread_cleanup_pop (1);
2164 return rc;
2167 static gpg_error_t
2168 attr_command (assuan_context_t ctx, char *line)
2170 struct client_s *client = assuan_get_pointer (ctx);
2171 gpg_error_t rc;
2172 struct argv_s *args[] = {
2173 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2174 NULL
2177 rc = parse_options (&line, args, client, 1);
2178 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2179 rc = GPG_ERR_SYNTAX;
2180 if (rc)
2181 return send_error (ctx, rc);
2183 if (client->opts & OPT_INQUIRE)
2185 unsigned char *result;
2186 size_t len;
2188 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2189 if (rc)
2190 return send_error (ctx, rc);
2192 pthread_cleanup_push ((void *)xfree, result);
2193 rc = do_attr (ctx, (char *)result);
2194 pthread_cleanup_pop (1);
2196 else
2197 rc = do_attr (ctx, line);
2199 return send_error (ctx, rc);
2202 static gpg_error_t
2203 parse_iscached_opt_lock (void *data, void *value)
2205 struct client_s *client = data;
2207 (void) value;
2208 client->opts |= OPT_LOCK;
2209 return 0;
2212 static gpg_error_t
2213 parse_iscached_opt_agent (void *data, void *value)
2215 struct client_s *client = data;
2217 (void) value;
2218 client->opts |= OPT_CACHE_AGENT;
2219 return 0;
2222 static gpg_error_t
2223 parse_iscached_opt_sign (void *data, void *value)
2225 struct client_s *client = data;
2227 (void) value;
2228 client->opts |= OPT_CACHE_SIGN;
2229 return 0;
2232 static gpg_error_t
2233 iscached_command (assuan_context_t ctx, char *line)
2235 struct client_s *client = assuan_get_pointer (ctx);
2236 gpg_error_t rc;
2237 int defer = 0;
2238 struct argv_s *args[] = {
2239 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2240 &(struct argv_s) {"agent", OPTION_TYPE_NOARG, parse_iscached_opt_agent},
2241 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_iscached_opt_sign},
2242 NULL
2245 if (!line || !*line)
2246 return send_error (ctx, GPG_ERR_SYNTAX);
2248 rc = parse_options (&line, args, client, 1);
2249 if (rc)
2250 return send_error (ctx, rc);
2251 else if (!valid_filename (line))
2252 return send_error (ctx, GPG_ERR_INV_VALUE);
2254 if (!(client->flags & FLAG_OPEN)
2255 && ((client->opts & OPT_CACHE_AGENT) || (client->opts & OPT_CACHE_SIGN)))
2256 return send_error (ctx, GPG_ERR_INV_STATE);
2258 rc = cache_iscached (line, &defer, (client->opts & OPT_CACHE_AGENT),
2259 (client->opts & OPT_CACHE_SIGN));
2260 if (!rc && defer)
2261 rc = GPG_ERR_NO_DATA;
2263 if (!rc)
2265 struct cache_data_s *cdata = cache_get_data (line);
2267 if (cdata)
2269 rc = validate_checksum (client, line, cdata, NULL, NULL);
2270 if (rc == GPG_ERR_CHECKSUM)
2271 rc = GPG_ERR_NO_DATA;
2275 if (client->opts & OPT_LOCK
2276 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2278 gpg_error_t trc = rc;
2280 if (strcmp (line, client->filename))
2281 cleanup_client (client);
2283 xfree (client->filename);
2284 client->filename = str_dup (line);
2285 rc = do_lock (client, 1);
2286 if (!rc)
2287 rc = trc;
2290 return send_error (ctx, rc);
2293 static gpg_error_t
2294 clearcache_command (assuan_context_t ctx, char *line)
2296 struct client_s *client = assuan_get_pointer (ctx);
2297 gpg_error_t rc = 0, all_rc = 0;
2298 int i;
2299 int t;
2300 int all = 0;
2301 struct client_thread_s *once = NULL;
2302 unsigned count;
2304 cache_lock ();
2305 count = cache_file_count ();
2306 MUTEX_LOCK (&cn_mutex);
2307 pthread_cleanup_push ((void *)cleanup_mutex_cb, &cn_mutex);
2309 if (!line || !*line)
2310 all = 1;
2312 t = slist_length (cn_thread_list);
2314 for (i = 0; i < t; i++)
2316 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2317 assuan_peercred_t peer;
2319 if (!thd->cl)
2320 continue;
2322 /* Lock each connected clients' file mutex to prevent any other client
2323 * from accessing the cache entry (the file mutex is locked upon
2324 * command startup). The cache for the entry is not cleared if the
2325 * file mutex is locked by another client to prevent this function
2326 * from blocking. Rather, it returns an error.
2328 if (all)
2330 if (thd->cl->filename)
2332 rc = do_validate_peer (ctx, thd->cl->filename, &peer);
2333 /* The current client doesn't have permission to open the other
2334 * filename do to "access" configuration parameter in a filename
2335 * section. Since we are clearning all cache entries the error
2336 * will be returned later during cache_clear(). */
2337 if (rc)
2339 rc = 0;
2340 continue;
2343 else // Idle client without opened file?
2344 continue;
2346 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->filename, -1, 0, -1);
2347 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2349 /* The current client owns the lock. */
2350 if (pthread_equal (pthread_self (), thd->tid))
2351 rc = 0;
2352 else
2354 if (cache_iscached (thd->cl->filename, NULL, 0, 0)
2355 == GPG_ERR_NO_DATA)
2357 rc = 0;
2358 continue;
2361 /* The cache entry will be cleared when the other client
2362 * disconnects and cache_timer_thread() does its thing. */
2363 cache_defer_clear (thd->cl->filename);
2366 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2368 rc = 0;
2369 continue;
2372 if (!rc)
2374 rc = cache_clear (NULL, thd->cl->filename, 1);
2375 cache_unlock_mutex (thd->cl->filename, 0);
2378 if (rc)
2379 all_rc = rc;
2381 rc = 0;
2383 else
2385 /* A single data filename was specified. Lock only this data file
2386 * mutex and free the cache entry. */
2387 rc = do_validate_peer (ctx, line, &peer);
2388 if (rc == GPG_ERR_FORBIDDEN)
2389 rc = peer_is_invoker (client);
2391 if (rc == GPG_ERR_EACCES)
2392 rc = GPG_ERR_FORBIDDEN;
2394 if (!rc && thd->cl->filename && !strcmp (thd->cl->filename , line))
2396 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->filename, -1, 0, -1);
2397 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2399 /* The current client owns the lock. */
2400 if (pthread_equal (pthread_self (), thd->tid))
2401 rc = 0;
2404 if (!rc)
2406 once = thd;
2407 rc = cache_clear (NULL, thd->cl->filename, 1);
2408 cache_unlock_mutex (thd->cl->filename, 0);
2410 else
2411 cache_defer_clear (thd->cl->filename);
2413 /* Found a client with the opened file. The cache entry has been
2414 * either cleared (self) or defered to be cleared. */
2415 break;
2420 /* Only connected clients' cache entries have been cleared. Now clear any
2421 * remaining cache entries without clients but only if there wasn't an
2422 * error from above since this would defeat the locking check of the
2423 * remaining entries. */
2424 if (all && !all_rc && cache_file_count ())
2426 rc = cache_clear (client, NULL, 1);
2427 if (rc == GPG_ERR_EACCES)
2428 rc = GPG_ERR_FORBIDDEN;
2431 /* No clients are using the specified file. */
2432 else if (!all_rc && !rc && !once)
2433 rc = cache_clear (NULL, line, 1);
2435 /* Release the connection mutex. */
2436 pthread_cleanup_pop (1);
2438 if (!rc || (cache_file_count () && count != cache_file_count ()))
2439 send_status_all (STATUS_CACHE, NULL);
2441 cache_unlock ();
2443 /* One or more files were locked while clearing all cache entries. */
2444 if (all_rc)
2445 rc = all_rc;
2447 return send_error (ctx, rc);
2450 static gpg_error_t
2451 cachetimeout_command (assuan_context_t ctx, char *line)
2453 int timeout;
2454 char **req = str_split (line, " ", 0);
2455 char *p;
2456 gpg_error_t rc = 0;
2457 assuan_peercred_t peer;
2459 if (!req || !*req || !req[1])
2461 strv_free (req);
2462 return send_error (ctx, GPG_ERR_SYNTAX);
2465 errno = 0;
2466 timeout = (int) strtol (req[1], &p, 10);
2467 if (errno != 0 || *p || timeout < -1)
2469 strv_free (req);
2470 return send_error (ctx, GPG_ERR_SYNTAX);
2473 rc = do_validate_peer (ctx, req[0], &peer);
2474 if (!rc)
2476 rc = cache_set_timeout (req[0], timeout);
2477 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2479 rc = 0;
2480 MUTEX_LOCK (&rcfile_mutex);
2481 config_set_int_param (&global_config, req[0], "cache_timeout",
2482 req[1]);
2483 MUTEX_UNLOCK (&rcfile_mutex);
2487 strv_free (req);
2488 return send_error (ctx, rc);
2491 static gpg_error_t
2492 dump_command (assuan_context_t ctx, char *line)
2494 xmlChar *xml;
2495 int len;
2496 struct client_s *client = assuan_get_pointer (ctx);
2497 gpg_error_t rc;
2499 if (disable_list_and_dump == 1)
2500 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2502 if (line && *line)
2503 return send_error (ctx, GPG_ERR_SYNTAX);
2505 rc = peer_is_invoker(client);
2506 if (rc)
2507 return send_error (ctx, rc);
2509 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2511 if (!xml)
2513 log_write ("%s(%i): %s", __FILE__, __LINE__,
2514 pwmd_strerror (GPG_ERR_ENOMEM));
2515 return send_error (ctx, GPG_ERR_ENOMEM);
2518 pthread_cleanup_push ((void *)xmlFree, xml);
2519 rc = xfer_data (ctx, (char *) xml, len);
2520 pthread_cleanup_pop (1);
2521 return send_error (ctx, rc);
2524 static gpg_error_t
2525 getconfig_command (assuan_context_t ctx, char *line)
2527 struct client_s *client = assuan_get_pointer (ctx);
2528 gpg_error_t rc = 0;
2529 char filename[255] = { 0 }, param[747] = { 0 };
2530 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2532 if (!line || !*line)
2533 return send_error (ctx, GPG_ERR_SYNTAX);
2535 if (strchr (line, ' '))
2537 sscanf (line, " %254[^ ] %746c", filename, param);
2538 paramp = param;
2539 fp = filename;
2542 if (fp && !valid_filename (fp))
2543 return send_error (ctx, GPG_ERR_INV_VALUE);
2545 paramp = str_down (paramp);
2546 if (!strcmp (paramp, "passphrase"))
2547 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2549 p = config_get_value (fp ? fp : "global", paramp);
2550 if (!p)
2551 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2553 tmp = expand_homedir (p);
2554 xfree (p);
2555 if (!tmp)
2557 log_write ("%s(%i): %s", __FILE__, __LINE__,
2558 pwmd_strerror (GPG_ERR_ENOMEM));
2559 return send_error (ctx, GPG_ERR_ENOMEM);
2562 p = tmp;
2563 pthread_cleanup_push ((void *)xfree, p);
2564 rc = xfer_data (ctx, p, strlen (p));
2565 pthread_cleanup_pop (1);
2566 return send_error (ctx, rc);
2569 struct xpath_s
2571 xmlXPathContextPtr xp;
2572 xmlXPathObjectPtr result;
2573 xmlBufferPtr buf;
2574 char **req;
2577 static void
2578 xpath_command_cleanup (void *arg)
2580 struct xpath_s *xpath = arg;
2582 if (!xpath)
2583 return;
2585 req_cleanup (xpath->req);
2587 if (xpath->buf)
2588 xmlBufferFree (xpath->buf);
2590 if (xpath->result)
2591 xmlXPathFreeObject (xpath->result);
2593 if (xpath->xp)
2594 xmlXPathFreeContext (xpath->xp);
2597 static gpg_error_t
2598 do_xpath (assuan_context_t ctx, char *line)
2600 gpg_error_t rc;
2601 struct client_s *client = assuan_get_pointer (ctx);
2602 struct xpath_s _x = { 0 };
2603 struct xpath_s *xpath = &_x;
2605 if (!line || !*line)
2606 return GPG_ERR_SYNTAX;
2608 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2610 if (strv_printf (&xpath->req, "%s", line) == 0)
2611 return GPG_ERR_ENOMEM;
2614 xpath->xp = xmlXPathNewContext (client->doc);
2615 if (!xpath->xp)
2617 rc = GPG_ERR_BAD_DATA;
2618 goto fail;
2621 xpath->result =
2622 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2623 if (!xpath->result)
2625 rc = GPG_ERR_BAD_DATA;
2626 goto fail;
2629 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2631 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2632 goto fail;
2635 rc = xml_recurse_xpath_nodeset (client, client->doc,
2636 xpath->result->nodesetval,
2637 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2638 NULL);
2639 if (rc)
2640 goto fail;
2641 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2643 rc = GPG_ERR_NO_DATA;
2644 goto fail;
2646 else if (xpath->req[1])
2648 rc = 0;
2649 goto fail;
2652 pthread_cleanup_push ((void *)xpath_command_cleanup, &xpath);
2653 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2654 xmlBufferLength (xpath->buf));
2655 pthread_cleanup_pop (0);
2656 fail:
2657 xpath_command_cleanup (xpath);
2658 return rc;
2661 static gpg_error_t
2662 xpath_command (assuan_context_t ctx, char *line)
2664 struct client_s *client = assuan_get_pointer (ctx);
2665 gpg_error_t rc;
2666 struct argv_s *args[] = {
2667 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2668 NULL
2671 if (disable_list_and_dump == 1)
2672 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2674 rc = peer_is_invoker(client);
2675 if (rc)
2676 return send_error (ctx, rc);
2678 rc = parse_options (&line, args, client, 1);
2679 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2680 rc = GPG_ERR_SYNTAX;
2681 if (rc)
2682 return send_error (ctx, rc);
2684 if (client->opts & OPT_INQUIRE)
2686 unsigned char *result;
2687 size_t len;
2689 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2690 if (rc)
2691 return send_error (ctx, rc);
2693 pthread_cleanup_push ((void *)xfree, result);
2694 rc = do_xpath (ctx, (char *)result);
2695 pthread_cleanup_pop (1);
2697 else
2698 rc = do_xpath (ctx, line);
2700 return send_error (ctx, rc);
2703 static gpg_error_t
2704 do_xpathattr (assuan_context_t ctx, char *line)
2706 struct client_s *client = assuan_get_pointer (ctx);
2707 gpg_error_t rc;
2708 char **req = NULL;
2709 int cmd = 0; //SET
2710 struct xpath_s _x = { 0 };
2711 struct xpath_s *xpath = &_x;
2713 if (!line || !*line)
2714 return GPG_ERR_SYNTAX;
2716 if ((req = str_split (line, " ", 3)) == NULL)
2717 return GPG_ERR_ENOMEM;
2719 if (!req[0])
2721 rc = GPG_ERR_SYNTAX;
2722 goto fail;
2725 if (!strcasecmp (req[0], "SET"))
2726 cmd = 0;
2727 else if (!strcasecmp (req[0], "DELETE"))
2728 cmd = 1;
2729 else
2731 rc = GPG_ERR_SYNTAX;
2732 goto fail;
2735 if (!req[1] || !req[2])
2737 rc = GPG_ERR_SYNTAX;
2738 goto fail;
2741 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2743 rc = GPG_ERR_ENOMEM;
2744 goto fail;
2747 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2749 rc = GPG_ERR_SYNTAX;
2750 goto fail;
2753 xpath->xp = xmlXPathNewContext (client->doc);
2754 if (!xpath->xp)
2756 rc = GPG_ERR_BAD_DATA;
2757 goto fail;
2760 xpath->result = xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2761 if (!xpath->result)
2763 rc = GPG_ERR_BAD_DATA;
2764 goto fail;
2767 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2769 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2770 goto fail;
2773 rc = xml_recurse_xpath_nodeset (client, client->doc,
2774 xpath->result->nodesetval,
2775 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2776 (xmlChar *) req[1]);
2778 fail:
2779 xpath_command_cleanup (xpath);
2780 strv_free (req);
2781 return rc;
2784 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2785 static gpg_error_t
2786 xpathattr_command (assuan_context_t ctx, char *line)
2788 struct client_s *client = assuan_get_pointer (ctx);
2789 gpg_error_t rc;
2790 struct argv_s *args[] = {
2791 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2792 NULL
2795 if (disable_list_and_dump == 1)
2796 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2798 rc = peer_is_invoker(client);
2799 if (rc)
2800 return send_error (ctx, rc);
2802 rc = parse_options (&line, args, client, 1);
2803 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2804 rc = GPG_ERR_SYNTAX;
2805 if (rc)
2806 return send_error (ctx, rc);
2808 if (client->opts & OPT_INQUIRE)
2810 unsigned char *result;
2811 size_t len;
2813 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2814 if (rc)
2815 return send_error (ctx, rc);
2817 pthread_cleanup_push ((void *)xfree, result);
2818 rc = do_xpathattr (ctx, (char *)result);
2819 pthread_cleanup_pop (1);
2821 else
2822 rc = do_xpathattr (ctx, line);
2824 return send_error (ctx, rc);
2827 static gpg_error_t
2828 do_import (struct client_s *client, const char *root_element,
2829 unsigned char *content)
2831 xmlDocPtr doc = NULL;
2832 xmlNodePtr n = NULL, root;
2833 gpg_error_t rc;
2834 struct string_s *str = NULL;
2835 struct xml_request_s *req = NULL;
2837 if (!content || !*content)
2838 return GPG_ERR_SYNTAX;
2840 if (root_element)
2842 rc = xml_new_request (client, root_element, XML_CMD_STORE, &req);
2843 if (rc)
2845 xfree (content);
2846 return rc;
2849 if (!xml_valid_element_path (req->args, 0))
2851 xml_free_request (req);
2852 xfree (content);
2853 return GPG_ERR_INV_VALUE;
2857 str = string_new_content ((char *)content);
2858 str = string_prepend (str, "<pwmd>");
2859 str = string_append (str, "</pwmd>");
2860 doc = xmlReadDoc ((xmlChar *) str->str, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2861 string_free (str, 1);
2862 if (!doc)
2864 rc = GPG_ERR_BAD_DATA;
2865 goto fail;
2868 root = xmlDocGetRootElement (doc);
2869 root = root->children;
2870 if (root->type != XML_ELEMENT_NODE)
2872 rc = GPG_ERR_BAD_DATA;
2873 goto fail;
2876 rc = xml_validate_import (client, root);
2877 if (rc)
2878 goto fail;
2880 if (req)
2882 /* Create the new specified root element path, if needed. */
2883 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc),
2884 &rc);
2885 if (rc)
2886 goto fail;
2888 /* Overwrite the children of the specified root element path. */
2889 (void)xml_unlink_node (client, n->children);
2890 n->children = NULL;
2892 else
2893 n = xmlDocGetRootElement (client->doc);
2895 if (n)
2897 xmlNodePtr copy;
2898 xmlChar *name = xml_attribute_value (root, (xmlChar *)"_name");
2900 if (!name)
2901 rc = GPG_ERR_BAD_DATA;
2902 else if (!req)
2904 /* No --root argument passed. Overwrite the current documents' root
2905 * element matching the root element of the imported data. */
2906 xml_free_request (req);
2907 req = NULL;
2908 rc = xml_new_request (client, (char *)name, XML_CMD_DELETE, &req);
2909 xmlFree (name);
2910 if (!rc)
2912 xmlNodePtr tmp;
2914 tmp = xml_find_elements (client, req,
2915 xmlDocGetRootElement (req->doc), &rc);
2916 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2917 goto fail;
2919 if (!rc)
2920 (void)xml_unlink_node (client, tmp);
2922 rc = 0;
2926 if (!rc)
2928 copy = xmlCopyNodeList (root);
2929 if (!copy)
2930 rc = GPG_ERR_ENOMEM;
2931 else
2933 n = xmlAddChildList (n, copy);
2934 if (!n)
2935 rc = GPG_ERR_ENOMEM;
2940 if (!rc && n && n->parent)
2941 rc = xml_update_element_mtime (client, n->parent);
2943 fail:
2944 xml_free_request (req);
2946 if (doc)
2947 xmlFreeDoc (doc);
2949 return rc;
2952 static gpg_error_t
2953 parse_import_opt_root (void *data, void *value)
2955 struct client_s *client = data;
2957 client->import_root = str_dup (value);
2958 return client->import_root ? 0 : GPG_ERR_ENOMEM;
2961 static gpg_error_t
2962 import_command (assuan_context_t ctx, char *line)
2964 gpg_error_t rc;
2965 struct client_s *client = assuan_get_pointer (ctx);
2966 unsigned char *result;
2967 size_t len;
2968 struct argv_s *args[] = {
2969 &(struct argv_s) {"root", OPTION_TYPE_ARG, parse_import_opt_root},
2970 NULL
2973 xfree (client->import_root);
2974 client->import_root = NULL;
2975 rc = parse_options (&line, args, client, 0);
2976 if (rc)
2977 return send_error (ctx, rc);
2979 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2980 if (rc)
2982 xfree (client->import_root);
2983 client->import_root = NULL;
2984 return send_error (ctx, rc);
2987 rc = do_import (client, client->import_root, result);
2988 xfree (client->import_root);
2989 client->import_root = NULL;
2990 return send_error (ctx, rc);
2993 static gpg_error_t
2994 do_lock (struct client_s *client, int add)
2996 gpg_error_t rc = lock_file_mutex (client, add);
2998 if (!rc)
2999 client->flags |= FLAG_LOCK_CMD;
3001 return rc;
3004 static gpg_error_t
3005 lock_command (assuan_context_t ctx, char *line)
3007 struct client_s *client = assuan_get_pointer (ctx);
3008 gpg_error_t rc;
3010 if (line && *line)
3011 return send_error (ctx, GPG_ERR_SYNTAX);
3013 rc = do_lock (client, 0);
3014 return send_error (ctx, rc);
3017 static gpg_error_t
3018 unlock_command (assuan_context_t ctx, char *line)
3020 struct client_s *client = assuan_get_pointer (ctx);
3021 gpg_error_t rc;
3023 if (line && *line)
3024 return send_error (ctx, GPG_ERR_SYNTAX);
3026 rc = unlock_file_mutex (client, 0);
3027 return send_error (ctx, rc);
3030 static void
3031 get_set_env (const char *name, const char *value)
3033 if (value && *value)
3034 setenv (name, value, 1);
3035 else
3036 unsetenv (name);
3039 static gpg_error_t
3040 option_command (assuan_context_t ctx, char *line)
3042 struct client_s *client = assuan_get_pointer (ctx);
3043 gpg_error_t rc = 0;
3044 char namebuf[255] = { 0 };
3045 char *name = namebuf;
3046 char *value = NULL, *p, *tmp = NULL;
3048 p = strchr (line, '=');
3049 if (!p)
3051 strncpy (namebuf, line, sizeof(namebuf));
3052 namebuf[sizeof(namebuf)-1] = 0;
3054 else
3056 strncpy (namebuf, line, strlen (line)-strlen (p));
3057 namebuf[sizeof(namebuf)-1] = 0;
3058 value = p+1;
3061 log_write2 ("OPTION name='%s' value='%s'", name, value);
3063 if (strcasecmp (name, (char *) "lock-timeout") == 0)
3065 long n = 0;
3067 if (value)
3069 n = strtol (value, &tmp, 10);
3070 if (tmp && *tmp)
3071 return send_error (ctx, GPG_ERR_INV_VALUE);
3074 client->lock_timeout = n;
3076 else if (strcasecmp (name, (char *) "NAME") == 0)
3078 if (value && strchr (value, ' '))
3079 rc = GPG_ERR_INV_VALUE;
3080 else
3082 tmp = pthread_getspecific (thread_name_key);
3083 xfree (tmp);
3084 MUTEX_LOCK (&cn_mutex);
3085 xfree (client->thd->name);
3086 client->thd->name = NULL;
3088 if (!value || !*value)
3089 pthread_setspecific (thread_name_key, str_dup (""));
3090 else
3092 client->thd->name = str_dup (value);
3093 pthread_setspecific (thread_name_key, str_dup (value));
3096 MUTEX_UNLOCK (&cn_mutex);
3099 else if (strcasecmp (name, "disable-pinentry") == 0)
3101 int n = 1;
3103 if (value && *value)
3105 n = (int) strtol (value, &tmp, 10);
3106 if (*tmp || n < 0 || n > 1)
3107 return send_error (ctx, GPG_ERR_INV_VALUE);
3110 if (n)
3111 client->flags |= FLAG_NO_PINENTRY;
3112 else
3113 client->flags &= ~FLAG_NO_PINENTRY;
3115 else if (strcasecmp (name, "ttyname") == 0)
3117 get_set_env ("GPG_TTY", value);
3119 else if (strcasecmp (name, "ttytype") == 0)
3121 get_set_env ("TERM", value);
3123 else if (strcasecmp (name, "display") == 0)
3125 get_set_env ("DISPLAY", value);
3127 else if (strcasecmp (name, "lc_messages") == 0)
3130 else if (strcasecmp (name, "lc_ctype") == 0)
3133 else if (strcasecmp (name, "desc") == 0)
3136 else if (strcasecmp (name, "pinentry-timeout") == 0)
3139 else
3140 rc = GPG_ERR_UNKNOWN_OPTION;
3142 return send_error (ctx, rc);
3145 static gpg_error_t
3146 do_rename (assuan_context_t ctx, char *line)
3148 struct client_s *client = assuan_get_pointer (ctx);
3149 char **args, *p;
3150 xmlNodePtr src, dst;
3151 struct string_s *str = NULL, *tstr;
3152 struct xml_request_s *req;
3153 gpg_error_t rc;
3155 args = str_split (line, " ", 0);
3156 if (!args || strv_length (args) != 2)
3158 strv_free (args);
3159 return GPG_ERR_SYNTAX;
3162 if (!xml_valid_element ((xmlChar *) args[1]))
3164 strv_free (args);
3165 return GPG_ERR_INV_VALUE;
3168 rc = xml_new_request (client, args[0], XML_CMD_RENAME, &req);
3169 if (rc)
3171 strv_free (args);
3172 return rc;
3175 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3176 if (rc)
3177 goto fail;
3179 rc = xml_is_element_owner (client, src);
3180 if (rc)
3181 goto fail;
3183 xmlChar *a = xmlGetProp (src, (xmlChar *) "_name");
3184 if (!a)
3186 rc = GPG_ERR_ENOMEM;
3187 goto fail;
3190 /* To prevent unwanted effects:
3192 * <element _name="a"><element _name="b"/></element>
3194 * RENAME a<TAB>b b
3196 if (xmlStrEqual (a, (xmlChar *) args[1]))
3198 xmlFree (a);
3199 rc = GPG_ERR_EEXIST;
3200 goto fail;
3203 xmlFree (a);
3204 str = string_new (args[0]);
3205 if (!str)
3207 rc = GPG_ERR_ENOMEM;
3208 goto fail;
3211 p = strrchr (str->str, '\t');
3212 if (p)
3213 tstr = string_truncate (str, strlen (str->str)-strlen (p));
3214 else
3215 tstr = string_truncate (str, 0);
3217 if (!tstr)
3219 string_free (str, 1);
3220 rc = GPG_ERR_ENOMEM;
3221 goto fail;
3224 str = tstr;
3225 tstr = string_append_printf (str, "%s%s", str->len ? "\t" : "", args[1]);
3226 if (!tstr)
3228 string_free (str, 1);
3229 rc = GPG_ERR_ENOMEM;
3230 goto fail;
3233 str = tstr;
3234 xml_free_request (req);
3235 rc = xml_new_request (client, str->str, XML_CMD_RENAME, &req);
3236 string_free (str, 1);
3237 if (rc)
3239 req = NULL;
3240 goto fail;
3243 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3244 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3245 goto fail;
3247 rc = 0;
3249 /* Target may exist:
3251 * <element _name="a"/>
3252 * <element _name="b" target="a"/>
3254 * RENAME b a
3256 if (src == dst)
3258 rc = GPG_ERR_EEXIST;
3259 goto fail;
3262 if (dst)
3264 rc = xml_is_element_owner (client, dst);
3265 if (rc)
3266 goto fail;
3268 rc = xml_add_attribute (client, src, "_name", args[1]);
3269 if (!rc)
3270 xml_unlink_node (client, dst);
3272 else
3273 rc = xml_add_attribute (client, src, "_name", args[1]);
3275 fail:
3276 xml_free_request (req);
3277 strv_free (args);
3278 return rc;
3281 static gpg_error_t
3282 rename_command (assuan_context_t ctx, char *line)
3284 struct client_s *client = assuan_get_pointer (ctx);
3285 gpg_error_t rc;
3286 struct argv_s *args[] = {
3287 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3288 NULL
3291 rc = parse_options (&line, args, client, 1);
3292 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3293 rc = GPG_ERR_SYNTAX;
3294 if (rc)
3295 return send_error (ctx, rc);
3297 if (client->opts & OPT_INQUIRE)
3299 unsigned char *result;
3300 size_t len;
3302 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3303 if (rc)
3304 return send_error (ctx, rc);
3306 pthread_cleanup_push ((void *)xfree, result);
3307 rc = do_rename (ctx, (char *)result);
3308 pthread_cleanup_pop (1);
3310 else
3311 rc = do_rename (ctx, line);
3313 return send_error (ctx, rc);
3316 static gpg_error_t
3317 do_copy (assuan_context_t ctx, char *line)
3319 struct client_s *client = assuan_get_pointer (ctx);
3320 char **args, **targs;
3321 xmlNodePtr src, dst, tree = NULL;
3322 struct xml_request_s *req;
3323 gpg_error_t rc;
3325 args = str_split (line, " ", 0);
3326 if (!args || strv_length (args) != 2)
3328 strv_free (args);
3329 return GPG_ERR_SYNTAX;
3332 targs = str_split (args[1], "\t", 0);
3333 if (!targs)
3335 strv_free (args);
3336 return GPG_ERR_SYNTAX;
3339 if (!xml_valid_element_path (targs, 0))
3341 strv_free (args);
3342 strv_free (targs);
3343 return GPG_ERR_INV_VALUE;
3345 strv_free (targs);
3347 rc = xml_new_request (client, args[0], XML_CMD_NONE, &req);
3348 if (rc)
3350 strv_free (args);
3351 return rc;
3354 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3355 if (rc)
3356 goto fail;
3358 tree = xmlCopyNodeList (src);
3359 if (!tree)
3361 rc = GPG_ERR_ENOMEM;
3362 goto fail;
3365 xml_free_request (req);
3366 /* Create the destination element path if it does not exist. */
3367 rc = xml_new_request (client, args[1], XML_CMD_STORE, &req);
3368 if (rc)
3370 req = NULL;
3371 goto fail;
3374 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3375 if (rc)
3376 goto fail;
3378 rc = xml_is_element_owner (client, dst);
3379 if (rc)
3380 goto fail;
3382 rc = xml_validate_target (client, req->doc, args[1], src);
3383 if (rc || src == dst)
3385 rc = rc ? rc : GPG_ERR_EEXIST;
3386 goto fail;
3389 /* Merge any attributes from the src node to the initial dst node. */
3390 for (xmlAttrPtr attr = tree->properties; attr; attr = attr->next)
3392 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3393 continue;
3395 xmlAttrPtr a = xmlHasProp (dst, attr->name);
3396 if (a)
3397 xmlRemoveProp (a);
3399 xmlChar *tmp = xmlNodeGetContent (attr->children);
3400 xmlNewProp (dst, attr->name, tmp);
3401 xmlFree (tmp);
3402 /* Create the default attributes. */
3403 rc = xml_add_attribute (client, dst, NULL, NULL);
3406 xmlNodePtr n = dst->children;
3407 (void)xml_unlink_node (client, n);
3408 dst->children = NULL;
3410 if (tree->children)
3412 n = xmlCopyNodeList (tree->children);
3413 if (!n)
3415 rc = GPG_ERR_ENOMEM;
3416 goto fail;
3419 n = xmlAddChildList (dst, n);
3420 if (!n)
3422 rc = GPG_ERR_ENOMEM;
3423 goto fail;
3426 rc = xml_update_element_mtime (client, xmlDocGetRootElement (client->doc)
3427 == dst->parent ? dst : dst->parent);
3430 fail:
3431 if (tree)
3432 (void)xml_unlink_node (client, tree);
3434 xml_free_request (req);
3435 strv_free (args);
3436 return rc;
3439 static gpg_error_t
3440 copy_command (assuan_context_t ctx, char *line)
3442 struct client_s *client = assuan_get_pointer (ctx);
3443 gpg_error_t rc;
3444 struct argv_s *args[] = {
3445 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3446 NULL
3449 rc = parse_options (&line, args, client, 1);
3450 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3451 rc = GPG_ERR_SYNTAX;
3452 if (rc)
3453 return send_error (ctx, rc);
3455 if (client->opts & OPT_INQUIRE)
3457 unsigned char *result;
3458 size_t len;
3460 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3461 if (rc)
3462 return send_error (ctx, rc);
3464 pthread_cleanup_push ((void *)xfree, result);
3465 rc = do_copy (ctx, (char *)result);
3466 pthread_cleanup_pop (1);
3468 else
3469 rc = do_copy (ctx, line);
3471 return send_error (ctx, rc);
3474 static gpg_error_t
3475 do_move (assuan_context_t ctx, char *line)
3477 struct client_s *client = assuan_get_pointer (ctx);
3478 char **args;
3479 xmlNodePtr src, dst;
3480 struct xml_request_s *req;
3481 gpg_error_t rc;
3483 args = str_split (line, " ", 0);
3484 if (!args || strv_length (args) != 2)
3486 strv_free (args);
3487 return GPG_ERR_SYNTAX;
3490 if (*args[1])
3492 char **targs = str_split (args[1], "\t", 0);
3493 if (!targs)
3495 strv_free (args);
3496 return GPG_ERR_ENOMEM;
3499 if (!xml_valid_element_path (targs, 0))
3501 strv_free (targs);
3502 strv_free (args);
3503 return GPG_ERR_INV_VALUE;
3506 strv_free (targs);
3509 rc = xml_new_request (client, args[0], XML_CMD_MOVE, &req);
3510 if (rc)
3512 strv_free (args);
3513 return rc;
3516 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3517 if (rc)
3518 goto fail;
3520 rc = xml_is_element_owner (client, src);
3521 if (rc)
3522 goto fail;
3524 xml_free_request (req);
3525 req = NULL;
3526 if (*args[1])
3528 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
3529 if (rc)
3530 goto fail;
3532 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc),
3533 &rc);
3535 else
3536 dst = xmlDocGetRootElement (client->doc);
3538 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3539 goto fail;
3541 rc = 0;
3543 for (xmlNodePtr n = dst; n; n = n->parent)
3545 if (n == src)
3547 rc = GPG_ERR_EEXIST;
3548 goto fail;
3552 if (dst)
3554 xmlChar *a = xml_attribute_value (src, (xmlChar *) "_name");
3555 xmlNodePtr dup = xml_find_element (client, dst->children, (char *) a, &rc);
3557 xmlFree (a);
3558 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3559 goto fail;
3561 rc = 0;
3563 if (dup)
3565 if (dup == src)
3567 rc = GPG_ERR_EEXIST;
3568 goto fail;
3571 if (dst == xmlDocGetRootElement (client->doc))
3573 xmlNodePtr n = src;
3574 int match = 0;
3576 while (n->parent && n->parent != dst)
3577 n = n->parent;
3579 xmlChar *a = xml_attribute_value (n, (xmlChar *) "_name");
3580 xmlChar *b = xml_attribute_value (src, (xmlChar *) "_name");
3582 if (xmlStrEqual (a, b))
3584 match = 1;
3585 xmlUnlinkNode (src);
3586 (void)xml_unlink_node (client, n);
3589 xmlFree (a);
3590 xmlFree (b);
3592 if (!match)
3593 (void)xml_unlink_node (client, dup);
3595 else
3596 xmlUnlinkNode (dup);
3600 if (!dst && *req->args)
3602 struct xml_request_s *nreq = NULL;
3603 xmlChar *name = xml_attribute_value (src, (xmlChar *) "_name");
3605 if (src->parent == xmlDocGetRootElement (client->doc)
3606 && !strcmp ((char *) name, *req->args))
3608 xmlFree (name);
3609 rc = GPG_ERR_EEXIST;
3610 goto fail;
3613 xmlFree (name);
3614 rc = xml_new_request (client, args[1], XML_CMD_STORE, &nreq);
3615 if (!rc)
3617 dst = xml_find_elements (client, nreq,
3618 xmlDocGetRootElement (nreq->doc), &rc);
3619 xml_free_request (nreq);
3622 if (rc)
3623 goto fail;
3626 xml_update_element_mtime (client, src->parent);
3627 xmlUnlinkNode (src);
3629 dst = xmlAddChildList (dst, src);
3630 if (!dst)
3631 rc = GPG_ERR_ENOMEM;
3632 else
3633 xml_update_element_mtime (client, dst->parent);
3635 fail:
3636 xml_free_request (req);
3637 strv_free (args);
3638 return rc;
3641 static gpg_error_t
3642 move_command (assuan_context_t ctx, char *line)
3644 struct client_s *client = assuan_get_pointer (ctx);
3645 gpg_error_t rc;
3646 struct argv_s *args[] = {
3647 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3648 NULL
3651 rc = parse_options (&line, args, client, 1);
3652 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3653 rc = GPG_ERR_SYNTAX;
3654 if (rc)
3655 return send_error (ctx, rc);
3657 if (client->opts & OPT_INQUIRE)
3659 unsigned char *result;
3660 size_t len;
3662 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3663 if (rc)
3664 return send_error (ctx, rc);
3666 pthread_cleanup_push ((void *)xfree, result);
3667 rc = do_move (ctx, (char *)result);
3668 pthread_cleanup_pop (1);
3670 else
3671 rc = do_move (ctx, line);
3673 return send_error (ctx, rc);
3676 static gpg_error_t
3677 ls_command (assuan_context_t ctx, char *line)
3679 gpg_error_t rc;
3680 char *tmp;
3681 char *dir;
3682 DIR *d;
3684 if (line && *line)
3685 return send_error (ctx, GPG_ERR_SYNTAX);
3687 tmp = str_asprintf ("%s/data", homedir);
3688 dir = expand_homedir (tmp);
3689 xfree (tmp);
3690 d = opendir (dir);
3691 rc = gpg_error_from_errno (errno);
3693 if (!d)
3695 xfree (dir);
3696 return send_error (ctx, rc);
3699 size_t len =
3700 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3701 struct dirent *p = xmalloc (len), *cur = NULL;
3702 char *list = NULL;
3704 xfree (dir);
3705 pthread_cleanup_push ((void *)xfree, p);
3706 pthread_cleanup_push ((void *)(void *)(void *)closedir, d);
3707 rc = 0;
3709 while (!readdir_r (d, p, &cur) && cur)
3711 struct stat st;
3713 if (stat (cur->d_name, &st) == -1 || !S_ISREG (st.st_mode))
3714 continue;
3716 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3718 if (!tmp)
3720 if (list)
3721 xfree (list);
3723 rc = GPG_ERR_ENOMEM;
3724 break;
3727 xfree (list);
3728 list = tmp;
3731 pthread_cleanup_pop (1); // closedir (d)
3732 pthread_cleanup_pop (1); // xfree (p)
3734 if (rc)
3735 return send_error (ctx, rc);
3737 if (!list)
3738 return send_error (ctx, GPG_ERR_NO_DATA);
3740 list[strlen (list) - 1] = 0;
3741 pthread_cleanup_push ((void *)xfree, list);
3742 rc = xfer_data (ctx, list, strlen (list));
3743 pthread_cleanup_pop (1);
3744 return send_error (ctx, rc);
3747 static gpg_error_t
3748 bye_notify (assuan_context_t ctx, char *line)
3750 struct client_s *cl = assuan_get_pointer (ctx);
3751 gpg_error_t ret = 0;
3753 update_client_state (cl, CLIENT_STATE_DISCON);
3755 #ifdef WITH_GNUTLS
3756 if (cl->thd->remote)
3758 int rc;
3762 struct timeval tv = { 0, 50000 };
3764 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3765 if (rc == GNUTLS_E_AGAIN)
3766 select (0, NULL, NULL, NULL, &tv);
3768 while (rc == GNUTLS_E_AGAIN);
3770 #endif
3772 /* This will let assuan_process_next() return. */
3773 if (fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK) == -1)
3775 cl->last_rc = gpg_error_from_errno (errno);
3776 ret = cl->last_rc;
3779 cl->last_rc = 0; // BYE command result
3780 return ret;
3783 static gpg_error_t
3784 reset_notify (assuan_context_t ctx, char *line)
3786 struct client_s *client = assuan_get_pointer (ctx);
3788 if (client)
3789 cleanup_client (client);
3791 return 0;
3795 * This is called before every Assuan command.
3797 static gpg_error_t
3798 command_startup (assuan_context_t ctx, const char *name)
3800 struct client_s *client = assuan_get_pointer (ctx);
3801 gpg_error_t rc;
3802 struct command_table_s *cmd = NULL;
3804 log_write2 ("command='%s'", name);
3805 client->last_rc = client->opts = 0;
3807 for (int i = 0; command_table[i]; i++)
3809 if (!strcasecmp (name, command_table[i]->name))
3811 if (command_table[i]->ignore_startup)
3812 return 0;
3813 cmd = command_table[i];
3814 break;
3818 if (!cmd)
3819 return GPG_ERR_UNKNOWN_COMMAND;
3821 client->last_rc = rc = gpg_error (file_modified (client, cmd));
3822 if (!rc)
3823 update_client_state (client, CLIENT_STATE_COMMAND);
3825 return rc;
3829 * This is called after every Assuan command.
3831 static void
3832 command_finalize (assuan_context_t ctx, gpg_error_t rc)
3834 struct client_s *client = assuan_get_pointer (ctx);
3836 if (!(client->flags & FLAG_LOCK_CMD))
3837 unlock_file_mutex (client, 0);
3839 unlock_flock (&client->flock_fd);
3840 log_write2 (_("command completed: rc=%u"), rc ? rc : client->last_rc);
3841 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
3842 #ifdef WITH_GNUTLS
3843 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
3844 #endif
3845 if (client->thd->state != CLIENT_STATE_DISCON)
3846 update_client_state (client, CLIENT_STATE_IDLE);
3849 static gpg_error_t
3850 parse_help_opt_html (void *data, void *value)
3852 struct client_s *client = data;
3854 (void) value;
3855 client->opts |= OPT_HTML;
3856 return 0;
3859 static gpg_error_t
3860 help_command (assuan_context_t ctx, char *line)
3862 gpg_error_t rc;
3863 int i;
3864 struct client_s *client = assuan_get_pointer (ctx);
3865 struct argv_s *args[] = {
3866 &(struct argv_s) {"html", OPTION_TYPE_NOARG, parse_help_opt_html},
3867 NULL
3870 rc = parse_options (&line, args, client, 1);
3871 if (rc)
3872 return send_error (ctx, rc);
3874 if (!line || !*line)
3876 char *tmp;
3877 char *help = str_dup (_("Usage: HELP [--html] [<COMMAND>]\n"
3878 "For commands that take an element path as an argument, each element is "
3879 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3880 "@*@*COMMANDS:"));
3882 for (i = 0; command_table[i]; i++)
3884 if (!command_table[i]->help)
3885 continue;
3887 /* @npxref{} won't put a "See " or "see " in front of the command.
3888 * It's not a texinfo command but needed for --html. */
3889 tmp = str_asprintf ("%s %s%s%s", help,
3890 client->opts & OPT_HTML ? "@npxref{" : "",
3891 command_table[i]->name,
3892 client->opts & OPT_HTML ? "}" : "");
3893 xfree (help);
3894 help = tmp;
3897 tmp = strip_texi_and_wrap (help, client->opts & OPT_HTML);
3898 xfree (help);
3899 pthread_cleanup_push ((void *)xfree, tmp);
3900 rc = xfer_data (ctx, tmp, strlen (tmp));
3901 pthread_cleanup_pop (1);
3902 return send_error (ctx, rc);
3905 for (i = 0; command_table[i]; i++)
3907 if (!strcasecmp (line, command_table[i]->name))
3909 char *help, *tmp;
3911 if (!command_table[i]->help)
3912 break;
3914 help = strip_texi_and_wrap (command_table[i]->help,
3915 client->opts & OPT_HTML);
3916 tmp = str_asprintf ("%s%s",
3917 (client->opts & OPT_HTML) ? "" : _("Usage: "),
3918 help);
3919 xfree (help);
3920 pthread_cleanup_push ((void *)xfree, tmp);
3921 rc = xfer_data (ctx, tmp, strlen (tmp));
3922 pthread_cleanup_pop (1);
3923 return send_error (ctx, rc);
3927 return send_error (ctx, GPG_ERR_INV_NAME);
3930 static void
3931 new_command (const char *name, int ignore, int unlock, int flock_type,
3932 gpg_error_t (*handler) (assuan_context_t, char *),
3933 const char *help)
3935 int i = 0;
3936 struct command_table_s **tmp;
3938 if (command_table)
3939 for (i = 0; command_table[i]; i++);
3941 tmp = xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
3942 assert (tmp);
3943 command_table = tmp;
3944 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
3945 command_table[i]->name = name;
3946 command_table[i]->handler = handler;
3947 command_table[i]->ignore_startup = ignore;
3948 command_table[i]->unlock = unlock;
3949 command_table[i]->flock_type = flock_type;
3950 command_table[i++]->help = help;
3951 command_table[i] = NULL;
3954 void
3955 deinit_commands ()
3957 int i;
3959 for (i = 0; command_table[i]; i++)
3960 xfree (command_table[i]);
3962 xfree (command_table);
3965 static int
3966 sort_commands (const void *arg1, const void *arg2)
3968 struct command_table_s *const *a = arg1;
3969 struct command_table_s *const *b = arg2;
3971 if (!*a || !*b)
3972 return 0;
3973 else if (*a && !*b)
3974 return 1;
3975 else if (!*a && *b)
3976 return -1;
3978 return strcmp ((*a)->name, (*b)->name);
3981 static gpg_error_t
3982 passwd_command (assuan_context_t ctx, char *line)
3984 struct client_s *client = assuan_get_pointer (ctx);
3985 gpg_error_t rc;
3987 rc = peer_is_invoker (client);
3988 if (rc == GPG_ERR_EACCES)
3989 return send_error (ctx, GPG_ERR_FORBIDDEN);
3990 else if (rc)
3991 return send_error (ctx, rc);
3993 if (client->flags & FLAG_NEW)
3994 return send_error (ctx, GPG_ERR_INV_STATE);
3996 client->crypto->keyfile = config_get_string (client->filename,
3997 "passphrase_file");
3998 rc = crypto_init_ctx (client->crypto, client->flags & FLAG_NO_PINENTRY,
3999 client->crypto->keyfile);
4000 if (rc)
4001 return send_error (ctx, rc);
4003 if (!rc)
4004 rc = crypto_passwd (client, client->crypto);
4006 crypto_free_non_keys (client->crypto);
4007 return send_error (ctx, rc);
4010 static gpg_error_t
4011 parse_opt_data (void *data, void *value)
4013 struct client_s *client = data;
4015 (void) value;
4016 client->opts |= OPT_DATA;
4017 return 0;
4020 static gpg_error_t
4021 send_client_list (assuan_context_t ctx)
4023 struct client_s *client = assuan_get_pointer (ctx);
4024 gpg_error_t rc = 0;
4026 if (client->opts & OPT_VERBOSE)
4028 unsigned i, t;
4029 char **list = NULL;
4030 char *line;
4032 MUTEX_LOCK (&cn_mutex);
4033 pthread_cleanup_push ((void *)cleanup_mutex_cb, &cn_mutex);
4034 t = slist_length (cn_thread_list);
4036 for (i = 0; i < t; i++)
4038 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
4039 char *tmp;
4041 if (thd->state == CLIENT_STATE_UNKNOWN)
4042 continue;
4044 tmp = build_client_info_line (thd, 0);
4045 if (tmp)
4047 char **l = strv_cat (list, tmp);
4048 if (!l)
4049 rc = GPG_ERR_ENOMEM;
4050 else
4051 list = l;
4053 else
4054 rc = GPG_ERR_ENOMEM;
4056 if (rc)
4058 strv_free (list);
4059 break;
4063 pthread_cleanup_pop (1);
4064 if (rc)
4065 return rc;
4067 line = strv_join ("\n", list);
4068 strv_free (list);
4069 pthread_cleanup_push ((void *)xfree, line);
4070 rc = xfer_data (ctx, line, strlen (line));
4071 pthread_cleanup_pop (1);
4072 return rc;
4075 if (client->opts & OPT_DATA)
4077 char buf[ASSUAN_LINELENGTH];
4079 MUTEX_LOCK (&cn_mutex);
4080 snprintf (buf, sizeof (buf), "%u", slist_length (cn_thread_list));
4081 MUTEX_UNLOCK (&cn_mutex);
4082 rc = xfer_data (ctx, buf, strlen (buf));
4084 else
4085 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4087 return rc;
4090 static gpg_error_t
4091 getinfo_command (assuan_context_t ctx, char *line)
4093 struct client_s *client = assuan_get_pointer (ctx);
4094 gpg_error_t rc;
4095 char buf[ASSUAN_LINELENGTH];
4096 struct argv_s *args[] = {
4097 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4098 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_opt_verbose},
4099 NULL
4102 rc = parse_options (&line, args, client, 1);
4103 if (rc)
4104 return send_error (ctx, rc);
4106 if (!strcasecmp (line, "clients"))
4108 rc = send_client_list (ctx);
4110 else if (!strcasecmp (line, "cache"))
4112 if (client->opts & OPT_DATA)
4114 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4115 rc = xfer_data (ctx, buf, strlen (buf));
4117 else
4118 rc = send_status (ctx, STATUS_CACHE, NULL);
4120 else if (!strcasecmp (line, "pid"))
4122 char buf[32];
4123 pid_t pid = getpid ();
4125 snprintf (buf, sizeof (buf), "%i", pid);
4126 rc = xfer_data (ctx, buf, strlen (buf));
4128 else if (!strcasecmp (line, "version"))
4130 char *buf = str_asprintf ("0x%06x %s", VERSION_HEX,
4131 #ifdef WITH_GNUTLS
4132 "GNUTLS "
4133 #endif
4134 #ifdef WITH_LIBACL
4135 "ACL "
4136 #endif
4137 "");
4138 rc = xfer_data (ctx, buf, strlen (buf));
4139 xfree (buf);
4141 else if (!strcasecmp (line, "last_error"))
4143 if (client->last_error)
4144 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4145 else
4146 rc = GPG_ERR_NO_DATA;
4148 else if (!strcasecmp (line, "user"))
4150 char *user = NULL;
4152 #ifdef WITH_GNUTLS
4153 if (client->thd->remote)
4154 user = str_asprintf ("#%s", client->thd->tls->fp);
4155 else
4156 user = get_username (client->thd->peer->uid);
4157 #else
4158 user = get_username (client->thd->peer->uid);
4159 #endif
4160 if (user)
4162 pthread_cleanup_push ((void *)xfree, user);
4163 rc = xfer_data (ctx, user, strlen (user));
4164 pthread_cleanup_pop (1);
4166 else
4167 rc = GPG_ERR_NO_DATA;
4169 else
4170 rc = gpg_error (GPG_ERR_SYNTAX);
4172 return send_error (ctx, rc);
4175 static gpg_error_t
4176 parse_listkeys_opt_secret_only (void *data, void *value)
4178 struct client_s *client = data;
4180 (void) value;
4181 client->opts |= OPT_SECRET_ONLY;
4182 return 0;
4185 static gpg_error_t
4186 keyinfo_command (assuan_context_t ctx, char *line)
4188 struct client_s *client = assuan_get_pointer (ctx);
4189 gpg_error_t rc = 0;
4190 char **keys = NULL, **p = NULL;
4191 int sym = 0;
4193 if (line && *line)
4194 return send_error (ctx, GPG_ERR_SYNTAX);
4196 if (!(client->flags & FLAG_OPEN))
4197 return send_error (ctx, GPG_ERR_INV_STATE);
4199 if (client->flags & FLAG_NEW)
4200 return send_error (ctx, GPG_ERR_NO_DATA);
4202 rc = lock_flock (ctx, client->filename, FLOCK_TYPE_SH, &client->flock_fd);
4203 if (rc)
4204 return send_error (ctx, rc);
4206 rc = crypto_is_symmetric (client->filename);
4207 unlock_flock (&client->flock_fd);
4208 if (!rc)
4209 sym = 1;
4210 else if (rc != GPG_ERR_BAD_DATA)
4211 return send_error (ctx, rc);
4213 rc = 0;
4214 if (!sym)
4216 p = strv_catv(keys, client->crypto->pubkey);
4217 if (!p)
4218 rc = GPG_ERR_ENOMEM;
4221 if (!rc)
4223 keys = p;
4224 for (p = client->crypto->sigkey; p && *p; p++)
4226 if (!strv_printf(&keys, "S%s", *p))
4228 strv_free (keys);
4229 return send_error (ctx, GPG_ERR_ENOMEM);
4233 else
4234 rc = GPG_ERR_ENOMEM;
4236 if (!rc && keys)
4238 line = strv_join ("\n", keys);
4239 strv_free (keys);
4240 pthread_cleanup_push ((void *)xfree, line);
4241 if (line)
4242 rc = xfer_data (ctx, line, strlen (line));
4243 else
4244 rc = GPG_ERR_ENOMEM;
4246 pthread_cleanup_pop (1);
4248 else if (!keys)
4249 rc = GPG_ERR_NO_DATA;
4250 else
4251 strv_free (keys);
4253 return send_error (ctx, rc);
4256 static gpg_error_t
4257 kill_command (assuan_context_t ctx, char *line)
4259 struct client_s *client = assuan_get_pointer (ctx);
4260 gpg_error_t rc;
4261 unsigned i, t;
4263 if (!line || !*line)
4264 return send_error (ctx, GPG_ERR_SYNTAX);
4266 MUTEX_LOCK (&cn_mutex);
4267 pthread_cleanup_push ((void *)cleanup_mutex_cb, &cn_mutex);
4268 t = slist_length (cn_thread_list);
4269 rc = GPG_ERR_ESRCH;
4271 for (i = 0; i < t; i++)
4273 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
4274 char *tmp = str_asprintf ("%p", thd->tid);
4276 if (strcmp (line, tmp))
4278 xfree (tmp);
4279 continue;
4282 xfree (tmp);
4283 rc = peer_is_invoker (client);
4284 if (!rc)
4286 #ifdef HAVE_PTHREAD_CANCEL
4287 rc = pthread_cancel (thd->tid);
4288 #else
4289 close (thd->fd);
4290 thd->fd = -1;
4291 #endif
4292 break;
4295 rc = GPG_ERR_FORBIDDEN;
4296 if (config_get_boolean ("global", "strict_kill"))
4297 break;
4299 #ifdef WITH_GNUTLS
4300 if (client->thd->remote && thd->remote)
4302 if (!strcmp (client->thd->tls->fp, thd->tls->fp))
4304 #ifdef HAVE_PTHREAD_CANCEL
4305 rc = pthread_cancel (thd->tid);
4306 #else
4307 close (thd->fd);
4308 thd->fd = -1;
4309 #endif
4310 break;
4313 else if (!client->thd->remote && !thd->remote)
4314 #endif
4316 if (client->thd->peer->uid == thd->peer->uid)
4318 #ifdef HAVE_PTHREAD_CANCEL
4319 rc = pthread_cancel (thd->tid);
4320 #else
4321 close (thd->fd);
4322 thd->fd = -1;
4323 #endif
4326 break;
4329 pthread_cleanup_pop (1);
4330 return send_error (ctx, rc);
4333 static gpg_error_t
4334 listkeys_command (assuan_context_t ctx, char *line)
4336 struct client_s *client = assuan_get_pointer (ctx);
4337 struct crypto_s *crypto = NULL;
4338 char **pattern = NULL;
4339 gpgme_key_t *keys = NULL;
4340 char **result = NULL;
4341 gpg_error_t rc;
4342 struct argv_s *args[] = {
4343 &(struct argv_s) {"secret-only", OPTION_TYPE_NOARG,
4344 parse_listkeys_opt_secret_only },
4345 NULL
4348 rc = parse_options (&line, args, client, 1);
4349 if (rc)
4350 return send_error (ctx, rc);
4352 rc = crypto_init (&crypto, client->ctx, client->filename,
4353 client->flags & FLAG_NO_PINENTRY, NULL);
4354 if (rc)
4355 return send_error (ctx, rc);
4357 pthread_cleanup_push ((void *)crypto_free, crypto);
4358 pattern = str_split (line, ",", 0);
4359 pthread_cleanup_push ((void *)(void *)strv_free, pattern);
4360 rc = crypto_list_keys (crypto, pattern, client->opts & OPT_SECRET_ONLY,
4361 &keys);
4362 pthread_cleanup_pop (1);
4363 pthread_cleanup_pop (1);
4364 if (!rc)
4366 int i;
4368 for (i = 0; keys[i]; i++)
4370 char *p = crypto_key_info (keys[i]);
4371 char **r;
4373 if (!p)
4375 rc = GPG_ERR_ENOMEM;
4376 break;
4379 r = strv_cat (result, p);
4380 if (!r)
4382 rc = GPG_ERR_ENOMEM;
4383 break;
4386 result = r;
4389 if (!i)
4390 rc = GPG_ERR_NO_DATA;
4392 crypto_free_key_list (keys);
4395 if (!rc)
4397 line = strv_join ("\n", result);
4398 strv_free (result);
4399 pthread_cleanup_push ((void *)xfree, line);
4400 if (!line)
4401 rc = GPG_ERR_ENOMEM;
4402 else
4403 rc = xfer_data (ctx, line, strlen (line));
4405 pthread_cleanup_pop (1);
4407 else
4408 strv_free (result);
4410 return send_error (ctx, rc);
4413 void
4414 init_commands ()
4416 /* !BEGIN-HELP-TEXT!
4418 * This comment is used as a marker to generate the offline documentation
4419 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4420 * script to determine where commands begin and end.
4422 new_command("HELP", 1, 1, 0, help_command, _(
4423 "HELP [--html] [<COMMAND>]\n"
4424 "Show available commands or command specific help text."
4425 "@*@*"
4426 "The @option{--html} option will output the help text in HTML format."
4429 new_command("KILL", 1, 0, 0, kill_command, _(
4430 "KILL <thread_id>\n"
4431 "Terminates the client identified by @var{thread_id} and releases any file "
4432 "lock or other resources it has held. See @code{GETINFO} (@pxref{GETINFO}) "
4433 "for details about listing connected clients. An @code{invoking_user} "
4434 "(@pxref{Configuration}) may kill any client while others may only kill "
4435 "clients of the same @code{UID} or @abbr{TLS} fingerprint.\n"
4438 new_command("LISTKEYS", 0, 0, 0, listkeys_command, _(
4439 "LISTKEYS [--secret-only] [pattern[,<pattern>]]\n"
4440 "Returns a new line separated list of key information matching a comma "
4441 "separated list of @var{pattern}'s. When option @option{--secret-only} is "
4442 "specified, only keys matching @var{pattern} that also have a secret key "
4443 "available will be returned."
4446 new_command("KEYINFO", 1, 0, 0, keyinfo_command, _(
4447 "KEYINFO\n"
4448 "Returns a new line separated list of key ID's that the currently opened "
4449 "data file has recipients and signers for. If the key is a signing key it "
4450 "will be prefixed with an @code{S}. If the file is a new one, or has no "
4451 "signers in the case of being symmetrically encrypted, the error code "
4452 "@code{GPG_ERR_NO_DATA} is returned."
4455 new_command("GETINFO", 1, 1, 0, getinfo_command, _(
4456 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4457 "Get server and other information. The information is returned via a status "
4458 "message (@pxref{Status Messages}) unless otherwise noted or @option{--data} "
4459 "is specified."
4460 "@*@*"
4461 "@var{CACHE} returns the number of cached documents."
4462 "@*@*"
4463 "@var{CLIENTS} returns the number of "
4464 "connected clients via a status message or a list of connected clients when "
4465 "the @option{--verbose} parameter is used (implies @option{--data}). A "
4466 "verbose line of a client list contains "
4467 "space delimited "
4468 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4469 "IP address if remote, file lock status, whether the current client is self "
4470 "or not, client state (see below), "
4471 "user ID or TLS fingerprint of the connected client, username if the "
4472 "client is a local one else @code{-}, and finally the time stamp of when the "
4473 "client connected."
4474 "@*@*"
4475 "Client state @code{0} is an unknown client state, state @code{1} indicates "
4476 "the client has connected but hasn't completed initializing, state @code{2} "
4477 "indicates that the client is idle, state @code{3} means the "
4478 "client is in a command and state @code{4} means the client is disconnecting. "
4479 "@*@*"
4480 "@var{PID} returns the process ID number of the server via a data response."
4481 "@*@*"
4482 "@var{VERSION} returns the server version number and compile-time features "
4483 "via a data response with each being space delimited."
4484 "@*@*"
4485 "@var{LAST_ERROR} returns a detailed description of the last failed command "
4486 "via a data response, when available."
4487 "@*@*"
4488 "@var{USER} returns the username or @abbr{TLS} hash of the connected client "
4489 "via a data response."
4492 new_command("PASSWD", 0, 0, FLOCK_TYPE_EX|FLOCK_TYPE_KEEP, passwd_command, _(
4493 "PASSWD\n"
4494 "Changes the passphrase of the secret key required to open the current "
4495 "data file. If the data file is symmetrically encrypted, the error "
4496 "@code{GPG_ERR_NOT_SUPPORTED} is returned. When symmetrically encrypted, "
4497 "the @code{SAVE} command (@pxref{SAVE}) should be used instead to prevent "
4498 "this command saving any unwanted changes to the @abbr{XML} document."
4499 "@*@*"
4500 "This command is not available to non-invoking clients "
4501 "(@pxref{Access Control})."
4504 new_command("OPEN", 1, 1, FLOCK_TYPE_SH|FLOCK_TYPE_KEEP, open_command, _(
4505 "OPEN [--lock] <filename>\n"
4506 "Opens @var{filename}. When the @var{filename} is not found on the "
4507 "file-system then a new in-memory document will be created. If the file is "
4508 "found, it is looked for in the file cache and when found no passphrase will "
4509 "be required to open it. When not cached, @cite{pinentry(1)} will be used to "
4510 "retrieve the passphrase for decryption unless @option{disable-pinentry} "
4511 "(@pxref{OPTION}) was specified in which case @command{pwmd} will "
4512 "@emph{INQUIRE} the client for the passphrase."
4513 "@*@*"
4514 "When the @option{--lock} option is passed then the file mutex will be "
4515 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4516 "file had been opened."
4519 new_command("SAVE", 0, 0, FLOCK_TYPE_EX|FLOCK_TYPE_KEEP, save_command, _(
4520 "SAVE [--symmetric] [--inquire-keyparam] [--keyid=<keyid>[,..] | [--inquire-keyid]] [--sign-keyid=<fingerprint>[,..] | [--inquire-sign-keyid]]\n"
4521 "Writes the in-memory @abbr{XML} document to disk. The file written to is the "
4522 "file that was opened using the @code{OPEN} command (@pxref{OPEN}). If the "
4523 "file is a new one or the option @option{--inquire-keyparam} was passed, a "
4524 "new keypair will be generated and @cite{pinentry(1)} will be used to prompt "
4525 "for the passphrase of the secret key used for signing."
4526 "@*@*"
4527 "The @option{--inquire-keyparam} option will send an "
4528 "@emph{INQUIRE} to the client to obtain the key parameters to use for "
4529 "generating the new keypair. The inquired data is expected to be in "
4530 "@cite{gnupg} @abbr{XML} format. See the @cite{gnupg} documentation for "
4531 "details. Note that when this option is specified a new keypair will be "
4532 "generated reguardless if the file is a new one and that the passphrase for "
4533 "the current file will be required before generating the new keypair. This "
4534 "option is available to non-invoking clients (@pxref{Access Control}) only "
4535 "when the file is a new one."
4536 "@*@*"
4537 "You can encrypt the data file to a recipient other than the one that it "
4538 "was encrypted with by passing the @option{--keyid} or "
4539 "@option{--inquire-keyid} option with "
4540 "the key ID of a public encryption key as its argument. Use the "
4541 "@command{LISTKEYS} command (@pxref{LISTKEYS}) to show key information by "
4542 "pattern. The @option{--sign-keyid} or @option{--inquire-sign-keyid} option "
4543 "may also be used to sign the data "
4544 "file with an alternate key by specifying the key ID of a secret key. "
4545 "A passphrase to decrypt the data file "
4546 "will be required if one or more of the original encryption or signing keys "
4547 "are not found in either of these two options' arguments. The original "
4548 "encryption or signing keys will be used when either of these options are "
4549 "not specified."
4550 "@*@*"
4551 "The @option{--keyid} and @option{--sign-keyid} options are not available "
4552 "for non-invoking clients "
4553 "(@pxref{Access Control}) when the recipients or signers do not match those "
4554 "that were used when the file was @code{OPEN}'ed."
4555 "@*@*"
4556 "The @option{--symmetric} option specifies that a new data file be "
4557 "conventionally encrypted. These types of data files do not use a recipient "
4558 "public key but may be signed by using the @option{--sign-keyid} or "
4559 "@option{--inquire-sign-keyid} options. To remove all signers from a "
4560 "symmtrically encrypted data file, leave the option value empty. Note that "
4561 "you cannot change encryption schemes once a data file has been saved."
4564 new_command("ISCACHED", 1, 0, 0, iscached_command, _(
4565 "ISCACHED [--lock] [--agent [--sign]] <filename>\n"
4566 "Determines the file cache status of the specified @var{filename}. "
4567 "The default is to test whether the filename is cached in memory. Passing "
4568 "option @option{--agent} will test the @command{gpg-agent} cache for at most "
4569 "one cached key used for opening the data file (@pxref{OPEN}). To test if "
4570 "a signing key is cached, pass @option{--sign} along with @option{--agent}. "
4571 "Both the @option{--agent} and @option{--sign} options require an opened data "
4572 "file."
4573 "@*@*"
4574 "An @emph{OK} response is returned if the specified @var{filename} is found "
4575 "in the cache. If not found in the cache but exists on the filesystem "
4576 "then @code{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4577 "returned."
4578 "@*@*"
4579 "The @option{--lock} option will lock the file mutex of @var{filename} when "
4580 "the file exists; it does not need to be opened nor cached. The lock will be "
4581 "released when the client exits or sends the @code{UNLOCK} command "
4582 "(@pxref{UNLOCK}). When this option is passed the current data file is closed."
4585 new_command("CLEARCACHE", 1, 1, 0, clearcache_command, _(
4586 "CLEARCACHE [<filename>]\n"
4587 "Clears a file cache entry for all or the specified @var{filename}. Note that "
4588 "this will also clear any @command{gpg-agent} cached keys which may cause "
4589 "problems if another data file shares the same keys as @var{filename}."
4590 "@*@*"
4591 "When clearing all cache entries a permissions test is done against the "
4592 "current client based on the @var{access} configuration parameter in a "
4593 "@var{filename} section. Both a cache entry may be cleared and an error "
4594 "returned depending on cached data files and client permissions."
4597 new_command("CACHETIMEOUT", 1, 1, 0, cachetimeout_command, _(
4598 "CACHETIMEOUT <filename> <seconds>\n"
4599 "The time in @var{seconds} until @var{filename} will be removed from the "
4600 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4601 "the passphrase for each @code{OPEN} command (@pxref{OPEN}) or @code{SAVE} "
4602 "(@pxref{SAVE}) command. @xref{Configuration}, and the @code{cache_timeout} "
4603 "parameter."
4606 new_command("LIST", 0, 1, 0, list_command, _(
4607 "LIST [--inquire] [--recurse] [element[<TAB>child[..]]]\n"
4608 "If no element path is given then a newline separated list of root elements "
4609 "is returned with a data response. If given, then children of the specified "
4610 "element path are returned."
4611 "@*@*"
4612 "Each element path "
4613 "returned will have zero or more flags appened to it. These flags are "
4614 "delimited from the element path by a single space character. A flag itself "
4615 "is a single character. Flag @code{P} indicates that access to the element "
4616 "is denied. Flag @code{+} indicates that there are child nodes of "
4617 "the current element path. Flag @code{E} indicates that an element of the "
4618 "element path contained in a @var{target} attribute could not be found. Flag "
4619 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4620 "(@pxref{Configuration}). Flag @code{T}, followed by a single space character, "
4621 "then an element path, is the element path of the @var{target} attribute "
4622 "contained in the current element."
4623 "@*@*"
4624 "When a specified element path contains an error, beit from the final "
4625 "element in the path or any previous element, the path is still shown but "
4626 "will contain the error flag for the element with the error. Determining "
4627 "the actual element which contains the error is up to the client. This can be "
4628 "done by traversing the final element up to parent elements that contain the "
4629 "same error flag."
4630 "@*@*"
4631 "The option @option{--recurse} may be used to list the entire element tree "
4632 "for a specified element path or the entire tree for all root elements."
4633 "@*@*"
4634 "When the @option{--inquire} option is passed then all remaining non-option "
4635 "arguments are retrieved via a server @emph{INQUIRE}."
4638 new_command("REALPATH", 0, 1, 0, realpath_command, _(
4639 "REALPATH [--inquire] element[<TAB>child[..]]\n"
4640 "Resolves all @code{target} attributes of the specified element path and "
4641 "returns the result with a data response. @xref{Target Attribute}, for details."
4642 "@*@*"
4643 "When the @option{--inquire} option is passed then all remaining non-option "
4644 "arguments are retrieved via a server @emph{INQUIRE}."
4647 new_command("STORE", 0, 1, 0, store_command, _(
4648 "STORE element[<TAB>child[..]]<TAB>[content]\n"
4649 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4650 "@*@*"
4651 "Creates a new element path or modifies the @var{content} of an existing "
4652 "element. If only a single element is specified then a new root element is "
4653 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4654 "set to the final @key{TAB} delimited element. If no @var{content} is "
4655 "specified after the final @key{TAB}, then the content of the existing "
4656 "element will be removed; or will be empty if creating a new element."
4657 "@*@*"
4658 "The only restriction of an element name is that it not contain whitespace "
4659 "characters. There is no other whitespace between the @key{TAB} delimited "
4660 "elements. It is recommended that the content of an element be base64 encoded "
4661 "when it contains control or @key{TAB} characters to prevent @abbr{XML} "
4662 "parsing and @command{pwmd} syntax errors."
4665 new_command("RENAME", 0, 1, 0, rename_command, _(
4666 "RENAME [--inquire] element[<TAB>child[..]] <value>\n"
4667 "Renames the specified @var{element} to the new @var{value}. If an element of "
4668 "the same name as the @var{value} already exists it will be overwritten."
4669 "@*@*"
4670 "When the @option{--inquire} option is passed then all remaining non-option "
4671 "arguments are retrieved via a server @emph{INQUIRE}."
4674 new_command("COPY", 0, 1, 0, copy_command, _(
4675 "COPY [--inquire] source[<TAB>child[..]] dest[<TAB>child[..]]\n"
4676 "Copies the entire element tree starting from the child node of the source "
4677 "element, to the destination element path. If the destination element path "
4678 "does not exist then it will be created; otherwise it is overwritten."
4679 "@*@*"
4680 "Note that attributes from the source element are merged into the "
4681 "destination element when the destination element path exists. When an "
4682 "attribute of the same name exists in both the source and destination "
4683 "elements then the destination attribute will be updated to the source "
4684 "attribute value."
4685 "@*@*"
4686 "When the @option{--inquire} option is passed then all remaining non-option "
4687 "arguments are retrieved via a server @emph{INQUIRE}."
4690 new_command("MOVE", 0, 1, 0, move_command, _(
4691 "MOVE [--inquire] source[<TAB>child[..]] [dest[<TAB>child[..]]]\n"
4692 "Moves the source element path to the destination element path. If the "
4693 "destination is not specified then it will be moved to the root node of the "
4694 "document. If the destination is specified and exists then it will be "
4695 "overwritten; otherwise non-existing elements of the destination element "
4696 "path will be created."
4697 "@*@*"
4698 "When the @option{--inquire} option is passed then all remaining non-option "
4699 "arguments are retrieved via a server @emph{INQUIRE}."
4702 new_command("DELETE", 0, 1, 0, delete_command, _(
4703 "DELETE [--inquire] element[<TAB>child[..]]\n"
4704 "Removes the specified element path and all of its children. This may break "
4705 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4706 "refers to this element or any of its children."
4707 "@*@*"
4708 "When the @option{--inquire} option is passed then all remaining non-option "
4709 "arguments are retrieved via a server @emph{INQUIRE}."
4712 new_command("GET", 0, 1, 0, get_command, _(
4713 "GET [--inquire] element[<TAB>child[..]]\n"
4714 "Retrieves the content of the specified element. The content is returned "
4715 "with a data response."
4716 "@*@*"
4717 "When the @option{--inquire} option is passed then all remaining non-option "
4718 "arguments are retrieved via a server @emph{INQUIRE}."
4721 new_command("ATTR", 0, 1, 0, attr_command, _(
4722 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] element[<TAB>child[..]] ..\n"
4723 "@table @asis\n"
4724 "@item ATTR SET attribute element[<TAB>child[..]] [value]\n"
4725 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4726 "element. When no @var{value} is specified any existing value will be removed."
4727 "@*@*"
4728 "@item ATTR DELETE attribute element[<TAB>child[..]]\n"
4729 " Removes an attribute from an element. If @var{attribute} is @code{_name} "
4730 "or @code{target} an error is returned. Use the @command{DELETE} command "
4731 "(@pxref{DELETE}) instead."
4732 "@*@*"
4733 "@item ATTR LIST element[<TAB>child[..]]\n"
4734 " Retrieves a newline separated list of attributes names and values "
4735 "from the specified element. Each attribute name and value is space delimited."
4736 "@*@*"
4737 "@item ATTR GET attribute element[<TAB>child[..]]\n"
4738 " Retrieves the value of an @var{attribute} from an element."
4739 "@end table\n"
4740 "@*@*"
4741 "When the @option{--inquire} option is passed then all remaining non-option "
4742 "arguments are retrieved via a server @emph{INQUIRE}."
4743 "@*@*"
4744 "@xref{Target Attribute}, for details about this special attribute and also "
4745 "@pxref{Other Attributes} for other attributes that are handled specially "
4746 "by @command{pwmd}."
4749 new_command("XPATH", 0, 1, 0, xpath_command, _(
4750 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4751 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4752 "specified it is assumed the expression is a request to return a result. "
4753 "Otherwise, the result is set to the @var{value} argument and the document is "
4754 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4755 "is assumed to be empty and the document is updated. For example:"
4756 "@sp 1\n"
4757 "@example\n"
4758 "XPATH //element[@@_name='password']@key{TAB}\n"
4759 "@end example\n"
4760 "@sp 1\n"
4761 "would clear the content of all @var{password} elements in the data file "
4762 "while leaving off the trailing @key{TAB} would return all @var{password} "
4763 "elements in @abbr{XML} format."
4764 "@*@*"
4765 "When the @option{--inquire} option is passed then all remaining non-option "
4766 "arguments are retrieved via a server @emph{INQUIRE}."
4767 "@*@*"
4768 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4769 "expression syntax."
4772 new_command("XPATHATTR", 0, 1, 0, xpathattr_command, _(
4773 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4774 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4775 "attributes and does not return a result. For the @var{SET} operation the "
4776 "@var{value} is optional but the field is required. If not specified then "
4777 "the attribute value will be empty. For example:"
4778 "@sp 1\n"
4779 "@example\n"
4780 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4781 "@end example\n"
4782 "@sp 1\n"
4783 "would create a @var{password} attribute for each @var{password} element "
4784 "found in the document. The attribute value will be empty but still exist."
4785 "@*@*"
4786 "When the @option{--inquire} option is passed then all remaining non-option "
4787 "arguments are retrieved via a server @emph{INQUIRE}."
4788 "@*@*"
4789 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4790 "expression syntax."
4793 new_command("IMPORT", 0, 1, 0, import_command, _(
4794 "IMPORT [--root=element[<TAB>child[..]]]\n"
4795 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4796 "@*@*"
4797 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4798 "argument is raw @abbr{XML} data. The content is created as a child of "
4799 "the element path specified with the @option{--root} option or at the "
4800 "document root when not specified. Existing elements of the same name will "
4801 "be overwritten."
4802 "@*@*"
4803 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4804 "for details."
4807 new_command("DUMP", 0, 1, 0, dump_command, _(
4808 "DUMP\n"
4809 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4810 "dumping a specific node."
4813 new_command("LOCK", 0, 0, 0, lock_command, _(
4814 "LOCK\n"
4815 "Locks the mutex associated with the opened file. This prevents other clients "
4816 "from sending commands to the same opened file until the client "
4817 "that sent this command either disconnects or sends the @code{UNLOCK} "
4818 "command. @xref{UNLOCK}."
4821 new_command("UNLOCK", 1, 0, 0, unlock_command, _(
4822 "UNLOCK\n"
4823 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4824 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4825 "@pxref{ISCACHED})."
4828 new_command("GETCONFIG", 1, 1, 0, getconfig_command, _(
4829 "GETCONFIG [filename] <parameter>\n"
4830 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4831 "data response. If no file has been opened then the value for @var{filename} "
4832 "or the default from the @var{global} section will be returned. If a file "
4833 "has been opened and no @var{filename} is specified, the value previously "
4834 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4837 new_command("OPTION", 1, 1, 0, option_command, _(
4838 "OPTION <NAME>=[<VALUE>]\n"
4839 "Sets a client option @var{name} to @var{value}. The value for an option is "
4840 "kept for the duration of the connection with the exception of the "
4841 "@command{pinentry} options which are defaults for all future connections "
4842 "(@pxref{Pinentry}). When @var{value} is empty the option is unset."
4843 "@*@*"
4844 "@table @asis\n"
4845 "@item DISABLE-PINENTRY\n"
4846 "Disable use of @command{pinentry} for passphrase retrieval. When @code{1}, a "
4847 "server inquire is sent to the client to obtain the passphrase. This option "
4848 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4849 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands. Set to @code{0} "
4850 "to use a @command{pinentry}."
4851 "@*@*"
4852 "@item DISPLAY\n"
4853 "Set or unset the X11 display to use when prompting for a passphrase."
4854 "@*@*"
4855 "@item TTYNAME\n"
4856 "Set the terminal device path to use when prompting for a passphrase."
4857 "@*@*"
4858 "@item TTYTYPE\n"
4859 "Set the terminal type for use with @option{TTYNAME}."
4860 "@*@*"
4861 "@item NAME\n"
4862 "Associates the thread ID of the connection with the specified textual "
4863 "representation. Useful for debugging log messages. May not contain whitespace."
4864 "@*@*"
4865 "@item LOCK-TIMEOUT\n"
4866 "When not @code{0}, the duration in tenths of a second to wait for the file "
4867 "mutex which has been locked by another thread to be released before returning "
4868 "an error. When @code{-1} the error will be returned immediately."
4869 "@end table\n"
4872 new_command("LS", 1, 1, 0, ls_command, _(
4873 "LS\n"
4874 "Returns a newline separated list of data files stored in the data directory "
4875 "@file{HOMEDIR/data} (@pxref{Invoking}) with a data response."
4878 new_command("RESET", 1, 1, 0, NULL, _(
4879 "RESET\n"
4880 "Closes the currently opened file but keeps any previously set client options "
4881 "(@pxref{OPTION})."
4884 new_command("NOP", 1, 1, 0, NULL, _(
4885 "NOP\n"
4886 "Does nothing. Always returns successfully."
4889 /* !END-HELP-TEXT! */
4890 new_command ("CANCEL", 1, 1, 0, NULL, NULL);
4891 new_command ("END", 1, 1, 0, NULL, NULL);
4892 new_command ("BYE", 1, 1, 0, NULL, NULL);
4894 int i;
4895 for (i = 0; command_table[i]; i++);
4896 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4897 sort_commands);
4900 gpg_error_t
4901 register_commands (assuan_context_t ctx)
4903 int i = 0, rc;
4905 for (; command_table[i]; i++)
4907 if (!command_table[i]->handler)
4908 continue;
4910 rc = assuan_register_command (ctx, command_table[i]->name,
4911 command_table[i]->handler,
4912 command_table[i]->help);
4913 if (rc)
4914 return rc;
4917 rc = assuan_register_bye_notify (ctx, bye_notify);
4918 if (rc)
4919 return rc;
4921 rc = assuan_register_reset_notify (ctx, reset_notify);
4922 if (rc)
4923 return rc;
4925 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4926 if (rc)
4927 return rc;
4929 return assuan_register_post_cmd_notify (ctx, command_finalize);