Remove unused code.
[libpwmd.git] / src / commands.c
blobcb495edd78b2981b6b874d744dc37152d6af10f1
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;
106 #ifdef WITH_GNUTLS
107 if (thd->remote)
108 uid = str_asprintf("#%s", thd->tls->fp);
109 else
111 uid = str_asprintf("%u", thd->peer->uid);
112 username = get_username (thd->peer->uid);
114 #else
115 uid = str_asprintf("%u", thd->peer->uid);
116 username = get_username (thd->peer->uid);
117 #endif
118 line = str_asprintf ("%p %s %s %s %u %u %u %s %s %u",
119 thd->tid,
120 thd->name ? thd->name : "-",
121 thd->cl && thd->cl->filename
122 && (thd->cl->flags & FLAG_OPEN)
123 ? thd->cl->filename : "/",
124 #ifdef WITH_GNUTLS
125 thd->remote ? thd->peeraddr : "-",
126 #else
127 "-",
128 #endif
129 thd->cl && thd->cl->flags & FLAG_HAS_LOCK ? 1 : 0,
130 !status && pthread_equal (pthread_self (), thd->tid) ? 1 : 0,
131 thd->state, uid,
132 #ifdef WITH_GNUTLS
133 thd->remote ? "-" : username,
134 #else
135 username,
136 #endif
137 thd->conntime
140 xfree (username);
141 xfree (uid);
142 return line;
145 void
146 update_client_state (struct client_s *client, unsigned s)
148 MUTEX_LOCK (&cn_mutex);
149 client->thd->state = s;
150 MUTEX_UNLOCK (&cn_mutex);
152 if (client->thd->state != CLIENT_STATE_UNKNOWN)
154 char *line = build_client_info_line (client->thd, 1);
156 pthread_cleanup_push (xfree, line);
157 if (line)
158 send_status_all_not_self (STATUS_STATE, "%s", line);
159 pthread_cleanup_pop (1);
163 static gpg_error_t
164 unlock_file_mutex (struct client_s *client, int remove)
166 gpg_error_t rc = 0;
168 // OPEN: keep the lock for the same file being reopened.
169 if (client->flags & FLAG_KEEP_LOCK && client->flags & FLAG_HAS_LOCK)
170 return 0;
172 if (!(client->flags & FLAG_HAS_LOCK))
173 return GPG_ERR_NOT_LOCKED;
175 rc = cache_unlock_mutex (client->filename, remove);
176 if (rc)
177 rc = GPG_ERR_INV_STATE;
178 else
179 client->flags &= ~(FLAG_HAS_LOCK | FLAG_LOCK_CMD);
181 return rc;
184 static gpg_error_t
185 lock_file_mutex (struct client_s *client, int add)
187 gpg_error_t rc = 0;
188 int timeout = config_get_integer (client->filename, "cache_timeout");
190 if (client->flags & FLAG_HAS_LOCK)
191 return 0;
193 rc = cache_lock_mutex (client->ctx, client->filename,
194 client->lock_timeout, add, timeout);
195 if (!rc)
196 client->flags |= FLAG_HAS_LOCK;
198 return rc;
201 static gpg_error_t
202 file_modified (struct client_s *client, struct command_table_s *cmd)
204 gpg_error_t rc = 0;
205 int type = !cmd->flock_type || (cmd->flock_type & FLOCK_TYPE_SH)
206 ? LOCK_SH : LOCK_EX;
208 if (!(client->flags & FLAG_OPEN))
209 return GPG_ERR_INV_STATE;
211 rc = lock_file_mutex (client, 0);
212 if (rc && rc != GPG_ERR_NO_DATA)
213 return rc;
215 rc = lock_flock (client->ctx, client->filename, type, &client->flock_fd);
216 if (!rc)
218 rc = validate_checksum (client, client->filename, NULL, NULL, NULL);
219 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
220 rc = 0;
221 else if (rc)
222 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
224 else if (gpg_err_code (rc) == GPG_ERR_ENOENT)
225 rc = 0;
227 /* FLAG_HAS_LOCK may have been set by the LOCK command or OPEN --lock. */
228 if ((cmd->unlock && !(client->flags & FLAG_HAS_LOCK)) || rc)
229 unlock_file_mutex (client, 0);
231 if (rc || !(cmd->flock_type & FLOCK_TYPE_KEEP))
232 unlock_flock (&client->flock_fd);
234 return rc;
237 static gpg_error_t
238 parse_xml (assuan_context_t ctx, int new)
240 struct client_s *client = assuan_get_pointer (ctx);
241 int cached = client->doc != NULL;
242 gpg_error_t rc = 0;
244 if (new)
246 client->doc = xml_new_document ();
247 if (client->doc)
249 xmlChar *result;
250 int len;
252 xmlDocDumpFormatMemory (client->doc, &result, &len, 0);
253 client->crypto->plaintext = result;
254 client->crypto->plaintext_size = len;
255 if (!client->crypto->plaintext)
257 xmlFreeDoc (client->doc);
258 client->doc = NULL;
259 rc = GPG_ERR_ENOMEM;
262 else
263 rc = GPG_ERR_ENOMEM;
265 else if (!cached)
266 rc = xml_parse_doc ((char *) client->crypto->plaintext,
267 client->crypto->plaintext_size,
268 (xmlDocPtr *)&client->doc);
270 return rc;
273 static void
274 free_client (struct client_s *client)
276 if (client->doc)
277 xmlFreeDoc (client->doc);
279 xfree (client->crc);
280 xfree (client->filename);
281 xfree (client->last_error);
282 crypto_free (client->crypto);
283 client->crypto = NULL;
286 void
287 reset_client (struct client_s *client)
289 assuan_context_t ctx = client->ctx;
290 struct client_thread_s *thd = client->thd;
291 long lock_timeout = client->lock_timeout;
292 xmlErrorPtr xml_error = client->xml_error;
293 int no_pinentry = (client->flags & FLAG_NO_PINENTRY);
294 int flock_fd = client->flock_fd;
296 unlock_file_mutex (client, client->flags & FLAG_NEW);
297 free_client (client);
298 memset (client, 0, sizeof (struct client_s));
299 client->flock_fd = flock_fd;
300 client->xml_error = xml_error;
301 client->ctx = ctx;
302 client->thd = thd;
303 client->lock_timeout = lock_timeout;
304 client->flags |= no_pinentry ? FLAG_NO_PINENTRY : 0;
307 static void
308 req_free (void *arg)
310 if (!arg)
311 return;
313 strv_free ((char **) arg);
316 static gpg_error_t
317 parse_open_opt_lock (void *data, void *value)
319 struct client_s *client = data;
321 client->opts |= OPT_LOCK_ON_OPEN;
322 return 0;
325 static gpg_error_t
326 parse_opt_inquire (void *data, void *value)
328 struct client_s *client = data;
330 (void) value;
331 client->opts |= OPT_INQUIRE;
332 return 0;
335 static gpg_error_t
336 update_checksum (struct client_s *client)
338 unsigned char *crc;
339 size_t len;
340 struct cache_data_s *cdata;
341 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
343 if (rc)
344 return rc;
346 xfree (client->crc);
347 client->crc = crc;
348 cdata = cache_get_data (client->filename);
349 if (cdata)
351 xfree (cdata->crc);
352 cdata->crc = xmalloc (len);
353 memcpy (cdata->crc, crc, len);
356 return 0;
359 static gpg_error_t
360 validate_checksum (struct client_s *client, const char *filename,
361 struct cache_data_s *cdata, unsigned char **r_crc,
362 size_t *r_crclen)
364 unsigned char *crc;
365 size_t len;
366 gpg_error_t rc;
367 int n = 0;
369 if (cdata && !cdata->crc)
370 return GPG_ERR_CHECKSUM;
372 rc = get_checksum (filename, &crc, &len);
373 if (rc)
374 return rc;
376 if (cdata)
377 n = memcmp (cdata->crc, crc, len);
378 else if (client->crc)
379 n = memcmp (client->crc, crc, len);
381 if (!n && r_crc)
383 *r_crc = crc;
384 *r_crclen = len;
386 else
387 xfree (crc);
389 return n ? GPG_ERR_CHECKSUM : 0;
392 static gpg_error_t
393 open_command (assuan_context_t ctx, char *line)
395 gpg_error_t rc;
396 struct client_s *client = assuan_get_pointer (ctx);
397 char **req, *filename;
398 int same_file = 0;
399 assuan_peercred_t peer;
400 struct cache_data_s *cdata = NULL;
401 int cached = 0;
402 unsigned char *crc = NULL;
403 size_t crclen = 0;
404 struct argv_s *args[] = {
405 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
406 NULL
409 rc = parse_options (&line, args, client, 1);
410 if (rc)
411 return send_error (ctx, rc);
413 req = str_split (line, " ", 2);
414 if (!req)
415 return send_error (ctx, GPG_ERR_SYNTAX);
417 rc = do_validate_peer (ctx, req[0], &peer);
418 if (rc == GPG_ERR_FORBIDDEN)
419 rc = peer_is_invoker (client);
421 if (rc)
423 strv_free (req);
424 return send_error (ctx, rc);
427 filename = req[0];
428 if (!valid_filename (filename))
430 strv_free (req);
431 return send_error (ctx, GPG_ERR_INV_VALUE);
434 pthread_cleanup_push ((void *)req_free, req);
435 /* This client may have locked a different file with ISCACHED --lock than
436 * the current filename. This will remove that lock. */
437 same_file = client->filename && !strcmp (filename, client->filename);
438 if (client->flags & FLAG_OPEN ||
439 (client->flags & FLAG_HAS_LOCK && !same_file))
441 uint32_t opts = client->opts;
442 uint32_t flags = client->flags;
444 if (same_file)
445 client->flags |= FLAG_KEEP_LOCK;
446 else if (client->flags & FLAG_NEW)
447 cache_clear (NULL, client->filename, 0);
449 reset_client (client);
450 client->opts = opts;
451 client->flags |= flags;
452 client->flags &= ~(FLAG_LOCK_CMD);
453 if (!same_file)
454 client->flags &= ~(FLAG_HAS_LOCK | FLAG_NEW);
457 client->filename = str_dup (filename);
458 if (!client->filename)
460 strv_free (req);
461 return send_error(ctx, GPG_ERR_ENOMEM);
464 /* Need to lock the mutex here because file_modified() cannot without
465 * knowing the filename. */
466 rc = lock_file_mutex (client, 1);
467 if (rc)
468 client->flags &= ~FLAG_OPEN;
470 if (!rc)
472 rc = lock_flock (ctx, filename, LOCK_SH, &client->flock_fd);
473 if (gpg_err_code (rc) == GPG_ERR_ENOENT)
474 rc = 0;
477 if (!rc)
479 struct stat st;
480 char *keyfile = config_get_string (client->filename, "passphrase_file");
482 rc = crypto_init (&client->crypto, client->ctx, client->filename,
483 client->flags & FLAG_NO_PINENTRY, keyfile);
484 if (rc)
485 xfree (keyfile);
487 if (!rc && stat (client->filename, &st) == -1)
488 rc = gpg_error_from_errno (errno);
489 else if (!rc && !S_ISREG (st.st_mode)) // to match LS output
490 rc = gpg_error_from_errno (ENOANO);
493 if (!rc || gpg_err_code (rc) == GPG_ERR_ENOENT)
495 cdata = cache_get_data (client->filename);
497 if (rc) // new file
499 rc = 0;
500 client->flags |= FLAG_NEW;
501 // data file disappeared. clear the cache entry.
502 cache_clear (NULL, client->filename, 1);
503 cdata = NULL;
505 else if (cdata && cdata->doc) // cached document
507 int reload = 0;
508 int defer = 0;
510 if (!rc && !(client->flags & FLAG_NEW))
512 rc = validate_checksum (client, client->filename, cdata, &crc,
513 &crclen);
514 if (rc == GPG_ERR_CHECKSUM)
516 rc = 0;
517 reload = 1;
521 if (!rc)
523 rc = cache_iscached (client->filename, &defer, 0, 0);
524 if ((!rc || rc == GPG_ERR_ENOENT) && defer)
526 rc = 0;
527 reload = 2;
531 if (!rc && reload)
533 if (reload == 1)
534 log_write ("%s: %s", client->filename,
535 pwmd_strerror (GPG_ERR_CHECKSUM));
536 cache_clear (NULL, client->filename, 1);
537 cdata = NULL;
538 rc = crypto_decrypt (client, client->crypto);
540 #ifdef WITH_GNUTLS
541 else if (!rc)
543 if (client->thd->remote
544 && config_get_boolean (client->filename, "tcp_require_key")
545 && !(client->flags & FLAG_NEW))
546 rc = crypto_try_decrypt (client,
547 (client->flags & FLAG_NO_PINENTRY));
549 #endif
551 if (!rc && cdata)
553 client->crypto->plaintext = cdata->doc;
554 client->crypto->plaintext_size = cdata->size;
555 rc = cache_decrypt (client->crypto);
556 if (!rc)
558 cdata->doc = NULL;
559 cdata->size = 0;
560 strv_free (client->crypto->pubkey);
561 strv_free (client->crypto->sigkey);
562 client->crypto->pubkey = strv_dup (cdata->pubkey);
563 client->crypto->sigkey = strv_dup (cdata->sigkey);
564 cached = 1;
566 else
568 client->crypto->plaintext = NULL;
569 client->crypto->plaintext_size = 0;
573 else // existing file
575 cached = cdata != NULL;
576 rc = crypto_decrypt (client, client->crypto);
580 if (!rc)
582 rc = parse_xml (ctx, client->flags & FLAG_NEW);
583 if (!rc)
585 rc = cache_encrypt (client->crypto);
586 if (rc)
587 cache_clear (NULL, client->filename, 1);
588 else
590 int timeout = config_get_integer (client->filename,
591 "cache_timeout");
593 cache_free_data_once (cdata);
594 cdata = xcalloc (1, sizeof (struct cache_data_s));
595 cdata->doc = client->crypto->plaintext;
596 cdata->size = client->crypto->plaintext_size;
597 cdata->pubkey = strv_dup(client->crypto->pubkey);
598 cdata->sigkey = strv_dup(client->crypto->sigkey);
599 client->crypto->plaintext = NULL;
600 client->crypto->plaintext_size = 0;
602 if (cached) // wont increment the refcount
604 /* Prevent using another FD to update the checksum for a
605 * cached data file. The validity has already been
606 * verified. */
607 xfree (client->crc);
608 client->crc = xmalloc (crclen);
609 memcpy (client->crc, crc, crclen);
610 xfree (cdata->crc);
611 cdata->crc = xmalloc (crclen);
612 memcpy (cdata->crc, crc, crclen);
614 rc = cache_set_data (client->filename, cdata);
615 /* The cache entry may have been removed and cache_set_data()
616 * already sent STATUS_CACHE. */
617 if (!cache_iscached (client->filename, NULL, 0, 0))
618 rc = send_status (ctx, STATUS_CACHE, NULL);
620 else
621 rc = cache_add_file (client->filename, cdata, timeout);
626 pthread_cleanup_pop (1);
627 xfree (crc);
629 if (!rc && !(client->flags & FLAG_NEW) && !cached)
630 rc = update_checksum (client);
632 if (!rc)
634 client->flags |= FLAG_OPEN;
636 if (client->flags & FLAG_NEW)
637 rc = send_status (ctx, STATUS_NEWFILE, NULL);
639 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
640 rc = do_lock (client, 0);
643 if (rc)
645 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
647 if (client->flags & FLAG_NEW)
648 cache_clear (NULL, client->filename, 1);
650 crypto_free (client->crypto);
651 client->crypto = NULL;
652 client->flags &= ~FLAG_OPEN;
654 else
655 crypto_free_non_keys (client->crypto);
657 return send_error (ctx, rc);
660 /* If not the invoking_user or is an existing file, check that the list of user
661 * supplied key ID's are in the list of current key ID's obtained when
662 * decrypting the data file.
664 static gpg_error_t
665 permitted_to_save (struct client_s *client, const char **keys,
666 const char **value)
668 gpg_error_t rc = 0;
669 const char **v;
671 if ((client->flags & FLAG_NEW) || (client->opts & OPT_SYMMETRIC))
672 return 0;
674 rc = peer_is_invoker (client);
675 if (!rc)
676 return 0;
678 /* Empty match. */
679 if ((!value && !keys) || ((value && !*value) && (keys && !*keys)))
680 return 0;
682 for (v = value; v && *v; v++)
684 const char **k, *pv = *v;
685 int match = 0;
687 if (*pv == '0' && *(pv+1) == 'x')
688 pv += 2;
690 for (k = keys; k && *k; k++)
692 const char *pk = *k;
694 if (*pk == '0' && *(pk+1) == 'x')
695 pk += 2;
697 rc = !strcmp (pv, pk) ? 0 : GPG_ERR_FORBIDDEN;
698 if (rc)
699 rc = !strcmp (pv, *k) ? 0 : GPG_ERR_FORBIDDEN;
701 if (!rc)
703 match = 1;
704 break;
708 if (!match)
709 return GPG_ERR_FORBIDDEN;
712 return rc;
715 /* Requires that the keyid be a fingerprint in 16 byte form. */
716 static gpg_error_t
717 parse_save_opt_keyid_common (struct client_s *client, const char **list,
718 const char *value, char ***dst)
720 gpg_error_t rc = 0;
721 char **keys = NULL;
723 if (value && *value)
725 keys = str_split (value, ",", 0);
726 if (!keys)
727 return GPG_ERR_ENOMEM;
730 rc = crypto_keyid_to_16b (keys);
731 if (!rc)
732 rc = permitted_to_save (client, list, (const char **)keys);
734 if (!rc)
735 *dst = keys;
736 else
737 strv_free (keys);
739 return rc;
742 static gpg_error_t
743 parse_save_opt_keyid (void *data, void *value)
745 struct client_s *client = data;
746 const char *str = value;
747 char **dst = NULL;
748 gpg_error_t rc;
750 rc = parse_save_opt_keyid_common (client,
751 (const char **)client->crypto->pubkey,
752 str, &dst);
753 if (rc)
754 return rc;
756 client->crypto->save.pubkey = dst;
757 return 0;
760 static gpg_error_t
761 parse_save_opt_sign_keyid (void *data, void *value)
763 struct client_s *client = data;
764 const char *str = value;
765 char **dst = NULL;
766 gpg_error_t rc;
768 rc = parse_save_opt_keyid_common (client,
769 (const char **)client->crypto->sigkey,
770 str, &dst);
771 if (rc)
772 return rc;
774 if (!dst)
775 client->opts |= OPT_NO_SIGNER;
777 client->crypto->save.sigkey = dst;
778 return 0;
781 static gpg_error_t
782 parse_save_opt_inquire (struct client_s *client, uint32_t opt)
784 if (opt == OPT_INQUIRE && !(client->flags & FLAG_NEW))
786 gpg_error_t rc = peer_is_invoker (client);
788 if (rc)
789 return rc;
792 client->opts |= opt;
793 return 0;
796 static gpg_error_t
797 parse_save_opt_inquire_keyparam (void *data, void *value)
799 return parse_save_opt_inquire (data, OPT_INQUIRE);
802 static gpg_error_t
803 parse_save_opt_inquire_keyid (void *data, void *value)
805 return parse_save_opt_inquire (data, OPT_INQUIRE_KEYID);
808 static gpg_error_t
809 parse_save_opt_inquire_sign_keyid (void *data, void *value)
811 return parse_save_opt_inquire (data, OPT_INQUIRE_SIGN_KEYID);
814 static gpg_error_t
815 parse_save_opt_symmetric (void *data, void *value)
817 struct client_s *client = data;
819 client->opts |= OPT_SYMMETRIC;
820 return 0;
823 /* Tests that the keys in new_keys are also in old_keys. */
824 static gpg_error_t
825 compare_keys (char **new_keys, char **old_keys)
827 char **o;
829 if (!old_keys || !*old_keys)
830 return 0;
832 crypto_keyid_to_16b (new_keys);
834 for (o = old_keys; *o; o++)
836 char **n;
838 for (n = new_keys; *n; n++)
840 if (!strcmp (*n, *o))
841 break;
844 if (!*n)
845 return GPG_ERR_NOT_FOUND;
848 return 0;
851 static gpg_error_t
852 inquire_keyid (struct client_s *client, uint32_t opt)
854 gpg_error_t rc;
855 unsigned char *result = NULL;
856 size_t len;
857 char *s;
858 char ***orig;
859 char ***save;
861 if (opt == OPT_INQUIRE_KEYID)
863 s = "INQUIRE_KEYID";
864 orig = &client->crypto->pubkey;
865 save = &client->crypto->save.pubkey;
867 else
869 s = "INQUIRE_SIGN_KEYID";
870 orig = &client->crypto->sigkey;
871 save = &client->crypto->save.sigkey;
874 rc = assuan_inquire (client->ctx, s, &result, &len, 0);
875 if (!rc)
877 char **dst = NULL;
879 rc = parse_save_opt_keyid_common (client, (const char **)*orig,
880 (char *)result, &dst);
881 if (!rc)
883 if (!dst && opt == OPT_INQUIRE_SIGN_KEYID
884 && client->opts & OPT_SYMMETRIC)
885 client->opts |= OPT_NO_SIGNER;
887 *save = dst;
891 xfree (result);
892 return rc;
895 /* The caching test of gpg-agent is both a blessing and a curse. When a key
896 * lookup in its cache fails it tries the last successful key to be unlocked.
897 * This prevents pwmd from successfully clearing a signing key since the
898 * previous key, which more than likely belongs to the same keypair as the
899 * encryption/decryption key, will have the passphrase cached and therefore the
900 * signing key also cached. So we need to clear both the signing and encryption
901 * keys to get the effect of requiring a passphrase when generating a new
902 * keypair for an existing data file, or when the "require_save_key"
903 * configuration parameter is set. The "require_save_key" parameter is mostly a
904 * failsafe of the gpg-agent option --ignore-cache-for-signing since
905 * some/most/all users may fail to set it.
907 static gpg_error_t
908 save_command (assuan_context_t ctx, char *line)
910 struct client_s *client = assuan_get_pointer (ctx);
911 struct cache_data_s *cdata = NULL;
912 gpg_error_t rc = 0, cached = 0;
913 int defer = 0;
914 struct stat st;
915 struct argv_s *args[] = {
916 &(struct argv_s) {"keyid", OPTION_TYPE_ARG, parse_save_opt_keyid},
917 &(struct argv_s) {"sign-keyid", OPTION_TYPE_OPTARG,
918 parse_save_opt_sign_keyid},
919 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
920 parse_save_opt_inquire_keyparam },
921 &(struct argv_s) {"inquire-keyid", OPTION_TYPE_NOARG,
922 parse_save_opt_inquire_keyid },
923 &(struct argv_s) {"inquire-sign-keyid", OPTION_TYPE_NOARG,
924 parse_save_opt_inquire_sign_keyid },
925 &(struct argv_s) {"symmetric", OPTION_TYPE_NOARG,
926 parse_save_opt_symmetric },
927 NULL
930 crypto_free_save (&client->crypto->save);
932 if (!(client->flags & FLAG_NEW))
934 rc = crypto_is_symmetric (client->filename);
935 if (!rc || rc == GPG_ERR_BAD_DATA)
937 if (!rc)
938 client->opts |= OPT_SYMMETRIC;
940 rc = 0; // PKI
944 if (rc)
945 return send_error (ctx, rc);
947 rc = parse_options (&line, args, client, 0);
948 if (rc)
949 return send_error (ctx, rc);
951 if (config_get_boolean (client->filename, "require_save_key"))
952 client->opts |= OPT_ASK;
954 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
955 return send_error (ctx, gpg_error_from_errno (errno));
957 if (errno != ENOENT && !S_ISREG (st.st_mode))
959 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
960 return send_error (ctx, GPG_ERR_ENOANO);
963 if (!rc)
964 cached = rc = cache_iscached (client->filename, &defer, 0, 1);
966 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
967 rc = 0;
968 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
969 rc = 0;
971 if (rc)
972 return send_error (ctx, rc);
974 /* Specifying both a recipient and symmetric encryption is an error. */
975 if (client->opts & OPT_INQUIRE_KEYID && client->opts & OPT_SYMMETRIC)
977 return send_error (ctx, GPG_ERR_CONFLICT);
979 else if (client->opts & OPT_INQUIRE && client->opts & OPT_SYMMETRIC)
981 return send_error (ctx, GPG_ERR_CONFLICT);
983 /* Existing file with a recipient and wanting symmetric is an error. */
984 else if (client->opts & OPT_SYMMETRIC && client->crypto->save.pubkey)
986 return send_error (ctx, GPG_ERR_CONFLICT);
988 else if (((client->opts & OPT_INQUIRE_KEYID)
989 || (client->opts & OPT_INQUIRE_SIGN_KEYID)))
991 if (client->opts & OPT_INQUIRE)
992 return send_error (ctx, GPG_ERR_CONFLICT);
994 if (client->opts & OPT_INQUIRE_KEYID)
995 rc = inquire_keyid (client, OPT_INQUIRE_KEYID);
997 if (!rc && (client->opts & OPT_INQUIRE_SIGN_KEYID))
998 rc = inquire_keyid (client, OPT_INQUIRE_SIGN_KEYID);
1000 else if ((client->crypto->save.pubkey || client->crypto->save.sigkey)
1001 && client->opts & OPT_INQUIRE)
1002 return send_error (ctx, GPG_ERR_CONFLICT);
1004 if (!rc)
1006 client->crypto->keyfile = config_get_string (client->filename,
1007 "passphrase_file");
1008 rc = crypto_init_ctx (client->crypto, (client->flags & FLAG_NO_PINENTRY),
1009 client->crypto->keyfile);
1012 if (!rc && (client->opts & OPT_INQUIRE))
1014 /* Require a passphrase when generating a new key pair for an existing
1015 * file.
1017 if (!(client->flags & FLAG_NEW))
1019 rc = cache_clear_agent_keys (client->filename, 1, 1);
1020 if (!rc)
1022 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
1023 client->opts &= ~OPT_ASK;
1027 if (!rc)
1029 unsigned char *params = NULL;
1030 size_t len;
1032 rc = assuan_inquire (client->ctx, "KEYPARAM", &params, &len, 0);
1033 if (!rc)
1035 client->crypto->flags |= CRYPTO_FLAG_NEWFILE;
1036 pthread_cleanup_push ((void *)xfree, params);
1037 rc = crypto_genkey (client, client->crypto, params);
1038 pthread_cleanup_pop (1);
1042 else if (!rc && (client->flags & FLAG_NEW))
1044 if (!client->crypto->save.pubkey && !client->crypto->save.sigkey
1045 && !(client->opts & OPT_SYMMETRIC))
1047 char *params = crypto_default_key_params ();
1049 if (!params)
1050 rc = GPG_ERR_ENOMEM;
1052 if (!rc)
1054 client->crypto->flags |= CRYPTO_FLAG_NEWFILE;
1055 pthread_cleanup_push ((void *)xfree, params);
1056 rc = crypto_genkey (client, client->crypto,
1057 (unsigned char *)params);
1058 pthread_cleanup_pop (1);
1061 else
1063 if (!client->crypto->save.pubkey && client->crypto->save.sigkey
1064 && !(client->opts & OPT_SYMMETRIC))
1065 client->crypto->save.pubkey = strv_dup (client->crypto->save.sigkey);
1067 if (client->crypto->save.pubkey && !client->crypto->save.sigkey)
1068 client->crypto->save.sigkey = strv_dup (client->crypto->save.pubkey);
1071 else if (!rc)
1073 cdata = cache_get_data (client->filename);
1074 if (cdata)
1076 if (client->crypto->save.pubkey)
1077 rc = compare_keys (client->crypto->save.pubkey, cdata->pubkey);
1079 /* Always allow a signer for symmetric data files. */
1080 if (!rc && client->crypto->save.sigkey
1081 && !(client->opts & OPT_SYMMETRIC))
1082 rc = compare_keys (client->crypto->save.sigkey, cdata->sigkey);
1084 /* Prevent saving to a recipient who is not in the original recipient
1085 * list without a passphrase. */
1086 if (rc || (client->crypto->sigkey && client->opts & OPT_NO_SIGNER))
1087 client->opts |= OPT_ASK;
1089 if (rc == GPG_ERR_NOT_FOUND)
1091 rc = peer_is_invoker (client);
1092 if (rc == GPG_ERR_EACCES)
1093 rc = GPG_ERR_FORBIDDEN;
1096 if (!client->crypto->save.pubkey
1097 && !(client->opts & OPT_SYMMETRIC))
1098 client->crypto->save.pubkey = strv_dup (cdata->pubkey);
1100 if (!rc && !client->crypto->save.sigkey && cdata->sigkey
1101 && !(client->opts & OPT_NO_SIGNER))
1102 client->crypto->save.sigkey = strv_dup (cdata->sigkey);
1103 else if (!rc && !client->crypto->save.sigkey
1104 && (client->opts & OPT_NO_SIGNER)
1105 && !(client->opts & OPT_SYMMETRIC))
1106 rc = GPG_ERR_NO_SIGNATURE_SCHEME;
1108 else
1110 client->crypto->save.pubkey = strv_dup (client->crypto->pubkey);
1111 client->crypto->save.sigkey = strv_dup (client->crypto->sigkey);
1115 if (!rc && !(client->flags & FLAG_NEW))
1117 /* Fixes {NEW_,SIGN_}PASSPHRASE inquire keywords. See passphrase_cb(). */
1118 if (client->opts & OPT_SYMMETRIC)
1119 client->crypto->flags |= CRYPTO_FLAG_PASSWD;
1121 if (client->opts & OPT_ASK)
1123 rc = cache_clear_agent_keys (client->filename, 1, 1);
1124 if (!rc)
1125 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
1129 if (!rc && client->opts & OPT_SYMMETRIC)
1130 client->crypto->flags |= CRYPTO_FLAG_PASSWD_NEW;
1132 if (!rc)
1133 rc = xml_update_element_mtime (client, xmlDocGetRootElement (client->doc));
1135 if (!rc)
1137 int size;
1139 xmlDocDumpFormatMemory (client->doc, &client->crypto->plaintext, &size,
1141 if (size > 0)
1142 client->crypto->plaintext_size = (size_t) size;
1143 else
1144 rc = GPG_ERR_ENOMEM;
1146 if (!rc)
1148 client->crypto->flags |= (client->opts & OPT_SYMMETRIC) ? CRYPTO_FLAG_SYMMETRIC : 0;
1149 client->crypto->flags |= (client->flags & FLAG_NEW) ? CRYPTO_FLAG_NEWFILE : 0;
1150 client->crypto->flags |= !(client->opts & OPT_SYMMETRIC) ? CRYPTO_FLAG_PASSWD_SIGN : 0;
1151 rc = crypto_encrypt (client, client->crypto);
1152 client->crypto->flags &= ~CRYPTO_FLAG_SYMMETRIC;
1155 if (!rc)
1157 rc = crypto_write_file (client->crypto);
1158 if (!rc)
1160 if (!cached) // no error
1161 cdata = cache_get_data (client->filename);
1164 if (!rc)
1166 rc = cache_encrypt (client->crypto);
1167 if (rc)
1169 cache_clear (NULL, client->filename, 1);
1170 strv_free (client->crypto->pubkey);
1171 client->crypto->pubkey = strv_dup (client->crypto->save.pubkey);
1172 strv_free (client->crypto->sigkey);
1173 client->crypto->sigkey = strv_dup (client->crypto->save.sigkey);
1174 client->flags &= ~(FLAG_NEW);
1178 if (!rc)
1180 int timeout = config_get_integer (client->filename,
1181 "cache_timeout");
1183 cache_free_data_once (cdata);
1184 cdata = xcalloc (1, sizeof (struct cache_data_s));
1185 cdata->doc = client->crypto->plaintext;
1186 client->crypto->plaintext = NULL;
1187 cdata->size = client->crypto->plaintext_size;
1188 client->crypto->plaintext_size = 0;
1189 cdata->pubkey = client->crypto->save.pubkey;
1190 client->crypto->save.pubkey = NULL;
1191 cdata->sigkey = client->crypto->save.sigkey;
1192 client->crypto->save.sigkey = NULL;
1194 /* Update in case the cache entry expires the next SAVE may not
1195 * have any known keys. */
1196 strv_free (client->crypto->pubkey);
1197 client->crypto->pubkey = strv_dup (cdata->pubkey);
1198 strv_free (client->crypto->sigkey);
1199 client->crypto->sigkey = strv_dup (cdata->sigkey);
1201 if (!cached) // no error and wont increment refcount
1202 rc = cache_set_data (client->filename, cdata);
1203 else
1204 rc = cache_add_file (client->filename, cdata, timeout);
1206 if (!rc && (cached || (client->flags & FLAG_NEW)))
1207 send_status_all (STATUS_CACHE, NULL);
1209 if (!rc)
1211 rc = update_checksum (client);
1212 client->flags &= ~(FLAG_NEW);
1218 crypto_free_non_keys (client->crypto);
1219 return send_error (ctx, rc);
1222 static gpg_error_t
1223 do_delete (assuan_context_t ctx, char *line)
1225 struct client_s *client = assuan_get_pointer (ctx);
1226 struct xml_request_s *req;
1227 xmlNodePtr n;
1228 gpg_error_t rc;
1230 rc = xml_new_request (client, line, XML_CMD_DELETE, &req);
1231 if (rc)
1232 return rc;
1234 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1235 xml_free_request (req);
1236 if (rc)
1237 return rc;
1239 rc = xml_is_element_owner (client, n);
1240 if (!rc)
1241 rc = xml_unlink_node (client, n);
1243 return rc;
1246 static gpg_error_t
1247 delete_command (assuan_context_t ctx, char *line)
1249 struct client_s *client = assuan_get_pointer (ctx);
1250 gpg_error_t rc;
1251 struct argv_s *args[] = {
1252 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1253 NULL
1256 rc = parse_options (&line, args, client, 1);
1257 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1258 rc = GPG_ERR_SYNTAX;
1259 if (rc)
1260 return send_error (ctx, rc);
1262 if (client->opts & OPT_INQUIRE)
1264 unsigned char *result;
1265 size_t len;
1267 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1268 if (rc)
1269 return send_error (ctx, rc);
1271 pthread_cleanup_push ((void *)xfree, result);
1272 rc = do_delete (ctx, (char *)result);
1273 pthread_cleanup_pop (1);
1275 else
1276 rc = do_delete (ctx, line);
1278 return send_error (ctx, rc);
1281 static gpg_error_t
1282 update_element_expirey (struct client_s *client, xmlNodePtr n)
1284 gpg_error_t rc = 0;
1285 xmlChar *expire, *incr;
1286 char *p;
1287 time_t e, e_orig, i;
1288 time_t now = time (NULL);
1290 expire = xml_attribute_value (n, (xmlChar *)"expire");
1291 if (!expire)
1292 return 0;
1294 errno = 0;
1295 e = strtoul ((char *)expire, &p, 10);
1296 if (errno || *p || e == ULONG_MAX || *expire == '-')
1298 xmlFree (expire);
1299 rc = GPG_ERR_ERANGE;
1300 goto fail;
1303 xmlFree (expire);
1304 incr = xml_attribute_value (n, (xmlChar *)"expire_increment");
1305 if (!incr)
1306 return 0;
1308 i = strtoul ((char *)incr, &p, 10);
1309 if (errno || *p || i == ULONG_MAX || *incr == '-')
1311 xmlFree (incr);
1312 rc = GPG_ERR_ERANGE;
1313 goto fail;
1316 xmlFree (incr);
1317 e_orig = e;
1318 e = now + i;
1319 p = str_asprintf ("%lu", e);
1320 if (!p)
1322 rc = GPG_ERR_ENOMEM;
1323 goto fail;
1326 rc = xml_add_attribute (client, n, "expire", p);
1327 xfree (p);
1328 if (!rc)
1329 rc = send_status (client->ctx, STATUS_EXPIRE, "%lu %lu", e_orig, e);
1331 fail:
1332 return rc;
1335 static gpg_error_t
1336 store_command (assuan_context_t ctx, char *line)
1338 struct client_s *client = assuan_get_pointer (ctx);
1339 gpg_error_t rc;
1340 size_t len;
1341 unsigned char *result;
1342 xmlNodePtr n, parent;
1343 int has_content;
1344 char *content = NULL;
1345 struct xml_request_s *req;
1347 if (line && *line)
1348 return send_error (ctx, GPG_ERR_SYNTAX);
1350 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1351 if (rc)
1352 return send_error (ctx, rc);
1354 rc = xml_new_request (client, (char *)result, XML_CMD_STORE, &req);
1355 xfree (result);
1356 if (rc)
1357 return send_error (ctx, rc);
1359 /* Prevent passing the element content around to save some memory. */
1360 len = strv_length (req->args);
1361 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1362 if (*(req->args+1) && !xml_valid_element_path (req->args, has_content))
1364 xml_free_request (req);
1365 return send_error (ctx, GPG_ERR_INV_VALUE);
1368 if (has_content || !*req->args[len-1])
1370 has_content = 1;
1371 content = req->args[len-1];
1372 req->args[len-1] = NULL;
1375 if (strv_length (req->args) > 1)
1377 rc = xml_check_recursion (client, req);
1378 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
1379 goto fail;
1382 parent = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1384 if (!rc && len > 1)
1386 rc = xml_is_element_owner (client, parent);
1387 if (!rc)
1389 n = xml_find_text_node (parent->children);
1390 if (n)
1391 xmlNodeSetContent (n, (xmlChar *) content);
1392 else
1393 xmlNodeAddContent (parent, (xmlChar *) content);
1395 (void)xml_update_element_mtime (client, parent);
1396 (void)update_element_expirey (client, parent);
1400 fail:
1401 xfree (content);
1402 xml_free_request (req);
1403 return send_error (ctx, rc);
1406 static gpg_error_t
1407 xfer_data (assuan_context_t ctx, const char *line, int total)
1409 struct client_s *client = assuan_get_pointer (ctx);
1410 int to_send;
1411 int sent = 0;
1412 gpg_error_t rc = 0;
1413 int progress = config_get_integer ("global", "xfer_progress");
1414 int flush = 0;
1416 if (!(client->flags & FLAG_LOCK_CMD))
1417 rc = unlock_file_mutex (client, 0);
1418 if (rc && rc != GPG_ERR_NOT_LOCKED)
1419 return rc;
1421 progress =
1422 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1423 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1424 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1426 if (rc)
1427 return rc;
1429 again:
1432 if (sent + to_send > total)
1433 to_send = total - sent;
1435 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1436 flush ? 0 : to_send);
1437 if (!rc)
1439 sent += flush ? 0 : to_send;
1441 if ((progress && !(sent % progress) && sent != total) ||
1442 (sent == total && flush))
1443 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1445 if (!flush && !rc && sent == total)
1447 flush = 1;
1448 goto again;
1452 while (!rc && sent < total);
1454 return rc;
1457 static gpg_error_t
1458 do_get (assuan_context_t ctx, char *line)
1460 struct client_s *client = assuan_get_pointer (ctx);
1461 gpg_error_t rc;
1462 struct xml_request_s *req;
1463 xmlNodePtr n;
1465 rc = xml_new_request (client, line, XML_CMD_NONE, &req);
1466 if (rc)
1467 return rc;
1469 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1470 if (!rc)
1472 if (n && n->children)
1474 xmlNodePtr tmp = n;
1476 n = xml_find_text_node (n->children);
1477 if (!n || !n->content || !*n->content)
1478 rc = GPG_ERR_NO_DATA;
1479 else
1481 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1482 if (!rc)
1484 xmlChar *expire = xml_attribute_value (tmp,
1485 (xmlChar *)"expire");
1486 if (expire)
1488 time_t now = time (NULL);
1489 time_t e;
1490 char *p;
1492 errno = 0;
1493 e = strtoul ((char *)expire, &p, 10);
1494 if (errno || *p || e == ULONG_MAX || *expire == '-')
1495 log_write (_("invalid expire attribute value: %s"),
1496 expire);
1497 else if (now >= e)
1498 rc = send_status (ctx, STATUS_EXPIRE, "%lu 0", e);
1500 xmlFree (expire);
1505 else
1506 rc = GPG_ERR_NO_DATA;
1509 xml_free_request (req);
1510 return rc;
1513 static gpg_error_t
1514 get_command (assuan_context_t ctx, char *line)
1516 struct client_s *client = assuan_get_pointer (ctx);
1517 gpg_error_t rc;
1518 struct argv_s *args[] = {
1519 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1520 NULL
1523 rc = parse_options (&line, args, client, 1);
1524 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1525 rc = GPG_ERR_SYNTAX;
1526 if (rc)
1527 return send_error (ctx, rc);
1529 if (client->opts & OPT_INQUIRE)
1531 unsigned char *result;
1532 size_t len;
1534 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1535 if (rc)
1536 return send_error (ctx, rc);
1538 pthread_cleanup_push ((void *)xfree, result);
1539 rc = do_get (ctx, (char *)result);
1540 pthread_cleanup_pop (1);
1542 else
1543 rc = do_get (ctx, line);
1545 return send_error (ctx, rc);
1548 static void list_command_free1 (void *arg);
1549 static gpg_error_t
1550 realpath_command (assuan_context_t ctx, char *line)
1552 gpg_error_t rc;
1553 char **realpath = NULL;
1554 struct client_s *client = assuan_get_pointer (ctx);
1555 struct argv_s *args[] = {
1556 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1557 NULL
1560 rc = parse_options (&line, args, client, 1);
1561 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1562 rc = GPG_ERR_SYNTAX;
1563 if (rc)
1564 return send_error (ctx, rc);
1566 if (client->opts & OPT_INQUIRE)
1568 unsigned char *result;
1569 size_t len;
1571 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1572 if (rc)
1573 return send_error (ctx, rc);
1575 pthread_cleanup_push ((void *)xfree, result);
1576 (void)xml_resolve_path (client, client->doc, result, &realpath, &rc);
1577 pthread_cleanup_pop (1);
1579 else
1580 (void)xml_resolve_path (client, client->doc, (unsigned char *)line,
1581 &realpath, &rc);
1583 if (!rc)
1585 char *tmp = strv_join ("\t", realpath);
1587 strv_free (realpath);
1588 if (!tmp)
1589 return send_error (ctx, GPG_ERR_ENOMEM);
1591 pthread_cleanup_push ((void *)xfree, tmp);
1592 rc = xfer_data (ctx, tmp, strlen (tmp));
1593 pthread_cleanup_pop (1);
1596 return send_error (ctx, rc);
1599 static void
1600 list_command_free1 (void *arg)
1602 if (arg)
1603 string_free ((struct string_s *) arg, 1);
1606 static gpg_error_t
1607 parse_list_opt_recurse (void *data, void *value)
1609 struct client_s *client = data;
1611 client->opts |= OPT_LIST_RECURSE;
1612 return 0;
1615 static gpg_error_t
1616 parse_opt_verbose (void *data, void *value)
1618 struct client_s *client = data;
1620 client->opts |= OPT_VERBOSE;
1621 return 0;
1624 static gpg_error_t
1625 list_path_once (struct client_s *client, char *line,
1626 struct element_list_s *elements, struct string_s *result)
1628 gpg_error_t rc;
1630 rc = xml_create_path_list (client, client->doc, NULL, elements, line, 0);
1631 if (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES)
1632 rc = 0;
1634 if (!rc)
1636 int total = slist_length (elements->list);
1638 if (total)
1640 int i;
1642 for (i = 0; i < total; i++)
1644 char *tmp = slist_nth_data (elements->list, i);
1646 string_append_printf (result, "%s%s", tmp,
1647 i + 1 == total ? "" : "\n");
1650 else
1651 rc = GPG_ERR_NO_DATA;
1654 return rc;
1657 static gpg_error_t
1658 do_list (assuan_context_t ctx, char *line)
1660 struct client_s *client = assuan_get_pointer (ctx);
1661 gpg_error_t rc = GPG_ERR_NO_DATA;
1662 struct element_list_s *elements = NULL;
1664 if (!line || !*line)
1666 struct string_s *str = string_new (NULL);
1667 xmlNodePtr n;
1669 if (!str)
1670 return GPG_ERR_ENOMEM;
1672 pthread_cleanup_push ((void *)list_command_free1, str);
1673 n = xmlDocGetRootElement (client->doc);
1674 for (n = n->children; n; n = n->next)
1676 xmlChar *name;
1678 if (n->type != XML_ELEMENT_NODE)
1679 continue;
1681 name = xmlGetProp (n, (xmlChar *)"_name");
1682 if (!name)
1684 rc = GPG_ERR_ENOMEM;
1685 break;
1688 elements = xcalloc (1, sizeof (struct element_list_s));
1689 if (!elements)
1691 xmlFree (name);
1692 rc = GPG_ERR_ENOMEM;
1693 break;
1696 elements->prefix = string_new (NULL);
1697 if (!elements->prefix)
1699 xmlFree (name);
1700 xml_free_element_list (elements);
1701 rc = GPG_ERR_ENOMEM;
1702 break;
1705 elements->recurse = client->opts & OPT_LIST_RECURSE;
1706 elements->root_only = !elements->recurse;
1707 pthread_cleanup_push ((void *)xml_free_element_list, elements);
1708 rc = list_path_once (client, (char *)name, elements, str);
1709 xmlFree (name);
1710 pthread_cleanup_pop (1);
1711 if (rc)
1712 break;
1714 if (n->next)
1715 string_append (str, "\n");
1718 if (!rc)
1719 rc = xfer_data (ctx, str->str, str->len);
1721 pthread_cleanup_pop (1);
1722 return rc;
1725 elements = xcalloc (1, sizeof (struct element_list_s));
1726 if (!elements)
1727 return GPG_ERR_ENOMEM;
1729 elements->prefix = string_new (NULL);
1730 if (!elements->prefix)
1732 xml_free_element_list (elements);
1733 return GPG_ERR_ENOMEM;
1736 elements->recurse = client->opts & OPT_LIST_RECURSE;
1737 pthread_cleanup_push ((void *)xml_free_element_list, elements);
1738 struct string_s *str = string_new (NULL);
1739 pthread_cleanup_push ((void *)list_command_free1, str);
1740 rc = list_path_once (client, line, elements, str);
1741 if (!rc)
1742 rc = xfer_data (ctx, str->str, str->len);
1744 pthread_cleanup_pop (1);
1745 pthread_cleanup_pop (1);
1746 return rc;
1749 static gpg_error_t
1750 list_command (assuan_context_t ctx, char *line)
1752 struct client_s *client = assuan_get_pointer (ctx);
1753 gpg_error_t rc;
1754 struct argv_s *args[] = {
1755 &(struct argv_s) {"recurse", OPTION_TYPE_NOARG, parse_list_opt_recurse},
1756 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1757 /* These are deprecated but kept for backward compatibility with older
1758 * clients. */
1759 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, NULL},
1760 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG, NULL},
1761 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_recurse},
1762 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG, NULL},
1763 NULL
1766 if (disable_list_and_dump == 1)
1767 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1769 rc = parse_options (&line, args, client, 1);
1770 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1771 rc = GPG_ERR_SYNTAX;
1772 if (rc)
1773 return send_error (ctx, rc);
1775 if (client->opts & OPT_INQUIRE)
1777 unsigned char *result;
1778 size_t len;
1780 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1781 if (rc)
1782 return send_error (ctx, rc);
1784 pthread_cleanup_push ((void *)xfree, result);
1785 rc = do_list (ctx, (char *)result);
1786 pthread_cleanup_pop (1);
1788 else
1789 rc = do_list (ctx, line);
1791 return send_error (ctx, rc);
1794 #define RESUMABLE_ERROR(rc) (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES \
1795 || rc == GPG_ERR_ELEMENT_NOT_FOUND || !rc)
1797 * args[0] - element path
1799 static gpg_error_t
1800 attribute_list (assuan_context_t ctx, char **args)
1802 struct client_s *client = assuan_get_pointer (ctx);
1803 char **attrlist = NULL;
1804 int i = 0;
1805 int target = 0;
1806 xmlAttrPtr a;
1807 xmlNodePtr n, an, r;
1808 char *line;
1809 gpg_error_t rc;
1810 struct xml_request_s *req;
1812 if (!args || !args[0] || !*args[0])
1813 return GPG_ERR_SYNTAX;
1815 rc = xml_new_request (client, args[0], XML_CMD_ATTR_LIST, &req);
1816 if (rc)
1817 return rc;
1819 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1820 xml_free_request (req);
1821 if (rc)
1822 return rc;
1824 again:
1825 for (a = n->properties; a; a = a->next)
1827 char **pa;
1829 if (target && xml_reserved_attribute ((char *)a->name))
1830 continue;
1832 pa = xrealloc (attrlist, (i + 2) * sizeof (char *));
1833 if (!pa)
1835 if (attrlist)
1836 strv_free (attrlist);
1838 log_write ("%s(%i): %s", __FILE__, __LINE__,
1839 pwmd_strerror (GPG_ERR_ENOMEM));
1840 return GPG_ERR_ENOMEM;
1843 attrlist = pa;
1844 an = a->children;
1845 attrlist[i] = str_asprintf ("%s %s", (char *) a->name, an && an->content
1846 ? (char *)an->content : "");
1848 if (!attrlist[i])
1850 strv_free (attrlist);
1851 log_write ("%s(%i): %s", __FILE__, __LINE__,
1852 pwmd_strerror (GPG_ERR_ENOMEM));
1853 return GPG_ERR_ENOMEM;
1856 attrlist[++i] = NULL;
1859 if (!attrlist)
1860 return GPG_ERR_NO_DATA;
1862 if (!target)
1864 r = xml_resolve_path (client, client->doc, (xmlChar *)args[0], NULL, &rc);
1865 if (!RESUMABLE_ERROR (rc))
1867 strv_free (attrlist);
1868 return rc;
1870 else if (!rc && r != n)
1872 target = 1;
1873 n = r;
1874 goto again;
1877 rc = 0;
1880 line = strv_join ("\n", attrlist);
1881 if (!line)
1883 log_write ("%s(%i): %s", __FILE__, __LINE__,
1884 pwmd_strerror (GPG_ERR_ENOMEM));
1885 strv_free (attrlist);
1886 return GPG_ERR_ENOMEM;
1889 pthread_cleanup_push ((void *)xfree, line);
1890 pthread_cleanup_push ((void *)req_free, attrlist);
1891 rc = xfer_data (ctx, line, strlen (line));
1892 pthread_cleanup_pop (1);
1893 pthread_cleanup_pop (1);
1894 return rc;
1898 * args[0] - attribute
1899 * args[1] - element path
1901 static gpg_error_t
1902 attribute_delete (struct client_s *client, char **args)
1904 gpg_error_t rc;
1905 xmlNodePtr n;
1907 if (!args || !args[0] || !*args[0] || !args[1] || !*args[1])
1908 return GPG_ERR_SYNTAX;
1910 if (!strcmp (args[0], "_name") || !strcmp (args[0], "target"))
1911 return GPG_ERR_INV_ATTR;
1913 if (!xml_reserved_attribute (args[0]))
1914 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
1915 else
1917 struct xml_request_s *req;
1919 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
1920 if (rc)
1921 return rc;
1923 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1924 xml_free_request (req);
1927 if (!rc)
1928 rc = xml_is_element_owner (client, n);
1930 if (!rc)
1931 rc = xml_delete_attribute (client, n, (xmlChar *) args[0]);
1933 return rc;
1937 * Creates the "target" attribute. When other commands encounter an element
1938 * with this attribute they follow the "target" after appending remaining
1939 * elements from the original element path to the target. The exception is the
1940 * ATTR command that operates on the element itself (for reserved attribute
1941 * names) and not the target.
1943 * If the source element path doesn't exist when using 'ATTR SET target', it is
1944 * created, but the destination element path must exist. This is simliar to the
1945 * ls(1) command: ln -s src dst.
1947 * args[0] - source element path
1948 * args[1] - destination element path
1950 static gpg_error_t
1951 set_target_attribute (struct client_s *client, char **args)
1953 struct xml_request_s *req = NULL;
1954 char **src, **dst;
1955 gpg_error_t rc;
1956 xmlNodePtr n;
1958 if (!args || !args[0] || !args[1])
1959 return GPG_ERR_SYNTAX;
1961 src = str_split (args[0], "\t", 0);
1962 if (!src)
1963 return GPG_ERR_SYNTAX;
1965 if (!xml_valid_element_path (src, 0))
1967 strv_free (src);
1968 return GPG_ERR_INV_VALUE;
1971 dst = str_split (args[1], "\t", 0);
1972 if (!dst)
1974 strv_free (src);
1975 return GPG_ERR_SYNTAX;
1978 // Be sure the destination element path exists. */
1979 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
1980 if (rc)
1981 goto fail;
1983 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1984 if (rc && rc != GPG_ERR_EACCES)
1985 goto fail;
1987 xml_free_request (req);
1988 req = NULL;
1990 if (!strcmp (args[0], args[1]))
1992 rc = GPG_ERR_EEXIST;
1993 goto fail;
1996 rc = xml_new_request (client, args[0], XML_CMD_ATTR_TARGET, &req);
1997 if (rc)
1998 goto fail;
2000 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2001 if (rc && rc != GPG_ERR_EACCES)
2002 goto fail;
2004 if (!rc)
2006 rc = xml_add_attribute (client, n, "target", args[1]);
2007 if (!rc)
2008 rc = xml_unlink_node (client, n->children);
2011 fail:
2012 strv_free (src);
2013 strv_free (dst);
2014 xml_free_request (req);
2015 return rc;
2019 * args[0] - attribute
2020 * args[1] - element path
2022 static gpg_error_t
2023 attribute_get (assuan_context_t ctx, char **args)
2025 struct client_s *client = assuan_get_pointer (ctx);
2026 xmlNodePtr n;
2027 xmlChar *a;
2028 gpg_error_t rc;
2029 struct xml_request_s *req;
2031 if (!args || !args[0] || !*args[0] || !args[1] || !*args[1])
2032 return GPG_ERR_SYNTAX;
2034 if (!xml_reserved_attribute (args[0]))
2035 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
2036 else
2038 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
2039 if (rc)
2040 return rc;
2042 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2043 xml_free_request (req);
2046 if (rc)
2047 return rc;
2049 a = xmlGetProp (n, (xmlChar *) args[0]);
2050 if (!a)
2051 return GPG_ERR_NOT_FOUND;
2053 pthread_cleanup_push ((void *)xmlFree, a);
2055 if (*a)
2056 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
2057 else
2058 rc = GPG_ERR_NO_DATA;
2060 pthread_cleanup_pop (1);
2061 return rc;
2065 * args[0] - attribute
2066 * args[1] - element path
2067 * args[2] - value
2069 static gpg_error_t
2070 attribute_set (struct client_s *client, char **args)
2072 struct xml_request_s *req;
2073 gpg_error_t rc;
2074 xmlNodePtr n;
2076 if (!args || !args[0] || !args[1])
2077 return GPG_ERR_SYNTAX;
2080 * Reserved attribute names.
2082 if (!strcmp (args[0], "_name")) // Use the RENAME command instead
2083 return GPG_ERR_INV_ATTR;
2084 else if (!strcmp (args[0], "target"))
2085 return set_target_attribute (client, args + 1);
2086 else if (!xml_valid_attribute (args[0]))
2087 return GPG_ERR_INV_VALUE;
2089 if (!xml_valid_attribute_value (args[2]))
2090 return GPG_ERR_INV_VALUE;
2092 if (!xml_reserved_attribute (args[0]))
2094 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
2095 if (!rc)
2097 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
2098 if (!rc)
2100 rc = xml_check_recursion (client, req);
2101 xml_free_request (req);
2105 else
2107 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
2108 if (rc)
2109 return rc;
2111 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2112 xml_free_request (req);
2115 if (!rc)
2116 rc = xml_is_element_owner (client, n);
2118 if (!rc)
2119 rc = xml_add_attribute (client, n, args[0], args[2]);
2121 return rc;
2125 * req[0] - command
2126 * req[1] - attribute name or element path if command is LIST
2127 * req[2] - element path
2128 * req[2] - element path or value
2131 static gpg_error_t
2132 do_attr (assuan_context_t ctx, char *line)
2134 struct client_s *client = assuan_get_pointer (ctx);
2135 gpg_error_t rc = 0;
2136 char **req;
2138 req = str_split (line, " ", 4);
2139 if (!req || !req[0] || !req[1])
2141 strv_free (req);
2142 return GPG_ERR_SYNTAX;
2145 pthread_cleanup_push ((void *)req_free, req);
2147 if (strcasecmp (req[0], "SET") == 0)
2148 rc = attribute_set (client, req + 1);
2149 else if (strcasecmp (req[0], "GET") == 0)
2150 rc = attribute_get (ctx, req + 1);
2151 else if (strcasecmp (req[0], "DELETE") == 0)
2152 rc = attribute_delete (client, req + 1);
2153 else if (strcasecmp (req[0], "LIST") == 0)
2154 rc = attribute_list (ctx, req + 1);
2155 else
2156 rc = GPG_ERR_SYNTAX;
2158 client->flags &= ~(FLAG_ACL_IGNORE|FLAG_ACL_ERROR);
2159 pthread_cleanup_pop (1);
2160 return rc;
2163 static gpg_error_t
2164 attr_command (assuan_context_t ctx, char *line)
2166 struct client_s *client = assuan_get_pointer (ctx);
2167 gpg_error_t rc;
2168 struct argv_s *args[] = {
2169 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2170 NULL
2173 rc = parse_options (&line, args, client, 1);
2174 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2175 rc = GPG_ERR_SYNTAX;
2176 if (rc)
2177 return send_error (ctx, rc);
2179 if (client->opts & OPT_INQUIRE)
2181 unsigned char *result;
2182 size_t len;
2184 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2185 if (rc)
2186 return send_error (ctx, rc);
2188 pthread_cleanup_push ((void *)xfree, result);
2189 rc = do_attr (ctx, (char *)result);
2190 pthread_cleanup_pop (1);
2192 else
2193 rc = do_attr (ctx, line);
2195 return send_error (ctx, rc);
2198 static gpg_error_t
2199 parse_iscached_opt_lock (void *data, void *value)
2201 struct client_s *client = data;
2203 (void) value;
2204 client->opts |= OPT_LOCK;
2205 return 0;
2208 static gpg_error_t
2209 parse_iscached_opt_agent (void *data, void *value)
2211 struct client_s *client = data;
2213 (void) value;
2214 client->opts |= OPT_CACHE_AGENT;
2215 return 0;
2218 static gpg_error_t
2219 parse_iscached_opt_sign (void *data, void *value)
2221 struct client_s *client = data;
2223 (void) value;
2224 client->opts |= OPT_CACHE_SIGN;
2225 return 0;
2228 static gpg_error_t
2229 iscached_command (assuan_context_t ctx, char *line)
2231 struct client_s *client = assuan_get_pointer (ctx);
2232 gpg_error_t rc;
2233 int defer = 0;
2234 struct argv_s *args[] = {
2235 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2236 &(struct argv_s) {"agent", OPTION_TYPE_NOARG, parse_iscached_opt_agent},
2237 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_iscached_opt_sign},
2238 NULL
2241 if (!line || !*line)
2242 return send_error (ctx, GPG_ERR_SYNTAX);
2244 rc = parse_options (&line, args, client, 1);
2245 if (rc)
2246 return send_error (ctx, rc);
2247 else if (!valid_filename (line))
2248 return send_error (ctx, GPG_ERR_INV_VALUE);
2250 if (!(client->flags & FLAG_OPEN)
2251 && ((client->opts & OPT_CACHE_AGENT) || (client->opts & OPT_CACHE_SIGN)))
2252 return send_error (ctx, GPG_ERR_INV_STATE);
2254 rc = cache_iscached (line, &defer, (client->opts & OPT_CACHE_AGENT),
2255 (client->opts & OPT_CACHE_SIGN));
2256 if (!rc && defer)
2257 rc = GPG_ERR_NO_DATA;
2259 if (!rc)
2261 struct cache_data_s *cdata = cache_get_data (line);
2263 if (cdata)
2265 rc = validate_checksum (client, line, cdata, NULL, NULL);
2266 if (rc == GPG_ERR_CHECKSUM)
2267 rc = GPG_ERR_NO_DATA;
2271 if (client->opts & OPT_LOCK
2272 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2274 gpg_error_t trc = rc;
2276 if (strcmp (line, client->filename))
2277 reset_client (client);
2279 xfree (client->filename);
2280 client->filename = str_dup (line);
2281 rc = do_lock (client, 1);
2282 if (!rc)
2283 rc = trc;
2286 return send_error (ctx, rc);
2289 static gpg_error_t
2290 clearcache_command (assuan_context_t ctx, char *line)
2292 struct client_s *client = assuan_get_pointer (ctx);
2293 gpg_error_t rc = 0, all_rc = 0;
2294 int i;
2295 int t;
2296 int all = 0;
2297 struct client_thread_s *once = NULL;
2298 unsigned count;
2300 cache_lock ();
2301 pthread_cleanup_push (cache_release_mutex, NULL);
2302 count = cache_file_count ();
2303 MUTEX_LOCK (&cn_mutex);
2304 pthread_cleanup_push ((void *)release_mutex_cb, &cn_mutex);
2306 if (!line || !*line)
2307 all = 1;
2309 t = slist_length (cn_thread_list);
2311 for (i = 0; i < t; i++)
2313 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2314 assuan_peercred_t peer;
2316 if (!thd->cl)
2317 continue;
2319 /* Lock each connected clients' file mutex to prevent any other client
2320 * from accessing the cache entry (the file mutex is locked upon
2321 * command startup). The cache for the entry is not cleared if the
2322 * file mutex is locked by another client to prevent this function
2323 * from blocking. Rather, it returns an error.
2325 if (all)
2327 if (thd->cl->filename)
2329 rc = do_validate_peer (ctx, thd->cl->filename, &peer);
2330 /* The current client doesn't have permission to open the other
2331 * filename do to "access" configuration parameter in a filename
2332 * section. Since we are clearning all cache entries the error
2333 * will be returned later during cache_clear(). */
2334 if (rc)
2336 rc = 0;
2337 continue;
2340 else // Idle client without opened file?
2341 continue;
2343 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->filename, -1, 0, -1);
2344 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2346 /* The current client owns the lock. */
2347 if (pthread_equal (pthread_self (), thd->tid))
2348 rc = 0;
2349 else
2351 if (cache_iscached (thd->cl->filename, NULL, 0, 0)
2352 == GPG_ERR_NO_DATA)
2354 rc = 0;
2355 continue;
2358 /* The cache entry will be cleared when the other client
2359 * disconnects and cache_timer_thread() does its thing. */
2360 cache_defer_clear (thd->cl->filename);
2363 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2365 rc = 0;
2366 continue;
2369 if (!rc)
2371 rc = cache_clear (NULL, thd->cl->filename, 1);
2372 cache_unlock_mutex (thd->cl->filename, 0);
2375 if (rc)
2376 all_rc = rc;
2378 rc = 0;
2380 else
2382 /* A single data filename was specified. Lock only this data file
2383 * mutex and free the cache entry. */
2384 rc = do_validate_peer (ctx, line, &peer);
2385 if (rc == GPG_ERR_FORBIDDEN)
2386 rc = peer_is_invoker (client);
2388 if (rc == GPG_ERR_EACCES)
2389 rc = GPG_ERR_FORBIDDEN;
2391 if (!rc && thd->cl->filename && !strcmp (thd->cl->filename , line))
2393 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->filename, -1, 0, -1);
2394 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2396 /* The current client owns the lock. */
2397 if (pthread_equal (pthread_self (), thd->tid))
2398 rc = 0;
2401 if (!rc)
2403 once = thd;
2404 rc = cache_clear (NULL, thd->cl->filename, 1);
2405 cache_unlock_mutex (thd->cl->filename, 0);
2407 else
2408 cache_defer_clear (thd->cl->filename);
2410 /* Found a client with the opened file. The cache entry has been
2411 * either cleared (self) or defered to be cleared. */
2412 break;
2417 /* Only connected clients' cache entries have been cleared. Now clear any
2418 * remaining cache entries without clients but only if there wasn't an
2419 * error from above since this would defeat the locking check of the
2420 * remaining entries. */
2421 if (all && !all_rc && cache_file_count ())
2423 rc = cache_clear (client, NULL, 1);
2424 if (rc == GPG_ERR_EACCES)
2425 rc = GPG_ERR_FORBIDDEN;
2428 /* No clients are using the specified file. */
2429 else if (!all_rc && !rc && !once)
2430 rc = cache_clear (NULL, line, 1);
2432 /* Release the connection mutex. */
2433 pthread_cleanup_pop (1);
2435 if (!rc || (cache_file_count () && count != cache_file_count ()))
2436 send_status_all (STATUS_CACHE, NULL);
2438 /* Release the cache mutex. */
2439 pthread_cleanup_pop (1);
2441 /* One or more files were locked while clearing all cache entries. */
2442 if (all_rc)
2443 rc = all_rc;
2445 return send_error (ctx, rc);
2448 static gpg_error_t
2449 cachetimeout_command (assuan_context_t ctx, char *line)
2451 struct client_s *client = assuan_get_pointer (ctx);
2452 int timeout;
2453 char **req = str_split (line, " ", 0);
2454 char *p;
2455 gpg_error_t rc = 0;
2456 assuan_peercred_t peer;
2458 if (!req || !*req || !req[1])
2460 strv_free (req);
2461 return send_error (ctx, GPG_ERR_SYNTAX);
2464 errno = 0;
2465 timeout = (int) strtol (req[1], &p, 10);
2466 if (errno != 0 || *p || timeout < -1)
2468 strv_free (req);
2469 return send_error (ctx, GPG_ERR_SYNTAX);
2472 rc = do_validate_peer (ctx, req[0], &peer);
2473 if (rc == GPG_ERR_FORBIDDEN)
2475 rc = peer_is_invoker (client);
2476 if (rc == GPG_ERR_EACCES)
2477 rc = GPG_ERR_FORBIDDEN;
2480 if (!rc)
2482 rc = cache_set_timeout (req[0], timeout);
2483 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2485 rc = 0;
2486 MUTEX_LOCK (&rcfile_mutex);
2487 config_set_int_param (&global_config, req[0], "cache_timeout",
2488 req[1]);
2489 MUTEX_UNLOCK (&rcfile_mutex);
2493 strv_free (req);
2494 return send_error (ctx, rc);
2497 static gpg_error_t
2498 dump_command (assuan_context_t ctx, char *line)
2500 xmlChar *xml;
2501 int len;
2502 struct client_s *client = assuan_get_pointer (ctx);
2503 gpg_error_t rc;
2505 if (disable_list_and_dump == 1)
2506 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2508 if (line && *line)
2509 return send_error (ctx, GPG_ERR_SYNTAX);
2511 rc = peer_is_invoker(client);
2512 if (rc)
2513 return send_error (ctx, rc);
2515 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2517 if (!xml)
2519 log_write ("%s(%i): %s", __FILE__, __LINE__,
2520 pwmd_strerror (GPG_ERR_ENOMEM));
2521 return send_error (ctx, GPG_ERR_ENOMEM);
2524 pthread_cleanup_push ((void *)xmlFree, xml);
2525 rc = xfer_data (ctx, (char *) xml, len);
2526 pthread_cleanup_pop (1);
2527 return send_error (ctx, rc);
2530 static gpg_error_t
2531 getconfig_command (assuan_context_t ctx, char *line)
2533 struct client_s *client = assuan_get_pointer (ctx);
2534 gpg_error_t rc = 0;
2535 char filename[255] = { 0 }, param[747] = { 0 };
2536 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2538 if (!line || !*line)
2539 return send_error (ctx, GPG_ERR_SYNTAX);
2541 if (strchr (line, ' '))
2543 sscanf (line, " %254[^ ] %746c", filename, param);
2544 paramp = param;
2545 fp = filename;
2548 if (fp && !valid_filename (fp))
2549 return send_error (ctx, GPG_ERR_INV_VALUE);
2551 paramp = str_down (paramp);
2552 if (!strcmp (paramp, "passphrase"))
2553 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2555 p = config_get_value (fp ? fp : "global", paramp);
2556 if (!p)
2557 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2559 tmp = expand_homedir (p);
2560 xfree (p);
2561 if (!tmp)
2563 log_write ("%s(%i): %s", __FILE__, __LINE__,
2564 pwmd_strerror (GPG_ERR_ENOMEM));
2565 return send_error (ctx, GPG_ERR_ENOMEM);
2568 p = tmp;
2569 pthread_cleanup_push ((void *)xfree, p);
2570 rc = xfer_data (ctx, p, strlen (p));
2571 pthread_cleanup_pop (1);
2572 return send_error (ctx, rc);
2575 struct xpath_s
2577 xmlXPathContextPtr xp;
2578 xmlXPathObjectPtr result;
2579 xmlBufferPtr buf;
2580 char **req;
2583 static void
2584 xpath_command_free (void *arg)
2586 struct xpath_s *xpath = arg;
2588 if (!xpath)
2589 return;
2591 req_free (xpath->req);
2593 if (xpath->buf)
2594 xmlBufferFree (xpath->buf);
2596 if (xpath->result)
2597 xmlXPathFreeObject (xpath->result);
2599 if (xpath->xp)
2600 xmlXPathFreeContext (xpath->xp);
2603 static gpg_error_t
2604 do_xpath (assuan_context_t ctx, char *line)
2606 gpg_error_t rc;
2607 struct client_s *client = assuan_get_pointer (ctx);
2608 struct xpath_s _x = { 0 };
2609 struct xpath_s *xpath = &_x;
2611 if (!line || !*line)
2612 return GPG_ERR_SYNTAX;
2614 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2616 if (strv_printf (&xpath->req, "%s", line) == 0)
2617 return GPG_ERR_ENOMEM;
2620 xpath->xp = xmlXPathNewContext (client->doc);
2621 if (!xpath->xp)
2623 rc = GPG_ERR_BAD_DATA;
2624 goto fail;
2627 xpath->result =
2628 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2629 if (!xpath->result)
2631 rc = GPG_ERR_BAD_DATA;
2632 goto fail;
2635 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2637 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2638 goto fail;
2641 rc = xml_recurse_xpath_nodeset (client, client->doc,
2642 xpath->result->nodesetval,
2643 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2644 NULL);
2645 if (rc)
2646 goto fail;
2647 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2649 rc = GPG_ERR_NO_DATA;
2650 goto fail;
2652 else if (xpath->req[1])
2654 rc = 0;
2655 goto fail;
2658 pthread_cleanup_push ((void *)xpath_command_free, &xpath);
2659 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2660 xmlBufferLength (xpath->buf));
2661 pthread_cleanup_pop (0);
2662 fail:
2663 xpath_command_free (xpath);
2664 return rc;
2667 static gpg_error_t
2668 xpath_command (assuan_context_t ctx, char *line)
2670 struct client_s *client = assuan_get_pointer (ctx);
2671 gpg_error_t rc;
2672 struct argv_s *args[] = {
2673 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2674 NULL
2677 if (disable_list_and_dump == 1)
2678 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2680 rc = peer_is_invoker(client);
2681 if (rc)
2682 return send_error (ctx, rc);
2684 rc = parse_options (&line, args, client, 1);
2685 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2686 rc = GPG_ERR_SYNTAX;
2687 if (rc)
2688 return send_error (ctx, rc);
2690 if (client->opts & OPT_INQUIRE)
2692 unsigned char *result;
2693 size_t len;
2695 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2696 if (rc)
2697 return send_error (ctx, rc);
2699 pthread_cleanup_push ((void *)xfree, result);
2700 rc = do_xpath (ctx, (char *)result);
2701 pthread_cleanup_pop (1);
2703 else
2704 rc = do_xpath (ctx, line);
2706 return send_error (ctx, rc);
2709 static gpg_error_t
2710 do_xpathattr (assuan_context_t ctx, char *line)
2712 struct client_s *client = assuan_get_pointer (ctx);
2713 gpg_error_t rc;
2714 char **req = NULL;
2715 int cmd = 0; //SET
2716 struct xpath_s _x = { 0 };
2717 struct xpath_s *xpath = &_x;
2719 if (!line || !*line)
2720 return GPG_ERR_SYNTAX;
2722 if ((req = str_split (line, " ", 3)) == NULL)
2723 return GPG_ERR_ENOMEM;
2725 if (!req[0])
2727 rc = GPG_ERR_SYNTAX;
2728 goto fail;
2731 if (!strcasecmp (req[0], "SET"))
2732 cmd = 0;
2733 else if (!strcasecmp (req[0], "DELETE"))
2734 cmd = 1;
2735 else
2737 rc = GPG_ERR_SYNTAX;
2738 goto fail;
2741 if (!req[1] || !req[2])
2743 rc = GPG_ERR_SYNTAX;
2744 goto fail;
2747 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2749 rc = GPG_ERR_ENOMEM;
2750 goto fail;
2753 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2755 rc = GPG_ERR_SYNTAX;
2756 goto fail;
2759 xpath->xp = xmlXPathNewContext (client->doc);
2760 if (!xpath->xp)
2762 rc = GPG_ERR_BAD_DATA;
2763 goto fail;
2766 xpath->result = xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2767 if (!xpath->result)
2769 rc = GPG_ERR_BAD_DATA;
2770 goto fail;
2773 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2775 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2776 goto fail;
2779 rc = xml_recurse_xpath_nodeset (client, client->doc,
2780 xpath->result->nodesetval,
2781 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2782 (xmlChar *) req[1]);
2784 fail:
2785 xpath_command_free (xpath);
2786 strv_free (req);
2787 return rc;
2790 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2791 static gpg_error_t
2792 xpathattr_command (assuan_context_t ctx, char *line)
2794 struct client_s *client = assuan_get_pointer (ctx);
2795 gpg_error_t rc;
2796 struct argv_s *args[] = {
2797 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2798 NULL
2801 if (disable_list_and_dump == 1)
2802 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2804 rc = peer_is_invoker(client);
2805 if (rc)
2806 return send_error (ctx, rc);
2808 rc = parse_options (&line, args, client, 1);
2809 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2810 rc = GPG_ERR_SYNTAX;
2811 if (rc)
2812 return send_error (ctx, rc);
2814 if (client->opts & OPT_INQUIRE)
2816 unsigned char *result;
2817 size_t len;
2819 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2820 if (rc)
2821 return send_error (ctx, rc);
2823 pthread_cleanup_push ((void *)xfree, result);
2824 rc = do_xpathattr (ctx, (char *)result);
2825 pthread_cleanup_pop (1);
2827 else
2828 rc = do_xpathattr (ctx, line);
2830 return send_error (ctx, rc);
2833 static gpg_error_t
2834 do_import (struct client_s *client, const char *root_element,
2835 unsigned char *content)
2837 xmlDocPtr doc = NULL;
2838 xmlNodePtr n = NULL, root;
2839 gpg_error_t rc;
2840 struct string_s *str = NULL;
2841 struct xml_request_s *req = NULL;
2843 if (!content || !*content)
2844 return GPG_ERR_SYNTAX;
2846 if (root_element)
2848 rc = xml_new_request (client, root_element, XML_CMD_STORE, &req);
2849 if (rc)
2851 xfree (content);
2852 return rc;
2855 if (!xml_valid_element_path (req->args, 0))
2857 xml_free_request (req);
2858 xfree (content);
2859 return GPG_ERR_INV_VALUE;
2863 str = string_new_content ((char *)content);
2864 str = string_prepend (str, "<pwmd>");
2865 str = string_append (str, "</pwmd>");
2866 doc = xmlReadDoc ((xmlChar *) str->str, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2867 string_free (str, 1);
2868 if (!doc)
2870 rc = GPG_ERR_BAD_DATA;
2871 goto fail;
2874 root = xmlDocGetRootElement (doc);
2875 root = root->children;
2876 if (root->type != XML_ELEMENT_NODE)
2878 rc = GPG_ERR_BAD_DATA;
2879 goto fail;
2882 rc = xml_validate_import (client, root);
2883 if (rc)
2884 goto fail;
2886 if (req)
2888 /* Create the new specified root element path, if needed. */
2889 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc),
2890 &rc);
2891 if (rc)
2892 goto fail;
2894 /* Overwrite the children of the specified root element path. */
2895 (void)xml_unlink_node (client, n->children);
2896 n->children = NULL;
2898 else
2899 n = xmlDocGetRootElement (client->doc);
2901 if (n)
2903 xmlNodePtr copy;
2904 xmlChar *name = xml_attribute_value (root, (xmlChar *)"_name");
2906 if (!name)
2907 rc = GPG_ERR_BAD_DATA;
2908 else if (!req)
2910 /* No --root argument passed. Overwrite the current documents' root
2911 * element matching the root element of the imported data. */
2912 xml_free_request (req);
2913 req = NULL;
2914 rc = xml_new_request (client, (char *)name, XML_CMD_DELETE, &req);
2915 xmlFree (name);
2916 if (!rc)
2918 xmlNodePtr tmp;
2920 tmp = xml_find_elements (client, req,
2921 xmlDocGetRootElement (req->doc), &rc);
2922 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2923 goto fail;
2925 if (!rc)
2926 (void)xml_unlink_node (client, tmp);
2928 rc = 0;
2932 if (!rc)
2934 copy = xmlCopyNodeList (root);
2935 if (!copy)
2936 rc = GPG_ERR_ENOMEM;
2937 else
2939 n = xmlAddChildList (n, copy);
2940 if (!n)
2941 rc = GPG_ERR_ENOMEM;
2946 if (!rc && n && n->parent)
2947 rc = xml_update_element_mtime (client, n->parent);
2949 fail:
2950 xml_free_request (req);
2952 if (doc)
2953 xmlFreeDoc (doc);
2955 return rc;
2958 static gpg_error_t
2959 parse_import_opt_root (void *data, void *value)
2961 struct client_s *client = data;
2963 client->import_root = str_dup (value);
2964 return client->import_root ? 0 : GPG_ERR_ENOMEM;
2967 static gpg_error_t
2968 import_command (assuan_context_t ctx, char *line)
2970 gpg_error_t rc;
2971 struct client_s *client = assuan_get_pointer (ctx);
2972 unsigned char *result;
2973 size_t len;
2974 struct argv_s *args[] = {
2975 &(struct argv_s) {"root", OPTION_TYPE_ARG, parse_import_opt_root},
2976 NULL
2979 xfree (client->import_root);
2980 client->import_root = NULL;
2981 rc = parse_options (&line, args, client, 0);
2982 if (rc)
2983 return send_error (ctx, rc);
2985 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2986 if (rc)
2988 xfree (client->import_root);
2989 client->import_root = NULL;
2990 return send_error (ctx, rc);
2993 rc = do_import (client, client->import_root, result);
2994 xfree (client->import_root);
2995 client->import_root = NULL;
2996 return send_error (ctx, rc);
2999 static gpg_error_t
3000 do_lock (struct client_s *client, int add)
3002 gpg_error_t rc = lock_file_mutex (client, add);
3004 if (!rc)
3005 client->flags |= FLAG_LOCK_CMD;
3007 return rc;
3010 static gpg_error_t
3011 lock_command (assuan_context_t ctx, char *line)
3013 struct client_s *client = assuan_get_pointer (ctx);
3014 gpg_error_t rc;
3016 if (line && *line)
3017 return send_error (ctx, GPG_ERR_SYNTAX);
3019 rc = do_lock (client, 0);
3020 return send_error (ctx, rc);
3023 static gpg_error_t
3024 unlock_command (assuan_context_t ctx, char *line)
3026 struct client_s *client = assuan_get_pointer (ctx);
3027 gpg_error_t rc;
3029 if (line && *line)
3030 return send_error (ctx, GPG_ERR_SYNTAX);
3032 rc = unlock_file_mutex (client, 0);
3033 return send_error (ctx, rc);
3036 static void
3037 get_set_env (const char *name, const char *value)
3039 if (value && *value)
3040 setenv (name, value, 1);
3041 else
3042 unsetenv (name);
3045 static gpg_error_t
3046 option_command (assuan_context_t ctx, char *line)
3048 struct client_s *client = assuan_get_pointer (ctx);
3049 gpg_error_t rc = 0;
3050 char namebuf[255] = { 0 };
3051 char *name = namebuf;
3052 char *value = NULL, *p, *tmp = NULL;
3054 p = strchr (line, '=');
3055 if (!p)
3057 strncpy (namebuf, line, sizeof(namebuf));
3058 namebuf[sizeof(namebuf)-1] = 0;
3060 else
3062 strncpy (namebuf, line, strlen (line)-strlen (p));
3063 namebuf[sizeof(namebuf)-1] = 0;
3064 value = p+1;
3067 log_write2 ("OPTION name='%s' value='%s'", name, value);
3069 if (strcasecmp (name, (char *) "lock-timeout") == 0)
3071 long n = 0;
3073 if (value)
3075 n = strtol (value, &tmp, 10);
3076 if (tmp && *tmp)
3077 return send_error (ctx, GPG_ERR_INV_VALUE);
3080 client->lock_timeout = n;
3082 else if (strcasecmp (name, (char *) "client-state") == 0)
3084 long n = 0;
3086 if (value)
3088 n = strtol (value, &tmp, 10);
3089 if ((tmp && *tmp) || (n < 0 || n > 1))
3090 return send_error (ctx, GPG_ERR_INV_VALUE);
3091 client->client_state = n;
3093 else
3094 client->client_state = 0;
3096 else if (strcasecmp (name, (char *) "NAME") == 0)
3098 if (value && strchr (value, ' '))
3099 rc = GPG_ERR_INV_VALUE;
3100 else
3102 tmp = pthread_getspecific (thread_name_key);
3103 xfree (tmp);
3104 MUTEX_LOCK (&cn_mutex);
3105 xfree (client->thd->name);
3106 client->thd->name = NULL;
3108 if (!value || !*value)
3109 pthread_setspecific (thread_name_key, str_dup (""));
3110 else
3112 client->thd->name = str_dup (value);
3113 pthread_setspecific (thread_name_key, str_dup (value));
3116 MUTEX_UNLOCK (&cn_mutex);
3119 else if (strcasecmp (name, "disable-pinentry") == 0)
3121 int n = 1;
3123 if (value && *value)
3125 n = (int) strtol (value, &tmp, 10);
3126 if (*tmp || n < 0 || n > 1)
3127 return send_error (ctx, GPG_ERR_INV_VALUE);
3130 if (n)
3131 client->flags |= FLAG_NO_PINENTRY;
3132 else
3133 client->flags &= ~FLAG_NO_PINENTRY;
3135 else if (strcasecmp (name, "ttyname") == 0)
3137 get_set_env ("GPG_TTY", value);
3139 else if (strcasecmp (name, "ttytype") == 0)
3141 get_set_env ("TERM", value);
3143 else if (strcasecmp (name, "display") == 0)
3145 get_set_env ("DISPLAY", value);
3147 else if (strcasecmp (name, "lc_messages") == 0)
3150 else if (strcasecmp (name, "lc_ctype") == 0)
3153 else if (strcasecmp (name, "desc") == 0)
3156 else if (strcasecmp (name, "pinentry-timeout") == 0)
3159 else
3160 rc = GPG_ERR_UNKNOWN_OPTION;
3162 return send_error (ctx, rc);
3165 static gpg_error_t
3166 do_rename (assuan_context_t ctx, char *line)
3168 struct client_s *client = assuan_get_pointer (ctx);
3169 char **args, *p;
3170 xmlNodePtr src, dst;
3171 struct string_s *str = NULL, *tstr;
3172 struct xml_request_s *req;
3173 gpg_error_t rc;
3175 args = str_split (line, " ", 0);
3176 if (!args || strv_length (args) != 2)
3178 strv_free (args);
3179 return GPG_ERR_SYNTAX;
3182 if (!xml_valid_element ((xmlChar *) args[1]))
3184 strv_free (args);
3185 return GPG_ERR_INV_VALUE;
3188 rc = xml_new_request (client, args[0], XML_CMD_RENAME, &req);
3189 if (rc)
3191 strv_free (args);
3192 return rc;
3195 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3196 if (rc)
3197 goto fail;
3199 rc = xml_is_element_owner (client, src);
3200 if (rc)
3201 goto fail;
3203 xmlChar *a = xmlGetProp (src, (xmlChar *) "_name");
3204 if (!a)
3206 rc = GPG_ERR_ENOMEM;
3207 goto fail;
3210 /* To prevent unwanted effects:
3212 * <element _name="a"><element _name="b"/></element>
3214 * RENAME a<TAB>b b
3216 if (xmlStrEqual (a, (xmlChar *) args[1]))
3218 xmlFree (a);
3219 rc = GPG_ERR_EEXIST;
3220 goto fail;
3223 xmlFree (a);
3224 str = string_new (args[0]);
3225 if (!str)
3227 rc = GPG_ERR_ENOMEM;
3228 goto fail;
3231 p = strrchr (str->str, '\t');
3232 if (p)
3233 tstr = string_truncate (str, strlen (str->str)-strlen (p));
3234 else
3235 tstr = string_truncate (str, 0);
3237 if (!tstr)
3239 string_free (str, 1);
3240 rc = GPG_ERR_ENOMEM;
3241 goto fail;
3244 str = tstr;
3245 tstr = string_append_printf (str, "%s%s", str->len ? "\t" : "", args[1]);
3246 if (!tstr)
3248 string_free (str, 1);
3249 rc = GPG_ERR_ENOMEM;
3250 goto fail;
3253 str = tstr;
3254 xml_free_request (req);
3255 rc = xml_new_request (client, str->str, XML_CMD_RENAME, &req);
3256 string_free (str, 1);
3257 if (rc)
3259 req = NULL;
3260 goto fail;
3263 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3264 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3265 goto fail;
3267 rc = 0;
3269 /* Target may exist:
3271 * <element _name="a"/>
3272 * <element _name="b" target="a"/>
3274 * RENAME b a
3276 if (src == dst)
3278 rc = GPG_ERR_EEXIST;
3279 goto fail;
3282 if (dst)
3284 rc = xml_is_element_owner (client, dst);
3285 if (rc)
3286 goto fail;
3288 rc = xml_add_attribute (client, src, "_name", args[1]);
3289 if (!rc)
3290 xml_unlink_node (client, dst);
3292 else
3293 rc = xml_add_attribute (client, src, "_name", args[1]);
3295 fail:
3296 xml_free_request (req);
3297 strv_free (args);
3298 return rc;
3301 static gpg_error_t
3302 rename_command (assuan_context_t ctx, char *line)
3304 struct client_s *client = assuan_get_pointer (ctx);
3305 gpg_error_t rc;
3306 struct argv_s *args[] = {
3307 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3308 NULL
3311 rc = parse_options (&line, args, client, 1);
3312 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3313 rc = GPG_ERR_SYNTAX;
3314 if (rc)
3315 return send_error (ctx, rc);
3317 if (client->opts & OPT_INQUIRE)
3319 unsigned char *result;
3320 size_t len;
3322 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3323 if (rc)
3324 return send_error (ctx, rc);
3326 pthread_cleanup_push ((void *)xfree, result);
3327 rc = do_rename (ctx, (char *)result);
3328 pthread_cleanup_pop (1);
3330 else
3331 rc = do_rename (ctx, line);
3333 return send_error (ctx, rc);
3336 static gpg_error_t
3337 do_copy (assuan_context_t ctx, char *line)
3339 struct client_s *client = assuan_get_pointer (ctx);
3340 char **args, **targs;
3341 xmlNodePtr src, dst, tree = NULL;
3342 struct xml_request_s *req;
3343 gpg_error_t rc;
3345 args = str_split (line, " ", 0);
3346 if (!args || strv_length (args) != 2)
3348 strv_free (args);
3349 return GPG_ERR_SYNTAX;
3352 targs = str_split (args[1], "\t", 0);
3353 if (!targs)
3355 strv_free (args);
3356 return GPG_ERR_SYNTAX;
3359 if (!xml_valid_element_path (targs, 0))
3361 strv_free (args);
3362 strv_free (targs);
3363 return GPG_ERR_INV_VALUE;
3365 strv_free (targs);
3367 rc = xml_new_request (client, args[0], XML_CMD_NONE, &req);
3368 if (rc)
3370 strv_free (args);
3371 return rc;
3374 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3375 if (rc)
3376 goto fail;
3378 tree = xmlCopyNodeList (src);
3379 if (!tree)
3381 rc = GPG_ERR_ENOMEM;
3382 goto fail;
3385 xml_free_request (req);
3386 /* Create the destination element path if it does not exist. */
3387 rc = xml_new_request (client, args[1], XML_CMD_STORE, &req);
3388 if (rc)
3390 req = NULL;
3391 goto fail;
3394 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3395 if (rc)
3396 goto fail;
3398 rc = xml_is_element_owner (client, dst);
3399 if (rc)
3400 goto fail;
3402 rc = xml_validate_target (client, req->doc, args[1], src);
3403 if (rc || src == dst)
3405 rc = rc ? rc : GPG_ERR_EEXIST;
3406 goto fail;
3409 /* Merge any attributes from the src node to the initial dst node. */
3410 for (xmlAttrPtr attr = tree->properties; attr; attr = attr->next)
3412 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3413 continue;
3415 xmlAttrPtr a = xmlHasProp (dst, attr->name);
3416 if (a)
3417 xmlRemoveProp (a);
3419 xmlChar *tmp = xmlNodeGetContent (attr->children);
3420 xmlNewProp (dst, attr->name, tmp);
3421 xmlFree (tmp);
3422 /* Create the default attributes. */
3423 rc = xml_add_attribute (client, dst, NULL, NULL);
3426 xmlNodePtr n = dst->children;
3427 (void)xml_unlink_node (client, n);
3428 dst->children = NULL;
3430 if (tree->children)
3432 n = xmlCopyNodeList (tree->children);
3433 if (!n)
3435 rc = GPG_ERR_ENOMEM;
3436 goto fail;
3439 n = xmlAddChildList (dst, n);
3440 if (!n)
3442 rc = GPG_ERR_ENOMEM;
3443 goto fail;
3446 rc = xml_update_element_mtime (client, xmlDocGetRootElement (client->doc)
3447 == dst->parent ? dst : dst->parent);
3450 fail:
3451 if (tree)
3452 (void)xml_unlink_node (client, tree);
3454 xml_free_request (req);
3455 strv_free (args);
3456 return rc;
3459 static gpg_error_t
3460 copy_command (assuan_context_t ctx, char *line)
3462 struct client_s *client = assuan_get_pointer (ctx);
3463 gpg_error_t rc;
3464 struct argv_s *args[] = {
3465 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3466 NULL
3469 rc = parse_options (&line, args, client, 1);
3470 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3471 rc = GPG_ERR_SYNTAX;
3472 if (rc)
3473 return send_error (ctx, rc);
3475 if (client->opts & OPT_INQUIRE)
3477 unsigned char *result;
3478 size_t len;
3480 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3481 if (rc)
3482 return send_error (ctx, rc);
3484 pthread_cleanup_push ((void *)xfree, result);
3485 rc = do_copy (ctx, (char *)result);
3486 pthread_cleanup_pop (1);
3488 else
3489 rc = do_copy (ctx, line);
3491 return send_error (ctx, rc);
3494 static gpg_error_t
3495 do_move (assuan_context_t ctx, char *line)
3497 struct client_s *client = assuan_get_pointer (ctx);
3498 char **args;
3499 xmlNodePtr src, dst;
3500 struct xml_request_s *req;
3501 gpg_error_t rc;
3503 args = str_split (line, " ", 0);
3504 if (!args || strv_length (args) != 2)
3506 strv_free (args);
3507 return GPG_ERR_SYNTAX;
3510 if (*args[1])
3512 char **targs = str_split (args[1], "\t", 0);
3513 if (!targs)
3515 strv_free (args);
3516 return GPG_ERR_ENOMEM;
3519 if (!xml_valid_element_path (targs, 0))
3521 strv_free (targs);
3522 strv_free (args);
3523 return GPG_ERR_INV_VALUE;
3526 strv_free (targs);
3529 rc = xml_new_request (client, args[0], XML_CMD_MOVE, &req);
3530 if (rc)
3532 strv_free (args);
3533 return rc;
3536 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3537 if (rc)
3538 goto fail;
3540 rc = xml_is_element_owner (client, src);
3541 if (rc)
3542 goto fail;
3544 xml_free_request (req);
3545 req = NULL;
3546 if (*args[1])
3548 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
3549 if (rc)
3550 goto fail;
3552 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc),
3553 &rc);
3555 else
3556 dst = xmlDocGetRootElement (client->doc);
3558 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3559 goto fail;
3561 rc = 0;
3563 for (xmlNodePtr n = dst; n; n = n->parent)
3565 if (n == src)
3567 rc = GPG_ERR_EEXIST;
3568 goto fail;
3572 if (dst)
3574 xmlChar *a = xml_attribute_value (src, (xmlChar *) "_name");
3575 xmlNodePtr dup = xml_find_element (client, dst->children, (char *) a, &rc);
3577 xmlFree (a);
3578 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3579 goto fail;
3581 rc = 0;
3583 if (dup)
3585 if (dup == src)
3587 rc = GPG_ERR_EEXIST;
3588 goto fail;
3591 if (dst == xmlDocGetRootElement (client->doc))
3593 xmlNodePtr n = src;
3594 int match = 0;
3596 while (n->parent && n->parent != dst)
3597 n = n->parent;
3599 xmlChar *a = xml_attribute_value (n, (xmlChar *) "_name");
3600 xmlChar *b = xml_attribute_value (src, (xmlChar *) "_name");
3602 if (xmlStrEqual (a, b))
3604 match = 1;
3605 xmlUnlinkNode (src);
3606 (void)xml_unlink_node (client, n);
3609 xmlFree (a);
3610 xmlFree (b);
3612 if (!match)
3613 (void)xml_unlink_node (client, dup);
3615 else
3616 xmlUnlinkNode (dup);
3620 if (!dst && *req->args)
3622 struct xml_request_s *nreq = NULL;
3623 xmlChar *name = xml_attribute_value (src, (xmlChar *) "_name");
3625 if (src->parent == xmlDocGetRootElement (client->doc)
3626 && !strcmp ((char *) name, *req->args))
3628 xmlFree (name);
3629 rc = GPG_ERR_EEXIST;
3630 goto fail;
3633 xmlFree (name);
3634 rc = xml_new_request (client, args[1], XML_CMD_STORE, &nreq);
3635 if (!rc)
3637 dst = xml_find_elements (client, nreq,
3638 xmlDocGetRootElement (nreq->doc), &rc);
3639 xml_free_request (nreq);
3642 if (rc)
3643 goto fail;
3646 xml_update_element_mtime (client, src->parent);
3647 xmlUnlinkNode (src);
3649 dst = xmlAddChildList (dst, src);
3650 if (!dst)
3651 rc = GPG_ERR_ENOMEM;
3652 else
3653 xml_update_element_mtime (client, dst->parent);
3655 fail:
3656 xml_free_request (req);
3657 strv_free (args);
3658 return rc;
3661 static gpg_error_t
3662 move_command (assuan_context_t ctx, char *line)
3664 struct client_s *client = assuan_get_pointer (ctx);
3665 gpg_error_t rc;
3666 struct argv_s *args[] = {
3667 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3668 NULL
3671 rc = parse_options (&line, args, client, 1);
3672 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3673 rc = GPG_ERR_SYNTAX;
3674 if (rc)
3675 return send_error (ctx, rc);
3677 if (client->opts & OPT_INQUIRE)
3679 unsigned char *result;
3680 size_t len;
3682 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3683 if (rc)
3684 return send_error (ctx, rc);
3686 pthread_cleanup_push ((void *)xfree, result);
3687 rc = do_move (ctx, (char *)result);
3688 pthread_cleanup_pop (1);
3690 else
3691 rc = do_move (ctx, line);
3693 return send_error (ctx, rc);
3696 static gpg_error_t
3697 ls_command (assuan_context_t ctx, char *line)
3699 gpg_error_t rc;
3700 char *tmp;
3701 char *dir;
3702 DIR *d;
3704 if (line && *line)
3705 return send_error (ctx, GPG_ERR_SYNTAX);
3707 tmp = str_asprintf ("%s/data", homedir);
3708 dir = expand_homedir (tmp);
3709 xfree (tmp);
3710 d = opendir (dir);
3711 rc = gpg_error_from_errno (errno);
3713 if (!d)
3715 xfree (dir);
3716 return send_error (ctx, rc);
3719 size_t len =
3720 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3721 struct dirent *p = xmalloc (len), *cur = NULL;
3722 char *list = NULL;
3724 xfree (dir);
3725 pthread_cleanup_push ((void *)xfree, p);
3726 pthread_cleanup_push ((void *)(void *)(void *)closedir, d);
3727 rc = 0;
3729 while (!readdir_r (d, p, &cur) && cur)
3731 struct stat st;
3733 if (stat (cur->d_name, &st) == -1 || !S_ISREG (st.st_mode))
3734 continue;
3736 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3738 if (!tmp)
3740 if (list)
3741 xfree (list);
3743 rc = GPG_ERR_ENOMEM;
3744 break;
3747 xfree (list);
3748 list = tmp;
3751 pthread_cleanup_pop (1); // closedir (d)
3752 pthread_cleanup_pop (1); // xfree (p)
3754 if (rc)
3755 return send_error (ctx, rc);
3757 if (!list)
3758 return send_error (ctx, GPG_ERR_NO_DATA);
3760 list[strlen (list) - 1] = 0;
3761 pthread_cleanup_push ((void *)xfree, list);
3762 rc = xfer_data (ctx, list, strlen (list));
3763 pthread_cleanup_pop (1);
3764 return send_error (ctx, rc);
3767 static gpg_error_t
3768 bye_notify (assuan_context_t ctx, char *line)
3770 struct client_s *cl = assuan_get_pointer (ctx);
3771 gpg_error_t ret = 0;
3773 update_client_state (cl, CLIENT_STATE_DISCON);
3775 #ifdef WITH_GNUTLS
3776 if (cl->thd->remote)
3778 int rc;
3782 struct timeval tv = { 0, 50000 };
3784 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3785 if (rc == GNUTLS_E_AGAIN)
3786 select (0, NULL, NULL, NULL, &tv);
3788 while (rc == GNUTLS_E_AGAIN);
3790 #endif
3792 /* This will let assuan_process_next() return. */
3793 if (fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK) == -1)
3795 cl->last_rc = gpg_error_from_errno (errno);
3796 ret = cl->last_rc;
3799 cl->last_rc = 0; // BYE command result
3800 return ret;
3803 static gpg_error_t
3804 reset_notify (assuan_context_t ctx, char *line)
3806 struct client_s *client = assuan_get_pointer (ctx);
3808 if (client)
3809 reset_client (client);
3811 return 0;
3815 * This is called before every Assuan command.
3817 static gpg_error_t
3818 command_startup (assuan_context_t ctx, const char *name)
3820 struct client_s *client = assuan_get_pointer (ctx);
3821 gpg_error_t rc;
3822 struct command_table_s *cmd = NULL;
3824 log_write2 ("command='%s'", name);
3825 client->last_rc = client->opts = 0;
3827 for (int i = 0; command_table[i]; i++)
3829 if (!strcasecmp (name, command_table[i]->name))
3831 if (command_table[i]->ignore_startup)
3832 return 0;
3833 cmd = command_table[i];
3834 break;
3838 if (!cmd)
3839 return GPG_ERR_UNKNOWN_COMMAND;
3841 client->last_rc = rc = gpg_error (file_modified (client, cmd));
3842 if (!rc)
3843 update_client_state (client, CLIENT_STATE_COMMAND);
3845 return rc;
3849 * This is called after every Assuan command.
3851 static void
3852 command_finalize (assuan_context_t ctx, gpg_error_t rc)
3854 struct client_s *client = assuan_get_pointer (ctx);
3856 if (!(client->flags & FLAG_LOCK_CMD))
3857 unlock_file_mutex (client, 0);
3859 unlock_flock (&client->flock_fd);
3860 log_write2 (_("command completed: rc=%u"), rc ? rc : client->last_rc);
3861 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
3862 #ifdef WITH_GNUTLS
3863 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
3864 #endif
3865 if (client->thd->state != CLIENT_STATE_DISCON)
3866 update_client_state (client, CLIENT_STATE_IDLE);
3869 static gpg_error_t
3870 parse_help_opt_html (void *data, void *value)
3872 struct client_s *client = data;
3874 (void) value;
3875 client->opts |= OPT_HTML;
3876 return 0;
3879 static gpg_error_t
3880 help_command (assuan_context_t ctx, char *line)
3882 gpg_error_t rc;
3883 int i;
3884 struct client_s *client = assuan_get_pointer (ctx);
3885 struct argv_s *args[] = {
3886 &(struct argv_s) {"html", OPTION_TYPE_NOARG, parse_help_opt_html},
3887 NULL
3890 rc = parse_options (&line, args, client, 1);
3891 if (rc)
3892 return send_error (ctx, rc);
3894 if (!line || !*line)
3896 char *tmp;
3897 char *help = str_dup (_("Usage: HELP [--html] [<COMMAND>]\n"
3898 "For commands that take an element path as an argument, each element is "
3899 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3900 "@*@*COMMANDS:"));
3902 for (i = 0; command_table[i]; i++)
3904 if (!command_table[i]->help)
3905 continue;
3907 /* @npxref{} won't put a "See " or "see " in front of the command.
3908 * It's not a texinfo command but needed for --html. */
3909 tmp = str_asprintf ("%s %s%s%s", help,
3910 client->opts & OPT_HTML ? "@npxref{" : "",
3911 command_table[i]->name,
3912 client->opts & OPT_HTML ? "}" : "");
3913 xfree (help);
3914 help = tmp;
3917 tmp = strip_texi_and_wrap (help, client->opts & OPT_HTML);
3918 xfree (help);
3919 pthread_cleanup_push ((void *)xfree, tmp);
3920 rc = xfer_data (ctx, tmp, strlen (tmp));
3921 pthread_cleanup_pop (1);
3922 return send_error (ctx, rc);
3925 for (i = 0; command_table[i]; i++)
3927 if (!strcasecmp (line, command_table[i]->name))
3929 char *help, *tmp;
3931 if (!command_table[i]->help)
3932 break;
3934 help = strip_texi_and_wrap (command_table[i]->help,
3935 client->opts & OPT_HTML);
3936 tmp = str_asprintf ("%s%s",
3937 (client->opts & OPT_HTML) ? "" : _("Usage: "),
3938 help);
3939 xfree (help);
3940 pthread_cleanup_push ((void *)xfree, tmp);
3941 rc = xfer_data (ctx, tmp, strlen (tmp));
3942 pthread_cleanup_pop (1);
3943 return send_error (ctx, rc);
3947 return send_error (ctx, GPG_ERR_INV_NAME);
3950 static void
3951 new_command (const char *name, int ignore, int unlock, int flock_type,
3952 gpg_error_t (*handler) (assuan_context_t, char *),
3953 const char *help)
3955 int i = 0;
3956 struct command_table_s **tmp;
3958 if (command_table)
3959 for (i = 0; command_table[i]; i++);
3961 tmp = xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
3962 assert (tmp);
3963 command_table = tmp;
3964 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
3965 command_table[i]->name = name;
3966 command_table[i]->handler = handler;
3967 command_table[i]->ignore_startup = ignore;
3968 command_table[i]->unlock = unlock;
3969 command_table[i]->flock_type = flock_type;
3970 command_table[i++]->help = help;
3971 command_table[i] = NULL;
3974 void
3975 deinit_commands ()
3977 int i;
3979 for (i = 0; command_table[i]; i++)
3980 xfree (command_table[i]);
3982 xfree (command_table);
3985 static int
3986 sort_commands (const void *arg1, const void *arg2)
3988 struct command_table_s *const *a = arg1;
3989 struct command_table_s *const *b = arg2;
3991 if (!*a || !*b)
3992 return 0;
3993 else if (*a && !*b)
3994 return 1;
3995 else if (!*a && *b)
3996 return -1;
3998 return strcmp ((*a)->name, (*b)->name);
4001 static gpg_error_t
4002 passwd_command (assuan_context_t ctx, char *line)
4004 struct client_s *client = assuan_get_pointer (ctx);
4005 gpg_error_t rc;
4007 rc = peer_is_invoker (client);
4008 if (rc == GPG_ERR_EACCES)
4009 return send_error (ctx, GPG_ERR_FORBIDDEN);
4010 else if (rc)
4011 return send_error (ctx, rc);
4013 if (client->flags & FLAG_NEW)
4014 return send_error (ctx, GPG_ERR_INV_STATE);
4016 client->crypto->keyfile = config_get_string (client->filename,
4017 "passphrase_file");
4018 rc = crypto_init_ctx (client->crypto, client->flags & FLAG_NO_PINENTRY,
4019 client->crypto->keyfile);
4020 if (rc)
4021 return send_error (ctx, rc);
4023 if (!rc)
4024 rc = crypto_passwd (client, client->crypto);
4026 crypto_free_non_keys (client->crypto);
4027 return send_error (ctx, rc);
4030 static gpg_error_t
4031 parse_opt_data (void *data, void *value)
4033 struct client_s *client = data;
4035 (void) value;
4036 client->opts |= OPT_DATA;
4037 return 0;
4040 static gpg_error_t
4041 send_client_list (assuan_context_t ctx)
4043 struct client_s *client = assuan_get_pointer (ctx);
4044 gpg_error_t rc = 0;
4046 if (client->opts & OPT_VERBOSE)
4048 unsigned i, t;
4049 char **list = NULL;
4050 char *line;
4052 MUTEX_LOCK (&cn_mutex);
4053 pthread_cleanup_push ((void *)release_mutex_cb, &cn_mutex);
4054 t = slist_length (cn_thread_list);
4056 for (i = 0; i < t; i++)
4058 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
4059 char *tmp;
4061 if (thd->state == CLIENT_STATE_UNKNOWN)
4062 continue;
4064 tmp = build_client_info_line (thd, 0);
4065 if (tmp)
4067 char **l = strv_cat (list, tmp);
4068 if (!l)
4069 rc = GPG_ERR_ENOMEM;
4070 else
4071 list = l;
4073 else
4074 rc = GPG_ERR_ENOMEM;
4076 if (rc)
4078 strv_free (list);
4079 break;
4083 pthread_cleanup_pop (1);
4084 if (rc)
4085 return rc;
4087 line = strv_join ("\n", list);
4088 strv_free (list);
4089 pthread_cleanup_push ((void *)xfree, line);
4090 rc = xfer_data (ctx, line, strlen (line));
4091 pthread_cleanup_pop (1);
4092 return rc;
4095 if (client->opts & OPT_DATA)
4097 char buf[ASSUAN_LINELENGTH];
4099 MUTEX_LOCK (&cn_mutex);
4100 snprintf (buf, sizeof (buf), "%u", slist_length (cn_thread_list));
4101 MUTEX_UNLOCK (&cn_mutex);
4102 rc = xfer_data (ctx, buf, strlen (buf));
4104 else
4105 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4107 return rc;
4110 static gpg_error_t
4111 getinfo_command (assuan_context_t ctx, char *line)
4113 struct client_s *client = assuan_get_pointer (ctx);
4114 gpg_error_t rc;
4115 char buf[ASSUAN_LINELENGTH];
4116 struct argv_s *args[] = {
4117 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4118 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_opt_verbose},
4119 NULL
4122 rc = parse_options (&line, args, client, 1);
4123 if (rc)
4124 return send_error (ctx, rc);
4126 if (!strcasecmp (line, "clients"))
4128 rc = send_client_list (ctx);
4130 else if (!strcasecmp (line, "cache"))
4132 if (client->opts & OPT_DATA)
4134 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4135 rc = xfer_data (ctx, buf, strlen (buf));
4137 else
4138 rc = send_status (ctx, STATUS_CACHE, NULL);
4140 else if (!strcasecmp (line, "pid"))
4142 char buf[32];
4143 pid_t pid = getpid ();
4145 snprintf (buf, sizeof (buf), "%i", pid);
4146 rc = xfer_data (ctx, buf, strlen (buf));
4148 else if (!strcasecmp (line, "version"))
4150 char *buf = str_asprintf ("0x%06x %s", VERSION_HEX,
4151 #ifdef WITH_GNUTLS
4152 "GNUTLS "
4153 #endif
4154 #ifdef WITH_LIBACL
4155 "ACL "
4156 #endif
4157 "");
4158 rc = xfer_data (ctx, buf, strlen (buf));
4159 xfree (buf);
4161 else if (!strcasecmp (line, "last_error"))
4163 if (client->last_error)
4164 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4165 else
4166 rc = GPG_ERR_NO_DATA;
4168 else if (!strcasecmp (line, "user"))
4170 char *user = NULL;
4172 #ifdef WITH_GNUTLS
4173 if (client->thd->remote)
4174 user = str_asprintf ("#%s", client->thd->tls->fp);
4175 else
4176 user = get_username (client->thd->peer->uid);
4177 #else
4178 user = get_username (client->thd->peer->uid);
4179 #endif
4180 if (user)
4182 pthread_cleanup_push ((void *)xfree, user);
4183 rc = xfer_data (ctx, user, strlen (user));
4184 pthread_cleanup_pop (1);
4186 else
4187 rc = GPG_ERR_NO_DATA;
4189 else
4190 rc = gpg_error (GPG_ERR_SYNTAX);
4192 return send_error (ctx, rc);
4195 static gpg_error_t
4196 parse_listkeys_opt_secret_only (void *data, void *value)
4198 struct client_s *client = data;
4200 (void) value;
4201 client->opts |= OPT_SECRET_ONLY;
4202 return 0;
4205 static gpg_error_t
4206 keyinfo_command (assuan_context_t ctx, char *line)
4208 struct client_s *client = assuan_get_pointer (ctx);
4209 gpg_error_t rc = 0;
4210 char **keys = NULL, **p = NULL;
4211 int sym = 0;
4213 if (line && *line)
4214 return send_error (ctx, GPG_ERR_SYNTAX);
4216 if (!(client->flags & FLAG_OPEN))
4217 return send_error (ctx, GPG_ERR_INV_STATE);
4219 if (client->flags & FLAG_NEW)
4220 return send_error (ctx, GPG_ERR_NO_DATA);
4222 rc = lock_flock (ctx, client->filename, FLOCK_TYPE_SH, &client->flock_fd);
4223 if (rc)
4224 return send_error (ctx, rc);
4226 rc = crypto_is_symmetric (client->filename);
4227 unlock_flock (&client->flock_fd);
4228 if (!rc)
4229 sym = 1;
4230 else if (rc != GPG_ERR_BAD_DATA)
4231 return send_error (ctx, rc);
4233 rc = 0;
4234 if (!sym)
4236 p = strv_catv(keys, client->crypto->pubkey);
4237 if (!p)
4238 rc = GPG_ERR_ENOMEM;
4241 if (!rc)
4243 keys = p;
4244 for (p = client->crypto->sigkey; p && *p; p++)
4246 if (!strv_printf(&keys, "S%s", *p))
4248 strv_free (keys);
4249 return send_error (ctx, GPG_ERR_ENOMEM);
4253 else
4254 rc = GPG_ERR_ENOMEM;
4256 if (!rc && keys)
4258 line = strv_join ("\n", keys);
4259 strv_free (keys);
4260 pthread_cleanup_push ((void *)xfree, line);
4261 if (line)
4262 rc = xfer_data (ctx, line, strlen (line));
4263 else
4264 rc = GPG_ERR_ENOMEM;
4266 pthread_cleanup_pop (1);
4268 else if (!keys)
4269 rc = GPG_ERR_NO_DATA;
4270 else
4271 strv_free (keys);
4273 return send_error (ctx, rc);
4276 static gpg_error_t
4277 kill_command (assuan_context_t ctx, char *line)
4279 struct client_s *client = assuan_get_pointer (ctx);
4280 gpg_error_t rc;
4281 unsigned i, t;
4283 if (!line || !*line)
4284 return send_error (ctx, GPG_ERR_SYNTAX);
4286 MUTEX_LOCK (&cn_mutex);
4287 pthread_cleanup_push ((void *)release_mutex_cb, &cn_mutex);
4288 t = slist_length (cn_thread_list);
4289 rc = GPG_ERR_ESRCH;
4291 for (i = 0; i < t; i++)
4293 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
4294 char *tmp = str_asprintf ("%p", thd->tid);
4296 if (strcmp (line, tmp))
4298 xfree (tmp);
4299 continue;
4302 xfree (tmp);
4303 rc = peer_is_invoker (client);
4304 if (!rc)
4306 #ifdef HAVE_PTHREAD_CANCEL
4307 pthread_cancel (thd->tid);
4308 #else
4309 pthread_kill (thd->tid, SIGUSR2);
4310 #endif
4311 break;
4313 else if (rc == GPG_ERR_EACCES)
4314 rc = GPG_ERR_FORBIDDEN;
4315 else if (rc)
4316 break;
4318 if (config_get_boolean ("global", "strict_kill"))
4319 break;
4321 #ifdef WITH_GNUTLS
4322 if (client->thd->remote && thd->remote)
4324 if (!thd->tls || !thd->tls->fp)
4326 rc = GPG_ERR_INV_STATE;
4327 break;
4329 if (!strcmp (client->thd->tls->fp, thd->tls->fp))
4331 #ifdef HAVE_PTHREAD_CANCEL
4332 pthread_cancel (thd->tid);
4333 #else
4334 pthread_kill (thd->tid, SIGUSR2);
4335 #endif
4336 break;
4339 else if (!client->thd->remote && !thd->remote)
4340 #endif
4342 if (client->thd->peer->uid == thd->peer->uid)
4344 #ifdef HAVE_PTHREAD_CANCEL
4345 pthread_cancel (thd->tid);
4346 #else
4347 pthread_kill (thd->tid, SIGUSR2);
4348 #endif
4351 break;
4354 pthread_cleanup_pop (1);
4355 return send_error (ctx, rc);
4358 static gpg_error_t
4359 listkeys_command (assuan_context_t ctx, char *line)
4361 struct client_s *client = assuan_get_pointer (ctx);
4362 struct crypto_s *crypto = NULL;
4363 char **pattern = NULL;
4364 gpgme_key_t *keys = NULL;
4365 char **result = NULL;
4366 gpg_error_t rc;
4367 struct argv_s *args[] = {
4368 &(struct argv_s) {"secret-only", OPTION_TYPE_NOARG,
4369 parse_listkeys_opt_secret_only },
4370 NULL
4373 rc = parse_options (&line, args, client, 1);
4374 if (rc)
4375 return send_error (ctx, rc);
4377 rc = crypto_init (&crypto, client->ctx, client->filename,
4378 client->flags & FLAG_NO_PINENTRY, NULL);
4379 if (rc)
4380 return send_error (ctx, rc);
4382 pthread_cleanup_push ((void *)crypto_free, crypto);
4383 pattern = str_split (line, ",", 0);
4384 pthread_cleanup_push ((void *)(void *)strv_free, pattern);
4385 rc = crypto_list_keys (crypto, pattern, client->opts & OPT_SECRET_ONLY,
4386 &keys);
4387 pthread_cleanup_pop (1);
4388 pthread_cleanup_pop (1);
4389 if (!rc)
4391 int i;
4393 for (i = 0; keys[i]; i++)
4395 char *p = crypto_key_info (keys[i]);
4396 char **r;
4398 if (!p)
4400 rc = GPG_ERR_ENOMEM;
4401 break;
4404 r = strv_cat (result, p);
4405 if (!r)
4407 rc = GPG_ERR_ENOMEM;
4408 break;
4411 result = r;
4414 if (!i)
4415 rc = GPG_ERR_NO_DATA;
4417 crypto_free_key_list (keys);
4420 if (!rc)
4422 line = strv_join ("\n", result);
4423 strv_free (result);
4424 pthread_cleanup_push ((void *)xfree, line);
4425 if (!line)
4426 rc = GPG_ERR_ENOMEM;
4427 else
4428 rc = xfer_data (ctx, line, strlen (line));
4430 pthread_cleanup_pop (1);
4432 else
4433 strv_free (result);
4435 return send_error (ctx, rc);
4438 void
4439 init_commands ()
4441 /* !BEGIN-HELP-TEXT!
4443 * This comment is used as a marker to generate the offline documentation
4444 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4445 * script to determine where commands begin and end.
4447 new_command("HELP", 1, 1, 0, help_command, _(
4448 "HELP [--html] [<COMMAND>]\n"
4449 "Show available commands or command specific help text."
4450 "@*@*"
4451 "The @option{--html} option will output the help text in HTML format."
4454 new_command("KILL", 1, 0, 0, kill_command, _(
4455 "KILL <thread_id>\n"
4456 "Terminates the client identified by @var{thread_id} and releases any file "
4457 "lock or other resources it has held. See @code{GETINFO} (@pxref{GETINFO}) "
4458 "for details about listing connected clients. An @code{invoking_user} "
4459 "(@pxref{Configuration}) may kill any client while others may only kill "
4460 "clients of the same @code{UID} or @abbr{TLS} fingerprint.\n"
4463 new_command("LISTKEYS", 0, 0, 0, listkeys_command, _(
4464 "LISTKEYS [--secret-only] [pattern[,<pattern>]]\n"
4465 "Returns a new line separated list of key information matching a comma "
4466 "separated list of @var{pattern}'s. When option @option{--secret-only} is "
4467 "specified, only keys matching @var{pattern} that also have a secret key "
4468 "available will be returned."
4471 new_command("KEYINFO", 1, 0, 0, keyinfo_command, _(
4472 "KEYINFO\n"
4473 "Returns a new line separated list of key ID's that the currently opened "
4474 "data file has recipients and signers for. If the key is a signing key it "
4475 "will be prefixed with an @code{S}. If the file is a new one, or has no "
4476 "signers in the case of being symmetrically encrypted, the error code "
4477 "@code{GPG_ERR_NO_DATA} is returned."
4480 new_command("GETINFO", 1, 1, 0, getinfo_command, _(
4481 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4482 "Get server and other information. The information is returned via a status "
4483 "message (@pxref{Status Messages}) unless otherwise noted or @option{--data} "
4484 "is specified."
4485 "@*@*"
4486 "@var{CACHE} returns the number of cached documents."
4487 "@*@*"
4488 "@var{CLIENTS} returns the number of "
4489 "connected clients via a status message or a list of connected clients when "
4490 "the @option{--verbose} parameter is used (implies @option{--data}). A "
4491 "verbose line of a client list contains "
4492 "space delimited "
4493 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4494 "IP address if remote, file lock status, whether the current client is self "
4495 "or not, client state (see below), "
4496 "user ID or TLS fingerprint of the connected client, username if the "
4497 "client is a local one else @code{-}, and finally the time stamp of when the "
4498 "client connected."
4499 "@*@*"
4500 "Client state @code{0} is an unknown client state, state @code{1} indicates "
4501 "the client has connected but hasn't completed initializing, state @code{2} "
4502 "indicates that the client is idle, state @code{3} means the "
4503 "client is in a command and state @code{4} means the client is disconnecting. "
4504 "@*@*"
4505 "@var{PID} returns the process ID number of the server via a data response."
4506 "@*@*"
4507 "@var{VERSION} returns the server version number and compile-time features "
4508 "via a data response with each being space delimited."
4509 "@*@*"
4510 "@var{LAST_ERROR} returns a detailed description of the last failed command "
4511 "via a data response, when available."
4512 "@*@*"
4513 "@var{USER} returns the username or @abbr{TLS} hash of the connected client "
4514 "via a data response."
4517 new_command("PASSWD", 0, 0, FLOCK_TYPE_EX|FLOCK_TYPE_KEEP, passwd_command, _(
4518 "PASSWD\n"
4519 "Changes the passphrase of the secret key required to open the current "
4520 "data file. If the data file is symmetrically encrypted, the error "
4521 "@code{GPG_ERR_NOT_SUPPORTED} is returned. When symmetrically encrypted, "
4522 "the @code{SAVE} command (@pxref{SAVE}) should be used instead to prevent "
4523 "this command saving any unwanted changes to the @abbr{XML} document."
4524 "@*@*"
4525 "This command is not available to non-invoking clients "
4526 "(@pxref{Access Control})."
4529 new_command("OPEN", 1, 1, FLOCK_TYPE_SH|FLOCK_TYPE_KEEP, open_command, _(
4530 "OPEN [--lock] <filename>\n"
4531 "Opens @var{filename}. When the @var{filename} is not found on the "
4532 "file-system then a new in-memory document will be created. If the file is "
4533 "found, it is looked for in the file cache and when found no passphrase will "
4534 "be required to open it. When not cached, @cite{pinentry(1)} will be used to "
4535 "retrieve the passphrase for decryption unless @option{disable-pinentry} "
4536 "(@pxref{OPTION}) was specified in which case @command{pwmd} will "
4537 "@emph{INQUIRE} the client for the passphrase."
4538 "@*@*"
4539 "When the @option{--lock} option is passed then the file mutex will be "
4540 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4541 "file had been opened."
4544 new_command("SAVE", 0, 0, FLOCK_TYPE_EX|FLOCK_TYPE_KEEP, save_command, _(
4545 "SAVE [--symmetric] [--inquire-keyparam] [--keyid=<fpr>[,..] | [--inquire-keyid]] [--sign-keyid=<fpr>[,..] | [--inquire-sign-keyid]]\n"
4546 "Writes the in-memory @abbr{XML} document to disk. The file written to is the "
4547 "file that was opened using the @code{OPEN} command (@pxref{OPEN}). If the "
4548 "file is a new one or the option @option{--inquire-keyparam} was passed, a "
4549 "new keypair will be generated and @cite{pinentry(1)} will be used to prompt "
4550 "for the passphrase of the secret key used for signing."
4551 "@*@*"
4552 "The @option{--inquire-keyparam} option will send an "
4553 "@emph{INQUIRE} to the client to obtain the key parameters to use for "
4554 "generating the new keypair. The inquired data is expected to be in "
4555 "@cite{gnupg} @abbr{XML} format. See the @cite{gnupg} documentation for "
4556 "details. Note that when this option is specified a new keypair will be "
4557 "generated reguardless if the file is a new one and that the passphrase for "
4558 "the current file will be required before generating the new keypair. This "
4559 "option is available to non-invoking clients (@pxref{Access Control}) only "
4560 "when the file is a new one."
4561 "@*@*"
4562 "You can encrypt the data file to a recipient other than the one that it "
4563 "was encrypted with by passing the @option{--keyid} or "
4564 "@option{--inquire-keyid} option with "
4565 "the fingerprint of a public encryption key as its argument. Use the "
4566 "@command{LISTKEYS} command (@pxref{LISTKEYS}) to show key information by "
4567 "pattern. The @option{--sign-keyid} or @option{--inquire-sign-keyid} option "
4568 "may also be used to sign the data "
4569 "file with an alternate key by specifying the fingerprint of a secret key. "
4570 "A passphrase to decrypt the data file "
4571 "will be required if one or more of the original encryption or signing keys "
4572 "are not found in either of these two options' arguments. The original "
4573 "encryption or signing keys will be used when either of these options are "
4574 "not specified."
4575 "@*@*"
4576 "The @option{--keyid} and @option{--sign-keyid} options are not available "
4577 "for non-invoking clients "
4578 "(@pxref{Access Control}) when the recipients or signers do not match those "
4579 "that were used when the file was @code{OPEN}'ed."
4580 "@*@*"
4581 "The @option{--symmetric} option specifies that a new data file be "
4582 "conventionally encrypted. These types of data files do not use a recipient "
4583 "public key but may be signed by using the @option{--sign-keyid} or "
4584 "@option{--inquire-sign-keyid} options. To remove all signers from a "
4585 "symmtrically encrypted data file, leave the option value empty. Note that "
4586 "you cannot change encryption schemes once a data file has been saved."
4589 new_command("ISCACHED", 1, 0, 0, iscached_command, _(
4590 "ISCACHED [--lock] [--agent [--sign]] <filename>\n"
4591 "Determines the file cache status of the specified @var{filename}. "
4592 "The default is to test whether the filename is cached in memory. Passing "
4593 "option @option{--agent} will test the @command{gpg-agent} cache for at most "
4594 "one cached key used for opening the data file (@pxref{OPEN}). To test if "
4595 "a signing key is cached, pass @option{--sign} along with @option{--agent}. "
4596 "Both the @option{--agent} and @option{--sign} options require an opened data "
4597 "file."
4598 "@*@*"
4599 "An @emph{OK} response is returned if the specified @var{filename} is found "
4600 "in the cache. If not found in the cache but exists on the filesystem "
4601 "then @code{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4602 "returned."
4603 "@*@*"
4604 "The @option{--lock} option will lock the file mutex of @var{filename} when "
4605 "the file exists; it does not need to be opened nor cached. The lock will be "
4606 "released when the client exits or sends the @code{UNLOCK} command "
4607 "(@pxref{UNLOCK}). When this option is passed the current data file is closed."
4610 new_command("CLEARCACHE", 1, 1, 0, clearcache_command, _(
4611 "CLEARCACHE [<filename>]\n"
4612 "Clears a file cache entry for all or the specified @var{filename}. Note that "
4613 "this will also clear any @command{gpg-agent} cached keys which may cause "
4614 "problems if another data file shares the same keys as @var{filename}."
4615 "@*@*"
4616 "When clearing all cache entries a permissions test is done against the "
4617 "current client based on the @var{access} configuration parameter in a "
4618 "@var{filename} section. Both a cache entry may be cleared and an error "
4619 "returned depending on cached data files and client permissions."
4622 new_command("CACHETIMEOUT", 1, 1, 0, cachetimeout_command, _(
4623 "CACHETIMEOUT <filename> <seconds>\n"
4624 "The time in @var{seconds} until @var{filename} will be removed from the "
4625 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4626 "the passphrase for each @code{OPEN} command (@pxref{OPEN}) or @code{SAVE} "
4627 "(@pxref{SAVE}) command. @xref{Configuration}, and the @code{cache_timeout} "
4628 "parameter."
4631 new_command("LIST", 0, 1, 0, list_command, _(
4632 "LIST [--inquire] [--recurse] [element[<TAB>child[..]]]\n"
4633 "If no element path is given then a newline separated list of root elements "
4634 "is returned with a data response. If given, then children of the specified "
4635 "element path are returned."
4636 "@*@*"
4637 "Each element path "
4638 "returned will have zero or more flags appened to it. These flags are "
4639 "delimited from the element path by a single space character. A flag itself "
4640 "is a single character. Flag @code{P} indicates that access to the element "
4641 "is denied. Flag @code{+} indicates that there are child nodes of "
4642 "the current element path. Flag @code{E} indicates that an element of the "
4643 "element path contained in a @var{target} attribute could not be found. Flag "
4644 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4645 "(@pxref{Configuration}). Flag @code{T}, followed by a single space character, "
4646 "then an element path, is the element path of the @var{target} attribute "
4647 "contained in the current element."
4648 "@*@*"
4649 "When a specified element path contains an error, beit from the final "
4650 "element in the path or any previous element, the path is still shown but "
4651 "will contain the error flag for the element with the error. Determining "
4652 "the actual element which contains the error is up to the client. This can be "
4653 "done by traversing the final element up to parent elements that contain the "
4654 "same error flag."
4655 "@*@*"
4656 "The option @option{--recurse} may be used to list the entire element tree "
4657 "for a specified element path or the entire tree for all root elements."
4658 "@*@*"
4659 "When the @option{--inquire} option is passed then all remaining non-option "
4660 "arguments are retrieved via a server @emph{INQUIRE}."
4663 new_command("REALPATH", 0, 1, 0, realpath_command, _(
4664 "REALPATH [--inquire] element[<TAB>child[..]]\n"
4665 "Resolves all @code{target} attributes of the specified element path and "
4666 "returns the result with a data response. @xref{Target Attribute}, for details."
4667 "@*@*"
4668 "When the @option{--inquire} option is passed then all remaining non-option "
4669 "arguments are retrieved via a server @emph{INQUIRE}."
4672 new_command("STORE", 0, 1, 0, store_command, _(
4673 "STORE element[<TAB>child[..]]<TAB>[content]\n"
4674 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4675 "@*@*"
4676 "Creates a new element path or modifies the @var{content} of an existing "
4677 "element. If only a single element is specified then a new root element is "
4678 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4679 "set to the final @key{TAB} delimited element. If no @var{content} is "
4680 "specified after the final @key{TAB}, then the content of the existing "
4681 "element will be removed; or will be empty if creating a new element."
4682 "@*@*"
4683 "The only restriction of an element name is that it not contain whitespace "
4684 "characters. There is no other whitespace between the @key{TAB} delimited "
4685 "elements. It is recommended that the content of an element be base64 encoded "
4686 "when it contains control or @key{TAB} characters to prevent @abbr{XML} "
4687 "parsing and @command{pwmd} syntax errors."
4690 new_command("RENAME", 0, 1, 0, rename_command, _(
4691 "RENAME [--inquire] element[<TAB>child[..]] <value>\n"
4692 "Renames the specified @var{element} to the new @var{value}. If an element of "
4693 "the same name as the @var{value} already exists it will be overwritten."
4694 "@*@*"
4695 "When the @option{--inquire} option is passed then all remaining non-option "
4696 "arguments are retrieved via a server @emph{INQUIRE}."
4699 new_command("COPY", 0, 1, 0, copy_command, _(
4700 "COPY [--inquire] source[<TAB>child[..]] dest[<TAB>child[..]]\n"
4701 "Copies the entire element tree starting from the child node of the source "
4702 "element, to the destination element path. If the destination element path "
4703 "does not exist then it will be created; otherwise it is overwritten."
4704 "@*@*"
4705 "Note that attributes from the source element are merged into the "
4706 "destination element when the destination element path exists. When an "
4707 "attribute of the same name exists in both the source and destination "
4708 "elements then the destination attribute will be updated to the source "
4709 "attribute value."
4710 "@*@*"
4711 "When the @option{--inquire} option is passed then all remaining non-option "
4712 "arguments are retrieved via a server @emph{INQUIRE}."
4715 new_command("MOVE", 0, 1, 0, move_command, _(
4716 "MOVE [--inquire] source[<TAB>child[..]] [dest[<TAB>child[..]]]\n"
4717 "Moves the source element path to the destination element path. If the "
4718 "destination is not specified then it will be moved to the root node of the "
4719 "document. If the destination is specified and exists then it will be "
4720 "overwritten; otherwise non-existing elements of the destination element "
4721 "path will be created."
4722 "@*@*"
4723 "When the @option{--inquire} option is passed then all remaining non-option "
4724 "arguments are retrieved via a server @emph{INQUIRE}."
4727 new_command("DELETE", 0, 1, 0, delete_command, _(
4728 "DELETE [--inquire] element[<TAB>child[..]]\n"
4729 "Removes the specified element path and all of its children. This may break "
4730 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4731 "refers to this element or any of its children."
4732 "@*@*"
4733 "When the @option{--inquire} option is passed then all remaining non-option "
4734 "arguments are retrieved via a server @emph{INQUIRE}."
4737 new_command("GET", 0, 1, 0, get_command, _(
4738 "GET [--inquire] element[<TAB>child[..]]\n"
4739 "Retrieves the content of the specified element. The content is returned "
4740 "with a data response."
4741 "@*@*"
4742 "When the @option{--inquire} option is passed then all remaining non-option "
4743 "arguments are retrieved via a server @emph{INQUIRE}."
4746 new_command("ATTR", 0, 1, 0, attr_command, _(
4747 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] element[<TAB>child[..]] ..\n"
4748 "@table @asis\n"
4749 "@item ATTR SET attribute element[<TAB>child[..]] [value]\n"
4750 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4751 "element. When no @var{value} is specified any existing value will be removed."
4752 "@*@*"
4753 "@item ATTR DELETE attribute element[<TAB>child[..]]\n"
4754 " Removes an attribute from an element. If @var{attribute} is @code{_name} "
4755 "or @code{target} an error is returned. Use the @command{DELETE} command "
4756 "(@pxref{DELETE}) instead."
4757 "@*@*"
4758 "@item ATTR LIST element[<TAB>child[..]]\n"
4759 " Retrieves a newline separated list of attributes names and values "
4760 "from the specified element. Each attribute name and value is space delimited."
4761 "@*@*"
4762 "@item ATTR GET attribute element[<TAB>child[..]]\n"
4763 " Retrieves the value of an @var{attribute} from an element."
4764 "@end table\n"
4765 "@*@*"
4766 "When the @option{--inquire} option is passed then all remaining non-option "
4767 "arguments are retrieved via a server @emph{INQUIRE}."
4768 "@*@*"
4769 "@xref{Target Attribute}, for details about this special attribute and also "
4770 "@pxref{Other Attributes} for other attributes that are handled specially "
4771 "by @command{pwmd}."
4774 new_command("XPATH", 0, 1, 0, xpath_command, _(
4775 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4776 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4777 "specified it is assumed the expression is a request to return a result. "
4778 "Otherwise, the result is set to the @var{value} argument and the document is "
4779 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4780 "is assumed to be empty and the document is updated. For example:"
4781 "@sp 1\n"
4782 "@example\n"
4783 "XPATH //element[@@_name='password']@key{TAB}\n"
4784 "@end example\n"
4785 "@sp 1\n"
4786 "would clear the content of all @var{password} elements in the data file "
4787 "while leaving off the trailing @key{TAB} would return all @var{password} "
4788 "elements in @abbr{XML} format."
4789 "@*@*"
4790 "When the @option{--inquire} option is passed then all remaining non-option "
4791 "arguments are retrieved via a server @emph{INQUIRE}."
4792 "@*@*"
4793 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4794 "expression syntax."
4797 new_command("XPATHATTR", 0, 1, 0, xpathattr_command, _(
4798 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4799 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4800 "attributes and does not return a result. For the @var{SET} operation the "
4801 "@var{value} is optional but the field is required. If not specified then "
4802 "the attribute value will be empty. For example:"
4803 "@sp 1\n"
4804 "@example\n"
4805 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4806 "@end example\n"
4807 "@sp 1\n"
4808 "would create a @var{password} attribute for each @var{password} element "
4809 "found in the document. The attribute value will be empty but still exist."
4810 "@*@*"
4811 "When the @option{--inquire} option is passed then all remaining non-option "
4812 "arguments are retrieved via a server @emph{INQUIRE}."
4813 "@*@*"
4814 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4815 "expression syntax."
4818 new_command("IMPORT", 0, 1, 0, import_command, _(
4819 "IMPORT [--root=element[<TAB>child[..]]]\n"
4820 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4821 "@*@*"
4822 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4823 "argument is raw @abbr{XML} data. The content is created as a child of "
4824 "the element path specified with the @option{--root} option or at the "
4825 "document root when not specified. Existing elements of the same name will "
4826 "be overwritten."
4827 "@*@*"
4828 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4829 "for details."
4832 new_command("DUMP", 0, 1, 0, dump_command, _(
4833 "DUMP\n"
4834 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4835 "dumping a specific node."
4838 new_command("LOCK", 0, 0, 0, lock_command, _(
4839 "LOCK\n"
4840 "Locks the mutex associated with the opened file. This prevents other clients "
4841 "from sending commands to the same opened file until the client "
4842 "that sent this command either disconnects or sends the @code{UNLOCK} "
4843 "command. @xref{UNLOCK}."
4846 new_command("UNLOCK", 1, 0, 0, unlock_command, _(
4847 "UNLOCK\n"
4848 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4849 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4850 "@pxref{ISCACHED})."
4853 new_command("GETCONFIG", 1, 1, 0, getconfig_command, _(
4854 "GETCONFIG [filename] <parameter>\n"
4855 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4856 "data response. If no file has been opened then the value for @var{filename} "
4857 "or the default from the @var{global} section will be returned. If a file "
4858 "has been opened and no @var{filename} is specified, the value previously "
4859 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4862 new_command("OPTION", 1, 1, 0, option_command, _(
4863 "OPTION <NAME>=[<VALUE>]\n"
4864 "Sets a client option @var{name} to @var{value}. The value for an option is "
4865 "kept for the duration of the connection with the exception of the "
4866 "@command{pinentry} options which are defaults for all future connections "
4867 "(@pxref{Pinentry}). When @var{value} is empty the option is unset."
4868 "@*@*"
4869 "@table @asis\n"
4870 "@item DISABLE-PINENTRY\n"
4871 "Disable use of @command{pinentry} for passphrase retrieval. When @code{1}, a "
4872 "server inquire is sent to the client to obtain the passphrase. This option "
4873 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4874 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands. Set to @code{0} "
4875 "to use a @command{pinentry}."
4876 "@*@*"
4877 "@item DISPLAY\n"
4878 "Set or unset the X11 display to use when prompting for a passphrase."
4879 "@*@*"
4880 "@item TTYNAME\n"
4881 "Set the terminal device path to use when prompting for a passphrase."
4882 "@*@*"
4883 "@item TTYTYPE\n"
4884 "Set the terminal type for use with @option{TTYNAME}."
4885 "@*@*"
4886 "@item NAME\n"
4887 "Associates the thread ID of the connection with the specified textual "
4888 "representation. Useful for debugging log messages. May not contain whitespace."
4889 "@*@*"
4890 "@item LOCK-TIMEOUT\n"
4891 "When not @code{0}, the duration in tenths of a second to wait for the file "
4892 "mutex which has been locked by another thread to be released before returning "
4893 "an error. When @code{-1} the error will be returned immediately."
4894 "@*@*"
4895 "@item CLIENT-STATE\n"
4896 "When set to @code{1} then client state status messages for other clients are "
4897 "sent to the current client. The default is @code{0}."
4898 "@end table\n"
4901 new_command("LS", 1, 1, 0, ls_command, _(
4902 "LS\n"
4903 "Returns a newline separated list of data files stored in the data directory "
4904 "@file{HOMEDIR/data} (@pxref{Invoking}) with a data response."
4907 new_command("RESET", 1, 1, 0, NULL, _(
4908 "RESET\n"
4909 "Closes the currently opened file but keeps any previously set client options "
4910 "(@pxref{OPTION})."
4913 new_command("NOP", 1, 1, 0, NULL, _(
4914 "NOP\n"
4915 "Does nothing. Always returns successfully."
4918 /* !END-HELP-TEXT! */
4919 new_command ("CANCEL", 1, 1, 0, NULL, NULL);
4920 new_command ("END", 1, 1, 0, NULL, NULL);
4921 new_command ("BYE", 1, 1, 0, NULL, NULL);
4923 int i;
4924 for (i = 0; command_table[i]; i++);
4925 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4926 sort_commands);
4929 gpg_error_t
4930 register_commands (assuan_context_t ctx)
4932 int i = 0, rc;
4934 for (; command_table[i]; i++)
4936 if (!command_table[i]->handler)
4937 continue;
4939 rc = assuan_register_command (ctx, command_table[i]->name,
4940 command_table[i]->handler,
4941 command_table[i]->help);
4942 if (rc)
4943 return rc;
4946 rc = assuan_register_bye_notify (ctx, bye_notify);
4947 if (rc)
4948 return rc;
4950 rc = assuan_register_reset_notify (ctx, reset_notify);
4951 if (rc)
4952 return rc;
4954 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4955 if (rc)
4956 return rc;
4958 return assuan_register_post_cmd_notify (ctx, command_finalize);