Reset signal_thread_key upon cancellation.
[libpwmd.git] / src / commands.c
blobdc5cccf1021388a1da9f69ef4b1ec2e2ceb8e5f4
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>
38 #include <signal.h>
40 #include "pwmd-error.h"
41 #include <gcrypt.h>
43 #include "mem.h"
44 #include "xml.h"
45 #include "util-misc.h"
46 #include "common.h"
47 #include "rcfile.h"
48 #include "cache.h"
49 #include "commands.h"
50 #include "mutex.h"
51 #include "crypto.h"
53 /* These are command option flags. */
54 #define OPT_INQUIRE 0x0001
55 #define OPT_NO_PASSPHRASE 0x0002
56 #define OPT_ASK 0x0004
57 #define OPT_LIST_RECURSE 0x0008
58 #define OPT_VERBOSE 0x0010
59 #define OPT_LOCK 0x0020
60 #define OPT_LOCK_ON_OPEN 0x0040
61 #define OPT_SIGN 0x0080
62 #define OPT_LIST_ALL 0x0100
63 #define OPT_DATA 0x0200
64 #define OPT_NO_AGENT 0x0400
65 #define OPT_SECRET_ONLY 0x0800
66 #define OPT_INQUIRE_KEYID 0x1000
67 #define OPT_INQUIRE_SIGN_KEYID 0x2000
68 #define OPT_SYMMETRIC 0x4000
69 #define OPT_NO_SIGNER 0x8000
70 #define OPT_HTML 0x10000
71 #define OPT_CACHE_AGENT 0x20000
72 #define OPT_CACHE_SIGN 0x40000
74 #define FLOCK_TYPE_NONE 0
75 #define FLOCK_TYPE_SH 0x0001
76 #define FLOCK_TYPE_EX 0x0002
77 #define FLOCK_TYPE_KEEP 0x0004
79 struct command_table_s
81 const char *name;
82 gpg_error_t (*handler) (assuan_context_t, char *line);
83 const char *help;
84 int ignore_startup;
85 int unlock; // unlock the file mutex after validating the checksum
86 uint32_t flock_type;
89 static struct command_table_s **command_table;
91 static gpg_error_t do_lock (struct client_s *client, int add);
92 static gpg_error_t validate_checksum (struct client_s *, const char *filename,
93 struct cache_data_s *, unsigned char **,
94 size_t *);
95 static gpg_error_t update_checksum (struct client_s *client);
97 /* When 'status' is true the 'self' field of the status line will be false
98 * because we never send the STATE status message to the same client that
99 * initiated it. */
100 static char *
101 build_client_info_line (struct client_thread_s *thd, int status)
103 char *uid, *username = NULL;
104 char *line;
105 char *name = pthread_getspecific (thread_name_key);
107 #ifdef WITH_GNUTLS
108 if (thd->remote)
109 uid = str_asprintf("#%s", thd->tls->fp);
110 else
112 uid = str_asprintf("%u", thd->peer->uid);
113 username = get_username (thd->peer->uid);
115 #else
116 uid = str_asprintf("%u", thd->peer->uid);
117 username = get_username (thd->peer->uid);
118 #endif
119 line = str_asprintf ("%p %s %s %s %u %u %u %s %s %u",
120 thd->tid,
121 name ? name : "-",
122 thd->cl && thd->cl->filename
123 && (thd->cl->flags & FLAG_OPEN)
124 ? thd->cl->filename : "/",
125 #ifdef WITH_GNUTLS
126 thd->remote ? thd->peeraddr : "-",
127 #else
128 "-",
129 #endif
130 thd->cl && thd->cl->flags & FLAG_HAS_LOCK ? 1 : 0,
131 !status && pthread_equal (pthread_self (), thd->tid) ? 1 : 0,
132 thd->state, uid,
133 #ifdef WITH_GNUTLS
134 thd->remote ? "-" : username,
135 #else
136 username,
137 #endif
138 thd->conntime
141 xfree (username);
142 xfree (uid);
143 return line;
146 void
147 update_client_state (struct client_s *client, unsigned s)
149 MUTEX_LOCK (&cn_mutex);
150 client->thd->state = s;
151 MUTEX_UNLOCK (&cn_mutex);
153 if (client->thd->state != CLIENT_STATE_UNKNOWN)
155 char *line = build_client_info_line (client->thd, 1);
157 pthread_cleanup_push (xfree, line);
158 if (line)
159 send_status_all_not_self (STATUS_STATE, "%s", line);
160 pthread_cleanup_pop (1);
164 static gpg_error_t
165 unlock_file_mutex (struct client_s *client, int remove)
167 gpg_error_t rc = 0;
169 // OPEN: keep the lock for the same file being reopened.
170 if (client->flags & FLAG_KEEP_LOCK && client->flags & FLAG_HAS_LOCK)
171 return 0;
173 if (!(client->flags & FLAG_HAS_LOCK))
174 return GPG_ERR_NOT_LOCKED;
176 rc = cache_unlock_mutex (client->filename, remove);
177 if (rc)
178 rc = GPG_ERR_INV_STATE;
179 else
180 client->flags &= ~(FLAG_HAS_LOCK | FLAG_LOCK_CMD);
182 return rc;
185 static gpg_error_t
186 lock_file_mutex (struct client_s *client, int add)
188 gpg_error_t rc = 0;
189 int timeout = config_get_integer (client->filename, "cache_timeout");
191 if (client->flags & FLAG_HAS_LOCK)
192 return 0;
194 rc = cache_lock_mutex (client->ctx, client->filename,
195 client->lock_timeout, add, timeout);
196 if (!rc)
197 client->flags |= FLAG_HAS_LOCK;
199 return rc;
202 static gpg_error_t
203 file_modified (struct client_s *client, struct command_table_s *cmd)
205 gpg_error_t rc = 0;
206 int type = !cmd->flock_type || (cmd->flock_type & FLOCK_TYPE_SH)
207 ? LOCK_SH : LOCK_EX;
209 if (!(client->flags & FLAG_OPEN))
210 return GPG_ERR_INV_STATE;
212 rc = lock_file_mutex (client, 0);
213 if (rc && rc != GPG_ERR_NO_DATA)
214 return rc;
216 rc = lock_flock (client->ctx, client->filename, type, &client->flock_fd);
217 if (!rc)
219 rc = validate_checksum (client, client->filename, NULL, NULL, NULL);
220 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
221 rc = 0;
222 else if (rc)
223 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
225 else if (gpg_err_code (rc) == GPG_ERR_ENOENT)
226 rc = 0;
228 /* FLAG_HAS_LOCK may have been set by the LOCK command or OPEN --lock. */
229 if ((cmd->unlock && !(client->flags & FLAG_HAS_LOCK)) || rc)
230 unlock_file_mutex (client, 0);
232 if (rc || !(cmd->flock_type & FLOCK_TYPE_KEEP))
233 unlock_flock (&client->flock_fd);
235 return rc;
238 static gpg_error_t
239 parse_xml (assuan_context_t ctx, int new)
241 struct client_s *client = assuan_get_pointer (ctx);
242 int cached = client->doc != NULL;
243 gpg_error_t rc = 0;
245 if (new)
247 client->doc = xml_new_document ();
248 if (client->doc)
250 xmlChar *result;
251 int len;
253 xmlDocDumpFormatMemory (client->doc, &result, &len, 0);
254 client->crypto->plaintext = result;
255 client->crypto->plaintext_size = len;
256 if (!client->crypto->plaintext)
258 xmlFreeDoc (client->doc);
259 client->doc = NULL;
260 rc = GPG_ERR_ENOMEM;
263 else
264 rc = GPG_ERR_ENOMEM;
266 else if (!cached)
267 rc = xml_parse_doc ((char *) client->crypto->plaintext,
268 client->crypto->plaintext_size,
269 (xmlDocPtr *)&client->doc);
271 return rc;
274 static void
275 free_client (struct client_s *client)
277 if (client->doc)
278 xmlFreeDoc (client->doc);
280 xfree (client->crc);
281 xfree (client->filename);
282 xfree (client->last_error);
283 crypto_free (client->crypto);
284 client->crypto = NULL;
287 void
288 reset_client (struct client_s *client)
290 assuan_context_t ctx = client->ctx;
291 struct client_thread_s *thd = client->thd;
292 long lock_timeout = client->lock_timeout;
293 xmlErrorPtr xml_error = client->xml_error;
294 int no_pinentry = (client->flags & FLAG_NO_PINENTRY);
295 int flock_fd = client->flock_fd;
297 unlock_file_mutex (client, client->flags & FLAG_NEW);
298 free_client (client);
299 memset (client, 0, sizeof (struct client_s));
300 client->flock_fd = flock_fd;
301 client->xml_error = xml_error;
302 client->ctx = ctx;
303 client->thd = thd;
304 client->lock_timeout = lock_timeout;
305 client->flags |= no_pinentry ? FLAG_NO_PINENTRY : 0;
308 static void
309 req_free (void *arg)
311 if (!arg)
312 return;
314 strv_free ((char **) arg);
317 static gpg_error_t
318 parse_open_opt_lock (void *data, void *value)
320 struct client_s *client = data;
322 client->opts |= OPT_LOCK_ON_OPEN;
323 return 0;
326 static gpg_error_t
327 parse_opt_inquire (void *data, void *value)
329 struct client_s *client = data;
331 (void) value;
332 client->opts |= OPT_INQUIRE;
333 return 0;
336 static gpg_error_t
337 update_checksum (struct client_s *client)
339 unsigned char *crc;
340 size_t len;
341 struct cache_data_s *cdata;
342 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
344 if (rc)
345 return rc;
347 xfree (client->crc);
348 client->crc = crc;
349 cdata = cache_get_data (client->filename);
350 if (cdata)
352 xfree (cdata->crc);
353 cdata->crc = xmalloc (len);
354 memcpy (cdata->crc, crc, len);
357 return 0;
360 static gpg_error_t
361 validate_checksum (struct client_s *client, const char *filename,
362 struct cache_data_s *cdata, unsigned char **r_crc,
363 size_t *r_crclen)
365 unsigned char *crc;
366 size_t len;
367 gpg_error_t rc;
368 int n = 0;
370 if (cdata && !cdata->crc)
371 return GPG_ERR_CHECKSUM;
373 rc = get_checksum (filename, &crc, &len);
374 if (rc)
375 return rc;
377 if (cdata)
378 n = memcmp (cdata->crc, crc, len);
379 else if (client->crc)
380 n = memcmp (client->crc, crc, len);
382 if (!n && r_crc)
384 *r_crc = crc;
385 *r_crclen = len;
387 else
388 xfree (crc);
390 return n ? GPG_ERR_CHECKSUM : 0;
393 static gpg_error_t
394 open_command (assuan_context_t ctx, char *line)
396 gpg_error_t rc;
397 struct client_s *client = assuan_get_pointer (ctx);
398 char **req, *filename;
399 int same_file = 0;
400 assuan_peercred_t peer;
401 struct cache_data_s *cdata = NULL;
402 int cached = 0;
403 unsigned char *crc = NULL;
404 size_t crclen = 0;
405 struct argv_s *args[] = {
406 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
407 NULL
410 rc = parse_options (&line, args, client, 1);
411 if (rc)
412 return send_error (ctx, rc);
414 req = str_split (line, " ", 2);
415 if (!req)
416 return send_error (ctx, GPG_ERR_SYNTAX);
418 rc = do_validate_peer (ctx, req[0], &peer);
419 if (rc == GPG_ERR_FORBIDDEN)
420 rc = peer_is_invoker (client);
422 if (rc)
424 strv_free (req);
425 return send_error (ctx, rc);
428 filename = req[0];
429 if (!valid_filename (filename))
431 strv_free (req);
432 return send_error (ctx, GPG_ERR_INV_VALUE);
435 pthread_cleanup_push ((void *)req_free, req);
436 /* This client may have locked a different file with ISCACHED --lock than
437 * the current filename. This will remove that lock. */
438 same_file = client->filename && !strcmp (filename, client->filename);
439 if (client->flags & FLAG_OPEN ||
440 (client->flags & FLAG_HAS_LOCK && !same_file))
442 uint32_t opts = client->opts;
443 uint32_t flags = client->flags;
445 if (same_file)
446 client->flags |= FLAG_KEEP_LOCK;
447 else if (client->flags & FLAG_NEW)
448 cache_clear (NULL, client->filename, 0);
450 reset_client (client);
451 client->opts = opts;
452 client->flags |= flags;
453 client->flags &= ~(FLAG_LOCK_CMD);
454 if (!same_file)
455 client->flags &= ~(FLAG_HAS_LOCK | FLAG_NEW);
458 client->filename = str_dup (filename);
459 if (!client->filename)
461 strv_free (req);
462 return send_error(ctx, GPG_ERR_ENOMEM);
465 /* Need to lock the mutex here because file_modified() cannot without
466 * knowing the filename. */
467 rc = lock_file_mutex (client, 1);
468 if (rc)
469 client->flags &= ~FLAG_OPEN;
471 if (!rc)
473 rc = lock_flock (ctx, filename, LOCK_SH, &client->flock_fd);
474 if (gpg_err_code (rc) == GPG_ERR_ENOENT)
475 rc = 0;
478 if (!rc)
480 struct stat st;
481 char *keyfile = config_get_string (client->filename, "passphrase_file");
483 rc = crypto_init (&client->crypto, client->ctx, client->filename,
484 client->flags & FLAG_NO_PINENTRY, keyfile);
485 if (rc)
486 xfree (keyfile);
488 if (!rc && stat (client->filename, &st) == -1)
489 rc = gpg_error_from_errno (errno);
490 else if (!rc && !S_ISREG (st.st_mode)) // to match LS output
491 rc = gpg_error_from_errno (ENOANO);
494 if (!rc || gpg_err_code (rc) == GPG_ERR_ENOENT)
496 cdata = cache_get_data (client->filename);
498 if (rc) // new file
500 rc = 0;
501 client->flags |= FLAG_NEW;
502 // data file disappeared. clear the cache entry.
503 cache_clear (NULL, client->filename, 1);
504 cdata = NULL;
506 else if (cdata && cdata->doc) // cached document
508 int reload = 0;
509 int defer = 0;
511 if (!rc && !(client->flags & FLAG_NEW))
513 rc = validate_checksum (client, client->filename, cdata, &crc,
514 &crclen);
515 if (rc == GPG_ERR_CHECKSUM)
517 rc = 0;
518 reload = 1;
522 if (!rc)
524 rc = cache_iscached (client->filename, &defer, 0, 0);
525 if ((!rc || rc == GPG_ERR_ENOENT) && defer)
527 rc = 0;
528 reload = 2;
532 if (!rc && reload)
534 if (reload == 1)
535 log_write ("%s: %s", client->filename,
536 pwmd_strerror (GPG_ERR_CHECKSUM));
537 cache_clear (NULL, client->filename, 1);
538 cdata = NULL;
539 rc = crypto_decrypt (client, client->crypto);
541 #ifdef WITH_GNUTLS
542 else if (!rc)
544 if (client->thd->remote
545 && config_get_boolean (client->filename, "tcp_require_key")
546 && !(client->flags & FLAG_NEW))
547 rc = crypto_try_decrypt (client,
548 (client->flags & FLAG_NO_PINENTRY));
550 #endif
552 if (!rc && cdata)
554 client->crypto->plaintext = cdata->doc;
555 client->crypto->plaintext_size = cdata->size;
556 rc = cache_decrypt (client->crypto);
557 if (!rc)
559 cdata->doc = NULL;
560 cdata->size = 0;
561 strv_free (client->crypto->pubkey);
562 strv_free (client->crypto->sigkey);
563 client->crypto->pubkey = strv_dup (cdata->pubkey);
564 client->crypto->sigkey = strv_dup (cdata->sigkey);
565 cached = 1;
567 else
569 client->crypto->plaintext = NULL;
570 client->crypto->plaintext_size = 0;
574 else // existing file
576 cached = cdata != NULL;
577 rc = crypto_decrypt (client, client->crypto);
581 if (!rc)
583 rc = parse_xml (ctx, client->flags & FLAG_NEW);
584 if (!rc)
586 rc = cache_encrypt (client->crypto);
587 if (rc)
588 cache_clear (NULL, client->filename, 1);
589 else
591 int timeout = config_get_integer (client->filename,
592 "cache_timeout");
594 cache_free_data_once (cdata);
595 cdata = xcalloc (1, sizeof (struct cache_data_s));
596 cdata->doc = client->crypto->plaintext;
597 cdata->size = client->crypto->plaintext_size;
598 cdata->pubkey = strv_dup(client->crypto->pubkey);
599 cdata->sigkey = strv_dup(client->crypto->sigkey);
600 client->crypto->plaintext = NULL;
601 client->crypto->plaintext_size = 0;
603 if (cached) // wont increment the refcount
605 /* Prevent using another FD to update the checksum for a
606 * cached data file. The validity has already been
607 * verified. */
608 xfree (client->crc);
609 client->crc = xmalloc (crclen);
610 memcpy (client->crc, crc, crclen);
611 xfree (cdata->crc);
612 cdata->crc = xmalloc (crclen);
613 memcpy (cdata->crc, crc, crclen);
615 rc = cache_set_data (client->filename, cdata);
616 /* The cache entry may have been removed and cache_set_data()
617 * already sent STATUS_CACHE. */
618 if (!cache_iscached (client->filename, NULL, 0, 0))
619 rc = send_status (ctx, STATUS_CACHE, NULL);
621 else
622 rc = cache_add_file (client->filename, cdata, timeout);
627 pthread_cleanup_pop (1);
628 xfree (crc);
630 if (!rc && !(client->flags & FLAG_NEW) && !cached)
631 rc = update_checksum (client);
633 if (!rc)
635 client->flags |= FLAG_OPEN;
637 if (client->flags & FLAG_NEW)
638 rc = send_status (ctx, STATUS_NEWFILE, NULL);
640 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
641 rc = do_lock (client, 0);
644 if (rc)
646 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
648 if (client->flags & FLAG_NEW)
649 cache_clear (NULL, client->filename, 1);
651 crypto_free (client->crypto);
652 client->crypto = NULL;
653 client->flags &= ~FLAG_OPEN;
655 else
656 crypto_free_non_keys (client->crypto);
658 return send_error (ctx, rc);
661 /* If not the invoking_user or is an existing file, check that the list of user
662 * supplied key ID's are in the list of current key ID's obtained when
663 * decrypting the data file.
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 /* Requires that the keyid be a fingerprint in 16 byte form. */
717 static gpg_error_t
718 parse_save_opt_keyid_common (struct client_s *client, const char **list,
719 const char *value, char ***dst)
721 gpg_error_t rc = 0;
722 char **keys = NULL;
724 if (value && *value)
726 keys = str_split (value, ",", 0);
727 if (!keys)
728 return GPG_ERR_ENOMEM;
731 rc = crypto_keyid_to_16b (keys);
732 if (!rc)
733 rc = permitted_to_save (client, list, (const char **)keys);
735 if (!rc)
736 *dst = keys;
737 else
738 strv_free (keys);
740 return rc;
743 static gpg_error_t
744 parse_save_opt_keyid (void *data, void *value)
746 struct client_s *client = data;
747 const char *str = value;
748 char **dst = NULL;
749 gpg_error_t rc;
751 rc = parse_save_opt_keyid_common (client,
752 (const char **)client->crypto->pubkey,
753 str, &dst);
754 if (rc)
755 return rc;
757 client->crypto->save.pubkey = dst;
758 return 0;
761 static gpg_error_t
762 parse_save_opt_sign_keyid (void *data, void *value)
764 struct client_s *client = data;
765 const char *str = value;
766 char **dst = NULL;
767 gpg_error_t rc;
769 rc = parse_save_opt_keyid_common (client,
770 (const char **)client->crypto->sigkey,
771 str, &dst);
772 if (rc)
773 return rc;
775 if (!dst)
776 client->opts |= OPT_NO_SIGNER;
778 client->crypto->save.sigkey = dst;
779 return 0;
782 static gpg_error_t
783 parse_save_opt_inquire (struct client_s *client, uint32_t opt)
785 if (opt == OPT_INQUIRE && !(client->flags & FLAG_NEW))
787 gpg_error_t rc = peer_is_invoker (client);
789 if (rc)
790 return rc;
793 client->opts |= opt;
794 return 0;
797 static gpg_error_t
798 parse_save_opt_inquire_keyparam (void *data, void *value)
800 return parse_save_opt_inquire (data, OPT_INQUIRE);
803 static gpg_error_t
804 parse_save_opt_inquire_keyid (void *data, void *value)
806 return parse_save_opt_inquire (data, OPT_INQUIRE_KEYID);
809 static gpg_error_t
810 parse_save_opt_inquire_sign_keyid (void *data, void *value)
812 return parse_save_opt_inquire (data, OPT_INQUIRE_SIGN_KEYID);
815 static gpg_error_t
816 parse_save_opt_symmetric (void *data, void *value)
818 struct client_s *client = data;
820 client->opts |= OPT_SYMMETRIC;
821 return 0;
824 /* Tests that the keys in new_keys are also in old_keys. */
825 static gpg_error_t
826 compare_keys (char **new_keys, char **old_keys)
828 char **o;
830 if (!old_keys || !*old_keys)
831 return 0;
833 crypto_keyid_to_16b (new_keys);
835 for (o = old_keys; *o; o++)
837 char **n;
839 for (n = new_keys; *n; n++)
841 if (!strcmp (*n, *o))
842 break;
845 if (!*n)
846 return GPG_ERR_NOT_FOUND;
849 return 0;
852 static gpg_error_t
853 inquire_keyid (struct client_s *client, uint32_t opt)
855 gpg_error_t rc;
856 unsigned char *result = NULL;
857 size_t len;
858 char *s;
859 char ***orig;
860 char ***save;
862 if (opt == OPT_INQUIRE_KEYID)
864 s = "INQUIRE_KEYID";
865 orig = &client->crypto->pubkey;
866 save = &client->crypto->save.pubkey;
868 else
870 s = "INQUIRE_SIGN_KEYID";
871 orig = &client->crypto->sigkey;
872 save = &client->crypto->save.sigkey;
875 rc = assuan_inquire (client->ctx, s, &result, &len, 0);
876 if (!rc)
878 char **dst = NULL;
880 rc = parse_save_opt_keyid_common (client, (const char **)*orig,
881 (char *)result, &dst);
882 if (!rc)
884 if (!dst && opt == OPT_INQUIRE_SIGN_KEYID
885 && client->opts & OPT_SYMMETRIC)
886 client->opts |= OPT_NO_SIGNER;
888 *save = dst;
892 xfree (result);
893 return rc;
896 /* The caching test of gpg-agent is both a blessing and a curse. When a key
897 * lookup in its cache fails it tries the last successful key to be unlocked.
898 * This prevents pwmd from successfully clearing a signing key since the
899 * previous key, which more than likely belongs to the same keypair as the
900 * encryption/decryption key, will have the passphrase cached and therefore the
901 * signing key also cached. So we need to clear both the signing and encryption
902 * keys to get the effect of requiring a passphrase when generating a new
903 * keypair for an existing data file, or when the "require_save_key"
904 * configuration parameter is set. The "require_save_key" parameter is mostly a
905 * failsafe of the gpg-agent option --ignore-cache-for-signing since
906 * some/most/all users may fail to set it.
908 static gpg_error_t
909 save_command (assuan_context_t ctx, char *line)
911 struct client_s *client = assuan_get_pointer (ctx);
912 struct cache_data_s *cdata = NULL;
913 gpg_error_t rc = 0, cached = 0;
914 int defer = 0;
915 struct stat st;
916 struct argv_s *args[] = {
917 &(struct argv_s) {"keyid", OPTION_TYPE_ARG, parse_save_opt_keyid},
918 &(struct argv_s) {"sign-keyid", OPTION_TYPE_OPTARG,
919 parse_save_opt_sign_keyid},
920 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
921 parse_save_opt_inquire_keyparam },
922 &(struct argv_s) {"inquire-keyid", OPTION_TYPE_NOARG,
923 parse_save_opt_inquire_keyid },
924 &(struct argv_s) {"inquire-sign-keyid", OPTION_TYPE_NOARG,
925 parse_save_opt_inquire_sign_keyid },
926 &(struct argv_s) {"symmetric", OPTION_TYPE_NOARG,
927 parse_save_opt_symmetric },
928 NULL
931 crypto_free_save (&client->crypto->save);
933 if (!(client->flags & FLAG_NEW))
935 rc = crypto_is_symmetric (client->filename);
936 if (!rc || rc == GPG_ERR_BAD_DATA)
938 if (!rc)
939 client->opts |= OPT_SYMMETRIC;
941 rc = 0; // PKI
945 if (rc)
946 return send_error (ctx, rc);
948 rc = parse_options (&line, args, client, 0);
949 if (rc)
950 return send_error (ctx, rc);
952 if (config_get_boolean (client->filename, "require_save_key"))
953 client->opts |= OPT_ASK;
955 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
956 return send_error (ctx, gpg_error_from_errno (errno));
958 if (errno != ENOENT && !S_ISREG (st.st_mode))
960 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
961 return send_error (ctx, GPG_ERR_ENOANO);
964 if (!rc)
965 cached = rc = cache_iscached (client->filename, &defer, 0, 1);
967 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
968 rc = 0;
969 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
970 rc = 0;
972 if (rc)
973 return send_error (ctx, rc);
975 /* Specifying both a recipient and symmetric encryption is an error. */
976 if (client->opts & OPT_INQUIRE_KEYID && client->opts & OPT_SYMMETRIC)
978 return send_error (ctx, GPG_ERR_CONFLICT);
980 else if (client->opts & OPT_INQUIRE && client->opts & OPT_SYMMETRIC)
982 return send_error (ctx, GPG_ERR_CONFLICT);
984 /* Existing file with a recipient and wanting symmetric is an error. */
985 else if (client->opts & OPT_SYMMETRIC && client->crypto->save.pubkey)
987 return send_error (ctx, GPG_ERR_CONFLICT);
989 else if (((client->opts & OPT_INQUIRE_KEYID)
990 || (client->opts & OPT_INQUIRE_SIGN_KEYID)))
992 if (client->opts & OPT_INQUIRE)
993 return send_error (ctx, GPG_ERR_CONFLICT);
995 if (client->opts & OPT_INQUIRE_KEYID)
996 rc = inquire_keyid (client, OPT_INQUIRE_KEYID);
998 if (!rc && (client->opts & OPT_INQUIRE_SIGN_KEYID))
999 rc = inquire_keyid (client, OPT_INQUIRE_SIGN_KEYID);
1001 else if ((client->crypto->save.pubkey || client->crypto->save.sigkey)
1002 && client->opts & OPT_INQUIRE)
1003 return send_error (ctx, GPG_ERR_CONFLICT);
1005 if (!rc)
1007 client->crypto->keyfile = config_get_string (client->filename,
1008 "passphrase_file");
1009 rc = crypto_init_ctx (client->crypto, (client->flags & FLAG_NO_PINENTRY),
1010 client->crypto->keyfile);
1013 if (!rc && (client->opts & OPT_INQUIRE))
1015 /* Require a passphrase when generating a new key pair for an existing
1016 * file.
1018 if (!(client->flags & FLAG_NEW))
1020 rc = cache_clear_agent_keys (client->filename, 1, 1);
1021 if (!rc)
1023 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
1024 client->opts &= ~OPT_ASK;
1028 if (!rc)
1030 unsigned char *params = NULL;
1031 size_t len;
1033 rc = assuan_inquire (client->ctx, "KEYPARAM", &params, &len, 0);
1034 if (!rc)
1036 client->crypto->flags |= CRYPTO_FLAG_NEWFILE;
1037 pthread_cleanup_push ((void *)xfree, params);
1038 rc = crypto_genkey (client, client->crypto, params);
1039 pthread_cleanup_pop (1);
1043 else if (!rc && (client->flags & FLAG_NEW))
1045 if (!client->crypto->save.pubkey && !client->crypto->save.sigkey
1046 && !(client->opts & OPT_SYMMETRIC))
1048 char *params = crypto_default_key_params ();
1050 if (!params)
1051 rc = GPG_ERR_ENOMEM;
1053 if (!rc)
1055 client->crypto->flags |= CRYPTO_FLAG_NEWFILE;
1056 pthread_cleanup_push ((void *)xfree, params);
1057 rc = crypto_genkey (client, client->crypto,
1058 (unsigned char *)params);
1059 pthread_cleanup_pop (1);
1062 else
1064 if (!client->crypto->save.pubkey && client->crypto->save.sigkey
1065 && !(client->opts & OPT_SYMMETRIC))
1066 client->crypto->save.pubkey = strv_dup (client->crypto->save.sigkey);
1068 if (client->crypto->save.pubkey && !client->crypto->save.sigkey)
1069 client->crypto->save.sigkey = strv_dup (client->crypto->save.pubkey);
1072 else if (!rc)
1074 cdata = cache_get_data (client->filename);
1075 if (cdata)
1077 if (client->crypto->save.pubkey)
1078 rc = compare_keys (client->crypto->save.pubkey, cdata->pubkey);
1080 /* Always allow a signer for symmetric data files. */
1081 if (!rc && client->crypto->save.sigkey
1082 && !(client->opts & OPT_SYMMETRIC))
1083 rc = compare_keys (client->crypto->save.sigkey, cdata->sigkey);
1085 /* Prevent saving to a recipient who is not in the original recipient
1086 * list without a passphrase. */
1087 if (rc || (client->crypto->sigkey && client->opts & OPT_NO_SIGNER))
1088 client->opts |= OPT_ASK;
1090 if (rc == GPG_ERR_NOT_FOUND)
1092 rc = peer_is_invoker (client);
1093 if (rc == GPG_ERR_EACCES)
1094 rc = GPG_ERR_FORBIDDEN;
1097 if (!client->crypto->save.pubkey
1098 && !(client->opts & OPT_SYMMETRIC))
1099 client->crypto->save.pubkey = strv_dup (cdata->pubkey);
1101 if (!rc && !client->crypto->save.sigkey && cdata->sigkey
1102 && !(client->opts & OPT_NO_SIGNER))
1103 client->crypto->save.sigkey = strv_dup (cdata->sigkey);
1104 else if (!rc && !client->crypto->save.sigkey
1105 && (client->opts & OPT_NO_SIGNER)
1106 && !(client->opts & OPT_SYMMETRIC))
1107 rc = GPG_ERR_NO_SIGNATURE_SCHEME;
1109 else
1111 client->crypto->save.pubkey = strv_dup (client->crypto->pubkey);
1112 client->crypto->save.sigkey = strv_dup (client->crypto->sigkey);
1116 if (!rc && !(client->flags & FLAG_NEW))
1118 /* Fixes {NEW_,SIGN_}PASSPHRASE inquire keywords. See passphrase_cb(). */
1119 if (client->opts & OPT_SYMMETRIC)
1120 client->crypto->flags |= CRYPTO_FLAG_PASSWD;
1122 if (client->opts & OPT_ASK)
1124 rc = cache_clear_agent_keys (client->filename, 1, 1);
1125 if (!rc)
1126 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
1130 if (!rc && client->opts & OPT_SYMMETRIC)
1131 client->crypto->flags |= CRYPTO_FLAG_PASSWD_NEW;
1133 if (!rc)
1134 rc = xml_update_element_mtime (client, xmlDocGetRootElement (client->doc));
1136 if (!rc)
1138 int size;
1140 xmlDocDumpFormatMemory (client->doc, &client->crypto->plaintext, &size,
1142 if (size > 0)
1143 client->crypto->plaintext_size = (size_t) size;
1144 else
1145 rc = GPG_ERR_ENOMEM;
1147 if (!rc)
1149 client->crypto->flags |= (client->opts & OPT_SYMMETRIC) ? CRYPTO_FLAG_SYMMETRIC : 0;
1150 client->crypto->flags |= (client->flags & FLAG_NEW) ? CRYPTO_FLAG_NEWFILE : 0;
1151 client->crypto->flags |= !(client->opts & OPT_SYMMETRIC) ? CRYPTO_FLAG_PASSWD_SIGN : 0;
1152 rc = crypto_encrypt (client, client->crypto);
1153 client->crypto->flags &= ~CRYPTO_FLAG_SYMMETRIC;
1156 if (!rc)
1158 rc = crypto_write_file (client->crypto);
1159 if (!rc)
1161 if (!cached) // no error
1162 cdata = cache_get_data (client->filename);
1165 if (!rc)
1167 rc = cache_encrypt (client->crypto);
1168 if (rc)
1170 cache_clear (NULL, client->filename, 1);
1171 strv_free (client->crypto->pubkey);
1172 client->crypto->pubkey = strv_dup (client->crypto->save.pubkey);
1173 strv_free (client->crypto->sigkey);
1174 client->crypto->sigkey = strv_dup (client->crypto->save.sigkey);
1175 client->flags &= ~(FLAG_NEW);
1179 if (!rc)
1181 int timeout = config_get_integer (client->filename,
1182 "cache_timeout");
1184 cache_free_data_once (cdata);
1185 cdata = xcalloc (1, sizeof (struct cache_data_s));
1186 cdata->doc = client->crypto->plaintext;
1187 client->crypto->plaintext = NULL;
1188 cdata->size = client->crypto->plaintext_size;
1189 client->crypto->plaintext_size = 0;
1190 cdata->pubkey = client->crypto->save.pubkey;
1191 client->crypto->save.pubkey = NULL;
1192 cdata->sigkey = client->crypto->save.sigkey;
1193 client->crypto->save.sigkey = NULL;
1195 /* Update in case the cache entry expires the next SAVE may not
1196 * have any known keys. */
1197 strv_free (client->crypto->pubkey);
1198 client->crypto->pubkey = strv_dup (cdata->pubkey);
1199 strv_free (client->crypto->sigkey);
1200 client->crypto->sigkey = strv_dup (cdata->sigkey);
1202 if (!cached) // no error and wont increment refcount
1203 rc = cache_set_data (client->filename, cdata);
1204 else
1205 rc = cache_add_file (client->filename, cdata, timeout);
1207 if (!rc && (cached || (client->flags & FLAG_NEW)))
1208 send_status_all (STATUS_CACHE, NULL);
1210 if (!rc)
1212 rc = update_checksum (client);
1213 client->flags &= ~(FLAG_NEW);
1219 crypto_free_non_keys (client->crypto);
1220 return send_error (ctx, rc);
1223 static gpg_error_t
1224 do_delete (assuan_context_t ctx, char *line)
1226 struct client_s *client = assuan_get_pointer (ctx);
1227 struct xml_request_s *req;
1228 xmlNodePtr n;
1229 gpg_error_t rc;
1231 rc = xml_new_request (client, line, XML_CMD_DELETE, &req);
1232 if (rc)
1233 return rc;
1235 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1236 xml_free_request (req);
1237 if (rc)
1238 return rc;
1240 rc = xml_is_element_owner (client, n);
1241 if (!rc)
1242 rc = xml_unlink_node (client, n);
1244 return rc;
1247 static gpg_error_t
1248 delete_command (assuan_context_t ctx, char *line)
1250 struct client_s *client = assuan_get_pointer (ctx);
1251 gpg_error_t rc;
1252 struct argv_s *args[] = {
1253 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1254 NULL
1257 rc = parse_options (&line, args, client, 1);
1258 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1259 rc = GPG_ERR_SYNTAX;
1260 if (rc)
1261 return send_error (ctx, rc);
1263 if (client->opts & OPT_INQUIRE)
1265 unsigned char *result;
1266 size_t len;
1268 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1269 if (rc)
1270 return send_error (ctx, rc);
1272 pthread_cleanup_push ((void *)xfree, result);
1273 rc = do_delete (ctx, (char *)result);
1274 pthread_cleanup_pop (1);
1276 else
1277 rc = do_delete (ctx, line);
1279 return send_error (ctx, rc);
1282 static gpg_error_t
1283 update_element_expirey (struct client_s *client, xmlNodePtr n)
1285 gpg_error_t rc = 0;
1286 xmlChar *expire, *incr;
1287 char *p;
1288 time_t e, e_orig, i;
1289 time_t now = time (NULL);
1291 expire = xml_attribute_value (n, (xmlChar *)"expire");
1292 if (!expire)
1293 return 0;
1295 errno = 0;
1296 e = strtoul ((char *)expire, &p, 10);
1297 if (errno || *p || e == ULONG_MAX || *expire == '-')
1299 xmlFree (expire);
1300 rc = GPG_ERR_ERANGE;
1301 goto fail;
1304 xmlFree (expire);
1305 incr = xml_attribute_value (n, (xmlChar *)"expire_increment");
1306 if (!incr)
1307 return 0;
1309 i = strtoul ((char *)incr, &p, 10);
1310 if (errno || *p || i == ULONG_MAX || *incr == '-')
1312 xmlFree (incr);
1313 rc = GPG_ERR_ERANGE;
1314 goto fail;
1317 xmlFree (incr);
1318 e_orig = e;
1319 e = now + i;
1320 p = str_asprintf ("%lu", e);
1321 if (!p)
1323 rc = GPG_ERR_ENOMEM;
1324 goto fail;
1327 rc = xml_add_attribute (client, n, "expire", p);
1328 xfree (p);
1329 if (!rc)
1330 rc = send_status (client->ctx, STATUS_EXPIRE, "%lu %lu", e_orig, e);
1332 fail:
1333 return rc;
1336 static gpg_error_t
1337 store_command (assuan_context_t ctx, char *line)
1339 struct client_s *client = assuan_get_pointer (ctx);
1340 gpg_error_t rc;
1341 size_t len;
1342 unsigned char *result;
1343 xmlNodePtr n, parent;
1344 int has_content;
1345 char *content = NULL;
1346 struct xml_request_s *req;
1348 if (line && *line)
1349 return send_error (ctx, GPG_ERR_SYNTAX);
1351 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1352 if (rc)
1353 return send_error (ctx, rc);
1355 rc = xml_new_request (client, (char *)result, XML_CMD_STORE, &req);
1356 xfree (result);
1357 if (rc)
1358 return send_error (ctx, rc);
1360 /* Prevent passing the element content around to save some memory. */
1361 len = strv_length (req->args);
1362 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1363 if (*(req->args+1) && !xml_valid_element_path (req->args, has_content))
1365 xml_free_request (req);
1366 return send_error (ctx, GPG_ERR_INV_VALUE);
1369 if (has_content || !*req->args[len-1])
1371 has_content = 1;
1372 content = req->args[len-1];
1373 req->args[len-1] = NULL;
1376 if (strv_length (req->args) > 1)
1378 rc = xml_check_recursion (client, req);
1379 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
1380 goto fail;
1383 parent = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1385 if (!rc && len > 1)
1387 rc = xml_is_element_owner (client, parent);
1388 if (!rc)
1390 n = xml_find_text_node (parent->children);
1391 if (n)
1392 xmlNodeSetContent (n, (xmlChar *) content);
1393 else
1394 xmlNodeAddContent (parent, (xmlChar *) content);
1396 (void)xml_update_element_mtime (client, parent);
1397 (void)update_element_expirey (client, parent);
1401 fail:
1402 xfree (content);
1403 xml_free_request (req);
1404 return send_error (ctx, rc);
1407 static gpg_error_t
1408 xfer_data (assuan_context_t ctx, const char *line, int total)
1410 struct client_s *client = assuan_get_pointer (ctx);
1411 int to_send;
1412 int sent = 0;
1413 gpg_error_t rc = 0;
1414 int progress = config_get_integer ("global", "xfer_progress");
1415 int flush = 0;
1417 if (!(client->flags & FLAG_LOCK_CMD))
1418 rc = unlock_file_mutex (client, 0);
1419 if (rc && rc != GPG_ERR_NOT_LOCKED)
1420 return rc;
1422 progress =
1423 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1424 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1425 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1427 if (rc)
1428 return rc;
1430 again:
1433 if (sent + to_send > total)
1434 to_send = total - sent;
1436 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1437 flush ? 0 : to_send);
1438 if (!rc)
1440 sent += flush ? 0 : to_send;
1442 if ((progress && !(sent % progress) && sent != total) ||
1443 (sent == total && flush))
1444 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1446 if (!flush && !rc && sent == total)
1448 flush = 1;
1449 goto again;
1453 while (!rc && sent < total);
1455 return rc;
1458 static gpg_error_t
1459 do_get (assuan_context_t ctx, char *line)
1461 struct client_s *client = assuan_get_pointer (ctx);
1462 gpg_error_t rc;
1463 struct xml_request_s *req;
1464 xmlNodePtr n;
1466 rc = xml_new_request (client, line, XML_CMD_NONE, &req);
1467 if (rc)
1468 return rc;
1470 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1471 if (!rc)
1473 if (n && n->children)
1475 xmlNodePtr tmp = n;
1477 n = xml_find_text_node (n->children);
1478 if (!n || !n->content || !*n->content)
1479 rc = GPG_ERR_NO_DATA;
1480 else
1482 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1483 if (!rc)
1485 xmlChar *expire = xml_attribute_value (tmp,
1486 (xmlChar *)"expire");
1487 if (expire)
1489 time_t now = time (NULL);
1490 time_t e;
1491 char *p;
1493 errno = 0;
1494 e = strtoul ((char *)expire, &p, 10);
1495 if (errno || *p || e == ULONG_MAX || *expire == '-')
1496 log_write (_("invalid expire attribute value: %s"),
1497 expire);
1498 else if (now >= e)
1499 rc = send_status (ctx, STATUS_EXPIRE, "%lu 0", e);
1501 xmlFree (expire);
1506 else
1507 rc = GPG_ERR_NO_DATA;
1510 xml_free_request (req);
1511 return rc;
1514 static gpg_error_t
1515 get_command (assuan_context_t ctx, char *line)
1517 struct client_s *client = assuan_get_pointer (ctx);
1518 gpg_error_t rc;
1519 struct argv_s *args[] = {
1520 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1521 NULL
1524 rc = parse_options (&line, args, client, 1);
1525 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1526 rc = GPG_ERR_SYNTAX;
1527 if (rc)
1528 return send_error (ctx, rc);
1530 if (client->opts & OPT_INQUIRE)
1532 unsigned char *result;
1533 size_t len;
1535 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1536 if (rc)
1537 return send_error (ctx, rc);
1539 pthread_cleanup_push ((void *)xfree, result);
1540 rc = do_get (ctx, (char *)result);
1541 pthread_cleanup_pop (1);
1543 else
1544 rc = do_get (ctx, line);
1546 return send_error (ctx, rc);
1549 static void list_command_free1 (void *arg);
1550 static gpg_error_t
1551 realpath_command (assuan_context_t ctx, char *line)
1553 gpg_error_t rc;
1554 char **realpath = NULL;
1555 struct client_s *client = assuan_get_pointer (ctx);
1556 struct argv_s *args[] = {
1557 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1558 NULL
1561 rc = parse_options (&line, args, client, 1);
1562 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1563 rc = GPG_ERR_SYNTAX;
1564 if (rc)
1565 return send_error (ctx, rc);
1567 if (client->opts & OPT_INQUIRE)
1569 unsigned char *result;
1570 size_t len;
1572 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1573 if (rc)
1574 return send_error (ctx, rc);
1576 pthread_cleanup_push ((void *)xfree, result);
1577 (void)xml_resolve_path (client, client->doc, result, &realpath, &rc);
1578 pthread_cleanup_pop (1);
1580 else
1581 (void)xml_resolve_path (client, client->doc, (unsigned char *)line,
1582 &realpath, &rc);
1584 if (!rc)
1586 char *tmp = strv_join ("\t", realpath);
1588 strv_free (realpath);
1589 if (!tmp)
1590 return send_error (ctx, GPG_ERR_ENOMEM);
1592 pthread_cleanup_push ((void *)xfree, tmp);
1593 rc = xfer_data (ctx, tmp, strlen (tmp));
1594 pthread_cleanup_pop (1);
1597 return send_error (ctx, rc);
1600 static void
1601 list_command_free1 (void *arg)
1603 if (arg)
1604 string_free ((struct string_s *) arg, 1);
1607 static gpg_error_t
1608 parse_list_opt_recurse (void *data, void *value)
1610 struct client_s *client = data;
1612 client->opts |= OPT_LIST_RECURSE;
1613 return 0;
1616 static gpg_error_t
1617 parse_opt_verbose (void *data, void *value)
1619 struct client_s *client = data;
1621 client->opts |= OPT_VERBOSE;
1622 return 0;
1625 static gpg_error_t
1626 list_path_once (struct client_s *client, char *line,
1627 struct element_list_s *elements, struct string_s *result)
1629 gpg_error_t rc;
1631 rc = xml_create_path_list (client, client->doc, NULL, elements, line, 0);
1632 if (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES)
1633 rc = 0;
1635 if (!rc)
1637 int total = slist_length (elements->list);
1639 if (total)
1641 int i;
1643 for (i = 0; i < total; i++)
1645 char *tmp = slist_nth_data (elements->list, i);
1647 string_append_printf (result, "%s%s", tmp,
1648 i + 1 == total ? "" : "\n");
1651 else
1652 rc = GPG_ERR_NO_DATA;
1655 return rc;
1658 static gpg_error_t
1659 do_list (assuan_context_t ctx, char *line)
1661 struct client_s *client = assuan_get_pointer (ctx);
1662 gpg_error_t rc = GPG_ERR_NO_DATA;
1663 struct element_list_s *elements = NULL;
1665 if (!line || !*line)
1667 struct string_s *str = string_new (NULL);
1668 xmlNodePtr n;
1670 if (!str)
1671 return GPG_ERR_ENOMEM;
1673 pthread_cleanup_push ((void *)list_command_free1, str);
1674 n = xmlDocGetRootElement (client->doc);
1675 for (n = n->children; n; n = n->next)
1677 xmlChar *name;
1679 if (n->type != XML_ELEMENT_NODE)
1680 continue;
1682 name = xmlGetProp (n, (xmlChar *)"_name");
1683 if (!name)
1685 rc = GPG_ERR_ENOMEM;
1686 break;
1689 elements = xcalloc (1, sizeof (struct element_list_s));
1690 if (!elements)
1692 xmlFree (name);
1693 rc = GPG_ERR_ENOMEM;
1694 break;
1697 elements->prefix = string_new (NULL);
1698 if (!elements->prefix)
1700 xmlFree (name);
1701 xml_free_element_list (elements);
1702 rc = GPG_ERR_ENOMEM;
1703 break;
1706 elements->recurse = client->opts & OPT_LIST_RECURSE;
1707 elements->root_only = !elements->recurse;
1708 pthread_cleanup_push ((void *)xml_free_element_list, elements);
1709 rc = list_path_once (client, (char *)name, elements, str);
1710 xmlFree (name);
1711 pthread_cleanup_pop (1);
1712 if (rc)
1713 break;
1715 if (n->next)
1716 string_append (str, "\n");
1719 if (!rc)
1720 rc = xfer_data (ctx, str->str, str->len);
1722 pthread_cleanup_pop (1);
1723 return rc;
1726 elements = xcalloc (1, sizeof (struct element_list_s));
1727 if (!elements)
1728 return GPG_ERR_ENOMEM;
1730 elements->prefix = string_new (NULL);
1731 if (!elements->prefix)
1733 xml_free_element_list (elements);
1734 return GPG_ERR_ENOMEM;
1737 elements->recurse = client->opts & OPT_LIST_RECURSE;
1738 pthread_cleanup_push ((void *)xml_free_element_list, elements);
1739 struct string_s *str = string_new (NULL);
1740 pthread_cleanup_push ((void *)list_command_free1, str);
1741 rc = list_path_once (client, line, elements, str);
1742 if (!rc)
1743 rc = xfer_data (ctx, str->str, str->len);
1745 pthread_cleanup_pop (1);
1746 pthread_cleanup_pop (1);
1747 return rc;
1750 static gpg_error_t
1751 list_command (assuan_context_t ctx, char *line)
1753 struct client_s *client = assuan_get_pointer (ctx);
1754 gpg_error_t rc;
1755 struct argv_s *args[] = {
1756 &(struct argv_s) {"recurse", OPTION_TYPE_NOARG, parse_list_opt_recurse},
1757 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1758 /* These are deprecated but kept for backward compatibility with older
1759 * clients. */
1760 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, NULL},
1761 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG, NULL},
1762 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_recurse},
1763 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG, NULL},
1764 NULL
1767 if (disable_list_and_dump == 1)
1768 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1770 rc = parse_options (&line, args, client, 1);
1771 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1772 rc = GPG_ERR_SYNTAX;
1773 if (rc)
1774 return send_error (ctx, rc);
1776 if (client->opts & OPT_INQUIRE)
1778 unsigned char *result;
1779 size_t len;
1781 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1782 if (rc)
1783 return send_error (ctx, rc);
1785 pthread_cleanup_push ((void *)xfree, result);
1786 rc = do_list (ctx, (char *)result);
1787 pthread_cleanup_pop (1);
1789 else
1790 rc = do_list (ctx, line);
1792 return send_error (ctx, rc);
1795 #define RESUMABLE_ERROR(rc) (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES \
1796 || rc == GPG_ERR_ELEMENT_NOT_FOUND || !rc)
1798 * args[0] - element path
1800 static gpg_error_t
1801 attribute_list (assuan_context_t ctx, char **args)
1803 struct client_s *client = assuan_get_pointer (ctx);
1804 char **attrlist = NULL;
1805 int i = 0;
1806 int target = 0;
1807 xmlAttrPtr a;
1808 xmlNodePtr n, an, r;
1809 char *line;
1810 gpg_error_t rc;
1811 struct xml_request_s *req;
1813 if (!args || !args[0] || !*args[0])
1814 return GPG_ERR_SYNTAX;
1816 rc = xml_new_request (client, args[0], XML_CMD_ATTR_LIST, &req);
1817 if (rc)
1818 return rc;
1820 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1821 xml_free_request (req);
1822 if (rc)
1823 return rc;
1825 again:
1826 for (a = n->properties; a; a = a->next)
1828 char **pa;
1830 if (target && xml_reserved_attribute ((char *)a->name))
1831 continue;
1833 pa = xrealloc (attrlist, (i + 2) * sizeof (char *));
1834 if (!pa)
1836 if (attrlist)
1837 strv_free (attrlist);
1839 log_write ("%s(%i): %s", __FILE__, __LINE__,
1840 pwmd_strerror (GPG_ERR_ENOMEM));
1841 return GPG_ERR_ENOMEM;
1844 attrlist = pa;
1845 an = a->children;
1846 attrlist[i] = str_asprintf ("%s %s", (char *) a->name, an && an->content
1847 ? (char *)an->content : "");
1849 if (!attrlist[i])
1851 strv_free (attrlist);
1852 log_write ("%s(%i): %s", __FILE__, __LINE__,
1853 pwmd_strerror (GPG_ERR_ENOMEM));
1854 return GPG_ERR_ENOMEM;
1857 attrlist[++i] = NULL;
1860 if (!attrlist)
1861 return GPG_ERR_NO_DATA;
1863 if (!target)
1865 r = xml_resolve_path (client, client->doc, (xmlChar *)args[0], NULL, &rc);
1866 if (!RESUMABLE_ERROR (rc))
1868 strv_free (attrlist);
1869 return rc;
1871 else if (!rc && r != n)
1873 target = 1;
1874 n = r;
1875 goto again;
1878 rc = 0;
1881 line = strv_join ("\n", attrlist);
1882 if (!line)
1884 log_write ("%s(%i): %s", __FILE__, __LINE__,
1885 pwmd_strerror (GPG_ERR_ENOMEM));
1886 strv_free (attrlist);
1887 return GPG_ERR_ENOMEM;
1890 pthread_cleanup_push ((void *)xfree, line);
1891 pthread_cleanup_push ((void *)req_free, attrlist);
1892 rc = xfer_data (ctx, line, strlen (line));
1893 pthread_cleanup_pop (1);
1894 pthread_cleanup_pop (1);
1895 return rc;
1899 * args[0] - attribute
1900 * args[1] - element path
1902 static gpg_error_t
1903 attribute_delete (struct client_s *client, char **args)
1905 gpg_error_t rc;
1906 xmlNodePtr n;
1908 if (!args || !args[0] || !*args[0] || !args[1] || !*args[1])
1909 return GPG_ERR_SYNTAX;
1911 if (!strcmp (args[0], "_name") || !strcmp (args[0], "target"))
1912 return GPG_ERR_INV_ATTR;
1914 if (!xml_reserved_attribute (args[0]))
1915 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
1916 else
1918 struct xml_request_s *req;
1920 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
1921 if (rc)
1922 return rc;
1924 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1925 xml_free_request (req);
1928 if (!rc)
1929 rc = xml_is_element_owner (client, n);
1931 if (!rc)
1932 rc = xml_delete_attribute (client, n, (xmlChar *) args[0]);
1934 return rc;
1938 * Creates the "target" attribute. When other commands encounter an element
1939 * with this attribute they follow the "target" after appending remaining
1940 * elements from the original element path to the target. The exception is the
1941 * ATTR command that operates on the element itself (for reserved attribute
1942 * names) and not the target.
1944 * If the source element path doesn't exist when using 'ATTR SET target', it is
1945 * created, but the destination element path must exist. This is simliar to the
1946 * ls(1) command: ln -s src dst.
1948 * args[0] - source element path
1949 * args[1] - destination element path
1951 static gpg_error_t
1952 set_target_attribute (struct client_s *client, char **args)
1954 struct xml_request_s *req = NULL;
1955 char **src, **dst;
1956 gpg_error_t rc;
1957 xmlNodePtr n;
1959 if (!args || !args[0] || !args[1])
1960 return GPG_ERR_SYNTAX;
1962 src = str_split (args[0], "\t", 0);
1963 if (!src)
1964 return GPG_ERR_SYNTAX;
1966 if (!xml_valid_element_path (src, 0))
1968 strv_free (src);
1969 return GPG_ERR_INV_VALUE;
1972 dst = str_split (args[1], "\t", 0);
1973 if (!dst)
1975 strv_free (src);
1976 return GPG_ERR_SYNTAX;
1979 // Be sure the destination element path exists. */
1980 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
1981 if (rc)
1982 goto fail;
1984 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1985 if (rc && rc != GPG_ERR_EACCES)
1986 goto fail;
1988 xml_free_request (req);
1989 req = NULL;
1991 if (!strcmp (args[0], args[1]))
1993 rc = GPG_ERR_EEXIST;
1994 goto fail;
1997 rc = xml_new_request (client, args[0], XML_CMD_ATTR_TARGET, &req);
1998 if (rc)
1999 goto fail;
2001 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2002 if (rc && rc != GPG_ERR_EACCES)
2003 goto fail;
2005 if (!rc)
2007 rc = xml_add_attribute (client, n, "target", args[1]);
2008 if (!rc)
2009 rc = xml_unlink_node (client, n->children);
2012 fail:
2013 strv_free (src);
2014 strv_free (dst);
2015 xml_free_request (req);
2016 return rc;
2020 * args[0] - attribute
2021 * args[1] - element path
2023 static gpg_error_t
2024 attribute_get (assuan_context_t ctx, char **args)
2026 struct client_s *client = assuan_get_pointer (ctx);
2027 xmlNodePtr n;
2028 xmlChar *a;
2029 gpg_error_t rc;
2030 struct xml_request_s *req;
2032 if (!args || !args[0] || !*args[0] || !args[1] || !*args[1])
2033 return GPG_ERR_SYNTAX;
2035 if (!xml_reserved_attribute (args[0]))
2036 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
2037 else
2039 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
2040 if (rc)
2041 return rc;
2043 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2044 xml_free_request (req);
2047 if (rc)
2048 return rc;
2050 a = xmlGetProp (n, (xmlChar *) args[0]);
2051 if (!a)
2052 return GPG_ERR_NOT_FOUND;
2054 pthread_cleanup_push ((void *)xmlFree, a);
2056 if (*a)
2057 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
2058 else
2059 rc = GPG_ERR_NO_DATA;
2061 pthread_cleanup_pop (1);
2062 return rc;
2066 * args[0] - attribute
2067 * args[1] - element path
2068 * args[2] - value
2070 static gpg_error_t
2071 attribute_set (struct client_s *client, char **args)
2073 struct xml_request_s *req;
2074 gpg_error_t rc;
2075 xmlNodePtr n;
2077 if (!args || !args[0] || !args[1])
2078 return GPG_ERR_SYNTAX;
2081 * Reserved attribute names.
2083 if (!strcmp (args[0], "_name")) // Use the RENAME command instead
2084 return GPG_ERR_INV_ATTR;
2085 else if (!strcmp (args[0], "target"))
2086 return set_target_attribute (client, args + 1);
2087 else if (!xml_valid_attribute (args[0]))
2088 return GPG_ERR_INV_VALUE;
2090 if (!xml_valid_attribute_value (args[2]))
2091 return GPG_ERR_INV_VALUE;
2093 if (!xml_reserved_attribute (args[0]))
2095 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
2096 if (!rc)
2098 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
2099 if (!rc)
2101 rc = xml_check_recursion (client, req);
2102 xml_free_request (req);
2106 else
2108 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
2109 if (rc)
2110 return rc;
2112 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2113 xml_free_request (req);
2116 if (!rc)
2117 rc = xml_is_element_owner (client, n);
2119 if (!rc)
2120 rc = xml_add_attribute (client, n, args[0], args[2]);
2122 return rc;
2126 * req[0] - command
2127 * req[1] - attribute name or element path if command is LIST
2128 * req[2] - element path
2129 * req[2] - element path or value
2132 static gpg_error_t
2133 do_attr (assuan_context_t ctx, char *line)
2135 struct client_s *client = assuan_get_pointer (ctx);
2136 gpg_error_t rc = 0;
2137 char **req;
2139 req = str_split (line, " ", 4);
2140 if (!req || !req[0] || !req[1])
2142 strv_free (req);
2143 return GPG_ERR_SYNTAX;
2146 pthread_cleanup_push ((void *)req_free, req);
2148 if (strcasecmp (req[0], "SET") == 0)
2149 rc = attribute_set (client, req + 1);
2150 else if (strcasecmp (req[0], "GET") == 0)
2151 rc = attribute_get (ctx, req + 1);
2152 else if (strcasecmp (req[0], "DELETE") == 0)
2153 rc = attribute_delete (client, req + 1);
2154 else if (strcasecmp (req[0], "LIST") == 0)
2155 rc = attribute_list (ctx, req + 1);
2156 else
2157 rc = GPG_ERR_SYNTAX;
2159 client->flags &= ~(FLAG_ACL_IGNORE|FLAG_ACL_ERROR);
2160 pthread_cleanup_pop (1);
2161 return rc;
2164 static gpg_error_t
2165 attr_command (assuan_context_t ctx, char *line)
2167 struct client_s *client = assuan_get_pointer (ctx);
2168 gpg_error_t rc;
2169 struct argv_s *args[] = {
2170 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2171 NULL
2174 rc = parse_options (&line, args, client, 1);
2175 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2176 rc = GPG_ERR_SYNTAX;
2177 if (rc)
2178 return send_error (ctx, rc);
2180 if (client->opts & OPT_INQUIRE)
2182 unsigned char *result;
2183 size_t len;
2185 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2186 if (rc)
2187 return send_error (ctx, rc);
2189 pthread_cleanup_push ((void *)xfree, result);
2190 rc = do_attr (ctx, (char *)result);
2191 pthread_cleanup_pop (1);
2193 else
2194 rc = do_attr (ctx, line);
2196 return send_error (ctx, rc);
2199 static gpg_error_t
2200 parse_iscached_opt_lock (void *data, void *value)
2202 struct client_s *client = data;
2204 (void) value;
2205 client->opts |= OPT_LOCK;
2206 return 0;
2209 static gpg_error_t
2210 parse_iscached_opt_agent (void *data, void *value)
2212 struct client_s *client = data;
2214 (void) value;
2215 client->opts |= OPT_CACHE_AGENT;
2216 return 0;
2219 static gpg_error_t
2220 parse_iscached_opt_sign (void *data, void *value)
2222 struct client_s *client = data;
2224 (void) value;
2225 client->opts |= OPT_CACHE_SIGN;
2226 return 0;
2229 static gpg_error_t
2230 iscached_command (assuan_context_t ctx, char *line)
2232 struct client_s *client = assuan_get_pointer (ctx);
2233 gpg_error_t rc;
2234 int defer = 0;
2235 struct argv_s *args[] = {
2236 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2237 &(struct argv_s) {"agent", OPTION_TYPE_NOARG, parse_iscached_opt_agent},
2238 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_iscached_opt_sign},
2239 NULL
2242 if (!line || !*line)
2243 return send_error (ctx, GPG_ERR_SYNTAX);
2245 rc = parse_options (&line, args, client, 1);
2246 if (rc)
2247 return send_error (ctx, rc);
2248 else if (!valid_filename (line))
2249 return send_error (ctx, GPG_ERR_INV_VALUE);
2251 if (!(client->flags & FLAG_OPEN)
2252 && ((client->opts & OPT_CACHE_AGENT) || (client->opts & OPT_CACHE_SIGN)))
2253 return send_error (ctx, GPG_ERR_INV_STATE);
2255 rc = cache_iscached (line, &defer, (client->opts & OPT_CACHE_AGENT),
2256 (client->opts & OPT_CACHE_SIGN));
2257 if (!rc && defer)
2258 rc = GPG_ERR_NO_DATA;
2260 if (!rc)
2262 struct cache_data_s *cdata = cache_get_data (line);
2264 if (cdata)
2266 rc = validate_checksum (client, line, cdata, NULL, NULL);
2267 if (rc == GPG_ERR_CHECKSUM)
2268 rc = GPG_ERR_NO_DATA;
2272 if (client->opts & OPT_LOCK
2273 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2275 gpg_error_t trc = rc;
2277 if (strcmp (line, client->filename))
2278 reset_client (client);
2280 xfree (client->filename);
2281 client->filename = str_dup (line);
2282 rc = do_lock (client, 1);
2283 if (!rc)
2284 rc = trc;
2287 return send_error (ctx, rc);
2290 static gpg_error_t
2291 clearcache_command (assuan_context_t ctx, char *line)
2293 struct client_s *client = assuan_get_pointer (ctx);
2294 gpg_error_t rc = 0, all_rc = 0;
2295 int i;
2296 int t;
2297 int all = 0;
2298 struct client_thread_s *once = NULL;
2299 unsigned count;
2301 cache_lock ();
2302 pthread_cleanup_push (cache_release_mutex, NULL);
2303 count = cache_file_count ();
2304 MUTEX_LOCK (&cn_mutex);
2305 pthread_cleanup_push ((void *)release_mutex_cb, &cn_mutex);
2307 if (!line || !*line)
2308 all = 1;
2310 t = slist_length (cn_thread_list);
2312 for (i = 0; i < t; i++)
2314 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2315 assuan_peercred_t peer;
2317 if (!thd->cl)
2318 continue;
2320 /* Lock each connected clients' file mutex to prevent any other client
2321 * from accessing the cache entry (the file mutex is locked upon
2322 * command startup). The cache for the entry is not cleared if the
2323 * file mutex is locked by another client to prevent this function
2324 * from blocking. Rather, it returns an error.
2326 if (all)
2328 if (thd->cl->filename)
2330 rc = do_validate_peer (ctx, thd->cl->filename, &peer);
2331 /* The current client doesn't have permission to open the other
2332 * filename do to "access" configuration parameter in a filename
2333 * section. Since we are clearning all cache entries the error
2334 * will be returned later during cache_clear(). */
2335 if (rc)
2337 rc = 0;
2338 continue;
2341 else // Idle client without opened file?
2342 continue;
2344 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->filename, -1, 0, -1);
2345 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2347 /* The current client owns the lock. */
2348 if (pthread_equal (pthread_self (), thd->tid))
2349 rc = 0;
2350 else
2352 if (cache_iscached (thd->cl->filename, NULL, 0, 0)
2353 == GPG_ERR_NO_DATA)
2355 rc = 0;
2356 continue;
2359 /* The cache entry will be cleared when the other client
2360 * disconnects and cache_timer_thread() does its thing. */
2361 cache_defer_clear (thd->cl->filename);
2364 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2366 rc = 0;
2367 continue;
2370 if (!rc)
2372 rc = cache_clear (NULL, thd->cl->filename, 1);
2373 cache_unlock_mutex (thd->cl->filename, 0);
2376 if (rc)
2377 all_rc = rc;
2379 rc = 0;
2381 else
2383 /* A single data filename was specified. Lock only this data file
2384 * mutex and free the cache entry. */
2385 rc = do_validate_peer (ctx, line, &peer);
2386 if (rc == GPG_ERR_FORBIDDEN)
2387 rc = peer_is_invoker (client);
2389 if (rc == GPG_ERR_EACCES)
2390 rc = GPG_ERR_FORBIDDEN;
2392 if (!rc && thd->cl->filename && !strcmp (thd->cl->filename , line))
2394 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->filename, -1, 0, -1);
2395 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2397 /* The current client owns the lock. */
2398 if (pthread_equal (pthread_self (), thd->tid))
2399 rc = 0;
2402 if (!rc)
2404 once = thd;
2405 rc = cache_clear (NULL, thd->cl->filename, 1);
2406 cache_unlock_mutex (thd->cl->filename, 0);
2408 else
2409 cache_defer_clear (thd->cl->filename);
2411 /* Found a client with the opened file. The cache entry has been
2412 * either cleared (self) or defered to be cleared. */
2413 break;
2418 /* Only connected clients' cache entries have been cleared. Now clear any
2419 * remaining cache entries without clients but only if there wasn't an
2420 * error from above since this would defeat the locking check of the
2421 * remaining entries. */
2422 if (all && !all_rc && cache_file_count ())
2424 rc = cache_clear (client, NULL, 1);
2425 if (rc == GPG_ERR_EACCES)
2426 rc = GPG_ERR_FORBIDDEN;
2429 /* No clients are using the specified file. */
2430 else if (!all_rc && !rc && !once)
2431 rc = cache_clear (NULL, line, 1);
2433 /* Release the connection mutex. */
2434 pthread_cleanup_pop (1);
2436 if (!rc || (cache_file_count () && count != cache_file_count ()))
2437 send_status_all (STATUS_CACHE, NULL);
2439 /* Release the cache mutex. */
2440 pthread_cleanup_pop (1);
2442 /* One or more files were locked while clearing all cache entries. */
2443 if (all_rc)
2444 rc = all_rc;
2446 return send_error (ctx, rc);
2449 static gpg_error_t
2450 cachetimeout_command (assuan_context_t ctx, char *line)
2452 struct client_s *client = assuan_get_pointer (ctx);
2453 int timeout;
2454 char **req = str_split (line, " ", 0);
2455 char *p;
2456 gpg_error_t rc = 0;
2457 assuan_peercred_t peer;
2459 if (!req || !*req || !req[1])
2461 strv_free (req);
2462 return send_error (ctx, GPG_ERR_SYNTAX);
2465 errno = 0;
2466 timeout = (int) strtol (req[1], &p, 10);
2467 if (errno != 0 || *p || timeout < -1)
2469 strv_free (req);
2470 return send_error (ctx, GPG_ERR_SYNTAX);
2473 rc = do_validate_peer (ctx, req[0], &peer);
2474 if (rc == GPG_ERR_FORBIDDEN)
2476 rc = peer_is_invoker (client);
2477 if (rc == GPG_ERR_EACCES)
2478 rc = GPG_ERR_FORBIDDEN;
2481 if (!rc)
2483 rc = cache_set_timeout (req[0], timeout);
2484 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2486 rc = 0;
2487 MUTEX_LOCK (&rcfile_mutex);
2488 config_set_int_param (&global_config, req[0], "cache_timeout",
2489 req[1]);
2490 MUTEX_UNLOCK (&rcfile_mutex);
2494 strv_free (req);
2495 return send_error (ctx, rc);
2498 static gpg_error_t
2499 dump_command (assuan_context_t ctx, char *line)
2501 xmlChar *xml;
2502 int len;
2503 struct client_s *client = assuan_get_pointer (ctx);
2504 gpg_error_t rc;
2506 if (disable_list_and_dump == 1)
2507 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2509 if (line && *line)
2510 return send_error (ctx, GPG_ERR_SYNTAX);
2512 rc = peer_is_invoker(client);
2513 if (rc)
2514 return send_error (ctx, rc);
2516 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2518 if (!xml)
2520 log_write ("%s(%i): %s", __FILE__, __LINE__,
2521 pwmd_strerror (GPG_ERR_ENOMEM));
2522 return send_error (ctx, GPG_ERR_ENOMEM);
2525 pthread_cleanup_push ((void *)xmlFree, xml);
2526 rc = xfer_data (ctx, (char *) xml, len);
2527 pthread_cleanup_pop (1);
2528 return send_error (ctx, rc);
2531 static gpg_error_t
2532 getconfig_command (assuan_context_t ctx, char *line)
2534 struct client_s *client = assuan_get_pointer (ctx);
2535 gpg_error_t rc = 0;
2536 char filename[255] = { 0 }, param[747] = { 0 };
2537 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2539 if (!line || !*line)
2540 return send_error (ctx, GPG_ERR_SYNTAX);
2542 if (strchr (line, ' '))
2544 sscanf (line, " %254[^ ] %746c", filename, param);
2545 paramp = param;
2546 fp = filename;
2549 if (fp && !valid_filename (fp))
2550 return send_error (ctx, GPG_ERR_INV_VALUE);
2552 paramp = str_down (paramp);
2553 if (!strcmp (paramp, "passphrase"))
2554 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2556 p = config_get_value (fp ? fp : "global", paramp);
2557 if (!p)
2558 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2560 tmp = expand_homedir (p);
2561 xfree (p);
2562 if (!tmp)
2564 log_write ("%s(%i): %s", __FILE__, __LINE__,
2565 pwmd_strerror (GPG_ERR_ENOMEM));
2566 return send_error (ctx, GPG_ERR_ENOMEM);
2569 p = tmp;
2570 pthread_cleanup_push ((void *)xfree, p);
2571 rc = xfer_data (ctx, p, strlen (p));
2572 pthread_cleanup_pop (1);
2573 return send_error (ctx, rc);
2576 struct xpath_s
2578 xmlXPathContextPtr xp;
2579 xmlXPathObjectPtr result;
2580 xmlBufferPtr buf;
2581 char **req;
2584 static void
2585 xpath_command_free (void *arg)
2587 struct xpath_s *xpath = arg;
2589 if (!xpath)
2590 return;
2592 req_free (xpath->req);
2594 if (xpath->buf)
2595 xmlBufferFree (xpath->buf);
2597 if (xpath->result)
2598 xmlXPathFreeObject (xpath->result);
2600 if (xpath->xp)
2601 xmlXPathFreeContext (xpath->xp);
2604 static gpg_error_t
2605 do_xpath (assuan_context_t ctx, char *line)
2607 gpg_error_t rc;
2608 struct client_s *client = assuan_get_pointer (ctx);
2609 struct xpath_s _x = { 0 };
2610 struct xpath_s *xpath = &_x;
2612 if (!line || !*line)
2613 return GPG_ERR_SYNTAX;
2615 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2617 if (strv_printf (&xpath->req, "%s", line) == 0)
2618 return GPG_ERR_ENOMEM;
2621 xpath->xp = xmlXPathNewContext (client->doc);
2622 if (!xpath->xp)
2624 rc = GPG_ERR_BAD_DATA;
2625 goto fail;
2628 xpath->result =
2629 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2630 if (!xpath->result)
2632 rc = GPG_ERR_BAD_DATA;
2633 goto fail;
2636 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2638 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2639 goto fail;
2642 rc = xml_recurse_xpath_nodeset (client, client->doc,
2643 xpath->result->nodesetval,
2644 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2645 NULL);
2646 if (rc)
2647 goto fail;
2648 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2650 rc = GPG_ERR_NO_DATA;
2651 goto fail;
2653 else if (xpath->req[1])
2655 rc = 0;
2656 goto fail;
2659 pthread_cleanup_push ((void *)xpath_command_free, &xpath);
2660 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2661 xmlBufferLength (xpath->buf));
2662 pthread_cleanup_pop (0);
2663 fail:
2664 xpath_command_free (xpath);
2665 return rc;
2668 static gpg_error_t
2669 xpath_command (assuan_context_t ctx, char *line)
2671 struct client_s *client = assuan_get_pointer (ctx);
2672 gpg_error_t rc;
2673 struct argv_s *args[] = {
2674 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2675 NULL
2678 if (disable_list_and_dump == 1)
2679 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2681 rc = peer_is_invoker(client);
2682 if (rc)
2683 return send_error (ctx, rc);
2685 rc = parse_options (&line, args, client, 1);
2686 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2687 rc = GPG_ERR_SYNTAX;
2688 if (rc)
2689 return send_error (ctx, rc);
2691 if (client->opts & OPT_INQUIRE)
2693 unsigned char *result;
2694 size_t len;
2696 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2697 if (rc)
2698 return send_error (ctx, rc);
2700 pthread_cleanup_push ((void *)xfree, result);
2701 rc = do_xpath (ctx, (char *)result);
2702 pthread_cleanup_pop (1);
2704 else
2705 rc = do_xpath (ctx, line);
2707 return send_error (ctx, rc);
2710 static gpg_error_t
2711 do_xpathattr (assuan_context_t ctx, char *line)
2713 struct client_s *client = assuan_get_pointer (ctx);
2714 gpg_error_t rc;
2715 char **req = NULL;
2716 int cmd = 0; //SET
2717 struct xpath_s _x = { 0 };
2718 struct xpath_s *xpath = &_x;
2720 if (!line || !*line)
2721 return GPG_ERR_SYNTAX;
2723 if ((req = str_split (line, " ", 3)) == NULL)
2724 return GPG_ERR_ENOMEM;
2726 if (!req[0])
2728 rc = GPG_ERR_SYNTAX;
2729 goto fail;
2732 if (!strcasecmp (req[0], "SET"))
2733 cmd = 0;
2734 else if (!strcasecmp (req[0], "DELETE"))
2735 cmd = 1;
2736 else
2738 rc = GPG_ERR_SYNTAX;
2739 goto fail;
2742 if (!req[1] || !req[2])
2744 rc = GPG_ERR_SYNTAX;
2745 goto fail;
2748 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2750 rc = GPG_ERR_ENOMEM;
2751 goto fail;
2754 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2756 rc = GPG_ERR_SYNTAX;
2757 goto fail;
2760 xpath->xp = xmlXPathNewContext (client->doc);
2761 if (!xpath->xp)
2763 rc = GPG_ERR_BAD_DATA;
2764 goto fail;
2767 xpath->result = xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2768 if (!xpath->result)
2770 rc = GPG_ERR_BAD_DATA;
2771 goto fail;
2774 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2776 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2777 goto fail;
2780 rc = xml_recurse_xpath_nodeset (client, client->doc,
2781 xpath->result->nodesetval,
2782 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2783 (xmlChar *) req[1]);
2785 fail:
2786 xpath_command_free (xpath);
2787 strv_free (req);
2788 return rc;
2791 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2792 static gpg_error_t
2793 xpathattr_command (assuan_context_t ctx, char *line)
2795 struct client_s *client = assuan_get_pointer (ctx);
2796 gpg_error_t rc;
2797 struct argv_s *args[] = {
2798 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2799 NULL
2802 if (disable_list_and_dump == 1)
2803 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2805 rc = peer_is_invoker(client);
2806 if (rc)
2807 return send_error (ctx, rc);
2809 rc = parse_options (&line, args, client, 1);
2810 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2811 rc = GPG_ERR_SYNTAX;
2812 if (rc)
2813 return send_error (ctx, rc);
2815 if (client->opts & OPT_INQUIRE)
2817 unsigned char *result;
2818 size_t len;
2820 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2821 if (rc)
2822 return send_error (ctx, rc);
2824 pthread_cleanup_push ((void *)xfree, result);
2825 rc = do_xpathattr (ctx, (char *)result);
2826 pthread_cleanup_pop (1);
2828 else
2829 rc = do_xpathattr (ctx, line);
2831 return send_error (ctx, rc);
2834 static gpg_error_t
2835 do_import (struct client_s *client, const char *root_element,
2836 unsigned char *content)
2838 xmlDocPtr doc = NULL;
2839 xmlNodePtr n = NULL, root;
2840 gpg_error_t rc;
2841 struct string_s *str = NULL;
2842 struct xml_request_s *req = NULL;
2844 if (!content || !*content)
2845 return GPG_ERR_SYNTAX;
2847 if (root_element)
2849 rc = xml_new_request (client, root_element, XML_CMD_STORE, &req);
2850 if (rc)
2852 xfree (content);
2853 return rc;
2856 if (!xml_valid_element_path (req->args, 0))
2858 xml_free_request (req);
2859 xfree (content);
2860 return GPG_ERR_INV_VALUE;
2864 str = string_new_content ((char *)content);
2865 str = string_prepend (str, "<pwmd>");
2866 str = string_append (str, "</pwmd>");
2867 doc = xmlReadDoc ((xmlChar *) str->str, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2868 string_free (str, 1);
2869 if (!doc)
2871 rc = GPG_ERR_BAD_DATA;
2872 goto fail;
2875 root = xmlDocGetRootElement (doc);
2876 root = root->children;
2877 if (root->type != XML_ELEMENT_NODE)
2879 rc = GPG_ERR_BAD_DATA;
2880 goto fail;
2883 rc = xml_validate_import (client, root);
2884 if (rc)
2885 goto fail;
2887 if (req)
2889 /* Create the new specified root element path, if needed. */
2890 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc),
2891 &rc);
2892 if (rc)
2893 goto fail;
2895 /* Overwrite the children of the specified root element path. */
2896 (void)xml_unlink_node (client, n->children);
2897 n->children = NULL;
2899 else
2900 n = xmlDocGetRootElement (client->doc);
2902 if (n)
2904 xmlNodePtr copy;
2905 xmlChar *name = xml_attribute_value (root, (xmlChar *)"_name");
2907 if (!name)
2908 rc = GPG_ERR_BAD_DATA;
2909 else if (!req)
2911 /* No --root argument passed. Overwrite the current documents' root
2912 * element matching the root element of the imported data. */
2913 xml_free_request (req);
2914 req = NULL;
2915 rc = xml_new_request (client, (char *)name, XML_CMD_DELETE, &req);
2916 xmlFree (name);
2917 if (!rc)
2919 xmlNodePtr tmp;
2921 tmp = xml_find_elements (client, req,
2922 xmlDocGetRootElement (req->doc), &rc);
2923 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2924 goto fail;
2926 if (!rc)
2927 (void)xml_unlink_node (client, tmp);
2929 rc = 0;
2933 if (!rc)
2935 copy = xmlCopyNodeList (root);
2936 if (!copy)
2937 rc = GPG_ERR_ENOMEM;
2938 else
2940 n = xmlAddChildList (n, copy);
2941 if (!n)
2942 rc = GPG_ERR_ENOMEM;
2947 if (!rc && n && n->parent)
2948 rc = xml_update_element_mtime (client, n->parent);
2950 fail:
2951 xml_free_request (req);
2953 if (doc)
2954 xmlFreeDoc (doc);
2956 return rc;
2959 static gpg_error_t
2960 parse_import_opt_root (void *data, void *value)
2962 struct client_s *client = data;
2964 client->import_root = str_dup (value);
2965 return client->import_root ? 0 : GPG_ERR_ENOMEM;
2968 static gpg_error_t
2969 import_command (assuan_context_t ctx, char *line)
2971 gpg_error_t rc;
2972 struct client_s *client = assuan_get_pointer (ctx);
2973 unsigned char *result;
2974 size_t len;
2975 struct argv_s *args[] = {
2976 &(struct argv_s) {"root", OPTION_TYPE_ARG, parse_import_opt_root},
2977 NULL
2980 xfree (client->import_root);
2981 client->import_root = NULL;
2982 rc = parse_options (&line, args, client, 0);
2983 if (rc)
2984 return send_error (ctx, rc);
2986 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2987 if (rc)
2989 xfree (client->import_root);
2990 client->import_root = NULL;
2991 return send_error (ctx, rc);
2994 rc = do_import (client, client->import_root, result);
2995 xfree (client->import_root);
2996 client->import_root = NULL;
2997 return send_error (ctx, rc);
3000 static gpg_error_t
3001 do_lock (struct client_s *client, int add)
3003 gpg_error_t rc = lock_file_mutex (client, add);
3005 if (!rc)
3006 client->flags |= FLAG_LOCK_CMD;
3008 return rc;
3011 static gpg_error_t
3012 lock_command (assuan_context_t ctx, char *line)
3014 struct client_s *client = assuan_get_pointer (ctx);
3015 gpg_error_t rc;
3017 if (line && *line)
3018 return send_error (ctx, GPG_ERR_SYNTAX);
3020 rc = do_lock (client, 0);
3021 return send_error (ctx, rc);
3024 static gpg_error_t
3025 unlock_command (assuan_context_t ctx, char *line)
3027 struct client_s *client = assuan_get_pointer (ctx);
3028 gpg_error_t rc;
3030 if (line && *line)
3031 return send_error (ctx, GPG_ERR_SYNTAX);
3033 rc = unlock_file_mutex (client, 0);
3034 return send_error (ctx, rc);
3037 static void
3038 get_set_env (const char *name, const char *value)
3040 if (value && *value)
3041 setenv (name, value, 1);
3042 else
3043 unsetenv (name);
3046 static gpg_error_t
3047 option_command (assuan_context_t ctx, char *line)
3049 struct client_s *client = assuan_get_pointer (ctx);
3050 gpg_error_t rc = 0;
3051 char namebuf[255] = { 0 };
3052 char *name = namebuf;
3053 char *value = NULL, *p, *tmp = NULL;
3055 p = strchr (line, '=');
3056 if (!p)
3058 strncpy (namebuf, line, sizeof(namebuf));
3059 namebuf[sizeof(namebuf)-1] = 0;
3061 else
3063 strncpy (namebuf, line, strlen (line)-strlen (p));
3064 namebuf[sizeof(namebuf)-1] = 0;
3065 value = p+1;
3068 log_write2 ("OPTION name='%s' value='%s'", name, value);
3070 if (strcasecmp (name, (char *) "lock-timeout") == 0)
3072 long n = 0;
3074 if (value)
3076 n = strtol (value, &tmp, 10);
3077 if (tmp && *tmp)
3078 return send_error (ctx, GPG_ERR_INV_VALUE);
3081 client->lock_timeout = n;
3083 else if (strcasecmp (name, (char *) "client-state") == 0)
3085 long n = 0;
3087 if (value)
3089 n = strtol (value, &tmp, 10);
3090 if ((tmp && *tmp) || (n < 0 || n > 1))
3091 return send_error (ctx, GPG_ERR_INV_VALUE);
3092 client->client_state = n;
3094 else
3095 client->client_state = 0;
3097 else if (strcasecmp (name, (char *) "NAME") == 0)
3099 if (value && strchr (value, ' '))
3100 rc = GPG_ERR_INV_VALUE;
3101 else
3103 tmp = pthread_getspecific (thread_name_key);
3104 pthread_setspecific (thread_name_key, NULL);
3105 xfree (tmp);
3106 MUTEX_LOCK (&cn_mutex);
3108 if (value && *value)
3109 pthread_setspecific (thread_name_key, str_dup (value));
3111 MUTEX_UNLOCK (&cn_mutex);
3114 else if (strcasecmp (name, "disable-pinentry") == 0)
3116 int n = 1;
3118 if (value && *value)
3120 n = (int) strtol (value, &tmp, 10);
3121 if (*tmp || n < 0 || n > 1)
3122 return send_error (ctx, GPG_ERR_INV_VALUE);
3125 if (n)
3126 client->flags |= FLAG_NO_PINENTRY;
3127 else
3128 client->flags &= ~FLAG_NO_PINENTRY;
3130 else if (strcasecmp (name, "ttyname") == 0)
3132 get_set_env ("GPG_TTY", value);
3134 else if (strcasecmp (name, "ttytype") == 0)
3136 get_set_env ("TERM", value);
3138 else if (strcasecmp (name, "display") == 0)
3140 get_set_env ("DISPLAY", value);
3142 else if (strcasecmp (name, "lc_messages") == 0)
3145 else if (strcasecmp (name, "lc_ctype") == 0)
3148 else if (strcasecmp (name, "desc") == 0)
3151 else if (strcasecmp (name, "pinentry-timeout") == 0)
3154 else
3155 rc = GPG_ERR_UNKNOWN_OPTION;
3157 return send_error (ctx, rc);
3160 static gpg_error_t
3161 do_rename (assuan_context_t ctx, char *line)
3163 struct client_s *client = assuan_get_pointer (ctx);
3164 char **args, *p;
3165 xmlNodePtr src, dst;
3166 struct string_s *str = NULL, *tstr;
3167 struct xml_request_s *req;
3168 gpg_error_t rc;
3170 args = str_split (line, " ", 0);
3171 if (!args || strv_length (args) != 2)
3173 strv_free (args);
3174 return GPG_ERR_SYNTAX;
3177 if (!xml_valid_element ((xmlChar *) args[1]))
3179 strv_free (args);
3180 return GPG_ERR_INV_VALUE;
3183 rc = xml_new_request (client, args[0], XML_CMD_RENAME, &req);
3184 if (rc)
3186 strv_free (args);
3187 return rc;
3190 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3191 if (rc)
3192 goto fail;
3194 rc = xml_is_element_owner (client, src);
3195 if (rc)
3196 goto fail;
3198 xmlChar *a = xmlGetProp (src, (xmlChar *) "_name");
3199 if (!a)
3201 rc = GPG_ERR_ENOMEM;
3202 goto fail;
3205 /* To prevent unwanted effects:
3207 * <element _name="a"><element _name="b"/></element>
3209 * RENAME a<TAB>b b
3211 if (xmlStrEqual (a, (xmlChar *) args[1]))
3213 xmlFree (a);
3214 rc = GPG_ERR_EEXIST;
3215 goto fail;
3218 xmlFree (a);
3219 str = string_new (args[0]);
3220 if (!str)
3222 rc = GPG_ERR_ENOMEM;
3223 goto fail;
3226 p = strrchr (str->str, '\t');
3227 if (p)
3228 tstr = string_truncate (str, strlen (str->str)-strlen (p));
3229 else
3230 tstr = string_truncate (str, 0);
3232 if (!tstr)
3234 string_free (str, 1);
3235 rc = GPG_ERR_ENOMEM;
3236 goto fail;
3239 str = tstr;
3240 tstr = string_append_printf (str, "%s%s", str->len ? "\t" : "", args[1]);
3241 if (!tstr)
3243 string_free (str, 1);
3244 rc = GPG_ERR_ENOMEM;
3245 goto fail;
3248 str = tstr;
3249 xml_free_request (req);
3250 rc = xml_new_request (client, str->str, XML_CMD_RENAME, &req);
3251 string_free (str, 1);
3252 if (rc)
3254 req = NULL;
3255 goto fail;
3258 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3259 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3260 goto fail;
3262 rc = 0;
3264 /* Target may exist:
3266 * <element _name="a"/>
3267 * <element _name="b" target="a"/>
3269 * RENAME b a
3271 if (src == dst)
3273 rc = GPG_ERR_EEXIST;
3274 goto fail;
3277 if (dst)
3279 rc = xml_is_element_owner (client, dst);
3280 if (rc)
3281 goto fail;
3283 rc = xml_add_attribute (client, src, "_name", args[1]);
3284 if (!rc)
3285 xml_unlink_node (client, dst);
3287 else
3288 rc = xml_add_attribute (client, src, "_name", args[1]);
3290 fail:
3291 xml_free_request (req);
3292 strv_free (args);
3293 return rc;
3296 static gpg_error_t
3297 rename_command (assuan_context_t ctx, char *line)
3299 struct client_s *client = assuan_get_pointer (ctx);
3300 gpg_error_t rc;
3301 struct argv_s *args[] = {
3302 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3303 NULL
3306 rc = parse_options (&line, args, client, 1);
3307 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3308 rc = GPG_ERR_SYNTAX;
3309 if (rc)
3310 return send_error (ctx, rc);
3312 if (client->opts & OPT_INQUIRE)
3314 unsigned char *result;
3315 size_t len;
3317 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3318 if (rc)
3319 return send_error (ctx, rc);
3321 pthread_cleanup_push ((void *)xfree, result);
3322 rc = do_rename (ctx, (char *)result);
3323 pthread_cleanup_pop (1);
3325 else
3326 rc = do_rename (ctx, line);
3328 return send_error (ctx, rc);
3331 static gpg_error_t
3332 do_copy (assuan_context_t ctx, char *line)
3334 struct client_s *client = assuan_get_pointer (ctx);
3335 char **args, **targs;
3336 xmlNodePtr src, dst, tree = NULL;
3337 struct xml_request_s *req;
3338 gpg_error_t rc;
3340 args = str_split (line, " ", 0);
3341 if (!args || strv_length (args) != 2)
3343 strv_free (args);
3344 return GPG_ERR_SYNTAX;
3347 targs = str_split (args[1], "\t", 0);
3348 if (!targs)
3350 strv_free (args);
3351 return GPG_ERR_SYNTAX;
3354 if (!xml_valid_element_path (targs, 0))
3356 strv_free (args);
3357 strv_free (targs);
3358 return GPG_ERR_INV_VALUE;
3360 strv_free (targs);
3362 rc = xml_new_request (client, args[0], XML_CMD_NONE, &req);
3363 if (rc)
3365 strv_free (args);
3366 return rc;
3369 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3370 if (rc)
3371 goto fail;
3373 tree = xmlCopyNodeList (src);
3374 if (!tree)
3376 rc = GPG_ERR_ENOMEM;
3377 goto fail;
3380 xml_free_request (req);
3381 /* Create the destination element path if it does not exist. */
3382 rc = xml_new_request (client, args[1], XML_CMD_STORE, &req);
3383 if (rc)
3385 req = NULL;
3386 goto fail;
3389 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3390 if (rc)
3391 goto fail;
3393 rc = xml_is_element_owner (client, dst);
3394 if (rc)
3395 goto fail;
3397 rc = xml_validate_target (client, req->doc, args[1], src);
3398 if (rc || src == dst)
3400 rc = rc ? rc : GPG_ERR_EEXIST;
3401 goto fail;
3404 /* Merge any attributes from the src node to the initial dst node. */
3405 for (xmlAttrPtr attr = tree->properties; attr; attr = attr->next)
3407 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3408 continue;
3410 xmlAttrPtr a = xmlHasProp (dst, attr->name);
3411 if (a)
3412 xmlRemoveProp (a);
3414 xmlChar *tmp = xmlNodeGetContent (attr->children);
3415 xmlNewProp (dst, attr->name, tmp);
3416 xmlFree (tmp);
3417 /* Create the default attributes. */
3418 rc = xml_add_attribute (client, dst, NULL, NULL);
3421 xmlNodePtr n = dst->children;
3422 (void)xml_unlink_node (client, n);
3423 dst->children = NULL;
3425 if (tree->children)
3427 n = xmlCopyNodeList (tree->children);
3428 if (!n)
3430 rc = GPG_ERR_ENOMEM;
3431 goto fail;
3434 n = xmlAddChildList (dst, n);
3435 if (!n)
3437 rc = GPG_ERR_ENOMEM;
3438 goto fail;
3441 rc = xml_update_element_mtime (client, xmlDocGetRootElement (client->doc)
3442 == dst->parent ? dst : dst->parent);
3445 fail:
3446 if (tree)
3447 (void)xml_unlink_node (client, tree);
3449 xml_free_request (req);
3450 strv_free (args);
3451 return rc;
3454 static gpg_error_t
3455 copy_command (assuan_context_t ctx, char *line)
3457 struct client_s *client = assuan_get_pointer (ctx);
3458 gpg_error_t rc;
3459 struct argv_s *args[] = {
3460 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3461 NULL
3464 rc = parse_options (&line, args, client, 1);
3465 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3466 rc = GPG_ERR_SYNTAX;
3467 if (rc)
3468 return send_error (ctx, rc);
3470 if (client->opts & OPT_INQUIRE)
3472 unsigned char *result;
3473 size_t len;
3475 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3476 if (rc)
3477 return send_error (ctx, rc);
3479 pthread_cleanup_push ((void *)xfree, result);
3480 rc = do_copy (ctx, (char *)result);
3481 pthread_cleanup_pop (1);
3483 else
3484 rc = do_copy (ctx, line);
3486 return send_error (ctx, rc);
3489 static gpg_error_t
3490 do_move (assuan_context_t ctx, char *line)
3492 struct client_s *client = assuan_get_pointer (ctx);
3493 char **args;
3494 xmlNodePtr src, dst;
3495 struct xml_request_s *req;
3496 gpg_error_t rc;
3498 args = str_split (line, " ", 0);
3499 if (!args || strv_length (args) != 2)
3501 strv_free (args);
3502 return GPG_ERR_SYNTAX;
3505 if (*args[1])
3507 char **targs = str_split (args[1], "\t", 0);
3508 if (!targs)
3510 strv_free (args);
3511 return GPG_ERR_ENOMEM;
3514 if (!xml_valid_element_path (targs, 0))
3516 strv_free (targs);
3517 strv_free (args);
3518 return GPG_ERR_INV_VALUE;
3521 strv_free (targs);
3524 rc = xml_new_request (client, args[0], XML_CMD_MOVE, &req);
3525 if (rc)
3527 strv_free (args);
3528 return rc;
3531 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3532 if (rc)
3533 goto fail;
3535 rc = xml_is_element_owner (client, src);
3536 if (rc)
3537 goto fail;
3539 xml_free_request (req);
3540 req = NULL;
3541 if (*args[1])
3543 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
3544 if (rc)
3545 goto fail;
3547 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc),
3548 &rc);
3550 else
3551 dst = xmlDocGetRootElement (client->doc);
3553 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3554 goto fail;
3556 rc = 0;
3558 for (xmlNodePtr n = dst; n; n = n->parent)
3560 if (n == src)
3562 rc = GPG_ERR_EEXIST;
3563 goto fail;
3567 if (dst)
3569 xmlChar *a = xml_attribute_value (src, (xmlChar *) "_name");
3570 xmlNodePtr dup = xml_find_element (client, dst->children, (char *) a, &rc);
3572 xmlFree (a);
3573 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3574 goto fail;
3576 rc = 0;
3578 if (dup)
3580 if (dup == src)
3582 rc = GPG_ERR_EEXIST;
3583 goto fail;
3586 if (dst == xmlDocGetRootElement (client->doc))
3588 xmlNodePtr n = src;
3589 int match = 0;
3591 while (n->parent && n->parent != dst)
3592 n = n->parent;
3594 xmlChar *a = xml_attribute_value (n, (xmlChar *) "_name");
3595 xmlChar *b = xml_attribute_value (src, (xmlChar *) "_name");
3597 if (xmlStrEqual (a, b))
3599 match = 1;
3600 xmlUnlinkNode (src);
3601 (void)xml_unlink_node (client, n);
3604 xmlFree (a);
3605 xmlFree (b);
3607 if (!match)
3608 (void)xml_unlink_node (client, dup);
3610 else
3611 xmlUnlinkNode (dup);
3615 if (!dst && *req->args)
3617 struct xml_request_s *nreq = NULL;
3618 xmlChar *name = xml_attribute_value (src, (xmlChar *) "_name");
3620 if (src->parent == xmlDocGetRootElement (client->doc)
3621 && !strcmp ((char *) name, *req->args))
3623 xmlFree (name);
3624 rc = GPG_ERR_EEXIST;
3625 goto fail;
3628 xmlFree (name);
3629 rc = xml_new_request (client, args[1], XML_CMD_STORE, &nreq);
3630 if (!rc)
3632 dst = xml_find_elements (client, nreq,
3633 xmlDocGetRootElement (nreq->doc), &rc);
3634 xml_free_request (nreq);
3637 if (rc)
3638 goto fail;
3641 xml_update_element_mtime (client, src->parent);
3642 xmlUnlinkNode (src);
3644 dst = xmlAddChildList (dst, src);
3645 if (!dst)
3646 rc = GPG_ERR_ENOMEM;
3647 else
3648 xml_update_element_mtime (client, dst->parent);
3650 fail:
3651 xml_free_request (req);
3652 strv_free (args);
3653 return rc;
3656 static gpg_error_t
3657 move_command (assuan_context_t ctx, char *line)
3659 struct client_s *client = assuan_get_pointer (ctx);
3660 gpg_error_t rc;
3661 struct argv_s *args[] = {
3662 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3663 NULL
3666 rc = parse_options (&line, args, client, 1);
3667 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3668 rc = GPG_ERR_SYNTAX;
3669 if (rc)
3670 return send_error (ctx, rc);
3672 if (client->opts & OPT_INQUIRE)
3674 unsigned char *result;
3675 size_t len;
3677 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3678 if (rc)
3679 return send_error (ctx, rc);
3681 pthread_cleanup_push ((void *)xfree, result);
3682 rc = do_move (ctx, (char *)result);
3683 pthread_cleanup_pop (1);
3685 else
3686 rc = do_move (ctx, line);
3688 return send_error (ctx, rc);
3691 static gpg_error_t
3692 ls_command (assuan_context_t ctx, char *line)
3694 gpg_error_t rc;
3695 char *tmp;
3696 char *dir;
3697 DIR *d;
3699 if (line && *line)
3700 return send_error (ctx, GPG_ERR_SYNTAX);
3702 tmp = str_asprintf ("%s/data", homedir);
3703 dir = expand_homedir (tmp);
3704 xfree (tmp);
3705 d = opendir (dir);
3706 rc = gpg_error_from_errno (errno);
3708 if (!d)
3710 xfree (dir);
3711 return send_error (ctx, rc);
3714 size_t len =
3715 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3716 struct dirent *p = xmalloc (len), *cur = NULL;
3717 char *list = NULL;
3719 xfree (dir);
3720 pthread_cleanup_push ((void *)xfree, p);
3721 pthread_cleanup_push ((void *)(void *)(void *)closedir, d);
3722 rc = 0;
3724 while (!readdir_r (d, p, &cur) && cur)
3726 struct stat st;
3728 if (stat (cur->d_name, &st) == -1 || !S_ISREG (st.st_mode))
3729 continue;
3731 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3733 if (!tmp)
3735 if (list)
3736 xfree (list);
3738 rc = GPG_ERR_ENOMEM;
3739 break;
3742 xfree (list);
3743 list = tmp;
3746 pthread_cleanup_pop (1); // closedir (d)
3747 pthread_cleanup_pop (1); // xfree (p)
3749 if (rc)
3750 return send_error (ctx, rc);
3752 if (!list)
3753 return send_error (ctx, GPG_ERR_NO_DATA);
3755 list[strlen (list) - 1] = 0;
3756 pthread_cleanup_push ((void *)xfree, list);
3757 rc = xfer_data (ctx, list, strlen (list));
3758 pthread_cleanup_pop (1);
3759 return send_error (ctx, rc);
3762 static gpg_error_t
3763 bye_notify (assuan_context_t ctx, char *line)
3765 struct client_s *cl = assuan_get_pointer (ctx);
3766 gpg_error_t ret = 0;
3768 update_client_state (cl, CLIENT_STATE_DISCON);
3770 #ifdef WITH_GNUTLS
3771 if (cl->thd->remote)
3773 int rc;
3777 struct timeval tv = { 0, 50000 };
3779 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3780 if (rc == GNUTLS_E_AGAIN)
3781 select (0, NULL, NULL, NULL, &tv);
3783 while (rc == GNUTLS_E_AGAIN);
3785 #endif
3787 /* This will let assuan_process_next() return. */
3788 if (fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK) == -1)
3790 cl->last_rc = gpg_error_from_errno (errno);
3791 ret = cl->last_rc;
3794 cl->last_rc = 0; // BYE command result
3795 return ret;
3798 static gpg_error_t
3799 reset_notify (assuan_context_t ctx, char *line)
3801 struct client_s *client = assuan_get_pointer (ctx);
3803 if (client)
3804 reset_client (client);
3806 return 0;
3810 * This is called before every Assuan command.
3812 static gpg_error_t
3813 command_startup (assuan_context_t ctx, const char *name)
3815 struct client_s *client = assuan_get_pointer (ctx);
3816 gpg_error_t rc;
3817 struct command_table_s *cmd = NULL;
3819 log_write2 ("command='%s'", name);
3820 client->last_rc = client->opts = 0;
3822 for (int i = 0; command_table[i]; i++)
3824 if (!strcasecmp (name, command_table[i]->name))
3826 if (command_table[i]->ignore_startup)
3827 return 0;
3828 cmd = command_table[i];
3829 break;
3833 if (!cmd)
3834 return GPG_ERR_UNKNOWN_COMMAND;
3836 client->last_rc = rc = gpg_error (file_modified (client, cmd));
3837 if (!rc)
3838 update_client_state (client, CLIENT_STATE_COMMAND);
3840 return rc;
3844 * This is called after every Assuan command.
3846 static void
3847 command_finalize (assuan_context_t ctx, gpg_error_t rc)
3849 struct client_s *client = assuan_get_pointer (ctx);
3851 if (!(client->flags & FLAG_LOCK_CMD))
3852 unlock_file_mutex (client, 0);
3854 unlock_flock (&client->flock_fd);
3855 log_write2 (_("command completed: rc=%u"), rc ? rc : client->last_rc);
3856 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
3857 #ifdef WITH_GNUTLS
3858 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
3859 #endif
3860 if (client->thd->state != CLIENT_STATE_DISCON)
3861 update_client_state (client, CLIENT_STATE_IDLE);
3864 static gpg_error_t
3865 parse_help_opt_html (void *data, void *value)
3867 struct client_s *client = data;
3869 (void) value;
3870 client->opts |= OPT_HTML;
3871 return 0;
3874 static gpg_error_t
3875 help_command (assuan_context_t ctx, char *line)
3877 gpg_error_t rc;
3878 int i;
3879 struct client_s *client = assuan_get_pointer (ctx);
3880 struct argv_s *args[] = {
3881 &(struct argv_s) {"html", OPTION_TYPE_NOARG, parse_help_opt_html},
3882 NULL
3885 rc = parse_options (&line, args, client, 1);
3886 if (rc)
3887 return send_error (ctx, rc);
3889 if (!line || !*line)
3891 char *tmp;
3892 char *help = str_dup (_("Usage: HELP [--html] [<COMMAND>]\n"
3893 "For commands that take an element path as an argument, each element is "
3894 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3895 "@*@*COMMANDS:"));
3897 for (i = 0; command_table[i]; i++)
3899 if (!command_table[i]->help)
3900 continue;
3902 /* @npxref{} won't put a "See " or "see " in front of the command.
3903 * It's not a texinfo command but needed for --html. */
3904 tmp = str_asprintf ("%s %s%s%s", help,
3905 client->opts & OPT_HTML ? "@npxref{" : "",
3906 command_table[i]->name,
3907 client->opts & OPT_HTML ? "}" : "");
3908 xfree (help);
3909 help = tmp;
3912 tmp = strip_texi_and_wrap (help, client->opts & OPT_HTML);
3913 xfree (help);
3914 pthread_cleanup_push ((void *)xfree, tmp);
3915 rc = xfer_data (ctx, tmp, strlen (tmp));
3916 pthread_cleanup_pop (1);
3917 return send_error (ctx, rc);
3920 for (i = 0; command_table[i]; i++)
3922 if (!strcasecmp (line, command_table[i]->name))
3924 char *help, *tmp;
3926 if (!command_table[i]->help)
3927 break;
3929 help = strip_texi_and_wrap (command_table[i]->help,
3930 client->opts & OPT_HTML);
3931 tmp = str_asprintf ("%s%s",
3932 (client->opts & OPT_HTML) ? "" : _("Usage: "),
3933 help);
3934 xfree (help);
3935 pthread_cleanup_push ((void *)xfree, tmp);
3936 rc = xfer_data (ctx, tmp, strlen (tmp));
3937 pthread_cleanup_pop (1);
3938 return send_error (ctx, rc);
3942 return send_error (ctx, GPG_ERR_INV_NAME);
3945 static void
3946 new_command (const char *name, int ignore, int unlock, int flock_type,
3947 gpg_error_t (*handler) (assuan_context_t, char *),
3948 const char *help)
3950 int i = 0;
3951 struct command_table_s **tmp;
3953 if (command_table)
3954 for (i = 0; command_table[i]; i++);
3956 tmp = xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
3957 assert (tmp);
3958 command_table = tmp;
3959 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
3960 command_table[i]->name = name;
3961 command_table[i]->handler = handler;
3962 command_table[i]->ignore_startup = ignore;
3963 command_table[i]->unlock = unlock;
3964 command_table[i]->flock_type = flock_type;
3965 command_table[i++]->help = help;
3966 command_table[i] = NULL;
3969 void
3970 deinit_commands ()
3972 int i;
3974 for (i = 0; command_table[i]; i++)
3975 xfree (command_table[i]);
3977 xfree (command_table);
3980 static int
3981 sort_commands (const void *arg1, const void *arg2)
3983 struct command_table_s *const *a = arg1;
3984 struct command_table_s *const *b = arg2;
3986 if (!*a || !*b)
3987 return 0;
3988 else if (*a && !*b)
3989 return 1;
3990 else if (!*a && *b)
3991 return -1;
3993 return strcmp ((*a)->name, (*b)->name);
3996 static gpg_error_t
3997 passwd_command (assuan_context_t ctx, char *line)
3999 struct client_s *client = assuan_get_pointer (ctx);
4000 gpg_error_t rc;
4002 rc = peer_is_invoker (client);
4003 if (rc == GPG_ERR_EACCES)
4004 return send_error (ctx, GPG_ERR_FORBIDDEN);
4005 else if (rc)
4006 return send_error (ctx, rc);
4008 if (client->flags & FLAG_NEW)
4009 return send_error (ctx, GPG_ERR_INV_STATE);
4011 client->crypto->keyfile = config_get_string (client->filename,
4012 "passphrase_file");
4013 rc = crypto_init_ctx (client->crypto, client->flags & FLAG_NO_PINENTRY,
4014 client->crypto->keyfile);
4015 if (rc)
4016 return send_error (ctx, rc);
4018 if (!rc)
4019 rc = crypto_passwd (client, client->crypto);
4021 crypto_free_non_keys (client->crypto);
4022 return send_error (ctx, rc);
4025 static gpg_error_t
4026 parse_opt_data (void *data, void *value)
4028 struct client_s *client = data;
4030 (void) value;
4031 client->opts |= OPT_DATA;
4032 return 0;
4035 static gpg_error_t
4036 send_client_list (assuan_context_t ctx)
4038 struct client_s *client = assuan_get_pointer (ctx);
4039 gpg_error_t rc = 0;
4041 if (client->opts & OPT_VERBOSE)
4043 unsigned i, t;
4044 char **list = NULL;
4045 char *line;
4047 MUTEX_LOCK (&cn_mutex);
4048 pthread_cleanup_push ((void *)release_mutex_cb, &cn_mutex);
4049 t = slist_length (cn_thread_list);
4051 for (i = 0; i < t; i++)
4053 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
4054 char *tmp;
4056 if (thd->state == CLIENT_STATE_UNKNOWN)
4057 continue;
4059 tmp = build_client_info_line (thd, 0);
4060 if (tmp)
4062 char **l = strv_cat (list, tmp);
4063 if (!l)
4064 rc = GPG_ERR_ENOMEM;
4065 else
4066 list = l;
4068 else
4069 rc = GPG_ERR_ENOMEM;
4071 if (rc)
4073 strv_free (list);
4074 break;
4078 pthread_cleanup_pop (1);
4079 if (rc)
4080 return rc;
4082 line = strv_join ("\n", list);
4083 strv_free (list);
4084 pthread_cleanup_push ((void *)xfree, line);
4085 rc = xfer_data (ctx, line, strlen (line));
4086 pthread_cleanup_pop (1);
4087 return rc;
4090 if (client->opts & OPT_DATA)
4092 char buf[ASSUAN_LINELENGTH];
4094 MUTEX_LOCK (&cn_mutex);
4095 snprintf (buf, sizeof (buf), "%u", slist_length (cn_thread_list));
4096 MUTEX_UNLOCK (&cn_mutex);
4097 rc = xfer_data (ctx, buf, strlen (buf));
4099 else
4100 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4102 return rc;
4105 static gpg_error_t
4106 getinfo_command (assuan_context_t ctx, char *line)
4108 struct client_s *client = assuan_get_pointer (ctx);
4109 gpg_error_t rc;
4110 char buf[ASSUAN_LINELENGTH];
4111 struct argv_s *args[] = {
4112 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4113 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_opt_verbose},
4114 NULL
4117 rc = parse_options (&line, args, client, 1);
4118 if (rc)
4119 return send_error (ctx, rc);
4121 if (!strcasecmp (line, "clients"))
4123 rc = send_client_list (ctx);
4125 else if (!strcasecmp (line, "cache"))
4127 if (client->opts & OPT_DATA)
4129 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4130 rc = xfer_data (ctx, buf, strlen (buf));
4132 else
4133 rc = send_status (ctx, STATUS_CACHE, NULL);
4135 else if (!strcasecmp (line, "pid"))
4137 char buf[32];
4138 pid_t pid = getpid ();
4140 snprintf (buf, sizeof (buf), "%i", pid);
4141 rc = xfer_data (ctx, buf, strlen (buf));
4143 else if (!strcasecmp (line, "version"))
4145 char *buf = str_asprintf ("0x%06x %s", VERSION_HEX,
4146 #ifdef WITH_GNUTLS
4147 "GNUTLS "
4148 #endif
4149 #ifdef WITH_LIBACL
4150 "ACL "
4151 #endif
4152 "");
4153 pthread_cleanup_push (xfree, buf);
4154 rc = xfer_data (ctx, buf, strlen (buf));
4155 pthread_cleanup_pop (1);
4157 else if (!strcasecmp (line, "last_error"))
4159 if (client->last_error)
4160 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4161 else
4162 rc = GPG_ERR_NO_DATA;
4164 else if (!strcasecmp (line, "user"))
4166 char *user = NULL;
4168 #ifdef WITH_GNUTLS
4169 if (client->thd->remote)
4170 user = str_asprintf ("#%s", client->thd->tls->fp);
4171 else
4172 user = get_username (client->thd->peer->uid);
4173 #else
4174 user = get_username (client->thd->peer->uid);
4175 #endif
4176 if (user)
4178 pthread_cleanup_push ((void *)xfree, user);
4179 rc = xfer_data (ctx, user, strlen (user));
4180 pthread_cleanup_pop (1);
4182 else
4183 rc = GPG_ERR_NO_DATA;
4185 else
4186 rc = gpg_error (GPG_ERR_SYNTAX);
4188 return send_error (ctx, rc);
4191 static gpg_error_t
4192 parse_listkeys_opt_secret_only (void *data, void *value)
4194 struct client_s *client = data;
4196 (void) value;
4197 client->opts |= OPT_SECRET_ONLY;
4198 return 0;
4201 static gpg_error_t
4202 keyinfo_command (assuan_context_t ctx, char *line)
4204 struct client_s *client = assuan_get_pointer (ctx);
4205 gpg_error_t rc = 0;
4206 char **keys = NULL, **p = NULL;
4207 int sym = 0;
4209 if (line && *line)
4210 return send_error (ctx, GPG_ERR_SYNTAX);
4212 if (!(client->flags & FLAG_OPEN))
4213 return send_error (ctx, GPG_ERR_INV_STATE);
4215 if (client->flags & FLAG_NEW)
4216 return send_error (ctx, GPG_ERR_NO_DATA);
4218 rc = lock_flock (ctx, client->filename, FLOCK_TYPE_SH, &client->flock_fd);
4219 if (rc)
4220 return send_error (ctx, rc);
4222 rc = crypto_is_symmetric (client->filename);
4223 unlock_flock (&client->flock_fd);
4224 if (!rc)
4225 sym = 1;
4226 else if (rc != GPG_ERR_BAD_DATA)
4227 return send_error (ctx, rc);
4229 rc = 0;
4230 if (!sym)
4232 p = strv_catv(keys, client->crypto->pubkey);
4233 if (!p)
4234 rc = GPG_ERR_ENOMEM;
4237 if (!rc)
4239 keys = p;
4240 for (p = client->crypto->sigkey; p && *p; p++)
4242 if (!strv_printf(&keys, "S%s", *p))
4244 strv_free (keys);
4245 return send_error (ctx, GPG_ERR_ENOMEM);
4249 else
4250 rc = GPG_ERR_ENOMEM;
4252 if (!rc && keys)
4254 line = strv_join ("\n", keys);
4255 strv_free (keys);
4256 pthread_cleanup_push ((void *)xfree, line);
4257 if (line)
4258 rc = xfer_data (ctx, line, strlen (line));
4259 else
4260 rc = GPG_ERR_ENOMEM;
4262 pthread_cleanup_pop (1);
4264 else if (!keys)
4265 rc = GPG_ERR_NO_DATA;
4266 else
4267 strv_free (keys);
4269 return send_error (ctx, rc);
4272 static gpg_error_t
4273 kill_command (assuan_context_t ctx, char *line)
4275 struct client_s *client = assuan_get_pointer (ctx);
4276 gpg_error_t rc;
4277 unsigned i, t;
4279 if (!line || !*line)
4280 return send_error (ctx, GPG_ERR_SYNTAX);
4282 MUTEX_LOCK (&cn_mutex);
4283 pthread_cleanup_push ((void *)release_mutex_cb, &cn_mutex);
4284 t = slist_length (cn_thread_list);
4285 rc = GPG_ERR_ESRCH;
4287 for (i = 0; i < t; i++)
4289 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
4290 char *tmp = str_asprintf ("%p", thd->tid);
4292 if (strcmp (line, tmp))
4294 xfree (tmp);
4295 continue;
4298 xfree (tmp);
4299 rc = peer_is_invoker (client);
4300 if (!rc)
4302 #ifdef HAVE_PTHREAD_CANCEL
4303 pthread_cancel (thd->tid);
4304 #else
4305 pthread_kill (thd->tid, SIGUSR2);
4306 #endif
4307 break;
4309 else if (rc == GPG_ERR_EACCES)
4310 rc = GPG_ERR_FORBIDDEN;
4311 else if (rc)
4312 break;
4314 if (config_get_boolean ("global", "strict_kill"))
4315 break;
4317 #ifdef WITH_GNUTLS
4318 if (client->thd->remote && thd->remote)
4320 if (!thd->tls || !thd->tls->fp)
4322 rc = GPG_ERR_INV_STATE;
4323 break;
4325 if (!strcmp (client->thd->tls->fp, thd->tls->fp))
4327 #ifdef HAVE_PTHREAD_CANCEL
4328 pthread_cancel (thd->tid);
4329 #else
4330 pthread_kill (thd->tid, SIGUSR2);
4331 #endif
4332 break;
4335 else if (!client->thd->remote && !thd->remote)
4336 #endif
4338 if (client->thd->peer->uid == thd->peer->uid)
4340 #ifdef HAVE_PTHREAD_CANCEL
4341 pthread_cancel (thd->tid);
4342 #else
4343 pthread_kill (thd->tid, SIGUSR2);
4344 #endif
4347 break;
4350 pthread_cleanup_pop (1);
4351 return send_error (ctx, rc);
4354 static gpg_error_t
4355 listkeys_command (assuan_context_t ctx, char *line)
4357 struct client_s *client = assuan_get_pointer (ctx);
4358 struct crypto_s *crypto = NULL;
4359 char **pattern = NULL;
4360 gpgme_key_t *keys = NULL;
4361 char **result = NULL;
4362 gpg_error_t rc;
4363 struct argv_s *args[] = {
4364 &(struct argv_s) {"secret-only", OPTION_TYPE_NOARG,
4365 parse_listkeys_opt_secret_only },
4366 NULL
4369 rc = parse_options (&line, args, client, 1);
4370 if (rc)
4371 return send_error (ctx, rc);
4373 rc = crypto_init (&crypto, client->ctx, client->filename,
4374 client->flags & FLAG_NO_PINENTRY, NULL);
4375 if (rc)
4376 return send_error (ctx, rc);
4378 pthread_cleanup_push ((void *)crypto_free, crypto);
4379 pattern = str_split (line, ",", 0);
4380 pthread_cleanup_push ((void *)(void *)strv_free, pattern);
4381 rc = crypto_list_keys (crypto, pattern, client->opts & OPT_SECRET_ONLY,
4382 &keys);
4383 pthread_cleanup_pop (1);
4384 pthread_cleanup_pop (1);
4385 if (!rc)
4387 int i;
4389 for (i = 0; keys[i]; i++)
4391 char *p = crypto_key_info (keys[i]);
4392 char **r;
4394 if (!p)
4396 rc = GPG_ERR_ENOMEM;
4397 break;
4400 r = strv_cat (result, p);
4401 if (!r)
4403 rc = GPG_ERR_ENOMEM;
4404 break;
4407 result = r;
4410 if (!i)
4411 rc = GPG_ERR_NO_DATA;
4413 crypto_free_key_list (keys);
4416 if (!rc)
4418 line = strv_join ("\n", result);
4419 strv_free (result);
4420 pthread_cleanup_push ((void *)xfree, line);
4421 if (!line)
4422 rc = GPG_ERR_ENOMEM;
4423 else
4424 rc = xfer_data (ctx, line, strlen (line));
4426 pthread_cleanup_pop (1);
4428 else
4429 strv_free (result);
4431 return send_error (ctx, rc);
4434 void
4435 init_commands ()
4437 /* !BEGIN-HELP-TEXT!
4439 * This comment is used as a marker to generate the offline documentation
4440 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4441 * script to determine where commands begin and end.
4443 new_command("HELP", 1, 1, 0, help_command, _(
4444 "HELP [--html] [<COMMAND>]\n"
4445 "Show available commands or command specific help text."
4446 "@*@*"
4447 "The @option{--html} option will output the help text in HTML format."
4450 new_command("KILL", 1, 0, 0, kill_command, _(
4451 "KILL <thread_id>\n"
4452 "Terminates the client identified by @var{thread_id} and releases any file "
4453 "lock or other resources it has held. See @code{GETINFO} (@pxref{GETINFO}) "
4454 "for details about listing connected clients. An @code{invoking_user} "
4455 "(@pxref{Configuration}) may kill any client while others may only kill "
4456 "clients of the same @code{UID} or @abbr{TLS} fingerprint.\n"
4459 new_command("LISTKEYS", 0, 0, 0, listkeys_command, _(
4460 "LISTKEYS [--secret-only] [pattern[,<pattern>]]\n"
4461 "Returns a new line separated list of key information matching a comma "
4462 "separated list of @var{pattern}'s. When option @option{--secret-only} is "
4463 "specified, only keys matching @var{pattern} that also have a secret key "
4464 "available will be returned."
4467 new_command("KEYINFO", 1, 0, 0, keyinfo_command, _(
4468 "KEYINFO\n"
4469 "Returns a new line separated list of key ID's that the currently opened "
4470 "data file has recipients and signers for. If the key is a signing key it "
4471 "will be prefixed with an @code{S}. If the file is a new one, or has no "
4472 "signers in the case of being symmetrically encrypted, the error code "
4473 "@code{GPG_ERR_NO_DATA} is returned."
4476 new_command("GETINFO", 1, 1, 0, getinfo_command, _(
4477 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4478 "Get server and other information. The information is returned via a status "
4479 "message (@pxref{Status Messages}) unless otherwise noted or @option{--data} "
4480 "is specified."
4481 "@*@*"
4482 "@var{CACHE} returns the number of cached documents."
4483 "@*@*"
4484 "@var{CLIENTS} returns the number of "
4485 "connected clients via a status message or a list of connected clients when "
4486 "the @option{--verbose} parameter is used (implies @option{--data}). A "
4487 "verbose line of a client list contains "
4488 "space delimited "
4489 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4490 "IP address if remote, file lock status, whether the current client is self "
4491 "or not, client state (see below), "
4492 "user ID or TLS fingerprint of the connected client, username if the "
4493 "client is a local one else @code{-}, and finally the time stamp of when the "
4494 "client connected."
4495 "@*@*"
4496 "Client state @code{0} is an unknown client state, state @code{1} indicates "
4497 "the client has connected but hasn't completed initializing, state @code{2} "
4498 "indicates that the client is idle, state @code{3} means the "
4499 "client is in a command and state @code{4} means the client is disconnecting. "
4500 "@*@*"
4501 "@var{PID} returns the process ID number of the server via a data response."
4502 "@*@*"
4503 "@var{VERSION} returns the server version number and compile-time features "
4504 "via a data response with each being space delimited."
4505 "@*@*"
4506 "@var{LAST_ERROR} returns a detailed description of the last failed command "
4507 "via a data response, when available."
4508 "@*@*"
4509 "@var{USER} returns the username or @abbr{TLS} hash of the connected client "
4510 "via a data response."
4513 new_command("PASSWD", 0, 0, FLOCK_TYPE_EX|FLOCK_TYPE_KEEP, passwd_command, _(
4514 "PASSWD\n"
4515 "Changes the passphrase of the secret key required to open the current "
4516 "data file. If the data file is symmetrically encrypted, the error "
4517 "@code{GPG_ERR_NOT_SUPPORTED} is returned. When symmetrically encrypted, "
4518 "the @code{SAVE} command (@pxref{SAVE}) should be used instead to prevent "
4519 "this command saving any unwanted changes to the @abbr{XML} document."
4520 "@*@*"
4521 "This command is not available to non-invoking clients "
4522 "(@pxref{Access Control})."
4525 new_command("OPEN", 1, 1, FLOCK_TYPE_SH|FLOCK_TYPE_KEEP, open_command, _(
4526 "OPEN [--lock] <filename>\n"
4527 "Opens @var{filename}. When the @var{filename} is not found on the "
4528 "file-system then a new in-memory document will be created. If the file is "
4529 "found, it is looked for in the file cache and when found no passphrase will "
4530 "be required to open it. When not cached, @cite{pinentry(1)} will be used to "
4531 "retrieve the passphrase for decryption unless @option{disable-pinentry} "
4532 "(@pxref{OPTION}) was specified in which case @command{pwmd} will "
4533 "@emph{INQUIRE} the client for the passphrase."
4534 "@*@*"
4535 "When the @option{--lock} option is passed then the file mutex will be "
4536 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4537 "file had been opened."
4540 new_command("SAVE", 0, 0, FLOCK_TYPE_EX|FLOCK_TYPE_KEEP, save_command, _(
4541 "SAVE [--symmetric] [--inquire-keyparam] [--keyid=<fpr>[,..] | [--inquire-keyid]] [--sign-keyid=<fpr>[,..] | [--inquire-sign-keyid]]\n"
4542 "Writes the in-memory @abbr{XML} document to disk. The file written to is the "
4543 "file that was opened using the @code{OPEN} command (@pxref{OPEN}). If the "
4544 "file is a new one or the option @option{--inquire-keyparam} was passed, a "
4545 "new keypair will be generated and @cite{pinentry(1)} will be used to prompt "
4546 "for the passphrase of the secret key used for signing."
4547 "@*@*"
4548 "The @option{--inquire-keyparam} option will send an "
4549 "@emph{INQUIRE} to the client to obtain the key parameters to use for "
4550 "generating the new keypair. The inquired data is expected to be in "
4551 "@cite{gnupg} @abbr{XML} format. See the @cite{gnupg} documentation for "
4552 "details. Note that when this option is specified a new keypair will be "
4553 "generated reguardless if the file is a new one and that the passphrase for "
4554 "the current file will be required before generating the new keypair. This "
4555 "option is available to non-invoking clients (@pxref{Access Control}) only "
4556 "when the file is a new one."
4557 "@*@*"
4558 "You can encrypt the data file to a recipient other than the one that it "
4559 "was encrypted with by passing the @option{--keyid} or "
4560 "@option{--inquire-keyid} option with "
4561 "the fingerprint of a public encryption key as its argument. Use the "
4562 "@command{LISTKEYS} command (@pxref{LISTKEYS}) to show key information by "
4563 "pattern. The @option{--sign-keyid} or @option{--inquire-sign-keyid} option "
4564 "may also be used to sign the data "
4565 "file with an alternate key by specifying the fingerprint of a secret key. "
4566 "A passphrase to decrypt the data file "
4567 "will be required if one or more of the original encryption or signing keys "
4568 "are not found in either of these two options' arguments. The original "
4569 "encryption or signing keys will be used when either of these options are "
4570 "not specified."
4571 "@*@*"
4572 "The @option{--keyid} and @option{--sign-keyid} options are not available "
4573 "for non-invoking clients "
4574 "(@pxref{Access Control}) when the recipients or signers do not match those "
4575 "that were used when the file was @code{OPEN}'ed."
4576 "@*@*"
4577 "The @option{--symmetric} option specifies that a new data file be "
4578 "conventionally encrypted. These types of data files do not use a recipient "
4579 "public key but may be signed by using the @option{--sign-keyid} or "
4580 "@option{--inquire-sign-keyid} options. To remove all signers from a "
4581 "symmtrically encrypted data file, leave the option value empty. Note that "
4582 "you cannot change encryption schemes once a data file has been saved."
4585 new_command("ISCACHED", 1, 0, 0, iscached_command, _(
4586 "ISCACHED [--lock] [--agent [--sign]] <filename>\n"
4587 "Determines the file cache status of the specified @var{filename}. "
4588 "The default is to test whether the filename is cached in memory. Passing "
4589 "option @option{--agent} will test the @command{gpg-agent} cache for at most "
4590 "one cached key used for opening the data file (@pxref{OPEN}). To test if "
4591 "a signing key is cached, pass @option{--sign} along with @option{--agent}. "
4592 "Both the @option{--agent} and @option{--sign} options require an opened data "
4593 "file."
4594 "@*@*"
4595 "An @emph{OK} response is returned if the specified @var{filename} is found "
4596 "in the cache. If not found in the cache but exists on the filesystem "
4597 "then @code{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4598 "returned."
4599 "@*@*"
4600 "The @option{--lock} option will lock the file mutex of @var{filename} when "
4601 "the file exists; it does not need to be opened nor cached. The lock will be "
4602 "released when the client exits or sends the @code{UNLOCK} command "
4603 "(@pxref{UNLOCK}). When this option is passed the current data file is closed."
4606 new_command("CLEARCACHE", 1, 1, 0, clearcache_command, _(
4607 "CLEARCACHE [<filename>]\n"
4608 "Clears a file cache entry for all or the specified @var{filename}. Note that "
4609 "this will also clear any @command{gpg-agent} cached keys which may cause "
4610 "problems if another data file shares the same keys as @var{filename}."
4611 "@*@*"
4612 "When clearing all cache entries a permissions test is done against the "
4613 "current client based on the @var{access} configuration parameter in a "
4614 "@var{filename} section. Both a cache entry may be cleared and an error "
4615 "returned depending on cached data files and client permissions."
4618 new_command("CACHETIMEOUT", 1, 1, 0, cachetimeout_command, _(
4619 "CACHETIMEOUT <filename> <seconds>\n"
4620 "The time in @var{seconds} until @var{filename} will be removed from the "
4621 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4622 "the passphrase for each @code{OPEN} command (@pxref{OPEN}) or @code{SAVE} "
4623 "(@pxref{SAVE}) command. @xref{Configuration}, and the @code{cache_timeout} "
4624 "parameter."
4627 new_command("LIST", 0, 1, 0, list_command, _(
4628 "LIST [--inquire] [--recurse] [element[<TAB>child[..]]]\n"
4629 "If no element path is given then a newline separated list of root elements "
4630 "is returned with a data response. If given, then children of the specified "
4631 "element path are returned."
4632 "@*@*"
4633 "Each element path "
4634 "returned will have zero or more flags appened to it. These flags are "
4635 "delimited from the element path by a single space character. A flag itself "
4636 "is a single character. Flag @code{P} indicates that access to the element "
4637 "is denied. Flag @code{+} indicates that there are child nodes of "
4638 "the current element path. Flag @code{E} indicates that an element of the "
4639 "element path contained in a @var{target} attribute could not be found. Flag "
4640 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4641 "(@pxref{Configuration}). Flag @code{T}, followed by a single space character, "
4642 "then an element path, is the element path of the @var{target} attribute "
4643 "contained in the current element."
4644 "@*@*"
4645 "When a specified element path contains an error, beit from the final "
4646 "element in the path or any previous element, the path is still shown but "
4647 "will contain the error flag for the element with the error. Determining "
4648 "the actual element which contains the error is up to the client. This can be "
4649 "done by traversing the final element up to parent elements that contain the "
4650 "same error flag."
4651 "@*@*"
4652 "The option @option{--recurse} may be used to list the entire element tree "
4653 "for a specified element path or the entire tree for all root elements."
4654 "@*@*"
4655 "When the @option{--inquire} option is passed then all remaining non-option "
4656 "arguments are retrieved via a server @emph{INQUIRE}."
4659 new_command("REALPATH", 0, 1, 0, realpath_command, _(
4660 "REALPATH [--inquire] element[<TAB>child[..]]\n"
4661 "Resolves all @code{target} attributes of the specified element path and "
4662 "returns the result with a data response. @xref{Target Attribute}, for details."
4663 "@*@*"
4664 "When the @option{--inquire} option is passed then all remaining non-option "
4665 "arguments are retrieved via a server @emph{INQUIRE}."
4668 new_command("STORE", 0, 1, 0, store_command, _(
4669 "STORE element[<TAB>child[..]]<TAB>[content]\n"
4670 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4671 "@*@*"
4672 "Creates a new element path or modifies the @var{content} of an existing "
4673 "element. If only a single element is specified then a new root element is "
4674 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4675 "set to the final @key{TAB} delimited element. If no @var{content} is "
4676 "specified after the final @key{TAB}, then the content of the existing "
4677 "element will be removed; or will be empty if creating a new element."
4678 "@*@*"
4679 "The only restriction of an element name is that it not contain whitespace "
4680 "characters. There is no other whitespace between the @key{TAB} delimited "
4681 "elements. It is recommended that the content of an element be base64 encoded "
4682 "when it contains control or @key{TAB} characters to prevent @abbr{XML} "
4683 "parsing and @command{pwmd} syntax errors."
4686 new_command("RENAME", 0, 1, 0, rename_command, _(
4687 "RENAME [--inquire] element[<TAB>child[..]] <value>\n"
4688 "Renames the specified @var{element} to the new @var{value}. If an element of "
4689 "the same name as the @var{value} already exists it will be overwritten."
4690 "@*@*"
4691 "When the @option{--inquire} option is passed then all remaining non-option "
4692 "arguments are retrieved via a server @emph{INQUIRE}."
4695 new_command("COPY", 0, 1, 0, copy_command, _(
4696 "COPY [--inquire] source[<TAB>child[..]] dest[<TAB>child[..]]\n"
4697 "Copies the entire element tree starting from the child node of the source "
4698 "element, to the destination element path. If the destination element path "
4699 "does not exist then it will be created; otherwise it is overwritten."
4700 "@*@*"
4701 "Note that attributes from the source element are merged into the "
4702 "destination element when the destination element path exists. When an "
4703 "attribute of the same name exists in both the source and destination "
4704 "elements then the destination attribute will be updated to the source "
4705 "attribute value."
4706 "@*@*"
4707 "When the @option{--inquire} option is passed then all remaining non-option "
4708 "arguments are retrieved via a server @emph{INQUIRE}."
4711 new_command("MOVE", 0, 1, 0, move_command, _(
4712 "MOVE [--inquire] source[<TAB>child[..]] [dest[<TAB>child[..]]]\n"
4713 "Moves the source element path to the destination element path. If the "
4714 "destination is not specified then it will be moved to the root node of the "
4715 "document. If the destination is specified and exists then it will be "
4716 "overwritten; otherwise non-existing elements of the destination element "
4717 "path will be created."
4718 "@*@*"
4719 "When the @option{--inquire} option is passed then all remaining non-option "
4720 "arguments are retrieved via a server @emph{INQUIRE}."
4723 new_command("DELETE", 0, 1, 0, delete_command, _(
4724 "DELETE [--inquire] element[<TAB>child[..]]\n"
4725 "Removes the specified element path and all of its children. This may break "
4726 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4727 "refers to this element or any of its children."
4728 "@*@*"
4729 "When the @option{--inquire} option is passed then all remaining non-option "
4730 "arguments are retrieved via a server @emph{INQUIRE}."
4733 new_command("GET", 0, 1, 0, get_command, _(
4734 "GET [--inquire] element[<TAB>child[..]]\n"
4735 "Retrieves the content of the specified element. The content is returned "
4736 "with a data response."
4737 "@*@*"
4738 "When the @option{--inquire} option is passed then all remaining non-option "
4739 "arguments are retrieved via a server @emph{INQUIRE}."
4742 new_command("ATTR", 0, 1, 0, attr_command, _(
4743 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] element[<TAB>child[..]] ..\n"
4744 "@table @asis\n"
4745 "@item ATTR SET attribute element[<TAB>child[..]] [value]\n"
4746 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4747 "element. When no @var{value} is specified any existing value will be removed."
4748 "@*@*"
4749 "@item ATTR DELETE attribute element[<TAB>child[..]]\n"
4750 " Removes an attribute from an element. If @var{attribute} is @code{_name} "
4751 "or @code{target} an error is returned. Use the @command{DELETE} command "
4752 "(@pxref{DELETE}) instead."
4753 "@*@*"
4754 "@item ATTR LIST element[<TAB>child[..]]\n"
4755 " Retrieves a newline separated list of attributes names and values "
4756 "from the specified element. Each attribute name and value is space delimited."
4757 "@*@*"
4758 "@item ATTR GET attribute element[<TAB>child[..]]\n"
4759 " Retrieves the value of an @var{attribute} from an element."
4760 "@end table\n"
4761 "@*@*"
4762 "When the @option{--inquire} option is passed then all remaining non-option "
4763 "arguments are retrieved via a server @emph{INQUIRE}."
4764 "@*@*"
4765 "@xref{Target Attribute}, for details about this special attribute and also "
4766 "@pxref{Other Attributes} for other attributes that are handled specially "
4767 "by @command{pwmd}."
4770 new_command("XPATH", 0, 1, 0, xpath_command, _(
4771 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4772 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4773 "specified it is assumed the expression is a request to return a result. "
4774 "Otherwise, the result is set to the @var{value} argument and the document is "
4775 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4776 "is assumed to be empty and the document is updated. For example:"
4777 "@sp 1\n"
4778 "@example\n"
4779 "XPATH //element[@@_name='password']@key{TAB}\n"
4780 "@end example\n"
4781 "@sp 1\n"
4782 "would clear the content of all @var{password} elements in the data file "
4783 "while leaving off the trailing @key{TAB} would return all @var{password} "
4784 "elements in @abbr{XML} format."
4785 "@*@*"
4786 "When the @option{--inquire} option is passed then all remaining non-option "
4787 "arguments are retrieved via a server @emph{INQUIRE}."
4788 "@*@*"
4789 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4790 "expression syntax."
4793 new_command("XPATHATTR", 0, 1, 0, xpathattr_command, _(
4794 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4795 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4796 "attributes and does not return a result. For the @var{SET} operation the "
4797 "@var{value} is optional but the field is required. If not specified then "
4798 "the attribute value will be empty. For example:"
4799 "@sp 1\n"
4800 "@example\n"
4801 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4802 "@end example\n"
4803 "@sp 1\n"
4804 "would create a @var{password} attribute for each @var{password} element "
4805 "found in the document. The attribute value will be empty but still exist."
4806 "@*@*"
4807 "When the @option{--inquire} option is passed then all remaining non-option "
4808 "arguments are retrieved via a server @emph{INQUIRE}."
4809 "@*@*"
4810 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4811 "expression syntax."
4814 new_command("IMPORT", 0, 1, 0, import_command, _(
4815 "IMPORT [--root=element[<TAB>child[..]]]\n"
4816 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4817 "@*@*"
4818 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4819 "argument is raw @abbr{XML} data. The content is created as a child of "
4820 "the element path specified with the @option{--root} option or at the "
4821 "document root when not specified. Existing elements of the same name will "
4822 "be overwritten."
4823 "@*@*"
4824 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4825 "for details."
4828 new_command("DUMP", 0, 1, 0, dump_command, _(
4829 "DUMP\n"
4830 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4831 "dumping a specific node."
4834 new_command("LOCK", 0, 0, 0, lock_command, _(
4835 "LOCK\n"
4836 "Locks the mutex associated with the opened file. This prevents other clients "
4837 "from sending commands to the same opened file until the client "
4838 "that sent this command either disconnects or sends the @code{UNLOCK} "
4839 "command. @xref{UNLOCK}."
4842 new_command("UNLOCK", 1, 0, 0, unlock_command, _(
4843 "UNLOCK\n"
4844 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4845 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4846 "@pxref{ISCACHED})."
4849 new_command("GETCONFIG", 1, 1, 0, getconfig_command, _(
4850 "GETCONFIG [filename] <parameter>\n"
4851 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4852 "data response. If no file has been opened then the value for @var{filename} "
4853 "or the default from the @var{global} section will be returned. If a file "
4854 "has been opened and no @var{filename} is specified, the value previously "
4855 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4858 new_command("OPTION", 1, 1, 0, option_command, _(
4859 "OPTION <NAME>=[<VALUE>]\n"
4860 "Sets a client option @var{name} to @var{value}. The value for an option is "
4861 "kept for the duration of the connection with the exception of the "
4862 "@command{pinentry} options which are defaults for all future connections "
4863 "(@pxref{Pinentry}). When @var{value} is empty the option is unset."
4864 "@*@*"
4865 "@table @asis\n"
4866 "@item DISABLE-PINENTRY\n"
4867 "Disable use of @command{pinentry} for passphrase retrieval. When @code{1}, a "
4868 "server inquire is sent to the client to obtain the passphrase. This option "
4869 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4870 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands. Set to @code{0} "
4871 "to use a @command{pinentry}."
4872 "@*@*"
4873 "@item DISPLAY\n"
4874 "Set or unset the X11 display to use when prompting for a passphrase."
4875 "@*@*"
4876 "@item TTYNAME\n"
4877 "Set the terminal device path to use when prompting for a passphrase."
4878 "@*@*"
4879 "@item TTYTYPE\n"
4880 "Set the terminal type for use with @option{TTYNAME}."
4881 "@*@*"
4882 "@item NAME\n"
4883 "Associates the thread ID of the connection with the specified textual "
4884 "representation. Useful for debugging log messages. May not contain whitespace."
4885 "@*@*"
4886 "@item LOCK-TIMEOUT\n"
4887 "When not @code{0}, the duration in tenths of a second to wait for the file "
4888 "mutex which has been locked by another thread to be released before returning "
4889 "an error. When @code{-1} the error will be returned immediately."
4890 "@*@*"
4891 "@item CLIENT-STATE\n"
4892 "When set to @code{1} then client state status messages for other clients are "
4893 "sent to the current client. The default is @code{0}."
4894 "@end table\n"
4897 new_command("LS", 1, 1, 0, ls_command, _(
4898 "LS\n"
4899 "Returns a newline separated list of data files stored in the data directory "
4900 "@file{HOMEDIR/data} (@pxref{Invoking}) with a data response."
4903 new_command("RESET", 1, 1, 0, NULL, _(
4904 "RESET\n"
4905 "Closes the currently opened file but keeps any previously set client options "
4906 "(@pxref{OPTION})."
4909 new_command("NOP", 1, 1, 0, NULL, _(
4910 "NOP\n"
4911 "Does nothing. Always returns successfully."
4914 /* !END-HELP-TEXT! */
4915 new_command ("CANCEL", 1, 1, 0, NULL, NULL);
4916 new_command ("END", 1, 1, 0, NULL, NULL);
4917 new_command ("BYE", 1, 1, 0, NULL, NULL);
4919 int i;
4920 for (i = 0; command_table[i]; i++);
4921 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4922 sort_commands);
4925 gpg_error_t
4926 register_commands (assuan_context_t ctx)
4928 int i = 0, rc;
4930 for (; command_table[i]; i++)
4932 if (!command_table[i]->handler)
4933 continue;
4935 rc = assuan_register_command (ctx, command_table[i]->name,
4936 command_table[i]->handler,
4937 command_table[i]->help);
4938 if (rc)
4939 return rc;
4942 rc = assuan_register_bye_notify (ctx, bye_notify);
4943 if (rc)
4944 return rc;
4946 rc = assuan_register_reset_notify (ctx, reset_notify);
4947 if (rc)
4948 return rc;
4950 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4951 if (rc)
4952 return rc;
4954 return assuan_register_post_cmd_notify (ctx, command_finalize);