ATTR SET: check for element error.
[pwmd.git] / src / commands.c
blob7ff34b6f6e73ee7ca4bf0456ad2d8e40e45844ec
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
71 #define FLOCK_TYPE_NONE 0
72 #define FLOCK_TYPE_SH 0x0001
73 #define FLOCK_TYPE_EX 0x0002
74 #define FLOCK_TYPE_KEEP 0x0004
76 struct command_table_s
78 const char *name;
79 gpg_error_t (*handler) (assuan_context_t, char *line);
80 const char *help;
81 int ignore_startup;
82 int unlock; // unlock the file mutex after validating the checksum
83 uint32_t flock_type;
86 static struct command_table_s **command_table;
88 static gpg_error_t do_lock (struct client_s *client, int add);
89 static gpg_error_t validate_checksum (struct client_s *,
90 struct cache_data_s *, unsigned char **,
91 size_t *);
92 static gpg_error_t update_checksum (struct client_s *client);
94 /* When 'status' is true the 'self' field of the status line will be false
95 * because we never send the STATE status message to the same client that
96 * initiated it. */
97 static char *
98 build_client_info_line (struct client_thread_s *thd, int status)
100 int with_state = config_get_boolean ("global", "send_state");
101 char *uid, *username = NULL;
102 char *line;
104 #ifdef WITH_GNUTLS
105 if (thd->remote)
106 uid = str_asprintf("#%s", thd->tls->fp);
107 else
109 uid = str_asprintf("%u", thd->peer->uid);
110 username = get_username (thd->peer->uid);
112 #else
113 uid = str_asprintf("%u", thd->peer->uid);
114 username = get_username (thd->peer->uid);
115 #endif
116 line = str_asprintf ("%p %s %s %s %u %u %u %s %s %u",
117 thd->tid,
118 thd->name ? thd->name : "-",
119 thd->cl && thd->cl->filename
120 && (thd->cl->flags & FLAG_OPEN)
121 ? thd->cl->filename : "/",
122 #ifdef WITH_GNUTLS
123 thd->remote ? thd->peeraddr : "-",
124 #else
125 "-",
126 #endif
127 thd->cl && thd->cl->flags & FLAG_HAS_LOCK ? 1 : 0,
128 !status && pthread_equal (pthread_self (), thd->tid) ? 1 : 0,
129 with_state ? thd->state : CLIENT_STATE_UNKNOWN, uid,
130 #ifdef WITH_GNUTLS
131 thd->remote ? "-" : username,
132 #else
133 username,
134 #endif
135 thd->conntime
138 xfree (username);
139 xfree (uid);
140 return line;
143 void
144 update_client_state (struct client_s *client, unsigned s)
146 MUTEX_LOCK (&cn_mutex);
147 client->thd->state = s;
148 MUTEX_UNLOCK (&cn_mutex);
149 int n = config_get_boolean ("global", "send_state");
151 if (n && client->thd->state != CLIENT_STATE_UNKNOWN)
153 char *line = build_client_info_line (client->thd, 1);
155 if (line)
156 send_status_all_not_self (n == 2, STATUS_STATE, "%s", line);
158 xfree (line);
162 static gpg_error_t
163 unlock_file_mutex (struct client_s *client, int remove)
165 gpg_error_t rc = 0;
167 // OPEN: keep the lock for the same file being reopened.
168 if (client->flags & FLAG_KEEP_LOCK && client->flags & FLAG_HAS_LOCK)
169 return 0;
171 if (!(client->flags & FLAG_HAS_LOCK))
172 return GPG_ERR_NOT_LOCKED;
174 rc = cache_unlock_mutex (client->md5file, remove);
175 if (rc)
176 rc = GPG_ERR_INV_STATE;
177 else
178 client->flags &= ~(FLAG_HAS_LOCK | FLAG_LOCK_CMD);
180 return rc;
183 static gpg_error_t
184 lock_file_mutex (struct client_s *client, int add)
186 gpg_error_t rc = 0;
187 int timeout = config_get_integer (client->filename, "cache_timeout");
189 if (client->flags & FLAG_HAS_LOCK)
190 return 0;
192 rc = cache_lock_mutex (client->ctx, client->md5file,
193 client->lock_timeout, add, timeout);
194 if (!rc)
195 client->flags |= FLAG_HAS_LOCK;
197 return rc;
200 static gpg_error_t
201 file_modified (struct client_s *client, struct command_table_s *cmd)
203 gpg_error_t rc = 0;
204 int type = !cmd->flock_type || (cmd->flock_type & FLOCK_TYPE_SH)
205 ? LOCK_SH : LOCK_EX;
207 if (!(client->flags & FLAG_OPEN))
208 return GPG_ERR_INV_STATE;
210 rc = lock_file_mutex (client, 0);
211 if (rc && rc != GPG_ERR_NO_DATA)
212 return rc;
214 rc = lock_flock (client->ctx, client->filename, type, &client->flock_fd);
215 if (!rc)
217 rc = validate_checksum (client, NULL, NULL, NULL);
218 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
219 rc = 0;
220 else if (rc)
221 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
223 else if (gpg_err_code (rc) == GPG_ERR_ENOENT)
224 rc = 0;
226 /* FLAG_HAS_LOCK may have been set by the LOCK command or OPEN --lock. */
227 if ((cmd->unlock && !(client->flags & FLAG_HAS_LOCK)) || rc)
228 unlock_file_mutex (client, 0);
230 if (rc || !(cmd->flock_type & FLOCK_TYPE_KEEP))
231 unlock_flock (&client->flock_fd);
233 return rc;
236 static gpg_error_t
237 parse_xml (assuan_context_t ctx, int new)
239 struct client_s *client = assuan_get_pointer (ctx);
240 int cached = client->doc != NULL;
241 gpg_error_t rc = 0;
243 if (new)
245 client->doc = xml_new_document ();
246 if (client->doc)
248 xmlChar *result;
249 int len;
251 xmlDocDumpFormatMemory (client->doc, &result, &len, 0);
252 client->crypto->plaintext = result;
253 client->crypto->plaintext_size = len;
254 if (!client->crypto->plaintext)
256 xmlFreeDoc (client->doc);
257 client->doc = NULL;
258 rc = GPG_ERR_ENOMEM;
261 else
262 rc = GPG_ERR_ENOMEM;
264 else if (!cached)
265 rc = xml_parse_doc ((char *) client->crypto->plaintext,
266 client->crypto->plaintext_size,
267 (xmlDocPtr *)&client->doc);
269 return rc;
272 static void
273 free_client (struct client_s *client)
275 if (client->doc)
276 xmlFreeDoc (client->doc);
278 xfree (client->crc);
279 xfree (client->filename);
280 xfree (client->last_error);
281 crypto_cleanup (client->crypto);
282 client->crypto = NULL;
285 void
286 cleanup_client (struct client_s *client)
288 assuan_context_t ctx = client->ctx;
289 struct client_thread_s *thd = client->thd;
290 long lock_timeout = client->lock_timeout;
291 xmlErrorPtr xml_error = client->xml_error;
292 int no_pinentry = (client->flags & FLAG_NO_PINENTRY);
293 int flock_fd = client->flock_fd;
295 unlock_file_mutex (client, client->flags & FLAG_NEW);
296 free_client (client);
297 memset (client, 0, sizeof (struct client_s));
298 client->flock_fd = flock_fd;
299 client->xml_error = xml_error;
300 client->ctx = ctx;
301 client->thd = thd;
302 client->lock_timeout = lock_timeout;
303 client->flags |= no_pinentry ? FLAG_NO_PINENTRY : 0;
306 static void
307 req_cleanup (void *arg)
309 if (!arg)
310 return;
312 strv_free ((char **) arg);
315 static gpg_error_t
316 parse_open_opt_lock (void *data, void *value)
318 struct client_s *client = data;
320 client->opts |= OPT_LOCK_ON_OPEN;
321 return 0;
324 static gpg_error_t
325 parse_opt_inquire (void *data, void *value)
327 struct client_s *client = data;
329 (void) value;
330 client->opts |= OPT_INQUIRE;
331 return 0;
334 static gpg_error_t
335 update_checksum (struct client_s *client)
337 unsigned char *crc;
338 size_t len;
339 struct cache_data_s *cdata;
340 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
342 if (rc)
343 return rc;
345 xfree (client->crc);
346 client->crc = crc;
347 cdata = cache_get_data (client->md5file);
348 if (cdata)
350 xfree (cdata->crc);
351 cdata->crc = xmalloc (len);
352 memcpy (cdata->crc, crc, len);
355 return 0;
358 static gpg_error_t
359 validate_checksum (struct client_s *client, struct cache_data_s *cdata,
360 unsigned char **r_crc, size_t *r_crclen)
362 unsigned char *crc;
363 size_t len;
364 gpg_error_t rc;
365 int n = 0;
367 if (cdata && !cdata->crc)
368 return GPG_ERR_CHECKSUM;
370 rc = get_checksum (client->filename, &crc, &len);
371 if (rc)
372 return rc;
374 if (cdata)
375 n = memcmp (cdata->crc, crc, len);
376 else if (client->crc)
377 n = memcmp (client->crc, crc, len);
379 if (!n && r_crc)
381 *r_crc = crc;
382 *r_crclen = len;
384 else
385 xfree (crc);
387 return n ? GPG_ERR_CHECKSUM : 0;
390 static gpg_error_t
391 open_command (assuan_context_t ctx, char *line)
393 gpg_error_t rc;
394 struct client_s *client = assuan_get_pointer (ctx);
395 char **req, *filename;
396 unsigned char md5file[16];
397 int same_file = 0;
398 assuan_peercred_t peer;
399 struct cache_data_s *cdata = NULL;
400 int cached = 0;
401 unsigned char *crc = NULL;
402 size_t crclen = 0;
403 struct argv_s *args[] = {
404 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
405 NULL
408 rc = parse_options (&line, args, client, 1);
409 if (rc)
410 return send_error (ctx, rc);
412 req = str_split (line, " ", 2);
413 if (!req)
414 return send_error (ctx, GPG_ERR_SYNTAX);
416 rc = do_validate_peer (ctx, req[0], &peer);
417 if (rc)
419 strv_free (req);
420 return send_error (ctx, rc);
423 filename = req[0];
424 if (!valid_filename (filename))
426 strv_free (req);
427 return send_error (ctx, GPG_ERR_INV_VALUE);
430 pthread_cleanup_push ((void *)req_cleanup, req);
431 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, filename, strlen (filename));
432 /* This client may have locked a different file with ISCACHED --lock than
433 * the current filename. This will remove that lock. */
434 same_file = !memcmp (md5file, client->md5file, 16) ? 1 : 0;
435 if (client->flags & FLAG_OPEN ||
436 (client->flags & FLAG_HAS_LOCK && !same_file))
438 uint32_t opts = client->opts;
439 uint32_t flags = client->flags;
441 if (same_file)
442 client->flags |= FLAG_KEEP_LOCK;
443 else if (client->flags & FLAG_NEW)
444 cache_clear (client->md5file);
446 cleanup_client (client);
447 client->opts = opts;
448 client->flags |= flags;
449 client->flags &= ~(FLAG_LOCK_CMD);
450 if (!same_file)
451 client->flags &= ~(FLAG_HAS_LOCK | FLAG_NEW);
454 memcpy (client->md5file, md5file, 16);
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;
479 rc = crypto_init (&client->crypto, client->ctx, client->filename,
480 client->flags & FLAG_NO_PINENTRY);
481 if (!rc && stat (client->filename, &st) == -1)
482 rc = gpg_error_from_errno (errno);
483 else if (!rc && !S_ISREG (st.st_mode)) // to match LS output
484 rc = gpg_error_from_errno (ENOANO);
487 if (!rc || gpg_err_code (rc) == GPG_ERR_ENOENT)
489 cdata = cache_get_data (client->md5file);
491 if (rc) // new file
493 rc = 0;
494 client->flags |= FLAG_NEW;
495 // data file disappeared. clear the cache entry.
496 cache_clear (client->md5file);
497 cdata = NULL;
499 else if (cdata && cdata->doc) // cached document
501 int reload = 0;
502 int defer = 0;
504 if (!rc && !(client->flags & FLAG_NEW))
506 rc = validate_checksum (client, cdata, &crc, &crclen);
507 if (rc == GPG_ERR_CHECKSUM)
509 rc = 0;
510 reload = 1;
514 if (!rc)
516 rc = cache_iscached (client->filename, &defer);
517 if ((!rc || rc == GPG_ERR_ENOENT) && defer)
519 rc = 0;
520 reload = 2;
524 if (!rc && reload)
526 if (reload == 1)
527 log_write ("%s: %s", client->filename,
528 pwmd_strerror (GPG_ERR_CHECKSUM));
529 cache_clear (client->md5file);
530 cdata = NULL;
531 rc = crypto_decrypt (client, client->crypto);
533 #ifdef WITH_GNUTLS
534 else if (!rc)
536 if (client->thd->remote
537 && config_get_boolean (client->filename, "tcp_require_key")
538 && !(client->flags & FLAG_NEW))
539 rc = crypto_try_decrypt (client,
540 (client->flags & FLAG_NO_PINENTRY));
542 #endif
544 if (!rc && cdata)
546 client->crypto->plaintext = cdata->doc;
547 client->crypto->plaintext_size = cdata->size;
548 rc = cache_decrypt (client->crypto);
549 if (!rc)
551 cdata->doc = NULL;
552 cdata->size = 0;
553 strv_free (client->crypto->pubkey);
554 strv_free (client->crypto->sigkey);
555 client->crypto->pubkey = strv_dup (cdata->pubkey);
556 client->crypto->sigkey = strv_dup (cdata->sigkey);
557 cached = 1;
559 else
561 client->crypto->plaintext = NULL;
562 client->crypto->plaintext_size = 0;
566 else // existing file
568 cached = cdata != NULL;
569 rc = crypto_decrypt (client, client->crypto);
573 if (!rc)
575 rc = parse_xml (ctx, client->flags & FLAG_NEW);
576 if (!rc)
578 rc = cache_encrypt (client->crypto);
579 if (rc)
580 cache_clear (client->md5file);
581 else
583 int timeout = config_get_integer (client->filename,
584 "cache_timeout");
586 free_cache_data_once (cdata);
587 cdata = xcalloc (1, sizeof (struct cache_data_s));
588 cdata->doc = client->crypto->plaintext;
589 cdata->size = client->crypto->plaintext_size;
590 cdata->pubkey = strv_dup(client->crypto->pubkey);
591 cdata->sigkey = strv_dup(client->crypto->sigkey);
592 client->crypto->plaintext = NULL;
593 client->crypto->plaintext_size = 0;
595 if (cached) // wont increment the refcount
597 /* Prevent using another FD to update the checksum for a
598 * cached data file. The validity has already been
599 * verified. */
600 xfree (client->crc);
601 client->crc = xmalloc (crclen);
602 memcpy (client->crc, crc, crclen);
603 xfree (cdata->crc);
604 cdata->crc = xmalloc (crclen);
605 memcpy (cdata->crc, crc, crclen);
607 rc = cache_set_data (client->md5file, cdata);
608 /* The cache entry may have been removed and cache_set_data()
609 * already sent STATUS_CACHE. */
610 if (!cache_iscached (client->filename, NULL))
611 rc = send_status (ctx, STATUS_CACHE, NULL);
613 else
614 rc = cache_add_file (client->md5file, cdata, timeout);
619 pthread_cleanup_pop (1);
620 xfree (crc);
622 if (!rc && !(client->flags & FLAG_NEW) && !cached)
623 rc = update_checksum (client);
625 if (!rc)
627 client->flags |= FLAG_OPEN;
629 if (client->flags & FLAG_NEW)
630 rc = send_status (ctx, STATUS_NEWFILE, NULL);
632 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
633 rc = do_lock (client, 0);
636 if (rc)
638 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
640 if (client->flags & FLAG_NEW)
641 cache_clear (client->md5file);
643 crypto_cleanup (client->crypto);
644 client->crypto = NULL;
645 client->flags &= ~FLAG_OPEN;
648 crypto_cleanup_non_keys (client->crypto);
649 return send_error (ctx, rc);
652 /* If not the invoking_user or is an existing file, check that the list of user
653 * supplied key ID's are in the list of current key ID's obtained when
654 * decrypting the data file. Both short and long form keyid fingerprints are
655 * valid:
657 * 8 byte with optional "0x" prefix
658 * 16 byte
659 * 40 byte
661 * Although, the key ID's are converted to the 8 byte form before calling this
662 * function in parse_save_opt_keyid_common() to keep consistancy with KEYINFO
663 * output.
665 static gpg_error_t
666 permitted_to_save (struct client_s *client, const char **keys,
667 const char **value)
669 gpg_error_t rc = 0;
670 const char **v;
672 if ((client->flags & FLAG_NEW) || (client->opts & OPT_SYMMETRIC))
673 return 0;
675 rc = peer_is_invoker (client);
676 if (!rc)
677 return 0;
679 /* Empty match. */
680 if ((!value && !keys) || ((value && !*value) && (keys && !*keys)))
681 return 0;
683 for (v = value; v && *v; v++)
685 const char **k, *pv = *v;
686 int match = 0;
688 if (*pv == '0' && *(pv+1) == 'x')
689 pv += 2;
691 for (k = keys; k && *k; k++)
693 const char *pk = *k;
695 if (*pk == '0' && *(pk+1) == 'x')
696 pk += 2;
698 rc = !strcmp (pv, pk) ? 0 : GPG_ERR_FORBIDDEN;
699 if (rc)
700 rc = !strcmp (pv, *k) ? 0 : GPG_ERR_FORBIDDEN;
702 if (!rc)
704 match = 1;
705 break;
709 if (!match)
710 return GPG_ERR_FORBIDDEN;
713 return rc;
716 static gpg_error_t
717 parse_save_opt_keyid_common (struct client_s *client, const char **list,
718 const char *value, char ***dst)
720 gpg_error_t rc;
721 char **p = NULL;
723 if (value && *value)
725 p = str_split (value, ",", 0);
726 if (!p)
727 return GPG_ERR_ENOMEM;
730 crypto_keyid_to_8b (p);
731 rc = permitted_to_save (client, list, (const char **)p);
732 if (!rc)
733 *dst = p;
734 else
735 strv_free (*dst);
737 return rc;
740 static gpg_error_t
741 parse_save_opt_keyid (void *data, void *value)
743 struct client_s *client = data;
744 const char *str = value;
745 char **dst = NULL;
746 gpg_error_t rc;
748 rc = parse_save_opt_keyid_common (client,
749 (const char **)client->crypto->pubkey,
750 str, &dst);
751 if (rc)
752 return rc;
754 client->crypto->save.pubkey = dst;
755 return 0;
758 static gpg_error_t
759 parse_save_opt_sign_keyid (void *data, void *value)
761 struct client_s *client = data;
762 const char *str = value;
763 char **dst = NULL;
764 gpg_error_t rc;
766 rc = parse_save_opt_keyid_common (client,
767 (const char **)client->crypto->sigkey,
768 str, &dst);
769 if (rc)
770 return rc;
772 if (!dst)
773 client->opts |= OPT_NO_SIGNER;
775 client->crypto->save.sigkey = dst;
776 return 0;
779 static gpg_error_t
780 parse_save_opt_inquire (struct client_s *client, uint32_t opt)
782 if (opt == OPT_INQUIRE && !(client->flags & FLAG_NEW))
784 gpg_error_t rc = peer_is_invoker (client);
786 if (rc)
787 return rc;
790 client->opts |= opt;
791 return 0;
794 static gpg_error_t
795 parse_save_opt_inquire_keyparam (void *data, void *value)
797 return parse_save_opt_inquire (data, OPT_INQUIRE);
800 static gpg_error_t
801 parse_save_opt_inquire_keyid (void *data, void *value)
803 return parse_save_opt_inquire (data, OPT_INQUIRE_KEYID);
806 static gpg_error_t
807 parse_save_opt_inquire_sign_keyid (void *data, void *value)
809 return parse_save_opt_inquire (data, OPT_INQUIRE_SIGN_KEYID);
812 static gpg_error_t
813 parse_save_opt_symmetric (void *data, void *value)
815 struct client_s *client = data;
817 client->opts |= OPT_SYMMETRIC;
818 return 0;
821 /* Tests that the keys in new_keys are also in old_keys. */
822 static gpg_error_t
823 compare_keys (char **new_keys, char **old_keys)
825 char **o;
827 if (!old_keys || !*old_keys)
828 return 0;
830 crypto_keyid_to_8b (new_keys);
832 for (o = old_keys; *o; o++)
834 char **n;
836 for (n = new_keys; *n; n++)
838 if (!strcmp (*n, *o))
839 break;
842 if (!*n)
843 return GPG_ERR_NOT_FOUND;
846 return 0;
849 static gpg_error_t
850 inquire_keyid (struct client_s *client, uint32_t opt)
852 gpg_error_t rc;
853 unsigned char *result = NULL;
854 size_t len;
855 char *s;
856 char ***orig;
857 char ***save;
859 if (opt == OPT_INQUIRE_KEYID)
861 s = "INQUIRE_KEYID";
862 orig = &client->crypto->pubkey;
863 save = &client->crypto->save.pubkey;
865 else
867 s = "INQUIRE_SIGN_KEYID";
868 orig = &client->crypto->sigkey;
869 save = &client->crypto->save.sigkey;
872 rc = assuan_inquire (client->ctx, s, &result, &len, 0);
873 if (!rc)
875 char **dst = NULL;
877 rc = parse_save_opt_keyid_common (client, (const char **)*orig,
878 (char *)result, &dst);
879 if (!rc)
881 if (!dst && opt == OPT_INQUIRE_SIGN_KEYID
882 && client->opts & OPT_SYMMETRIC)
883 client->opts |= OPT_NO_SIGNER;
885 *save = dst;
889 xfree (result);
890 return rc;
893 static gpg_error_t
894 save_command (assuan_context_t ctx, char *line)
896 struct client_s *client = assuan_get_pointer (ctx);
897 struct cache_data_s *cdata = NULL;
898 gpg_error_t rc = 0, cached = 0;
899 int defer = 0;
900 struct stat st;
901 struct argv_s *args[] = {
902 &(struct argv_s) {"keyid", OPTION_TYPE_ARG, parse_save_opt_keyid},
903 &(struct argv_s) {"sign-keyid", OPTION_TYPE_OPTARG,
904 parse_save_opt_sign_keyid},
905 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
906 parse_save_opt_inquire_keyparam },
907 &(struct argv_s) {"inquire-keyid", OPTION_TYPE_NOARG,
908 parse_save_opt_inquire_keyid },
909 &(struct argv_s) {"inquire-sign-keyid", OPTION_TYPE_NOARG,
910 parse_save_opt_inquire_sign_keyid },
911 &(struct argv_s) {"symmetric", OPTION_TYPE_NOARG,
912 parse_save_opt_symmetric },
913 NULL
916 crypto_cleanup_save (&client->crypto->save);
918 if (!(client->flags & FLAG_NEW))
920 rc = crypto_is_symmetric (client->filename);
921 if (!rc || rc == GPG_ERR_BAD_DATA)
923 if (!rc)
924 client->opts |= OPT_SYMMETRIC;
926 rc = 0; // PKI
930 if (rc)
931 return send_error (ctx, rc);
933 rc = parse_options (&line, args, client, 0);
934 if (rc)
935 return send_error (ctx, rc);
937 if (config_get_boolean (client->filename, "require_save_key"))
938 client->opts |= OPT_ASK;
940 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
941 return send_error (ctx, gpg_error_from_errno (errno));
943 if (errno != ENOENT && !S_ISREG (st.st_mode))
945 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
946 return send_error (ctx, GPG_ERR_ENOANO);
949 if (!rc)
950 cached = rc = cache_iscached (client->filename, &defer);
952 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
953 rc = 0;
954 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
955 rc = 0;
957 if (rc)
958 return send_error (ctx, rc);
960 /* Specifying both a recipient and symmetric encryption is an error. */
961 if (client->opts & OPT_INQUIRE_KEYID && client->opts & OPT_SYMMETRIC)
963 return send_error (ctx, GPG_ERR_CONFLICT);
965 else if (client->opts & OPT_INQUIRE && client->opts & OPT_SYMMETRIC)
967 return send_error (ctx, GPG_ERR_CONFLICT);
969 /* Existing file with a recipient and wanting symmetric is an error. */
970 else if (client->opts & OPT_SYMMETRIC && client->crypto->save.pubkey)
972 return send_error (ctx, GPG_ERR_CONFLICT);
974 else if (((client->opts & OPT_INQUIRE_KEYID)
975 || (client->opts & OPT_INQUIRE_SIGN_KEYID)))
977 if (client->opts & OPT_INQUIRE)
978 return send_error (ctx, GPG_ERR_CONFLICT);
980 if (client->opts & OPT_INQUIRE_KEYID)
981 rc = inquire_keyid (client, OPT_INQUIRE_KEYID);
983 if (!rc && (client->opts & OPT_INQUIRE_SIGN_KEYID))
984 rc = inquire_keyid (client, OPT_INQUIRE_SIGN_KEYID);
986 else if ((client->crypto->save.pubkey || client->crypto->save.sigkey)
987 && client->opts & OPT_INQUIRE)
988 return send_error (ctx, GPG_ERR_CONFLICT);
990 if (!rc)
991 rc = crypto_init_ctx (client->crypto, client->flags & FLAG_NO_PINENTRY);
993 if (!rc && (client->opts & OPT_INQUIRE))
995 /* Require a passphrase when generating a new key pair for an existing
996 * file.
998 if (!(client->flags & FLAG_NEW))
1000 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
1001 client->opts &= ~OPT_ASK;
1004 if (!rc)
1006 unsigned char *params = NULL;
1007 size_t len;
1009 rc = assuan_inquire (client->ctx, "KEYPARAM", &params, &len, 0);
1010 if (!rc)
1012 client->crypto->flags |= CRYPTO_FLAG_NEWFILE;
1013 pthread_cleanup_push ((void *)xfree, params);
1014 rc = crypto_genkey (client, client->crypto, params);
1015 pthread_cleanup_pop (1);
1019 else if (!rc && (client->flags & FLAG_NEW))
1021 if (!client->crypto->save.pubkey && !client->crypto->save.sigkey
1022 && !(client->opts & OPT_SYMMETRIC))
1024 char *params = crypto_default_key_params ();
1026 if (!params)
1027 rc = GPG_ERR_ENOMEM;
1029 if (!rc)
1031 client->crypto->flags |= CRYPTO_FLAG_NEWFILE;
1032 pthread_cleanup_push ((void *)xfree, params);
1033 rc = crypto_genkey (client, client->crypto,
1034 (unsigned char *)params);
1035 pthread_cleanup_pop (1);
1038 else
1040 if (!client->crypto->save.pubkey && client->crypto->save.sigkey
1041 && !(client->opts & OPT_SYMMETRIC))
1042 client->crypto->save.pubkey = strv_dup (client->crypto->save.sigkey);
1044 if (client->crypto->save.pubkey && !client->crypto->save.sigkey)
1045 client->crypto->save.sigkey = strv_dup (client->crypto->save.pubkey);
1048 else if (!rc)
1050 cdata = cache_get_data (client->md5file);
1051 if (cdata)
1053 if (client->crypto->save.pubkey)
1054 rc = compare_keys (client->crypto->save.pubkey, cdata->pubkey);
1056 /* Always allow a signer for symmetric data files. */
1057 if (!rc && client->crypto->save.sigkey
1058 && !(client->opts & OPT_SYMMETRIC))
1059 rc = compare_keys (client->crypto->save.sigkey, cdata->sigkey);
1061 /* Prevent saving to a recipient who is not in the original recipient
1062 * list without a passphrase. */
1063 if (rc || (client->crypto->sigkey && client->opts & OPT_NO_SIGNER))
1064 client->opts |= OPT_ASK;
1066 if (rc == GPG_ERR_NOT_FOUND)
1068 rc = peer_is_invoker (client);
1069 if (rc == GPG_ERR_EACCES)
1070 rc = GPG_ERR_FORBIDDEN;
1073 if (!client->crypto->save.pubkey
1074 && !(client->opts & OPT_SYMMETRIC))
1075 client->crypto->save.pubkey = strv_dup (cdata->pubkey);
1077 if (!client->crypto->save.sigkey && cdata->sigkey
1078 && !(client->opts & OPT_NO_SIGNER))
1079 client->crypto->save.sigkey = strv_dup (cdata->sigkey);
1080 else if (!client->crypto->save.sigkey
1081 && (client->opts & OPT_NO_SIGNER)
1082 && !(client->opts & OPT_SYMMETRIC))
1083 rc = GPG_ERR_NO_SIGNATURE_SCHEME;
1085 else
1087 client->crypto->save.pubkey = strv_dup (client->crypto->pubkey);
1088 client->crypto->save.sigkey = strv_dup (client->crypto->sigkey);
1092 if (!rc && !(client->flags & FLAG_NEW))
1094 /* Fixes {NEW_,SIGN_}PASSPHRASE inquire keywords. See passphrase_cb(). */
1095 if (client->opts & OPT_SYMMETRIC)
1096 client->crypto->flags |= CRYPTO_FLAG_PASSWD;
1098 if (client->opts & OPT_ASK)
1099 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
1102 if (!rc && client->opts & OPT_SYMMETRIC)
1103 client->crypto->flags |= CRYPTO_FLAG_PASSWD_NEW;
1105 if (!rc)
1106 rc = xml_update_element_mtime (client, xmlDocGetRootElement (client->doc));
1108 if (!rc)
1110 int size;
1112 xmlDocDumpFormatMemory (client->doc, &client->crypto->plaintext, &size,
1114 if (size > 0)
1115 client->crypto->plaintext_size = (size_t) size;
1116 else
1117 rc = GPG_ERR_ENOMEM;
1119 if (!rc)
1121 client->crypto->flags |= (client->opts & OPT_SYMMETRIC) ? CRYPTO_FLAG_SYMMETRIC : 0;
1122 client->crypto->flags |= (client->flags & FLAG_NEW) ? CRYPTO_FLAG_NEWFILE : 0;
1123 rc = crypto_encrypt (client, client->crypto);
1124 client->crypto->flags &= ~CRYPTO_FLAG_SYMMETRIC;
1127 if (!rc)
1129 rc = crypto_write_file (client->crypto);
1130 if (!rc)
1132 if (!cached) // no error
1133 cdata = cache_get_data (client->md5file);
1136 if (!rc)
1138 rc = cache_encrypt (client->crypto);
1139 if (rc)
1141 cache_clear (client->md5file);
1142 strv_free (client->crypto->pubkey);
1143 client->crypto->pubkey = strv_dup (client->crypto->save.pubkey);
1144 strv_free (client->crypto->sigkey);
1145 client->crypto->sigkey = strv_dup (client->crypto->save.sigkey);
1146 client->flags &= ~(FLAG_NEW);
1150 if (!rc)
1152 int timeout = config_get_integer (client->filename,
1153 "cache_timeout");
1155 free_cache_data_once (cdata);
1156 cdata = xcalloc (1, sizeof (struct cache_data_s));
1157 cdata->doc = client->crypto->plaintext;
1158 client->crypto->plaintext = NULL;
1159 cdata->size = client->crypto->plaintext_size;
1160 client->crypto->plaintext_size = 0;
1161 cdata->pubkey = client->crypto->save.pubkey;
1162 client->crypto->save.pubkey = NULL;
1163 cdata->sigkey = client->crypto->save.sigkey;
1164 client->crypto->save.sigkey = NULL;
1166 /* Update in case the cache entry expires the next SAVE may not
1167 * have any known keys. */
1168 strv_free (client->crypto->pubkey);
1169 client->crypto->pubkey = strv_dup (cdata->pubkey);
1170 strv_free (client->crypto->sigkey);
1171 client->crypto->sigkey = strv_dup (cdata->sigkey);
1173 if (!cached) // no error and wont increment refcount
1174 rc = cache_set_data (client->md5file, cdata);
1175 else
1176 rc = cache_add_file (client->md5file, cdata, timeout);
1178 if (!rc && (cached || (client->flags & FLAG_NEW)))
1179 send_status_all (STATUS_CACHE, NULL);
1181 if (!rc)
1183 rc = update_checksum (client);
1184 client->flags &= ~(FLAG_NEW);
1190 crypto_cleanup_non_keys (client->crypto);
1191 return send_error (ctx, rc);
1194 static gpg_error_t
1195 do_delete (assuan_context_t ctx, char *line)
1197 struct client_s *client = assuan_get_pointer (ctx);
1198 struct xml_request_s *req;
1199 xmlNodePtr n;
1200 gpg_error_t rc;
1202 rc = xml_new_request (client, line, XML_CMD_DELETE, &req);
1203 if (rc)
1204 return rc;
1206 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1207 xml_free_request (req);
1208 if (rc)
1209 return rc;
1211 rc = xml_is_element_owner (client, n);
1212 if (!rc)
1213 rc = xml_unlink_node (client, n);
1215 return rc;
1218 static gpg_error_t
1219 delete_command (assuan_context_t ctx, char *line)
1221 struct client_s *client = assuan_get_pointer (ctx);
1222 gpg_error_t rc;
1223 struct argv_s *args[] = {
1224 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1225 NULL
1228 rc = parse_options (&line, args, client, 1);
1229 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1230 rc = GPG_ERR_SYNTAX;
1231 if (rc)
1232 return send_error (ctx, rc);
1234 if (client->opts & OPT_INQUIRE)
1236 unsigned char *result;
1237 size_t len;
1239 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1240 if (rc)
1241 return send_error (ctx, rc);
1243 pthread_cleanup_push ((void *)xfree, result);
1244 rc = do_delete (ctx, (char *)result);
1245 pthread_cleanup_pop (1);
1247 else
1248 rc = do_delete (ctx, line);
1250 return send_error (ctx, rc);
1253 static gpg_error_t
1254 update_element_expirey (struct client_s *client, xmlNodePtr n)
1256 gpg_error_t rc = 0;
1257 xmlChar *expire, *incr;
1258 char *p;
1259 time_t e, e_orig, i;
1260 time_t now = time (NULL);
1262 expire = xml_attribute_value (n, (xmlChar *)"expire");
1263 if (!expire)
1264 return 0;
1266 errno = 0;
1267 e = strtoul ((char *)expire, &p, 10);
1268 if (errno || *p || e == ULONG_MAX || *expire == '-')
1270 xmlFree (expire);
1271 rc = GPG_ERR_ERANGE;
1272 goto fail;
1275 xmlFree (expire);
1276 incr = xml_attribute_value (n, (xmlChar *)"expire_increment");
1277 if (!incr)
1278 return 0;
1280 i = strtoul ((char *)incr, &p, 10);
1281 if (errno || *p || i == ULONG_MAX || *incr == '-')
1283 xmlFree (incr);
1284 rc = GPG_ERR_ERANGE;
1285 goto fail;
1288 xmlFree (incr);
1289 e_orig = e;
1290 e = now + i;
1291 p = str_asprintf ("%lu", e);
1292 if (!p)
1294 rc = GPG_ERR_ENOMEM;
1295 goto fail;
1298 rc = xml_add_attribute (client, n, "expire", p);
1299 xfree (p);
1300 if (!rc)
1301 rc = send_status (client->ctx, STATUS_EXPIRE, "%lu %lu", e_orig, e);
1303 fail:
1304 return rc;
1307 static gpg_error_t
1308 store_command (assuan_context_t ctx, char *line)
1310 struct client_s *client = assuan_get_pointer (ctx);
1311 gpg_error_t rc;
1312 size_t len;
1313 unsigned char *result;
1314 xmlNodePtr n, parent;
1315 int has_content;
1316 char *content = NULL;
1317 struct xml_request_s *req;
1319 if (line && *line)
1320 return send_error (ctx, GPG_ERR_SYNTAX);
1322 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1323 if (rc)
1324 return send_error (ctx, rc);
1326 rc = xml_new_request (client, (char *)result, XML_CMD_STORE, &req);
1327 xfree (result);
1328 if (rc)
1329 return send_error (ctx, rc);
1331 /* Prevent passing the element content around to save some memory. */
1332 len = strv_length (req->args);
1333 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1334 if (*(req->args+1) && !xml_valid_element_path (req->args, has_content))
1336 xml_free_request (req);
1337 return send_error (ctx, GPG_ERR_INV_VALUE);
1340 if (has_content || !*req->args[len-1])
1342 has_content = 1;
1343 content = req->args[len-1];
1344 req->args[len-1] = NULL;
1347 if (strv_length (req->args) > 1)
1349 rc = xml_check_recursion (client, req);
1350 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
1351 goto fail;
1354 parent = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1356 if (!rc && len > 1)
1358 rc = xml_is_element_owner (client, parent);
1359 if (!rc)
1361 n = xml_find_text_node (parent->children);
1362 if (n)
1363 xmlNodeSetContent (n, (xmlChar *) content);
1364 else
1365 xmlNodeAddContent (parent, (xmlChar *) content);
1367 (void)xml_update_element_mtime (client, parent);
1368 (void)update_element_expirey (client, parent);
1372 fail:
1373 xfree (content);
1374 xml_free_request (req);
1375 return send_error (ctx, rc);
1378 static gpg_error_t
1379 xfer_data (assuan_context_t ctx, const char *line, int total)
1381 struct client_s *client = assuan_get_pointer (ctx);
1382 int to_send;
1383 int sent = 0;
1384 gpg_error_t rc = 0;
1385 int progress = config_get_integer ("global", "xfer_progress");
1386 int flush = 0;
1388 if (!(client->flags & FLAG_LOCK_CMD))
1389 rc = unlock_file_mutex (client, 0);
1390 if (rc && rc != GPG_ERR_NOT_LOCKED)
1391 return rc;
1393 progress =
1394 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1395 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1396 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1398 if (rc)
1399 return rc;
1401 again:
1404 if (sent + to_send > total)
1405 to_send = total - sent;
1407 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1408 flush ? 0 : to_send);
1409 if (!rc)
1411 sent += flush ? 0 : to_send;
1413 if ((progress && !(sent % progress) && sent != total) ||
1414 (sent == total && flush))
1415 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1417 if (!flush && !rc && sent == total)
1419 flush = 1;
1420 goto again;
1424 while (!rc && sent < total);
1426 return rc;
1429 static gpg_error_t
1430 do_get (assuan_context_t ctx, char *line)
1432 struct client_s *client = assuan_get_pointer (ctx);
1433 gpg_error_t rc;
1434 struct xml_request_s *req;
1435 xmlNodePtr n;
1437 rc = xml_new_request (client, line, XML_CMD_NONE, &req);
1438 if (rc)
1439 return rc;
1441 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1442 if (!rc)
1444 if (n && n->children)
1446 xmlNodePtr tmp = n;
1448 n = xml_find_text_node (n->children);
1449 if (!n || !n->content || !*n->content)
1450 rc = GPG_ERR_NO_DATA;
1451 else
1453 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1454 if (!rc)
1456 xmlChar *expire = xml_attribute_value (tmp,
1457 (xmlChar *)"expire");
1458 if (expire)
1460 time_t now = time (NULL);
1461 time_t e;
1462 char *p;
1464 errno = 0;
1465 e = strtoul ((char *)expire, &p, 10);
1466 if (errno || *p || e == ULONG_MAX || *expire == '-')
1467 log_write (_("invalid expire attribute value: %s"),
1468 expire);
1469 else if (now >= e)
1470 rc = send_status (ctx, STATUS_EXPIRE, "%lu 0", e);
1472 xmlFree (expire);
1477 else
1478 rc = GPG_ERR_NO_DATA;
1481 xml_free_request (req);
1482 return rc;
1485 static gpg_error_t
1486 get_command (assuan_context_t ctx, char *line)
1488 struct client_s *client = assuan_get_pointer (ctx);
1489 gpg_error_t rc;
1490 struct argv_s *args[] = {
1491 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1492 NULL
1495 rc = parse_options (&line, args, client, 1);
1496 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1497 rc = GPG_ERR_SYNTAX;
1498 if (rc)
1499 return send_error (ctx, rc);
1501 if (client->opts & OPT_INQUIRE)
1503 unsigned char *result;
1504 size_t len;
1506 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1507 if (rc)
1508 return send_error (ctx, rc);
1510 pthread_cleanup_push ((void *)xfree, result);
1511 rc = do_get (ctx, (char *)result);
1512 pthread_cleanup_pop (1);
1514 else
1515 rc = do_get (ctx, line);
1517 return send_error (ctx, rc);
1520 static void list_command_cleanup1 (void *arg);
1521 static gpg_error_t
1522 realpath_command (assuan_context_t ctx, char *line)
1524 gpg_error_t rc;
1525 char **realpath = NULL;
1526 struct client_s *client = assuan_get_pointer (ctx);
1527 struct argv_s *args[] = {
1528 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1529 NULL
1532 rc = parse_options (&line, args, client, 1);
1533 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1534 rc = GPG_ERR_SYNTAX;
1535 if (rc)
1536 return send_error (ctx, rc);
1538 if (client->opts & OPT_INQUIRE)
1540 unsigned char *result;
1541 size_t len;
1543 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1544 if (rc)
1545 return send_error (ctx, rc);
1547 pthread_cleanup_push ((void *)xfree, result);
1548 (void)xml_resolve_path (client, client->doc, result, &realpath, &rc);
1549 pthread_cleanup_pop (1);
1551 else
1552 (void)xml_resolve_path (client, client->doc, (unsigned char *)line,
1553 &realpath, &rc);
1555 if (!rc)
1557 char *tmp = strv_join ("\t", realpath);
1559 strv_free (realpath);
1560 if (!tmp)
1561 return send_error (ctx, GPG_ERR_ENOMEM);
1563 pthread_cleanup_push ((void *)xfree, tmp);
1564 rc = xfer_data (ctx, tmp, strlen (tmp));
1565 pthread_cleanup_pop (1);
1568 return send_error (ctx, rc);
1571 static void
1572 list_command_cleanup1 (void *arg)
1574 if (arg)
1575 string_free ((struct string_s *) arg, 1);
1578 static gpg_error_t
1579 parse_list_opt_recurse (void *data, void *value)
1581 struct client_s *client = data;
1583 client->opts |= OPT_LIST_RECURSE;
1584 return 0;
1587 static gpg_error_t
1588 parse_opt_verbose (void *data, void *value)
1590 struct client_s *client = data;
1592 client->opts |= OPT_VERBOSE;
1593 return 0;
1596 static gpg_error_t
1597 list_path_once (struct client_s *client, char *line,
1598 struct element_list_s *elements, struct string_s *result)
1600 gpg_error_t rc;
1602 rc = xml_create_path_list (client, client->doc, NULL, elements, line, 0);
1603 if (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES)
1604 rc = 0;
1606 if (!rc)
1608 int total = slist_length (elements->list);
1610 if (total)
1612 int i;
1614 for (i = 0; i < total; i++)
1616 char *tmp = slist_nth_data (elements->list, i);
1618 string_append_printf (result, "%s%s", tmp,
1619 i + 1 == total ? "" : "\n");
1622 else
1623 rc = GPG_ERR_NO_DATA;
1626 return rc;
1629 static gpg_error_t
1630 do_list (assuan_context_t ctx, char *line)
1632 struct client_s *client = assuan_get_pointer (ctx);
1633 gpg_error_t rc = GPG_ERR_NO_DATA;
1634 struct element_list_s *elements = NULL;
1636 if (!line || !*line)
1638 struct string_s *str = string_new (NULL);
1639 xmlNodePtr n;
1641 if (!str)
1642 return GPG_ERR_ENOMEM;
1644 pthread_cleanup_push ((void *)list_command_cleanup1, str);
1645 n = xmlDocGetRootElement (client->doc);
1646 for (n = n->children; n; n = n->next)
1648 xmlChar *name;
1650 if (n->type != XML_ELEMENT_NODE)
1651 continue;
1653 name = xmlGetProp (n, (xmlChar *)"_name");
1654 if (!name)
1656 rc = GPG_ERR_ENOMEM;
1657 break;
1660 elements = xcalloc (1, sizeof (struct element_list_s));
1661 if (!elements)
1663 xmlFree (name);
1664 rc = GPG_ERR_ENOMEM;
1665 break;
1668 elements->prefix = string_new (NULL);
1669 if (!elements->prefix)
1671 xmlFree (name);
1672 xml_free_element_list (elements);
1673 rc = GPG_ERR_ENOMEM;
1674 break;
1677 elements->recurse = client->opts & OPT_LIST_RECURSE;
1678 elements->root_only = !elements->recurse;
1679 pthread_cleanup_push ((void *)xml_free_element_list, elements);
1680 rc = list_path_once (client, (char *)name, elements, str);
1681 xmlFree (name);
1682 pthread_cleanup_pop (1);
1683 if (rc)
1684 break;
1686 if (n->next)
1687 string_append (str, "\n");
1690 if (!rc)
1691 rc = xfer_data (ctx, str->str, str->len);
1693 pthread_cleanup_pop (1);
1694 return rc;
1697 elements = xcalloc (1, sizeof (struct element_list_s));
1698 if (!elements)
1699 return GPG_ERR_ENOMEM;
1701 elements->prefix = string_new (NULL);
1702 if (!elements->prefix)
1704 xml_free_element_list (elements);
1705 return GPG_ERR_ENOMEM;
1708 elements->recurse = client->opts & OPT_LIST_RECURSE;
1709 pthread_cleanup_push ((void *)xml_free_element_list, elements);
1710 struct string_s *str = string_new (NULL);
1711 pthread_cleanup_push ((void *)list_command_cleanup1, str);
1712 rc = list_path_once (client, line, elements, str);
1713 if (!rc)
1714 rc = xfer_data (ctx, str->str, str->len);
1716 pthread_cleanup_pop (1);
1717 pthread_cleanup_pop (1);
1718 return rc;
1721 static gpg_error_t
1722 list_command (assuan_context_t ctx, char *line)
1724 struct client_s *client = assuan_get_pointer (ctx);
1725 gpg_error_t rc;
1726 struct argv_s *args[] = {
1727 &(struct argv_s) {"recurse", OPTION_TYPE_NOARG, parse_list_opt_recurse},
1728 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1729 /* These are deprecated but kept for backward compatibility with older
1730 * clients. */
1731 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, NULL},
1732 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG, NULL},
1733 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_recurse},
1734 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG, NULL},
1735 NULL
1738 if (disable_list_and_dump == 1)
1739 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1741 rc = parse_options (&line, args, client, 1);
1742 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1743 rc = GPG_ERR_SYNTAX;
1744 if (rc)
1745 return send_error (ctx, rc);
1747 if (client->opts & OPT_INQUIRE)
1749 unsigned char *result;
1750 size_t len;
1752 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1753 if (rc)
1754 return send_error (ctx, rc);
1756 pthread_cleanup_push ((void *)xfree, result);
1757 rc = do_list (ctx, (char *)result);
1758 pthread_cleanup_pop (1);
1760 else
1761 rc = do_list (ctx, line);
1763 return send_error (ctx, rc);
1766 #define RESUMABLE_ERROR(rc) (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES \
1767 || rc == GPG_ERR_ELEMENT_NOT_FOUND || !rc)
1769 * args[0] - element path
1771 static gpg_error_t
1772 attribute_list (assuan_context_t ctx, char **args)
1774 struct client_s *client = assuan_get_pointer (ctx);
1775 char **attrlist = NULL;
1776 int i = 0;
1777 int target = 0;
1778 xmlAttrPtr a;
1779 xmlNodePtr n, an, r;
1780 char *line;
1781 gpg_error_t rc;
1782 struct xml_request_s *req;
1784 if (!args || !args[0] || !*args[0])
1785 return GPG_ERR_SYNTAX;
1787 rc = xml_new_request (client, args[0], XML_CMD_ATTR_LIST, &req);
1788 if (rc)
1789 return rc;
1791 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1792 xml_free_request (req);
1793 if (rc)
1794 return rc;
1796 again:
1797 for (a = n->properties; a; a = a->next)
1799 char **pa;
1801 if (target && xml_reserved_attribute ((char *)a->name))
1802 continue;
1804 pa = xrealloc (attrlist, (i + 2) * sizeof (char *));
1805 if (!pa)
1807 if (attrlist)
1808 strv_free (attrlist);
1810 log_write ("%s(%i): %s", __FILE__, __LINE__,
1811 pwmd_strerror (GPG_ERR_ENOMEM));
1812 return GPG_ERR_ENOMEM;
1815 attrlist = pa;
1816 an = a->children;
1817 attrlist[i] = str_asprintf ("%s %s", (char *) a->name, an && an->content
1818 ? (char *)an->content : "");
1820 if (!attrlist[i])
1822 strv_free (attrlist);
1823 log_write ("%s(%i): %s", __FILE__, __LINE__,
1824 pwmd_strerror (GPG_ERR_ENOMEM));
1825 return GPG_ERR_ENOMEM;
1828 attrlist[++i] = NULL;
1831 if (!attrlist)
1832 return GPG_ERR_NO_DATA;
1834 if (!target)
1836 r = xml_resolve_path (client, client->doc, (xmlChar *)args[0], NULL, &rc);
1837 if (!RESUMABLE_ERROR (rc))
1839 strv_free (attrlist);
1840 return rc;
1842 else if (!rc && r != n)
1844 target = 1;
1845 n = r;
1846 goto again;
1849 rc = 0;
1852 line = strv_join ("\n", attrlist);
1853 if (!line)
1855 log_write ("%s(%i): %s", __FILE__, __LINE__,
1856 pwmd_strerror (GPG_ERR_ENOMEM));
1857 strv_free (attrlist);
1858 return GPG_ERR_ENOMEM;
1861 pthread_cleanup_push ((void *)xfree, line);
1862 pthread_cleanup_push ((void *)req_cleanup, attrlist);
1863 rc = xfer_data (ctx, line, strlen (line));
1864 pthread_cleanup_pop (1);
1865 pthread_cleanup_pop (1);
1866 return rc;
1870 * args[0] - attribute
1871 * args[1] - element path
1873 static gpg_error_t
1874 attribute_delete (struct client_s *client, char **args)
1876 gpg_error_t rc;
1877 xmlNodePtr n;
1879 if (!args || !args[0] || !*args[0] || !args[1] || !*args[1])
1880 return GPG_ERR_SYNTAX;
1882 if (!strcmp (args[0], "_name") || !strcmp (args[0], "target"))
1883 return GPG_ERR_INV_ATTR;
1885 if (!xml_reserved_attribute (args[0]))
1886 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
1887 else
1889 struct xml_request_s *req;
1891 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
1892 if (rc)
1893 return rc;
1895 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1896 xml_free_request (req);
1899 if (!rc)
1900 rc = xml_is_element_owner (client, n);
1902 if (!rc)
1903 rc = xml_delete_attribute (client, n, (xmlChar *) args[0]);
1905 return rc;
1909 * Creates the "target" attribute. When other commands encounter an element
1910 * with this attribute they follow the "target" after appending remaining
1911 * elements from the original element path to the target. The exception is the
1912 * ATTR command that operates on the element itself (for reserved attribute
1913 * names) and not the target.
1915 * If the source element path doesn't exist when using 'ATTR SET target', it is
1916 * created, but the destination element path must exist. This is simliar to the
1917 * ls(1) command: ln -s src dst.
1919 * args[0] - source element path
1920 * args[1] - destination element path
1922 static gpg_error_t
1923 set_target_attribute (struct client_s *client, char **args)
1925 struct xml_request_s *req = NULL;
1926 char **src, **dst;
1927 gpg_error_t rc;
1928 xmlNodePtr n;
1930 if (!args || !args[0] || !args[1])
1931 return GPG_ERR_SYNTAX;
1933 src = str_split (args[0], "\t", 0);
1934 if (!src)
1935 return GPG_ERR_SYNTAX;
1937 if (!xml_valid_element_path (src, 0))
1939 strv_free (src);
1940 return GPG_ERR_INV_VALUE;
1943 dst = str_split (args[1], "\t", 0);
1944 if (!dst)
1946 strv_free (src);
1947 return GPG_ERR_SYNTAX;
1950 // Be sure the destination element path exists. */
1951 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
1952 if (rc)
1953 goto fail;
1955 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1956 if (rc && rc != GPG_ERR_EACCES)
1957 goto fail;
1959 xml_free_request (req);
1960 req = NULL;
1962 if (!strcmp (args[0], args[1]))
1964 rc = GPG_ERR_EEXIST;
1965 goto fail;
1968 rc = xml_new_request (client, args[0], XML_CMD_ATTR_TARGET, &req);
1969 if (rc)
1970 goto fail;
1972 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1973 if (rc && rc != GPG_ERR_EACCES)
1974 goto fail;
1976 if (!rc)
1978 rc = xml_add_attribute (client, n, "target", args[1]);
1979 if (!rc)
1980 rc = xml_unlink_node (client, n->children);
1983 fail:
1984 strv_free (src);
1985 strv_free (dst);
1986 xml_free_request (req);
1987 return rc;
1991 * args[0] - attribute
1992 * args[1] - element path
1994 static gpg_error_t
1995 attribute_get (assuan_context_t ctx, char **args)
1997 struct client_s *client = assuan_get_pointer (ctx);
1998 xmlNodePtr n;
1999 xmlChar *a;
2000 gpg_error_t rc;
2001 struct xml_request_s *req;
2003 if (!args || !args[0] || !*args[0] || !args[1] || !*args[1])
2004 return GPG_ERR_SYNTAX;
2006 if (!xml_reserved_attribute (args[0]))
2007 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
2008 else
2010 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
2011 if (rc)
2012 return rc;
2014 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2015 xml_free_request (req);
2018 if (rc)
2019 return rc;
2021 a = xmlGetProp (n, (xmlChar *) args[0]);
2022 if (!a)
2023 return GPG_ERR_NOT_FOUND;
2025 pthread_cleanup_push ((void *)xmlFree, a);
2027 if (*a)
2028 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
2029 else
2030 rc = GPG_ERR_NO_DATA;
2032 pthread_cleanup_pop (1);
2033 return rc;
2037 * args[0] - attribute
2038 * args[1] - element path
2039 * args[2] - value
2041 static gpg_error_t
2042 attribute_set (struct client_s *client, char **args)
2044 struct xml_request_s *req;
2045 gpg_error_t rc;
2046 xmlNodePtr n;
2048 if (!args || !args[0] || !args[1])
2049 return GPG_ERR_SYNTAX;
2052 * Reserved attribute names.
2054 if (!strcmp (args[0], "_name")) // Use the RENAME command instead
2055 return GPG_ERR_INV_ATTR;
2056 else if (!strcmp (args[0], "target"))
2057 return set_target_attribute (client, args + 1);
2058 else if (!xml_valid_attribute (args[0]))
2059 return GPG_ERR_INV_VALUE;
2061 if (!xml_valid_attribute_value (args[2]))
2062 return GPG_ERR_INV_VALUE;
2064 if (!xml_reserved_attribute (args[0]))
2066 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
2067 if (!rc)
2069 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
2070 if (!rc)
2072 rc = xml_check_recursion (client, req);
2073 xml_free_request (req);
2077 else
2079 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
2080 if (rc)
2081 return rc;
2083 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2084 xml_free_request (req);
2087 if (!rc)
2088 rc = xml_is_element_owner (client, n);
2090 if (!rc)
2091 rc = xml_add_attribute (client, n, args[0], args[2]);
2093 return rc;
2097 * req[0] - command
2098 * req[1] - attribute name or element path if command is LIST
2099 * req[2] - element path
2100 * req[2] - element path or value
2103 static gpg_error_t
2104 do_attr (assuan_context_t ctx, char *line)
2106 struct client_s *client = assuan_get_pointer (ctx);
2107 gpg_error_t rc = 0;
2108 char **req;
2110 req = str_split (line, " ", 4);
2111 if (!req || !req[0] || !req[1])
2113 strv_free (req);
2114 return GPG_ERR_SYNTAX;
2117 pthread_cleanup_push ((void *)req_cleanup, req);
2119 if (strcasecmp (req[0], "SET") == 0)
2120 rc = attribute_set (client, req + 1);
2121 else if (strcasecmp (req[0], "GET") == 0)
2122 rc = attribute_get (ctx, req + 1);
2123 else if (strcasecmp (req[0], "DELETE") == 0)
2124 rc = attribute_delete (client, req + 1);
2125 else if (strcasecmp (req[0], "LIST") == 0)
2126 rc = attribute_list (ctx, req + 1);
2127 else
2128 rc = GPG_ERR_SYNTAX;
2130 client->flags &= ~(FLAG_ACL_IGNORE|FLAG_ACL_ERROR);
2131 pthread_cleanup_pop (1);
2132 return rc;
2135 static gpg_error_t
2136 attr_command (assuan_context_t ctx, char *line)
2138 struct client_s *client = assuan_get_pointer (ctx);
2139 gpg_error_t rc;
2140 struct argv_s *args[] = {
2141 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2142 NULL
2145 rc = parse_options (&line, args, client, 1);
2146 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2147 rc = GPG_ERR_SYNTAX;
2148 if (rc)
2149 return send_error (ctx, rc);
2151 if (client->opts & OPT_INQUIRE)
2153 unsigned char *result;
2154 size_t len;
2156 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2157 if (rc)
2158 return send_error (ctx, rc);
2160 pthread_cleanup_push ((void *)xfree, result);
2161 rc = do_attr (ctx, (char *)result);
2162 pthread_cleanup_pop (1);
2164 else
2165 rc = do_attr (ctx, line);
2167 return send_error (ctx, rc);
2170 static gpg_error_t
2171 parse_iscached_opt_lock (void *data, void *value)
2173 struct client_s *client = data;
2175 (void) value;
2176 client->opts |= OPT_LOCK;
2177 return 0;
2180 static gpg_error_t
2181 iscached_command (assuan_context_t ctx, char *line)
2183 struct client_s *client = assuan_get_pointer (ctx);
2184 gpg_error_t rc;
2185 struct argv_s *args[] = {
2186 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2187 NULL
2190 if (!line || !*line)
2191 return send_error (ctx, GPG_ERR_SYNTAX);
2193 rc = parse_options (&line, args, client, 1);
2194 if (rc)
2195 return send_error (ctx, rc);
2196 else if (!valid_filename (line))
2197 return send_error (ctx, GPG_ERR_INV_VALUE);
2199 rc = cache_iscached (line, NULL);
2200 if (client->opts & OPT_LOCK
2201 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2203 unsigned char md5file[16];
2204 gpg_error_t trc = rc;
2206 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2207 if (memcmp (md5file, client->md5file, 16))
2208 cleanup_client (client);
2210 memcpy (client->md5file, md5file, 16);
2211 rc = do_lock (client, 1);
2212 if (!rc)
2213 rc = trc;
2216 return send_error (ctx, rc);
2219 static gpg_error_t
2220 clearcache_command (assuan_context_t ctx, char *line)
2222 gpg_error_t rc = 0, all_rc = 0;
2223 unsigned char md5file[16];
2224 int i;
2225 int t;
2226 int all = 0;
2227 struct client_thread_s *once = NULL;
2229 cache_lock ();
2230 MUTEX_LOCK (&cn_mutex);
2231 pthread_cleanup_push ((void *)cleanup_mutex_cb, &cn_mutex);
2233 if (!line || !*line)
2234 all = 1;
2236 t = slist_length (cn_thread_list);
2238 for (i = 0; i < t; i++)
2240 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2241 assuan_peercred_t peer;
2243 if (!thd->cl)
2244 continue;
2246 /* Lock each connected clients' file mutex to prevent any other client
2247 * from accessing the cache entry (the file mutex is locked upon
2248 * command startup). The cache for the entry is not cleared if the
2249 * file mutex is locked by another client to prevent this function
2250 * from blocking.
2252 if (all)
2254 if (thd->cl->filename)
2256 rc = do_validate_peer (ctx, thd->cl->filename, &peer);
2257 all_rc = !all_rc ? rc : all_rc;
2258 if (rc)
2260 rc = 0;
2261 continue;
2265 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
2266 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2268 if (pthread_equal (pthread_self (), thd->tid))
2269 rc = 0;
2270 else
2272 if (!thd->cl->filename ||
2273 cache_iscached (thd->cl->filename,
2274 NULL) == GPG_ERR_NO_DATA)
2276 rc = 0;
2277 continue;
2280 cache_defer_clear (thd->cl->md5file);
2283 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2285 rc = 0;
2286 continue;
2289 if (!rc)
2291 rc = cache_clear (thd->cl->md5file);
2292 cache_unlock_mutex (thd->cl->md5file, 0);
2295 if (rc)
2296 all_rc = rc;
2298 rc = 0;
2300 /* A single data filename was specified. Lock only this data file
2301 * mutex and free the cache entry. */
2302 else
2304 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2305 rc = do_validate_peer (ctx, line, &peer);
2307 if (!rc && !memcmp (thd->cl->md5file, md5file, sizeof (md5file)))
2309 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0,
2310 -1);
2311 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2313 if (pthread_equal (pthread_self (), thd->tid))
2314 rc = 0;
2317 if (!rc)
2319 once = thd;
2320 rc = cache_clear (thd->cl->md5file);
2321 cache_unlock_mutex (thd->cl->md5file, 0);
2323 else
2325 cache_defer_clear (thd->cl->md5file);
2328 break;
2333 /* Only connected clients' cache entries have been cleared. Now clear any
2334 * remaining cache entries without clients but only if there wasn't an
2335 * error from above since this would defeat the locking check of the
2336 * remaining entries. */
2337 if (!all_rc && all)
2339 cache_clear (NULL);
2342 /* No clients are using the specified file. */
2343 else if (!all_rc && !rc && !once)
2345 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2346 rc = cache_clear (md5file);
2349 /* Release the connection mutex. */
2350 pthread_cleanup_pop (1);
2351 cache_unlock ();
2353 if (!rc)
2354 send_status_all (STATUS_CACHE, NULL);
2356 /* One or more files were locked while clearing all cache entries. */
2357 if (all_rc)
2358 rc = all_rc;
2360 return send_error (ctx, rc);
2363 static gpg_error_t
2364 cachetimeout_command (assuan_context_t ctx, char *line)
2366 int timeout;
2367 char **req = str_split (line, " ", 0);
2368 char *p;
2369 gpg_error_t rc = 0;
2370 assuan_peercred_t peer;
2372 if (!req || !*req || !req[1])
2374 strv_free (req);
2375 return send_error (ctx, GPG_ERR_SYNTAX);
2378 errno = 0;
2379 timeout = (int) strtol (req[1], &p, 10);
2380 if (errno != 0 || *p || timeout < -1)
2382 strv_free (req);
2383 return send_error (ctx, GPG_ERR_SYNTAX);
2386 rc = do_validate_peer (ctx, req[0], &peer);
2387 if (!rc)
2389 unsigned char md5file[16];
2391 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, req[0], strlen (req[0]));
2392 rc = cache_set_timeout (md5file, timeout);
2393 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2395 rc = 0;
2396 MUTEX_LOCK (&rcfile_mutex);
2397 config_set_int_param (&global_config, req[0], "cache_timeout",
2398 req[1]);
2399 MUTEX_UNLOCK (&rcfile_mutex);
2403 strv_free (req);
2404 return send_error (ctx, rc);
2407 static gpg_error_t
2408 dump_command (assuan_context_t ctx, char *line)
2410 xmlChar *xml;
2411 int len;
2412 struct client_s *client = assuan_get_pointer (ctx);
2413 gpg_error_t rc;
2415 if (disable_list_and_dump == 1)
2416 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2418 if (line && *line)
2419 return send_error (ctx, GPG_ERR_SYNTAX);
2421 rc = peer_is_invoker(client);
2422 if (rc)
2423 return send_error (ctx, rc);
2425 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2427 if (!xml)
2429 log_write ("%s(%i): %s", __FILE__, __LINE__,
2430 pwmd_strerror (GPG_ERR_ENOMEM));
2431 return send_error (ctx, GPG_ERR_ENOMEM);
2434 pthread_cleanup_push ((void *)xmlFree, xml);
2435 rc = xfer_data (ctx, (char *) xml, len);
2436 pthread_cleanup_pop (1);
2437 return send_error (ctx, rc);
2440 static gpg_error_t
2441 getconfig_command (assuan_context_t ctx, char *line)
2443 struct client_s *client = assuan_get_pointer (ctx);
2444 gpg_error_t rc = 0;
2445 char filename[255] = { 0 }, param[747] = { 0 };
2446 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2448 if (!line || !*line)
2449 return send_error (ctx, GPG_ERR_SYNTAX);
2451 if (strchr (line, ' '))
2453 sscanf (line, " %254[^ ] %746c", filename, param);
2454 paramp = param;
2455 fp = filename;
2458 if (fp && !valid_filename (fp))
2459 return send_error (ctx, GPG_ERR_INV_VALUE);
2461 paramp = str_down (paramp);
2462 if (!strcmp (paramp, "passphrase"))
2463 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2465 p = config_get_value (fp ? fp : "global", paramp);
2466 if (!p)
2467 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2469 tmp = expand_homedir (p);
2470 xfree (p);
2471 if (!tmp)
2473 log_write ("%s(%i): %s", __FILE__, __LINE__,
2474 pwmd_strerror (GPG_ERR_ENOMEM));
2475 return send_error (ctx, GPG_ERR_ENOMEM);
2478 p = tmp;
2479 pthread_cleanup_push ((void *)xfree, p);
2480 rc = xfer_data (ctx, p, strlen (p));
2481 pthread_cleanup_pop (1);
2482 return send_error (ctx, rc);
2485 struct xpath_s
2487 xmlXPathContextPtr xp;
2488 xmlXPathObjectPtr result;
2489 xmlBufferPtr buf;
2490 char **req;
2493 static void
2494 xpath_command_cleanup (void *arg)
2496 struct xpath_s *xpath = arg;
2498 if (!xpath)
2499 return;
2501 req_cleanup (xpath->req);
2503 if (xpath->buf)
2504 xmlBufferFree (xpath->buf);
2506 if (xpath->result)
2507 xmlXPathFreeObject (xpath->result);
2509 if (xpath->xp)
2510 xmlXPathFreeContext (xpath->xp);
2513 static gpg_error_t
2514 do_xpath (assuan_context_t ctx, char *line)
2516 gpg_error_t rc;
2517 struct client_s *client = assuan_get_pointer (ctx);
2518 struct xpath_s _x = { 0 };
2519 struct xpath_s *xpath = &_x;
2521 if (!line || !*line)
2522 return GPG_ERR_SYNTAX;
2524 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2526 if (strv_printf (&xpath->req, "%s", line) == 0)
2527 return GPG_ERR_ENOMEM;
2530 xpath->xp = xmlXPathNewContext (client->doc);
2531 if (!xpath->xp)
2533 rc = GPG_ERR_BAD_DATA;
2534 goto fail;
2537 xpath->result =
2538 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2539 if (!xpath->result)
2541 rc = GPG_ERR_BAD_DATA;
2542 goto fail;
2545 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2547 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2548 goto fail;
2551 rc = xml_recurse_xpath_nodeset (client, client->doc,
2552 xpath->result->nodesetval,
2553 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2554 NULL);
2555 if (rc)
2556 goto fail;
2557 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2559 rc = GPG_ERR_NO_DATA;
2560 goto fail;
2562 else if (xpath->req[1])
2564 rc = 0;
2565 goto fail;
2568 pthread_cleanup_push ((void *)xpath_command_cleanup, &xpath);
2569 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2570 xmlBufferLength (xpath->buf));
2571 pthread_cleanup_pop (0);
2572 fail:
2573 xpath_command_cleanup (xpath);
2574 return rc;
2577 static gpg_error_t
2578 xpath_command (assuan_context_t ctx, char *line)
2580 struct client_s *client = assuan_get_pointer (ctx);
2581 gpg_error_t rc;
2582 struct argv_s *args[] = {
2583 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2584 NULL
2587 if (disable_list_and_dump == 1)
2588 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2590 rc = peer_is_invoker(client);
2591 if (rc)
2592 return send_error (ctx, rc);
2594 rc = parse_options (&line, args, client, 1);
2595 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2596 rc = GPG_ERR_SYNTAX;
2597 if (rc)
2598 return send_error (ctx, rc);
2600 if (client->opts & OPT_INQUIRE)
2602 unsigned char *result;
2603 size_t len;
2605 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2606 if (rc)
2607 return send_error (ctx, rc);
2609 pthread_cleanup_push ((void *)xfree, result);
2610 rc = do_xpath (ctx, (char *)result);
2611 pthread_cleanup_pop (1);
2613 else
2614 rc = do_xpath (ctx, line);
2616 return send_error (ctx, rc);
2619 static gpg_error_t
2620 do_xpathattr (assuan_context_t ctx, char *line)
2622 struct client_s *client = assuan_get_pointer (ctx);
2623 gpg_error_t rc;
2624 char **req = NULL;
2625 int cmd = 0; //SET
2626 struct xpath_s _x = { 0 };
2627 struct xpath_s *xpath = &_x;
2629 if (!line || !*line)
2630 return GPG_ERR_SYNTAX;
2632 if ((req = str_split (line, " ", 3)) == NULL)
2633 return GPG_ERR_ENOMEM;
2635 if (!req[0])
2637 rc = GPG_ERR_SYNTAX;
2638 goto fail;
2641 if (!strcasecmp (req[0], "SET"))
2642 cmd = 0;
2643 else if (!strcasecmp (req[0], "DELETE"))
2644 cmd = 1;
2645 else
2647 rc = GPG_ERR_SYNTAX;
2648 goto fail;
2651 if (!req[1] || !req[2])
2653 rc = GPG_ERR_SYNTAX;
2654 goto fail;
2657 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2659 rc = GPG_ERR_ENOMEM;
2660 goto fail;
2663 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2665 rc = GPG_ERR_SYNTAX;
2666 goto fail;
2669 xpath->xp = xmlXPathNewContext (client->doc);
2670 if (!xpath->xp)
2672 rc = GPG_ERR_BAD_DATA;
2673 goto fail;
2676 xpath->result = xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2677 if (!xpath->result)
2679 rc = GPG_ERR_BAD_DATA;
2680 goto fail;
2683 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2685 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2686 goto fail;
2689 rc = xml_recurse_xpath_nodeset (client, client->doc,
2690 xpath->result->nodesetval,
2691 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2692 (xmlChar *) req[1]);
2694 fail:
2695 xpath_command_cleanup (xpath);
2696 strv_free (req);
2697 return rc;
2700 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2701 static gpg_error_t
2702 xpathattr_command (assuan_context_t ctx, char *line)
2704 struct client_s *client = assuan_get_pointer (ctx);
2705 gpg_error_t rc;
2706 struct argv_s *args[] = {
2707 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2708 NULL
2711 if (disable_list_and_dump == 1)
2712 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2714 rc = peer_is_invoker(client);
2715 if (rc)
2716 return send_error (ctx, rc);
2718 rc = parse_options (&line, args, client, 1);
2719 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2720 rc = GPG_ERR_SYNTAX;
2721 if (rc)
2722 return send_error (ctx, rc);
2724 if (client->opts & OPT_INQUIRE)
2726 unsigned char *result;
2727 size_t len;
2729 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2730 if (rc)
2731 return send_error (ctx, rc);
2733 pthread_cleanup_push ((void *)xfree, result);
2734 rc = do_xpathattr (ctx, (char *)result);
2735 pthread_cleanup_pop (1);
2737 else
2738 rc = do_xpathattr (ctx, line);
2740 return send_error (ctx, rc);
2743 static gpg_error_t
2744 do_import (struct client_s *client, const char *root_element,
2745 unsigned char *content)
2747 xmlDocPtr doc = NULL;
2748 xmlNodePtr n = NULL, root;
2749 gpg_error_t rc;
2750 struct string_s *str = NULL;
2751 struct xml_request_s *req = NULL;
2753 if (!content || !*content)
2754 return GPG_ERR_SYNTAX;
2756 if (root_element)
2758 rc = xml_new_request (client, root_element, XML_CMD_STORE, &req);
2759 if (rc)
2761 xfree (content);
2762 return rc;
2765 if (!xml_valid_element_path (req->args, 0))
2767 xml_free_request (req);
2768 xfree (content);
2769 return GPG_ERR_INV_VALUE;
2773 str = string_new_content ((char *)content);
2774 str = string_prepend (str, "<pwmd>");
2775 str = string_append (str, "</pwmd>");
2776 doc = xmlReadDoc ((xmlChar *) str->str, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2777 string_free (str, 1);
2778 if (!doc)
2780 rc = GPG_ERR_BAD_DATA;
2781 goto fail;
2784 root = xmlDocGetRootElement (doc);
2785 root = root->children;
2786 if (root->type != XML_ELEMENT_NODE)
2788 rc = GPG_ERR_BAD_DATA;
2789 goto fail;
2792 rc = xml_validate_import (client, root);
2793 if (rc)
2794 goto fail;
2796 if (req)
2798 /* Create the new specified root element path, if needed. */
2799 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc),
2800 &rc);
2801 if (rc)
2802 goto fail;
2804 /* Overwrite the children of the specified root element path. */
2805 xmlUnlinkNode (n->children);
2806 xmlFreeNodeList (n->children);
2807 n->children = NULL;
2809 else
2810 n = xmlDocGetRootElement (client->doc);
2812 if (n)
2814 xmlNodePtr copy;
2815 xmlChar *name = xml_attribute_value (root, (xmlChar *)"_name");
2817 if (!name)
2818 rc = GPG_ERR_BAD_DATA;
2819 else if (!req)
2821 /* No --root argument passed. Overwrite the current documents' root
2822 * element matching the root element of the imported data. */
2823 xml_free_request (req);
2824 req = NULL;
2825 rc = xml_new_request (client, (char *)name, XML_CMD_DELETE, &req);
2826 xmlFree (name);
2827 if (!rc)
2829 xmlNodePtr tmp;
2831 tmp = xml_find_elements (client, req,
2832 xmlDocGetRootElement (req->doc), &rc);
2833 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2834 goto fail;
2836 if (!rc)
2838 xmlUnlinkNode (tmp);
2839 xmlFreeNodeList (tmp);
2842 rc = 0;
2846 if (!rc)
2848 copy = xmlCopyNodeList (root);
2849 if (!copy)
2850 rc = GPG_ERR_ENOMEM;
2851 else
2853 n = xmlAddChildList (n, copy);
2854 if (!n)
2855 rc = GPG_ERR_ENOMEM;
2860 if (!rc && n && n->parent)
2861 rc = xml_update_element_mtime (client, n->parent);
2863 fail:
2864 xml_free_request (req);
2866 if (doc)
2867 xmlFreeDoc (doc);
2869 return rc;
2872 static gpg_error_t
2873 parse_import_opt_root (void *data, void *value)
2875 struct client_s *client = data;
2877 client->import_root = str_dup (value);
2878 return client->import_root ? 0 : GPG_ERR_ENOMEM;
2881 static gpg_error_t
2882 import_command (assuan_context_t ctx, char *line)
2884 gpg_error_t rc;
2885 struct client_s *client = assuan_get_pointer (ctx);
2886 unsigned char *result;
2887 size_t len;
2888 struct argv_s *args[] = {
2889 &(struct argv_s) {"root", OPTION_TYPE_ARG, parse_import_opt_root},
2890 NULL
2893 xfree (client->import_root);
2894 client->import_root = NULL;
2895 rc = parse_options (&line, args, client, 0);
2896 if (rc)
2897 return send_error (ctx, rc);
2899 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2900 if (rc)
2902 xfree (client->import_root);
2903 client->import_root = NULL;
2904 return send_error (ctx, rc);
2907 rc = do_import (client, client->import_root, result);
2908 xfree (client->import_root);
2909 client->import_root = NULL;
2910 return send_error (ctx, rc);
2913 static gpg_error_t
2914 do_lock (struct client_s *client, int add)
2916 gpg_error_t rc = lock_file_mutex (client, add);
2918 if (!rc)
2919 client->flags |= FLAG_LOCK_CMD;
2921 return rc;
2924 static gpg_error_t
2925 lock_command (assuan_context_t ctx, char *line)
2927 struct client_s *client = assuan_get_pointer (ctx);
2928 gpg_error_t rc;
2930 if (line && *line)
2931 return send_error (ctx, GPG_ERR_SYNTAX);
2933 rc = do_lock (client, 0);
2934 return send_error (ctx, rc);
2937 static gpg_error_t
2938 unlock_command (assuan_context_t ctx, char *line)
2940 struct client_s *client = assuan_get_pointer (ctx);
2941 gpg_error_t rc;
2943 if (line && *line)
2944 return send_error (ctx, GPG_ERR_SYNTAX);
2946 rc = unlock_file_mutex (client, 0);
2947 return send_error (ctx, rc);
2950 static void
2951 get_set_env (const char *name, const char *value)
2953 if (value && *value)
2954 setenv (name, value, 1);
2955 else
2956 unsetenv (name);
2959 static gpg_error_t
2960 option_command (assuan_context_t ctx, char *line)
2962 struct client_s *client = assuan_get_pointer (ctx);
2963 gpg_error_t rc = 0;
2964 char namebuf[255] = { 0 };
2965 char *name = namebuf;
2966 char *value = NULL, *p, *tmp = NULL;
2968 p = strchr (line, '=');
2969 if (!p)
2971 strncpy (namebuf, line, sizeof(namebuf));
2972 namebuf[sizeof(namebuf)-1] = 0;
2974 else
2976 strncpy (namebuf, line, strlen (line)-strlen (p));
2977 namebuf[sizeof(namebuf)-1] = 0;
2978 value = p+1;
2981 log_write2 ("OPTION name='%s' value='%s'", name, value);
2983 if (strcasecmp (name, (char *) "lock-timeout") == 0)
2985 long n = 0;
2987 if (value)
2989 n = strtol (value, &tmp, 10);
2990 if (tmp && *tmp)
2991 return send_error (ctx, GPG_ERR_INV_VALUE);
2994 client->lock_timeout = n;
2996 else if (strcasecmp (name, (char *) "NAME") == 0)
2998 if (value && strchr (value, ' '))
2999 rc = GPG_ERR_INV_VALUE;
3000 else
3002 tmp = pthread_getspecific (thread_name_key);
3003 xfree (tmp);
3004 MUTEX_LOCK (&cn_mutex);
3005 xfree (client->thd->name);
3006 client->thd->name = NULL;
3008 if (!value || !*value)
3009 pthread_setspecific (thread_name_key, str_dup (""));
3010 else
3012 client->thd->name = str_dup (value);
3013 pthread_setspecific (thread_name_key, str_dup (value));
3016 MUTEX_UNLOCK (&cn_mutex);
3019 else if (strcasecmp (name, "disable-pinentry") == 0)
3021 int n = 1;
3023 if (value && *value)
3025 n = (int) strtol (value, &tmp, 10);
3026 if (*tmp || n < 0 || n > 1)
3027 return send_error (ctx, GPG_ERR_INV_VALUE);
3030 if (n)
3031 client->flags |= FLAG_NO_PINENTRY;
3032 else
3033 client->flags &= ~FLAG_NO_PINENTRY;
3035 else if (strcasecmp (name, "ttyname") == 0)
3037 get_set_env ("GPG_TTY", value);
3039 else if (strcasecmp (name, "ttytype") == 0)
3041 get_set_env ("TERM", value);
3043 else if (strcasecmp (name, "display") == 0)
3045 get_set_env ("DISPLAY", value);
3047 else if (strcasecmp (name, "lc_messages") == 0)
3050 else if (strcasecmp (name, "lc_ctype") == 0)
3053 else if (strcasecmp (name, "desc") == 0)
3056 else if (strcasecmp (name, "pinentry-timeout") == 0)
3059 else
3060 rc = GPG_ERR_UNKNOWN_OPTION;
3062 return send_error (ctx, rc);
3065 static gpg_error_t
3066 do_rename (assuan_context_t ctx, char *line)
3068 struct client_s *client = assuan_get_pointer (ctx);
3069 char **args, *p;
3070 xmlNodePtr src, dst;
3071 struct string_s *str = NULL, *tstr;
3072 struct xml_request_s *req;
3073 gpg_error_t rc;
3075 args = str_split (line, " ", 0);
3076 if (!args || strv_length (args) != 2)
3078 strv_free (args);
3079 return GPG_ERR_SYNTAX;
3082 if (!xml_valid_element ((xmlChar *) args[1]))
3084 strv_free (args);
3085 return GPG_ERR_INV_VALUE;
3088 rc = xml_new_request (client, args[0], XML_CMD_RENAME, &req);
3089 if (rc)
3091 strv_free (args);
3092 return rc;
3095 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3096 if (rc)
3097 goto fail;
3099 rc = xml_is_element_owner (client, src);
3100 if (rc)
3101 goto fail;
3103 xmlChar *a = xmlGetProp (src, (xmlChar *) "_name");
3104 if (!a)
3106 rc = GPG_ERR_ENOMEM;
3107 goto fail;
3110 /* To prevent unwanted effects:
3112 * <element _name="a"><element _name="b"/></element>
3114 * RENAME a<TAB>b b
3116 if (xmlStrEqual (a, (xmlChar *) args[1]))
3118 xmlFree (a);
3119 rc = GPG_ERR_CONFLICT;
3120 goto fail;
3123 xmlFree (a);
3124 str = string_new (args[0]);
3125 if (!str)
3127 rc = GPG_ERR_ENOMEM;
3128 goto fail;
3131 p = strrchr (str->str, '\t');
3132 if (p)
3133 tstr = string_truncate (str, strlen (str->str)-strlen (p));
3134 else
3135 tstr = string_truncate (str, 0);
3137 if (!tstr)
3139 string_free (str, 1);
3140 rc = GPG_ERR_ENOMEM;
3141 goto fail;
3144 str = tstr;
3145 tstr = string_append_printf (str, "%s%s", str->len ? "\t" : "", args[1]);
3146 if (!tstr)
3148 string_free (str, 1);
3149 rc = GPG_ERR_ENOMEM;
3150 goto fail;
3153 str = tstr;
3154 xml_free_request (req);
3155 rc = xml_new_request (client, str->str, XML_CMD_RENAME, &req);
3156 string_free (str, 1);
3157 if (rc)
3159 req = NULL;
3160 goto fail;
3163 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3164 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3165 goto fail;
3167 rc = 0;
3169 /* Target may exist:
3171 * <element _name="a"/>
3172 * <element _name="b" target="a"/>
3174 * RENAME b a
3176 if (src == dst)
3178 rc = GPG_ERR_CONFLICT;
3179 goto fail;
3182 if (dst)
3184 rc = xml_is_element_owner (client, dst);
3185 if (rc)
3186 goto fail;
3188 rc = xml_add_attribute (client, src, "_name", args[1]);
3189 if (!rc)
3190 xml_unlink_node (client, dst);
3192 else
3193 rc = xml_add_attribute (client, src, "_name", args[1]);
3195 fail:
3196 xml_free_request (req);
3197 strv_free (args);
3198 return rc;
3201 static gpg_error_t
3202 rename_command (assuan_context_t ctx, char *line)
3204 struct client_s *client = assuan_get_pointer (ctx);
3205 gpg_error_t rc;
3206 struct argv_s *args[] = {
3207 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3208 NULL
3211 rc = parse_options (&line, args, client, 1);
3212 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3213 rc = GPG_ERR_SYNTAX;
3214 if (rc)
3215 return send_error (ctx, rc);
3217 if (client->opts & OPT_INQUIRE)
3219 unsigned char *result;
3220 size_t len;
3222 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3223 if (rc)
3224 return send_error (ctx, rc);
3226 pthread_cleanup_push ((void *)xfree, result);
3227 rc = do_rename (ctx, (char *)result);
3228 pthread_cleanup_pop (1);
3230 else
3231 rc = do_rename (ctx, line);
3233 return send_error (ctx, rc);
3236 static gpg_error_t
3237 do_copy (assuan_context_t ctx, char *line)
3239 struct client_s *client = assuan_get_pointer (ctx);
3240 char **args, **targs;
3241 xmlNodePtr src, dst, tree = NULL;
3242 struct xml_request_s *req;
3243 gpg_error_t rc;
3245 args = str_split (line, " ", 0);
3246 if (!args || strv_length (args) != 2)
3248 strv_free (args);
3249 return GPG_ERR_SYNTAX;
3252 targs = str_split (args[1], "\t", 0);
3253 if (!targs)
3255 strv_free (args);
3256 return GPG_ERR_ENOMEM;
3259 if (!xml_valid_element_path (targs, 0))
3261 strv_free (args);
3262 strv_free (targs);
3263 return GPG_ERR_INV_VALUE;
3265 strv_free (targs);
3267 rc = xml_new_request (client, args[0], XML_CMD_NONE, &req);
3268 if (rc)
3270 strv_free (args);
3271 return rc;
3274 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3275 if (rc)
3276 goto fail;
3278 tree = xmlCopyNodeList (src);
3279 if (!tree)
3281 rc = GPG_ERR_ENOMEM;
3282 goto fail;
3285 xml_free_request (req);
3286 /* Create the destination element path if it does not exist. */
3287 rc = xml_new_request (client, args[1], XML_CMD_STORE, &req);
3288 if (rc)
3290 req = NULL;
3291 goto fail;
3294 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3295 if (rc)
3296 goto fail;
3298 rc = xml_is_element_owner (client, dst);
3299 if (rc)
3300 goto fail;
3302 rc = xml_validate_target (client, req->doc, args[1], src);
3303 if (rc || src == dst)
3305 rc = GPG_ERR_CONFLICT;
3306 goto fail;
3309 /* Merge any attributes from the src node to the initial dst node. */
3310 for (xmlAttrPtr attr = tree->properties; attr; attr = attr->next)
3312 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3313 continue;
3315 xmlAttrPtr a = xmlHasProp (dst, attr->name);
3316 if (a)
3317 xmlRemoveProp (a);
3319 xmlChar *tmp = xmlNodeGetContent (attr->children);
3320 xmlNewProp (dst, attr->name, tmp);
3321 xmlFree (tmp);
3322 /* Create the default attributes. */
3323 rc = xml_add_attribute (client, dst, NULL, NULL);
3326 xmlNodePtr n = dst->children;
3327 xmlUnlinkNode (n);
3328 xmlFreeNodeList (n);
3329 dst->children = NULL;
3331 if (tree->children)
3333 n = xmlCopyNodeList (tree->children);
3334 if (!n)
3336 rc = GPG_ERR_ENOMEM;
3337 goto fail;
3340 n = xmlAddChildList (dst, n);
3341 if (!n)
3343 rc = GPG_ERR_ENOMEM;
3344 goto fail;
3347 rc = xml_update_element_mtime (client, xmlDocGetRootElement (client->doc)
3348 == dst->parent ? dst : dst->parent);
3351 fail:
3352 if (tree)
3354 xmlUnlinkNode (tree);
3355 xmlFreeNodeList (tree);
3358 xml_free_request (req);
3359 strv_free (args);
3360 return rc;
3363 static gpg_error_t
3364 copy_command (assuan_context_t ctx, char *line)
3366 struct client_s *client = assuan_get_pointer (ctx);
3367 gpg_error_t rc;
3368 struct argv_s *args[] = {
3369 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3370 NULL
3373 rc = parse_options (&line, args, client, 1);
3374 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3375 rc = GPG_ERR_SYNTAX;
3376 if (rc)
3377 return send_error (ctx, rc);
3379 if (client->opts & OPT_INQUIRE)
3381 unsigned char *result;
3382 size_t len;
3384 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3385 if (rc)
3386 return send_error (ctx, rc);
3388 pthread_cleanup_push ((void *)xfree, result);
3389 rc = do_copy (ctx, (char *)result);
3390 pthread_cleanup_pop (1);
3392 else
3393 rc = do_copy (ctx, line);
3395 return send_error (ctx, rc);
3398 static gpg_error_t
3399 do_move (assuan_context_t ctx, char *line)
3401 struct client_s *client = assuan_get_pointer (ctx);
3402 char **args;
3403 xmlNodePtr src, dst;
3404 struct xml_request_s *req;
3405 gpg_error_t rc;
3407 args = str_split (line, " ", 0);
3408 if (!args || strv_length (args) != 2)
3410 strv_free (args);
3411 return GPG_ERR_SYNTAX;
3414 if (*args[1])
3416 char **targs = str_split (args[1], "\t", 0);
3417 if (!targs)
3419 strv_free (args);
3420 return GPG_ERR_ENOMEM;
3423 if (!xml_valid_element_path (targs, 0))
3425 strv_free (targs);
3426 strv_free (args);
3427 return GPG_ERR_INV_VALUE;
3430 strv_free (targs);
3433 rc = xml_new_request (client, args[0], XML_CMD_MOVE, &req);
3434 if (rc)
3436 strv_free (args);
3437 return rc;
3440 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3441 if (rc)
3442 goto fail;
3444 rc = xml_is_element_owner (client, src);
3445 if (rc)
3446 goto fail;
3448 xml_free_request (req);
3449 req = NULL;
3450 if (*args[1])
3452 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
3453 if (rc)
3454 goto fail;
3456 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc),
3457 &rc);
3459 else
3460 dst = xmlDocGetRootElement (client->doc);
3462 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3463 goto fail;
3465 rc = 0;
3467 for (xmlNodePtr n = dst; n; n = n->parent)
3469 if (n == src)
3471 rc = GPG_ERR_CONFLICT;
3472 goto fail;
3476 if (dst)
3478 xmlChar *a = xml_attribute_value (src, (xmlChar *) "_name");
3479 xmlNodePtr dup = xml_find_element (client, dst->children, (char *) a, &rc);
3481 xmlFree (a);
3482 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3483 goto fail;
3485 rc = 0;
3487 if (dup)
3489 if (dup == src)
3491 rc = GPG_ERR_CONFLICT;
3492 goto fail;
3495 if (dst == xmlDocGetRootElement (client->doc))
3497 xmlNodePtr n = src;
3498 int match = 0;
3500 while (n->parent && n->parent != dst)
3501 n = n->parent;
3503 xmlChar *a = xml_attribute_value (n, (xmlChar *) "_name");
3504 xmlChar *b = xml_attribute_value (src, (xmlChar *) "_name");
3506 if (xmlStrEqual (a, b))
3508 match = 1;
3509 xmlUnlinkNode (src);
3510 xmlUnlinkNode (n);
3511 xmlFreeNodeList (n);
3514 xmlFree (a);
3515 xmlFree (b);
3517 if (!match)
3519 xmlUnlinkNode (dup);
3520 xmlFreeNodeList (dup);
3523 else
3524 xmlUnlinkNode (dup);
3528 if (!dst && *req->args)
3530 struct xml_request_s *nreq = NULL;
3531 xmlChar *name = xml_attribute_value (src, (xmlChar *) "_name");
3533 if (src->parent == xmlDocGetRootElement (client->doc)
3534 && !strcmp ((char *) name, *req->args))
3536 xmlFree (name);
3537 rc = GPG_ERR_CONFLICT;
3538 goto fail;
3541 xmlFree (name);
3542 rc = xml_new_request (client, args[1], XML_CMD_STORE, &nreq);
3543 if (!rc)
3545 dst = xml_find_elements (client, nreq,
3546 xmlDocGetRootElement (nreq->doc), &rc);
3547 xml_free_request (nreq);
3550 if (rc)
3551 goto fail;
3554 xml_update_element_mtime (client, src->parent);
3555 xmlUnlinkNode (src);
3557 dst = xmlAddChildList (dst, src);
3558 if (!dst)
3559 rc = GPG_ERR_ENOMEM;
3560 else
3561 xml_update_element_mtime (client, dst->parent);
3563 fail:
3564 xml_free_request (req);
3565 strv_free (args);
3566 return rc;
3569 static gpg_error_t
3570 move_command (assuan_context_t ctx, char *line)
3572 struct client_s *client = assuan_get_pointer (ctx);
3573 gpg_error_t rc;
3574 struct argv_s *args[] = {
3575 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3576 NULL
3579 rc = parse_options (&line, args, client, 1);
3580 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3581 rc = GPG_ERR_SYNTAX;
3582 if (rc)
3583 return send_error (ctx, rc);
3585 if (client->opts & OPT_INQUIRE)
3587 unsigned char *result;
3588 size_t len;
3590 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3591 if (rc)
3592 return send_error (ctx, rc);
3594 pthread_cleanup_push ((void *)xfree, result);
3595 rc = do_move (ctx, (char *)result);
3596 pthread_cleanup_pop (1);
3598 else
3599 rc = do_move (ctx, line);
3601 return send_error (ctx, rc);
3604 static gpg_error_t
3605 ls_command (assuan_context_t ctx, char *line)
3607 gpg_error_t rc;
3608 char *tmp;
3609 char *dir;
3610 DIR *d;
3612 if (line && *line)
3613 return send_error (ctx, GPG_ERR_SYNTAX);
3615 tmp = str_asprintf ("%s/data", homedir);
3616 dir = expand_homedir (tmp);
3617 xfree (tmp);
3618 d = opendir (dir);
3619 rc = gpg_error_from_errno (errno);
3621 if (!d)
3623 xfree (dir);
3624 return send_error (ctx, rc);
3627 size_t len =
3628 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3629 struct dirent *p = xmalloc (len), *cur = NULL;
3630 char *list = NULL;
3632 xfree (dir);
3633 pthread_cleanup_push ((void *)xfree, p);
3634 pthread_cleanup_push ((void *)(void *)(void *)closedir, d);
3635 rc = 0;
3637 while (!readdir_r (d, p, &cur) && cur)
3639 struct stat st;
3641 if (stat (cur->d_name, &st) == -1 || !S_ISREG (st.st_mode))
3642 continue;
3644 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3646 if (!tmp)
3648 if (list)
3649 xfree (list);
3651 rc = GPG_ERR_ENOMEM;
3652 break;
3655 xfree (list);
3656 list = tmp;
3659 pthread_cleanup_pop (1); // closedir (d)
3660 pthread_cleanup_pop (1); // xfree (p)
3662 if (rc)
3663 return send_error (ctx, rc);
3665 if (!list)
3666 return send_error (ctx, GPG_ERR_NO_DATA);
3668 list[strlen (list) - 1] = 0;
3669 pthread_cleanup_push ((void *)xfree, list);
3670 rc = xfer_data (ctx, list, strlen (list));
3671 pthread_cleanup_pop (1);
3672 return send_error (ctx, rc);
3675 static gpg_error_t
3676 bye_notify (assuan_context_t ctx, char *line)
3678 struct client_s *cl = assuan_get_pointer (ctx);
3679 gpg_error_t ret = 0;
3681 update_client_state (cl, CLIENT_STATE_DISCON);
3683 #ifdef WITH_GNUTLS
3684 if (cl->thd->remote)
3686 int rc;
3690 struct timeval tv = { 0, 50000 };
3692 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3693 if (rc == GNUTLS_E_AGAIN)
3694 select (0, NULL, NULL, NULL, &tv);
3696 while (rc == GNUTLS_E_AGAIN);
3698 #endif
3700 /* This will let assuan_process_next() return. */
3701 if (fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK) == -1)
3703 cl->last_rc = gpg_error_from_errno (errno);
3704 ret = cl->last_rc;
3707 cl->last_rc = 0; // BYE command result
3708 return ret;
3711 static gpg_error_t
3712 reset_notify (assuan_context_t ctx, char *line)
3714 struct client_s *client = assuan_get_pointer (ctx);
3716 if (client)
3717 cleanup_client (client);
3719 return 0;
3723 * This is called before every Assuan command.
3725 static gpg_error_t
3726 command_startup (assuan_context_t ctx, const char *name)
3728 struct client_s *client = assuan_get_pointer (ctx);
3729 gpg_error_t rc;
3730 struct command_table_s *cmd = NULL;
3732 log_write2 ("command='%s'", name);
3733 client->last_rc = client->opts = 0;
3735 for (int i = 0; command_table[i]; i++)
3737 if (!strcasecmp (name, command_table[i]->name))
3739 if (command_table[i]->ignore_startup)
3740 return 0;
3741 cmd = command_table[i];
3742 break;
3746 if (!cmd)
3747 return GPG_ERR_UNKNOWN_COMMAND;
3749 client->last_rc = rc = gpg_error (file_modified (client, cmd));
3750 if (!rc)
3751 update_client_state (client, CLIENT_STATE_COMMAND);
3753 return rc;
3757 * This is called after every Assuan command.
3759 static void
3760 command_finalize (assuan_context_t ctx, gpg_error_t rc)
3762 struct client_s *client = assuan_get_pointer (ctx);
3764 if (!(client->flags & FLAG_LOCK_CMD))
3765 unlock_file_mutex (client, 0);
3767 unlock_flock (&client->flock_fd);
3768 log_write2 (_("command completed: rc=%u"), rc ? rc : client->last_rc);
3769 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
3770 #ifdef WITH_GNUTLS
3771 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
3772 #endif
3773 if (client->thd->state != CLIENT_STATE_DISCON)
3774 update_client_state (client, CLIENT_STATE_IDLE);
3777 static gpg_error_t
3778 parse_help_opt_html (void *data, void *value)
3780 struct client_s *client = data;
3782 (void) value;
3783 client->opts |= OPT_HTML;
3784 return 0;
3787 static gpg_error_t
3788 help_command (assuan_context_t ctx, char *line)
3790 gpg_error_t rc;
3791 int i;
3792 struct client_s *client = assuan_get_pointer (ctx);
3793 struct argv_s *args[] = {
3794 &(struct argv_s) {"html", OPTION_TYPE_NOARG, parse_help_opt_html},
3795 NULL
3798 rc = parse_options (&line, args, client, 1);
3799 if (rc)
3800 return send_error (ctx, rc);
3802 if (!line || !*line)
3804 char *tmp;
3805 char *help = str_dup (_("Usage: HELP [--html] [<COMMAND>]\n"
3806 "For commands that take an element path as an argument, each element is "
3807 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3808 "@*@*COMMANDS:"));
3810 for (i = 0; command_table[i]; i++)
3812 if (!command_table[i]->help)
3813 continue;
3815 /* @npxref{} won't put a "See " or "see " in front of the command.
3816 * It's not a texinfo command but needed for --html. */
3817 tmp = str_asprintf ("%s %s%s%s", help,
3818 client->opts & OPT_HTML ? "@npxref{" : "",
3819 command_table[i]->name,
3820 client->opts & OPT_HTML ? "}" : "");
3821 xfree (help);
3822 help = tmp;
3825 tmp = strip_texi_and_wrap (help, client->opts & OPT_HTML);
3826 xfree (help);
3827 pthread_cleanup_push ((void *)xfree, tmp);
3828 rc = xfer_data (ctx, tmp, strlen (tmp));
3829 pthread_cleanup_pop (1);
3830 return send_error (ctx, rc);
3833 for (i = 0; command_table[i]; i++)
3835 if (!strcasecmp (line, command_table[i]->name))
3837 char *help, *tmp;
3839 if (!command_table[i]->help)
3840 break;
3842 help = strip_texi_and_wrap (command_table[i]->help,
3843 client->opts & OPT_HTML);
3844 tmp = str_asprintf ("%s%s",
3845 (client->opts & OPT_HTML) ? "" : _("Usage: "),
3846 help);
3847 xfree (help);
3848 pthread_cleanup_push ((void *)xfree, tmp);
3849 rc = xfer_data (ctx, tmp, strlen (tmp));
3850 pthread_cleanup_pop (1);
3851 return send_error (ctx, rc);
3855 return send_error (ctx, GPG_ERR_INV_NAME);
3858 static void
3859 new_command (const char *name, int ignore, int unlock, int flock_type,
3860 gpg_error_t (*handler) (assuan_context_t, char *),
3861 const char *help)
3863 int i = 0;
3864 struct command_table_s **tmp;
3866 if (command_table)
3867 for (i = 0; command_table[i]; i++);
3869 tmp = xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
3870 assert (tmp);
3871 command_table = tmp;
3872 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
3873 command_table[i]->name = name;
3874 command_table[i]->handler = handler;
3875 command_table[i]->ignore_startup = ignore;
3876 command_table[i]->unlock = unlock;
3877 command_table[i]->flock_type = flock_type;
3878 command_table[i++]->help = help;
3879 command_table[i] = NULL;
3882 void
3883 deinit_commands ()
3885 int i;
3887 for (i = 0; command_table[i]; i++)
3888 xfree (command_table[i]);
3890 xfree (command_table);
3893 static int
3894 sort_commands (const void *arg1, const void *arg2)
3896 struct command_table_s *const *a = arg1;
3897 struct command_table_s *const *b = arg2;
3899 if (!*a || !*b)
3900 return 0;
3901 else if (*a && !*b)
3902 return 1;
3903 else if (!*a && *b)
3904 return -1;
3906 return strcmp ((*a)->name, (*b)->name);
3909 static gpg_error_t
3910 passwd_command (assuan_context_t ctx, char *line)
3912 struct client_s *client = assuan_get_pointer (ctx);
3913 gpg_error_t rc;
3915 rc = peer_is_invoker (client);
3916 if (rc == GPG_ERR_EACCES)
3917 return send_error (ctx, GPG_ERR_FORBIDDEN);
3918 else if (rc)
3919 return send_error (ctx, rc);
3921 if (client->flags & FLAG_NEW)
3922 return send_error (ctx, GPG_ERR_INV_STATE);
3924 rc = crypto_init_ctx (client->crypto, client->flags & FLAG_NO_PINENTRY);
3925 if (rc)
3926 return send_error (ctx, rc);
3928 if (!rc)
3929 rc = crypto_passwd (client, client->crypto);
3931 crypto_cleanup_non_keys (client->crypto);
3932 return send_error (ctx, rc);
3935 static gpg_error_t
3936 parse_opt_data (void *data, void *value)
3938 struct client_s *client = data;
3940 (void) value;
3941 client->opts |= OPT_DATA;
3942 return 0;
3945 static gpg_error_t
3946 send_client_list (assuan_context_t ctx)
3948 struct client_s *client = assuan_get_pointer (ctx);
3949 gpg_error_t rc = 0;
3951 if (client->opts & OPT_VERBOSE)
3953 unsigned i, t;
3954 char **list = NULL;
3955 char *line;
3957 MUTEX_LOCK (&cn_mutex);
3958 pthread_cleanup_push ((void *)cleanup_mutex_cb, &cn_mutex);
3959 t = slist_length (cn_thread_list);
3961 for (i = 0; i < t; i++)
3963 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
3964 char *tmp;
3966 if (thd->state == CLIENT_STATE_UNKNOWN)
3967 continue;
3969 tmp = build_client_info_line (thd, 0);
3970 if (tmp)
3972 char **l = strv_cat (list, tmp);
3973 if (!l)
3974 rc = GPG_ERR_ENOMEM;
3975 else
3976 list = l;
3978 else
3979 rc = GPG_ERR_ENOMEM;
3981 if (rc)
3983 strv_free (list);
3984 break;
3988 pthread_cleanup_pop (1);
3989 if (rc)
3990 return rc;
3992 line = strv_join ("\n", list);
3993 strv_free (list);
3994 pthread_cleanup_push ((void *)xfree, line);
3995 rc = xfer_data (ctx, line, strlen (line));
3996 pthread_cleanup_pop (1);
3997 return rc;
4000 if (client->opts & OPT_DATA)
4002 char buf[ASSUAN_LINELENGTH];
4004 MUTEX_LOCK (&cn_mutex);
4005 snprintf (buf, sizeof (buf), "%u", slist_length (cn_thread_list));
4006 MUTEX_UNLOCK (&cn_mutex);
4007 rc = xfer_data (ctx, buf, strlen (buf));
4009 else
4010 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4012 return rc;
4015 static gpg_error_t
4016 getinfo_command (assuan_context_t ctx, char *line)
4018 struct client_s *client = assuan_get_pointer (ctx);
4019 gpg_error_t rc;
4020 char buf[ASSUAN_LINELENGTH];
4021 struct argv_s *args[] = {
4022 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4023 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_opt_verbose},
4024 NULL
4027 rc = parse_options (&line, args, client, 1);
4028 if (rc)
4029 return send_error (ctx, rc);
4031 if (!strcasecmp (line, "clients"))
4033 rc = send_client_list (ctx);
4035 else if (!strcasecmp (line, "cache"))
4037 if (client->opts & OPT_DATA)
4039 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4040 rc = xfer_data (ctx, buf, strlen (buf));
4042 else
4043 rc = send_status (ctx, STATUS_CACHE, NULL);
4045 else if (!strcasecmp (line, "pid"))
4047 char buf[32];
4048 pid_t pid = getpid ();
4050 snprintf (buf, sizeof (buf), "%i", pid);
4051 rc = xfer_data (ctx, buf, strlen (buf));
4053 else if (!strcasecmp (line, "version"))
4055 char *buf = str_asprintf ("0x%06x %s", VERSION_HEX,
4056 #ifdef WITH_GNUTLS
4057 "GNUTLS "
4058 #endif
4059 #ifdef WITH_LIBACL
4060 "ACL "
4061 #endif
4062 "");
4063 rc = xfer_data (ctx, buf, strlen (buf));
4064 xfree (buf);
4066 else if (!strcasecmp (line, "last_error"))
4068 if (client->last_error)
4069 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4070 else
4071 rc = GPG_ERR_NO_DATA;
4073 else if (!strcasecmp (line, "user"))
4075 char *user = NULL;
4077 #ifdef WITH_GNUTLS
4078 if (client->thd->remote)
4079 user = str_asprintf ("#%s", client->thd->tls->fp);
4080 else
4081 user = get_username (client->thd->peer->uid);
4082 #else
4083 user = get_username (client->thd->peer->uid);
4084 #endif
4085 if (user)
4087 pthread_cleanup_push ((void *)xfree, user);
4088 rc = xfer_data (ctx, user, strlen (user));
4089 pthread_cleanup_pop (1);
4091 else
4092 rc = GPG_ERR_NO_DATA;
4094 else
4095 rc = gpg_error (GPG_ERR_SYNTAX);
4097 return send_error (ctx, rc);
4100 static gpg_error_t
4101 parse_listkeys_opt_secret_only (void *data, void *value)
4103 struct client_s *client = data;
4105 (void) value;
4106 client->opts |= OPT_SECRET_ONLY;
4107 return 0;
4110 static gpg_error_t
4111 keyinfo_command (assuan_context_t ctx, char *line)
4113 struct client_s *client = assuan_get_pointer (ctx);
4114 gpg_error_t rc = 0;
4115 char **keys = NULL, **p = NULL;
4116 int sym = 0;
4118 if (line && *line)
4119 return send_error (ctx, GPG_ERR_SYNTAX);
4121 if (!(client->flags & FLAG_OPEN))
4122 return send_error (ctx, GPG_ERR_INV_STATE);
4124 if (client->flags & FLAG_NEW)
4125 return send_error (ctx, GPG_ERR_NO_DATA);
4127 rc = lock_flock (ctx, client->filename, FLOCK_TYPE_SH, &client->flock_fd);
4128 if (rc)
4129 return send_error (ctx, rc);
4131 rc = crypto_is_symmetric (client->filename);
4132 unlock_flock (&client->flock_fd);
4133 if (!rc)
4134 sym = 1;
4135 else if (rc != GPG_ERR_BAD_DATA)
4136 return send_error (ctx, rc);
4138 rc = 0;
4139 if (!sym)
4141 p = strv_catv(keys, client->crypto->pubkey);
4142 if (!p)
4143 rc = GPG_ERR_ENOMEM;
4146 if (!rc)
4148 keys = p;
4149 for (p = client->crypto->sigkey; p && *p; p++)
4151 if (!strv_printf(&keys, "S%s", *p))
4153 strv_free (keys);
4154 return send_error (ctx, GPG_ERR_ENOMEM);
4158 else
4159 rc = GPG_ERR_ENOMEM;
4161 if (!rc && keys)
4163 line = strv_join ("\n", keys);
4164 strv_free (keys);
4165 pthread_cleanup_push ((void *)xfree, line);
4166 if (line)
4167 rc = xfer_data (ctx, line, strlen (line));
4168 else
4169 rc = GPG_ERR_ENOMEM;
4171 pthread_cleanup_pop (1);
4173 else if (!keys)
4174 rc = GPG_ERR_NO_DATA;
4175 else
4176 strv_free (keys);
4178 return send_error (ctx, rc);
4181 static gpg_error_t
4182 kill_command (assuan_context_t ctx, char *line)
4184 struct client_s *client = assuan_get_pointer (ctx);
4185 gpg_error_t rc;
4186 unsigned i, t;
4188 if (!line || !*line)
4189 return send_error (ctx, GPG_ERR_SYNTAX);
4191 MUTEX_LOCK (&cn_mutex);
4192 pthread_cleanup_push ((void *)cleanup_mutex_cb, &cn_mutex);
4193 t = slist_length (cn_thread_list);
4194 rc = GPG_ERR_ESRCH;
4196 for (i = 0; i < t; i++)
4198 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
4199 char *tmp = str_asprintf ("%p", thd->tid);
4201 if (strcmp (line, tmp))
4203 xfree (tmp);
4204 continue;
4207 xfree (tmp);
4208 rc = peer_is_invoker (client);
4209 if (!rc)
4211 #ifdef HAVE_PTHREAD_CANCEL
4212 rc = pthread_cancel (thd->tid);
4213 #else
4214 close (thd->fd);
4215 thd->fd = -1;
4216 #endif
4217 break;
4220 rc = GPG_ERR_FORBIDDEN;
4221 if (config_get_boolean ("global", "strict_kill"))
4222 break;
4224 #ifdef WITH_GNUTLS
4225 if (client->thd->remote && thd->remote)
4227 if (!strcmp (client->thd->tls->fp, thd->tls->fp))
4229 #ifdef HAVE_PTHREAD_CANCEL
4230 rc = pthread_cancel (thd->tid);
4231 #else
4232 close (thd->fd);
4233 thd->fd = -1;
4234 #endif
4235 break;
4238 else if (!client->thd->remote && !thd->remote)
4239 #endif
4241 if (client->thd->peer->uid == thd->peer->uid)
4243 #ifdef HAVE_PTHREAD_CANCEL
4244 rc = pthread_cancel (thd->tid);
4245 #else
4246 close (thd->fd);
4247 thd->fd = -1;
4248 #endif
4251 break;
4254 pthread_cleanup_pop (1);
4255 return send_error (ctx, rc);
4258 static gpg_error_t
4259 listkeys_command (assuan_context_t ctx, char *line)
4261 struct client_s *client = assuan_get_pointer (ctx);
4262 struct crypto_s *crypto = NULL;
4263 char **pattern = NULL;
4264 gpgme_key_t *keys = NULL;
4265 char **result = NULL;
4266 gpg_error_t rc;
4267 struct argv_s *args[] = {
4268 &(struct argv_s) {"secret-only", OPTION_TYPE_NOARG,
4269 parse_listkeys_opt_secret_only },
4270 NULL
4273 rc = parse_options (&line, args, client, 1);
4274 if (rc)
4275 return send_error (ctx, rc);
4277 rc = crypto_init (&crypto, client->ctx, client->filename,
4278 client->flags & FLAG_NO_PINENTRY);
4279 if (rc)
4280 return send_error (ctx, rc);
4282 pthread_cleanup_push ((void *)(void *)crypto_cleanup, crypto);
4283 pattern = str_split (line, ",", 0);
4284 pthread_cleanup_push ((void *)(void *)strv_free, pattern);
4285 rc = crypto_list_keys (crypto, pattern, client->opts & OPT_SECRET_ONLY,
4286 &keys);
4287 pthread_cleanup_pop (1);
4288 pthread_cleanup_pop (1);
4289 if (!rc)
4291 int i;
4293 for (i = 0; keys[i]; i++)
4295 char *p = crypto_key_info (keys[i]);
4296 char **r;
4298 if (!p)
4300 rc = GPG_ERR_ENOMEM;
4301 break;
4304 r = strv_cat (result, p);
4305 if (!r)
4307 rc = GPG_ERR_ENOMEM;
4308 break;
4311 result = r;
4314 if (!i)
4315 rc = GPG_ERR_NO_DATA;
4317 crypto_free_key_list (keys);
4320 if (!rc)
4322 line = strv_join ("\n", result);
4323 strv_free (result);
4324 pthread_cleanup_push ((void *)xfree, line);
4325 if (!line)
4326 rc = GPG_ERR_ENOMEM;
4327 else
4328 rc = xfer_data (ctx, line, strlen (line));
4330 pthread_cleanup_pop (1);
4332 else
4333 strv_free (result);
4335 return send_error (ctx, rc);
4338 void
4339 init_commands ()
4341 /* !BEGIN-HELP-TEXT!
4343 * This comment is used as a marker to generate the offline documentation
4344 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4345 * script to determine where commands begin and end.
4347 new_command("HELP", 1, 1, 0, help_command, _(
4348 "HELP [--html] [<COMMAND>]\n"
4349 "Show available commands or command specific help text."
4350 "@*@*"
4351 "The @option{--html} option will output the help text in HTML format."
4354 new_command("KILL", 1, 0, 0, kill_command, _(
4355 "KILL <thread_id>\n"
4356 "Terminates the client identified by @var{thread_id} and releases any file "
4357 "lock or other resources it has held. See @code{GETINFO} (@pxref{GETINFO}) "
4358 "for details about listing connected clients. An @code{invoking_user} "
4359 "(@pxref{Configuration}) may kill any client while others may only kill "
4360 "clients of the same @code{UID} or @abbr{TLS} fingerprint.\n"
4363 new_command("LISTKEYS", 0, 0, 0, listkeys_command, _(
4364 "LISTKEYS [--secret-only] [pattern[,<pattern>]]\n"
4365 "Returns a new line separated list of key information matching a comma "
4366 "separated list of @var{pattern}'s. When option @option{--secret-only} is "
4367 "specified, only keys matching @var{pattern} that also have a secret key "
4368 "available will be returned."
4371 new_command("KEYINFO", 1, 0, 0, keyinfo_command, _(
4372 "KEYINFO\n"
4373 "Returns a new line separated list of key ID's that the currently opened "
4374 "data file has recipients and signers for. If the key is a signing key it "
4375 "will be prefixed with an @code{S}. If the file is a new one, or has no "
4376 "signers in the case of being symmetrically encrypted, the error code "
4377 "@code{GPG_ERR_NO_DATA} is returned."
4380 new_command("GETINFO", 1, 1, 0, getinfo_command, _(
4381 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4382 "Get server and other information. The information is returned via a status "
4383 "message (@pxref{Status Messages}) unless otherwise noted or @option{--data} "
4384 "is specified."
4385 "@*@*"
4386 "@var{CACHE} returns the number of cached documents."
4387 "@*@*"
4388 "@var{CLIENTS} returns the number of "
4389 "connected clients via a status message or a list of connected clients when "
4390 "the @option{--verbose} parameter is used (implies @option{--data}). A "
4391 "verbose line of a client list contains "
4392 "space delimited "
4393 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4394 "IP address if remote, file lock status, whether the current client is self "
4395 "or not, client state (see below), "
4396 "user ID or TLS fingerprint of the connected client, username if the "
4397 "client is a local one else @code{-}, and finally the time stamp of when the "
4398 "client connected."
4399 "@*@*"
4400 "Client state @code{0} is an unknown client state, state @code{1} indicates "
4401 "the client has connected but hasn't completed initializing, state @code{2} "
4402 "indicates that the client is idle, state @code{3} means the "
4403 "client is in a command and state @code{4} means the client is disconnecting. "
4404 "@*@*"
4405 "@var{PID} returns the process ID number of the server via a data response."
4406 "@*@*"
4407 "@var{VERSION} returns the server version number and compile-time features "
4408 "via a data response with each being space delimited."
4409 "@*@*"
4410 "@var{LAST_ERROR} returns a detailed description of the last failed command "
4411 "via a data response, when available."
4412 "@*@*"
4413 "@var{USER} returns the username or @abbr{TLS} hash of the connected client "
4414 "via a data response."
4417 new_command("PASSWD", 0, 0, FLOCK_TYPE_EX|FLOCK_TYPE_KEEP, passwd_command, _(
4418 "PASSWD\n"
4419 "Changes the passphrase of the secret key required to open the current "
4420 "data file. If the data file is symmetrically encrypted, the error "
4421 "@code{GPG_ERR_NOT_SUPPORTED} is returned. When symmetrically encrypted, "
4422 "the @code{SAVE} command (@pxref{SAVE}) should be used instead to prevent "
4423 "this command saving any unwanted changes to the @abbr{XML} document."
4424 "@*@*"
4425 "This command is not available to non-invoking clients "
4426 "(@pxref{Access Control})."
4429 new_command("OPEN", 1, 1, FLOCK_TYPE_SH|FLOCK_TYPE_KEEP, open_command, _(
4430 "OPEN [--lock] <filename>\n"
4431 "Opens @var{filename}. When the @var{filename} is not found on the "
4432 "file-system then a new in-memory document will be created. If the file is "
4433 "found, it is looked for in the file cache and when found no passphrase will "
4434 "be required to open it. When not cached, @cite{pinentry(1)} will be used to "
4435 "retrieve the passphrase for decryption unless @option{disable-pinentry} "
4436 "(@pxref{OPTION}) was specified in which case @command{pwmd} will "
4437 "@emph{INQUIRE} the client for the passphrase."
4438 "@*@*"
4439 "When the @option{--lock} option is passed then the file mutex will be "
4440 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4441 "file had been opened."
4444 new_command("SAVE", 0, 0, FLOCK_TYPE_EX|FLOCK_TYPE_KEEP, save_command, _(
4445 "SAVE [--symmetric] [--inquire-keyparam] [--keyid=<keyid>[,..] | [--inquire-keyid]] [--sign-keyid=<fingerprint>[,..] | [--inquire-sign-keyid]]\n"
4446 "Writes the in-memory @abbr{XML} document to disk. The file written to is the "
4447 "file that was opened using the @code{OPEN} command (@pxref{OPEN}). If the "
4448 "file is a new one or the option @option{--inquire-keyparam} was passed, a "
4449 "new keypair will be generated and @cite{pinentry(1)} will be used to prompt "
4450 "for the passphrase of the secret key used for signing."
4451 "@*@*"
4452 "The @option{--inquire-keyparam} option will send an "
4453 "@emph{INQUIRE} to the client to obtain the key parameters to use for "
4454 "generating the new keypair. The inquired data is expected to be in "
4455 "@cite{gnupg} @abbr{XML} format. See the @cite{gnupg} documentation for "
4456 "details. Note that when this option is specified a new keypair will be "
4457 "generated reguardless if the file is a new one and that the passphrase for "
4458 "the current file will be required before generating the new keypair. This "
4459 "option is available to non-invoking clients (@pxref{Access Control}) only "
4460 "when the file is a new one."
4461 "@*@*"
4462 "You can encrypt the data file to a recipient other than the one that it "
4463 "was encrypted with by passing the @option{--keyid} or "
4464 "@option{--inquire-keyid} option with "
4465 "the key ID of a public encryption key as its argument. Use the "
4466 "@command{LISTKEYS} command (@pxref{LISTKEYS}) to show key information by "
4467 "pattern. The @option{--sign-keyid} or @option{--inquire-sign-keyid} option "
4468 "may also be used to sign the data "
4469 "file with an alternate key by specifying the key ID of a secret key. "
4470 "A passphrase to decrypt the data file "
4471 "will be required if one or more of the original encryption or signing keys "
4472 "are not found in either of these two options' arguments. The original "
4473 "encryption or signing keys will be used when either of these options are "
4474 "not specified."
4475 "@*@*"
4476 "The @option{--keyid} and @option{--sign-keyid} options are not available "
4477 "for non-invoking clients "
4478 "(@pxref{Access Control}) when the recipients or signers do not match those "
4479 "that were used when the file was @code{OPEN}'ed."
4480 "@*@*"
4481 "The @option{--symmetric} option specifies that a new data file be "
4482 "conventionally encrypted. These types of data files do not use a recipient "
4483 "public key but may be signed by using the @option{--sign-keyid} or "
4484 "@option{--inquire-sign-keyid} options. To remove all signers from a "
4485 "symmtrically encrypted data file, leave the option value empty. Note that "
4486 "you cannot change encryption schemes once a data file has been saved."
4489 new_command("ISCACHED", 1, 0, 0, iscached_command, _(
4490 "ISCACHED [--lock] <filename>\n"
4491 "An @emph{OK} response is returned if the specified @var{filename} is found "
4492 "in the file cache. If not found in the cache but exists on the filesystem "
4493 "then @code{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4494 "returned."
4495 "@*@*"
4496 "The @option{--lock} option will lock the file mutex of @var{filename} when "
4497 "the file exists; it does not need to be opened nor cached. The lock will be "
4498 "released when the client exits or sends the @code{UNLOCK} command "
4499 "(@pxref{UNLOCK})."
4502 new_command("CLEARCACHE", 1, 1, 0, clearcache_command, _(
4503 "CLEARCACHE [<filename>]\n"
4504 "Clears a file cache entry for all or the specified @var{filename}."
4507 new_command("CACHETIMEOUT", 1, 1, 0, cachetimeout_command, _(
4508 "CACHETIMEOUT <filename> <seconds>\n"
4509 "The time in @var{seconds} until @var{filename} will be removed from the "
4510 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4511 "the passphrase for each @code{OPEN} command (@pxref{OPEN}) or @code{SAVE} "
4512 "(@pxref{SAVE}) command. @xref{Configuration}, and the @code{cache_timeout} "
4513 "parameter."
4516 new_command("LIST", 0, 1, 0, list_command, _(
4517 "LIST [--inquire] [--recurse] [element[<TAB>child[..]]]\n"
4518 "If no element path is given then a newline separated list of root elements "
4519 "is returned with a data response. If given, then children of the specified "
4520 "element path are returned."
4521 "@*@*"
4522 "Each element path "
4523 "returned will have zero or more flags appened to it. These flags are "
4524 "delimited from the element path by a single space character. A flag itself "
4525 "is a single character. Flag @code{P} indicates that access to the element "
4526 "is denied. Flag @code{+} indicates that there are child nodes of "
4527 "the current element path. Flag @code{E} indicates that an element of the "
4528 "element path contained in a @var{target} attribute could not be found. Flag "
4529 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4530 "(@pxref{Configuration}). Flag @code{T}, followed by a single space character, "
4531 "then an element path, is the element path of the @var{target} attribute "
4532 "contained in the current element."
4533 "@*@*"
4534 "When a specified element path contains an error, beit from the final "
4535 "element in the path or any previous element, the path is still shown but "
4536 "will contain the error flag for the element with the error. Determining "
4537 "the actual element which contains the error is up to the client. This can be "
4538 "done by traversing the final element up to parent elements that contain the "
4539 "same error flag."
4540 "@*@*"
4541 "The option @option{--recurse} may be used to list the entire element tree "
4542 "for a specified element path or the entire tree for all root elements."
4543 "@*@*"
4544 "When the @option{--inquire} option is passed then all remaining non-option "
4545 "arguments are retrieved via a server @emph{INQUIRE}."
4548 new_command("REALPATH", 0, 1, 0, realpath_command, _(
4549 "REALPATH [--inquire] element[<TAB>child[..]]\n"
4550 "Resolves all @code{target} attributes of the specified element path and "
4551 "returns the result with a data response. @xref{Target Attribute}, for details."
4552 "@*@*"
4553 "When the @option{--inquire} option is passed then all remaining non-option "
4554 "arguments are retrieved via a server @emph{INQUIRE}."
4557 new_command("STORE", 0, 1, 0, store_command, _(
4558 "STORE element[<TAB>child[..]]<TAB>[content]\n"
4559 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4560 "@*@*"
4561 "Creates a new element path or modifies the @var{content} of an existing "
4562 "element. If only a single element is specified then a new root element is "
4563 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4564 "set to the final @key{TAB} delimited element. If no @var{content} is "
4565 "specified after the final @key{TAB}, then the content of the existing "
4566 "element will be removed; or will be empty if creating a new element."
4567 "@*@*"
4568 "The only restriction of an element name is that it not contain whitespace "
4569 "characters. There is no other whitespace between the @key{TAB} delimited "
4570 "elements. It is recommended that the content of an element be base64 encoded "
4571 "when it contains control or @key{TAB} characters to prevent @abbr{XML} "
4572 "parsing and @command{pwmd} syntax errors."
4575 new_command("RENAME", 0, 1, 0, rename_command, _(
4576 "RENAME [--inquire] element[<TAB>child[..]] <value>\n"
4577 "Renames the specified @var{element} to the new @var{value}. If an element of "
4578 "the same name as the @var{value} already exists it will be overwritten."
4579 "@*@*"
4580 "When the @option{--inquire} option is passed then all remaining non-option "
4581 "arguments are retrieved via a server @emph{INQUIRE}."
4584 new_command("COPY", 0, 1, 0, copy_command, _(
4585 "COPY [--inquire] source[<TAB>child[..]] dest[<TAB>child[..]]\n"
4586 "Copies the entire element tree starting from the child node of the source "
4587 "element, to the destination element path. If the destination element path "
4588 "does not exist then it will be created; otherwise it is overwritten."
4589 "@*@*"
4590 "Note that attributes from the source element are merged into the "
4591 "destination element when the destination element path exists. When an "
4592 "attribute of the same name exists in both the source and destination "
4593 "elements then the destination attribute will be updated to the source "
4594 "attribute value."
4595 "@*@*"
4596 "When the @option{--inquire} option is passed then all remaining non-option "
4597 "arguments are retrieved via a server @emph{INQUIRE}."
4600 new_command("MOVE", 0, 1, 0, move_command, _(
4601 "MOVE [--inquire] source[<TAB>child[..]] [dest[<TAB>child[..]]]\n"
4602 "Moves the source element path to the destination element path. If the "
4603 "destination is not specified then it will be moved to the root node of the "
4604 "document. If the destination is specified and exists then it will be "
4605 "overwritten; otherwise non-existing elements of the destination element "
4606 "path will be created."
4607 "@*@*"
4608 "When the @option{--inquire} option is passed then all remaining non-option "
4609 "arguments are retrieved via a server @emph{INQUIRE}."
4612 new_command("DELETE", 0, 1, 0, delete_command, _(
4613 "DELETE [--inquire] element[<TAB>child[..]]\n"
4614 "Removes the specified element path and all of its children. This may break "
4615 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4616 "refers to this element or any of its children."
4617 "@*@*"
4618 "When the @option{--inquire} option is passed then all remaining non-option "
4619 "arguments are retrieved via a server @emph{INQUIRE}."
4622 new_command("GET", 0, 1, 0, get_command, _(
4623 "GET [--inquire] element[<TAB>child[..]]\n"
4624 "Retrieves the content of the specified element. The content is returned "
4625 "with a data response."
4626 "@*@*"
4627 "When the @option{--inquire} option is passed then all remaining non-option "
4628 "arguments are retrieved via a server @emph{INQUIRE}."
4631 new_command("ATTR", 0, 1, 0, attr_command, _(
4632 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] element[<TAB>child[..]] ..\n"
4633 "@table @asis\n"
4634 "@item ATTR SET attribute element[<TAB>child[..]] [value]\n"
4635 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4636 "element. When no @var{value} is specified any existing value will be removed."
4637 "@*@*"
4638 "@item ATTR DELETE attribute element[<TAB>child[..]]\n"
4639 " Removes an attribute from an element. If @var{attribute} is @code{_name} "
4640 "or @code{target} an error is returned. Use the @command{DELETE} command "
4641 "(@pxref{DELETE}) instead."
4642 "@*@*"
4643 "@item ATTR LIST element[<TAB>child[..]]\n"
4644 " Retrieves a newline separated list of attributes names and values "
4645 "from the specified element. Each attribute name and value is space delimited."
4646 "@*@*"
4647 "@item ATTR GET attribute element[<TAB>child[..]]\n"
4648 " Retrieves the value of an @var{attribute} from an element."
4649 "@end table\n"
4650 "@*@*"
4651 "When the @option{--inquire} option is passed then all remaining non-option "
4652 "arguments are retrieved via a server @emph{INQUIRE}."
4653 "@*@*"
4654 "@xref{Target Attribute}, for details about this special attribute and also "
4655 "@pxref{Other Attributes} for other attributes that are handled specially "
4656 "by @command{pwmd}."
4659 new_command("XPATH", 0, 1, 0, xpath_command, _(
4660 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4661 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4662 "specified it is assumed the expression is a request to return a result. "
4663 "Otherwise, the result is set to the @var{value} argument and the document is "
4664 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4665 "is assumed to be empty and the document is updated. For example:"
4666 "@sp 1\n"
4667 "@example\n"
4668 "XPATH //element[@@_name='password']@key{TAB}\n"
4669 "@end example\n"
4670 "@sp 1\n"
4671 "would clear the content of all @var{password} elements in the data file "
4672 "while leaving off the trailing @key{TAB} would return all @var{password} "
4673 "elements in @abbr{XML} format."
4674 "@*@*"
4675 "When the @option{--inquire} option is passed then all remaining non-option "
4676 "arguments are retrieved via a server @emph{INQUIRE}."
4677 "@*@*"
4678 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4679 "expression syntax."
4682 new_command("XPATHATTR", 0, 1, 0, xpathattr_command, _(
4683 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4684 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4685 "attributes and does not return a result. For the @var{SET} operation the "
4686 "@var{value} is optional but the field is required. If not specified then "
4687 "the attribute value will be empty. For example:"
4688 "@sp 1\n"
4689 "@example\n"
4690 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4691 "@end example\n"
4692 "@sp 1\n"
4693 "would create a @var{password} attribute for each @var{password} element "
4694 "found in the document. The attribute value will be empty but still exist."
4695 "@*@*"
4696 "When the @option{--inquire} option is passed then all remaining non-option "
4697 "arguments are retrieved via a server @emph{INQUIRE}."
4698 "@*@*"
4699 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4700 "expression syntax."
4703 new_command("IMPORT", 0, 1, 0, import_command, _(
4704 "IMPORT [--root=element[<TAB>child[..]]]\n"
4705 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4706 "@*@*"
4707 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4708 "argument is raw @abbr{XML} data. The content is created as a child of "
4709 "the element path specified with the @option{--root} option or at the "
4710 "document root when not specified. Existing elements of the same name will "
4711 "be overwritten."
4712 "@*@*"
4713 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4714 "for details."
4717 new_command("DUMP", 0, 1, 0, dump_command, _(
4718 "DUMP\n"
4719 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4720 "dumping a specific node."
4723 new_command("LOCK", 0, 0, 0, lock_command, _(
4724 "LOCK\n"
4725 "Locks the mutex associated with the opened file. This prevents other clients "
4726 "from sending commands to the same opened file until the client "
4727 "that sent this command either disconnects or sends the @code{UNLOCK} "
4728 "command. @xref{UNLOCK}."
4731 new_command("UNLOCK", 1, 0, 0, unlock_command, _(
4732 "UNLOCK\n"
4733 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4734 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4735 "@pxref{ISCACHED})."
4738 new_command("GETCONFIG", 1, 1, 0, getconfig_command, _(
4739 "GETCONFIG [filename] <parameter>\n"
4740 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4741 "data response. If no file has been opened then the value for @var{filename} "
4742 "or the default from the @var{global} section will be returned. If a file "
4743 "has been opened and no @var{filename} is specified, the value previously "
4744 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4747 new_command("OPTION", 1, 1, 0, option_command, _(
4748 "OPTION <NAME>=[<VALUE>]\n"
4749 "Sets a client option @var{name} to @var{value}. The value for an option is "
4750 "kept for the duration of the connection with the exception of the "
4751 "@command{pinentry} options which are defaults for all future connections "
4752 "(@pxref{Pinentry}). When @var{value} is empty the option is unset."
4753 "@*@*"
4754 "@table @asis\n"
4755 "@item DISABLE-PINENTRY\n"
4756 "Disable use of @command{pinentry} for passphrase retrieval. When @code{1}, a "
4757 "server inquire is sent to the client to obtain the passphrase. This option "
4758 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4759 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands. Set to @code{0} "
4760 "to use a @command{pinentry}."
4761 "@*@*"
4762 "@item DISPLAY\n"
4763 "Set or unset the X11 display to use when prompting for a passphrase."
4764 "@*@*"
4765 "@item TTYNAME\n"
4766 "Set the terminal device path to use when prompting for a passphrase."
4767 "@*@*"
4768 "@item TTYTYPE\n"
4769 "Set the terminal type for use with @option{TTYNAME}."
4770 "@*@*"
4771 "@item NAME\n"
4772 "Associates the thread ID of the connection with the specified textual "
4773 "representation. Useful for debugging log messages. May not contain whitespace."
4774 "@*@*"
4775 "@item LOCK-TIMEOUT\n"
4776 "When not @code{0}, the duration in tenths of a second to wait for the file "
4777 "mutex which has been locked by another thread to be released before returning "
4778 "an error. When @code{-1} the error will be returned immediately."
4779 "@end table\n"
4782 new_command("LS", 1, 1, 0, ls_command, _(
4783 "LS\n"
4784 "Returns a newline separated list of data files stored in the data directory "
4785 "@file{HOMEDIR/data} (@pxref{Invoking}) with a data response."
4788 new_command("RESET", 1, 1, 0, NULL, _(
4789 "RESET\n"
4790 "Closes the currently opened file but keeps any previously set client options "
4791 "(@pxref{OPTION})."
4794 new_command("NOP", 1, 1, 0, NULL, _(
4795 "NOP\n"
4796 "Does nothing. Always returns successfully."
4799 /* !END-HELP-TEXT! */
4800 new_command ("CANCEL", 1, 1, 0, NULL, NULL);
4801 new_command ("END", 1, 1, 0, NULL, NULL);
4802 new_command ("BYE", 1, 1, 0, NULL, NULL);
4804 int i;
4805 for (i = 0; command_table[i]; i++);
4806 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4807 sort_commands);
4810 gpg_error_t
4811 register_commands (assuan_context_t ctx)
4813 int i = 0, rc;
4815 for (; command_table[i]; i++)
4817 if (!command_table[i]->handler)
4818 continue;
4820 rc = assuan_register_command (ctx, command_table[i]->name,
4821 command_table[i]->handler,
4822 command_table[i]->help);
4823 if (rc)
4824 return rc;
4827 rc = assuan_register_bye_notify (ctx, bye_notify);
4828 if (rc)
4829 return rc;
4831 rc = assuan_register_reset_notify (ctx, reset_notify);
4832 if (rc)
4833 return rc;
4835 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4836 if (rc)
4837 return rc;
4839 return assuan_register_post_cmd_notify (ctx, command_finalize);