Fix some Coverity warnings and other fixes.
[libpwmd.git] / src / commands.c
blobead40edb7fbf1d1d0a85805df868bedaffd72ea0
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"
52 #include "acl.h"
54 /* These are command option flags. */
55 #define OPT_INQUIRE 0x0001
56 #define OPT_NO_PASSPHRASE 0x0002
57 #define OPT_ASK 0x0004
58 #define OPT_LIST_RECURSE 0x0008
59 #define OPT_VERBOSE 0x0010
60 #define OPT_LOCK 0x0020
61 #define OPT_LOCK_ON_OPEN 0x0040
62 #define OPT_SIGN 0x0080
63 #define OPT_LIST_ALL 0x0100
64 #define OPT_DATA 0x0200
65 #define OPT_NO_AGENT 0x0400
66 #define OPT_SECRET_ONLY 0x0800
67 #define OPT_INQUIRE_KEYID 0x1000
68 #define OPT_INQUIRE_SIGN_KEYID 0x2000
69 #define OPT_SYMMETRIC 0x4000
70 #define OPT_NO_SIGNER 0x8000
71 #define OPT_HTML 0x10000
72 #define OPT_CACHE_AGENT 0x20000
73 #define OPT_CACHE_SIGN 0x40000
75 #define FLOCK_TYPE_NONE 0
76 #define FLOCK_TYPE_SH 0x0001
77 #define FLOCK_TYPE_EX 0x0002
78 #define FLOCK_TYPE_KEEP 0x0004
80 struct command_table_s
82 const char *name;
83 gpg_error_t (*handler) (assuan_context_t, char *line);
84 const char *help;
85 int ignore_startup;
86 int unlock; // unlock the file mutex after validating the checksum
87 uint32_t flock_type;
90 static struct command_table_s **command_table;
92 static gpg_error_t do_lock (struct client_s *client, int add);
93 static gpg_error_t validate_checksum (struct client_s *, const char *filename,
94 struct cache_data_s *, unsigned char **,
95 size_t *);
96 static gpg_error_t update_checksum (struct client_s *client);
98 /* When 'status' is true the 'self' field of the status line will be false
99 * because we never send the STATE status message to the same client that
100 * initiated it. */
101 static char *
102 build_client_info_line (struct client_thread_s *thd, int status)
104 char *uid, *username = NULL;
105 char *line;
106 char *name = pthread_getspecific (thread_name_key);
108 #ifdef WITH_GNUTLS
109 if (thd->remote)
110 uid = str_asprintf("#%s", thd->tls->fp);
111 else
113 uid = str_asprintf("%u", thd->peer->uid);
114 username = get_username (thd->peer->uid);
116 #else
117 uid = str_asprintf("%u", thd->peer->uid);
118 username = get_username (thd->peer->uid);
119 #endif
120 line = str_asprintf ("%p %s %s %s %u %u %u %s %s %u",
121 thd->tid,
122 name ? name : "-",
123 thd->cl && thd->cl->filename
124 && (thd->cl->flags & FLAG_OPEN)
125 ? thd->cl->filename : "/",
126 #ifdef WITH_GNUTLS
127 thd->remote ? thd->peeraddr : "-",
128 #else
129 "-",
130 #endif
131 thd->cl && thd->cl->flags & FLAG_HAS_LOCK ? 1 : 0,
132 !status && pthread_equal (pthread_self (), thd->tid) ? 1 : 0,
133 thd->state, uid,
134 #ifdef WITH_GNUTLS
135 thd->remote ? "-" : username,
136 #else
137 username,
138 #endif
139 thd->conntime
142 xfree (username);
143 xfree (uid);
144 return line;
147 void
148 update_client_state (struct client_s *client, unsigned s)
150 MUTEX_LOCK (&cn_mutex);
151 client->thd->state = s;
152 MUTEX_UNLOCK (&cn_mutex);
154 if (client->thd->state != CLIENT_STATE_UNKNOWN)
156 char *line = build_client_info_line (client->thd, 1);
158 pthread_cleanup_push (xfree, line);
159 if (line)
160 send_status_all_not_self (STATUS_STATE, "%s", line);
161 pthread_cleanup_pop (1);
165 static gpg_error_t
166 unlock_file_mutex (struct client_s *client, int remove)
168 gpg_error_t rc = 0;
170 // OPEN: keep the lock for the same file being reopened.
171 if (client->flags & FLAG_KEEP_LOCK && client->flags & FLAG_HAS_LOCK)
172 return 0;
174 if (!(client->flags & FLAG_HAS_LOCK))
175 return GPG_ERR_NOT_LOCKED;
177 rc = cache_unlock_mutex (client->filename, remove);
178 if (rc)
179 rc = GPG_ERR_INV_STATE;
180 else
181 client->flags &= ~(FLAG_HAS_LOCK | FLAG_LOCK_CMD);
183 return rc;
186 static gpg_error_t
187 lock_file_mutex (struct client_s *client, int add)
189 gpg_error_t rc = 0;
190 int timeout = config_get_integer (client->filename, "cache_timeout");
192 if (client->flags & FLAG_HAS_LOCK)
193 return 0;
195 rc = cache_lock_mutex (client->ctx, client->filename,
196 client->lock_timeout, add, timeout);
197 if (!rc)
198 client->flags |= FLAG_HAS_LOCK;
200 return rc;
203 static gpg_error_t
204 file_modified (struct client_s *client, struct command_table_s *cmd)
206 gpg_error_t rc = 0;
207 int type = !cmd->flock_type || (cmd->flock_type & FLOCK_TYPE_SH)
208 ? LOCK_SH : LOCK_EX;
210 if (!(client->flags & FLAG_OPEN))
211 return GPG_ERR_INV_STATE;
213 rc = lock_file_mutex (client, 0);
214 if (rc && rc != GPG_ERR_NO_DATA)
215 return rc;
217 rc = lock_flock (client->ctx, client->filename, type, &client->flock_fd);
218 if (!rc)
220 rc = validate_checksum (client, client->filename, NULL, NULL, NULL);
221 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
222 rc = 0;
223 else if (rc)
224 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
226 else if (gpg_err_code (rc) == GPG_ERR_ENOENT)
227 rc = 0;
229 /* FLAG_HAS_LOCK may have been set by the LOCK command or OPEN --lock. */
230 if ((cmd->unlock && !(client->flags & FLAG_HAS_LOCK)) || rc)
231 unlock_file_mutex (client, 0);
233 if (rc || !(cmd->flock_type & FLOCK_TYPE_KEEP))
234 unlock_flock (&client->flock_fd);
236 return rc;
239 static gpg_error_t
240 parse_xml (assuan_context_t ctx, int new)
242 struct client_s *client = assuan_get_pointer (ctx);
243 int cached = client->doc != NULL;
244 gpg_error_t rc = 0;
246 if (new)
248 client->doc = xml_new_document ();
249 if (client->doc)
251 xmlChar *result;
252 int len;
254 xmlDocDumpFormatMemory (client->doc, &result, &len, 0);
255 client->crypto->plaintext = result;
256 client->crypto->plaintext_size = len;
257 if (!client->crypto->plaintext)
259 xmlFreeDoc (client->doc);
260 client->doc = NULL;
261 rc = GPG_ERR_ENOMEM;
264 else
265 rc = GPG_ERR_ENOMEM;
267 else if (!cached)
268 rc = xml_parse_doc ((char *) client->crypto->plaintext,
269 client->crypto->plaintext_size,
270 (xmlDocPtr *)&client->doc);
272 return rc;
275 static void
276 free_client (struct client_s *client)
278 if (client->doc)
279 xmlFreeDoc (client->doc);
281 xfree (client->crc);
282 xfree (client->filename);
283 xfree (client->last_error);
284 crypto_free (client->crypto);
285 client->crypto = NULL;
288 void
289 reset_client (struct client_s *client)
291 assuan_context_t ctx = client->ctx;
292 struct client_thread_s *thd = client->thd;
293 long lock_timeout = client->lock_timeout;
294 xmlErrorPtr xml_error = client->xml_error;
295 int no_pinentry = (client->flags & FLAG_NO_PINENTRY);
296 int flock_fd = client->flock_fd;
298 unlock_file_mutex (client, client->flags & FLAG_NEW);
299 free_client (client);
300 memset (client, 0, sizeof (struct client_s));
301 client->flock_fd = flock_fd;
302 client->xml_error = xml_error;
303 client->ctx = ctx;
304 client->thd = thd;
305 client->lock_timeout = lock_timeout;
306 client->flags |= no_pinentry ? FLAG_NO_PINENTRY : 0;
309 static void
310 req_free (void *arg)
312 if (!arg)
313 return;
315 strv_free ((char **) arg);
318 static gpg_error_t
319 parse_open_opt_lock (void *data, void *value)
321 struct client_s *client = data;
323 (void)value;
324 client->opts |= OPT_LOCK_ON_OPEN;
325 return 0;
328 static gpg_error_t
329 parse_opt_inquire (void *data, void *value)
331 struct client_s *client = data;
333 (void) value;
334 client->opts |= OPT_INQUIRE;
335 return 0;
338 static gpg_error_t
339 update_checksum (struct client_s *client)
341 unsigned char *crc;
342 size_t len;
343 struct cache_data_s *cdata;
344 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
346 if (rc)
347 return rc;
349 xfree (client->crc);
350 client->crc = crc;
351 cdata = cache_get_data (client->filename);
352 if (cdata)
354 xfree (cdata->crc);
355 cdata->crc = xmalloc (len);
356 memcpy (cdata->crc, crc, len);
359 return 0;
362 static gpg_error_t
363 validate_checksum (struct client_s *client, const char *filename,
364 struct cache_data_s *cdata, unsigned char **r_crc,
365 size_t *r_crclen)
367 unsigned char *crc;
368 size_t len;
369 gpg_error_t rc;
370 int n = 0;
372 if (cdata && !cdata->crc)
373 return GPG_ERR_CHECKSUM;
375 rc = get_checksum (filename, &crc, &len);
376 if (rc)
377 return rc;
379 if (cdata)
380 n = memcmp (cdata->crc, crc, len);
381 else if (client->crc)
382 n = memcmp (client->crc, crc, len);
384 if (!n && r_crc)
386 *r_crc = crc;
387 *r_crclen = len;
389 else
390 xfree (crc);
392 return n ? GPG_ERR_CHECKSUM : 0;
395 static gpg_error_t
396 open_command (assuan_context_t ctx, char *line)
398 gpg_error_t rc;
399 struct client_s *client = assuan_get_pointer (ctx);
400 char **req, *filename;
401 int same_file = 0;
402 assuan_peercred_t peer;
403 struct cache_data_s *cdata = NULL;
404 int cached = 0;
405 unsigned char *crc = NULL;
406 size_t crclen = 0;
407 struct argv_s *args[] = {
408 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
409 NULL
412 rc = parse_options (&line, args, client, 1);
413 if (rc)
414 return send_error (ctx, rc);
416 req = str_split (line, " ", 2);
417 if (!req)
418 return send_error (ctx, GPG_ERR_SYNTAX);
420 rc = do_validate_peer (ctx, req[0], &peer);
421 if (rc == GPG_ERR_FORBIDDEN)
422 rc = peer_is_invoker (client);
424 if (rc)
426 strv_free (req);
427 return send_error (ctx, rc);
430 filename = req[0];
431 if (!valid_filename (filename))
433 strv_free (req);
434 return send_error (ctx, GPG_ERR_INV_VALUE);
437 pthread_cleanup_push ((void *)req_free, req);
438 /* This client may have locked a different file with ISCACHED --lock than
439 * the current filename. This will remove that lock. */
440 same_file = client->filename && !strcmp (filename, client->filename);
441 if (client->flags & FLAG_OPEN ||
442 (client->flags & FLAG_HAS_LOCK && !same_file))
444 uint32_t opts = client->opts;
445 uint32_t flags = client->flags;
447 if (same_file)
448 client->flags |= FLAG_KEEP_LOCK;
449 else if (client->flags & FLAG_NEW)
450 cache_clear (NULL, client->filename, 0);
452 reset_client (client);
453 client->opts = opts;
454 client->flags |= flags;
455 client->flags &= ~(FLAG_LOCK_CMD);
456 if (!same_file)
457 client->flags &= ~(FLAG_HAS_LOCK | FLAG_NEW);
460 client->filename = str_dup (filename);
461 if (!client->filename)
463 strv_free (req);
464 return send_error(ctx, GPG_ERR_ENOMEM);
467 /* Need to lock the mutex here because file_modified() cannot without
468 * knowing the filename. */
469 rc = lock_file_mutex (client, 1);
470 if (rc)
471 client->flags &= ~FLAG_OPEN;
473 if (!rc)
475 rc = lock_flock (ctx, filename, LOCK_SH, &client->flock_fd);
476 if (gpg_err_code (rc) == GPG_ERR_ENOENT)
477 rc = 0;
480 if (!rc)
482 struct stat st;
483 char *keyfile = config_get_string (client->filename, "passphrase_file");
485 rc = crypto_init (&client->crypto, client->ctx, client->filename,
486 client->flags & FLAG_NO_PINENTRY, keyfile);
487 if (rc)
488 xfree (keyfile);
490 if (!rc && lstat (client->filename, &st) == -1)
491 rc = gpg_error_from_errno (errno);
492 else if (!rc && !S_ISREG (st.st_mode)) // to match LS output
493 rc = gpg_error_from_errno (ENODEV);
496 if (!rc || gpg_err_code (rc) == GPG_ERR_ENOENT)
498 cdata = cache_get_data (client->filename);
500 if (rc) // new file
502 rc = 0;
503 client->flags |= FLAG_NEW;
504 // data file disappeared. clear the cache entry.
505 cache_clear (NULL, client->filename, 1);
506 cdata = NULL;
508 else if (cdata && cdata->doc) // cached document
510 int reload = 0;
511 int defer = 0;
513 if (!rc && !(client->flags & FLAG_NEW))
515 rc = validate_checksum (client, client->filename, cdata, &crc,
516 &crclen);
517 if (rc == GPG_ERR_CHECKSUM)
519 rc = 0;
520 reload = 1;
524 if (!rc)
526 rc = cache_iscached (client->filename, &defer, 0, 0);
527 if ((!rc || rc == GPG_ERR_ENOENT) && defer)
529 rc = 0;
530 reload = 2;
534 if (!rc && reload)
536 if (reload == 1)
537 log_write ("%s: %s", client->filename,
538 pwmd_strerror (GPG_ERR_CHECKSUM));
539 cache_clear (NULL, client->filename, 1);
540 cdata = NULL;
541 rc = crypto_decrypt (client, client->crypto);
543 #ifdef WITH_GNUTLS
544 else if (!rc)
546 if (client->thd->remote
547 && config_get_boolean (client->filename, "tcp_require_key")
548 && !(client->flags & FLAG_NEW))
549 rc = crypto_try_decrypt (client,
550 (client->flags & FLAG_NO_PINENTRY));
552 #endif
554 if (!rc && cdata)
556 client->crypto->plaintext = cdata->doc;
557 client->crypto->plaintext_size = cdata->size;
558 rc = cache_decrypt (client->crypto);
559 if (!rc)
561 cdata->doc = NULL;
562 cdata->size = 0;
563 strv_free (client->crypto->pubkey);
564 strv_free (client->crypto->sigkey);
565 client->crypto->pubkey = strv_dup (cdata->pubkey);
566 client->crypto->sigkey = strv_dup (cdata->sigkey);
567 cached = 1;
569 else
571 client->crypto->plaintext = NULL;
572 client->crypto->plaintext_size = 0;
576 else // existing file
578 cached = cdata != NULL;
579 rc = crypto_decrypt (client, client->crypto);
583 if (!rc)
585 rc = parse_xml (ctx, client->flags & FLAG_NEW);
586 if (!rc)
588 rc = cache_encrypt (client->crypto);
589 if (rc)
590 cache_clear (NULL, client->filename, 1);
591 else
593 int timeout = config_get_integer (client->filename,
594 "cache_timeout");
596 cache_free_data_once (cdata);
597 cdata = xcalloc (1, sizeof (struct cache_data_s));
598 cdata->doc = client->crypto->plaintext;
599 cdata->size = client->crypto->plaintext_size;
600 cdata->pubkey = strv_dup(client->crypto->pubkey);
601 cdata->sigkey = strv_dup(client->crypto->sigkey);
602 client->crypto->plaintext = NULL;
603 client->crypto->plaintext_size = 0;
605 if (cached) // wont increment the refcount
607 /* Prevent using another FD to update the checksum for a
608 * cached data file. The validity has already been
609 * verified. */
610 xfree (client->crc);
611 client->crc = xmalloc (crclen);
612 memcpy (client->crc, crc, crclen);
613 xfree (cdata->crc);
614 cdata->crc = xmalloc (crclen);
615 memcpy (cdata->crc, crc, crclen);
617 rc = cache_set_data (client->filename, cdata);
618 /* The cache entry may have been removed and cache_set_data()
619 * already sent STATUS_CACHE. */
620 if (!cache_iscached (client->filename, NULL, 0, 0))
621 rc = send_status (ctx, STATUS_CACHE, NULL);
623 else
624 rc = cache_add_file (client->filename, cdata, timeout);
629 pthread_cleanup_pop (1);
630 xfree (crc);
632 if (!rc && !(client->flags & FLAG_NEW) && !cached)
633 rc = update_checksum (client);
635 if (!rc)
637 client->flags |= FLAG_OPEN;
639 if (client->flags & FLAG_NEW)
640 rc = send_status (ctx, STATUS_NEWFILE, NULL);
642 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
643 rc = do_lock (client, 0);
646 if (rc)
648 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
650 if (client->flags & FLAG_NEW)
651 cache_clear (NULL, client->filename, 1);
653 crypto_free (client->crypto);
654 client->crypto = NULL;
655 client->flags &= ~FLAG_OPEN;
657 else
658 crypto_free_non_keys (client->crypto);
660 return send_error (ctx, rc);
663 /* If not the invoking_user or is an existing file, check that the list of user
664 * supplied key ID's are in the list of current key ID's obtained when
665 * decrypting the data file.
667 static gpg_error_t
668 permitted_to_save (struct client_s *client, const char **keys,
669 const char **value)
671 gpg_error_t rc = 0;
672 const char **v;
674 if ((client->flags & FLAG_NEW) || (client->opts & OPT_SYMMETRIC))
675 return 0;
677 rc = peer_is_invoker (client);
678 if (!rc)
679 return 0;
681 /* Empty match. */
682 if ((!value && !keys) || ((value && !*value) && (keys && !*keys)))
683 return 0;
685 for (v = value; v && *v; v++)
687 const char **k, *pv = *v;
688 int match = 0;
690 if (*pv == '0' && *(pv+1) == 'x')
691 pv += 2;
693 for (k = keys; k && *k; k++)
695 const char *pk = *k;
697 if (*pk == '0' && *(pk+1) == 'x')
698 pk += 2;
700 rc = !strcmp (pv, pk) ? 0 : GPG_ERR_FORBIDDEN;
701 if (rc)
702 rc = !strcmp (pv, *k) ? 0 : GPG_ERR_FORBIDDEN;
704 if (!rc)
706 match = 1;
707 break;
711 if (!match)
712 return GPG_ERR_FORBIDDEN;
715 return rc;
718 /* Requires that the keyid be a fingerprint in 16 byte form. */
719 static gpg_error_t
720 parse_save_opt_keyid_common (struct client_s *client, const char **list,
721 const char *value, char ***dst)
723 gpg_error_t rc = 0;
724 char **keys = NULL;
726 if (value && *value)
728 keys = str_split (value, ",", 0);
729 if (!keys)
730 return GPG_ERR_ENOMEM;
733 rc = crypto_keyid_to_16b (keys);
734 if (!rc)
735 rc = permitted_to_save (client, list, (const char **)keys);
737 if (!rc)
738 *dst = keys;
739 else
740 strv_free (keys);
742 return rc;
745 static gpg_error_t
746 parse_save_opt_keyid (void *data, void *value)
748 struct client_s *client = data;
749 const char *str = value;
750 char **dst = NULL;
751 gpg_error_t rc;
753 rc = parse_save_opt_keyid_common (client,
754 (const char **)client->crypto->pubkey,
755 str, &dst);
756 if (rc)
757 return rc;
759 client->crypto->save.pubkey = dst;
760 return 0;
763 static gpg_error_t
764 parse_save_opt_sign_keyid (void *data, void *value)
766 struct client_s *client = data;
767 const char *str = value;
768 char **dst = NULL;
769 gpg_error_t rc;
771 rc = parse_save_opt_keyid_common (client,
772 (const char **)client->crypto->sigkey,
773 str, &dst);
774 if (rc)
775 return rc;
777 if (!dst)
778 client->opts |= OPT_NO_SIGNER;
780 client->crypto->save.sigkey = dst;
781 return 0;
784 static gpg_error_t
785 parse_save_opt_inquire (struct client_s *client, uint32_t opt)
787 if (opt == OPT_INQUIRE && !(client->flags & FLAG_NEW))
789 gpg_error_t rc = peer_is_invoker (client);
791 if (rc)
792 return rc;
795 client->opts |= opt;
796 return 0;
799 static gpg_error_t
800 parse_save_opt_inquire_keyparam (void *data, void *value)
802 (void)value;
803 return parse_save_opt_inquire (data, OPT_INQUIRE);
806 static gpg_error_t
807 parse_save_opt_inquire_keyid (void *data, void *value)
809 (void)value;
810 return parse_save_opt_inquire (data, OPT_INQUIRE_KEYID);
813 static gpg_error_t
814 parse_save_opt_inquire_sign_keyid (void *data, void *value)
816 (void)value;
817 return parse_save_opt_inquire (data, OPT_INQUIRE_SIGN_KEYID);
820 static gpg_error_t
821 parse_save_opt_symmetric (void *data, void *value)
823 struct client_s *client = data;
825 (void)value;
826 client->opts |= OPT_SYMMETRIC;
827 return 0;
830 /* Tests that the keys in new_keys are also in old_keys. */
831 static gpg_error_t
832 compare_keys (char **new_keys, char **old_keys)
834 char **o;
836 if (!old_keys || !*old_keys)
837 return 0;
839 crypto_keyid_to_16b (new_keys);
841 for (o = old_keys; *o; o++)
843 char **n;
845 for (n = new_keys; *n; n++)
847 if (!strcmp (*n, *o))
848 break;
851 if (!*n)
852 return GPG_ERR_NOT_FOUND;
855 return 0;
858 static gpg_error_t
859 inquire_keyid (struct client_s *client, uint32_t opt)
861 gpg_error_t rc;
862 unsigned char *result = NULL;
863 size_t len;
864 const char *s;
865 char ***orig;
866 char ***save;
868 if (opt == OPT_INQUIRE_KEYID)
870 s = "INQUIRE_KEYID";
871 orig = &client->crypto->pubkey;
872 save = &client->crypto->save.pubkey;
874 else
876 s = "INQUIRE_SIGN_KEYID";
877 orig = &client->crypto->sigkey;
878 save = &client->crypto->save.sigkey;
881 rc = assuan_inquire (client->ctx, s, &result, &len, 0);
882 if (!rc)
884 char **dst = NULL;
886 rc = parse_save_opt_keyid_common (client, (const char **)*orig,
887 (char *)result, &dst);
888 if (!rc)
890 if (!dst && opt == OPT_INQUIRE_SIGN_KEYID
891 && client->opts & OPT_SYMMETRIC)
892 client->opts |= OPT_NO_SIGNER;
894 *save = dst;
898 xfree (result);
899 return rc;
902 /* The caching test of gpg-agent is both a blessing and a curse. When a key
903 * lookup in its cache fails it tries the last successful key to be unlocked.
904 * This prevents pwmd from successfully clearing a signing key since the
905 * previous key, which more than likely belongs to the same keypair as the
906 * encryption/decryption key, will have the passphrase cached and therefore the
907 * signing key also cached. So we need to clear both the signing and encryption
908 * keys to get the effect of requiring a passphrase when generating a new
909 * keypair for an existing data file, or when the "require_save_key"
910 * configuration parameter is set. The "require_save_key" parameter is mostly a
911 * failsafe of the gpg-agent option --ignore-cache-for-signing since
912 * some/most/all users may fail to set it.
914 static gpg_error_t
915 save_command (assuan_context_t ctx, char *line)
917 struct client_s *client = assuan_get_pointer (ctx);
918 struct cache_data_s *cdata = NULL;
919 gpg_error_t rc = 0, cached = 0;
920 int defer = 0;
921 struct stat st;
922 struct argv_s *args[] = {
923 &(struct argv_s) {"keyid", OPTION_TYPE_ARG, parse_save_opt_keyid},
924 &(struct argv_s) {"sign-keyid", OPTION_TYPE_OPTARG,
925 parse_save_opt_sign_keyid},
926 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
927 parse_save_opt_inquire_keyparam },
928 &(struct argv_s) {"inquire-keyid", OPTION_TYPE_NOARG,
929 parse_save_opt_inquire_keyid },
930 &(struct argv_s) {"inquire-sign-keyid", OPTION_TYPE_NOARG,
931 parse_save_opt_inquire_sign_keyid },
932 &(struct argv_s) {"symmetric", OPTION_TYPE_NOARG,
933 parse_save_opt_symmetric },
934 NULL
937 crypto_free_save (&client->crypto->save);
939 if (!(client->flags & FLAG_NEW))
941 rc = crypto_is_symmetric (client->filename);
942 if (!rc || rc == GPG_ERR_BAD_DATA)
944 if (!rc)
945 client->opts |= OPT_SYMMETRIC;
947 rc = 0; // PKI
951 if (rc)
952 return send_error (ctx, rc);
954 rc = parse_options (&line, args, client, 0);
955 if (rc)
956 return send_error (ctx, rc);
959 if (config_get_boolean (client->filename, "require_save_key")
960 || (client->opts & OPT_SYMMETRIC))
961 client->opts |= OPT_ASK;
963 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
964 return send_error (ctx, gpg_error_from_errno (errno));
966 if (errno != ENOENT && !S_ISREG (st.st_mode))
968 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENODEV));
969 return send_error (ctx, GPG_ERR_ENODEV);
972 if (!rc)
973 cached = rc = cache_iscached (client->filename, &defer, 0, 1);
975 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
976 rc = 0;
977 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
978 rc = 0;
980 if (rc)
981 return send_error (ctx, rc);
983 /* Specifying both a recipient and symmetric encryption is an error. */
984 if (client->opts & OPT_INQUIRE_KEYID && client->opts & OPT_SYMMETRIC)
986 return send_error (ctx, GPG_ERR_CONFLICT);
988 else if (client->opts & OPT_INQUIRE && client->opts & OPT_SYMMETRIC)
990 return send_error (ctx, GPG_ERR_CONFLICT);
992 /* Existing file with a recipient and wanting symmetric is an error. */
993 else if (client->opts & OPT_SYMMETRIC && client->crypto->save.pubkey)
995 return send_error (ctx, GPG_ERR_CONFLICT);
997 else if (((client->opts & OPT_INQUIRE_KEYID)
998 || (client->opts & OPT_INQUIRE_SIGN_KEYID)))
1000 if (client->opts & OPT_INQUIRE)
1001 return send_error (ctx, GPG_ERR_CONFLICT);
1003 if (client->opts & OPT_INQUIRE_KEYID)
1004 rc = inquire_keyid (client, OPT_INQUIRE_KEYID);
1006 if (!rc && (client->opts & OPT_INQUIRE_SIGN_KEYID))
1007 rc = inquire_keyid (client, OPT_INQUIRE_SIGN_KEYID);
1009 else if ((client->crypto->save.pubkey || client->crypto->save.sigkey)
1010 && client->opts & OPT_INQUIRE)
1011 return send_error (ctx, GPG_ERR_CONFLICT);
1013 if (!rc)
1015 client->crypto->keyfile = config_get_string (client->filename,
1016 "passphrase_file");
1017 rc = crypto_init_ctx (client->crypto, (client->flags & FLAG_NO_PINENTRY),
1018 client->crypto->keyfile);
1021 if (!rc && (client->opts & OPT_INQUIRE))
1023 /* Require a passphrase when generating a new key pair for an existing
1024 * file.
1026 if (!(client->flags & FLAG_NEW))
1028 rc = cache_clear_agent_keys (client->filename, 1, 1);
1029 if (!rc)
1031 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
1032 client->opts &= ~OPT_ASK;
1036 if (!rc)
1038 unsigned char *params = NULL;
1039 size_t len;
1041 rc = assuan_inquire (client->ctx, "KEYPARAM", &params, &len, 0);
1042 if (!rc)
1044 client->crypto->flags |= CRYPTO_FLAG_NEWFILE;
1045 pthread_cleanup_push ((void *)xfree, params);
1046 rc = crypto_genkey (client, client->crypto, params);
1047 pthread_cleanup_pop (1);
1051 else if (!rc && (client->flags & FLAG_NEW))
1053 if (!client->crypto->save.pubkey && !client->crypto->save.sigkey
1054 && !(client->opts & OPT_SYMMETRIC))
1056 char *params = crypto_default_key_params ();
1058 if (!params)
1059 rc = GPG_ERR_ENOMEM;
1061 if (!rc)
1063 client->crypto->flags |= CRYPTO_FLAG_NEWFILE;
1064 pthread_cleanup_push ((void *)xfree, params);
1065 rc = crypto_genkey (client, client->crypto,
1066 (unsigned char *)params);
1067 pthread_cleanup_pop (1);
1070 else
1072 if (!client->crypto->save.pubkey && client->crypto->save.sigkey
1073 && !(client->opts & OPT_SYMMETRIC))
1074 client->crypto->save.pubkey = strv_dup (client->crypto->save.sigkey);
1076 if (client->crypto->save.pubkey && !client->crypto->save.sigkey)
1077 client->crypto->save.sigkey = strv_dup (client->crypto->save.pubkey);
1080 else if (!rc)
1082 cdata = cache_get_data (client->filename);
1083 if (cdata)
1085 if (client->crypto->save.pubkey)
1086 rc = compare_keys (client->crypto->save.pubkey, cdata->pubkey);
1088 /* Always allow a signer for symmetric data files. */
1089 if (!rc && client->crypto->save.sigkey
1090 && !(client->opts & OPT_SYMMETRIC))
1091 rc = compare_keys (client->crypto->save.sigkey, cdata->sigkey);
1093 /* Prevent saving to a recipient who is not in the original recipient
1094 * list without a passphrase. */
1095 if (rc || (client->crypto->sigkey && client->opts & OPT_NO_SIGNER))
1096 client->opts |= OPT_ASK;
1098 if (rc == GPG_ERR_NOT_FOUND)
1100 rc = peer_is_invoker (client);
1101 if (rc == GPG_ERR_EACCES)
1102 rc = GPG_ERR_FORBIDDEN;
1105 if (!client->crypto->save.pubkey
1106 && !(client->opts & OPT_SYMMETRIC))
1107 client->crypto->save.pubkey = strv_dup (cdata->pubkey);
1109 if (!rc && !client->crypto->save.sigkey && cdata->sigkey
1110 && !(client->opts & OPT_NO_SIGNER))
1111 client->crypto->save.sigkey = strv_dup (cdata->sigkey);
1112 else if (!rc && !client->crypto->save.sigkey
1113 && (client->opts & OPT_NO_SIGNER)
1114 && !(client->opts & OPT_SYMMETRIC))
1115 rc = GPG_ERR_NO_SIGNATURE_SCHEME;
1117 else
1119 client->crypto->save.pubkey = strv_dup (client->crypto->pubkey);
1120 client->crypto->save.sigkey = strv_dup (client->crypto->sigkey);
1124 if (!rc && !(client->flags & FLAG_NEW))
1126 /* Fixes {NEW_,SIGN_}PASSPHRASE inquire keywords. See passphrase_cb(). */
1127 if (client->opts & OPT_SYMMETRIC)
1128 client->crypto->flags |= CRYPTO_FLAG_PASSWD;
1130 if (client->opts & OPT_ASK)
1132 rc = cache_clear_agent_keys (client->filename, 1, 1);
1133 if (!rc)
1134 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
1138 if (!rc && client->opts & OPT_SYMMETRIC)
1139 client->crypto->flags |= CRYPTO_FLAG_PASSWD_NEW;
1141 if (!rc)
1142 rc = xml_update_element_mtime (client, xmlDocGetRootElement (client->doc));
1144 if (!rc)
1146 int size;
1148 xmlDocDumpFormatMemory (client->doc, &client->crypto->plaintext, &size,
1150 if (size > 0)
1151 client->crypto->plaintext_size = (size_t) size;
1152 else
1153 rc = GPG_ERR_ENOMEM;
1155 if (!rc)
1157 client->crypto->flags |= (client->opts & OPT_SYMMETRIC) ? CRYPTO_FLAG_SYMMETRIC : 0;
1158 client->crypto->flags |= (client->flags & FLAG_NEW) ? CRYPTO_FLAG_NEWFILE : 0;
1159 client->crypto->flags |= !(client->opts & OPT_SYMMETRIC) ? CRYPTO_FLAG_PASSWD_SIGN : 0;
1160 rc = crypto_encrypt (client, client->crypto);
1161 client->crypto->flags &= ~CRYPTO_FLAG_SYMMETRIC;
1164 if (!rc)
1166 rc = crypto_write_file (client->crypto);
1167 if (!rc)
1169 if (!cached) // no error
1170 cdata = cache_get_data (client->filename);
1173 if (!rc)
1175 rc = cache_encrypt (client->crypto);
1176 if (rc)
1178 cache_clear (NULL, client->filename, 1);
1179 strv_free (client->crypto->pubkey);
1180 client->crypto->pubkey = strv_dup (client->crypto->save.pubkey);
1181 strv_free (client->crypto->sigkey);
1182 client->crypto->sigkey = strv_dup (client->crypto->save.sigkey);
1183 client->flags &= ~(FLAG_NEW);
1187 if (!rc)
1189 int timeout = config_get_integer (client->filename,
1190 "cache_timeout");
1192 cache_free_data_once (cdata);
1193 cdata = xcalloc (1, sizeof (struct cache_data_s));
1194 cdata->doc = client->crypto->plaintext;
1195 client->crypto->plaintext = NULL;
1196 cdata->size = client->crypto->plaintext_size;
1197 client->crypto->plaintext_size = 0;
1198 cdata->pubkey = client->crypto->save.pubkey;
1199 client->crypto->save.pubkey = NULL;
1200 cdata->sigkey = client->crypto->save.sigkey;
1201 client->crypto->save.sigkey = NULL;
1203 /* Update in case the cache entry expires the next SAVE may not
1204 * have any known keys. */
1205 strv_free (client->crypto->pubkey);
1206 client->crypto->pubkey = strv_dup (cdata->pubkey);
1207 strv_free (client->crypto->sigkey);
1208 client->crypto->sigkey = strv_dup (cdata->sigkey);
1210 if (!cached) // no error and wont increment refcount
1211 rc = cache_set_data (client->filename, cdata);
1212 else
1213 rc = cache_add_file (client->filename, cdata, timeout);
1215 if (!rc && (cached || (client->flags & FLAG_NEW)))
1216 send_status_all (STATUS_CACHE, NULL);
1218 if (!rc)
1220 rc = update_checksum (client);
1221 client->flags &= ~(FLAG_NEW);
1227 crypto_free_non_keys (client->crypto);
1228 return send_error (ctx, rc);
1231 static gpg_error_t
1232 do_delete (assuan_context_t ctx, char *line)
1234 struct client_s *client = assuan_get_pointer (ctx);
1235 struct xml_request_s *req;
1236 xmlNodePtr n;
1237 gpg_error_t rc;
1239 rc = xml_new_request (client, line, XML_CMD_DELETE, &req);
1240 if (rc)
1241 return rc;
1243 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1244 xml_free_request (req);
1245 if (rc)
1246 return rc;
1248 rc = xml_is_element_owner (client, n);
1249 if (!rc)
1250 rc = xml_unlink_node (client, n);
1252 return rc;
1255 static gpg_error_t
1256 delete_command (assuan_context_t ctx, char *line)
1258 struct client_s *client = assuan_get_pointer (ctx);
1259 gpg_error_t rc;
1260 struct argv_s *args[] = {
1261 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1262 NULL
1265 rc = parse_options (&line, args, client, 1);
1266 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1267 rc = GPG_ERR_SYNTAX;
1268 if (rc)
1269 return send_error (ctx, rc);
1271 if (client->opts & OPT_INQUIRE)
1273 unsigned char *result;
1274 size_t len;
1276 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1277 if (rc)
1278 return send_error (ctx, rc);
1280 pthread_cleanup_push ((void *)xfree, result);
1281 rc = do_delete (ctx, (char *)result);
1282 pthread_cleanup_pop (1);
1284 else
1285 rc = do_delete (ctx, line);
1287 return send_error (ctx, rc);
1290 static gpg_error_t
1291 update_element_expirey (struct client_s *client, xmlNodePtr n)
1293 gpg_error_t rc = 0;
1294 xmlChar *expire, *incr;
1295 char *p;
1296 time_t e, e_orig, i;
1297 time_t now = time (NULL);
1299 expire = xml_attribute_value (n, (xmlChar *)"expire");
1300 if (!expire)
1301 return 0;
1303 errno = 0;
1304 e = strtoul ((char *)expire, &p, 10);
1305 if (errno || *p || e == ULONG_MAX || *expire == '-')
1307 xmlFree (expire);
1308 rc = GPG_ERR_ERANGE;
1309 goto fail;
1312 xmlFree (expire);
1313 incr = xml_attribute_value (n, (xmlChar *)"expire_increment");
1314 if (!incr)
1315 return 0;
1317 i = strtoul ((char *)incr, &p, 10);
1318 if (errno || *p || i == ULONG_MAX || *incr == '-')
1320 xmlFree (incr);
1321 rc = GPG_ERR_ERANGE;
1322 goto fail;
1325 xmlFree (incr);
1326 e_orig = e;
1327 e = now + i;
1328 p = str_asprintf ("%lu", e);
1329 if (!p)
1331 rc = GPG_ERR_ENOMEM;
1332 goto fail;
1335 rc = xml_add_attribute (client, n, "expire", p);
1336 xfree (p);
1337 if (!rc)
1338 rc = send_status (client->ctx, STATUS_EXPIRE, "%lu %lu", e_orig, e);
1340 fail:
1341 return rc;
1344 static gpg_error_t
1345 store_command (assuan_context_t ctx, char *line)
1347 struct client_s *client = assuan_get_pointer (ctx);
1348 gpg_error_t rc;
1349 size_t len;
1350 unsigned char *result;
1351 xmlNodePtr n, parent;
1352 int has_content;
1353 char *content = NULL;
1354 struct xml_request_s *req;
1356 if (line && *line)
1357 return send_error (ctx, GPG_ERR_SYNTAX);
1359 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1360 if (rc)
1361 return send_error (ctx, rc);
1363 rc = xml_new_request (client, (char *)result, XML_CMD_STORE, &req);
1364 xfree (result);
1365 if (rc)
1366 return send_error (ctx, rc);
1368 /* Prevent passing the element content around to save some memory. */
1369 len = strv_length (req->args);
1370 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1371 if (*(req->args+1) && !xml_valid_element_path (req->args, has_content))
1373 xml_free_request (req);
1374 return send_error (ctx, GPG_ERR_INV_VALUE);
1377 if (has_content || !*req->args[len-1])
1379 has_content = 1;
1380 content = req->args[len-1];
1381 req->args[len-1] = NULL;
1384 if (strv_length (req->args) > 1)
1386 rc = xml_check_recursion (client, req);
1387 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
1388 goto fail;
1391 parent = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1393 if (!rc && len > 1)
1395 rc = xml_is_element_owner (client, parent);
1396 if (!rc)
1398 n = xml_find_text_node (parent->children);
1399 if (n)
1400 xmlNodeSetContent (n, (xmlChar *) content);
1401 else
1402 xmlNodeAddContent (parent, (xmlChar *) content);
1404 (void)xml_update_element_mtime (client, parent);
1405 (void)update_element_expirey (client, parent);
1409 fail:
1410 xfree (content);
1411 xml_free_request (req);
1412 return send_error (ctx, rc);
1415 static gpg_error_t
1416 xfer_data (assuan_context_t ctx, const char *line, int total)
1418 struct client_s *client = assuan_get_pointer (ctx);
1419 int to_send;
1420 int sent = 0;
1421 gpg_error_t rc = 0;
1422 int progress = config_get_integer ("global", "xfer_progress");
1423 int flush = 0;
1425 if (!(client->flags & FLAG_LOCK_CMD))
1426 rc = unlock_file_mutex (client, 0);
1427 if (rc && rc != GPG_ERR_NOT_LOCKED)
1428 return rc;
1430 progress =
1431 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1432 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1433 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1435 if (rc)
1436 return rc;
1438 again:
1441 if (sent + to_send > total)
1442 to_send = total - sent;
1444 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1445 flush ? 0 : to_send);
1446 if (!rc)
1448 sent += flush ? 0 : to_send;
1450 if ((progress && !(sent % progress) && sent != total) ||
1451 (sent == total && flush))
1452 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1454 if (!flush && !rc && sent == total)
1456 flush = 1;
1457 goto again;
1461 while (!rc && sent < total);
1463 return rc;
1466 static gpg_error_t
1467 do_get (assuan_context_t ctx, char *line)
1469 struct client_s *client = assuan_get_pointer (ctx);
1470 gpg_error_t rc;
1471 struct xml_request_s *req;
1472 xmlNodePtr n;
1474 rc = xml_new_request (client, line, XML_CMD_NONE, &req);
1475 if (rc)
1476 return rc;
1478 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1479 if (!rc)
1481 if (n && n->children)
1483 xmlNodePtr tmp = n;
1485 n = xml_find_text_node (n->children);
1486 if (!n || !n->content || !*n->content)
1487 rc = GPG_ERR_NO_DATA;
1488 else
1490 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1491 if (!rc)
1493 xmlChar *expire = xml_attribute_value (tmp,
1494 (xmlChar *)"expire");
1495 if (expire)
1497 time_t now = time (NULL);
1498 time_t e;
1499 char *p;
1501 errno = 0;
1502 e = strtoul ((char *)expire, &p, 10);
1503 if (errno || *p || e == ULONG_MAX || *expire == '-')
1504 log_write (_("invalid expire attribute value: %s"),
1505 expire);
1506 else if (now >= e)
1507 rc = send_status (ctx, STATUS_EXPIRE, "%lu 0", e);
1509 xmlFree (expire);
1514 else
1515 rc = GPG_ERR_NO_DATA;
1518 xml_free_request (req);
1519 return rc;
1522 static gpg_error_t
1523 get_command (assuan_context_t ctx, char *line)
1525 struct client_s *client = assuan_get_pointer (ctx);
1526 gpg_error_t rc;
1527 struct argv_s *args[] = {
1528 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1529 NULL
1532 rc = parse_options (&line, args, client, 1);
1533 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1534 rc = GPG_ERR_SYNTAX;
1535 if (rc)
1536 return send_error (ctx, rc);
1538 if (client->opts & OPT_INQUIRE)
1540 unsigned char *result;
1541 size_t len;
1543 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1544 if (rc)
1545 return send_error (ctx, rc);
1547 pthread_cleanup_push ((void *)xfree, result);
1548 rc = do_get (ctx, (char *)result);
1549 pthread_cleanup_pop (1);
1551 else
1552 rc = do_get (ctx, line);
1554 return send_error (ctx, rc);
1557 static void list_command_free1 (void *arg);
1558 static gpg_error_t
1559 realpath_command (assuan_context_t ctx, char *line)
1561 gpg_error_t rc;
1562 char **realpath = NULL;
1563 struct client_s *client = assuan_get_pointer (ctx);
1564 struct argv_s *args[] = {
1565 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1566 NULL
1569 rc = parse_options (&line, args, client, 1);
1570 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1571 rc = GPG_ERR_SYNTAX;
1572 if (rc)
1573 return send_error (ctx, rc);
1575 if (client->opts & OPT_INQUIRE)
1577 unsigned char *result;
1578 size_t len;
1580 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1581 if (rc)
1582 return send_error (ctx, rc);
1584 pthread_cleanup_push ((void *)xfree, result);
1585 (void)xml_resolve_path (client, client->doc, result, &realpath, &rc);
1586 pthread_cleanup_pop (1);
1588 else
1589 (void)xml_resolve_path (client, client->doc, (unsigned char *)line,
1590 &realpath, &rc);
1592 if (!rc)
1594 char *tmp = strv_join ((char *)"\t", realpath);
1596 strv_free (realpath);
1597 if (!tmp)
1598 return send_error (ctx, GPG_ERR_ENOMEM);
1600 pthread_cleanup_push ((void *)xfree, tmp);
1601 rc = xfer_data (ctx, tmp, strlen (tmp));
1602 pthread_cleanup_pop (1);
1605 return send_error (ctx, rc);
1608 static void
1609 list_command_free1 (void *arg)
1611 if (arg)
1612 string_free ((struct string_s *) arg, 1);
1615 static gpg_error_t
1616 parse_list_opt_recurse (void *data, void *value)
1618 struct client_s *client = data;
1620 (void)value;
1621 client->opts |= OPT_LIST_RECURSE;
1622 return 0;
1625 static gpg_error_t
1626 parse_opt_verbose (void *data, void *value)
1628 struct client_s *client = data;
1630 (void)value;
1631 client->opts |= OPT_VERBOSE;
1632 return 0;
1635 static gpg_error_t
1636 list_path_once (struct client_s *client, char *line,
1637 struct element_list_s *elements, struct string_s *result)
1639 gpg_error_t rc;
1641 rc = xml_create_path_list (client, client->doc, NULL, elements, line, 0);
1642 if (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES)
1643 rc = 0;
1645 if (!rc)
1647 int total = slist_length (elements->list);
1649 if (total)
1651 int i;
1653 for (i = 0; i < total; i++)
1655 char *tmp = slist_nth_data (elements->list, i);
1657 string_append_printf (result, "%s%s", tmp,
1658 i + 1 == total ? "" : "\n");
1661 else
1662 rc = GPG_ERR_NO_DATA;
1665 return rc;
1668 static gpg_error_t
1669 do_list (assuan_context_t ctx, char *line)
1671 struct client_s *client = assuan_get_pointer (ctx);
1672 gpg_error_t rc = GPG_ERR_NO_DATA;
1673 struct element_list_s *elements = NULL;
1675 if (!line || !*line)
1677 struct string_s *str = string_new (NULL);
1678 xmlNodePtr n;
1680 if (!str)
1681 return GPG_ERR_ENOMEM;
1683 pthread_cleanup_push ((void *)list_command_free1, str);
1684 n = xmlDocGetRootElement (client->doc);
1685 for (n = n->children; n; n = n->next)
1687 xmlChar *name;
1689 if (n->type != XML_ELEMENT_NODE)
1690 continue;
1692 name = xmlGetProp (n, (xmlChar *)"_name");
1693 if (!name)
1695 rc = GPG_ERR_ENOMEM;
1696 break;
1699 elements = xcalloc (1, sizeof (struct element_list_s));
1700 if (!elements)
1702 xmlFree (name);
1703 rc = GPG_ERR_ENOMEM;
1704 break;
1707 elements->prefix = string_new (NULL);
1708 if (!elements->prefix)
1710 xmlFree (name);
1711 xml_free_element_list (elements);
1712 rc = GPG_ERR_ENOMEM;
1713 break;
1716 elements->recurse = client->opts & OPT_LIST_RECURSE;
1717 elements->root_only = !elements->recurse;
1718 pthread_cleanup_push ((void *)xml_free_element_list, elements);
1719 rc = list_path_once (client, (char *)name, elements, str);
1720 xmlFree (name);
1721 pthread_cleanup_pop (1);
1722 if (rc)
1723 break;
1725 if (n->next)
1726 string_append (str, "\n");
1729 if (!rc)
1730 rc = xfer_data (ctx, str->str, str->len);
1732 pthread_cleanup_pop (1);
1733 return rc;
1736 elements = xcalloc (1, sizeof (struct element_list_s));
1737 if (!elements)
1738 return GPG_ERR_ENOMEM;
1740 elements->prefix = string_new (NULL);
1741 if (!elements->prefix)
1743 xml_free_element_list (elements);
1744 return GPG_ERR_ENOMEM;
1747 elements->recurse = client->opts & OPT_LIST_RECURSE;
1748 pthread_cleanup_push ((void *)xml_free_element_list, elements);
1749 struct string_s *str = string_new (NULL);
1750 pthread_cleanup_push ((void *)list_command_free1, str);
1751 rc = list_path_once (client, line, elements, str);
1752 if (!rc)
1753 rc = xfer_data (ctx, str->str, str->len);
1755 pthread_cleanup_pop (1);
1756 pthread_cleanup_pop (1);
1757 return rc;
1760 static gpg_error_t
1761 list_command (assuan_context_t ctx, char *line)
1763 struct client_s *client = assuan_get_pointer (ctx);
1764 gpg_error_t rc;
1765 struct argv_s *args[] = {
1766 &(struct argv_s) {"recurse", OPTION_TYPE_NOARG, parse_list_opt_recurse},
1767 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1768 /* These are deprecated but kept for backward compatibility with older
1769 * clients. */
1770 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, NULL},
1771 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG, NULL},
1772 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_recurse},
1773 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG, NULL},
1774 NULL
1777 if (disable_list_and_dump == 1)
1778 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1780 rc = parse_options (&line, args, client, 1);
1781 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1782 rc = GPG_ERR_SYNTAX;
1783 if (rc)
1784 return send_error (ctx, rc);
1786 if (client->opts & OPT_INQUIRE)
1788 unsigned char *result;
1789 size_t len;
1791 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1792 if (rc)
1793 return send_error (ctx, rc);
1795 pthread_cleanup_push ((void *)xfree, result);
1796 rc = do_list (ctx, (char *)result);
1797 pthread_cleanup_pop (1);
1799 else
1800 rc = do_list (ctx, line);
1802 return send_error (ctx, rc);
1805 #define RESUMABLE_ERROR(rc) (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES \
1806 || rc == GPG_ERR_ELEMENT_NOT_FOUND || !rc)
1808 * args[0] - element path
1810 static gpg_error_t
1811 attribute_list (assuan_context_t ctx, char **args)
1813 struct client_s *client = assuan_get_pointer (ctx);
1814 char **attrlist = NULL;
1815 int i = 0;
1816 int target = 0;
1817 xmlAttrPtr a;
1818 xmlNodePtr n, an, r;
1819 char *line;
1820 gpg_error_t rc;
1821 struct xml_request_s *req;
1823 if (!args || !args[0] || !*args[0])
1824 return GPG_ERR_SYNTAX;
1826 rc = xml_new_request (client, args[0], XML_CMD_ATTR_LIST, &req);
1827 if (rc)
1828 return rc;
1830 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1831 xml_free_request (req);
1832 if (rc)
1833 return rc;
1835 again:
1836 for (a = n->properties; a; a = a->next)
1838 char **pa;
1840 if (target && xml_reserved_attribute ((char *)a->name))
1841 continue;
1843 pa = xrealloc (attrlist, (i + 2) * sizeof (char *));
1844 if (!pa)
1846 if (attrlist)
1847 strv_free (attrlist);
1849 log_write ("%s(%i): %s", __FILE__, __LINE__,
1850 pwmd_strerror (GPG_ERR_ENOMEM));
1851 return GPG_ERR_ENOMEM;
1854 attrlist = pa;
1855 an = a->children;
1856 attrlist[i] = str_asprintf ("%s %s", (char *) a->name, an && an->content
1857 ? (char *)an->content : "");
1859 if (!attrlist[i])
1861 strv_free (attrlist);
1862 log_write ("%s(%i): %s", __FILE__, __LINE__,
1863 pwmd_strerror (GPG_ERR_ENOMEM));
1864 return GPG_ERR_ENOMEM;
1867 attrlist[++i] = NULL;
1870 if (!attrlist)
1871 return GPG_ERR_NO_DATA;
1873 if (!target)
1875 r = xml_resolve_path (client, client->doc, (xmlChar *)args[0], NULL, &rc);
1876 if (!RESUMABLE_ERROR (rc))
1878 strv_free (attrlist);
1879 return rc;
1881 else if (!rc && r != n)
1883 target = 1;
1884 n = r;
1885 goto again;
1888 rc = 0;
1891 line = strv_join ("\n", attrlist);
1892 if (!line)
1894 log_write ("%s(%i): %s", __FILE__, __LINE__,
1895 pwmd_strerror (GPG_ERR_ENOMEM));
1896 strv_free (attrlist);
1897 return GPG_ERR_ENOMEM;
1900 pthread_cleanup_push ((void *)xfree, line);
1901 pthread_cleanup_push ((void *)req_free, attrlist);
1902 rc = xfer_data (ctx, line, strlen (line));
1903 pthread_cleanup_pop (1);
1904 pthread_cleanup_pop (1);
1905 return rc;
1909 * args[0] - attribute
1910 * args[1] - element path
1912 static gpg_error_t
1913 attribute_delete (struct client_s *client, char **args)
1915 gpg_error_t rc;
1916 xmlNodePtr n;
1918 if (!args || !args[0] || !*args[0] || !args[1] || !*args[1])
1919 return GPG_ERR_SYNTAX;
1921 if (!strcmp (args[0], "_name") || !strcmp (args[0], "target"))
1922 return GPG_ERR_INV_ATTR;
1924 if (!xml_reserved_attribute (args[0]))
1925 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
1926 else
1928 struct xml_request_s *req;
1930 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
1931 if (rc)
1932 return rc;
1934 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1935 xml_free_request (req);
1938 if (!rc)
1939 rc = xml_is_element_owner (client, n);
1941 if (!rc)
1942 rc = xml_delete_attribute (client, n, (xmlChar *) args[0]);
1944 return rc;
1948 * Creates the "target" attribute. When other commands encounter an element
1949 * with this attribute they follow the "target" after appending remaining
1950 * elements from the original element path to the target. The exception is the
1951 * ATTR command that operates on the element itself (for reserved attribute
1952 * names) and not the target.
1954 * If the source element path doesn't exist when using 'ATTR SET target', it is
1955 * created, but the destination element path must exist. This is simliar to the
1956 * ls(1) command: ln -s src dst.
1958 * args[0] - source element path
1959 * args[1] - destination element path
1961 static gpg_error_t
1962 set_target_attribute (struct client_s *client, char **args)
1964 struct xml_request_s *req = NULL;
1965 char **src, **dst;
1966 gpg_error_t rc;
1967 xmlNodePtr n;
1969 if (!args || !args[0] || !args[1])
1970 return GPG_ERR_SYNTAX;
1972 src = str_split (args[0], "\t", 0);
1973 if (!src)
1974 return GPG_ERR_SYNTAX;
1976 if (!xml_valid_element_path (src, 0))
1978 strv_free (src);
1979 return GPG_ERR_INV_VALUE;
1982 dst = str_split (args[1], "\t", 0);
1983 if (!dst)
1985 strv_free (src);
1986 return GPG_ERR_SYNTAX;
1989 // Be sure the destination element path exists. */
1990 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
1991 if (rc)
1992 goto fail;
1994 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1995 if (rc && rc != GPG_ERR_EACCES)
1996 goto fail;
1998 xml_free_request (req);
1999 req = NULL;
2001 if (!strcmp (args[0], args[1]))
2003 rc = GPG_ERR_EEXIST;
2004 goto fail;
2007 rc = xml_new_request (client, args[0], XML_CMD_ATTR_TARGET, &req);
2008 if (rc)
2009 goto fail;
2011 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2012 if (rc && rc != GPG_ERR_EACCES)
2013 goto fail;
2015 if (!rc)
2017 rc = xml_add_attribute (client, n, "target", args[1]);
2018 if (!rc)
2019 rc = xml_unlink_node (client, n->children);
2022 fail:
2023 strv_free (src);
2024 strv_free (dst);
2025 xml_free_request (req);
2026 return rc;
2030 * args[0] - attribute
2031 * args[1] - element path
2033 static gpg_error_t
2034 attribute_get (assuan_context_t ctx, char **args)
2036 struct client_s *client = assuan_get_pointer (ctx);
2037 xmlNodePtr n;
2038 xmlChar *a;
2039 gpg_error_t rc;
2040 struct xml_request_s *req;
2042 if (!args || !args[0] || !*args[0] || !args[1] || !*args[1])
2043 return GPG_ERR_SYNTAX;
2045 if (!xml_reserved_attribute (args[0]))
2046 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
2047 else
2049 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
2050 if (rc)
2051 return rc;
2053 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2054 xml_free_request (req);
2057 if (rc)
2058 return rc;
2060 a = xmlGetProp (n, (xmlChar *) args[0]);
2061 if (!a)
2062 return GPG_ERR_NOT_FOUND;
2064 pthread_cleanup_push ((void *)xmlFree, a);
2066 if (*a)
2067 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
2068 else
2069 rc = GPG_ERR_NO_DATA;
2071 pthread_cleanup_pop (1);
2072 return rc;
2076 * args[0] - attribute
2077 * args[1] - element path
2078 * args[2] - value
2080 static gpg_error_t
2081 attribute_set (struct client_s *client, char **args)
2083 struct xml_request_s *req;
2084 gpg_error_t rc;
2085 xmlNodePtr n;
2087 if (!args || !args[0] || !args[1])
2088 return GPG_ERR_SYNTAX;
2091 * Reserved attribute names.
2093 if (!strcmp (args[0], "_name")) // Use the RENAME command instead
2094 return GPG_ERR_INV_ATTR;
2095 else if (!strcmp (args[0], "target"))
2096 return set_target_attribute (client, args + 1);
2097 else if (!xml_valid_attribute (args[0]))
2098 return GPG_ERR_INV_VALUE;
2100 if (!xml_valid_attribute_value (args[2]))
2101 return GPG_ERR_INV_VALUE;
2103 if (!xml_reserved_attribute (args[0]))
2105 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
2106 if (!rc)
2108 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
2109 if (!rc)
2111 rc = xml_check_recursion (client, req);
2112 xml_free_request (req);
2116 else
2118 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
2119 if (rc)
2120 return rc;
2122 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2123 xml_free_request (req);
2126 if (!rc)
2127 rc = xml_is_element_owner (client, n);
2129 if (!rc)
2130 rc = xml_add_attribute (client, n, args[0], args[2]);
2132 return rc;
2136 * req[0] - command
2137 * req[1] - attribute name or element path if command is LIST
2138 * req[2] - element path
2139 * req[2] - element path or value
2142 static gpg_error_t
2143 do_attr (assuan_context_t ctx, char *line)
2145 struct client_s *client = assuan_get_pointer (ctx);
2146 gpg_error_t rc = 0;
2147 char **req;
2149 req = str_split (line, " ", 4);
2150 if (!req || !req[0] || !req[1])
2152 strv_free (req);
2153 return GPG_ERR_SYNTAX;
2156 pthread_cleanup_push ((void *)req_free, req);
2158 if (strcasecmp (req[0], "SET") == 0)
2159 rc = attribute_set (client, req + 1);
2160 else if (strcasecmp (req[0], "GET") == 0)
2161 rc = attribute_get (ctx, req + 1);
2162 else if (strcasecmp (req[0], "DELETE") == 0)
2163 rc = attribute_delete (client, req + 1);
2164 else if (strcasecmp (req[0], "LIST") == 0)
2165 rc = attribute_list (ctx, req + 1);
2166 else
2167 rc = GPG_ERR_SYNTAX;
2169 client->flags &= ~(FLAG_ACL_IGNORE|FLAG_ACL_ERROR);
2170 pthread_cleanup_pop (1);
2171 return rc;
2174 static gpg_error_t
2175 attr_command (assuan_context_t ctx, char *line)
2177 struct client_s *client = assuan_get_pointer (ctx);
2178 gpg_error_t rc;
2179 struct argv_s *args[] = {
2180 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2181 NULL
2184 rc = parse_options (&line, args, client, 1);
2185 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2186 rc = GPG_ERR_SYNTAX;
2187 if (rc)
2188 return send_error (ctx, rc);
2190 if (client->opts & OPT_INQUIRE)
2192 unsigned char *result;
2193 size_t len;
2195 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2196 if (rc)
2197 return send_error (ctx, rc);
2199 pthread_cleanup_push ((void *)xfree, result);
2200 rc = do_attr (ctx, (char *)result);
2201 pthread_cleanup_pop (1);
2203 else
2204 rc = do_attr (ctx, line);
2206 return send_error (ctx, rc);
2209 static gpg_error_t
2210 parse_iscached_opt_lock (void *data, void *value)
2212 struct client_s *client = data;
2214 (void) value;
2215 client->opts |= OPT_LOCK;
2216 return 0;
2219 static gpg_error_t
2220 parse_iscached_opt_agent (void *data, void *value)
2222 struct client_s *client = data;
2224 (void) value;
2225 client->opts |= OPT_CACHE_AGENT;
2226 return 0;
2229 static gpg_error_t
2230 parse_iscached_opt_sign (void *data, void *value)
2232 struct client_s *client = data;
2234 (void) value;
2235 client->opts |= OPT_CACHE_SIGN;
2236 return 0;
2239 static gpg_error_t
2240 iscached_command (assuan_context_t ctx, char *line)
2242 struct client_s *client = assuan_get_pointer (ctx);
2243 gpg_error_t rc;
2244 int defer = 0;
2245 struct argv_s *args[] = {
2246 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2247 &(struct argv_s) {"agent", OPTION_TYPE_NOARG, parse_iscached_opt_agent},
2248 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_iscached_opt_sign},
2249 NULL
2252 if (!line || !*line)
2253 return send_error (ctx, GPG_ERR_SYNTAX);
2255 rc = parse_options (&line, args, client, 1);
2256 if (rc)
2257 return send_error (ctx, rc);
2258 else if (!valid_filename (line))
2259 return send_error (ctx, GPG_ERR_INV_VALUE);
2261 if (!(client->flags & FLAG_OPEN)
2262 && ((client->opts & OPT_CACHE_AGENT) || (client->opts & OPT_CACHE_SIGN)))
2263 return send_error (ctx, GPG_ERR_INV_STATE);
2265 rc = cache_iscached (line, &defer, (client->opts & OPT_CACHE_AGENT),
2266 (client->opts & OPT_CACHE_SIGN));
2267 if (!rc && defer)
2268 rc = GPG_ERR_NO_DATA;
2270 if (!rc)
2272 struct cache_data_s *cdata = cache_get_data (line);
2274 if (cdata)
2276 rc = validate_checksum (client, line, cdata, NULL, NULL);
2277 if (rc == GPG_ERR_CHECKSUM)
2278 rc = GPG_ERR_NO_DATA;
2282 if (client->opts & OPT_LOCK
2283 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2285 gpg_error_t trc = rc;
2287 if (strcmp (line, client->filename))
2288 reset_client (client);
2290 xfree (client->filename);
2291 client->filename = str_dup (line);
2292 rc = do_lock (client, 1);
2293 if (!rc)
2294 rc = trc;
2297 return send_error (ctx, rc);
2300 static gpg_error_t
2301 clearcache_command (assuan_context_t ctx, char *line)
2303 struct client_s *client = assuan_get_pointer (ctx);
2304 gpg_error_t rc = 0, all_rc = 0;
2305 int i;
2306 int t;
2307 int all = 0;
2308 struct client_thread_s *once = NULL;
2309 unsigned count;
2311 cache_lock ();
2312 pthread_cleanup_push (cache_release_mutex, NULL);
2313 count = cache_file_count ();
2314 MUTEX_LOCK (&cn_mutex);
2315 pthread_cleanup_push ((void *)release_mutex_cb, &cn_mutex);
2317 if (!line || !*line)
2318 all = 1;
2320 t = slist_length (cn_thread_list);
2322 for (i = 0; i < t; i++)
2324 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2325 assuan_peercred_t peer;
2327 if (!thd->cl)
2328 continue;
2330 /* Lock each connected clients' file mutex to prevent any other client
2331 * from accessing the cache entry (the file mutex is locked upon
2332 * command startup). The cache for the entry is not cleared if the
2333 * file mutex is locked by another client to prevent this function
2334 * from blocking. Rather, it returns an error.
2336 if (all)
2338 if (thd->cl->filename)
2340 rc = do_validate_peer (ctx, thd->cl->filename, &peer);
2341 /* The current client doesn't have permission to open the other
2342 * filename do to "access" configuration parameter in a filename
2343 * section. Since we are clearning all cache entries the error
2344 * will be returned later during cache_clear(). */
2345 if (rc)
2347 rc = 0;
2348 continue;
2351 else // Idle client without opened file?
2352 continue;
2354 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->filename, -1, 0, -1);
2355 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2357 /* The current client owns the lock. */
2358 if (pthread_equal (pthread_self (), thd->tid))
2359 rc = 0;
2360 else
2362 if (cache_iscached (thd->cl->filename, NULL, 0, 0)
2363 == GPG_ERR_NO_DATA)
2365 rc = 0;
2366 continue;
2369 /* The cache entry will be cleared when the other client
2370 * disconnects and cache_timer_thread() does its thing. */
2371 cache_defer_clear (thd->cl->filename);
2374 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2376 rc = 0;
2377 continue;
2380 if (!rc)
2382 rc = cache_clear (NULL, thd->cl->filename, 1);
2383 cache_unlock_mutex (thd->cl->filename, 0);
2386 if (rc)
2387 all_rc = rc;
2389 rc = 0;
2391 else
2393 /* A single data filename was specified. Lock only this data file
2394 * mutex and free the cache entry. */
2395 rc = do_validate_peer (ctx, line, &peer);
2396 if (rc == GPG_ERR_FORBIDDEN)
2397 rc = peer_is_invoker (client);
2399 if (rc == GPG_ERR_EACCES)
2400 rc = GPG_ERR_FORBIDDEN;
2402 if (!rc && thd->cl->filename && !strcmp (thd->cl->filename , line))
2404 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->filename, -1, 0, -1);
2405 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2407 /* The current client owns the lock. */
2408 if (pthread_equal (pthread_self (), thd->tid))
2409 rc = 0;
2412 if (!rc)
2414 once = thd;
2415 rc = cache_clear (NULL, thd->cl->filename, 1);
2416 cache_unlock_mutex (thd->cl->filename, 0);
2418 else
2419 cache_defer_clear (thd->cl->filename);
2421 /* Found a client with the opened file. The cache entry has been
2422 * either cleared (self) or defered to be cleared. */
2423 break;
2428 /* Only connected clients' cache entries have been cleared. Now clear any
2429 * remaining cache entries without clients but only if there wasn't an
2430 * error from above since this would defeat the locking check of the
2431 * remaining entries. */
2432 if (all && !all_rc && cache_file_count ())
2434 rc = cache_clear (client, NULL, 1);
2435 if (rc == GPG_ERR_EACCES)
2436 rc = GPG_ERR_FORBIDDEN;
2439 /* No clients are using the specified file. */
2440 else if (!all_rc && !rc && !once)
2441 rc = cache_clear (NULL, line, 1);
2443 /* Release the connection mutex. */
2444 pthread_cleanup_pop (1);
2446 if (!rc || (cache_file_count () && count != cache_file_count ()))
2447 send_status_all (STATUS_CACHE, NULL);
2449 /* Release the cache mutex. */
2450 pthread_cleanup_pop (1);
2452 /* One or more files were locked while clearing all cache entries. */
2453 if (all_rc)
2454 rc = all_rc;
2456 return send_error (ctx, rc);
2459 static gpg_error_t
2460 cachetimeout_command (assuan_context_t ctx, char *line)
2462 struct client_s *client = assuan_get_pointer (ctx);
2463 int timeout;
2464 char **req = str_split (line, " ", 0);
2465 char *p;
2466 gpg_error_t rc = 0;
2467 assuan_peercred_t peer;
2469 if (!req || !*req || !req[1])
2471 strv_free (req);
2472 return send_error (ctx, GPG_ERR_SYNTAX);
2475 errno = 0;
2476 timeout = (int) strtol (req[1], &p, 10);
2477 if (errno != 0 || *p || timeout < -1)
2479 strv_free (req);
2480 return send_error (ctx, GPG_ERR_SYNTAX);
2483 rc = do_validate_peer (ctx, req[0], &peer);
2484 if (rc == GPG_ERR_FORBIDDEN)
2486 rc = peer_is_invoker (client);
2487 if (rc == GPG_ERR_EACCES)
2488 rc = GPG_ERR_FORBIDDEN;
2491 if (!rc)
2493 rc = cache_set_timeout (req[0], timeout);
2494 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2496 rc = 0;
2497 MUTEX_LOCK (&rcfile_mutex);
2498 config_set_int_param (&global_config, req[0], "cache_timeout",
2499 req[1]);
2500 MUTEX_UNLOCK (&rcfile_mutex);
2504 strv_free (req);
2505 return send_error (ctx, rc);
2508 static gpg_error_t
2509 dump_command (assuan_context_t ctx, char *line)
2511 xmlChar *xml;
2512 int len;
2513 struct client_s *client = assuan_get_pointer (ctx);
2514 gpg_error_t rc;
2516 if (disable_list_and_dump == 1)
2517 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2519 if (line && *line)
2520 return send_error (ctx, GPG_ERR_SYNTAX);
2522 rc = peer_is_invoker(client);
2523 if (rc)
2524 return send_error (ctx, rc);
2526 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2528 if (!xml)
2530 log_write ("%s(%i): %s", __FILE__, __LINE__,
2531 pwmd_strerror (GPG_ERR_ENOMEM));
2532 return send_error (ctx, GPG_ERR_ENOMEM);
2535 pthread_cleanup_push ((void *)xmlFree, xml);
2536 rc = xfer_data (ctx, (char *) xml, len);
2537 pthread_cleanup_pop (1);
2538 return send_error (ctx, rc);
2541 static gpg_error_t
2542 getconfig_command (assuan_context_t ctx, char *line)
2544 struct client_s *client = assuan_get_pointer (ctx);
2545 gpg_error_t rc = 0;
2546 char filename[255] = { 0 }, param[747] = { 0 };
2547 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2549 if (!line || !*line)
2550 return send_error (ctx, GPG_ERR_SYNTAX);
2552 if (strchr (line, ' '))
2554 int ret = sscanf (line, " %254[^ ] %746c", filename, param);
2556 if (ret <= 0)
2557 return send_error (ctx, gpg_error_from_syserror());
2558 paramp = param;
2559 fp = filename;
2562 if (fp && !valid_filename (fp))
2563 return send_error (ctx, GPG_ERR_INV_VALUE);
2565 paramp = str_down (paramp);
2566 p = config_get_value (fp ? fp : "global", paramp);
2567 if (!p)
2568 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2570 tmp = expand_homedir (p);
2571 xfree (p);
2572 if (!tmp)
2574 log_write ("%s(%i): %s", __FILE__, __LINE__,
2575 pwmd_strerror (GPG_ERR_ENOMEM));
2576 return send_error (ctx, GPG_ERR_ENOMEM);
2579 p = tmp;
2580 pthread_cleanup_push ((void *)xfree, p);
2581 rc = xfer_data (ctx, p, strlen (p));
2582 pthread_cleanup_pop (1);
2583 return send_error (ctx, rc);
2586 struct xpath_s
2588 xmlXPathContextPtr xp;
2589 xmlXPathObjectPtr result;
2590 xmlBufferPtr buf;
2591 char **req;
2594 static void
2595 xpath_command_free (void *arg)
2597 struct xpath_s *xpath = arg;
2599 if (!xpath)
2600 return;
2602 req_free (xpath->req);
2604 if (xpath->buf)
2605 xmlBufferFree (xpath->buf);
2607 if (xpath->result)
2608 xmlXPathFreeObject (xpath->result);
2610 if (xpath->xp)
2611 xmlXPathFreeContext (xpath->xp);
2614 static gpg_error_t
2615 do_xpath (assuan_context_t ctx, char *line)
2617 gpg_error_t rc;
2618 struct client_s *client = assuan_get_pointer (ctx);
2619 struct xpath_s _x = { 0 };
2620 struct xpath_s *xpath = &_x;
2622 if (!line || !*line)
2623 return GPG_ERR_SYNTAX;
2625 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2627 if (strv_printf (&xpath->req, "%s", line) == 0)
2628 return GPG_ERR_ENOMEM;
2631 xpath->xp = xmlXPathNewContext (client->doc);
2632 if (!xpath->xp)
2634 rc = GPG_ERR_BAD_DATA;
2635 goto fail;
2638 xpath->result =
2639 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2640 if (!xpath->result)
2642 rc = GPG_ERR_BAD_DATA;
2643 goto fail;
2646 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2648 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2649 goto fail;
2652 rc = xml_recurse_xpath_nodeset (client, client->doc,
2653 xpath->result->nodesetval,
2654 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2655 NULL);
2656 if (rc)
2657 goto fail;
2658 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2660 rc = GPG_ERR_NO_DATA;
2661 goto fail;
2663 else if (xpath->req[1])
2665 rc = 0;
2666 goto fail;
2669 pthread_cleanup_push ((void *)xpath_command_free, &xpath);
2670 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2671 xmlBufferLength (xpath->buf));
2672 pthread_cleanup_pop (0);
2673 fail:
2674 xpath_command_free (xpath);
2675 return rc;
2678 static gpg_error_t
2679 xpath_command (assuan_context_t ctx, char *line)
2681 struct client_s *client = assuan_get_pointer (ctx);
2682 gpg_error_t rc;
2683 struct argv_s *args[] = {
2684 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2685 NULL
2688 if (disable_list_and_dump == 1)
2689 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2691 rc = peer_is_invoker(client);
2692 if (rc)
2693 return send_error (ctx, rc);
2695 rc = parse_options (&line, args, client, 1);
2696 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2697 rc = GPG_ERR_SYNTAX;
2698 if (rc)
2699 return send_error (ctx, rc);
2701 if (client->opts & OPT_INQUIRE)
2703 unsigned char *result;
2704 size_t len;
2706 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2707 if (rc)
2708 return send_error (ctx, rc);
2710 pthread_cleanup_push ((void *)xfree, result);
2711 rc = do_xpath (ctx, (char *)result);
2712 pthread_cleanup_pop (1);
2714 else
2715 rc = do_xpath (ctx, line);
2717 return send_error (ctx, rc);
2720 static gpg_error_t
2721 do_xpathattr (assuan_context_t ctx, char *line)
2723 struct client_s *client = assuan_get_pointer (ctx);
2724 gpg_error_t rc;
2725 char **req = NULL;
2726 int cmd = 0; //SET
2727 struct xpath_s _x = { 0 };
2728 struct xpath_s *xpath = &_x;
2730 if (!line || !*line)
2731 return GPG_ERR_SYNTAX;
2733 if ((req = str_split (line, " ", 3)) == NULL)
2734 return GPG_ERR_ENOMEM;
2736 if (!req[0])
2738 rc = GPG_ERR_SYNTAX;
2739 goto fail;
2742 if (!strcasecmp (req[0], "SET"))
2743 cmd = 0;
2744 else if (!strcasecmp (req[0], "DELETE"))
2745 cmd = 1;
2746 else
2748 rc = GPG_ERR_SYNTAX;
2749 goto fail;
2752 if (!req[1] || !req[2])
2754 rc = GPG_ERR_SYNTAX;
2755 goto fail;
2758 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2760 rc = GPG_ERR_ENOMEM;
2761 goto fail;
2764 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2766 rc = GPG_ERR_SYNTAX;
2767 goto fail;
2770 xpath->xp = xmlXPathNewContext (client->doc);
2771 if (!xpath->xp)
2773 rc = GPG_ERR_BAD_DATA;
2774 goto fail;
2777 xpath->result = xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2778 if (!xpath->result)
2780 rc = GPG_ERR_BAD_DATA;
2781 goto fail;
2784 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2786 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2787 goto fail;
2790 rc = xml_recurse_xpath_nodeset (client, client->doc,
2791 xpath->result->nodesetval,
2792 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2793 (xmlChar *) req[1]);
2795 fail:
2796 xpath_command_free (xpath);
2797 strv_free (req);
2798 return rc;
2801 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2802 static gpg_error_t
2803 xpathattr_command (assuan_context_t ctx, char *line)
2805 struct client_s *client = assuan_get_pointer (ctx);
2806 gpg_error_t rc;
2807 struct argv_s *args[] = {
2808 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2809 NULL
2812 if (disable_list_and_dump == 1)
2813 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2815 rc = peer_is_invoker(client);
2816 if (rc)
2817 return send_error (ctx, rc);
2819 rc = parse_options (&line, args, client, 1);
2820 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2821 rc = GPG_ERR_SYNTAX;
2822 if (rc)
2823 return send_error (ctx, rc);
2825 if (client->opts & OPT_INQUIRE)
2827 unsigned char *result;
2828 size_t len;
2830 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2831 if (rc)
2832 return send_error (ctx, rc);
2834 pthread_cleanup_push ((void *)xfree, result);
2835 rc = do_xpathattr (ctx, (char *)result);
2836 pthread_cleanup_pop (1);
2838 else
2839 rc = do_xpathattr (ctx, line);
2841 return send_error (ctx, rc);
2844 static gpg_error_t
2845 do_import (struct client_s *client, const char *root_element,
2846 unsigned char *content)
2848 xmlDocPtr doc = NULL;
2849 xmlNodePtr n = NULL, root;
2850 gpg_error_t rc;
2851 struct string_s *str = NULL, *tstr = NULL;
2852 struct xml_request_s *req = NULL;
2854 if (!content || !*content)
2855 return GPG_ERR_SYNTAX;
2857 if (root_element)
2859 rc = xml_new_request (client, root_element, XML_CMD_STORE, &req);
2860 if (rc)
2862 xfree (content);
2863 return rc;
2866 if (!xml_valid_element_path (req->args, 0))
2868 xml_free_request (req);
2869 xfree (content);
2870 return GPG_ERR_INV_VALUE;
2874 str = string_new_content ((char *)content);
2875 tstr = string_prepend (str, "<pwmd>");
2876 if (tstr)
2878 str = tstr;
2879 tstr = string_append (str, "</pwmd>");
2880 if (!tstr)
2881 rc = GPG_ERR_ENOMEM;
2883 else
2884 rc = GPG_ERR_ENOMEM;
2886 if (rc)
2887 goto fail;
2889 doc = xmlReadDoc ((xmlChar *) str->str, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2890 string_free (str, 1);
2891 if (!doc)
2893 rc = GPG_ERR_BAD_DATA;
2894 goto fail;
2897 root = xmlDocGetRootElement (doc);
2898 root = root->children;
2899 if (root->type != XML_ELEMENT_NODE)
2901 rc = GPG_ERR_BAD_DATA;
2902 goto fail;
2905 rc = xml_validate_import (client, root);
2906 if (rc)
2907 goto fail;
2909 if (req)
2911 /* Create the new specified root element path, if needed. */
2912 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc),
2913 &rc);
2914 if (rc)
2915 goto fail;
2917 /* Overwrite the children of the specified root element path. */
2918 (void)xml_unlink_node (client, n->children);
2919 n->children = NULL;
2921 else
2922 n = xmlDocGetRootElement (client->doc);
2924 if (n)
2926 xmlNodePtr copy;
2927 xmlChar *name = xml_attribute_value (root, (xmlChar *)"_name");
2929 if (!name)
2930 rc = GPG_ERR_BAD_DATA;
2931 else if (!req)
2933 /* No --root argument passed. Overwrite the current documents' root
2934 * element matching the root element of the imported data. */
2935 xml_free_request (req);
2936 req = NULL;
2937 rc = xml_new_request (client, (char *)name, XML_CMD_DELETE, &req);
2938 xmlFree (name);
2939 if (!rc)
2941 xmlNodePtr tmp;
2943 tmp = xml_find_elements (client, req,
2944 xmlDocGetRootElement (req->doc), &rc);
2945 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2946 goto fail;
2948 if (!rc)
2949 (void)xml_unlink_node (client, tmp);
2951 rc = 0;
2955 if (!rc)
2957 copy = xmlCopyNodeList (root);
2958 if (!copy)
2959 rc = GPG_ERR_ENOMEM;
2960 else
2962 n = xmlAddChildList (n, copy);
2963 if (!n)
2964 rc = GPG_ERR_ENOMEM;
2969 if (!rc && n && n->parent)
2970 rc = xml_update_element_mtime (client, n->parent);
2972 fail:
2973 xml_free_request (req);
2975 if (doc)
2976 xmlFreeDoc (doc);
2978 return rc;
2981 static gpg_error_t
2982 parse_import_opt_root (void *data, void *value)
2984 struct client_s *client = data;
2986 client->import_root = str_dup (value);
2987 return client->import_root ? 0 : GPG_ERR_ENOMEM;
2990 static gpg_error_t
2991 import_command (assuan_context_t ctx, char *line)
2993 gpg_error_t rc;
2994 struct client_s *client = assuan_get_pointer (ctx);
2995 unsigned char *result;
2996 size_t len;
2997 struct argv_s *args[] = {
2998 &(struct argv_s) {"root", OPTION_TYPE_ARG, parse_import_opt_root},
2999 NULL
3002 xfree (client->import_root);
3003 client->import_root = NULL;
3004 rc = parse_options (&line, args, client, 0);
3005 if (rc)
3006 return send_error (ctx, rc);
3008 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3009 if (rc)
3011 xfree (client->import_root);
3012 client->import_root = NULL;
3013 return send_error (ctx, rc);
3016 rc = do_import (client, client->import_root, result);
3017 xfree (client->import_root);
3018 client->import_root = NULL;
3019 return send_error (ctx, rc);
3022 static gpg_error_t
3023 do_lock (struct client_s *client, int add)
3025 gpg_error_t rc = lock_file_mutex (client, add);
3027 if (!rc)
3028 client->flags |= FLAG_LOCK_CMD;
3030 return rc;
3033 static gpg_error_t
3034 lock_command (assuan_context_t ctx, char *line)
3036 struct client_s *client = assuan_get_pointer (ctx);
3037 gpg_error_t rc;
3039 if (line && *line)
3040 return send_error (ctx, GPG_ERR_SYNTAX);
3042 rc = do_lock (client, 0);
3043 return send_error (ctx, rc);
3046 static gpg_error_t
3047 unlock_command (assuan_context_t ctx, char *line)
3049 struct client_s *client = assuan_get_pointer (ctx);
3050 gpg_error_t rc;
3052 if (line && *line)
3053 return send_error (ctx, GPG_ERR_SYNTAX);
3055 rc = unlock_file_mutex (client, 0);
3056 return send_error (ctx, rc);
3059 static void
3060 get_set_env (const char *name, const char *value)
3062 if (value && *value)
3063 setenv (name, value, 1);
3064 else
3065 unsetenv (name);
3068 static gpg_error_t
3069 option_command (assuan_context_t ctx, char *line)
3071 struct client_s *client = assuan_get_pointer (ctx);
3072 gpg_error_t rc = 0;
3073 char namebuf[255] = { 0 };
3074 char *name = namebuf;
3075 char *value = NULL, *p, *tmp = NULL;
3077 p = strchr (line, '=');
3078 if (!p)
3080 strncpy (namebuf, line, sizeof(namebuf));
3081 namebuf[sizeof(namebuf)-1] = 0;
3083 else
3085 strncpy (namebuf, line, strlen (line)-strlen (p));
3086 namebuf[sizeof(namebuf)-1] = 0;
3087 value = p+1;
3090 log_write2 ("OPTION name='%s' value='%s'", name, value);
3092 if (strcasecmp (name, (char *) "lock-timeout") == 0)
3094 long n = 0;
3096 if (value)
3098 n = strtol (value, &tmp, 10);
3099 if (tmp && *tmp)
3100 return send_error (ctx, GPG_ERR_INV_VALUE);
3103 client->lock_timeout = n;
3105 else if (strcasecmp (name, (char *) "client-state") == 0)
3107 long n = 0;
3109 if (value)
3111 n = strtol (value, &tmp, 10);
3112 if ((tmp && *tmp) || (n < 0 || n > 1))
3113 return send_error (ctx, GPG_ERR_INV_VALUE);
3114 client->client_state = n;
3116 else
3117 client->client_state = 0;
3119 else if (strcasecmp (name, (char *) "NAME") == 0)
3121 if (value && strchr (value, ' '))
3122 rc = GPG_ERR_INV_VALUE;
3123 else
3125 tmp = pthread_getspecific (thread_name_key);
3126 pthread_setspecific (thread_name_key, NULL);
3127 xfree (tmp);
3128 MUTEX_LOCK (&cn_mutex);
3130 if (value && *value)
3131 pthread_setspecific (thread_name_key, str_dup (value));
3133 MUTEX_UNLOCK (&cn_mutex);
3136 else if (strcasecmp (name, "disable-pinentry") == 0)
3138 int n = 1;
3140 if (value && *value)
3142 n = (int) strtol (value, &tmp, 10);
3143 if (*tmp || n < 0 || n > 1)
3144 return send_error (ctx, GPG_ERR_INV_VALUE);
3147 if (n)
3148 client->flags |= FLAG_NO_PINENTRY;
3149 else
3150 client->flags &= ~FLAG_NO_PINENTRY;
3152 else if (strcasecmp (name, "ttyname") == 0)
3154 get_set_env ("GPG_TTY", value);
3156 else if (strcasecmp (name, "ttytype") == 0)
3158 get_set_env ("TERM", value);
3160 else if (strcasecmp (name, "display") == 0)
3162 get_set_env ("DISPLAY", value);
3164 else if (strcasecmp (name, "lc_messages") == 0)
3167 else if (strcasecmp (name, "lc_ctype") == 0)
3170 else if (strcasecmp (name, "desc") == 0)
3173 else if (strcasecmp (name, "pinentry-timeout") == 0)
3176 else
3177 rc = GPG_ERR_UNKNOWN_OPTION;
3179 return send_error (ctx, rc);
3182 static gpg_error_t
3183 do_rename (assuan_context_t ctx, char *line)
3185 struct client_s *client = assuan_get_pointer (ctx);
3186 char **args, *p;
3187 xmlNodePtr src, dst;
3188 struct string_s *str = NULL, *tstr;
3189 struct xml_request_s *req;
3190 gpg_error_t rc;
3192 args = str_split (line, " ", 0);
3193 if (!args || strv_length (args) != 2)
3195 strv_free (args);
3196 return GPG_ERR_SYNTAX;
3199 if (!xml_valid_element ((xmlChar *) args[1]))
3201 strv_free (args);
3202 return GPG_ERR_INV_VALUE;
3205 rc = xml_new_request (client, args[0], XML_CMD_RENAME, &req);
3206 if (rc)
3208 strv_free (args);
3209 return rc;
3212 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3213 if (rc)
3214 goto fail;
3216 rc = xml_is_element_owner (client, src);
3217 if (rc)
3218 goto fail;
3220 xmlChar *a = xmlGetProp (src, (xmlChar *) "_name");
3221 if (!a)
3223 rc = GPG_ERR_ENOMEM;
3224 goto fail;
3227 /* To prevent unwanted effects:
3229 * <element _name="a"><element _name="b"/></element>
3231 * RENAME a<TAB>b b
3233 if (xmlStrEqual (a, (xmlChar *) args[1]))
3235 xmlFree (a);
3236 rc = GPG_ERR_EEXIST;
3237 goto fail;
3240 xmlFree (a);
3241 str = string_new (args[0]);
3242 if (!str)
3244 rc = GPG_ERR_ENOMEM;
3245 goto fail;
3248 p = strrchr (str->str, '\t');
3249 if (p)
3250 tstr = string_truncate (str, strlen (str->str)-strlen (p));
3251 else
3252 tstr = string_truncate (str, 0);
3254 if (!tstr)
3256 string_free (str, 1);
3257 rc = GPG_ERR_ENOMEM;
3258 goto fail;
3261 str = tstr;
3262 tstr = string_append_printf (str, "%s%s", str->len ? "\t" : "", args[1]);
3263 if (!tstr)
3265 string_free (str, 1);
3266 rc = GPG_ERR_ENOMEM;
3267 goto fail;
3270 str = tstr;
3271 xml_free_request (req);
3272 rc = xml_new_request (client, str->str, XML_CMD_RENAME, &req);
3273 string_free (str, 1);
3274 if (rc)
3276 req = NULL;
3277 goto fail;
3280 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3281 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3282 goto fail;
3284 rc = 0;
3286 /* Target may exist:
3288 * <element _name="a"/>
3289 * <element _name="b" target="a"/>
3291 * RENAME b a
3293 if (src == dst)
3295 rc = GPG_ERR_EEXIST;
3296 goto fail;
3299 if (dst)
3301 rc = xml_is_element_owner (client, dst);
3302 if (rc)
3303 goto fail;
3305 rc = xml_add_attribute (client, src, "_name", args[1]);
3306 if (!rc)
3307 xml_unlink_node (client, dst);
3309 else
3310 rc = xml_add_attribute (client, src, "_name", args[1]);
3312 fail:
3313 xml_free_request (req);
3314 strv_free (args);
3315 return rc;
3318 static gpg_error_t
3319 rename_command (assuan_context_t ctx, char *line)
3321 struct client_s *client = assuan_get_pointer (ctx);
3322 gpg_error_t rc;
3323 struct argv_s *args[] = {
3324 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3325 NULL
3328 rc = parse_options (&line, args, client, 1);
3329 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3330 rc = GPG_ERR_SYNTAX;
3331 if (rc)
3332 return send_error (ctx, rc);
3334 if (client->opts & OPT_INQUIRE)
3336 unsigned char *result;
3337 size_t len;
3339 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3340 if (rc)
3341 return send_error (ctx, rc);
3343 pthread_cleanup_push ((void *)xfree, result);
3344 rc = do_rename (ctx, (char *)result);
3345 pthread_cleanup_pop (1);
3347 else
3348 rc = do_rename (ctx, line);
3350 return send_error (ctx, rc);
3353 static gpg_error_t
3354 do_copy (assuan_context_t ctx, char *line)
3356 struct client_s *client = assuan_get_pointer (ctx);
3357 char **args, **targs;
3358 xmlNodePtr src, dst, tree = NULL;
3359 struct xml_request_s *req;
3360 gpg_error_t rc;
3362 args = str_split (line, " ", 0);
3363 if (!args || strv_length (args) != 2)
3365 strv_free (args);
3366 return GPG_ERR_SYNTAX;
3369 targs = str_split (args[1], "\t", 0);
3370 if (!targs)
3372 strv_free (args);
3373 return GPG_ERR_SYNTAX;
3376 if (!xml_valid_element_path (targs, 0))
3378 strv_free (args);
3379 strv_free (targs);
3380 return GPG_ERR_INV_VALUE;
3382 strv_free (targs);
3384 rc = xml_new_request (client, args[0], XML_CMD_NONE, &req);
3385 if (rc)
3387 strv_free (args);
3388 return rc;
3391 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3392 if (rc)
3393 goto fail;
3395 tree = xmlCopyNodeList (src);
3396 if (!tree)
3398 rc = GPG_ERR_ENOMEM;
3399 goto fail;
3402 xml_free_request (req);
3403 /* Create the destination element path if it does not exist. */
3404 rc = xml_new_request (client, args[1], XML_CMD_STORE, &req);
3405 if (rc)
3407 req = NULL;
3408 goto fail;
3411 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3412 if (rc)
3413 goto fail;
3415 rc = xml_is_element_owner (client, dst);
3416 if (rc)
3417 goto fail;
3419 rc = xml_validate_target (client, req->doc, args[1], src);
3420 if (rc || src == dst)
3422 rc = rc ? rc : GPG_ERR_EEXIST;
3423 goto fail;
3426 /* Merge any attributes from the src node to the initial dst node. */
3427 for (xmlAttrPtr attr = tree->properties; attr; attr = attr->next)
3429 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3430 continue;
3432 xmlAttrPtr a = xmlHasProp (dst, attr->name);
3433 if (a)
3434 xmlRemoveProp (a);
3436 xmlChar *tmp = xmlNodeGetContent (attr->children);
3437 xmlNewProp (dst, attr->name, tmp);
3438 xmlFree (tmp);
3439 /* Create the default attributes. */
3440 rc = xml_add_attribute (client, dst, NULL, NULL);
3443 xmlNodePtr n = dst->children;
3444 (void)xml_unlink_node (client, n);
3445 dst->children = NULL;
3447 if (tree->children)
3449 n = xmlCopyNodeList (tree->children);
3450 if (!n)
3452 rc = GPG_ERR_ENOMEM;
3453 goto fail;
3456 n = xmlAddChildList (dst, n);
3457 if (!n)
3459 rc = GPG_ERR_ENOMEM;
3460 goto fail;
3463 rc = xml_update_element_mtime (client, xmlDocGetRootElement (client->doc)
3464 == dst->parent ? dst : dst->parent);
3467 fail:
3468 if (tree)
3469 (void)xml_unlink_node (client, tree);
3471 xml_free_request (req);
3472 strv_free (args);
3473 return rc;
3476 static gpg_error_t
3477 copy_command (assuan_context_t ctx, char *line)
3479 struct client_s *client = assuan_get_pointer (ctx);
3480 gpg_error_t rc;
3481 struct argv_s *args[] = {
3482 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3483 NULL
3486 rc = parse_options (&line, args, client, 1);
3487 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3488 rc = GPG_ERR_SYNTAX;
3489 if (rc)
3490 return send_error (ctx, rc);
3492 if (client->opts & OPT_INQUIRE)
3494 unsigned char *result;
3495 size_t len;
3497 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3498 if (rc)
3499 return send_error (ctx, rc);
3501 pthread_cleanup_push ((void *)xfree, result);
3502 rc = do_copy (ctx, (char *)result);
3503 pthread_cleanup_pop (1);
3505 else
3506 rc = do_copy (ctx, line);
3508 return send_error (ctx, rc);
3511 static gpg_error_t
3512 do_move (assuan_context_t ctx, char *line)
3514 struct client_s *client = assuan_get_pointer (ctx);
3515 char **args;
3516 xmlNodePtr src, dst;
3517 struct xml_request_s *req;
3518 gpg_error_t rc;
3520 args = str_split (line, " ", 0);
3521 if (!args || strv_length (args) != 2)
3523 strv_free (args);
3524 return GPG_ERR_SYNTAX;
3527 if (*args[1])
3529 char **targs = str_split (args[1], "\t", 0);
3530 if (!targs)
3532 strv_free (args);
3533 return GPG_ERR_ENOMEM;
3536 if (!xml_valid_element_path (targs, 0))
3538 strv_free (targs);
3539 strv_free (args);
3540 return GPG_ERR_INV_VALUE;
3543 strv_free (targs);
3546 rc = xml_new_request (client, args[0], XML_CMD_MOVE, &req);
3547 if (rc)
3549 strv_free (args);
3550 return rc;
3553 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3554 if (rc)
3555 goto fail;
3557 rc = xml_is_element_owner (client, src);
3558 if (rc)
3559 goto fail;
3561 xml_free_request (req);
3562 req = NULL;
3563 if (*args[1])
3565 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
3566 if (rc)
3567 goto fail;
3569 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc),
3570 &rc);
3572 else
3573 dst = xmlDocGetRootElement (client->doc);
3575 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3576 goto fail;
3578 rc = 0;
3580 for (xmlNodePtr n = dst; n; n = n->parent)
3582 if (n == src)
3584 rc = GPG_ERR_EEXIST;
3585 goto fail;
3589 if (dst)
3591 xmlChar *a = xml_attribute_value (src, (xmlChar *) "_name");
3592 xmlNodePtr dup = xml_find_element (client, dst->children, (char *) a, &rc);
3594 xmlFree (a);
3595 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3596 goto fail;
3598 rc = 0;
3600 if (dup)
3602 if (dup == src)
3604 rc = GPG_ERR_EEXIST;
3605 goto fail;
3608 if (dst == xmlDocGetRootElement (client->doc))
3610 xmlNodePtr n = src;
3611 int match = 0;
3613 while (n->parent && n->parent != dst)
3614 n = n->parent;
3616 a = xml_attribute_value (n, (xmlChar *) "_name");
3617 xmlChar *b = xml_attribute_value (src, (xmlChar *) "_name");
3619 if (xmlStrEqual (a, b))
3621 match = 1;
3622 xmlUnlinkNode (src);
3623 (void)xml_unlink_node (client, n);
3626 xmlFree (a);
3627 xmlFree (b);
3629 if (!match)
3630 (void)xml_unlink_node (client, dup);
3632 else
3633 xmlUnlinkNode (dup);
3637 if (!dst && *req->args)
3639 struct xml_request_s *nreq = NULL;
3640 xmlChar *name = xml_attribute_value (src, (xmlChar *) "_name");
3642 if (src->parent == xmlDocGetRootElement (client->doc)
3643 && !strcmp ((char *) name, *req->args))
3645 xmlFree (name);
3646 rc = GPG_ERR_EEXIST;
3647 goto fail;
3650 xmlFree (name);
3651 rc = xml_new_request (client, args[1], XML_CMD_STORE, &nreq);
3652 if (!rc)
3654 dst = xml_find_elements (client, nreq,
3655 xmlDocGetRootElement (nreq->doc), &rc);
3656 xml_free_request (nreq);
3659 if (rc)
3660 goto fail;
3663 xml_update_element_mtime (client, src->parent);
3664 xmlUnlinkNode (src);
3666 dst = xmlAddChildList (dst, src);
3667 if (!dst)
3668 rc = GPG_ERR_ENOMEM;
3669 else
3670 xml_update_element_mtime (client, dst->parent);
3672 fail:
3673 xml_free_request (req);
3674 strv_free (args);
3675 return rc;
3678 static gpg_error_t
3679 move_command (assuan_context_t ctx, char *line)
3681 struct client_s *client = assuan_get_pointer (ctx);
3682 gpg_error_t rc;
3683 struct argv_s *args[] = {
3684 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3685 NULL
3688 rc = parse_options (&line, args, client, 1);
3689 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3690 rc = GPG_ERR_SYNTAX;
3691 if (rc)
3692 return send_error (ctx, rc);
3694 if (client->opts & OPT_INQUIRE)
3696 unsigned char *result;
3697 size_t len;
3699 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3700 if (rc)
3701 return send_error (ctx, rc);
3703 pthread_cleanup_push ((void *)xfree, result);
3704 rc = do_move (ctx, (char *)result);
3705 pthread_cleanup_pop (1);
3707 else
3708 rc = do_move (ctx, line);
3710 return send_error (ctx, rc);
3713 static gpg_error_t
3714 ls_command (assuan_context_t ctx, char *line)
3716 gpg_error_t rc;
3717 char *tmp;
3718 char *dir;
3719 DIR *d;
3721 if (line && *line)
3722 return send_error (ctx, GPG_ERR_SYNTAX);
3724 tmp = str_asprintf ("%s/data", homedir);
3725 dir = expand_homedir (tmp);
3726 xfree (tmp);
3727 d = opendir (dir);
3728 rc = gpg_error_from_errno (errno);
3730 if (!d)
3732 xfree (dir);
3733 return send_error (ctx, rc);
3736 size_t len =
3737 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3738 struct dirent *p = xmalloc (len), *cur = NULL;
3739 char *list = NULL;
3741 xfree (dir);
3742 pthread_cleanup_push ((void *)xfree, p);
3743 pthread_cleanup_push ((void *)(void *)(void *)closedir, d);
3744 rc = 0;
3746 while (!readdir_r (d, p, &cur) && cur)
3748 struct stat st;
3750 if (lstat (cur->d_name, &st) == -1 || !S_ISREG (st.st_mode))
3751 continue;
3753 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3755 if (!tmp)
3757 if (list)
3758 xfree (list);
3760 rc = GPG_ERR_ENOMEM;
3761 break;
3764 xfree (list);
3765 list = tmp;
3768 pthread_cleanup_pop (1); // closedir (d)
3769 pthread_cleanup_pop (1); // xfree (p)
3771 if (rc)
3772 return send_error (ctx, rc);
3774 if (!list)
3775 return send_error (ctx, GPG_ERR_NO_DATA);
3777 list[strlen (list) - 1] = 0;
3778 pthread_cleanup_push ((void *)xfree, list);
3779 rc = xfer_data (ctx, list, strlen (list));
3780 pthread_cleanup_pop (1);
3781 return send_error (ctx, rc);
3784 static gpg_error_t
3785 bye_notify (assuan_context_t ctx, char *line)
3787 struct client_s *cl = assuan_get_pointer (ctx);
3788 gpg_error_t ret = 0;
3790 (void)line;
3791 update_client_state (cl, CLIENT_STATE_DISCON);
3793 #ifdef WITH_GNUTLS
3794 cl->disco = 1;
3795 if (cl->thd->remote)
3797 int rc;
3801 struct timeval tv = { 0, 50000 };
3803 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3804 if (rc == GNUTLS_E_AGAIN)
3805 select (0, NULL, NULL, NULL, &tv);
3807 while (rc == GNUTLS_E_AGAIN);
3809 #endif
3811 /* This will let assuan_process_next() return. */
3812 if (fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK) == -1)
3814 cl->last_rc = gpg_error_from_errno (errno);
3815 ret = cl->last_rc;
3818 cl->last_rc = 0; // BYE command result
3819 return ret;
3822 static gpg_error_t
3823 reset_notify (assuan_context_t ctx, char *line)
3825 struct client_s *client = assuan_get_pointer (ctx);
3827 (void)line;
3828 if (client)
3829 reset_client (client);
3831 return 0;
3835 * This is called before every Assuan command.
3837 static gpg_error_t
3838 command_startup (assuan_context_t ctx, const char *name)
3840 struct client_s *client = assuan_get_pointer (ctx);
3841 gpg_error_t rc;
3842 struct command_table_s *cmd = NULL;
3844 log_write2 ("command='%s'", name);
3845 client->last_rc = client->opts = 0;
3847 for (int i = 0; command_table[i]; i++)
3849 if (!strcasecmp (name, command_table[i]->name))
3851 if (command_table[i]->ignore_startup)
3852 return 0;
3853 cmd = command_table[i];
3854 break;
3858 if (!cmd)
3859 return GPG_ERR_UNKNOWN_COMMAND;
3861 client->last_rc = rc = gpg_error (file_modified (client, cmd));
3862 if (!rc)
3863 update_client_state (client, CLIENT_STATE_COMMAND);
3865 return rc;
3869 * This is called after every Assuan command.
3871 static void
3872 command_finalize (assuan_context_t ctx, gpg_error_t rc)
3874 struct client_s *client = assuan_get_pointer (ctx);
3876 if (!(client->flags & FLAG_LOCK_CMD))
3877 unlock_file_mutex (client, 0);
3879 unlock_flock (&client->flock_fd);
3880 log_write2 (_("command completed: rc=%u"), rc ? rc : client->last_rc);
3881 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
3882 #ifdef WITH_GNUTLS
3883 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
3884 #endif
3885 if (client->thd->state != CLIENT_STATE_DISCON)
3886 update_client_state (client, CLIENT_STATE_IDLE);
3889 static gpg_error_t
3890 parse_help_opt_html (void *data, void *value)
3892 struct client_s *client = data;
3894 (void) value;
3895 client->opts |= OPT_HTML;
3896 return 0;
3899 static gpg_error_t
3900 help_command (assuan_context_t ctx, char *line)
3902 gpg_error_t rc;
3903 int i;
3904 struct client_s *client = assuan_get_pointer (ctx);
3905 struct argv_s *args[] = {
3906 &(struct argv_s) {"html", OPTION_TYPE_NOARG, parse_help_opt_html},
3907 NULL
3910 rc = parse_options (&line, args, client, 1);
3911 if (rc)
3912 return send_error (ctx, rc);
3914 if (!line || !*line)
3916 char *tmp;
3917 char *help = str_dup (_("Usage: HELP [--html] [<COMMAND>]\n"
3918 "For commands that take an element path as an argument, each element is "
3919 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3920 "@*@*COMMANDS:"));
3922 for (i = 0; command_table[i]; i++)
3924 if (!command_table[i]->help)
3925 continue;
3927 /* @npxref{} won't put a "See " or "see " in front of the command.
3928 * It's not a texinfo command but needed for --html. */
3929 tmp = str_asprintf ("%s %s%s%s", help,
3930 client->opts & OPT_HTML ? "@npxref{" : "",
3931 command_table[i]->name,
3932 client->opts & OPT_HTML ? "}" : "");
3933 xfree (help);
3934 help = tmp;
3937 tmp = strip_texi_and_wrap (help, client->opts & OPT_HTML);
3938 xfree (help);
3939 pthread_cleanup_push ((void *)xfree, tmp);
3940 rc = xfer_data (ctx, tmp, strlen (tmp));
3941 pthread_cleanup_pop (1);
3942 return send_error (ctx, rc);
3945 for (i = 0; command_table[i]; i++)
3947 if (!strcasecmp (line, command_table[i]->name))
3949 char *help, *tmp;
3951 if (!command_table[i]->help)
3952 break;
3954 help = strip_texi_and_wrap (command_table[i]->help,
3955 client->opts & OPT_HTML);
3956 tmp = str_asprintf ("%s%s",
3957 (client->opts & OPT_HTML) ? "" : _("Usage: "),
3958 help);
3959 xfree (help);
3960 pthread_cleanup_push ((void *)xfree, tmp);
3961 rc = xfer_data (ctx, tmp, strlen (tmp));
3962 pthread_cleanup_pop (1);
3963 return send_error (ctx, rc);
3967 return send_error (ctx, GPG_ERR_INV_NAME);
3970 static void
3971 new_command (const char *name, int ignore, int unlock, int flock_type,
3972 gpg_error_t (*handler) (assuan_context_t, char *),
3973 const char *help)
3975 int i = 0;
3976 struct command_table_s **tmp;
3978 if (command_table)
3979 for (i = 0; command_table[i]; i++);
3981 tmp = xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
3982 assert (tmp);
3983 command_table = tmp;
3984 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
3985 command_table[i]->name = name;
3986 command_table[i]->handler = handler;
3987 command_table[i]->ignore_startup = ignore;
3988 command_table[i]->unlock = unlock;
3989 command_table[i]->flock_type = flock_type;
3990 command_table[i++]->help = help;
3991 command_table[i] = NULL;
3994 void
3995 deinit_commands ()
3997 int i;
3999 for (i = 0; command_table[i]; i++)
4000 xfree (command_table[i]);
4002 xfree (command_table);
4005 static int
4006 sort_commands (const void *arg1, const void *arg2)
4008 struct command_table_s *const *a = arg1;
4009 struct command_table_s *const *b = arg2;
4011 if (!*a || !*b)
4012 return 0;
4013 else if (*a && !*b)
4014 return 1;
4015 else if (!*a && *b)
4016 return -1;
4018 return strcmp ((*a)->name, (*b)->name);
4021 static gpg_error_t
4022 passwd_command (assuan_context_t ctx, char *line)
4024 struct client_s *client = assuan_get_pointer (ctx);
4025 gpg_error_t rc;
4027 (void)line;
4028 rc = peer_is_invoker (client);
4029 if (rc == GPG_ERR_EACCES)
4030 return send_error (ctx, GPG_ERR_FORBIDDEN);
4031 else if (rc)
4032 return send_error (ctx, rc);
4034 if (client->flags & FLAG_NEW)
4035 return send_error (ctx, GPG_ERR_INV_STATE);
4037 client->crypto->keyfile = config_get_string (client->filename,
4038 "passphrase_file");
4039 rc = crypto_init_ctx (client->crypto, client->flags & FLAG_NO_PINENTRY,
4040 client->crypto->keyfile);
4041 if (rc)
4042 return send_error (ctx, rc);
4044 if (!rc)
4045 rc = crypto_passwd (client, client->crypto);
4047 crypto_free_non_keys (client->crypto);
4048 return send_error (ctx, rc);
4051 static gpg_error_t
4052 parse_opt_data (void *data, void *value)
4054 struct client_s *client = data;
4056 (void) value;
4057 client->opts |= OPT_DATA;
4058 return 0;
4061 static gpg_error_t
4062 send_client_list (assuan_context_t ctx)
4064 struct client_s *client = assuan_get_pointer (ctx);
4065 gpg_error_t rc = 0;
4067 if (client->opts & OPT_VERBOSE)
4069 unsigned i, t;
4070 char **list = NULL;
4071 char *line;
4073 MUTEX_LOCK (&cn_mutex);
4074 pthread_cleanup_push ((void *)release_mutex_cb, &cn_mutex);
4075 t = slist_length (cn_thread_list);
4077 for (i = 0; i < t; i++)
4079 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
4080 char *tmp;
4082 if (thd->state == CLIENT_STATE_UNKNOWN)
4083 continue;
4085 tmp = build_client_info_line (thd, 0);
4086 if (tmp)
4088 char **l = strv_cat (list, tmp);
4089 if (!l)
4090 rc = GPG_ERR_ENOMEM;
4091 else
4092 list = l;
4094 else
4095 rc = GPG_ERR_ENOMEM;
4097 if (rc)
4099 strv_free (list);
4100 break;
4104 pthread_cleanup_pop (1);
4105 if (rc)
4106 return rc;
4108 line = strv_join ("\n", list);
4109 strv_free (list);
4110 pthread_cleanup_push ((void *)xfree, line);
4111 rc = xfer_data (ctx, line, strlen (line));
4112 pthread_cleanup_pop (1);
4113 return rc;
4116 if (client->opts & OPT_DATA)
4118 char buf[ASSUAN_LINELENGTH];
4120 MUTEX_LOCK (&cn_mutex);
4121 snprintf (buf, sizeof (buf), "%u", slist_length (cn_thread_list));
4122 MUTEX_UNLOCK (&cn_mutex);
4123 rc = xfer_data (ctx, buf, strlen (buf));
4125 else
4126 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4128 return rc;
4131 static gpg_error_t
4132 getinfo_command (assuan_context_t ctx, char *line)
4134 struct client_s *client = assuan_get_pointer (ctx);
4135 gpg_error_t rc;
4136 char buf[ASSUAN_LINELENGTH];
4137 struct argv_s *args[] = {
4138 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4139 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_opt_verbose},
4140 NULL
4143 rc = parse_options (&line, args, client, 1);
4144 if (rc)
4145 return send_error (ctx, rc);
4147 if (!strcasecmp (line, "clients"))
4149 rc = send_client_list (ctx);
4151 else if (!strcasecmp (line, "cache"))
4153 if (client->opts & OPT_DATA)
4155 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4156 rc = xfer_data (ctx, buf, strlen (buf));
4158 else
4159 rc = send_status (ctx, STATUS_CACHE, NULL);
4161 else if (!strcasecmp (line, "pid"))
4163 pid_t pid = getpid ();
4165 snprintf (buf, sizeof (buf), "%u", pid);
4166 rc = xfer_data (ctx, buf, strlen (buf));
4168 else if (!strcasecmp (line, "version"))
4170 char *tmp = str_asprintf ("0x%06x %s", VERSION_HEX,
4171 #ifdef WITH_GNUTLS
4172 "GNUTLS "
4173 #endif
4174 #ifdef WITH_LIBACL
4175 "ACL "
4176 #endif
4177 "");
4178 pthread_cleanup_push (xfree, tmp);
4179 rc = xfer_data (ctx, tmp, strlen (tmp));
4180 pthread_cleanup_pop (1);
4182 else if (!strcasecmp (line, "last_error"))
4184 if (client->last_error)
4185 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4186 else
4187 rc = GPG_ERR_NO_DATA;
4189 else if (!strcasecmp (line, "user"))
4191 char *user = NULL;
4193 #ifdef WITH_GNUTLS
4194 if (client->thd->remote)
4195 user = str_asprintf ("#%s", client->thd->tls->fp);
4196 else
4197 user = get_username (client->thd->peer->uid);
4198 #else
4199 user = get_username (client->thd->peer->uid);
4200 #endif
4201 if (user)
4203 pthread_cleanup_push ((void *)xfree, user);
4204 rc = xfer_data (ctx, user, strlen (user));
4205 pthread_cleanup_pop (1);
4207 else
4208 rc = GPG_ERR_NO_DATA;
4210 else
4211 rc = gpg_error (GPG_ERR_SYNTAX);
4213 return send_error (ctx, rc);
4216 static gpg_error_t
4217 parse_listkeys_opt_secret_only (void *data, void *value)
4219 struct client_s *client = data;
4221 (void) value;
4222 client->opts |= OPT_SECRET_ONLY;
4223 return 0;
4226 static gpg_error_t
4227 keyinfo_command (assuan_context_t ctx, char *line)
4229 struct client_s *client = assuan_get_pointer (ctx);
4230 gpg_error_t rc = 0;
4231 char **keys = NULL, **p = NULL;
4232 int sym = 0;
4234 if (line && *line)
4235 return send_error (ctx, GPG_ERR_SYNTAX);
4237 if (!(client->flags & FLAG_OPEN))
4238 return send_error (ctx, GPG_ERR_INV_STATE);
4240 if (client->flags & FLAG_NEW)
4241 return send_error (ctx, GPG_ERR_NO_DATA);
4243 rc = lock_flock (ctx, client->filename, FLOCK_TYPE_SH, &client->flock_fd);
4244 if (rc)
4245 return send_error (ctx, rc);
4247 rc = crypto_is_symmetric (client->filename);
4248 unlock_flock (&client->flock_fd);
4249 if (!rc)
4250 sym = 1;
4251 else if (rc != GPG_ERR_BAD_DATA)
4252 return send_error (ctx, rc);
4254 rc = 0;
4255 if (!sym)
4257 p = strv_catv(keys, client->crypto->pubkey);
4258 if (!p)
4259 rc = GPG_ERR_ENOMEM;
4262 if (!rc)
4264 keys = p;
4265 for (p = client->crypto->sigkey; p && *p; p++)
4267 if (!strv_printf(&keys, "S%s", *p))
4269 strv_free (keys);
4270 return send_error (ctx, GPG_ERR_ENOMEM);
4274 else
4275 rc = GPG_ERR_ENOMEM;
4277 if (!rc)
4279 if (keys)
4281 line = strv_join ("\n", keys);
4282 strv_free (keys);
4283 pthread_cleanup_push ((void *)xfree, line);
4284 if (line)
4285 rc = xfer_data (ctx, line, strlen (line));
4286 else
4287 rc = GPG_ERR_ENOMEM;
4289 pthread_cleanup_pop (1);
4291 else
4292 rc = GPG_ERR_NO_DATA;
4294 else
4295 strv_free (keys);
4297 return send_error (ctx, rc);
4300 static gpg_error_t
4301 kill_command (assuan_context_t ctx, char *line)
4303 struct client_s *client = assuan_get_pointer (ctx);
4304 gpg_error_t rc;
4305 unsigned i, t;
4307 if (!line || !*line)
4308 return send_error (ctx, GPG_ERR_SYNTAX);
4310 MUTEX_LOCK (&cn_mutex);
4311 pthread_cleanup_push ((void *)release_mutex_cb, &cn_mutex);
4312 t = slist_length (cn_thread_list);
4313 rc = GPG_ERR_ESRCH;
4315 for (i = 0; i < t; i++)
4317 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
4318 char *tmp = str_asprintf ("%p", thd->tid);
4320 if (strcmp (line, tmp))
4322 xfree (tmp);
4323 continue;
4326 xfree (tmp);
4327 rc = peer_is_invoker (client);
4328 if (!rc)
4330 #ifdef HAVE_PTHREAD_CANCEL
4331 pthread_cancel (thd->tid);
4332 #else
4333 pthread_kill (thd->tid, SIGUSR2);
4334 #endif
4335 break;
4337 else if (rc == GPG_ERR_EACCES)
4338 rc = GPG_ERR_FORBIDDEN;
4339 else if (rc)
4340 break;
4342 if (config_get_boolean ("global", "strict_kill"))
4343 break;
4345 #ifdef WITH_GNUTLS
4346 if (client->thd->remote && thd->remote)
4348 if (!thd->tls || !thd->tls->fp)
4350 rc = GPG_ERR_INV_STATE;
4351 break;
4353 if (!strcmp (client->thd->tls->fp, thd->tls->fp))
4355 #ifdef HAVE_PTHREAD_CANCEL
4356 pthread_cancel (thd->tid);
4357 #else
4358 pthread_kill (thd->tid, SIGUSR2);
4359 #endif
4360 break;
4363 else if (!client->thd->remote && !thd->remote)
4364 #endif
4366 if (client->thd->peer->uid == thd->peer->uid)
4368 #ifdef HAVE_PTHREAD_CANCEL
4369 pthread_cancel (thd->tid);
4370 #else
4371 pthread_kill (thd->tid, SIGUSR2);
4372 #endif
4375 break;
4378 pthread_cleanup_pop (1);
4379 return send_error (ctx, rc);
4382 static gpg_error_t
4383 listkeys_command (assuan_context_t ctx, char *line)
4385 struct client_s *client = assuan_get_pointer (ctx);
4386 struct crypto_s *crypto = NULL;
4387 char **pattern = NULL;
4388 gpgme_key_t *keys = NULL;
4389 char **result = NULL;
4390 gpg_error_t rc;
4391 struct argv_s *args[] = {
4392 &(struct argv_s) {"secret-only", OPTION_TYPE_NOARG,
4393 parse_listkeys_opt_secret_only },
4394 NULL
4397 rc = parse_options (&line, args, client, 1);
4398 if (rc)
4399 return send_error (ctx, rc);
4401 rc = crypto_init (&crypto, client->ctx, client->filename,
4402 client->flags & FLAG_NO_PINENTRY, NULL);
4403 if (rc)
4404 return send_error (ctx, rc);
4406 pthread_cleanup_push ((void *)crypto_free, crypto);
4407 pattern = str_split (line, ",", 0);
4408 pthread_cleanup_push ((void *)(void *)strv_free, pattern);
4409 rc = crypto_list_keys (crypto, pattern, client->opts & OPT_SECRET_ONLY,
4410 &keys);
4411 pthread_cleanup_pop (1);
4412 pthread_cleanup_pop (1);
4413 if (!rc)
4415 int i;
4417 for (i = 0; keys[i]; i++)
4419 char *p = crypto_key_info (keys[i]);
4420 char **r;
4422 if (!p)
4424 rc = GPG_ERR_ENOMEM;
4425 break;
4428 r = strv_cat (result, p);
4429 if (!r)
4431 rc = GPG_ERR_ENOMEM;
4432 break;
4435 result = r;
4438 if (!i)
4439 rc = GPG_ERR_NO_DATA;
4441 crypto_free_key_list (keys);
4444 if (!rc)
4446 line = strv_join ("\n", result);
4447 strv_free (result);
4448 pthread_cleanup_push ((void *)xfree, line);
4449 if (!line)
4450 rc = GPG_ERR_ENOMEM;
4451 else
4452 rc = xfer_data (ctx, line, strlen (line));
4454 pthread_cleanup_pop (1);
4456 else
4457 strv_free (result);
4459 if (gpg_err_code (rc) == GPG_ERR_NO_PUBKEY
4460 || gpg_err_code (rc) == GPG_ERR_NO_SECKEY)
4461 rc = GPG_ERR_NO_DATA;
4463 return send_error (ctx, rc);
4466 void
4467 init_commands ()
4469 /* !BEGIN-HELP-TEXT!
4471 * This comment is used as a marker to generate the offline documentation
4472 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4473 * script to determine where commands begin and end.
4475 new_command("HELP", 1, 1, 0, help_command, _(
4476 "HELP [--html] [<COMMAND>]\n"
4477 "Show available commands or command specific help text."
4478 "@*@*"
4479 "The @option{--html} option will output the help text in HTML format."
4482 new_command("KILL", 1, 0, 0, kill_command, _(
4483 "KILL <thread_id>\n"
4484 "Terminates the client identified by @var{thread_id} and releases any file "
4485 "lock or other resources it has held. See @code{GETINFO} (@pxref{GETINFO}) "
4486 "for details about listing connected clients. An @code{invoking_user} "
4487 "(@pxref{Configuration}) may kill any client while others may only kill "
4488 "clients of the same @code{UID} or @abbr{TLS} fingerprint."
4491 new_command("LISTKEYS", 0, 0, 0, listkeys_command, _(
4492 "LISTKEYS [--secret-only] [pattern[,<pattern>]]\n"
4493 "Returns a new line separated list of key information matching a comma "
4494 "separated list of @var{pattern}'s. When option @option{--secret-only} is "
4495 "specified, only keys matching @var{pattern} that also have a secret key "
4496 "available will be returned."
4499 new_command("KEYINFO", 1, 0, 0, keyinfo_command, _(
4500 "KEYINFO\n"
4501 "Returns a new line separated list of key ID's that the currently opened "
4502 "data file has recipients and signers for. If the key is a signing key it "
4503 "will be prefixed with an @code{S}. If the file is a new one, or has no "
4504 "signers in the case of being symmetrically encrypted, the error code "
4505 "@code{GPG_ERR_NO_DATA} is returned."
4508 new_command("GETINFO", 1, 1, 0, getinfo_command, _(
4509 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4510 "Get server and other information. The information is returned via a status "
4511 "message (@pxref{Status Messages}) unless otherwise noted or @option{--data} "
4512 "is specified."
4513 "@*@*"
4514 "@var{CACHE} returns the number of cached documents."
4515 "@*@*"
4516 "@var{CLIENTS} returns the number of "
4517 "connected clients via a status message or a list of connected clients when "
4518 "the @option{--verbose} parameter is used (implies @option{--data}). A "
4519 "verbose line of a client list contains "
4520 "space delimited "
4521 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4522 "IP address if remote, file lock status, whether the current client is self "
4523 "or not, client state (see below), "
4524 "user ID or TLS fingerprint of the connected client, username if the "
4525 "client is a local one else @code{-}, and finally the time stamp of when the "
4526 "client connected."
4527 "@*@*"
4528 "Client state @code{0} is an unknown client state, state @code{1} indicates "
4529 "the client has connected but hasn't completed initializing, state @code{2} "
4530 "indicates that the client is idle, state @code{3} means the "
4531 "client is in a command and state @code{4} means the client is disconnecting. "
4532 "@*@*"
4533 "@var{PID} returns the process ID number of the server via a data response."
4534 "@*@*"
4535 "@var{VERSION} returns the server version number and compile-time features "
4536 "via a data response with each being space delimited."
4537 "@*@*"
4538 "@var{LAST_ERROR} returns a detailed description of the last failed command "
4539 "via a data response, when available."
4540 "@*@*"
4541 "@var{USER} returns the username or @abbr{TLS} hash of the connected client "
4542 "via a data response."
4545 new_command("PASSWD", 0, 0, FLOCK_TYPE_EX|FLOCK_TYPE_KEEP, passwd_command, _(
4546 "PASSWD\n"
4547 "Changes the passphrase of the secret key required to open the current "
4548 "data file. If the data file is symmetrically encrypted, the error "
4549 "@code{GPG_ERR_NOT_SUPPORTED} is returned. When symmetrically encrypted, "
4550 "the @code{SAVE} command (@pxref{SAVE}) should be used instead to prevent "
4551 "this command saving any unwanted changes to the @abbr{XML} document."
4552 "@*@*"
4553 "This command is not available to non-invoking clients "
4554 "(@pxref{Access Control})."
4557 new_command("OPEN", 1, 1, FLOCK_TYPE_SH|FLOCK_TYPE_KEEP, open_command, _(
4558 "OPEN [--lock] <filename>\n"
4559 "Opens @var{filename}. When the @var{filename} is not found on the "
4560 "file-system then a new in-memory document will be created. If the file is "
4561 "found, it is looked for in the file cache and when found no passphrase will "
4562 "be required to open it. When not cached, @cite{pinentry(1)} will be used to "
4563 "retrieve the passphrase for decryption unless @option{disable-pinentry} "
4564 "(@pxref{OPTION}) was specified in which case @command{pwmd} will "
4565 "@emph{INQUIRE} the client for the passphrase."
4566 "@*@*"
4567 "When the @option{--lock} option is passed then the file mutex will be "
4568 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4569 "file had been opened."
4572 new_command("SAVE", 0, 0, FLOCK_TYPE_EX|FLOCK_TYPE_KEEP, save_command, _(
4573 "SAVE [--symmetric] [--inquire-keyparam] [--keyid=<fpr>[,..] | [--inquire-keyid]] [--sign-keyid=<fpr>[,..] | [--inquire-sign-keyid]]\n"
4574 "Writes the in-memory @abbr{XML} document to disk. The file written to is the "
4575 "file that was opened using the @code{OPEN} command (@pxref{OPEN}). If the "
4576 "file is a new one or the option @option{--inquire-keyparam} was passed, a "
4577 "new keypair will be generated and @cite{pinentry(1)} will be used to prompt "
4578 "for the passphrase of the secret key used for signing."
4579 "@*@*"
4580 "The @option{--inquire-keyparam} option will send an "
4581 "@emph{INQUIRE} to the client to obtain the key parameters to use for "
4582 "generating the new keypair. The inquired data is expected to be in "
4583 "@cite{gnupg} @abbr{XML} format. See the @cite{gnupg} documentation for "
4584 "details. Note that when this option is specified a new keypair will be "
4585 "generated reguardless if the file is a new one and that the passphrase for "
4586 "the current file will be required before generating the new keypair. This "
4587 "option is available to non-invoking clients (@pxref{Access Control}) only "
4588 "when the file is a new one."
4589 "@*@*"
4590 "You can encrypt the data file to a recipient other than the one that it "
4591 "was encrypted with by passing the @option{--keyid} or "
4592 "@option{--inquire-keyid} option with "
4593 "the fingerprint of a public encryption key as its argument. Use the "
4594 "@command{LISTKEYS} command (@pxref{LISTKEYS}) to show key information by "
4595 "pattern. The @option{--sign-keyid} or @option{--inquire-sign-keyid} option "
4596 "may also be used to sign the data "
4597 "file with an alternate key by specifying the fingerprint of a secret key. "
4598 "A passphrase to decrypt the data file "
4599 "will be required when one or more of the original encryption or signing keys "
4600 "are not found in either of these two options' arguments or when the data "
4601 "file is symmetrically encrypted, reguardless of the @code{require_save_key} "
4602 "configuration parameter. The original encryption or signing keys will be "
4603 "used when either of these options are not specified."
4604 "@*@*"
4605 "The @option{--keyid} and @option{--sign-keyid} options are not available "
4606 "for non-invoking clients "
4607 "(@pxref{Access Control}) when the recipients or signers do not match those "
4608 "that were used when the file was @code{OPEN}'ed."
4609 "@*@*"
4610 "The @option{--symmetric} option specifies that a new data file be "
4611 "conventionally encrypted. These types of data files do not use a recipient "
4612 "public key but may be signed by using the @option{--sign-keyid} or "
4613 "@option{--inquire-sign-keyid} options. To remove all signers from a "
4614 "symmtrically encrypted data file, leave the option value empty. Note that "
4615 "you cannot change encryption schemes once a data file has been saved."
4618 new_command("ISCACHED", 1, 0, 0, iscached_command, _(
4619 "ISCACHED [--lock] [--agent [--sign]] <filename>\n"
4620 "Determines the file cache status of the specified @var{filename}. "
4621 "The default is to test whether the filename is cached in memory. Passing "
4622 "option @option{--agent} will test the @command{gpg-agent} cache for at most "
4623 "one cached key used for opening the data file (@pxref{OPEN}). To test if "
4624 "a signing key is cached, pass @option{--sign} along with @option{--agent}. "
4625 "Both the @option{--agent} and @option{--sign} options require an opened data "
4626 "file."
4627 "@*@*"
4628 "An @emph{OK} response is returned if the specified @var{filename} is found "
4629 "in the cache. If not found in the cache but exists on the filesystem "
4630 "then @code{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4631 "returned."
4632 "@*@*"
4633 "The @option{--lock} option will lock the file mutex of @var{filename} when "
4634 "the file exists; it does not need to be opened nor cached. The lock will be "
4635 "released when the client exits or sends the @code{UNLOCK} command "
4636 "(@pxref{UNLOCK}). When this option is passed the current data file is closed."
4639 new_command("CLEARCACHE", 1, 1, 0, clearcache_command, _(
4640 "CLEARCACHE [<filename>]\n"
4641 "Clears a file cache entry for all or the specified @var{filename}. Note that "
4642 "this will also clear any @command{gpg-agent} cached keys which may cause "
4643 "problems if another data file shares the same keys as @var{filename}."
4644 "@*@*"
4645 "When clearing all cache entries a permissions test is done against the "
4646 "current client based on the @var{access} configuration parameter in a "
4647 "@var{filename} section. Both a cache entry may be cleared and an error "
4648 "returned depending on cached data files and client permissions."
4651 new_command("CACHETIMEOUT", 1, 1, 0, cachetimeout_command, _(
4652 "CACHETIMEOUT <filename> <seconds>\n"
4653 "The time in @var{seconds} until @var{filename} will be removed from the "
4654 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4655 "the passphrase for each @code{OPEN} command (@pxref{OPEN}) or @code{SAVE} "
4656 "(@pxref{SAVE}) command. @xref{Configuration}, and the @code{cache_timeout} "
4657 "parameter."
4660 new_command("LIST", 0, 1, 0, list_command, _(
4661 "LIST [--inquire] [--recurse] [element[<TAB>child[..]]]\n"
4662 "If no element path is given then a newline separated list of root elements "
4663 "is returned with a data response. If given, then children of the specified "
4664 "element path are returned."
4665 "@*@*"
4666 "Each element path "
4667 "returned will have zero or more flags appened to it. These flags are "
4668 "delimited from the element path by a single space character. A flag itself "
4669 "is a single character. Flag @code{P} indicates that access to the element "
4670 "is denied. Flag @code{+} indicates that there are child nodes of "
4671 "the current element path. Flag @code{E} indicates that an element of the "
4672 "element path contained in a @var{target} attribute could not be found. Flag "
4673 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4674 "(@pxref{Configuration}). Flag @code{T}, followed by a single space character, "
4675 "then an element path, is the element path of the @var{target} attribute "
4676 "contained in the current element."
4677 "@*@*"
4678 "When a specified element path contains an error, beit from the final "
4679 "element in the path or any previous element, the path is still shown but "
4680 "will contain the error flag for the element with the error. Determining "
4681 "the actual element which contains the error is up to the client. This can be "
4682 "done by traversing the final element up to parent elements that contain the "
4683 "same error flag."
4684 "@*@*"
4685 "The option @option{--recurse} may be used to list the entire element tree "
4686 "for a specified element path or the entire tree for all root elements."
4687 "@*@*"
4688 "When the @option{--inquire} option is passed then all remaining non-option "
4689 "arguments are retrieved via a server @emph{INQUIRE}."
4692 new_command("REALPATH", 0, 1, 0, realpath_command, _(
4693 "REALPATH [--inquire] element[<TAB>child[..]]\n"
4694 "Resolves all @code{target} attributes of the specified element path and "
4695 "returns the result with a data response. @xref{Target Attribute}, for details."
4696 "@*@*"
4697 "When the @option{--inquire} option is passed then all remaining non-option "
4698 "arguments are retrieved via a server @emph{INQUIRE}."
4701 new_command("STORE", 0, 1, 0, store_command, _(
4702 "STORE element[<TAB>child[..]]<TAB>[content]\n"
4703 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4704 "@*@*"
4705 "Creates a new element path or modifies the @var{content} of an existing "
4706 "element. If only a single element is specified then a new root element is "
4707 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4708 "set to the final @key{TAB} delimited element. If no @var{content} is "
4709 "specified after the final @key{TAB}, then the content of the existing "
4710 "element will be removed; or will be empty if creating a new element."
4711 "@*@*"
4712 "The only restriction of an element name is that it not contain whitespace "
4713 "characters. There is no other whitespace between the @key{TAB} delimited "
4714 "elements. It is recommended that the content of an element be base64 encoded "
4715 "when it contains control or @key{TAB} characters to prevent @abbr{XML} "
4716 "parsing and @command{pwmd} syntax errors."
4719 new_command("RENAME", 0, 1, 0, rename_command, _(
4720 "RENAME [--inquire] element[<TAB>child[..]] <value>\n"
4721 "Renames the specified @var{element} to the new @var{value}. If an element of "
4722 "the same name as the @var{value} already exists it will be overwritten."
4723 "@*@*"
4724 "When the @option{--inquire} option is passed then all remaining non-option "
4725 "arguments are retrieved via a server @emph{INQUIRE}."
4728 new_command("COPY", 0, 1, 0, copy_command, _(
4729 "COPY [--inquire] source[<TAB>child[..]] dest[<TAB>child[..]]\n"
4730 "Copies the entire element tree starting from the child node of the source "
4731 "element, to the destination element path. If the destination element path "
4732 "does not exist then it will be created; otherwise it is overwritten."
4733 "@*@*"
4734 "Note that attributes from the source element are merged into the "
4735 "destination element when the destination element path exists. When an "
4736 "attribute of the same name exists in both the source and destination "
4737 "elements then the destination attribute will be updated to the source "
4738 "attribute value."
4739 "@*@*"
4740 "When the @option{--inquire} option is passed then all remaining non-option "
4741 "arguments are retrieved via a server @emph{INQUIRE}."
4744 new_command("MOVE", 0, 1, 0, move_command, _(
4745 "MOVE [--inquire] source[<TAB>child[..]] [dest[<TAB>child[..]]]\n"
4746 "Moves the source element path to the destination element path. If the "
4747 "destination is not specified then it will be moved to the root node of the "
4748 "document. If the destination is specified and exists then it will be "
4749 "overwritten; otherwise non-existing elements of the destination element "
4750 "path will be created."
4751 "@*@*"
4752 "When the @option{--inquire} option is passed then all remaining non-option "
4753 "arguments are retrieved via a server @emph{INQUIRE}."
4756 new_command("DELETE", 0, 1, 0, delete_command, _(
4757 "DELETE [--inquire] element[<TAB>child[..]]\n"
4758 "Removes the specified element path and all of its children. This may break "
4759 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4760 "refers to this element or any of its children."
4761 "@*@*"
4762 "When the @option{--inquire} option is passed then all remaining non-option "
4763 "arguments are retrieved via a server @emph{INQUIRE}."
4766 new_command("GET", 0, 1, 0, get_command, _(
4767 "GET [--inquire] element[<TAB>child[..]]\n"
4768 "Retrieves the content of the specified element. The content is returned "
4769 "with a data response."
4770 "@*@*"
4771 "When the @option{--inquire} option is passed then all remaining non-option "
4772 "arguments are retrieved via a server @emph{INQUIRE}."
4775 new_command("ATTR", 0, 1, 0, attr_command, _(
4776 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] element[<TAB>child[..]] ..\n"
4777 "@table @asis\n"
4778 "@item ATTR SET attribute element[<TAB>child[..]] [value]\n"
4779 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4780 "element. When no @var{value} is specified any existing value will be removed."
4781 "@*@*"
4782 "@item ATTR DELETE attribute element[<TAB>child[..]]\n"
4783 " Removes an attribute from an element. If @var{attribute} is @code{_name} "
4784 "or @code{target} an error is returned. Use the @command{DELETE} command "
4785 "(@pxref{DELETE}) instead."
4786 "@*@*"
4787 "@item ATTR LIST element[<TAB>child[..]]\n"
4788 " Retrieves a newline separated list of attributes names and values "
4789 "from the specified element. Each attribute name and value is space delimited."
4790 "@*@*"
4791 "@item ATTR GET attribute element[<TAB>child[..]]\n"
4792 " Retrieves the value of an @var{attribute} from an element."
4793 "@end table\n"
4794 "@*@*"
4795 "When the @option{--inquire} option is passed then all remaining non-option "
4796 "arguments are retrieved via a server @emph{INQUIRE}."
4797 "@*@*"
4798 "@xref{Target Attribute}, for details about this special attribute and also "
4799 "@pxref{Other Attributes} for other attributes that are handled specially "
4800 "by @command{pwmd}."
4803 new_command("XPATH", 0, 1, 0, xpath_command, _(
4804 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4805 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4806 "specified it is assumed the expression is a request to return a result. "
4807 "Otherwise, the result is set to the @var{value} argument and the document is "
4808 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4809 "is assumed to be empty and the document is updated. For example:"
4810 "@sp 1\n"
4811 "@example\n"
4812 "XPATH //element[@@_name='password']@key{TAB}\n"
4813 "@end example\n"
4814 "@sp 1\n"
4815 "would clear the content of all @var{password} elements in the data file "
4816 "while leaving off the trailing @key{TAB} would return all @var{password} "
4817 "elements in @abbr{XML} format."
4818 "@*@*"
4819 "When the @option{--inquire} option is passed then all remaining non-option "
4820 "arguments are retrieved via a server @emph{INQUIRE}."
4821 "@*@*"
4822 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4823 "expression syntax."
4826 new_command("XPATHATTR", 0, 1, 0, xpathattr_command, _(
4827 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4828 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4829 "attributes and does not return a result. For the @var{SET} operation the "
4830 "@var{value} is optional but the field is required. If not specified then "
4831 "the attribute value will be empty. For example:"
4832 "@sp 1\n"
4833 "@example\n"
4834 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4835 "@end example\n"
4836 "@sp 1\n"
4837 "would create a @var{password} attribute for each @var{password} element "
4838 "found in the document. The attribute value will be empty but still exist."
4839 "@*@*"
4840 "When the @option{--inquire} option is passed then all remaining non-option "
4841 "arguments are retrieved via a server @emph{INQUIRE}."
4842 "@*@*"
4843 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4844 "expression syntax."
4847 new_command("IMPORT", 0, 1, 0, import_command, _(
4848 "IMPORT [--root=element[<TAB>child[..]]]\n"
4849 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4850 "@*@*"
4851 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4852 "argument is raw @abbr{XML} data. The content is created as a child of "
4853 "the element path specified with the @option{--root} option or at the "
4854 "document root when not specified. Existing elements of the same name will "
4855 "be overwritten."
4856 "@*@*"
4857 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4858 "for details."
4861 new_command("DUMP", 0, 1, 0, dump_command, _(
4862 "DUMP\n"
4863 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4864 "dumping a specific node."
4867 new_command("LOCK", 0, 0, 0, lock_command, _(
4868 "LOCK\n"
4869 "Locks the mutex associated with the opened file. This prevents other clients "
4870 "from sending commands to the same opened file until the client "
4871 "that sent this command either disconnects or sends the @code{UNLOCK} "
4872 "command. @xref{UNLOCK}."
4875 new_command("UNLOCK", 1, 0, 0, unlock_command, _(
4876 "UNLOCK\n"
4877 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4878 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4879 "@pxref{ISCACHED})."
4882 new_command("GETCONFIG", 1, 1, 0, getconfig_command, _(
4883 "GETCONFIG [filename] <parameter>\n"
4884 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4885 "data response. If no file has been opened then the value for @var{filename} "
4886 "or the default from the @var{global} section will be returned. If a file "
4887 "has been opened and no @var{filename} is specified, the value previously "
4888 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4891 new_command("OPTION", 1, 1, 0, option_command, _(
4892 "OPTION <NAME>=[<VALUE>]\n"
4893 "Sets a client option @var{name} to @var{value}. The value for an option is "
4894 "kept for the duration of the connection with the exception of the "
4895 "@command{pinentry} options which are defaults for all future connections "
4896 "(@pxref{Pinentry}). When @var{value} is empty the option is unset."
4897 "@*@*"
4898 "@table @asis\n"
4899 "@item DISABLE-PINENTRY\n"
4900 "Disable use of @command{pinentry} for passphrase retrieval. When @code{1}, a "
4901 "server inquire is sent to the client to obtain the passphrase. This option "
4902 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4903 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands. Set to @code{0} "
4904 "to use a @command{pinentry}."
4905 "@*@*"
4906 "@item DISPLAY\n"
4907 "Set or unset the X11 display to use when prompting for a passphrase."
4908 "@*@*"
4909 "@item TTYNAME\n"
4910 "Set the terminal device path to use when prompting for a passphrase."
4911 "@*@*"
4912 "@item TTYTYPE\n"
4913 "Set the terminal type for use with @option{TTYNAME}."
4914 "@*@*"
4915 "@item NAME\n"
4916 "Associates the thread ID of the connection with the specified textual "
4917 "representation. Useful for debugging log messages. May not contain whitespace."
4918 "@*@*"
4919 "@item LOCK-TIMEOUT\n"
4920 "When not @code{0}, the duration in tenths of a second to wait for the file "
4921 "mutex which has been locked by another thread to be released before returning "
4922 "an error. When @code{-1} the error will be returned immediately."
4923 "@*@*"
4924 "@item CLIENT-STATE\n"
4925 "When set to @code{1} then client state status messages for other clients are "
4926 "sent to the current client. The default is @code{0}."
4927 "@end table\n"
4930 new_command("LS", 1, 1, 0, ls_command, _(
4931 "LS\n"
4932 "Returns a newline separated list of data files stored in the data directory "
4933 "@file{HOMEDIR/data} (@pxref{Invoking}) with a data response."
4936 new_command("RESET", 1, 1, 0, NULL, _(
4937 "RESET\n"
4938 "Closes the currently opened file but keeps any previously set client options "
4939 "(@pxref{OPTION})."
4942 new_command("NOP", 1, 1, 0, NULL, _(
4943 "NOP\n"
4944 "Does nothing. Always returns successfully."
4947 /* !END-HELP-TEXT! */
4948 new_command ("CANCEL", 1, 1, 0, NULL, NULL);
4949 new_command ("END", 1, 1, 0, NULL, NULL);
4950 new_command ("BYE", 1, 1, 0, NULL, NULL);
4952 int i;
4953 for (i = 0; command_table[i]; i++);
4954 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4955 sort_commands);
4958 gpg_error_t
4959 register_commands (assuan_context_t ctx)
4961 int i = 0, rc;
4963 for (; command_table[i]; i++)
4965 if (!command_table[i]->handler)
4966 continue;
4968 rc = assuan_register_command (ctx, command_table[i]->name,
4969 command_table[i]->handler,
4970 command_table[i]->help);
4971 if (rc)
4972 return rc;
4975 rc = assuan_register_bye_notify (ctx, bye_notify);
4976 if (rc)
4977 return rc;
4979 rc = assuan_register_reset_notify (ctx, reset_notify);
4980 if (rc)
4981 return rc;
4983 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4984 if (rc)
4985 return rc;
4987 return assuan_register_post_cmd_notify (ctx, command_finalize);