Move some TLS stuff to tls.c.
[libpwmd.git] / src / commands.c
blob4206845d3e7f6f6840ef36e7170e8f41a0560519
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 client->opts |= OPT_LOCK_ON_OPEN;
324 return 0;
327 static gpg_error_t
328 parse_opt_inquire (void *data, void *value)
330 struct client_s *client = data;
332 (void) value;
333 client->opts |= OPT_INQUIRE;
334 return 0;
337 static gpg_error_t
338 update_checksum (struct client_s *client)
340 unsigned char *crc;
341 size_t len;
342 struct cache_data_s *cdata;
343 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
345 if (rc)
346 return rc;
348 xfree (client->crc);
349 client->crc = crc;
350 cdata = cache_get_data (client->filename);
351 if (cdata)
353 xfree (cdata->crc);
354 cdata->crc = xmalloc (len);
355 memcpy (cdata->crc, crc, len);
358 return 0;
361 static gpg_error_t
362 validate_checksum (struct client_s *client, const char *filename,
363 struct cache_data_s *cdata, unsigned char **r_crc,
364 size_t *r_crclen)
366 unsigned char *crc;
367 size_t len;
368 gpg_error_t rc;
369 int n = 0;
371 if (cdata && !cdata->crc)
372 return GPG_ERR_CHECKSUM;
374 rc = get_checksum (filename, &crc, &len);
375 if (rc)
376 return rc;
378 if (cdata)
379 n = memcmp (cdata->crc, crc, len);
380 else if (client->crc)
381 n = memcmp (client->crc, crc, len);
383 if (!n && r_crc)
385 *r_crc = crc;
386 *r_crclen = len;
388 else
389 xfree (crc);
391 return n ? GPG_ERR_CHECKSUM : 0;
394 static gpg_error_t
395 open_command (assuan_context_t ctx, char *line)
397 gpg_error_t rc;
398 struct client_s *client = assuan_get_pointer (ctx);
399 char **req, *filename;
400 int same_file = 0;
401 assuan_peercred_t peer;
402 struct cache_data_s *cdata = NULL;
403 int cached = 0;
404 unsigned char *crc = NULL;
405 size_t crclen = 0;
406 struct argv_s *args[] = {
407 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
408 NULL
411 rc = parse_options (&line, args, client, 1);
412 if (rc)
413 return send_error (ctx, rc);
415 req = str_split (line, " ", 2);
416 if (!req)
417 return send_error (ctx, GPG_ERR_SYNTAX);
419 rc = do_validate_peer (ctx, req[0], &peer);
420 if (rc == GPG_ERR_FORBIDDEN)
421 rc = peer_is_invoker (client);
423 if (rc)
425 strv_free (req);
426 return send_error (ctx, rc);
429 filename = req[0];
430 if (!valid_filename (filename))
432 strv_free (req);
433 return send_error (ctx, GPG_ERR_INV_VALUE);
436 pthread_cleanup_push ((void *)req_free, req);
437 /* This client may have locked a different file with ISCACHED --lock than
438 * the current filename. This will remove that lock. */
439 same_file = client->filename && !strcmp (filename, client->filename);
440 if (client->flags & FLAG_OPEN ||
441 (client->flags & FLAG_HAS_LOCK && !same_file))
443 uint32_t opts = client->opts;
444 uint32_t flags = client->flags;
446 if (same_file)
447 client->flags |= FLAG_KEEP_LOCK;
448 else if (client->flags & FLAG_NEW)
449 cache_clear (NULL, client->filename, 0);
451 reset_client (client);
452 client->opts = opts;
453 client->flags |= flags;
454 client->flags &= ~(FLAG_LOCK_CMD);
455 if (!same_file)
456 client->flags &= ~(FLAG_HAS_LOCK | FLAG_NEW);
459 client->filename = str_dup (filename);
460 if (!client->filename)
462 strv_free (req);
463 return send_error(ctx, GPG_ERR_ENOMEM);
466 /* Need to lock the mutex here because file_modified() cannot without
467 * knowing the filename. */
468 rc = lock_file_mutex (client, 1);
469 if (rc)
470 client->flags &= ~FLAG_OPEN;
472 if (!rc)
474 rc = lock_flock (ctx, filename, LOCK_SH, &client->flock_fd);
475 if (gpg_err_code (rc) == GPG_ERR_ENOENT)
476 rc = 0;
479 if (!rc)
481 struct stat st;
482 char *keyfile = config_get_string (client->filename, "passphrase_file");
484 rc = crypto_init (&client->crypto, client->ctx, client->filename,
485 client->flags & FLAG_NO_PINENTRY, keyfile);
486 if (rc)
487 xfree (keyfile);
489 if (!rc && stat (client->filename, &st) == -1)
490 rc = gpg_error_from_errno (errno);
491 else if (!rc && !S_ISREG (st.st_mode)) // to match LS output
492 rc = gpg_error_from_errno (ENOANO);
495 if (!rc || gpg_err_code (rc) == GPG_ERR_ENOENT)
497 cdata = cache_get_data (client->filename);
499 if (rc) // new file
501 rc = 0;
502 client->flags |= FLAG_NEW;
503 // data file disappeared. clear the cache entry.
504 cache_clear (NULL, client->filename, 1);
505 cdata = NULL;
507 else if (cdata && cdata->doc) // cached document
509 int reload = 0;
510 int defer = 0;
512 if (!rc && !(client->flags & FLAG_NEW))
514 rc = validate_checksum (client, client->filename, cdata, &crc,
515 &crclen);
516 if (rc == GPG_ERR_CHECKSUM)
518 rc = 0;
519 reload = 1;
523 if (!rc)
525 rc = cache_iscached (client->filename, &defer, 0, 0);
526 if ((!rc || rc == GPG_ERR_ENOENT) && defer)
528 rc = 0;
529 reload = 2;
533 if (!rc && reload)
535 if (reload == 1)
536 log_write ("%s: %s", client->filename,
537 pwmd_strerror (GPG_ERR_CHECKSUM));
538 cache_clear (NULL, client->filename, 1);
539 cdata = NULL;
540 rc = crypto_decrypt (client, client->crypto);
542 #ifdef WITH_GNUTLS
543 else if (!rc)
545 if (client->thd->remote
546 && config_get_boolean (client->filename, "tcp_require_key")
547 && !(client->flags & FLAG_NEW))
548 rc = crypto_try_decrypt (client,
549 (client->flags & FLAG_NO_PINENTRY));
551 #endif
553 if (!rc && cdata)
555 client->crypto->plaintext = cdata->doc;
556 client->crypto->plaintext_size = cdata->size;
557 rc = cache_decrypt (client->crypto);
558 if (!rc)
560 cdata->doc = NULL;
561 cdata->size = 0;
562 strv_free (client->crypto->pubkey);
563 strv_free (client->crypto->sigkey);
564 client->crypto->pubkey = strv_dup (cdata->pubkey);
565 client->crypto->sigkey = strv_dup (cdata->sigkey);
566 cached = 1;
568 else
570 client->crypto->plaintext = NULL;
571 client->crypto->plaintext_size = 0;
575 else // existing file
577 cached = cdata != NULL;
578 rc = crypto_decrypt (client, client->crypto);
582 if (!rc)
584 rc = parse_xml (ctx, client->flags & FLAG_NEW);
585 if (!rc)
587 rc = cache_encrypt (client->crypto);
588 if (rc)
589 cache_clear (NULL, client->filename, 1);
590 else
592 int timeout = config_get_integer (client->filename,
593 "cache_timeout");
595 cache_free_data_once (cdata);
596 cdata = xcalloc (1, sizeof (struct cache_data_s));
597 cdata->doc = client->crypto->plaintext;
598 cdata->size = client->crypto->plaintext_size;
599 cdata->pubkey = strv_dup(client->crypto->pubkey);
600 cdata->sigkey = strv_dup(client->crypto->sigkey);
601 client->crypto->plaintext = NULL;
602 client->crypto->plaintext_size = 0;
604 if (cached) // wont increment the refcount
606 /* Prevent using another FD to update the checksum for a
607 * cached data file. The validity has already been
608 * verified. */
609 xfree (client->crc);
610 client->crc = xmalloc (crclen);
611 memcpy (client->crc, crc, crclen);
612 xfree (cdata->crc);
613 cdata->crc = xmalloc (crclen);
614 memcpy (cdata->crc, crc, crclen);
616 rc = cache_set_data (client->filename, cdata);
617 /* The cache entry may have been removed and cache_set_data()
618 * already sent STATUS_CACHE. */
619 if (!cache_iscached (client->filename, NULL, 0, 0))
620 rc = send_status (ctx, STATUS_CACHE, NULL);
622 else
623 rc = cache_add_file (client->filename, cdata, timeout);
628 pthread_cleanup_pop (1);
629 xfree (crc);
631 if (!rc && !(client->flags & FLAG_NEW) && !cached)
632 rc = update_checksum (client);
634 if (!rc)
636 client->flags |= FLAG_OPEN;
638 if (client->flags & FLAG_NEW)
639 rc = send_status (ctx, STATUS_NEWFILE, NULL);
641 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
642 rc = do_lock (client, 0);
645 if (rc)
647 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
649 if (client->flags & FLAG_NEW)
650 cache_clear (NULL, client->filename, 1);
652 crypto_free (client->crypto);
653 client->crypto = NULL;
654 client->flags &= ~FLAG_OPEN;
656 else
657 crypto_free_non_keys (client->crypto);
659 return send_error (ctx, rc);
662 /* If not the invoking_user or is an existing file, check that the list of user
663 * supplied key ID's are in the list of current key ID's obtained when
664 * decrypting the data file.
666 static gpg_error_t
667 permitted_to_save (struct client_s *client, const char **keys,
668 const char **value)
670 gpg_error_t rc = 0;
671 const char **v;
673 if ((client->flags & FLAG_NEW) || (client->opts & OPT_SYMMETRIC))
674 return 0;
676 rc = peer_is_invoker (client);
677 if (!rc)
678 return 0;
680 /* Empty match. */
681 if ((!value && !keys) || ((value && !*value) && (keys && !*keys)))
682 return 0;
684 for (v = value; v && *v; v++)
686 const char **k, *pv = *v;
687 int match = 0;
689 if (*pv == '0' && *(pv+1) == 'x')
690 pv += 2;
692 for (k = keys; k && *k; k++)
694 const char *pk = *k;
696 if (*pk == '0' && *(pk+1) == 'x')
697 pk += 2;
699 rc = !strcmp (pv, pk) ? 0 : GPG_ERR_FORBIDDEN;
700 if (rc)
701 rc = !strcmp (pv, *k) ? 0 : GPG_ERR_FORBIDDEN;
703 if (!rc)
705 match = 1;
706 break;
710 if (!match)
711 return GPG_ERR_FORBIDDEN;
714 return rc;
717 /* Requires that the keyid be a fingerprint in 16 byte form. */
718 static gpg_error_t
719 parse_save_opt_keyid_common (struct client_s *client, const char **list,
720 const char *value, char ***dst)
722 gpg_error_t rc = 0;
723 char **keys = NULL;
725 if (value && *value)
727 keys = str_split (value, ",", 0);
728 if (!keys)
729 return GPG_ERR_ENOMEM;
732 rc = crypto_keyid_to_16b (keys);
733 if (!rc)
734 rc = permitted_to_save (client, list, (const char **)keys);
736 if (!rc)
737 *dst = keys;
738 else
739 strv_free (keys);
741 return rc;
744 static gpg_error_t
745 parse_save_opt_keyid (void *data, void *value)
747 struct client_s *client = data;
748 const char *str = value;
749 char **dst = NULL;
750 gpg_error_t rc;
752 rc = parse_save_opt_keyid_common (client,
753 (const char **)client->crypto->pubkey,
754 str, &dst);
755 if (rc)
756 return rc;
758 client->crypto->save.pubkey = dst;
759 return 0;
762 static gpg_error_t
763 parse_save_opt_sign_keyid (void *data, void *value)
765 struct client_s *client = data;
766 const char *str = value;
767 char **dst = NULL;
768 gpg_error_t rc;
770 rc = parse_save_opt_keyid_common (client,
771 (const char **)client->crypto->sigkey,
772 str, &dst);
773 if (rc)
774 return rc;
776 if (!dst)
777 client->opts |= OPT_NO_SIGNER;
779 client->crypto->save.sigkey = dst;
780 return 0;
783 static gpg_error_t
784 parse_save_opt_inquire (struct client_s *client, uint32_t opt)
786 if (opt == OPT_INQUIRE && !(client->flags & FLAG_NEW))
788 gpg_error_t rc = peer_is_invoker (client);
790 if (rc)
791 return rc;
794 client->opts |= opt;
795 return 0;
798 static gpg_error_t
799 parse_save_opt_inquire_keyparam (void *data, void *value)
801 return parse_save_opt_inquire (data, OPT_INQUIRE);
804 static gpg_error_t
805 parse_save_opt_inquire_keyid (void *data, void *value)
807 return parse_save_opt_inquire (data, OPT_INQUIRE_KEYID);
810 static gpg_error_t
811 parse_save_opt_inquire_sign_keyid (void *data, void *value)
813 return parse_save_opt_inquire (data, OPT_INQUIRE_SIGN_KEYID);
816 static gpg_error_t
817 parse_save_opt_symmetric (void *data, void *value)
819 struct client_s *client = data;
821 client->opts |= OPT_SYMMETRIC;
822 return 0;
825 /* Tests that the keys in new_keys are also in old_keys. */
826 static gpg_error_t
827 compare_keys (char **new_keys, char **old_keys)
829 char **o;
831 if (!old_keys || !*old_keys)
832 return 0;
834 crypto_keyid_to_16b (new_keys);
836 for (o = old_keys; *o; o++)
838 char **n;
840 for (n = new_keys; *n; n++)
842 if (!strcmp (*n, *o))
843 break;
846 if (!*n)
847 return GPG_ERR_NOT_FOUND;
850 return 0;
853 static gpg_error_t
854 inquire_keyid (struct client_s *client, uint32_t opt)
856 gpg_error_t rc;
857 unsigned char *result = NULL;
858 size_t len;
859 char *s;
860 char ***orig;
861 char ***save;
863 if (opt == OPT_INQUIRE_KEYID)
865 s = "INQUIRE_KEYID";
866 orig = &client->crypto->pubkey;
867 save = &client->crypto->save.pubkey;
869 else
871 s = "INQUIRE_SIGN_KEYID";
872 orig = &client->crypto->sigkey;
873 save = &client->crypto->save.sigkey;
876 rc = assuan_inquire (client->ctx, s, &result, &len, 0);
877 if (!rc)
879 char **dst = NULL;
881 rc = parse_save_opt_keyid_common (client, (const char **)*orig,
882 (char *)result, &dst);
883 if (!rc)
885 if (!dst && opt == OPT_INQUIRE_SIGN_KEYID
886 && client->opts & OPT_SYMMETRIC)
887 client->opts |= OPT_NO_SIGNER;
889 *save = dst;
893 xfree (result);
894 return rc;
897 /* The caching test of gpg-agent is both a blessing and a curse. When a key
898 * lookup in its cache fails it tries the last successful key to be unlocked.
899 * This prevents pwmd from successfully clearing a signing key since the
900 * previous key, which more than likely belongs to the same keypair as the
901 * encryption/decryption key, will have the passphrase cached and therefore the
902 * signing key also cached. So we need to clear both the signing and encryption
903 * keys to get the effect of requiring a passphrase when generating a new
904 * keypair for an existing data file, or when the "require_save_key"
905 * configuration parameter is set. The "require_save_key" parameter is mostly a
906 * failsafe of the gpg-agent option --ignore-cache-for-signing since
907 * some/most/all users may fail to set it.
909 static gpg_error_t
910 save_command (assuan_context_t ctx, char *line)
912 struct client_s *client = assuan_get_pointer (ctx);
913 struct cache_data_s *cdata = NULL;
914 gpg_error_t rc = 0, cached = 0;
915 int defer = 0;
916 struct stat st;
917 struct argv_s *args[] = {
918 &(struct argv_s) {"keyid", OPTION_TYPE_ARG, parse_save_opt_keyid},
919 &(struct argv_s) {"sign-keyid", OPTION_TYPE_OPTARG,
920 parse_save_opt_sign_keyid},
921 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
922 parse_save_opt_inquire_keyparam },
923 &(struct argv_s) {"inquire-keyid", OPTION_TYPE_NOARG,
924 parse_save_opt_inquire_keyid },
925 &(struct argv_s) {"inquire-sign-keyid", OPTION_TYPE_NOARG,
926 parse_save_opt_inquire_sign_keyid },
927 &(struct argv_s) {"symmetric", OPTION_TYPE_NOARG,
928 parse_save_opt_symmetric },
929 NULL
932 crypto_free_save (&client->crypto->save);
934 if (!(client->flags & FLAG_NEW))
936 rc = crypto_is_symmetric (client->filename);
937 if (!rc || rc == GPG_ERR_BAD_DATA)
939 if (!rc)
940 client->opts |= OPT_SYMMETRIC;
942 rc = 0; // PKI
946 if (rc)
947 return send_error (ctx, rc);
949 rc = parse_options (&line, args, client, 0);
950 if (rc)
951 return send_error (ctx, rc);
953 if (config_get_boolean (client->filename, "require_save_key"))
954 client->opts |= OPT_ASK;
956 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
957 return send_error (ctx, gpg_error_from_errno (errno));
959 if (errno != ENOENT && !S_ISREG (st.st_mode))
961 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
962 return send_error (ctx, GPG_ERR_ENOANO);
965 if (!rc)
966 cached = rc = cache_iscached (client->filename, &defer, 0, 1);
968 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
969 rc = 0;
970 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
971 rc = 0;
973 if (rc)
974 return send_error (ctx, rc);
976 /* Specifying both a recipient and symmetric encryption is an error. */
977 if (client->opts & OPT_INQUIRE_KEYID && client->opts & OPT_SYMMETRIC)
979 return send_error (ctx, GPG_ERR_CONFLICT);
981 else if (client->opts & OPT_INQUIRE && client->opts & OPT_SYMMETRIC)
983 return send_error (ctx, GPG_ERR_CONFLICT);
985 /* Existing file with a recipient and wanting symmetric is an error. */
986 else if (client->opts & OPT_SYMMETRIC && client->crypto->save.pubkey)
988 return send_error (ctx, GPG_ERR_CONFLICT);
990 else if (((client->opts & OPT_INQUIRE_KEYID)
991 || (client->opts & OPT_INQUIRE_SIGN_KEYID)))
993 if (client->opts & OPT_INQUIRE)
994 return send_error (ctx, GPG_ERR_CONFLICT);
996 if (client->opts & OPT_INQUIRE_KEYID)
997 rc = inquire_keyid (client, OPT_INQUIRE_KEYID);
999 if (!rc && (client->opts & OPT_INQUIRE_SIGN_KEYID))
1000 rc = inquire_keyid (client, OPT_INQUIRE_SIGN_KEYID);
1002 else if ((client->crypto->save.pubkey || client->crypto->save.sigkey)
1003 && client->opts & OPT_INQUIRE)
1004 return send_error (ctx, GPG_ERR_CONFLICT);
1006 if (!rc)
1008 client->crypto->keyfile = config_get_string (client->filename,
1009 "passphrase_file");
1010 rc = crypto_init_ctx (client->crypto, (client->flags & FLAG_NO_PINENTRY),
1011 client->crypto->keyfile);
1014 if (!rc && (client->opts & OPT_INQUIRE))
1016 /* Require a passphrase when generating a new key pair for an existing
1017 * file.
1019 if (!(client->flags & FLAG_NEW))
1021 rc = cache_clear_agent_keys (client->filename, 1, 1);
1022 if (!rc)
1024 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
1025 client->opts &= ~OPT_ASK;
1029 if (!rc)
1031 unsigned char *params = NULL;
1032 size_t len;
1034 rc = assuan_inquire (client->ctx, "KEYPARAM", &params, &len, 0);
1035 if (!rc)
1037 client->crypto->flags |= CRYPTO_FLAG_NEWFILE;
1038 pthread_cleanup_push ((void *)xfree, params);
1039 rc = crypto_genkey (client, client->crypto, params);
1040 pthread_cleanup_pop (1);
1044 else if (!rc && (client->flags & FLAG_NEW))
1046 if (!client->crypto->save.pubkey && !client->crypto->save.sigkey
1047 && !(client->opts & OPT_SYMMETRIC))
1049 char *params = crypto_default_key_params ();
1051 if (!params)
1052 rc = GPG_ERR_ENOMEM;
1054 if (!rc)
1056 client->crypto->flags |= CRYPTO_FLAG_NEWFILE;
1057 pthread_cleanup_push ((void *)xfree, params);
1058 rc = crypto_genkey (client, client->crypto,
1059 (unsigned char *)params);
1060 pthread_cleanup_pop (1);
1063 else
1065 if (!client->crypto->save.pubkey && client->crypto->save.sigkey
1066 && !(client->opts & OPT_SYMMETRIC))
1067 client->crypto->save.pubkey = strv_dup (client->crypto->save.sigkey);
1069 if (client->crypto->save.pubkey && !client->crypto->save.sigkey)
1070 client->crypto->save.sigkey = strv_dup (client->crypto->save.pubkey);
1073 else if (!rc)
1075 cdata = cache_get_data (client->filename);
1076 if (cdata)
1078 if (client->crypto->save.pubkey)
1079 rc = compare_keys (client->crypto->save.pubkey, cdata->pubkey);
1081 /* Always allow a signer for symmetric data files. */
1082 if (!rc && client->crypto->save.sigkey
1083 && !(client->opts & OPT_SYMMETRIC))
1084 rc = compare_keys (client->crypto->save.sigkey, cdata->sigkey);
1086 /* Prevent saving to a recipient who is not in the original recipient
1087 * list without a passphrase. */
1088 if (rc || (client->crypto->sigkey && client->opts & OPT_NO_SIGNER))
1089 client->opts |= OPT_ASK;
1091 if (rc == GPG_ERR_NOT_FOUND)
1093 rc = peer_is_invoker (client);
1094 if (rc == GPG_ERR_EACCES)
1095 rc = GPG_ERR_FORBIDDEN;
1098 if (!client->crypto->save.pubkey
1099 && !(client->opts & OPT_SYMMETRIC))
1100 client->crypto->save.pubkey = strv_dup (cdata->pubkey);
1102 if (!rc && !client->crypto->save.sigkey && cdata->sigkey
1103 && !(client->opts & OPT_NO_SIGNER))
1104 client->crypto->save.sigkey = strv_dup (cdata->sigkey);
1105 else if (!rc && !client->crypto->save.sigkey
1106 && (client->opts & OPT_NO_SIGNER)
1107 && !(client->opts & OPT_SYMMETRIC))
1108 rc = GPG_ERR_NO_SIGNATURE_SCHEME;
1110 else
1112 client->crypto->save.pubkey = strv_dup (client->crypto->pubkey);
1113 client->crypto->save.sigkey = strv_dup (client->crypto->sigkey);
1117 if (!rc && !(client->flags & FLAG_NEW))
1119 /* Fixes {NEW_,SIGN_}PASSPHRASE inquire keywords. See passphrase_cb(). */
1120 if (client->opts & OPT_SYMMETRIC)
1121 client->crypto->flags |= CRYPTO_FLAG_PASSWD;
1123 if (client->opts & OPT_ASK)
1125 rc = cache_clear_agent_keys (client->filename, 1, 1);
1126 if (!rc)
1127 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
1131 if (!rc && client->opts & OPT_SYMMETRIC)
1132 client->crypto->flags |= CRYPTO_FLAG_PASSWD_NEW;
1134 if (!rc)
1135 rc = xml_update_element_mtime (client, xmlDocGetRootElement (client->doc));
1137 if (!rc)
1139 int size;
1141 xmlDocDumpFormatMemory (client->doc, &client->crypto->plaintext, &size,
1143 if (size > 0)
1144 client->crypto->plaintext_size = (size_t) size;
1145 else
1146 rc = GPG_ERR_ENOMEM;
1148 if (!rc)
1150 client->crypto->flags |= (client->opts & OPT_SYMMETRIC) ? CRYPTO_FLAG_SYMMETRIC : 0;
1151 client->crypto->flags |= (client->flags & FLAG_NEW) ? CRYPTO_FLAG_NEWFILE : 0;
1152 client->crypto->flags |= !(client->opts & OPT_SYMMETRIC) ? CRYPTO_FLAG_PASSWD_SIGN : 0;
1153 rc = crypto_encrypt (client, client->crypto);
1154 client->crypto->flags &= ~CRYPTO_FLAG_SYMMETRIC;
1157 if (!rc)
1159 rc = crypto_write_file (client->crypto);
1160 if (!rc)
1162 if (!cached) // no error
1163 cdata = cache_get_data (client->filename);
1166 if (!rc)
1168 rc = cache_encrypt (client->crypto);
1169 if (rc)
1171 cache_clear (NULL, client->filename, 1);
1172 strv_free (client->crypto->pubkey);
1173 client->crypto->pubkey = strv_dup (client->crypto->save.pubkey);
1174 strv_free (client->crypto->sigkey);
1175 client->crypto->sigkey = strv_dup (client->crypto->save.sigkey);
1176 client->flags &= ~(FLAG_NEW);
1180 if (!rc)
1182 int timeout = config_get_integer (client->filename,
1183 "cache_timeout");
1185 cache_free_data_once (cdata);
1186 cdata = xcalloc (1, sizeof (struct cache_data_s));
1187 cdata->doc = client->crypto->plaintext;
1188 client->crypto->plaintext = NULL;
1189 cdata->size = client->crypto->plaintext_size;
1190 client->crypto->plaintext_size = 0;
1191 cdata->pubkey = client->crypto->save.pubkey;
1192 client->crypto->save.pubkey = NULL;
1193 cdata->sigkey = client->crypto->save.sigkey;
1194 client->crypto->save.sigkey = NULL;
1196 /* Update in case the cache entry expires the next SAVE may not
1197 * have any known keys. */
1198 strv_free (client->crypto->pubkey);
1199 client->crypto->pubkey = strv_dup (cdata->pubkey);
1200 strv_free (client->crypto->sigkey);
1201 client->crypto->sigkey = strv_dup (cdata->sigkey);
1203 if (!cached) // no error and wont increment refcount
1204 rc = cache_set_data (client->filename, cdata);
1205 else
1206 rc = cache_add_file (client->filename, cdata, timeout);
1208 if (!rc && (cached || (client->flags & FLAG_NEW)))
1209 send_status_all (STATUS_CACHE, NULL);
1211 if (!rc)
1213 rc = update_checksum (client);
1214 client->flags &= ~(FLAG_NEW);
1220 crypto_free_non_keys (client->crypto);
1221 return send_error (ctx, rc);
1224 static gpg_error_t
1225 do_delete (assuan_context_t ctx, char *line)
1227 struct client_s *client = assuan_get_pointer (ctx);
1228 struct xml_request_s *req;
1229 xmlNodePtr n;
1230 gpg_error_t rc;
1232 rc = xml_new_request (client, line, XML_CMD_DELETE, &req);
1233 if (rc)
1234 return rc;
1236 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1237 xml_free_request (req);
1238 if (rc)
1239 return rc;
1241 rc = xml_is_element_owner (client, n);
1242 if (!rc)
1243 rc = xml_unlink_node (client, n);
1245 return rc;
1248 static gpg_error_t
1249 delete_command (assuan_context_t ctx, char *line)
1251 struct client_s *client = assuan_get_pointer (ctx);
1252 gpg_error_t rc;
1253 struct argv_s *args[] = {
1254 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1255 NULL
1258 rc = parse_options (&line, args, client, 1);
1259 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1260 rc = GPG_ERR_SYNTAX;
1261 if (rc)
1262 return send_error (ctx, rc);
1264 if (client->opts & OPT_INQUIRE)
1266 unsigned char *result;
1267 size_t len;
1269 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1270 if (rc)
1271 return send_error (ctx, rc);
1273 pthread_cleanup_push ((void *)xfree, result);
1274 rc = do_delete (ctx, (char *)result);
1275 pthread_cleanup_pop (1);
1277 else
1278 rc = do_delete (ctx, line);
1280 return send_error (ctx, rc);
1283 static gpg_error_t
1284 update_element_expirey (struct client_s *client, xmlNodePtr n)
1286 gpg_error_t rc = 0;
1287 xmlChar *expire, *incr;
1288 char *p;
1289 time_t e, e_orig, i;
1290 time_t now = time (NULL);
1292 expire = xml_attribute_value (n, (xmlChar *)"expire");
1293 if (!expire)
1294 return 0;
1296 errno = 0;
1297 e = strtoul ((char *)expire, &p, 10);
1298 if (errno || *p || e == ULONG_MAX || *expire == '-')
1300 xmlFree (expire);
1301 rc = GPG_ERR_ERANGE;
1302 goto fail;
1305 xmlFree (expire);
1306 incr = xml_attribute_value (n, (xmlChar *)"expire_increment");
1307 if (!incr)
1308 return 0;
1310 i = strtoul ((char *)incr, &p, 10);
1311 if (errno || *p || i == ULONG_MAX || *incr == '-')
1313 xmlFree (incr);
1314 rc = GPG_ERR_ERANGE;
1315 goto fail;
1318 xmlFree (incr);
1319 e_orig = e;
1320 e = now + i;
1321 p = str_asprintf ("%lu", e);
1322 if (!p)
1324 rc = GPG_ERR_ENOMEM;
1325 goto fail;
1328 rc = xml_add_attribute (client, n, "expire", p);
1329 xfree (p);
1330 if (!rc)
1331 rc = send_status (client->ctx, STATUS_EXPIRE, "%lu %lu", e_orig, e);
1333 fail:
1334 return rc;
1337 static gpg_error_t
1338 store_command (assuan_context_t ctx, char *line)
1340 struct client_s *client = assuan_get_pointer (ctx);
1341 gpg_error_t rc;
1342 size_t len;
1343 unsigned char *result;
1344 xmlNodePtr n, parent;
1345 int has_content;
1346 char *content = NULL;
1347 struct xml_request_s *req;
1349 if (line && *line)
1350 return send_error (ctx, GPG_ERR_SYNTAX);
1352 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1353 if (rc)
1354 return send_error (ctx, rc);
1356 rc = xml_new_request (client, (char *)result, XML_CMD_STORE, &req);
1357 xfree (result);
1358 if (rc)
1359 return send_error (ctx, rc);
1361 /* Prevent passing the element content around to save some memory. */
1362 len = strv_length (req->args);
1363 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1364 if (*(req->args+1) && !xml_valid_element_path (req->args, has_content))
1366 xml_free_request (req);
1367 return send_error (ctx, GPG_ERR_INV_VALUE);
1370 if (has_content || !*req->args[len-1])
1372 has_content = 1;
1373 content = req->args[len-1];
1374 req->args[len-1] = NULL;
1377 if (strv_length (req->args) > 1)
1379 rc = xml_check_recursion (client, req);
1380 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
1381 goto fail;
1384 parent = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1386 if (!rc && len > 1)
1388 rc = xml_is_element_owner (client, parent);
1389 if (!rc)
1391 n = xml_find_text_node (parent->children);
1392 if (n)
1393 xmlNodeSetContent (n, (xmlChar *) content);
1394 else
1395 xmlNodeAddContent (parent, (xmlChar *) content);
1397 (void)xml_update_element_mtime (client, parent);
1398 (void)update_element_expirey (client, parent);
1402 fail:
1403 xfree (content);
1404 xml_free_request (req);
1405 return send_error (ctx, rc);
1408 static gpg_error_t
1409 xfer_data (assuan_context_t ctx, const char *line, int total)
1411 struct client_s *client = assuan_get_pointer (ctx);
1412 int to_send;
1413 int sent = 0;
1414 gpg_error_t rc = 0;
1415 int progress = config_get_integer ("global", "xfer_progress");
1416 int flush = 0;
1418 if (!(client->flags & FLAG_LOCK_CMD))
1419 rc = unlock_file_mutex (client, 0);
1420 if (rc && rc != GPG_ERR_NOT_LOCKED)
1421 return rc;
1423 progress =
1424 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1425 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1426 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1428 if (rc)
1429 return rc;
1431 again:
1434 if (sent + to_send > total)
1435 to_send = total - sent;
1437 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1438 flush ? 0 : to_send);
1439 if (!rc)
1441 sent += flush ? 0 : to_send;
1443 if ((progress && !(sent % progress) && sent != total) ||
1444 (sent == total && flush))
1445 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1447 if (!flush && !rc && sent == total)
1449 flush = 1;
1450 goto again;
1454 while (!rc && sent < total);
1456 return rc;
1459 static gpg_error_t
1460 do_get (assuan_context_t ctx, char *line)
1462 struct client_s *client = assuan_get_pointer (ctx);
1463 gpg_error_t rc;
1464 struct xml_request_s *req;
1465 xmlNodePtr n;
1467 rc = xml_new_request (client, line, XML_CMD_NONE, &req);
1468 if (rc)
1469 return rc;
1471 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1472 if (!rc)
1474 if (n && n->children)
1476 xmlNodePtr tmp = n;
1478 n = xml_find_text_node (n->children);
1479 if (!n || !n->content || !*n->content)
1480 rc = GPG_ERR_NO_DATA;
1481 else
1483 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1484 if (!rc)
1486 xmlChar *expire = xml_attribute_value (tmp,
1487 (xmlChar *)"expire");
1488 if (expire)
1490 time_t now = time (NULL);
1491 time_t e;
1492 char *p;
1494 errno = 0;
1495 e = strtoul ((char *)expire, &p, 10);
1496 if (errno || *p || e == ULONG_MAX || *expire == '-')
1497 log_write (_("invalid expire attribute value: %s"),
1498 expire);
1499 else if (now >= e)
1500 rc = send_status (ctx, STATUS_EXPIRE, "%lu 0", e);
1502 xmlFree (expire);
1507 else
1508 rc = GPG_ERR_NO_DATA;
1511 xml_free_request (req);
1512 return rc;
1515 static gpg_error_t
1516 get_command (assuan_context_t ctx, char *line)
1518 struct client_s *client = assuan_get_pointer (ctx);
1519 gpg_error_t rc;
1520 struct argv_s *args[] = {
1521 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1522 NULL
1525 rc = parse_options (&line, args, client, 1);
1526 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1527 rc = GPG_ERR_SYNTAX;
1528 if (rc)
1529 return send_error (ctx, rc);
1531 if (client->opts & OPT_INQUIRE)
1533 unsigned char *result;
1534 size_t len;
1536 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1537 if (rc)
1538 return send_error (ctx, rc);
1540 pthread_cleanup_push ((void *)xfree, result);
1541 rc = do_get (ctx, (char *)result);
1542 pthread_cleanup_pop (1);
1544 else
1545 rc = do_get (ctx, line);
1547 return send_error (ctx, rc);
1550 static void list_command_free1 (void *arg);
1551 static gpg_error_t
1552 realpath_command (assuan_context_t ctx, char *line)
1554 gpg_error_t rc;
1555 char **realpath = NULL;
1556 struct client_s *client = assuan_get_pointer (ctx);
1557 struct argv_s *args[] = {
1558 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1559 NULL
1562 rc = parse_options (&line, args, client, 1);
1563 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1564 rc = GPG_ERR_SYNTAX;
1565 if (rc)
1566 return send_error (ctx, rc);
1568 if (client->opts & OPT_INQUIRE)
1570 unsigned char *result;
1571 size_t len;
1573 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1574 if (rc)
1575 return send_error (ctx, rc);
1577 pthread_cleanup_push ((void *)xfree, result);
1578 (void)xml_resolve_path (client, client->doc, result, &realpath, &rc);
1579 pthread_cleanup_pop (1);
1581 else
1582 (void)xml_resolve_path (client, client->doc, (unsigned char *)line,
1583 &realpath, &rc);
1585 if (!rc)
1587 char *tmp = strv_join ("\t", realpath);
1589 strv_free (realpath);
1590 if (!tmp)
1591 return send_error (ctx, GPG_ERR_ENOMEM);
1593 pthread_cleanup_push ((void *)xfree, tmp);
1594 rc = xfer_data (ctx, tmp, strlen (tmp));
1595 pthread_cleanup_pop (1);
1598 return send_error (ctx, rc);
1601 static void
1602 list_command_free1 (void *arg)
1604 if (arg)
1605 string_free ((struct string_s *) arg, 1);
1608 static gpg_error_t
1609 parse_list_opt_recurse (void *data, void *value)
1611 struct client_s *client = data;
1613 client->opts |= OPT_LIST_RECURSE;
1614 return 0;
1617 static gpg_error_t
1618 parse_opt_verbose (void *data, void *value)
1620 struct client_s *client = data;
1622 client->opts |= OPT_VERBOSE;
1623 return 0;
1626 static gpg_error_t
1627 list_path_once (struct client_s *client, char *line,
1628 struct element_list_s *elements, struct string_s *result)
1630 gpg_error_t rc;
1632 rc = xml_create_path_list (client, client->doc, NULL, elements, line, 0);
1633 if (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES)
1634 rc = 0;
1636 if (!rc)
1638 int total = slist_length (elements->list);
1640 if (total)
1642 int i;
1644 for (i = 0; i < total; i++)
1646 char *tmp = slist_nth_data (elements->list, i);
1648 string_append_printf (result, "%s%s", tmp,
1649 i + 1 == total ? "" : "\n");
1652 else
1653 rc = GPG_ERR_NO_DATA;
1656 return rc;
1659 static gpg_error_t
1660 do_list (assuan_context_t ctx, char *line)
1662 struct client_s *client = assuan_get_pointer (ctx);
1663 gpg_error_t rc = GPG_ERR_NO_DATA;
1664 struct element_list_s *elements = NULL;
1666 if (!line || !*line)
1668 struct string_s *str = string_new (NULL);
1669 xmlNodePtr n;
1671 if (!str)
1672 return GPG_ERR_ENOMEM;
1674 pthread_cleanup_push ((void *)list_command_free1, str);
1675 n = xmlDocGetRootElement (client->doc);
1676 for (n = n->children; n; n = n->next)
1678 xmlChar *name;
1680 if (n->type != XML_ELEMENT_NODE)
1681 continue;
1683 name = xmlGetProp (n, (xmlChar *)"_name");
1684 if (!name)
1686 rc = GPG_ERR_ENOMEM;
1687 break;
1690 elements = xcalloc (1, sizeof (struct element_list_s));
1691 if (!elements)
1693 xmlFree (name);
1694 rc = GPG_ERR_ENOMEM;
1695 break;
1698 elements->prefix = string_new (NULL);
1699 if (!elements->prefix)
1701 xmlFree (name);
1702 xml_free_element_list (elements);
1703 rc = GPG_ERR_ENOMEM;
1704 break;
1707 elements->recurse = client->opts & OPT_LIST_RECURSE;
1708 elements->root_only = !elements->recurse;
1709 pthread_cleanup_push ((void *)xml_free_element_list, elements);
1710 rc = list_path_once (client, (char *)name, elements, str);
1711 xmlFree (name);
1712 pthread_cleanup_pop (1);
1713 if (rc)
1714 break;
1716 if (n->next)
1717 string_append (str, "\n");
1720 if (!rc)
1721 rc = xfer_data (ctx, str->str, str->len);
1723 pthread_cleanup_pop (1);
1724 return rc;
1727 elements = xcalloc (1, sizeof (struct element_list_s));
1728 if (!elements)
1729 return GPG_ERR_ENOMEM;
1731 elements->prefix = string_new (NULL);
1732 if (!elements->prefix)
1734 xml_free_element_list (elements);
1735 return GPG_ERR_ENOMEM;
1738 elements->recurse = client->opts & OPT_LIST_RECURSE;
1739 pthread_cleanup_push ((void *)xml_free_element_list, elements);
1740 struct string_s *str = string_new (NULL);
1741 pthread_cleanup_push ((void *)list_command_free1, str);
1742 rc = list_path_once (client, line, elements, str);
1743 if (!rc)
1744 rc = xfer_data (ctx, str->str, str->len);
1746 pthread_cleanup_pop (1);
1747 pthread_cleanup_pop (1);
1748 return rc;
1751 static gpg_error_t
1752 list_command (assuan_context_t ctx, char *line)
1754 struct client_s *client = assuan_get_pointer (ctx);
1755 gpg_error_t rc;
1756 struct argv_s *args[] = {
1757 &(struct argv_s) {"recurse", OPTION_TYPE_NOARG, parse_list_opt_recurse},
1758 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1759 /* These are deprecated but kept for backward compatibility with older
1760 * clients. */
1761 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, NULL},
1762 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG, NULL},
1763 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_recurse},
1764 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG, NULL},
1765 NULL
1768 if (disable_list_and_dump == 1)
1769 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1771 rc = parse_options (&line, args, client, 1);
1772 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
1773 rc = GPG_ERR_SYNTAX;
1774 if (rc)
1775 return send_error (ctx, rc);
1777 if (client->opts & OPT_INQUIRE)
1779 unsigned char *result;
1780 size_t len;
1782 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1783 if (rc)
1784 return send_error (ctx, rc);
1786 pthread_cleanup_push ((void *)xfree, result);
1787 rc = do_list (ctx, (char *)result);
1788 pthread_cleanup_pop (1);
1790 else
1791 rc = do_list (ctx, line);
1793 return send_error (ctx, rc);
1796 #define RESUMABLE_ERROR(rc) (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES \
1797 || rc == GPG_ERR_ELEMENT_NOT_FOUND || !rc)
1799 * args[0] - element path
1801 static gpg_error_t
1802 attribute_list (assuan_context_t ctx, char **args)
1804 struct client_s *client = assuan_get_pointer (ctx);
1805 char **attrlist = NULL;
1806 int i = 0;
1807 int target = 0;
1808 xmlAttrPtr a;
1809 xmlNodePtr n, an, r;
1810 char *line;
1811 gpg_error_t rc;
1812 struct xml_request_s *req;
1814 if (!args || !args[0] || !*args[0])
1815 return GPG_ERR_SYNTAX;
1817 rc = xml_new_request (client, args[0], XML_CMD_ATTR_LIST, &req);
1818 if (rc)
1819 return rc;
1821 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1822 xml_free_request (req);
1823 if (rc)
1824 return rc;
1826 again:
1827 for (a = n->properties; a; a = a->next)
1829 char **pa;
1831 if (target && xml_reserved_attribute ((char *)a->name))
1832 continue;
1834 pa = xrealloc (attrlist, (i + 2) * sizeof (char *));
1835 if (!pa)
1837 if (attrlist)
1838 strv_free (attrlist);
1840 log_write ("%s(%i): %s", __FILE__, __LINE__,
1841 pwmd_strerror (GPG_ERR_ENOMEM));
1842 return GPG_ERR_ENOMEM;
1845 attrlist = pa;
1846 an = a->children;
1847 attrlist[i] = str_asprintf ("%s %s", (char *) a->name, an && an->content
1848 ? (char *)an->content : "");
1850 if (!attrlist[i])
1852 strv_free (attrlist);
1853 log_write ("%s(%i): %s", __FILE__, __LINE__,
1854 pwmd_strerror (GPG_ERR_ENOMEM));
1855 return GPG_ERR_ENOMEM;
1858 attrlist[++i] = NULL;
1861 if (!attrlist)
1862 return GPG_ERR_NO_DATA;
1864 if (!target)
1866 r = xml_resolve_path (client, client->doc, (xmlChar *)args[0], NULL, &rc);
1867 if (!RESUMABLE_ERROR (rc))
1869 strv_free (attrlist);
1870 return rc;
1872 else if (!rc && r != n)
1874 target = 1;
1875 n = r;
1876 goto again;
1879 rc = 0;
1882 line = strv_join ("\n", attrlist);
1883 if (!line)
1885 log_write ("%s(%i): %s", __FILE__, __LINE__,
1886 pwmd_strerror (GPG_ERR_ENOMEM));
1887 strv_free (attrlist);
1888 return GPG_ERR_ENOMEM;
1891 pthread_cleanup_push ((void *)xfree, line);
1892 pthread_cleanup_push ((void *)req_free, attrlist);
1893 rc = xfer_data (ctx, line, strlen (line));
1894 pthread_cleanup_pop (1);
1895 pthread_cleanup_pop (1);
1896 return rc;
1900 * args[0] - attribute
1901 * args[1] - element path
1903 static gpg_error_t
1904 attribute_delete (struct client_s *client, char **args)
1906 gpg_error_t rc;
1907 xmlNodePtr n;
1909 if (!args || !args[0] || !*args[0] || !args[1] || !*args[1])
1910 return GPG_ERR_SYNTAX;
1912 if (!strcmp (args[0], "_name") || !strcmp (args[0], "target"))
1913 return GPG_ERR_INV_ATTR;
1915 if (!xml_reserved_attribute (args[0]))
1916 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
1917 else
1919 struct xml_request_s *req;
1921 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
1922 if (rc)
1923 return rc;
1925 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1926 xml_free_request (req);
1929 if (!rc)
1930 rc = xml_is_element_owner (client, n);
1932 if (!rc)
1933 rc = xml_delete_attribute (client, n, (xmlChar *) args[0]);
1935 return rc;
1939 * Creates the "target" attribute. When other commands encounter an element
1940 * with this attribute they follow the "target" after appending remaining
1941 * elements from the original element path to the target. The exception is the
1942 * ATTR command that operates on the element itself (for reserved attribute
1943 * names) and not the target.
1945 * If the source element path doesn't exist when using 'ATTR SET target', it is
1946 * created, but the destination element path must exist. This is simliar to the
1947 * ls(1) command: ln -s src dst.
1949 * args[0] - source element path
1950 * args[1] - destination element path
1952 static gpg_error_t
1953 set_target_attribute (struct client_s *client, char **args)
1955 struct xml_request_s *req = NULL;
1956 char **src, **dst;
1957 gpg_error_t rc;
1958 xmlNodePtr n;
1960 if (!args || !args[0] || !args[1])
1961 return GPG_ERR_SYNTAX;
1963 src = str_split (args[0], "\t", 0);
1964 if (!src)
1965 return GPG_ERR_SYNTAX;
1967 if (!xml_valid_element_path (src, 0))
1969 strv_free (src);
1970 return GPG_ERR_INV_VALUE;
1973 dst = str_split (args[1], "\t", 0);
1974 if (!dst)
1976 strv_free (src);
1977 return GPG_ERR_SYNTAX;
1980 // Be sure the destination element path exists. */
1981 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
1982 if (rc)
1983 goto fail;
1985 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
1986 if (rc && rc != GPG_ERR_EACCES)
1987 goto fail;
1989 xml_free_request (req);
1990 req = NULL;
1992 if (!strcmp (args[0], args[1]))
1994 rc = GPG_ERR_EEXIST;
1995 goto fail;
1998 rc = xml_new_request (client, args[0], XML_CMD_ATTR_TARGET, &req);
1999 if (rc)
2000 goto fail;
2002 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2003 if (rc && rc != GPG_ERR_EACCES)
2004 goto fail;
2006 if (!rc)
2008 rc = xml_add_attribute (client, n, "target", args[1]);
2009 if (!rc)
2010 rc = xml_unlink_node (client, n->children);
2013 fail:
2014 strv_free (src);
2015 strv_free (dst);
2016 xml_free_request (req);
2017 return rc;
2021 * args[0] - attribute
2022 * args[1] - element path
2024 static gpg_error_t
2025 attribute_get (assuan_context_t ctx, char **args)
2027 struct client_s *client = assuan_get_pointer (ctx);
2028 xmlNodePtr n;
2029 xmlChar *a;
2030 gpg_error_t rc;
2031 struct xml_request_s *req;
2033 if (!args || !args[0] || !*args[0] || !args[1] || !*args[1])
2034 return GPG_ERR_SYNTAX;
2036 if (!xml_reserved_attribute (args[0]))
2037 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
2038 else
2040 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
2041 if (rc)
2042 return rc;
2044 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2045 xml_free_request (req);
2048 if (rc)
2049 return rc;
2051 a = xmlGetProp (n, (xmlChar *) args[0]);
2052 if (!a)
2053 return GPG_ERR_NOT_FOUND;
2055 pthread_cleanup_push ((void *)xmlFree, a);
2057 if (*a)
2058 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
2059 else
2060 rc = GPG_ERR_NO_DATA;
2062 pthread_cleanup_pop (1);
2063 return rc;
2067 * args[0] - attribute
2068 * args[1] - element path
2069 * args[2] - value
2071 static gpg_error_t
2072 attribute_set (struct client_s *client, char **args)
2074 struct xml_request_s *req;
2075 gpg_error_t rc;
2076 xmlNodePtr n;
2078 if (!args || !args[0] || !args[1])
2079 return GPG_ERR_SYNTAX;
2082 * Reserved attribute names.
2084 if (!strcmp (args[0], "_name")) // Use the RENAME command instead
2085 return GPG_ERR_INV_ATTR;
2086 else if (!strcmp (args[0], "target"))
2087 return set_target_attribute (client, args + 1);
2088 else if (!xml_valid_attribute (args[0]))
2089 return GPG_ERR_INV_VALUE;
2091 if (!xml_valid_attribute_value (args[2]))
2092 return GPG_ERR_INV_VALUE;
2094 if (!xml_reserved_attribute (args[0]))
2096 n = xml_resolve_path (client, client->doc, (xmlChar *)args[1], NULL, &rc);
2097 if (!rc)
2099 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
2100 if (!rc)
2102 rc = xml_check_recursion (client, req);
2103 xml_free_request (req);
2107 else
2109 rc = xml_new_request (client, args[1], XML_CMD_ATTR, &req);
2110 if (rc)
2111 return rc;
2113 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
2114 xml_free_request (req);
2117 if (!rc)
2118 rc = xml_is_element_owner (client, n);
2120 if (!rc)
2121 rc = xml_add_attribute (client, n, args[0], args[2]);
2123 return rc;
2127 * req[0] - command
2128 * req[1] - attribute name or element path if command is LIST
2129 * req[2] - element path
2130 * req[2] - element path or value
2133 static gpg_error_t
2134 do_attr (assuan_context_t ctx, char *line)
2136 struct client_s *client = assuan_get_pointer (ctx);
2137 gpg_error_t rc = 0;
2138 char **req;
2140 req = str_split (line, " ", 4);
2141 if (!req || !req[0] || !req[1])
2143 strv_free (req);
2144 return GPG_ERR_SYNTAX;
2147 pthread_cleanup_push ((void *)req_free, req);
2149 if (strcasecmp (req[0], "SET") == 0)
2150 rc = attribute_set (client, req + 1);
2151 else if (strcasecmp (req[0], "GET") == 0)
2152 rc = attribute_get (ctx, req + 1);
2153 else if (strcasecmp (req[0], "DELETE") == 0)
2154 rc = attribute_delete (client, req + 1);
2155 else if (strcasecmp (req[0], "LIST") == 0)
2156 rc = attribute_list (ctx, req + 1);
2157 else
2158 rc = GPG_ERR_SYNTAX;
2160 client->flags &= ~(FLAG_ACL_IGNORE|FLAG_ACL_ERROR);
2161 pthread_cleanup_pop (1);
2162 return rc;
2165 static gpg_error_t
2166 attr_command (assuan_context_t ctx, char *line)
2168 struct client_s *client = assuan_get_pointer (ctx);
2169 gpg_error_t rc;
2170 struct argv_s *args[] = {
2171 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2172 NULL
2175 rc = parse_options (&line, args, client, 1);
2176 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2177 rc = GPG_ERR_SYNTAX;
2178 if (rc)
2179 return send_error (ctx, rc);
2181 if (client->opts & OPT_INQUIRE)
2183 unsigned char *result;
2184 size_t len;
2186 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2187 if (rc)
2188 return send_error (ctx, rc);
2190 pthread_cleanup_push ((void *)xfree, result);
2191 rc = do_attr (ctx, (char *)result);
2192 pthread_cleanup_pop (1);
2194 else
2195 rc = do_attr (ctx, line);
2197 return send_error (ctx, rc);
2200 static gpg_error_t
2201 parse_iscached_opt_lock (void *data, void *value)
2203 struct client_s *client = data;
2205 (void) value;
2206 client->opts |= OPT_LOCK;
2207 return 0;
2210 static gpg_error_t
2211 parse_iscached_opt_agent (void *data, void *value)
2213 struct client_s *client = data;
2215 (void) value;
2216 client->opts |= OPT_CACHE_AGENT;
2217 return 0;
2220 static gpg_error_t
2221 parse_iscached_opt_sign (void *data, void *value)
2223 struct client_s *client = data;
2225 (void) value;
2226 client->opts |= OPT_CACHE_SIGN;
2227 return 0;
2230 static gpg_error_t
2231 iscached_command (assuan_context_t ctx, char *line)
2233 struct client_s *client = assuan_get_pointer (ctx);
2234 gpg_error_t rc;
2235 int defer = 0;
2236 struct argv_s *args[] = {
2237 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2238 &(struct argv_s) {"agent", OPTION_TYPE_NOARG, parse_iscached_opt_agent},
2239 &(struct argv_s) {"sign", OPTION_TYPE_NOARG, parse_iscached_opt_sign},
2240 NULL
2243 if (!line || !*line)
2244 return send_error (ctx, GPG_ERR_SYNTAX);
2246 rc = parse_options (&line, args, client, 1);
2247 if (rc)
2248 return send_error (ctx, rc);
2249 else if (!valid_filename (line))
2250 return send_error (ctx, GPG_ERR_INV_VALUE);
2252 if (!(client->flags & FLAG_OPEN)
2253 && ((client->opts & OPT_CACHE_AGENT) || (client->opts & OPT_CACHE_SIGN)))
2254 return send_error (ctx, GPG_ERR_INV_STATE);
2256 rc = cache_iscached (line, &defer, (client->opts & OPT_CACHE_AGENT),
2257 (client->opts & OPT_CACHE_SIGN));
2258 if (!rc && defer)
2259 rc = GPG_ERR_NO_DATA;
2261 if (!rc)
2263 struct cache_data_s *cdata = cache_get_data (line);
2265 if (cdata)
2267 rc = validate_checksum (client, line, cdata, NULL, NULL);
2268 if (rc == GPG_ERR_CHECKSUM)
2269 rc = GPG_ERR_NO_DATA;
2273 if (client->opts & OPT_LOCK
2274 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2276 gpg_error_t trc = rc;
2278 if (strcmp (line, client->filename))
2279 reset_client (client);
2281 xfree (client->filename);
2282 client->filename = str_dup (line);
2283 rc = do_lock (client, 1);
2284 if (!rc)
2285 rc = trc;
2288 return send_error (ctx, rc);
2291 static gpg_error_t
2292 clearcache_command (assuan_context_t ctx, char *line)
2294 struct client_s *client = assuan_get_pointer (ctx);
2295 gpg_error_t rc = 0, all_rc = 0;
2296 int i;
2297 int t;
2298 int all = 0;
2299 struct client_thread_s *once = NULL;
2300 unsigned count;
2302 cache_lock ();
2303 pthread_cleanup_push (cache_release_mutex, NULL);
2304 count = cache_file_count ();
2305 MUTEX_LOCK (&cn_mutex);
2306 pthread_cleanup_push ((void *)release_mutex_cb, &cn_mutex);
2308 if (!line || !*line)
2309 all = 1;
2311 t = slist_length (cn_thread_list);
2313 for (i = 0; i < t; i++)
2315 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2316 assuan_peercred_t peer;
2318 if (!thd->cl)
2319 continue;
2321 /* Lock each connected clients' file mutex to prevent any other client
2322 * from accessing the cache entry (the file mutex is locked upon
2323 * command startup). The cache for the entry is not cleared if the
2324 * file mutex is locked by another client to prevent this function
2325 * from blocking. Rather, it returns an error.
2327 if (all)
2329 if (thd->cl->filename)
2331 rc = do_validate_peer (ctx, thd->cl->filename, &peer);
2332 /* The current client doesn't have permission to open the other
2333 * filename do to "access" configuration parameter in a filename
2334 * section. Since we are clearning all cache entries the error
2335 * will be returned later during cache_clear(). */
2336 if (rc)
2338 rc = 0;
2339 continue;
2342 else // Idle client without opened file?
2343 continue;
2345 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->filename, -1, 0, -1);
2346 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2348 /* The current client owns the lock. */
2349 if (pthread_equal (pthread_self (), thd->tid))
2350 rc = 0;
2351 else
2353 if (cache_iscached (thd->cl->filename, NULL, 0, 0)
2354 == GPG_ERR_NO_DATA)
2356 rc = 0;
2357 continue;
2360 /* The cache entry will be cleared when the other client
2361 * disconnects and cache_timer_thread() does its thing. */
2362 cache_defer_clear (thd->cl->filename);
2365 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2367 rc = 0;
2368 continue;
2371 if (!rc)
2373 rc = cache_clear (NULL, thd->cl->filename, 1);
2374 cache_unlock_mutex (thd->cl->filename, 0);
2377 if (rc)
2378 all_rc = rc;
2380 rc = 0;
2382 else
2384 /* A single data filename was specified. Lock only this data file
2385 * mutex and free the cache entry. */
2386 rc = do_validate_peer (ctx, line, &peer);
2387 if (rc == GPG_ERR_FORBIDDEN)
2388 rc = peer_is_invoker (client);
2390 if (rc == GPG_ERR_EACCES)
2391 rc = GPG_ERR_FORBIDDEN;
2393 if (!rc && thd->cl->filename && !strcmp (thd->cl->filename , line))
2395 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->filename, -1, 0, -1);
2396 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2398 /* The current client owns the lock. */
2399 if (pthread_equal (pthread_self (), thd->tid))
2400 rc = 0;
2403 if (!rc)
2405 once = thd;
2406 rc = cache_clear (NULL, thd->cl->filename, 1);
2407 cache_unlock_mutex (thd->cl->filename, 0);
2409 else
2410 cache_defer_clear (thd->cl->filename);
2412 /* Found a client with the opened file. The cache entry has been
2413 * either cleared (self) or defered to be cleared. */
2414 break;
2419 /* Only connected clients' cache entries have been cleared. Now clear any
2420 * remaining cache entries without clients but only if there wasn't an
2421 * error from above since this would defeat the locking check of the
2422 * remaining entries. */
2423 if (all && !all_rc && cache_file_count ())
2425 rc = cache_clear (client, NULL, 1);
2426 if (rc == GPG_ERR_EACCES)
2427 rc = GPG_ERR_FORBIDDEN;
2430 /* No clients are using the specified file. */
2431 else if (!all_rc && !rc && !once)
2432 rc = cache_clear (NULL, line, 1);
2434 /* Release the connection mutex. */
2435 pthread_cleanup_pop (1);
2437 if (!rc || (cache_file_count () && count != cache_file_count ()))
2438 send_status_all (STATUS_CACHE, NULL);
2440 /* Release the cache mutex. */
2441 pthread_cleanup_pop (1);
2443 /* One or more files were locked while clearing all cache entries. */
2444 if (all_rc)
2445 rc = all_rc;
2447 return send_error (ctx, rc);
2450 static gpg_error_t
2451 cachetimeout_command (assuan_context_t ctx, char *line)
2453 struct client_s *client = assuan_get_pointer (ctx);
2454 int timeout;
2455 char **req = str_split (line, " ", 0);
2456 char *p;
2457 gpg_error_t rc = 0;
2458 assuan_peercred_t peer;
2460 if (!req || !*req || !req[1])
2462 strv_free (req);
2463 return send_error (ctx, GPG_ERR_SYNTAX);
2466 errno = 0;
2467 timeout = (int) strtol (req[1], &p, 10);
2468 if (errno != 0 || *p || timeout < -1)
2470 strv_free (req);
2471 return send_error (ctx, GPG_ERR_SYNTAX);
2474 rc = do_validate_peer (ctx, req[0], &peer);
2475 if (rc == GPG_ERR_FORBIDDEN)
2477 rc = peer_is_invoker (client);
2478 if (rc == GPG_ERR_EACCES)
2479 rc = GPG_ERR_FORBIDDEN;
2482 if (!rc)
2484 rc = cache_set_timeout (req[0], timeout);
2485 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2487 rc = 0;
2488 MUTEX_LOCK (&rcfile_mutex);
2489 config_set_int_param (&global_config, req[0], "cache_timeout",
2490 req[1]);
2491 MUTEX_UNLOCK (&rcfile_mutex);
2495 strv_free (req);
2496 return send_error (ctx, rc);
2499 static gpg_error_t
2500 dump_command (assuan_context_t ctx, char *line)
2502 xmlChar *xml;
2503 int len;
2504 struct client_s *client = assuan_get_pointer (ctx);
2505 gpg_error_t rc;
2507 if (disable_list_and_dump == 1)
2508 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2510 if (line && *line)
2511 return send_error (ctx, GPG_ERR_SYNTAX);
2513 rc = peer_is_invoker(client);
2514 if (rc)
2515 return send_error (ctx, rc);
2517 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2519 if (!xml)
2521 log_write ("%s(%i): %s", __FILE__, __LINE__,
2522 pwmd_strerror (GPG_ERR_ENOMEM));
2523 return send_error (ctx, GPG_ERR_ENOMEM);
2526 pthread_cleanup_push ((void *)xmlFree, xml);
2527 rc = xfer_data (ctx, (char *) xml, len);
2528 pthread_cleanup_pop (1);
2529 return send_error (ctx, rc);
2532 static gpg_error_t
2533 getconfig_command (assuan_context_t ctx, char *line)
2535 struct client_s *client = assuan_get_pointer (ctx);
2536 gpg_error_t rc = 0;
2537 char filename[255] = { 0 }, param[747] = { 0 };
2538 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2540 if (!line || !*line)
2541 return send_error (ctx, GPG_ERR_SYNTAX);
2543 if (strchr (line, ' '))
2545 sscanf (line, " %254[^ ] %746c", filename, param);
2546 paramp = param;
2547 fp = filename;
2550 if (fp && !valid_filename (fp))
2551 return send_error (ctx, GPG_ERR_INV_VALUE);
2553 paramp = str_down (paramp);
2554 if (!strcmp (paramp, "passphrase"))
2555 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2557 p = config_get_value (fp ? fp : "global", paramp);
2558 if (!p)
2559 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2561 tmp = expand_homedir (p);
2562 xfree (p);
2563 if (!tmp)
2565 log_write ("%s(%i): %s", __FILE__, __LINE__,
2566 pwmd_strerror (GPG_ERR_ENOMEM));
2567 return send_error (ctx, GPG_ERR_ENOMEM);
2570 p = tmp;
2571 pthread_cleanup_push ((void *)xfree, p);
2572 rc = xfer_data (ctx, p, strlen (p));
2573 pthread_cleanup_pop (1);
2574 return send_error (ctx, rc);
2577 struct xpath_s
2579 xmlXPathContextPtr xp;
2580 xmlXPathObjectPtr result;
2581 xmlBufferPtr buf;
2582 char **req;
2585 static void
2586 xpath_command_free (void *arg)
2588 struct xpath_s *xpath = arg;
2590 if (!xpath)
2591 return;
2593 req_free (xpath->req);
2595 if (xpath->buf)
2596 xmlBufferFree (xpath->buf);
2598 if (xpath->result)
2599 xmlXPathFreeObject (xpath->result);
2601 if (xpath->xp)
2602 xmlXPathFreeContext (xpath->xp);
2605 static gpg_error_t
2606 do_xpath (assuan_context_t ctx, char *line)
2608 gpg_error_t rc;
2609 struct client_s *client = assuan_get_pointer (ctx);
2610 struct xpath_s _x = { 0 };
2611 struct xpath_s *xpath = &_x;
2613 if (!line || !*line)
2614 return GPG_ERR_SYNTAX;
2616 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2618 if (strv_printf (&xpath->req, "%s", line) == 0)
2619 return GPG_ERR_ENOMEM;
2622 xpath->xp = xmlXPathNewContext (client->doc);
2623 if (!xpath->xp)
2625 rc = GPG_ERR_BAD_DATA;
2626 goto fail;
2629 xpath->result =
2630 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2631 if (!xpath->result)
2633 rc = GPG_ERR_BAD_DATA;
2634 goto fail;
2637 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2639 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2640 goto fail;
2643 rc = xml_recurse_xpath_nodeset (client, client->doc,
2644 xpath->result->nodesetval,
2645 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2646 NULL);
2647 if (rc)
2648 goto fail;
2649 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2651 rc = GPG_ERR_NO_DATA;
2652 goto fail;
2654 else if (xpath->req[1])
2656 rc = 0;
2657 goto fail;
2660 pthread_cleanup_push ((void *)xpath_command_free, &xpath);
2661 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2662 xmlBufferLength (xpath->buf));
2663 pthread_cleanup_pop (0);
2664 fail:
2665 xpath_command_free (xpath);
2666 return rc;
2669 static gpg_error_t
2670 xpath_command (assuan_context_t ctx, char *line)
2672 struct client_s *client = assuan_get_pointer (ctx);
2673 gpg_error_t rc;
2674 struct argv_s *args[] = {
2675 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2676 NULL
2679 if (disable_list_and_dump == 1)
2680 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2682 rc = peer_is_invoker(client);
2683 if (rc)
2684 return send_error (ctx, rc);
2686 rc = parse_options (&line, args, client, 1);
2687 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2688 rc = GPG_ERR_SYNTAX;
2689 if (rc)
2690 return send_error (ctx, rc);
2692 if (client->opts & OPT_INQUIRE)
2694 unsigned char *result;
2695 size_t len;
2697 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2698 if (rc)
2699 return send_error (ctx, rc);
2701 pthread_cleanup_push ((void *)xfree, result);
2702 rc = do_xpath (ctx, (char *)result);
2703 pthread_cleanup_pop (1);
2705 else
2706 rc = do_xpath (ctx, line);
2708 return send_error (ctx, rc);
2711 static gpg_error_t
2712 do_xpathattr (assuan_context_t ctx, char *line)
2714 struct client_s *client = assuan_get_pointer (ctx);
2715 gpg_error_t rc;
2716 char **req = NULL;
2717 int cmd = 0; //SET
2718 struct xpath_s _x = { 0 };
2719 struct xpath_s *xpath = &_x;
2721 if (!line || !*line)
2722 return GPG_ERR_SYNTAX;
2724 if ((req = str_split (line, " ", 3)) == NULL)
2725 return GPG_ERR_ENOMEM;
2727 if (!req[0])
2729 rc = GPG_ERR_SYNTAX;
2730 goto fail;
2733 if (!strcasecmp (req[0], "SET"))
2734 cmd = 0;
2735 else if (!strcasecmp (req[0], "DELETE"))
2736 cmd = 1;
2737 else
2739 rc = GPG_ERR_SYNTAX;
2740 goto fail;
2743 if (!req[1] || !req[2])
2745 rc = GPG_ERR_SYNTAX;
2746 goto fail;
2749 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2751 rc = GPG_ERR_ENOMEM;
2752 goto fail;
2755 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2757 rc = GPG_ERR_SYNTAX;
2758 goto fail;
2761 xpath->xp = xmlXPathNewContext (client->doc);
2762 if (!xpath->xp)
2764 rc = GPG_ERR_BAD_DATA;
2765 goto fail;
2768 xpath->result = xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2769 if (!xpath->result)
2771 rc = GPG_ERR_BAD_DATA;
2772 goto fail;
2775 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2777 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2778 goto fail;
2781 rc = xml_recurse_xpath_nodeset (client, client->doc,
2782 xpath->result->nodesetval,
2783 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2784 (xmlChar *) req[1]);
2786 fail:
2787 xpath_command_free (xpath);
2788 strv_free (req);
2789 return rc;
2792 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2793 static gpg_error_t
2794 xpathattr_command (assuan_context_t ctx, char *line)
2796 struct client_s *client = assuan_get_pointer (ctx);
2797 gpg_error_t rc;
2798 struct argv_s *args[] = {
2799 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2800 NULL
2803 if (disable_list_and_dump == 1)
2804 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2806 rc = peer_is_invoker(client);
2807 if (rc)
2808 return send_error (ctx, rc);
2810 rc = parse_options (&line, args, client, 1);
2811 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
2812 rc = GPG_ERR_SYNTAX;
2813 if (rc)
2814 return send_error (ctx, rc);
2816 if (client->opts & OPT_INQUIRE)
2818 unsigned char *result;
2819 size_t len;
2821 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2822 if (rc)
2823 return send_error (ctx, rc);
2825 pthread_cleanup_push ((void *)xfree, result);
2826 rc = do_xpathattr (ctx, (char *)result);
2827 pthread_cleanup_pop (1);
2829 else
2830 rc = do_xpathattr (ctx, line);
2832 return send_error (ctx, rc);
2835 static gpg_error_t
2836 do_import (struct client_s *client, const char *root_element,
2837 unsigned char *content)
2839 xmlDocPtr doc = NULL;
2840 xmlNodePtr n = NULL, root;
2841 gpg_error_t rc;
2842 struct string_s *str = NULL;
2843 struct xml_request_s *req = NULL;
2845 if (!content || !*content)
2846 return GPG_ERR_SYNTAX;
2848 if (root_element)
2850 rc = xml_new_request (client, root_element, XML_CMD_STORE, &req);
2851 if (rc)
2853 xfree (content);
2854 return rc;
2857 if (!xml_valid_element_path (req->args, 0))
2859 xml_free_request (req);
2860 xfree (content);
2861 return GPG_ERR_INV_VALUE;
2865 str = string_new_content ((char *)content);
2866 str = string_prepend (str, "<pwmd>");
2867 str = string_append (str, "</pwmd>");
2868 doc = xmlReadDoc ((xmlChar *) str->str, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2869 string_free (str, 1);
2870 if (!doc)
2872 rc = GPG_ERR_BAD_DATA;
2873 goto fail;
2876 root = xmlDocGetRootElement (doc);
2877 root = root->children;
2878 if (root->type != XML_ELEMENT_NODE)
2880 rc = GPG_ERR_BAD_DATA;
2881 goto fail;
2884 rc = xml_validate_import (client, root);
2885 if (rc)
2886 goto fail;
2888 if (req)
2890 /* Create the new specified root element path, if needed. */
2891 n = xml_find_elements (client, req, xmlDocGetRootElement (req->doc),
2892 &rc);
2893 if (rc)
2894 goto fail;
2896 /* Overwrite the children of the specified root element path. */
2897 (void)xml_unlink_node (client, n->children);
2898 n->children = NULL;
2900 else
2901 n = xmlDocGetRootElement (client->doc);
2903 if (n)
2905 xmlNodePtr copy;
2906 xmlChar *name = xml_attribute_value (root, (xmlChar *)"_name");
2908 if (!name)
2909 rc = GPG_ERR_BAD_DATA;
2910 else if (!req)
2912 /* No --root argument passed. Overwrite the current documents' root
2913 * element matching the root element of the imported data. */
2914 xml_free_request (req);
2915 req = NULL;
2916 rc = xml_new_request (client, (char *)name, XML_CMD_DELETE, &req);
2917 xmlFree (name);
2918 if (!rc)
2920 xmlNodePtr tmp;
2922 tmp = xml_find_elements (client, req,
2923 xmlDocGetRootElement (req->doc), &rc);
2924 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2925 goto fail;
2927 if (!rc)
2928 (void)xml_unlink_node (client, tmp);
2930 rc = 0;
2934 if (!rc)
2936 copy = xmlCopyNodeList (root);
2937 if (!copy)
2938 rc = GPG_ERR_ENOMEM;
2939 else
2941 n = xmlAddChildList (n, copy);
2942 if (!n)
2943 rc = GPG_ERR_ENOMEM;
2948 if (!rc && n && n->parent)
2949 rc = xml_update_element_mtime (client, n->parent);
2951 fail:
2952 xml_free_request (req);
2954 if (doc)
2955 xmlFreeDoc (doc);
2957 return rc;
2960 static gpg_error_t
2961 parse_import_opt_root (void *data, void *value)
2963 struct client_s *client = data;
2965 client->import_root = str_dup (value);
2966 return client->import_root ? 0 : GPG_ERR_ENOMEM;
2969 static gpg_error_t
2970 import_command (assuan_context_t ctx, char *line)
2972 gpg_error_t rc;
2973 struct client_s *client = assuan_get_pointer (ctx);
2974 unsigned char *result;
2975 size_t len;
2976 struct argv_s *args[] = {
2977 &(struct argv_s) {"root", OPTION_TYPE_ARG, parse_import_opt_root},
2978 NULL
2981 xfree (client->import_root);
2982 client->import_root = NULL;
2983 rc = parse_options (&line, args, client, 0);
2984 if (rc)
2985 return send_error (ctx, rc);
2987 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2988 if (rc)
2990 xfree (client->import_root);
2991 client->import_root = NULL;
2992 return send_error (ctx, rc);
2995 rc = do_import (client, client->import_root, result);
2996 xfree (client->import_root);
2997 client->import_root = NULL;
2998 return send_error (ctx, rc);
3001 static gpg_error_t
3002 do_lock (struct client_s *client, int add)
3004 gpg_error_t rc = lock_file_mutex (client, add);
3006 if (!rc)
3007 client->flags |= FLAG_LOCK_CMD;
3009 return rc;
3012 static gpg_error_t
3013 lock_command (assuan_context_t ctx, char *line)
3015 struct client_s *client = assuan_get_pointer (ctx);
3016 gpg_error_t rc;
3018 if (line && *line)
3019 return send_error (ctx, GPG_ERR_SYNTAX);
3021 rc = do_lock (client, 0);
3022 return send_error (ctx, rc);
3025 static gpg_error_t
3026 unlock_command (assuan_context_t ctx, char *line)
3028 struct client_s *client = assuan_get_pointer (ctx);
3029 gpg_error_t rc;
3031 if (line && *line)
3032 return send_error (ctx, GPG_ERR_SYNTAX);
3034 rc = unlock_file_mutex (client, 0);
3035 return send_error (ctx, rc);
3038 static void
3039 get_set_env (const char *name, const char *value)
3041 if (value && *value)
3042 setenv (name, value, 1);
3043 else
3044 unsetenv (name);
3047 static gpg_error_t
3048 option_command (assuan_context_t ctx, char *line)
3050 struct client_s *client = assuan_get_pointer (ctx);
3051 gpg_error_t rc = 0;
3052 char namebuf[255] = { 0 };
3053 char *name = namebuf;
3054 char *value = NULL, *p, *tmp = NULL;
3056 p = strchr (line, '=');
3057 if (!p)
3059 strncpy (namebuf, line, sizeof(namebuf));
3060 namebuf[sizeof(namebuf)-1] = 0;
3062 else
3064 strncpy (namebuf, line, strlen (line)-strlen (p));
3065 namebuf[sizeof(namebuf)-1] = 0;
3066 value = p+1;
3069 log_write2 ("OPTION name='%s' value='%s'", name, value);
3071 if (strcasecmp (name, (char *) "lock-timeout") == 0)
3073 long n = 0;
3075 if (value)
3077 n = strtol (value, &tmp, 10);
3078 if (tmp && *tmp)
3079 return send_error (ctx, GPG_ERR_INV_VALUE);
3082 client->lock_timeout = n;
3084 else if (strcasecmp (name, (char *) "client-state") == 0)
3086 long n = 0;
3088 if (value)
3090 n = strtol (value, &tmp, 10);
3091 if ((tmp && *tmp) || (n < 0 || n > 1))
3092 return send_error (ctx, GPG_ERR_INV_VALUE);
3093 client->client_state = n;
3095 else
3096 client->client_state = 0;
3098 else if (strcasecmp (name, (char *) "NAME") == 0)
3100 if (value && strchr (value, ' '))
3101 rc = GPG_ERR_INV_VALUE;
3102 else
3104 tmp = pthread_getspecific (thread_name_key);
3105 pthread_setspecific (thread_name_key, NULL);
3106 xfree (tmp);
3107 MUTEX_LOCK (&cn_mutex);
3109 if (value && *value)
3110 pthread_setspecific (thread_name_key, str_dup (value));
3112 MUTEX_UNLOCK (&cn_mutex);
3115 else if (strcasecmp (name, "disable-pinentry") == 0)
3117 int n = 1;
3119 if (value && *value)
3121 n = (int) strtol (value, &tmp, 10);
3122 if (*tmp || n < 0 || n > 1)
3123 return send_error (ctx, GPG_ERR_INV_VALUE);
3126 if (n)
3127 client->flags |= FLAG_NO_PINENTRY;
3128 else
3129 client->flags &= ~FLAG_NO_PINENTRY;
3131 else if (strcasecmp (name, "ttyname") == 0)
3133 get_set_env ("GPG_TTY", value);
3135 else if (strcasecmp (name, "ttytype") == 0)
3137 get_set_env ("TERM", value);
3139 else if (strcasecmp (name, "display") == 0)
3141 get_set_env ("DISPLAY", value);
3143 else if (strcasecmp (name, "lc_messages") == 0)
3146 else if (strcasecmp (name, "lc_ctype") == 0)
3149 else if (strcasecmp (name, "desc") == 0)
3152 else if (strcasecmp (name, "pinentry-timeout") == 0)
3155 else
3156 rc = GPG_ERR_UNKNOWN_OPTION;
3158 return send_error (ctx, rc);
3161 static gpg_error_t
3162 do_rename (assuan_context_t ctx, char *line)
3164 struct client_s *client = assuan_get_pointer (ctx);
3165 char **args, *p;
3166 xmlNodePtr src, dst;
3167 struct string_s *str = NULL, *tstr;
3168 struct xml_request_s *req;
3169 gpg_error_t rc;
3171 args = str_split (line, " ", 0);
3172 if (!args || strv_length (args) != 2)
3174 strv_free (args);
3175 return GPG_ERR_SYNTAX;
3178 if (!xml_valid_element ((xmlChar *) args[1]))
3180 strv_free (args);
3181 return GPG_ERR_INV_VALUE;
3184 rc = xml_new_request (client, args[0], XML_CMD_RENAME, &req);
3185 if (rc)
3187 strv_free (args);
3188 return rc;
3191 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3192 if (rc)
3193 goto fail;
3195 rc = xml_is_element_owner (client, src);
3196 if (rc)
3197 goto fail;
3199 xmlChar *a = xmlGetProp (src, (xmlChar *) "_name");
3200 if (!a)
3202 rc = GPG_ERR_ENOMEM;
3203 goto fail;
3206 /* To prevent unwanted effects:
3208 * <element _name="a"><element _name="b"/></element>
3210 * RENAME a<TAB>b b
3212 if (xmlStrEqual (a, (xmlChar *) args[1]))
3214 xmlFree (a);
3215 rc = GPG_ERR_EEXIST;
3216 goto fail;
3219 xmlFree (a);
3220 str = string_new (args[0]);
3221 if (!str)
3223 rc = GPG_ERR_ENOMEM;
3224 goto fail;
3227 p = strrchr (str->str, '\t');
3228 if (p)
3229 tstr = string_truncate (str, strlen (str->str)-strlen (p));
3230 else
3231 tstr = string_truncate (str, 0);
3233 if (!tstr)
3235 string_free (str, 1);
3236 rc = GPG_ERR_ENOMEM;
3237 goto fail;
3240 str = tstr;
3241 tstr = string_append_printf (str, "%s%s", str->len ? "\t" : "", args[1]);
3242 if (!tstr)
3244 string_free (str, 1);
3245 rc = GPG_ERR_ENOMEM;
3246 goto fail;
3249 str = tstr;
3250 xml_free_request (req);
3251 rc = xml_new_request (client, str->str, XML_CMD_RENAME, &req);
3252 string_free (str, 1);
3253 if (rc)
3255 req = NULL;
3256 goto fail;
3259 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3260 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3261 goto fail;
3263 rc = 0;
3265 /* Target may exist:
3267 * <element _name="a"/>
3268 * <element _name="b" target="a"/>
3270 * RENAME b a
3272 if (src == dst)
3274 rc = GPG_ERR_EEXIST;
3275 goto fail;
3278 if (dst)
3280 rc = xml_is_element_owner (client, dst);
3281 if (rc)
3282 goto fail;
3284 rc = xml_add_attribute (client, src, "_name", args[1]);
3285 if (!rc)
3286 xml_unlink_node (client, dst);
3288 else
3289 rc = xml_add_attribute (client, src, "_name", args[1]);
3291 fail:
3292 xml_free_request (req);
3293 strv_free (args);
3294 return rc;
3297 static gpg_error_t
3298 rename_command (assuan_context_t ctx, char *line)
3300 struct client_s *client = assuan_get_pointer (ctx);
3301 gpg_error_t rc;
3302 struct argv_s *args[] = {
3303 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3304 NULL
3307 rc = parse_options (&line, args, client, 1);
3308 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3309 rc = GPG_ERR_SYNTAX;
3310 if (rc)
3311 return send_error (ctx, rc);
3313 if (client->opts & OPT_INQUIRE)
3315 unsigned char *result;
3316 size_t len;
3318 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3319 if (rc)
3320 return send_error (ctx, rc);
3322 pthread_cleanup_push ((void *)xfree, result);
3323 rc = do_rename (ctx, (char *)result);
3324 pthread_cleanup_pop (1);
3326 else
3327 rc = do_rename (ctx, line);
3329 return send_error (ctx, rc);
3332 static gpg_error_t
3333 do_copy (assuan_context_t ctx, char *line)
3335 struct client_s *client = assuan_get_pointer (ctx);
3336 char **args, **targs;
3337 xmlNodePtr src, dst, tree = NULL;
3338 struct xml_request_s *req;
3339 gpg_error_t rc;
3341 args = str_split (line, " ", 0);
3342 if (!args || strv_length (args) != 2)
3344 strv_free (args);
3345 return GPG_ERR_SYNTAX;
3348 targs = str_split (args[1], "\t", 0);
3349 if (!targs)
3351 strv_free (args);
3352 return GPG_ERR_SYNTAX;
3355 if (!xml_valid_element_path (targs, 0))
3357 strv_free (args);
3358 strv_free (targs);
3359 return GPG_ERR_INV_VALUE;
3361 strv_free (targs);
3363 rc = xml_new_request (client, args[0], XML_CMD_NONE, &req);
3364 if (rc)
3366 strv_free (args);
3367 return rc;
3370 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3371 if (rc)
3372 goto fail;
3374 tree = xmlCopyNodeList (src);
3375 if (!tree)
3377 rc = GPG_ERR_ENOMEM;
3378 goto fail;
3381 xml_free_request (req);
3382 /* Create the destination element path if it does not exist. */
3383 rc = xml_new_request (client, args[1], XML_CMD_STORE, &req);
3384 if (rc)
3386 req = NULL;
3387 goto fail;
3390 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3391 if (rc)
3392 goto fail;
3394 rc = xml_is_element_owner (client, dst);
3395 if (rc)
3396 goto fail;
3398 rc = xml_validate_target (client, req->doc, args[1], src);
3399 if (rc || src == dst)
3401 rc = rc ? rc : GPG_ERR_EEXIST;
3402 goto fail;
3405 /* Merge any attributes from the src node to the initial dst node. */
3406 for (xmlAttrPtr attr = tree->properties; attr; attr = attr->next)
3408 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3409 continue;
3411 xmlAttrPtr a = xmlHasProp (dst, attr->name);
3412 if (a)
3413 xmlRemoveProp (a);
3415 xmlChar *tmp = xmlNodeGetContent (attr->children);
3416 xmlNewProp (dst, attr->name, tmp);
3417 xmlFree (tmp);
3418 /* Create the default attributes. */
3419 rc = xml_add_attribute (client, dst, NULL, NULL);
3422 xmlNodePtr n = dst->children;
3423 (void)xml_unlink_node (client, n);
3424 dst->children = NULL;
3426 if (tree->children)
3428 n = xmlCopyNodeList (tree->children);
3429 if (!n)
3431 rc = GPG_ERR_ENOMEM;
3432 goto fail;
3435 n = xmlAddChildList (dst, n);
3436 if (!n)
3438 rc = GPG_ERR_ENOMEM;
3439 goto fail;
3442 rc = xml_update_element_mtime (client, xmlDocGetRootElement (client->doc)
3443 == dst->parent ? dst : dst->parent);
3446 fail:
3447 if (tree)
3448 (void)xml_unlink_node (client, tree);
3450 xml_free_request (req);
3451 strv_free (args);
3452 return rc;
3455 static gpg_error_t
3456 copy_command (assuan_context_t ctx, char *line)
3458 struct client_s *client = assuan_get_pointer (ctx);
3459 gpg_error_t rc;
3460 struct argv_s *args[] = {
3461 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3462 NULL
3465 rc = parse_options (&line, args, client, 1);
3466 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3467 rc = GPG_ERR_SYNTAX;
3468 if (rc)
3469 return send_error (ctx, rc);
3471 if (client->opts & OPT_INQUIRE)
3473 unsigned char *result;
3474 size_t len;
3476 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3477 if (rc)
3478 return send_error (ctx, rc);
3480 pthread_cleanup_push ((void *)xfree, result);
3481 rc = do_copy (ctx, (char *)result);
3482 pthread_cleanup_pop (1);
3484 else
3485 rc = do_copy (ctx, line);
3487 return send_error (ctx, rc);
3490 static gpg_error_t
3491 do_move (assuan_context_t ctx, char *line)
3493 struct client_s *client = assuan_get_pointer (ctx);
3494 char **args;
3495 xmlNodePtr src, dst;
3496 struct xml_request_s *req;
3497 gpg_error_t rc;
3499 args = str_split (line, " ", 0);
3500 if (!args || strv_length (args) != 2)
3502 strv_free (args);
3503 return GPG_ERR_SYNTAX;
3506 if (*args[1])
3508 char **targs = str_split (args[1], "\t", 0);
3509 if (!targs)
3511 strv_free (args);
3512 return GPG_ERR_ENOMEM;
3515 if (!xml_valid_element_path (targs, 0))
3517 strv_free (targs);
3518 strv_free (args);
3519 return GPG_ERR_INV_VALUE;
3522 strv_free (targs);
3525 rc = xml_new_request (client, args[0], XML_CMD_MOVE, &req);
3526 if (rc)
3528 strv_free (args);
3529 return rc;
3532 src = xml_find_elements (client, req, xmlDocGetRootElement (req->doc), &rc);
3533 if (rc)
3534 goto fail;
3536 rc = xml_is_element_owner (client, src);
3537 if (rc)
3538 goto fail;
3540 xml_free_request (req);
3541 req = NULL;
3542 if (*args[1])
3544 rc = xml_new_request (client, args[1], XML_CMD_NONE, &req);
3545 if (rc)
3546 goto fail;
3548 dst = xml_find_elements (client, req, xmlDocGetRootElement (req->doc),
3549 &rc);
3551 else
3552 dst = xmlDocGetRootElement (client->doc);
3554 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3555 goto fail;
3557 rc = 0;
3559 for (xmlNodePtr n = dst; n; n = n->parent)
3561 if (n == src)
3563 rc = GPG_ERR_EEXIST;
3564 goto fail;
3568 if (dst)
3570 xmlChar *a = xml_attribute_value (src, (xmlChar *) "_name");
3571 xmlNodePtr dup = xml_find_element (client, dst->children, (char *) a, &rc);
3573 xmlFree (a);
3574 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3575 goto fail;
3577 rc = 0;
3579 if (dup)
3581 if (dup == src)
3583 rc = GPG_ERR_EEXIST;
3584 goto fail;
3587 if (dst == xmlDocGetRootElement (client->doc))
3589 xmlNodePtr n = src;
3590 int match = 0;
3592 while (n->parent && n->parent != dst)
3593 n = n->parent;
3595 xmlChar *a = xml_attribute_value (n, (xmlChar *) "_name");
3596 xmlChar *b = xml_attribute_value (src, (xmlChar *) "_name");
3598 if (xmlStrEqual (a, b))
3600 match = 1;
3601 xmlUnlinkNode (src);
3602 (void)xml_unlink_node (client, n);
3605 xmlFree (a);
3606 xmlFree (b);
3608 if (!match)
3609 (void)xml_unlink_node (client, dup);
3611 else
3612 xmlUnlinkNode (dup);
3616 if (!dst && *req->args)
3618 struct xml_request_s *nreq = NULL;
3619 xmlChar *name = xml_attribute_value (src, (xmlChar *) "_name");
3621 if (src->parent == xmlDocGetRootElement (client->doc)
3622 && !strcmp ((char *) name, *req->args))
3624 xmlFree (name);
3625 rc = GPG_ERR_EEXIST;
3626 goto fail;
3629 xmlFree (name);
3630 rc = xml_new_request (client, args[1], XML_CMD_STORE, &nreq);
3631 if (!rc)
3633 dst = xml_find_elements (client, nreq,
3634 xmlDocGetRootElement (nreq->doc), &rc);
3635 xml_free_request (nreq);
3638 if (rc)
3639 goto fail;
3642 xml_update_element_mtime (client, src->parent);
3643 xmlUnlinkNode (src);
3645 dst = xmlAddChildList (dst, src);
3646 if (!dst)
3647 rc = GPG_ERR_ENOMEM;
3648 else
3649 xml_update_element_mtime (client, dst->parent);
3651 fail:
3652 xml_free_request (req);
3653 strv_free (args);
3654 return rc;
3657 static gpg_error_t
3658 move_command (assuan_context_t ctx, char *line)
3660 struct client_s *client = assuan_get_pointer (ctx);
3661 gpg_error_t rc;
3662 struct argv_s *args[] = {
3663 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3664 NULL
3667 rc = parse_options (&line, args, client, 1);
3668 if (!rc && (client->opts & OPT_INQUIRE) && line && *line)
3669 rc = GPG_ERR_SYNTAX;
3670 if (rc)
3671 return send_error (ctx, rc);
3673 if (client->opts & OPT_INQUIRE)
3675 unsigned char *result;
3676 size_t len;
3678 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3679 if (rc)
3680 return send_error (ctx, rc);
3682 pthread_cleanup_push ((void *)xfree, result);
3683 rc = do_move (ctx, (char *)result);
3684 pthread_cleanup_pop (1);
3686 else
3687 rc = do_move (ctx, line);
3689 return send_error (ctx, rc);
3692 static gpg_error_t
3693 ls_command (assuan_context_t ctx, char *line)
3695 gpg_error_t rc;
3696 char *tmp;
3697 char *dir;
3698 DIR *d;
3700 if (line && *line)
3701 return send_error (ctx, GPG_ERR_SYNTAX);
3703 tmp = str_asprintf ("%s/data", homedir);
3704 dir = expand_homedir (tmp);
3705 xfree (tmp);
3706 d = opendir (dir);
3707 rc = gpg_error_from_errno (errno);
3709 if (!d)
3711 xfree (dir);
3712 return send_error (ctx, rc);
3715 size_t len =
3716 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3717 struct dirent *p = xmalloc (len), *cur = NULL;
3718 char *list = NULL;
3720 xfree (dir);
3721 pthread_cleanup_push ((void *)xfree, p);
3722 pthread_cleanup_push ((void *)(void *)(void *)closedir, d);
3723 rc = 0;
3725 while (!readdir_r (d, p, &cur) && cur)
3727 struct stat st;
3729 if (stat (cur->d_name, &st) == -1 || !S_ISREG (st.st_mode))
3730 continue;
3732 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3734 if (!tmp)
3736 if (list)
3737 xfree (list);
3739 rc = GPG_ERR_ENOMEM;
3740 break;
3743 xfree (list);
3744 list = tmp;
3747 pthread_cleanup_pop (1); // closedir (d)
3748 pthread_cleanup_pop (1); // xfree (p)
3750 if (rc)
3751 return send_error (ctx, rc);
3753 if (!list)
3754 return send_error (ctx, GPG_ERR_NO_DATA);
3756 list[strlen (list) - 1] = 0;
3757 pthread_cleanup_push ((void *)xfree, list);
3758 rc = xfer_data (ctx, list, strlen (list));
3759 pthread_cleanup_pop (1);
3760 return send_error (ctx, rc);
3763 static gpg_error_t
3764 bye_notify (assuan_context_t ctx, char *line)
3766 struct client_s *cl = assuan_get_pointer (ctx);
3767 gpg_error_t ret = 0;
3769 update_client_state (cl, CLIENT_STATE_DISCON);
3771 #ifdef WITH_GNUTLS
3772 if (cl->thd->remote)
3774 int rc;
3778 struct timeval tv = { 0, 50000 };
3780 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3781 if (rc == GNUTLS_E_AGAIN)
3782 select (0, NULL, NULL, NULL, &tv);
3784 while (rc == GNUTLS_E_AGAIN);
3786 #endif
3788 /* This will let assuan_process_next() return. */
3789 if (fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK) == -1)
3791 cl->last_rc = gpg_error_from_errno (errno);
3792 ret = cl->last_rc;
3795 cl->last_rc = 0; // BYE command result
3796 return ret;
3799 static gpg_error_t
3800 reset_notify (assuan_context_t ctx, char *line)
3802 struct client_s *client = assuan_get_pointer (ctx);
3804 if (client)
3805 reset_client (client);
3807 return 0;
3811 * This is called before every Assuan command.
3813 static gpg_error_t
3814 command_startup (assuan_context_t ctx, const char *name)
3816 struct client_s *client = assuan_get_pointer (ctx);
3817 gpg_error_t rc;
3818 struct command_table_s *cmd = NULL;
3820 log_write2 ("command='%s'", name);
3821 client->last_rc = client->opts = 0;
3823 for (int i = 0; command_table[i]; i++)
3825 if (!strcasecmp (name, command_table[i]->name))
3827 if (command_table[i]->ignore_startup)
3828 return 0;
3829 cmd = command_table[i];
3830 break;
3834 if (!cmd)
3835 return GPG_ERR_UNKNOWN_COMMAND;
3837 client->last_rc = rc = gpg_error (file_modified (client, cmd));
3838 if (!rc)
3839 update_client_state (client, CLIENT_STATE_COMMAND);
3841 return rc;
3845 * This is called after every Assuan command.
3847 static void
3848 command_finalize (assuan_context_t ctx, gpg_error_t rc)
3850 struct client_s *client = assuan_get_pointer (ctx);
3852 if (!(client->flags & FLAG_LOCK_CMD))
3853 unlock_file_mutex (client, 0);
3855 unlock_flock (&client->flock_fd);
3856 log_write2 (_("command completed: rc=%u"), rc ? rc : client->last_rc);
3857 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
3858 #ifdef WITH_GNUTLS
3859 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
3860 #endif
3861 if (client->thd->state != CLIENT_STATE_DISCON)
3862 update_client_state (client, CLIENT_STATE_IDLE);
3865 static gpg_error_t
3866 parse_help_opt_html (void *data, void *value)
3868 struct client_s *client = data;
3870 (void) value;
3871 client->opts |= OPT_HTML;
3872 return 0;
3875 static gpg_error_t
3876 help_command (assuan_context_t ctx, char *line)
3878 gpg_error_t rc;
3879 int i;
3880 struct client_s *client = assuan_get_pointer (ctx);
3881 struct argv_s *args[] = {
3882 &(struct argv_s) {"html", OPTION_TYPE_NOARG, parse_help_opt_html},
3883 NULL
3886 rc = parse_options (&line, args, client, 1);
3887 if (rc)
3888 return send_error (ctx, rc);
3890 if (!line || !*line)
3892 char *tmp;
3893 char *help = str_dup (_("Usage: HELP [--html] [<COMMAND>]\n"
3894 "For commands that take an element path as an argument, each element is "
3895 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3896 "@*@*COMMANDS:"));
3898 for (i = 0; command_table[i]; i++)
3900 if (!command_table[i]->help)
3901 continue;
3903 /* @npxref{} won't put a "See " or "see " in front of the command.
3904 * It's not a texinfo command but needed for --html. */
3905 tmp = str_asprintf ("%s %s%s%s", help,
3906 client->opts & OPT_HTML ? "@npxref{" : "",
3907 command_table[i]->name,
3908 client->opts & OPT_HTML ? "}" : "");
3909 xfree (help);
3910 help = tmp;
3913 tmp = strip_texi_and_wrap (help, client->opts & OPT_HTML);
3914 xfree (help);
3915 pthread_cleanup_push ((void *)xfree, tmp);
3916 rc = xfer_data (ctx, tmp, strlen (tmp));
3917 pthread_cleanup_pop (1);
3918 return send_error (ctx, rc);
3921 for (i = 0; command_table[i]; i++)
3923 if (!strcasecmp (line, command_table[i]->name))
3925 char *help, *tmp;
3927 if (!command_table[i]->help)
3928 break;
3930 help = strip_texi_and_wrap (command_table[i]->help,
3931 client->opts & OPT_HTML);
3932 tmp = str_asprintf ("%s%s",
3933 (client->opts & OPT_HTML) ? "" : _("Usage: "),
3934 help);
3935 xfree (help);
3936 pthread_cleanup_push ((void *)xfree, tmp);
3937 rc = xfer_data (ctx, tmp, strlen (tmp));
3938 pthread_cleanup_pop (1);
3939 return send_error (ctx, rc);
3943 return send_error (ctx, GPG_ERR_INV_NAME);
3946 static void
3947 new_command (const char *name, int ignore, int unlock, int flock_type,
3948 gpg_error_t (*handler) (assuan_context_t, char *),
3949 const char *help)
3951 int i = 0;
3952 struct command_table_s **tmp;
3954 if (command_table)
3955 for (i = 0; command_table[i]; i++);
3957 tmp = xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
3958 assert (tmp);
3959 command_table = tmp;
3960 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
3961 command_table[i]->name = name;
3962 command_table[i]->handler = handler;
3963 command_table[i]->ignore_startup = ignore;
3964 command_table[i]->unlock = unlock;
3965 command_table[i]->flock_type = flock_type;
3966 command_table[i++]->help = help;
3967 command_table[i] = NULL;
3970 void
3971 deinit_commands ()
3973 int i;
3975 for (i = 0; command_table[i]; i++)
3976 xfree (command_table[i]);
3978 xfree (command_table);
3981 static int
3982 sort_commands (const void *arg1, const void *arg2)
3984 struct command_table_s *const *a = arg1;
3985 struct command_table_s *const *b = arg2;
3987 if (!*a || !*b)
3988 return 0;
3989 else if (*a && !*b)
3990 return 1;
3991 else if (!*a && *b)
3992 return -1;
3994 return strcmp ((*a)->name, (*b)->name);
3997 static gpg_error_t
3998 passwd_command (assuan_context_t ctx, char *line)
4000 struct client_s *client = assuan_get_pointer (ctx);
4001 gpg_error_t rc;
4003 rc = peer_is_invoker (client);
4004 if (rc == GPG_ERR_EACCES)
4005 return send_error (ctx, GPG_ERR_FORBIDDEN);
4006 else if (rc)
4007 return send_error (ctx, rc);
4009 if (client->flags & FLAG_NEW)
4010 return send_error (ctx, GPG_ERR_INV_STATE);
4012 client->crypto->keyfile = config_get_string (client->filename,
4013 "passphrase_file");
4014 rc = crypto_init_ctx (client->crypto, client->flags & FLAG_NO_PINENTRY,
4015 client->crypto->keyfile);
4016 if (rc)
4017 return send_error (ctx, rc);
4019 if (!rc)
4020 rc = crypto_passwd (client, client->crypto);
4022 crypto_free_non_keys (client->crypto);
4023 return send_error (ctx, rc);
4026 static gpg_error_t
4027 parse_opt_data (void *data, void *value)
4029 struct client_s *client = data;
4031 (void) value;
4032 client->opts |= OPT_DATA;
4033 return 0;
4036 static gpg_error_t
4037 send_client_list (assuan_context_t ctx)
4039 struct client_s *client = assuan_get_pointer (ctx);
4040 gpg_error_t rc = 0;
4042 if (client->opts & OPT_VERBOSE)
4044 unsigned i, t;
4045 char **list = NULL;
4046 char *line;
4048 MUTEX_LOCK (&cn_mutex);
4049 pthread_cleanup_push ((void *)release_mutex_cb, &cn_mutex);
4050 t = slist_length (cn_thread_list);
4052 for (i = 0; i < t; i++)
4054 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
4055 char *tmp;
4057 if (thd->state == CLIENT_STATE_UNKNOWN)
4058 continue;
4060 tmp = build_client_info_line (thd, 0);
4061 if (tmp)
4063 char **l = strv_cat (list, tmp);
4064 if (!l)
4065 rc = GPG_ERR_ENOMEM;
4066 else
4067 list = l;
4069 else
4070 rc = GPG_ERR_ENOMEM;
4072 if (rc)
4074 strv_free (list);
4075 break;
4079 pthread_cleanup_pop (1);
4080 if (rc)
4081 return rc;
4083 line = strv_join ("\n", list);
4084 strv_free (list);
4085 pthread_cleanup_push ((void *)xfree, line);
4086 rc = xfer_data (ctx, line, strlen (line));
4087 pthread_cleanup_pop (1);
4088 return rc;
4091 if (client->opts & OPT_DATA)
4093 char buf[ASSUAN_LINELENGTH];
4095 MUTEX_LOCK (&cn_mutex);
4096 snprintf (buf, sizeof (buf), "%u", slist_length (cn_thread_list));
4097 MUTEX_UNLOCK (&cn_mutex);
4098 rc = xfer_data (ctx, buf, strlen (buf));
4100 else
4101 rc = send_status (ctx, STATUS_CLIENTS, NULL);
4103 return rc;
4106 static gpg_error_t
4107 getinfo_command (assuan_context_t ctx, char *line)
4109 struct client_s *client = assuan_get_pointer (ctx);
4110 gpg_error_t rc;
4111 char buf[ASSUAN_LINELENGTH];
4112 struct argv_s *args[] = {
4113 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
4114 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_opt_verbose},
4115 NULL
4118 rc = parse_options (&line, args, client, 1);
4119 if (rc)
4120 return send_error (ctx, rc);
4122 if (!strcasecmp (line, "clients"))
4124 rc = send_client_list (ctx);
4126 else if (!strcasecmp (line, "cache"))
4128 if (client->opts & OPT_DATA)
4130 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
4131 rc = xfer_data (ctx, buf, strlen (buf));
4133 else
4134 rc = send_status (ctx, STATUS_CACHE, NULL);
4136 else if (!strcasecmp (line, "pid"))
4138 char buf[32];
4139 pid_t pid = getpid ();
4141 snprintf (buf, sizeof (buf), "%i", pid);
4142 rc = xfer_data (ctx, buf, strlen (buf));
4144 else if (!strcasecmp (line, "version"))
4146 char *buf = str_asprintf ("0x%06x %s", VERSION_HEX,
4147 #ifdef WITH_GNUTLS
4148 "GNUTLS "
4149 #endif
4150 #ifdef WITH_LIBACL
4151 "ACL "
4152 #endif
4153 "");
4154 pthread_cleanup_push (xfree, buf);
4155 rc = xfer_data (ctx, buf, strlen (buf));
4156 pthread_cleanup_pop (1);
4158 else if (!strcasecmp (line, "last_error"))
4160 if (client->last_error)
4161 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4162 else
4163 rc = GPG_ERR_NO_DATA;
4165 else if (!strcasecmp (line, "user"))
4167 char *user = NULL;
4169 #ifdef WITH_GNUTLS
4170 if (client->thd->remote)
4171 user = str_asprintf ("#%s", client->thd->tls->fp);
4172 else
4173 user = get_username (client->thd->peer->uid);
4174 #else
4175 user = get_username (client->thd->peer->uid);
4176 #endif
4177 if (user)
4179 pthread_cleanup_push ((void *)xfree, user);
4180 rc = xfer_data (ctx, user, strlen (user));
4181 pthread_cleanup_pop (1);
4183 else
4184 rc = GPG_ERR_NO_DATA;
4186 else
4187 rc = gpg_error (GPG_ERR_SYNTAX);
4189 return send_error (ctx, rc);
4192 static gpg_error_t
4193 parse_listkeys_opt_secret_only (void *data, void *value)
4195 struct client_s *client = data;
4197 (void) value;
4198 client->opts |= OPT_SECRET_ONLY;
4199 return 0;
4202 static gpg_error_t
4203 keyinfo_command (assuan_context_t ctx, char *line)
4205 struct client_s *client = assuan_get_pointer (ctx);
4206 gpg_error_t rc = 0;
4207 char **keys = NULL, **p = NULL;
4208 int sym = 0;
4210 if (line && *line)
4211 return send_error (ctx, GPG_ERR_SYNTAX);
4213 if (!(client->flags & FLAG_OPEN))
4214 return send_error (ctx, GPG_ERR_INV_STATE);
4216 if (client->flags & FLAG_NEW)
4217 return send_error (ctx, GPG_ERR_NO_DATA);
4219 rc = lock_flock (ctx, client->filename, FLOCK_TYPE_SH, &client->flock_fd);
4220 if (rc)
4221 return send_error (ctx, rc);
4223 rc = crypto_is_symmetric (client->filename);
4224 unlock_flock (&client->flock_fd);
4225 if (!rc)
4226 sym = 1;
4227 else if (rc != GPG_ERR_BAD_DATA)
4228 return send_error (ctx, rc);
4230 rc = 0;
4231 if (!sym)
4233 p = strv_catv(keys, client->crypto->pubkey);
4234 if (!p)
4235 rc = GPG_ERR_ENOMEM;
4238 if (!rc)
4240 keys = p;
4241 for (p = client->crypto->sigkey; p && *p; p++)
4243 if (!strv_printf(&keys, "S%s", *p))
4245 strv_free (keys);
4246 return send_error (ctx, GPG_ERR_ENOMEM);
4250 else
4251 rc = GPG_ERR_ENOMEM;
4253 if (!rc && keys)
4255 line = strv_join ("\n", keys);
4256 strv_free (keys);
4257 pthread_cleanup_push ((void *)xfree, line);
4258 if (line)
4259 rc = xfer_data (ctx, line, strlen (line));
4260 else
4261 rc = GPG_ERR_ENOMEM;
4263 pthread_cleanup_pop (1);
4265 else if (!keys)
4266 rc = GPG_ERR_NO_DATA;
4267 else
4268 strv_free (keys);
4270 return send_error (ctx, rc);
4273 static gpg_error_t
4274 kill_command (assuan_context_t ctx, char *line)
4276 struct client_s *client = assuan_get_pointer (ctx);
4277 gpg_error_t rc;
4278 unsigned i, t;
4280 if (!line || !*line)
4281 return send_error (ctx, GPG_ERR_SYNTAX);
4283 MUTEX_LOCK (&cn_mutex);
4284 pthread_cleanup_push ((void *)release_mutex_cb, &cn_mutex);
4285 t = slist_length (cn_thread_list);
4286 rc = GPG_ERR_ESRCH;
4288 for (i = 0; i < t; i++)
4290 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
4291 char *tmp = str_asprintf ("%p", thd->tid);
4293 if (strcmp (line, tmp))
4295 xfree (tmp);
4296 continue;
4299 xfree (tmp);
4300 rc = peer_is_invoker (client);
4301 if (!rc)
4303 #ifdef HAVE_PTHREAD_CANCEL
4304 pthread_cancel (thd->tid);
4305 #else
4306 pthread_kill (thd->tid, SIGUSR2);
4307 #endif
4308 break;
4310 else if (rc == GPG_ERR_EACCES)
4311 rc = GPG_ERR_FORBIDDEN;
4312 else if (rc)
4313 break;
4315 if (config_get_boolean ("global", "strict_kill"))
4316 break;
4318 #ifdef WITH_GNUTLS
4319 if (client->thd->remote && thd->remote)
4321 if (!thd->tls || !thd->tls->fp)
4323 rc = GPG_ERR_INV_STATE;
4324 break;
4326 if (!strcmp (client->thd->tls->fp, thd->tls->fp))
4328 #ifdef HAVE_PTHREAD_CANCEL
4329 pthread_cancel (thd->tid);
4330 #else
4331 pthread_kill (thd->tid, SIGUSR2);
4332 #endif
4333 break;
4336 else if (!client->thd->remote && !thd->remote)
4337 #endif
4339 if (client->thd->peer->uid == thd->peer->uid)
4341 #ifdef HAVE_PTHREAD_CANCEL
4342 pthread_cancel (thd->tid);
4343 #else
4344 pthread_kill (thd->tid, SIGUSR2);
4345 #endif
4348 break;
4351 pthread_cleanup_pop (1);
4352 return send_error (ctx, rc);
4355 static gpg_error_t
4356 listkeys_command (assuan_context_t ctx, char *line)
4358 struct client_s *client = assuan_get_pointer (ctx);
4359 struct crypto_s *crypto = NULL;
4360 char **pattern = NULL;
4361 gpgme_key_t *keys = NULL;
4362 char **result = NULL;
4363 gpg_error_t rc;
4364 struct argv_s *args[] = {
4365 &(struct argv_s) {"secret-only", OPTION_TYPE_NOARG,
4366 parse_listkeys_opt_secret_only },
4367 NULL
4370 rc = parse_options (&line, args, client, 1);
4371 if (rc)
4372 return send_error (ctx, rc);
4374 rc = crypto_init (&crypto, client->ctx, client->filename,
4375 client->flags & FLAG_NO_PINENTRY, NULL);
4376 if (rc)
4377 return send_error (ctx, rc);
4379 pthread_cleanup_push ((void *)crypto_free, crypto);
4380 pattern = str_split (line, ",", 0);
4381 pthread_cleanup_push ((void *)(void *)strv_free, pattern);
4382 rc = crypto_list_keys (crypto, pattern, client->opts & OPT_SECRET_ONLY,
4383 &keys);
4384 pthread_cleanup_pop (1);
4385 pthread_cleanup_pop (1);
4386 if (!rc)
4388 int i;
4390 for (i = 0; keys[i]; i++)
4392 char *p = crypto_key_info (keys[i]);
4393 char **r;
4395 if (!p)
4397 rc = GPG_ERR_ENOMEM;
4398 break;
4401 r = strv_cat (result, p);
4402 if (!r)
4404 rc = GPG_ERR_ENOMEM;
4405 break;
4408 result = r;
4411 if (!i)
4412 rc = GPG_ERR_NO_DATA;
4414 crypto_free_key_list (keys);
4417 if (!rc)
4419 line = strv_join ("\n", result);
4420 strv_free (result);
4421 pthread_cleanup_push ((void *)xfree, line);
4422 if (!line)
4423 rc = GPG_ERR_ENOMEM;
4424 else
4425 rc = xfer_data (ctx, line, strlen (line));
4427 pthread_cleanup_pop (1);
4429 else
4430 strv_free (result);
4432 return send_error (ctx, rc);
4435 void
4436 init_commands ()
4438 /* !BEGIN-HELP-TEXT!
4440 * This comment is used as a marker to generate the offline documentation
4441 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4442 * script to determine where commands begin and end.
4444 new_command("HELP", 1, 1, 0, help_command, _(
4445 "HELP [--html] [<COMMAND>]\n"
4446 "Show available commands or command specific help text."
4447 "@*@*"
4448 "The @option{--html} option will output the help text in HTML format."
4451 new_command("KILL", 1, 0, 0, kill_command, _(
4452 "KILL <thread_id>\n"
4453 "Terminates the client identified by @var{thread_id} and releases any file "
4454 "lock or other resources it has held. See @code{GETINFO} (@pxref{GETINFO}) "
4455 "for details about listing connected clients. An @code{invoking_user} "
4456 "(@pxref{Configuration}) may kill any client while others may only kill "
4457 "clients of the same @code{UID} or @abbr{TLS} fingerprint.\n"
4460 new_command("LISTKEYS", 0, 0, 0, listkeys_command, _(
4461 "LISTKEYS [--secret-only] [pattern[,<pattern>]]\n"
4462 "Returns a new line separated list of key information matching a comma "
4463 "separated list of @var{pattern}'s. When option @option{--secret-only} is "
4464 "specified, only keys matching @var{pattern} that also have a secret key "
4465 "available will be returned."
4468 new_command("KEYINFO", 1, 0, 0, keyinfo_command, _(
4469 "KEYINFO\n"
4470 "Returns a new line separated list of key ID's that the currently opened "
4471 "data file has recipients and signers for. If the key is a signing key it "
4472 "will be prefixed with an @code{S}. If the file is a new one, or has no "
4473 "signers in the case of being symmetrically encrypted, the error code "
4474 "@code{GPG_ERR_NO_DATA} is returned."
4477 new_command("GETINFO", 1, 1, 0, getinfo_command, _(
4478 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4479 "Get server and other information. The information is returned via a status "
4480 "message (@pxref{Status Messages}) unless otherwise noted or @option{--data} "
4481 "is specified."
4482 "@*@*"
4483 "@var{CACHE} returns the number of cached documents."
4484 "@*@*"
4485 "@var{CLIENTS} returns the number of "
4486 "connected clients via a status message or a list of connected clients when "
4487 "the @option{--verbose} parameter is used (implies @option{--data}). A "
4488 "verbose line of a client list contains "
4489 "space delimited "
4490 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4491 "IP address if remote, file lock status, whether the current client is self "
4492 "or not, client state (see below), "
4493 "user ID or TLS fingerprint of the connected client, username if the "
4494 "client is a local one else @code{-}, and finally the time stamp of when the "
4495 "client connected."
4496 "@*@*"
4497 "Client state @code{0} is an unknown client state, state @code{1} indicates "
4498 "the client has connected but hasn't completed initializing, state @code{2} "
4499 "indicates that the client is idle, state @code{3} means the "
4500 "client is in a command and state @code{4} means the client is disconnecting. "
4501 "@*@*"
4502 "@var{PID} returns the process ID number of the server via a data response."
4503 "@*@*"
4504 "@var{VERSION} returns the server version number and compile-time features "
4505 "via a data response with each being space delimited."
4506 "@*@*"
4507 "@var{LAST_ERROR} returns a detailed description of the last failed command "
4508 "via a data response, when available."
4509 "@*@*"
4510 "@var{USER} returns the username or @abbr{TLS} hash of the connected client "
4511 "via a data response."
4514 new_command("PASSWD", 0, 0, FLOCK_TYPE_EX|FLOCK_TYPE_KEEP, passwd_command, _(
4515 "PASSWD\n"
4516 "Changes the passphrase of the secret key required to open the current "
4517 "data file. If the data file is symmetrically encrypted, the error "
4518 "@code{GPG_ERR_NOT_SUPPORTED} is returned. When symmetrically encrypted, "
4519 "the @code{SAVE} command (@pxref{SAVE}) should be used instead to prevent "
4520 "this command saving any unwanted changes to the @abbr{XML} document."
4521 "@*@*"
4522 "This command is not available to non-invoking clients "
4523 "(@pxref{Access Control})."
4526 new_command("OPEN", 1, 1, FLOCK_TYPE_SH|FLOCK_TYPE_KEEP, open_command, _(
4527 "OPEN [--lock] <filename>\n"
4528 "Opens @var{filename}. When the @var{filename} is not found on the "
4529 "file-system then a new in-memory document will be created. If the file is "
4530 "found, it is looked for in the file cache and when found no passphrase will "
4531 "be required to open it. When not cached, @cite{pinentry(1)} will be used to "
4532 "retrieve the passphrase for decryption unless @option{disable-pinentry} "
4533 "(@pxref{OPTION}) was specified in which case @command{pwmd} will "
4534 "@emph{INQUIRE} the client for the passphrase."
4535 "@*@*"
4536 "When the @option{--lock} option is passed then the file mutex will be "
4537 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4538 "file had been opened."
4541 new_command("SAVE", 0, 0, FLOCK_TYPE_EX|FLOCK_TYPE_KEEP, save_command, _(
4542 "SAVE [--symmetric] [--inquire-keyparam] [--keyid=<fpr>[,..] | [--inquire-keyid]] [--sign-keyid=<fpr>[,..] | [--inquire-sign-keyid]]\n"
4543 "Writes the in-memory @abbr{XML} document to disk. The file written to is the "
4544 "file that was opened using the @code{OPEN} command (@pxref{OPEN}). If the "
4545 "file is a new one or the option @option{--inquire-keyparam} was passed, a "
4546 "new keypair will be generated and @cite{pinentry(1)} will be used to prompt "
4547 "for the passphrase of the secret key used for signing."
4548 "@*@*"
4549 "The @option{--inquire-keyparam} option will send an "
4550 "@emph{INQUIRE} to the client to obtain the key parameters to use for "
4551 "generating the new keypair. The inquired data is expected to be in "
4552 "@cite{gnupg} @abbr{XML} format. See the @cite{gnupg} documentation for "
4553 "details. Note that when this option is specified a new keypair will be "
4554 "generated reguardless if the file is a new one and that the passphrase for "
4555 "the current file will be required before generating the new keypair. This "
4556 "option is available to non-invoking clients (@pxref{Access Control}) only "
4557 "when the file is a new one."
4558 "@*@*"
4559 "You can encrypt the data file to a recipient other than the one that it "
4560 "was encrypted with by passing the @option{--keyid} or "
4561 "@option{--inquire-keyid} option with "
4562 "the fingerprint of a public encryption key as its argument. Use the "
4563 "@command{LISTKEYS} command (@pxref{LISTKEYS}) to show key information by "
4564 "pattern. The @option{--sign-keyid} or @option{--inquire-sign-keyid} option "
4565 "may also be used to sign the data "
4566 "file with an alternate key by specifying the fingerprint of a secret key. "
4567 "A passphrase to decrypt the data file "
4568 "will be required if one or more of the original encryption or signing keys "
4569 "are not found in either of these two options' arguments. The original "
4570 "encryption or signing keys will be used when either of these options are "
4571 "not specified."
4572 "@*@*"
4573 "The @option{--keyid} and @option{--sign-keyid} options are not available "
4574 "for non-invoking clients "
4575 "(@pxref{Access Control}) when the recipients or signers do not match those "
4576 "that were used when the file was @code{OPEN}'ed."
4577 "@*@*"
4578 "The @option{--symmetric} option specifies that a new data file be "
4579 "conventionally encrypted. These types of data files do not use a recipient "
4580 "public key but may be signed by using the @option{--sign-keyid} or "
4581 "@option{--inquire-sign-keyid} options. To remove all signers from a "
4582 "symmtrically encrypted data file, leave the option value empty. Note that "
4583 "you cannot change encryption schemes once a data file has been saved."
4586 new_command("ISCACHED", 1, 0, 0, iscached_command, _(
4587 "ISCACHED [--lock] [--agent [--sign]] <filename>\n"
4588 "Determines the file cache status of the specified @var{filename}. "
4589 "The default is to test whether the filename is cached in memory. Passing "
4590 "option @option{--agent} will test the @command{gpg-agent} cache for at most "
4591 "one cached key used for opening the data file (@pxref{OPEN}). To test if "
4592 "a signing key is cached, pass @option{--sign} along with @option{--agent}. "
4593 "Both the @option{--agent} and @option{--sign} options require an opened data "
4594 "file."
4595 "@*@*"
4596 "An @emph{OK} response is returned if the specified @var{filename} is found "
4597 "in the cache. If not found in the cache but exists on the filesystem "
4598 "then @code{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4599 "returned."
4600 "@*@*"
4601 "The @option{--lock} option will lock the file mutex of @var{filename} when "
4602 "the file exists; it does not need to be opened nor cached. The lock will be "
4603 "released when the client exits or sends the @code{UNLOCK} command "
4604 "(@pxref{UNLOCK}). When this option is passed the current data file is closed."
4607 new_command("CLEARCACHE", 1, 1, 0, clearcache_command, _(
4608 "CLEARCACHE [<filename>]\n"
4609 "Clears a file cache entry for all or the specified @var{filename}. Note that "
4610 "this will also clear any @command{gpg-agent} cached keys which may cause "
4611 "problems if another data file shares the same keys as @var{filename}."
4612 "@*@*"
4613 "When clearing all cache entries a permissions test is done against the "
4614 "current client based on the @var{access} configuration parameter in a "
4615 "@var{filename} section. Both a cache entry may be cleared and an error "
4616 "returned depending on cached data files and client permissions."
4619 new_command("CACHETIMEOUT", 1, 1, 0, cachetimeout_command, _(
4620 "CACHETIMEOUT <filename> <seconds>\n"
4621 "The time in @var{seconds} until @var{filename} will be removed from the "
4622 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4623 "the passphrase for each @code{OPEN} command (@pxref{OPEN}) or @code{SAVE} "
4624 "(@pxref{SAVE}) command. @xref{Configuration}, and the @code{cache_timeout} "
4625 "parameter."
4628 new_command("LIST", 0, 1, 0, list_command, _(
4629 "LIST [--inquire] [--recurse] [element[<TAB>child[..]]]\n"
4630 "If no element path is given then a newline separated list of root elements "
4631 "is returned with a data response. If given, then children of the specified "
4632 "element path are returned."
4633 "@*@*"
4634 "Each element path "
4635 "returned will have zero or more flags appened to it. These flags are "
4636 "delimited from the element path by a single space character. A flag itself "
4637 "is a single character. Flag @code{P} indicates that access to the element "
4638 "is denied. Flag @code{+} indicates that there are child nodes of "
4639 "the current element path. Flag @code{E} indicates that an element of the "
4640 "element path contained in a @var{target} attribute could not be found. Flag "
4641 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4642 "(@pxref{Configuration}). Flag @code{T}, followed by a single space character, "
4643 "then an element path, is the element path of the @var{target} attribute "
4644 "contained in the current element."
4645 "@*@*"
4646 "When a specified element path contains an error, beit from the final "
4647 "element in the path or any previous element, the path is still shown but "
4648 "will contain the error flag for the element with the error. Determining "
4649 "the actual element which contains the error is up to the client. This can be "
4650 "done by traversing the final element up to parent elements that contain the "
4651 "same error flag."
4652 "@*@*"
4653 "The option @option{--recurse} may be used to list the entire element tree "
4654 "for a specified element path or the entire tree for all root elements."
4655 "@*@*"
4656 "When the @option{--inquire} option is passed then all remaining non-option "
4657 "arguments are retrieved via a server @emph{INQUIRE}."
4660 new_command("REALPATH", 0, 1, 0, realpath_command, _(
4661 "REALPATH [--inquire] element[<TAB>child[..]]\n"
4662 "Resolves all @code{target} attributes of the specified element path and "
4663 "returns the result with a data response. @xref{Target Attribute}, for details."
4664 "@*@*"
4665 "When the @option{--inquire} option is passed then all remaining non-option "
4666 "arguments are retrieved via a server @emph{INQUIRE}."
4669 new_command("STORE", 0, 1, 0, store_command, _(
4670 "STORE element[<TAB>child[..]]<TAB>[content]\n"
4671 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4672 "@*@*"
4673 "Creates a new element path or modifies the @var{content} of an existing "
4674 "element. If only a single element is specified then a new root element is "
4675 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4676 "set to the final @key{TAB} delimited element. If no @var{content} is "
4677 "specified after the final @key{TAB}, then the content of the existing "
4678 "element will be removed; or will be empty if creating a new element."
4679 "@*@*"
4680 "The only restriction of an element name is that it not contain whitespace "
4681 "characters. There is no other whitespace between the @key{TAB} delimited "
4682 "elements. It is recommended that the content of an element be base64 encoded "
4683 "when it contains control or @key{TAB} characters to prevent @abbr{XML} "
4684 "parsing and @command{pwmd} syntax errors."
4687 new_command("RENAME", 0, 1, 0, rename_command, _(
4688 "RENAME [--inquire] element[<TAB>child[..]] <value>\n"
4689 "Renames the specified @var{element} to the new @var{value}. If an element of "
4690 "the same name as the @var{value} already exists it will be overwritten."
4691 "@*@*"
4692 "When the @option{--inquire} option is passed then all remaining non-option "
4693 "arguments are retrieved via a server @emph{INQUIRE}."
4696 new_command("COPY", 0, 1, 0, copy_command, _(
4697 "COPY [--inquire] source[<TAB>child[..]] dest[<TAB>child[..]]\n"
4698 "Copies the entire element tree starting from the child node of the source "
4699 "element, to the destination element path. If the destination element path "
4700 "does not exist then it will be created; otherwise it is overwritten."
4701 "@*@*"
4702 "Note that attributes from the source element are merged into the "
4703 "destination element when the destination element path exists. When an "
4704 "attribute of the same name exists in both the source and destination "
4705 "elements then the destination attribute will be updated to the source "
4706 "attribute value."
4707 "@*@*"
4708 "When the @option{--inquire} option is passed then all remaining non-option "
4709 "arguments are retrieved via a server @emph{INQUIRE}."
4712 new_command("MOVE", 0, 1, 0, move_command, _(
4713 "MOVE [--inquire] source[<TAB>child[..]] [dest[<TAB>child[..]]]\n"
4714 "Moves the source element path to the destination element path. If the "
4715 "destination is not specified then it will be moved to the root node of the "
4716 "document. If the destination is specified and exists then it will be "
4717 "overwritten; otherwise non-existing elements of the destination element "
4718 "path will be created."
4719 "@*@*"
4720 "When the @option{--inquire} option is passed then all remaining non-option "
4721 "arguments are retrieved via a server @emph{INQUIRE}."
4724 new_command("DELETE", 0, 1, 0, delete_command, _(
4725 "DELETE [--inquire] element[<TAB>child[..]]\n"
4726 "Removes the specified element path and all of its children. This may break "
4727 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4728 "refers to this element or any of its children."
4729 "@*@*"
4730 "When the @option{--inquire} option is passed then all remaining non-option "
4731 "arguments are retrieved via a server @emph{INQUIRE}."
4734 new_command("GET", 0, 1, 0, get_command, _(
4735 "GET [--inquire] element[<TAB>child[..]]\n"
4736 "Retrieves the content of the specified element. The content is returned "
4737 "with a data response."
4738 "@*@*"
4739 "When the @option{--inquire} option is passed then all remaining non-option "
4740 "arguments are retrieved via a server @emph{INQUIRE}."
4743 new_command("ATTR", 0, 1, 0, attr_command, _(
4744 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] element[<TAB>child[..]] ..\n"
4745 "@table @asis\n"
4746 "@item ATTR SET attribute element[<TAB>child[..]] [value]\n"
4747 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4748 "element. When no @var{value} is specified any existing value will be removed."
4749 "@*@*"
4750 "@item ATTR DELETE attribute element[<TAB>child[..]]\n"
4751 " Removes an attribute from an element. If @var{attribute} is @code{_name} "
4752 "or @code{target} an error is returned. Use the @command{DELETE} command "
4753 "(@pxref{DELETE}) instead."
4754 "@*@*"
4755 "@item ATTR LIST element[<TAB>child[..]]\n"
4756 " Retrieves a newline separated list of attributes names and values "
4757 "from the specified element. Each attribute name and value is space delimited."
4758 "@*@*"
4759 "@item ATTR GET attribute element[<TAB>child[..]]\n"
4760 " Retrieves the value of an @var{attribute} from an element."
4761 "@end table\n"
4762 "@*@*"
4763 "When the @option{--inquire} option is passed then all remaining non-option "
4764 "arguments are retrieved via a server @emph{INQUIRE}."
4765 "@*@*"
4766 "@xref{Target Attribute}, for details about this special attribute and also "
4767 "@pxref{Other Attributes} for other attributes that are handled specially "
4768 "by @command{pwmd}."
4771 new_command("XPATH", 0, 1, 0, xpath_command, _(
4772 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4773 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4774 "specified it is assumed the expression is a request to return a result. "
4775 "Otherwise, the result is set to the @var{value} argument and the document is "
4776 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4777 "is assumed to be empty and the document is updated. For example:"
4778 "@sp 1\n"
4779 "@example\n"
4780 "XPATH //element[@@_name='password']@key{TAB}\n"
4781 "@end example\n"
4782 "@sp 1\n"
4783 "would clear the content of all @var{password} elements in the data file "
4784 "while leaving off the trailing @key{TAB} would return all @var{password} "
4785 "elements in @abbr{XML} format."
4786 "@*@*"
4787 "When the @option{--inquire} option is passed then all remaining non-option "
4788 "arguments are retrieved via a server @emph{INQUIRE}."
4789 "@*@*"
4790 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4791 "expression syntax."
4794 new_command("XPATHATTR", 0, 1, 0, xpathattr_command, _(
4795 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4796 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4797 "attributes and does not return a result. For the @var{SET} operation the "
4798 "@var{value} is optional but the field is required. If not specified then "
4799 "the attribute value will be empty. For example:"
4800 "@sp 1\n"
4801 "@example\n"
4802 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4803 "@end example\n"
4804 "@sp 1\n"
4805 "would create a @var{password} attribute for each @var{password} element "
4806 "found in the document. The attribute value will be empty but still exist."
4807 "@*@*"
4808 "When the @option{--inquire} option is passed then all remaining non-option "
4809 "arguments are retrieved via a server @emph{INQUIRE}."
4810 "@*@*"
4811 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4812 "expression syntax."
4815 new_command("IMPORT", 0, 1, 0, import_command, _(
4816 "IMPORT [--root=element[<TAB>child[..]]]\n"
4817 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4818 "@*@*"
4819 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4820 "argument is raw @abbr{XML} data. The content is created as a child of "
4821 "the element path specified with the @option{--root} option or at the "
4822 "document root when not specified. Existing elements of the same name will "
4823 "be overwritten."
4824 "@*@*"
4825 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4826 "for details."
4829 new_command("DUMP", 0, 1, 0, dump_command, _(
4830 "DUMP\n"
4831 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4832 "dumping a specific node."
4835 new_command("LOCK", 0, 0, 0, lock_command, _(
4836 "LOCK\n"
4837 "Locks the mutex associated with the opened file. This prevents other clients "
4838 "from sending commands to the same opened file until the client "
4839 "that sent this command either disconnects or sends the @code{UNLOCK} "
4840 "command. @xref{UNLOCK}."
4843 new_command("UNLOCK", 1, 0, 0, unlock_command, _(
4844 "UNLOCK\n"
4845 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4846 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4847 "@pxref{ISCACHED})."
4850 new_command("GETCONFIG", 1, 1, 0, getconfig_command, _(
4851 "GETCONFIG [filename] <parameter>\n"
4852 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4853 "data response. If no file has been opened then the value for @var{filename} "
4854 "or the default from the @var{global} section will be returned. If a file "
4855 "has been opened and no @var{filename} is specified, the value previously "
4856 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4859 new_command("OPTION", 1, 1, 0, option_command, _(
4860 "OPTION <NAME>=[<VALUE>]\n"
4861 "Sets a client option @var{name} to @var{value}. The value for an option is "
4862 "kept for the duration of the connection with the exception of the "
4863 "@command{pinentry} options which are defaults for all future connections "
4864 "(@pxref{Pinentry}). When @var{value} is empty the option is unset."
4865 "@*@*"
4866 "@table @asis\n"
4867 "@item DISABLE-PINENTRY\n"
4868 "Disable use of @command{pinentry} for passphrase retrieval. When @code{1}, a "
4869 "server inquire is sent to the client to obtain the passphrase. This option "
4870 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4871 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands. Set to @code{0} "
4872 "to use a @command{pinentry}."
4873 "@*@*"
4874 "@item DISPLAY\n"
4875 "Set or unset the X11 display to use when prompting for a passphrase."
4876 "@*@*"
4877 "@item TTYNAME\n"
4878 "Set the terminal device path to use when prompting for a passphrase."
4879 "@*@*"
4880 "@item TTYTYPE\n"
4881 "Set the terminal type for use with @option{TTYNAME}."
4882 "@*@*"
4883 "@item NAME\n"
4884 "Associates the thread ID of the connection with the specified textual "
4885 "representation. Useful for debugging log messages. May not contain whitespace."
4886 "@*@*"
4887 "@item LOCK-TIMEOUT\n"
4888 "When not @code{0}, the duration in tenths of a second to wait for the file "
4889 "mutex which has been locked by another thread to be released before returning "
4890 "an error. When @code{-1} the error will be returned immediately."
4891 "@*@*"
4892 "@item CLIENT-STATE\n"
4893 "When set to @code{1} then client state status messages for other clients are "
4894 "sent to the current client. The default is @code{0}."
4895 "@end table\n"
4898 new_command("LS", 1, 1, 0, ls_command, _(
4899 "LS\n"
4900 "Returns a newline separated list of data files stored in the data directory "
4901 "@file{HOMEDIR/data} (@pxref{Invoking}) with a data response."
4904 new_command("RESET", 1, 1, 0, NULL, _(
4905 "RESET\n"
4906 "Closes the currently opened file but keeps any previously set client options "
4907 "(@pxref{OPTION})."
4910 new_command("NOP", 1, 1, 0, NULL, _(
4911 "NOP\n"
4912 "Does nothing. Always returns successfully."
4915 /* !END-HELP-TEXT! */
4916 new_command ("CANCEL", 1, 1, 0, NULL, NULL);
4917 new_command ("END", 1, 1, 0, NULL, NULL);
4918 new_command ("BYE", 1, 1, 0, NULL, NULL);
4920 int i;
4921 for (i = 0; command_table[i]; i++);
4922 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4923 sort_commands);
4926 gpg_error_t
4927 register_commands (assuan_context_t ctx)
4929 int i = 0, rc;
4931 for (; command_table[i]; i++)
4933 if (!command_table[i]->handler)
4934 continue;
4936 rc = assuan_register_command (ctx, command_table[i]->name,
4937 command_table[i]->handler,
4938 command_table[i]->help);
4939 if (rc)
4940 return rc;
4943 rc = assuan_register_bye_notify (ctx, bye_notify);
4944 if (rc)
4945 return rc;
4947 rc = assuan_register_reset_notify (ctx, reset_notify);
4948 if (rc)
4949 return rc;
4951 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4952 if (rc)
4953 return rc;
4955 return assuan_register_post_cmd_notify (ctx, command_finalize);