Add STATUS_STATE to indicate client state change.
[libpwmd.git] / src / commands.c
blobde46465c7e171d6b1e97444bb4dfe07586e51df5
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of pwmd.
7 Pwmd is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 Pwmd is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <err.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <ctype.h>
33 #include <dirent.h>
34 #include <pthread.h>
35 #include <stdint.h>
37 #include "pwmd-error.h"
38 #include <gcrypt.h>
40 #include "mem.h"
41 #include "xml.h"
42 #include "util-misc.h"
43 #include "common.h"
44 #include "rcfile.h"
45 #include "cache.h"
46 #include "commands.h"
47 #include "mutex.h"
48 #include "crypto.h"
50 /* Flags needed to be retained for a client across commands. */
51 #define FLAG_NEW 0x0001
52 #define FLAG_HAS_LOCK 0x0002
53 #define FLAG_LOCK_CMD 0x0004
54 #define FLAG_NO_PINENTRY 0x0008
55 #define FLAG_OPEN 0x0010
56 #define FLAG_KEEP_LOCK 0x0020
58 /* These are command option flags. */
59 #define OPT_INQUIRE 0x0001
60 #define OPT_NO_PASSPHRASE 0x0002
61 #define OPT_RESET 0x0004
62 #define OPT_LIST_RECURSE 0x0008
63 #define OPT_VERBOSE 0x0010
64 #define OPT_LOCK 0x0020
65 #define OPT_LOCK_ON_OPEN 0x0040
66 #define OPT_SIGN 0x0080
67 #define OPT_LIST_ALL 0x0100
68 #define OPT_LIST_WITH_TARGET 0x0200
69 #define OPT_DATA 0x0400
70 #define OPT_NO_AGENT 0x0800
71 #define OPT_SECRET_ONLY 0x1000
72 #define OPT_INQUIRE_KEYID 0x2000
73 #define OPT_INQUIRE_SIGN_KEYID 0x4000
75 struct command_table_s
77 const char *name;
78 gpg_error_t (*handler) (assuan_context_t, char *line);
79 const char *help;
80 int ignore_startup;
81 int unlock; // unlock the file mutex after validating the checksum
84 static struct command_table_s **command_table;
86 static gpg_error_t do_lock (struct client_s *client, int add);
87 static gpg_error_t validate_checksum (struct client_s *,
88 struct cache_data_s *);
89 static gpg_error_t update_checksum (struct client_s *client);
91 void
92 update_client_state (struct client_s *client, unsigned s)
94 client->thd->state = s;
96 if (config_get_boolean ("global", "send_state"))
97 send_status_all_not_self (STATUS_STATE, "%p %u", client->thd->tid,
98 client->thd->state);
101 static gpg_error_t
102 unlock_file_mutex (struct client_s *client, int remove)
104 gpg_error_t rc = 0;
106 // OPEN: keep the lock for the same file being reopened.
107 if (client->flags & FLAG_KEEP_LOCK && client->flags & FLAG_HAS_LOCK)
108 return 0;
110 if (!(client->flags & FLAG_HAS_LOCK))
111 return GPG_ERR_NOT_LOCKED;
113 rc = cache_unlock_mutex (client->md5file, remove);
114 if (rc)
115 rc = GPG_ERR_INV_STATE;
116 else
117 client->flags &= ~(FLAG_HAS_LOCK | FLAG_LOCK_CMD);
119 return rc;
122 static gpg_error_t
123 lock_file_mutex (struct client_s *client, int add)
125 gpg_error_t rc = 0;
126 int timeout = config_get_integer (client->filename, "cache_timeout");
128 if (client->flags & FLAG_HAS_LOCK)
129 return 0;
131 rc = cache_lock_mutex (client->ctx, client->md5file,
132 client->lock_timeout, add, timeout);
133 if (!rc)
134 client->flags |= FLAG_HAS_LOCK;
136 return rc;
139 static gpg_error_t
140 file_modified (struct client_s *client, struct command_table_s *cmd)
142 gpg_error_t rc = 0;
144 if (!(client->flags & FLAG_OPEN))
145 return GPG_ERR_INV_STATE;
147 rc = lock_file_mutex (client, 0);
148 if (!rc || rc == GPG_ERR_NO_DATA)
150 rc = validate_checksum (client, NULL);
151 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
152 rc = 0;
153 else if (rc)
154 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
157 if ((cmd->unlock && !(client->flags & FLAG_HAS_LOCK)) || rc)
158 unlock_file_mutex (client, 0);
160 return rc;
163 static gpg_error_t
164 parse_xml (assuan_context_t ctx, int new)
166 struct client_s *client = assuan_get_pointer (ctx);
167 int cached = client->doc != NULL;
168 gpg_error_t rc = 0;
170 if (new)
172 client->doc = new_document ();
173 if (client->doc)
175 xmlChar *result;
176 int len;
178 xmlDocDumpFormatMemory (client->doc, &result, &len, 0);
179 client->crypto->plaintext = result;
180 client->crypto->plaintext_size = len;
181 if (!client->crypto->plaintext)
183 xmlFreeDoc (client->doc);
184 client->doc = NULL;
185 rc = GPG_ERR_ENOMEM;
189 else if (!cached)
190 rc = parse_doc ((char *) client->crypto->plaintext,
191 client->crypto->plaintext_size, (xmlDocPtr *)&client->doc);
193 return rc;
196 static void
197 free_client (struct client_s *client)
199 if (client->doc)
200 xmlFreeDoc (client->doc);
202 xfree (client->crc);
203 xfree (client->filename);
204 xfree (client->last_error);
205 crypto_cleanup (client->crypto);
206 client->crypto = NULL;
209 void
210 cleanup_client (struct client_s *client)
212 assuan_context_t ctx = client->ctx;
213 struct client_thread_s *thd = client->thd;
214 long lock_timeout = client->lock_timeout;
215 xmlErrorPtr xml_error = client->xml_error;
216 int no_pinentry = (client->flags & FLAG_NO_PINENTRY);
218 unlock_file_mutex (client, client->flags & FLAG_NEW);
219 free_client (client);
220 memset (client, 0, sizeof (struct client_s));
221 client->xml_error = xml_error;
222 client->ctx = ctx;
223 client->thd = thd;
224 client->lock_timeout = lock_timeout;
225 client->flags |= no_pinentry ? FLAG_NO_PINENTRY : 0;
228 static void
229 req_cleanup (void *arg)
231 if (!arg)
232 return;
234 strv_free ((char **) arg);
237 static gpg_error_t
238 parse_open_opt_lock (void *data, void *value)
240 struct client_s *client = data;
242 client->opts |= OPT_LOCK_ON_OPEN;
243 return 0;
246 static gpg_error_t
247 parse_opt_inquire (void *data, void *value)
249 struct client_s *client = data;
251 (void) value;
252 client->opts |= OPT_INQUIRE;
253 return 0;
256 static gpg_error_t
257 update_checksum (struct client_s *client)
259 unsigned char *crc;
260 size_t len;
261 struct cache_data_s *cdata;
262 gpg_error_t rc = get_checksum (client->filename, &crc, &len);
264 if (rc)
265 return rc;
267 xfree (client->crc);
268 client->crc = crc;
269 cdata = cache_get_data (client->md5file);
270 if (cdata)
272 xfree (cdata->crc);
273 cdata->crc = xmalloc (len);
274 memcpy (cdata->crc, crc, len);
277 return 0;
280 static gpg_error_t
281 validate_checksum (struct client_s *client, struct cache_data_s *cdata)
283 unsigned char *crc;
284 size_t len;
285 gpg_error_t rc;
286 int n = 0;
288 if (cdata && !cdata->crc)
289 return GPG_ERR_CHECKSUM;
291 rc = get_checksum (client->filename, &crc, &len);
292 if (rc)
293 return rc;
295 if (cdata)
296 n = memcmp (cdata->crc, crc, len);
297 else if (client->crc)
298 n = memcmp (client->crc, crc, len);
300 xfree (crc);
301 return n ? GPG_ERR_CHECKSUM : 0;
304 static gpg_error_t
305 open_command (assuan_context_t ctx, char *line)
307 gpg_error_t rc;
308 struct client_s *client = assuan_get_pointer (ctx);
309 char **req, *filename;
310 unsigned char md5file[16];
311 int same_file = 0;
312 assuan_peercred_t peer;
313 struct cache_data_s *cdata = NULL;
314 int cached = 0;
315 struct argv_s *args[] = {
316 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_open_opt_lock},
317 NULL
320 rc = parse_options (&line, args, client);
321 if (rc)
322 return send_error (ctx, rc);
324 req = str_split (line, " ", 2);
325 if (!req)
326 return send_error (ctx, GPG_ERR_SYNTAX);
328 rc = do_validate_peer (ctx, req[0], &peer);
329 if (rc)
331 strv_free (req);
332 return send_error (ctx, rc);
335 filename = req[0];
336 if (!valid_filename (filename))
338 strv_free (req);
339 return send_error (ctx, GPG_ERR_INV_VALUE);
342 pthread_cleanup_push (req_cleanup, req);
343 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, filename, strlen (filename));
344 /* This client may have locked a different file with ISCACHED --lock than
345 * the current filename. This will remove that lock. */
346 same_file = !memcmp (md5file, client->md5file, 16) ? 1 : 0;
347 if (client->flags & FLAG_OPEN ||
348 (client->flags & FLAG_HAS_LOCK && !same_file))
350 uint32_t opts = client->opts;
351 uint32_t flags = client->flags;
353 if (same_file)
354 client->flags |= FLAG_KEEP_LOCK;
355 else if (client->flags & FLAG_NEW)
356 cache_clear (client->md5file);
358 cleanup_client (client);
359 client->opts = opts;
360 client->flags |= flags;
361 client->flags &= ~(FLAG_LOCK_CMD);
362 if (!same_file)
363 client->flags &= ~(FLAG_HAS_LOCK | FLAG_NEW);
366 memcpy (client->md5file, md5file, 16);
367 client->filename = str_dup (filename);
368 if (!client->filename)
370 strv_free (req);
371 return send_error(ctx, GPG_ERR_ENOMEM);
374 /* Need to lock the mutex here because file_modified() cannot without
375 * knowing the filename. */
376 rc = lock_file_mutex (client, 1);
377 if (rc)
378 client->flags &= ~FLAG_OPEN;
380 if (!rc)
382 struct stat st;
384 rc = crypto_init (&client->crypto, client->ctx, client->filename,
385 client->flags & FLAG_NO_PINENTRY);
386 if (!rc && stat (client->filename, &st) == -1)
387 rc = gpg_error_from_errno (errno);
390 if (!rc || gpg_err_code (rc) == GPG_ERR_ENOENT)
392 cdata = cache_get_data (client->md5file);
394 if (rc) // new file
396 rc = 0;
397 client->flags |= FLAG_NEW;
398 // data file disappeared. clear the cache entry.
399 cache_clear (client->md5file);
400 cdata = NULL;
402 else if (cdata && cdata->doc) // cached document
404 int reload = 0;
406 if (!rc && !(client->flags & FLAG_NEW))
408 rc = validate_checksum (client, cdata);
409 if (rc == GPG_ERR_CHECKSUM)
411 rc = 0;
412 reload = 1;
416 #ifdef WITH_GNUTLS
417 if (!rc && client->thd->remote
418 && config_get_boolean (client->filename, "tcp_require_key")
419 && !(client->flags & FLAG_NEW))
421 rc = GPG_ERR_KEY_EXPIRED;
422 reload = 1;
424 #endif
425 if (reload) // GPG_ERR_CHECKSUM or GPG_ERR_KEY_EXPIRED
427 log_write ("%s: %s", client->filename,
428 pwmd_strerror (rc ? rc : GPG_ERR_CHECKSUM));
429 cache_clear (client->md5file);
430 cdata = NULL;
431 rc = crypto_decrypt (client, client->crypto);
434 if (!rc && cdata)
436 client->crypto->plaintext = cdata->doc;
437 client->crypto->plaintext_size = cdata->size;
438 rc = cache_decrypt (client->crypto);
439 if (!rc)
441 cdata->doc = NULL;
442 cdata->size = 0;
443 strv_free (client->crypto->pubkey);
444 strv_free (client->crypto->sigkey);
445 client->crypto->pubkey = strv_dup (cdata->pubkey);
446 client->crypto->sigkey = strv_dup (cdata->sigkey);
447 cached = 1;
449 else
451 client->crypto->plaintext = NULL;
452 client->crypto->plaintext_size = 0;
456 else // existing file
458 cached = cdata != NULL;
459 rc = crypto_decrypt (client, client->crypto);
463 if (!rc)
465 rc = parse_xml (ctx, client->flags & FLAG_NEW);
466 if (!rc)
468 rc = cache_encrypt (client->crypto);
469 if (rc)
470 cache_clear (client->md5file);
471 else
473 int timeout = config_get_integer (client->filename,
474 "cache_timeout");
476 free_cache_data_once (cdata);
477 cdata = xcalloc (1, sizeof (struct cache_data_s));
478 cdata->doc = client->crypto->plaintext;
479 cdata->size = client->crypto->plaintext_size;
480 cdata->pubkey = strv_dup(client->crypto->pubkey);
481 cdata->sigkey = strv_dup(client->crypto->sigkey);
482 client->crypto->plaintext = NULL;
483 client->crypto->plaintext_size = 0;
485 if (cached) // wont increment the refcount
487 rc = cache_set_data (client->md5file, cdata);
488 /* The cache entry may have been removed and cache_set_data()
489 * already sent STATUS_CACHE. */
490 if (!cache_iscached (client->filename, NULL))
491 rc = send_status (ctx, STATUS_CACHE, NULL);
493 else
494 rc = cache_add_file (client->md5file, cdata, timeout);
499 pthread_cleanup_pop (1);
501 if (!rc)
503 client->flags |= FLAG_OPEN;
504 update_checksum (client);
506 if (client->flags & FLAG_NEW)
507 rc = send_status (ctx, STATUS_NEWFILE, NULL);
509 if (!rc && (client->opts & OPT_LOCK_ON_OPEN))
510 rc = do_lock (client, 0);
512 else
514 log_write ("%s: %s", client->filename, pwmd_strerror (rc));
516 if (client->flags & FLAG_NEW)
517 cache_clear (client->md5file);
519 crypto_cleanup (client->crypto);
520 client->crypto = NULL;
521 client->flags &= ~FLAG_OPEN;
524 crypto_cleanup_non_keys (client->crypto);
525 return send_error (ctx, rc);
528 static gpg_error_t
529 parse_opt_no_passphrase (void *data, void *value)
531 struct client_s *client = data;
533 (void) value;
534 client->opts |= OPT_NO_PASSPHRASE;
535 return 0;
538 /* If not the invoking_user or new file, check that the list of user supplied
539 * keyid's are in the list of current keyid's obtained when decrypting the data
540 * file. Both short and long form keyid fingerprints are valid:
542 * 8 byte with optional "0x" prefix
543 * 16 byte
544 * 40 byte
546 * Only key fingerprints are compared and not user id's when not the
547 * invoking_user.
549 static gpg_error_t
550 permitted_to_save (struct client_s *client, const char **keys,
551 const char **value)
553 gpg_error_t rc = 0;
554 const char **v;
556 if (client->flags & FLAG_NEW)
557 return 0;
559 rc = peer_is_invoker (client);
560 if (!rc)
561 return 0;
563 for (v = value; v && *v; v++)
565 const char **k, *pv = *v;
566 int match = 0;
568 if (*pv == '0' && *(pv+1) == 'x')
569 pv += 2;
571 for (k = keys; k && *k; k++)
573 const char *pk = *k;
575 if (*pk == '0' && *(pk+1) == 'x')
576 pk += 2;
578 rc = !strcmp (pv, pk) ? 0 : GPG_ERR_EACCES;
579 if (rc)
581 size_t len = strlen (*k);
582 const char *p = *k;
584 p += len == 16 ? 8 : 32;
585 rc = !strcmp (pv, p) ? 0 : GPG_ERR_EACCES;
588 if (!rc)
590 match = 1;
591 break;
595 if (!match)
596 return GPG_ERR_EACCES;
599 return rc;
602 static gpg_error_t
603 parse_save_opt_keyid_common (struct client_s *client, const char **list,
604 const char *value, char ***dst)
606 gpg_error_t rc;
607 char **p;
609 if (!value || !*value)
610 return GPG_ERR_INV_VALUE;
612 p = str_split (value, ",", 0);
613 if (!p)
614 return GPG_ERR_ENOMEM;
616 rc = permitted_to_save (client, list, (const char **)p);
617 if (!rc)
618 *dst = p;
619 else
620 strv_free (*dst);
622 return rc;
625 static gpg_error_t
626 parse_save_opt_keyid (void *data, void *value)
628 struct client_s *client = data;
629 const char *str = value;
630 char **dst = NULL;
631 gpg_error_t rc;
633 rc = parse_save_opt_keyid_common (client,
634 (const char **)client->crypto->pubkey,
635 str, &dst);
636 if (rc)
637 return rc;
639 client->crypto->save.pubkey = dst;
640 return 0;
643 static gpg_error_t
644 parse_save_opt_sign_keyid (void *data, void *value)
646 struct client_s *client = data;
647 const char *str = value;
648 char **dst = NULL;
649 gpg_error_t rc;
651 rc = parse_save_opt_keyid_common (client,
652 (const char **)client->crypto->sigkey,
653 str, &dst);
654 if (rc)
655 return rc;
657 client->crypto->save.sigkey = dst;
658 return 0;
661 static gpg_error_t
662 parse_save_opt_inquire (struct client_s *client, uint32_t opt)
664 if (opt == OPT_INQUIRE && !(client->flags & FLAG_NEW))
666 gpg_error_t rc = peer_is_invoker (client);
668 if (rc)
669 return rc;
672 client->opts |= opt;
673 return 0;
676 static gpg_error_t
677 parse_save_opt_inquire_keyparam (void *data, void *value)
679 return parse_save_opt_inquire (data, OPT_INQUIRE);
682 static gpg_error_t
683 parse_save_opt_inquire_keyid (void *data, void *value)
685 return parse_save_opt_inquire (data, OPT_INQUIRE_KEYID);
688 static gpg_error_t
689 parse_save_opt_inquire_sign_keyid (void *data, void *value)
691 return parse_save_opt_inquire (data, OPT_INQUIRE_SIGN_KEYID);
694 /* Tests that the keys in new_keys are also in old_keys. */
695 static gpg_error_t
696 compare_keys (char **new_keys, char **old_keys)
698 char **o;
700 if (!new_keys || !*new_keys)
701 return 0;
703 if (!old_keys || !*old_keys)
704 return 0;
706 for (o = old_keys; *o; o++)
708 char **n;
710 for (n = new_keys; *n; n++)
712 if (!strcmp (*n, *o))
713 break;
716 if (!*n)
717 return GPG_ERR_NOT_FOUND;
720 return 0;
723 static gpg_error_t
724 inquire_keyid (struct client_s *client, uint32_t opt)
726 gpg_error_t rc;
727 unsigned char *result = NULL;
728 size_t len;
729 char *s;
730 char ***orig;
731 char ***save;
733 if (opt == OPT_INQUIRE_KEYID)
735 s = "INQUIRE_KEYID";
736 orig = &client->crypto->pubkey;
737 save = &client->crypto->save.pubkey;
739 else
741 s = "INQUIRE_SIGN_KEYID";
742 orig = &client->crypto->sigkey;
743 save = &client->crypto->save.sigkey;
746 rc = assuan_inquire (client->ctx, s, &result, &len, 0);
747 if (!rc)
749 char **dst = NULL;
751 rc = parse_save_opt_keyid_common (client, (const char **)*orig,
752 (char *)result, &dst);
753 if (!rc)
754 *save = dst;
757 xfree (result);
758 return rc;
761 static gpg_error_t
762 save_command (assuan_context_t ctx, char *line)
764 struct client_s *client = assuan_get_pointer (ctx);
765 struct cache_data_s *cdata = NULL;
766 gpg_error_t rc, cached = 0;
767 int defer = 0;
768 struct stat st;
769 struct argv_s *args[] = {
770 &(struct argv_s) {"keyid", OPTION_TYPE_ARG, parse_save_opt_keyid},
771 &(struct argv_s) {"sign-keyid", OPTION_TYPE_ARG, parse_save_opt_sign_keyid},
772 &(struct argv_s) {"inquire-keyparam", OPTION_TYPE_NOARG,
773 parse_save_opt_inquire_keyparam },
774 &(struct argv_s) {"inquire-keyid", OPTION_TYPE_NOARG,
775 parse_save_opt_inquire_keyid },
776 &(struct argv_s) {"inquire-sign-keyid", OPTION_TYPE_NOARG,
777 parse_save_opt_inquire_sign_keyid },
778 NULL
781 rc = parse_options (&line, args, client);
782 if (rc)
783 return send_error (ctx, rc);
785 if (lstat (client->filename, &st) == -1 && errno != ENOENT)
786 return send_error (ctx, gpg_error_from_errno (errno));
788 if (errno != ENOENT && !S_ISREG (st.st_mode))
790 log_write ("%s: %s", client->filename, pwmd_strerror (GPG_ERR_ENOANO));
791 return send_error (ctx, GPG_ERR_ENOANO);
794 cached = rc = cache_iscached (client->filename, &defer);
795 if (gpg_err_code (rc) == GPG_ERR_ENOENT && (client->flags & FLAG_NEW))
796 rc = 0;
797 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
798 rc = 0;
800 if (!rc)
801 rc = update_element_mtime (client, xmlDocGetRootElement (client->doc));
803 if (rc)
804 return send_error (ctx, rc);
806 rc = crypto_init_ctx (client->crypto, client->flags & FLAG_NO_PINENTRY);
807 if (rc)
808 return send_error (ctx, rc);
810 if (((client->opts & OPT_INQUIRE_KEYID)
811 || (client->opts & OPT_INQUIRE_SIGN_KEYID)))
813 if (client->opts & OPT_INQUIRE)
814 return send_error (ctx, GPG_ERR_CONFLICT);
816 if (client->opts & OPT_INQUIRE_KEYID)
817 rc = inquire_keyid (client, OPT_INQUIRE_KEYID);
819 if (!rc && (client->opts & OPT_INQUIRE_SIGN_KEYID))
820 rc = inquire_keyid (client, OPT_INQUIRE_SIGN_KEYID);
822 else if ((client->crypto->save.pubkey || client->crypto->save.sigkey)
823 && client->opts & OPT_INQUIRE)
824 return send_error (ctx, GPG_ERR_CONFLICT);
826 if (!rc && (client->opts & OPT_INQUIRE))
828 /* Require a passphrase when generating a new key pair for an existing
829 * file.
831 if (!(client->flags & FLAG_NEW))
832 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
834 if (!rc)
836 unsigned char *params = NULL;
837 size_t len;
839 rc = assuan_inquire (client->ctx, "KEYPARAM", &params, &len, 0);
840 if (!rc)
842 pthread_cleanup_push (xfree, params);
843 rc = crypto_genkey (client, client->crypto, params);
844 pthread_cleanup_pop (1);
848 else if (!rc && (client->flags & FLAG_NEW))
850 if (!client->crypto->save.pubkey && !client->crypto->save.sigkey)
852 char *params = crypto_default_key_params ();
854 if (!params)
855 rc = GPG_ERR_ENOMEM;
857 if (!rc)
859 pthread_cleanup_push (xfree, params);
860 rc = crypto_genkey (client, client->crypto,
861 (unsigned char *)params);
862 pthread_cleanup_pop (1);
865 else
867 if (!client->crypto->save.pubkey && client->crypto->save.sigkey)
868 client->crypto->save.pubkey = strv_dup (client->crypto->save.sigkey);
870 if (client->crypto->save.pubkey && !client->crypto->save.sigkey)
871 client->crypto->save.sigkey = strv_dup (client->crypto->save.pubkey);
874 else if (!rc)
876 cdata = cache_get_data (client->md5file);
877 if (cdata)
879 rc = compare_keys (client->crypto->save.pubkey, cdata->pubkey);
880 if (!rc)
881 rc = compare_keys (client->crypto->save.sigkey, cdata->sigkey);
883 /* Prevent saving to a recipient who is not in the original recipient
884 * list without a passphrase. */
885 if (rc)
886 rc = crypto_try_decrypt (client, client->flags & FLAG_NO_PINENTRY);
888 if (!rc)
890 if (!client->crypto->save.pubkey)
891 client->crypto->save.pubkey = strv_dup (cdata->pubkey);
893 if (!client->crypto->save.sigkey && cdata->sigkey)
894 client->crypto->save.sigkey = strv_dup (cdata->sigkey);
895 else if (!client->crypto->save.sigkey)
896 client->crypto->save.sigkey = strv_dup (cdata->pubkey);
899 else
901 client->crypto->save.pubkey = strv_dup (client->crypto->pubkey);
902 client->crypto->save.sigkey = strv_dup (client->crypto->sigkey);
906 if (!rc)
908 int size;
910 xmlDocDumpFormatMemory (client->doc, &client->crypto->plaintext, &size,
912 if (size > 0)
913 client->crypto->plaintext_size = (size_t) size;
914 else
915 rc = GPG_ERR_ENOMEM;
917 rc = crypto_encrypt (client, client->crypto);
918 if (!rc)
920 rc = crypto_write_file (client->crypto);
921 if (!rc)
923 if (!cached) // no error
924 cdata = cache_get_data (client->md5file);
927 if (!rc)
929 rc = cache_encrypt (client->crypto);
930 if (rc)
932 cache_clear (client->md5file);
933 strv_free (client->crypto->pubkey);
934 client->crypto->pubkey = strv_dup (client->crypto->save.pubkey);
935 strv_free (client->crypto->sigkey);
936 client->crypto->sigkey = strv_dup (client->crypto->save.sigkey);
937 client->flags &= ~(FLAG_NEW);
941 if (!rc)
943 int timeout = config_get_integer (client->filename,
944 "cache_timeout");
946 free_cache_data_once (cdata);
947 cdata = xcalloc (1, sizeof (struct cache_data_s));
948 cdata->doc = client->crypto->plaintext;
949 client->crypto->plaintext = NULL;
950 cdata->size = client->crypto->plaintext_size;
951 client->crypto->plaintext_size = 0;
952 cdata->pubkey = client->crypto->save.pubkey;
953 client->crypto->save.pubkey = NULL;
954 cdata->sigkey = client->crypto->save.sigkey;
955 client->crypto->save.sigkey = NULL;
957 /* Update in case the cache entry expires the next SAVE may not
958 * have any known keys. */
959 strv_free (client->crypto->pubkey);
960 client->crypto->pubkey = strv_dup (cdata->pubkey);
961 strv_free (client->crypto->sigkey);
962 client->crypto->sigkey = strv_dup (cdata->sigkey);
964 if (!cached) // no error and wont increment refcount
965 rc = cache_set_data (client->md5file, cdata);
966 else
967 rc = cache_add_file (client->md5file, cdata, timeout);
969 if (!rc && (cached || (client->flags & FLAG_NEW)))
970 send_status_all (STATUS_CACHE, NULL);
972 if (!rc)
974 rc = update_checksum (client);
975 client->flags &= ~(FLAG_NEW);
981 crypto_cleanup_non_keys (client->crypto);
982 return send_error (ctx, rc);
985 static gpg_error_t
986 do_delete (assuan_context_t ctx, char *line)
988 struct client_s *client = assuan_get_pointer (ctx);
989 gpg_error_t rc;
990 char **req;
991 xmlNodePtr n;
993 if (strchr (line, '\t'))
994 req = str_split (line, "\t", 0);
995 else
996 req = str_split (line, " ", 0);
998 if (!req || !*req)
999 return GPG_ERR_SYNTAX;
1001 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1002 if (!n)
1004 strv_free (req);
1005 return rc;
1009 * No sub-node defined. Remove the entire node (root element).
1011 if (!req[1])
1013 if (n)
1015 rc = is_element_owner (client, n);
1016 if (!rc)
1018 rc = unlink_node (client, n);
1019 xmlFreeNode (n);
1023 strv_free (req);
1024 return rc;
1027 n = find_elements (client, client->doc, n->children, req + 1, &rc, NULL,
1028 NULL, NULL, 0, 0, NULL, 0);
1029 strv_free (req);
1030 if (!n)
1031 return rc;
1033 rc = is_element_owner (client, n);
1034 if (!rc)
1036 rc = unlink_node (client, n);
1037 xmlFreeNode (n);
1040 return rc;
1043 static gpg_error_t
1044 delete_command (assuan_context_t ctx, char *line)
1046 struct client_s *client = assuan_get_pointer (ctx);
1047 gpg_error_t rc;
1048 struct argv_s *args[] = {
1049 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1050 NULL
1053 rc = parse_options (&line, args, client);
1054 if (rc)
1055 return send_error (ctx, rc);
1057 if (client->opts & OPT_INQUIRE)
1059 unsigned char *result;
1060 size_t len;
1062 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1063 if (rc)
1064 return send_error (ctx, rc);
1066 pthread_cleanup_push (xfree, result);
1067 rc = do_delete (ctx, (char *)result);
1068 pthread_cleanup_pop (1);
1070 else
1071 rc = do_delete (ctx, line);
1073 return send_error (ctx, rc);
1076 static gpg_error_t
1077 store_command (assuan_context_t ctx, char *line)
1079 struct client_s *client = assuan_get_pointer (ctx);
1080 gpg_error_t rc;
1081 size_t len;
1082 unsigned char *result;
1083 char **req;
1084 xmlNodePtr n, parent;
1085 int has_content;
1086 char *content = NULL;
1088 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1089 if (rc)
1090 return send_error (ctx, rc);
1092 req = str_split ((char *) result, "\t", 0);
1093 xfree (result);
1095 if (!req || !*req)
1096 return send_error (ctx, GPG_ERR_SYNTAX);
1098 len = strv_length (req);
1099 has_content = line[strlen (line) - 1] != '\t' && len > 1;
1100 if (*(req + 1) && !valid_element_path (req, has_content))
1102 strv_free (req);
1103 return send_error (ctx, GPG_ERR_INV_VALUE);
1106 if (has_content || !*req[len - 1])
1108 has_content = 1;
1109 content = req[len - 1];
1110 req[len - 1] = NULL;
1113 again:
1114 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1115 if (rc && rc == GPG_ERR_ELEMENT_NOT_FOUND)
1117 rc = new_root_element (client, client->doc, *req);
1118 if (rc)
1120 strv_free (req);
1121 return send_error (ctx, rc);
1124 goto again;
1127 if (!n)
1129 strv_free (req);
1130 return send_error (ctx, rc);
1133 parent = n;
1135 if (req[1] && *req[1])
1137 if (!n->children)
1138 parent = create_elements_cb (client, 1, n, req + 1, &rc, NULL);
1139 else
1140 parent = find_elements (client, client->doc, n->children, req + 1, &rc,
1141 NULL, NULL, create_elements_cb, 0, 0, NULL,
1145 if (!rc && len > 1)
1147 rc = is_element_owner (client, parent);
1148 if (!rc)
1150 n = find_text_node (parent->children);
1151 if (n)
1152 xmlNodeSetContent (n, (xmlChar *) content);
1153 else
1154 xmlNodeAddContent (parent, (xmlChar *) content);
1156 update_element_mtime (client, parent);
1160 xfree (content);
1161 strv_free (req);
1162 return send_error (ctx, rc);
1165 static gpg_error_t
1166 xfer_data (assuan_context_t ctx, const char *line, int total)
1168 int to_send;
1169 int sent = 0;
1170 gpg_error_t rc;
1171 int progress = config_get_integer ("global", "xfer_progress");
1172 int flush = 0;
1174 progress =
1175 progress > 0 ? (progress / ASSUAN_LINELENGTH) * ASSUAN_LINELENGTH : 0;
1176 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1177 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1179 if (rc)
1180 return rc;
1182 again:
1185 if (sent + to_send > total)
1186 to_send = total - sent;
1188 rc = assuan_send_data (ctx, flush ? NULL : (char *) line + sent,
1189 flush ? 0 : to_send);
1190 if (!rc)
1192 sent += flush ? 0 : to_send;
1194 if ((progress && !(sent % progress) && sent != total) ||
1195 (sent == total && flush))
1196 rc = send_status (ctx, STATUS_XFER, "%li %li", sent, total);
1198 if (!flush && !rc && sent == total)
1200 flush = 1;
1201 goto again;
1205 while (!rc && sent < total);
1207 return rc;
1210 static gpg_error_t
1211 do_get (assuan_context_t ctx, char *line)
1213 struct client_s *client = assuan_get_pointer (ctx);
1214 gpg_error_t rc;
1215 char **req;
1216 xmlNodePtr n;
1218 req = str_split (line, "\t", 0);
1220 if (!req || !*req)
1222 strv_free (req);
1223 return GPG_ERR_SYNTAX;
1226 n = find_root_element (client, client->doc, &req, &rc, NULL, 0, 0);
1227 if (!n)
1229 strv_free (req);
1230 return rc;
1233 if (req[1])
1235 find_elements (client, client->doc, n->children, req + 1, &rc, NULL, NULL, NULL,
1236 0, 0, NULL, 0);
1238 strv_free (req);
1239 if (rc)
1240 return rc;
1242 if (!n || !n->children)
1243 return GPG_ERR_NO_DATA;
1245 n = find_text_node (n->children);
1246 if (!n || !n->content || !*n->content)
1247 return GPG_ERR_NO_DATA;
1249 rc = xfer_data (ctx, (char *) n->content, xmlStrlen (n->content));
1250 return rc;
1253 static gpg_error_t
1254 get_command (assuan_context_t ctx, char *line)
1256 struct client_s *client = assuan_get_pointer (ctx);
1257 gpg_error_t rc;
1258 struct argv_s *args[] = {
1259 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1260 NULL
1263 rc = parse_options (&line, args, client);
1264 if (rc)
1265 return send_error (ctx, rc);
1267 if (client->opts & OPT_INQUIRE)
1269 unsigned char *result;
1270 size_t len;
1272 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1273 if (rc)
1274 return send_error (ctx, rc);
1276 pthread_cleanup_push (xfree, result);
1277 rc = do_get (ctx, (char *)result);
1278 pthread_cleanup_pop (1);
1280 else
1281 rc = do_get (ctx, line);
1283 return send_error (ctx, rc);
1286 static void list_command_cleanup1 (void *arg);
1287 static gpg_error_t
1288 realpath_command (assuan_context_t ctx, char *line)
1290 struct string_s *string = NULL;
1291 gpg_error_t rc;
1292 struct client_s *client = assuan_get_pointer (ctx);
1293 struct argv_s *args[] = {
1294 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1295 NULL
1298 rc = parse_options (&line, args, client);
1299 if (rc)
1300 return send_error (ctx, rc);
1302 if (client->opts & OPT_INQUIRE)
1304 unsigned char *result;
1305 size_t len;
1307 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1308 if (rc)
1309 return send_error (ctx, rc);
1311 pthread_cleanup_push (xfree, result);
1312 rc = build_realpath (client, client->doc, (char *)result, &string);
1313 pthread_cleanup_pop (1);
1315 else
1316 rc = build_realpath (client, client->doc, line, &string);
1318 if (!rc)
1320 pthread_cleanup_push (list_command_cleanup1, string);
1321 rc = xfer_data (ctx, string->str, string->len);
1322 pthread_cleanup_pop (1);
1325 return send_error (ctx, rc);
1328 static void
1329 list_command_cleanup1 (void *arg)
1331 if (arg)
1332 string_free ((struct string_s *) arg, 1);
1335 static void
1336 list_command_cleanup2 (void *arg)
1338 struct element_list_s *elements = arg;
1340 if (elements)
1342 if (elements->list)
1344 int total = slist_length (elements->list);
1345 int i;
1347 for (i = 0; i < total; i++)
1349 char *tmp = slist_nth_data (elements->list, i);
1350 xfree (tmp);
1353 slist_free (elements->list);
1356 if (elements->prefix)
1357 xfree (elements->prefix);
1359 if (elements->req)
1360 strv_free (elements->req);
1362 xfree (elements);
1366 static gpg_error_t
1367 parse_list_opt_norecurse (void *data, void *value)
1369 struct client_s *client = data;
1371 client->opts &= ~(OPT_LIST_RECURSE);
1372 return 0;
1375 static gpg_error_t
1376 parse_opt_verbose (void *data, void *value)
1378 struct client_s *client = data;
1380 client->opts |= OPT_VERBOSE;
1381 return 0;
1384 static gpg_error_t
1385 parse_list_opt_target (void *data, void *value)
1387 struct client_s *client = data;
1389 client->opts |= OPT_LIST_WITH_TARGET;
1390 return 0;
1393 static gpg_error_t
1394 parse_list_opt_all (void *data, void *value)
1396 struct client_s *client = data;
1398 client->opts |= OPT_LIST_ALL;
1399 return 0;
1402 static gpg_error_t
1403 list_path_once (struct client_s *client, char *line,
1404 struct element_list_s *elements, struct string_s *result)
1406 gpg_error_t rc;
1408 elements->req = str_split (line, " ", 0);
1409 if (!elements->req)
1410 strv_printf (&elements->req, "%s", line);
1412 rc = create_path_list (client, client->doc, elements, *elements->req);
1413 if ((rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES) && elements->verbose)
1414 rc = 0;
1416 if (!rc)
1418 int total = slist_length (elements->list);
1420 if (!total)
1421 rc = GPG_ERR_NO_DATA;
1423 if (!rc)
1425 if (!rc)
1427 int i;
1429 for (i = 0; i < total; i++)
1431 char *tmp = slist_nth_data (elements->list, i);
1433 string_append_printf (result, "%s%s", tmp,
1434 i + 1 == total ? "" : "\n");
1438 else
1439 rc = GPG_ERR_NO_DATA;
1442 return rc;
1445 static int
1446 has_list_flag (char *path, char *flags)
1448 char *p = path;
1450 while (*p && *++p != ' ');
1452 if (!*p)
1453 return 0;
1455 for (; *p; p++)
1457 char *f;
1459 for (f = flags; *f && *f != ' '; f++)
1461 if (*p == *f)
1462 return 1;
1466 return 0;
1469 static gpg_error_t
1470 do_list (assuan_context_t ctx, char *line)
1472 struct client_s *client = assuan_get_pointer (ctx);
1473 gpg_error_t rc;
1474 struct element_list_s *elements = NULL;
1476 elements = xcalloc (1, sizeof (struct element_list_s));
1477 if (!elements)
1478 return GPG_ERR_ENOMEM;
1480 elements->recurse = client->opts & OPT_LIST_RECURSE;
1481 elements->verbose =
1482 (client->opts & OPT_VERBOSE) | (client->opts & OPT_LIST_WITH_TARGET);
1483 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1485 if (!line || !*line)
1487 struct string_s *str = NULL;
1489 pthread_cleanup_push (list_command_cleanup2, elements);
1490 rc = list_root_elements (client, client->doc, &str, elements->verbose,
1491 elements->with_target);
1492 pthread_cleanup_pop (1);
1493 pthread_cleanup_push (list_command_cleanup1, str);
1495 if (!rc)
1497 if (client->opts & OPT_LIST_ALL)
1499 char **roots = str_split (str->str, "\n", 0);
1500 char **p;
1502 pthread_cleanup_push (req_cleanup, roots);
1503 string_truncate (str, 0);
1505 for (p = roots; *p; p++)
1507 if (strchr (*p, ' '))
1509 if (has_list_flag (*p, "EOP"))
1511 string_append_printf (str, "%s%s", *p,
1512 *(p + 1) ? "\n" : "");
1513 continue;
1517 elements = xcalloc (1, sizeof (struct element_list_s));
1518 if (!elements)
1520 rc = GPG_ERR_ENOMEM;
1521 break;
1524 elements->recurse = client->opts & OPT_LIST_RECURSE;
1525 elements->verbose =
1526 (client->opts & OPT_VERBOSE) | (client->opts &
1527 OPT_LIST_WITH_TARGET);
1528 elements->with_target = client->opts & OPT_LIST_WITH_TARGET;
1529 pthread_cleanup_push (list_command_cleanup2, elements);
1530 rc = list_path_once (client, *p, elements, str);
1531 pthread_cleanup_pop (1);
1532 if (rc)
1533 break;
1535 if (*(p + 1))
1536 string_append (str, "\n");
1539 pthread_cleanup_pop (1);
1542 if (!rc)
1543 rc = xfer_data (ctx, str->str, str->len);
1546 pthread_cleanup_pop (1);
1547 return rc;
1550 pthread_cleanup_push (list_command_cleanup2, elements);
1551 struct string_s *str = string_new (NULL);
1552 pthread_cleanup_push (list_command_cleanup1, str);
1553 rc = list_path_once (client, line, elements, str);
1554 if (!rc)
1555 rc = xfer_data (ctx, str->str, str->len);
1557 pthread_cleanup_pop (1);
1558 pthread_cleanup_pop (1);
1559 return rc;
1562 static gpg_error_t
1563 list_command (assuan_context_t ctx, char *line)
1565 struct client_s *client = assuan_get_pointer (ctx);
1566 gpg_error_t rc;
1567 struct argv_s *args[] = {
1568 &(struct argv_s) {"no-recurse", OPTION_TYPE_NOARG,
1569 parse_list_opt_norecurse},
1570 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_opt_verbose},
1571 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
1572 &(struct argv_s) {"with-target", OPTION_TYPE_NOARG,
1573 parse_list_opt_target},
1574 &(struct argv_s) {"all", OPTION_TYPE_NOARG, parse_list_opt_all},
1575 NULL
1578 if (disable_list_and_dump == 1)
1579 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
1581 client->opts |= OPT_LIST_RECURSE;
1582 rc = parse_options (&line, args, client);
1583 if (rc)
1584 return send_error (ctx, rc);
1586 if (client->opts & OPT_LIST_ALL)
1587 client->opts |= OPT_LIST_RECURSE | OPT_VERBOSE;
1589 if (client->opts & OPT_INQUIRE)
1591 unsigned char *result;
1592 size_t len;
1594 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
1595 if (rc)
1596 return send_error (ctx, rc);
1598 pthread_cleanup_push (xfree, result);
1599 rc = do_list (ctx, (char *)result);
1600 pthread_cleanup_pop (1);
1602 else
1603 rc = do_list (ctx, line);
1605 return send_error (ctx, rc);
1609 * req[0] - element path
1611 static gpg_error_t
1612 attribute_list (assuan_context_t ctx, char **req)
1614 struct client_s *client = assuan_get_pointer (ctx);
1615 char **attrlist = NULL;
1616 int i = 0;
1617 char **path = NULL;
1618 xmlAttrPtr a;
1619 xmlNodePtr n, an;
1620 char *line;
1621 gpg_error_t rc;
1623 if (!req || !req[0])
1624 return GPG_ERR_SYNTAX;
1626 if ((path = str_split (req[0], "\t", 0)) == NULL)
1629 * The first argument may be only a root element.
1631 if ((path = str_split (req[0], " ", 0)) == NULL)
1632 return GPG_ERR_SYNTAX;
1635 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1637 if (!n)
1639 strv_free (path);
1640 return rc;
1643 if (path[1])
1645 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1646 NULL, NULL, NULL, 0, 0, NULL, 0);
1648 if (!n)
1650 strv_free (path);
1651 return rc;
1655 strv_free (path);
1657 for (a = n->properties; a; a = a->next)
1659 char **pa;
1661 if ((pa = xrealloc (attrlist, (i + 2) * sizeof (char *))) == NULL)
1663 if (attrlist)
1664 strv_free (attrlist);
1666 log_write ("%s(%i): %s", __FILE__, __LINE__,
1667 pwmd_strerror (GPG_ERR_ENOMEM));
1668 return GPG_ERR_ENOMEM;
1671 attrlist = pa;
1672 an = a->children;
1673 attrlist[i] = str_asprintf ("%s %s", (char *) a->name,
1675 && an->content ? (char *) an->content : "");
1677 if (!attrlist[i])
1679 strv_free (attrlist);
1680 log_write ("%s(%i): %s", __FILE__, __LINE__,
1681 pwmd_strerror (GPG_ERR_ENOMEM));
1682 return GPG_ERR_ENOMEM;
1685 attrlist[++i] = NULL;
1688 if (!attrlist)
1689 return GPG_ERR_NO_DATA;
1691 line = strv_join ("\n", attrlist);
1693 if (!line)
1695 log_write ("%s(%i): %s", __FILE__, __LINE__,
1696 pwmd_strerror (GPG_ERR_ENOMEM));
1697 strv_free (attrlist);
1698 return GPG_ERR_ENOMEM;
1701 pthread_cleanup_push (xfree, line);
1702 pthread_cleanup_push (req_cleanup, attrlist);
1703 rc = xfer_data (ctx, line, strlen (line));
1704 pthread_cleanup_pop (1);
1705 pthread_cleanup_pop (1);
1706 return rc;
1710 * req[0] - attribute
1711 * req[1] - element path
1713 static gpg_error_t
1714 attribute_delete (struct client_s *client, char **req)
1716 xmlNodePtr n;
1717 char **path = NULL;
1718 gpg_error_t rc;
1720 if (!req || !req[0] || !req[1])
1721 return GPG_ERR_SYNTAX;
1723 if (!strcmp (req[0], "_name"))
1724 return GPG_ERR_INV_ATTR;
1726 if ((path = str_split (req[1], "\t", 0)) == NULL)
1729 * The first argument may be only a root element.
1731 if ((path = str_split (req[1], " ", 0)) == NULL)
1732 return GPG_ERR_SYNTAX;
1735 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1736 if (!n)
1737 goto fail;
1739 if (path[1])
1741 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1742 NULL, NULL, NULL, 0, 0, NULL, 0);
1743 if (!n)
1744 goto fail;
1747 if (!strcmp (req[0], (char *) "_acl"))
1749 rc = is_element_owner (client, n);
1750 if (rc)
1751 goto fail;
1754 rc = delete_attribute (client, n, (xmlChar *) req[0]);
1756 fail:
1757 strv_free (path);
1758 return rc;
1761 static xmlNodePtr
1762 create_element_path (struct client_s *client,
1763 char ***elements, gpg_error_t * rc, xmlNodePtr parent)
1765 char **req = *elements;
1766 char **req_orig = strv_dup (req);
1767 xmlNodePtr n = NULL;
1769 *rc = 0;
1771 if (!req_orig)
1773 *rc = GPG_ERR_ENOMEM;
1774 log_write ("%s(%i): %s", __FILE__, __LINE__,
1775 pwmd_strerror (GPG_ERR_ENOMEM));
1776 goto fail;
1779 again:
1780 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
1781 if (!n)
1783 if (*rc != GPG_ERR_ELEMENT_NOT_FOUND)
1784 goto fail;
1786 *rc = new_root_element (client, client->doc, req[0]);
1787 if (*rc)
1788 goto fail;
1790 goto again;
1792 else if (n == parent)
1794 *rc = GPG_ERR_CONFLICT;
1795 goto fail;
1798 if (req[1])
1800 if (!n->children)
1801 n = create_target_elements_cb (client, 1, n, req + 1, rc, NULL);
1802 else
1803 n = find_elements (client, client->doc, n->children, req + 1, rc,
1804 NULL, NULL, create_target_elements_cb, 0, 0,
1805 parent, 0);
1807 if (!n)
1808 goto fail;
1811 * Reset the position of the element tree now that the elements
1812 * have been created.
1814 strv_free (req);
1815 req = req_orig;
1816 req_orig = NULL;
1817 n = find_root_element (client, client->doc, &req, rc, NULL, 0, 0);
1818 if (!n)
1819 goto fail;
1821 n = find_elements (client, client->doc, n->children, req + 1, rc,
1822 NULL, NULL, NULL, 0, 0, NULL, 0);
1823 if (!n)
1824 goto fail;
1827 fail:
1828 if (req_orig)
1829 strv_free (req_orig);
1831 *elements = req;
1832 return n;
1836 * Creates a "target" attribute. When other commands encounter an element with
1837 * this attribute, the element path is modified to the target value. If the
1838 * source element path doesn't exist when using 'ATTR SET target', it is
1839 * created, but the destination element path must exist.
1841 * req[0] - source element path
1842 * req[1] - destination element path
1844 static gpg_error_t
1845 target_attribute (struct client_s *client, char **req)
1847 char **src, **dst, *line = NULL, **odst = NULL;
1848 gpg_error_t rc;
1849 xmlNodePtr n;
1851 if (!req || !req[0] || !req[1])
1852 return GPG_ERR_SYNTAX;
1854 if ((src = str_split (req[0], "\t", 0)) == NULL)
1857 * The first argument may be only a root element.
1859 if ((src = str_split (req[0], " ", 0)) == NULL)
1860 return GPG_ERR_SYNTAX;
1863 if (!valid_element_path (src, 0))
1864 return GPG_ERR_INV_VALUE;
1866 if ((dst = str_split (req[1], "\t", 0)) == NULL)
1869 * The first argument may be only a root element.
1871 if ((dst = str_split (req[1], " ", 0)) == NULL)
1873 rc = GPG_ERR_SYNTAX;
1874 goto fail;
1878 odst = strv_dup (dst);
1879 if (!odst)
1881 rc = GPG_ERR_ENOMEM;
1882 goto fail;
1885 n = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
1887 * Make sure the destination element path exists.
1889 if (!n)
1890 goto fail;
1892 if (dst[1])
1894 n = find_elements (client, client->doc, n->children, dst + 1, &rc,
1895 NULL, NULL, NULL, 0, 0, NULL, 0);
1896 if (!n)
1897 goto fail;
1900 rc = validate_target_attribute (client, client->doc, req[0], n);
1901 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
1902 goto fail;
1904 n = create_element_path (client, &src, &rc, NULL);
1905 if (rc)
1906 goto fail;
1908 line = strv_join ("\t", odst);
1909 if (!line)
1911 rc = GPG_ERR_ENOMEM;
1912 goto fail;
1915 rc = add_attribute (client, n, "target", line);
1917 fail:
1918 xfree (line);
1919 strv_free (src);
1920 strv_free (dst);
1921 strv_free (odst);
1922 return rc;
1926 * req[0] - attribute
1927 * req[1] - element path
1929 static gpg_error_t
1930 attribute_get (assuan_context_t ctx, char **req)
1932 struct client_s *client = assuan_get_pointer (ctx);
1933 xmlNodePtr n;
1934 xmlChar *a;
1935 char **path = NULL;
1936 gpg_error_t rc;
1938 if (!req || !req[0] || !req[1])
1939 return GPG_ERR_SYNTAX;
1941 if (strchr (req[1], '\t'))
1943 if ((path = str_split (req[1], "\t", 0)) == NULL)
1944 return GPG_ERR_SYNTAX;
1946 else
1948 if ((path = str_split (req[1], " ", 0)) == NULL)
1949 return GPG_ERR_SYNTAX;
1952 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
1954 if (!n)
1955 goto fail;
1957 if (path[1])
1959 n = find_elements (client, client->doc, n->children, path + 1, &rc,
1960 NULL, NULL, NULL, 0, 0, NULL, 0);
1962 if (!n)
1963 goto fail;
1966 strv_free (path);
1968 if ((a = xmlGetProp (n, (xmlChar *) req[0])) == NULL)
1969 return GPG_ERR_NOT_FOUND;
1971 pthread_cleanup_push (xmlFree, a);
1973 if (*a)
1974 rc = xfer_data (ctx, (char *) a, xmlStrlen (a));
1975 else
1976 rc = GPG_ERR_NO_DATA;
1978 pthread_cleanup_pop (1);
1979 return rc;
1981 fail:
1982 strv_free (path);
1983 return rc;
1987 * req[0] - attribute
1988 * req[1] - element path
1989 * req[2] - value
1991 static gpg_error_t
1992 attribute_set (struct client_s *client, char **req)
1994 char **path = NULL;
1995 gpg_error_t rc;
1996 xmlNodePtr n;
1998 if (!req || !req[0] || !req[1])
1999 return GPG_ERR_SYNTAX;
2002 * Reserved attribute names.
2004 if (!strcmp (req[0], "_name"))
2005 return GPG_ERR_INV_ATTR;
2006 else if (!strcmp (req[0], "target"))
2007 return target_attribute (client, req + 1);
2008 else if (!valid_xml_attribute (req[0]))
2009 return GPG_ERR_INV_VALUE;
2011 if ((path = str_split (req[1], "\t", 0)) == NULL)
2014 * The first argument may be only a root element.
2016 if ((path = str_split (req[1], " ", 0)) == NULL)
2017 return GPG_ERR_SYNTAX;
2020 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2022 if (!n)
2023 goto fail;
2025 if (path[1])
2027 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2028 NULL, NULL, NULL, 0, 0, NULL, 0);
2030 if (!n)
2031 goto fail;
2034 if (!strcmp (req[0], (char *) "_acl"))
2036 rc = is_element_owner (client, n);
2037 if (rc)
2038 goto fail;
2041 rc = add_attribute (client, n, req[0], req[2]);
2043 fail:
2044 strv_free (path);
2045 return rc;
2049 * req[0] - command
2050 * req[1] - attribute name or element path if command is LIST
2051 * req[2] - element path
2052 * req[2] - element path or value
2055 static gpg_error_t
2056 do_attr (assuan_context_t ctx, char *line)
2058 struct client_s *client = assuan_get_pointer (ctx);
2059 gpg_error_t rc = 0;
2060 char **req;
2062 req = str_split (line, " ", 4);
2063 if (!req || !req[0] || !req[1])
2065 strv_free (req);
2066 return GPG_ERR_SYNTAX;
2069 pthread_cleanup_push (req_cleanup, req);
2071 if (strcasecmp (req[0], "SET") == 0)
2072 rc = attribute_set (client, req + 1);
2073 else if (strcasecmp (req[0], "GET") == 0)
2074 rc = attribute_get (ctx, req + 1);
2075 else if (strcasecmp (req[0], "DELETE") == 0)
2076 rc = attribute_delete (client, req + 1);
2077 else if (strcasecmp (req[0], "LIST") == 0)
2078 rc = attribute_list (ctx, req + 1);
2079 else
2080 rc = GPG_ERR_SYNTAX;
2082 pthread_cleanup_pop (1);
2083 return rc;
2086 static gpg_error_t
2087 attr_command (assuan_context_t ctx, char *line)
2089 struct client_s *client = assuan_get_pointer (ctx);
2090 gpg_error_t rc;
2091 struct argv_s *args[] = {
2092 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2093 NULL
2096 rc = parse_options (&line, args, client);
2097 if (rc)
2098 return send_error (ctx, rc);
2100 if (client->opts & OPT_INQUIRE)
2102 unsigned char *result;
2103 size_t len;
2105 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2106 if (rc)
2107 return send_error (ctx, rc);
2109 pthread_cleanup_push (xfree, result);
2110 rc = do_attr (ctx, (char *)result);
2111 pthread_cleanup_pop (1);
2113 else
2114 rc = do_attr (ctx, line);
2116 return send_error (ctx, rc);
2119 static gpg_error_t
2120 parse_iscached_opt_lock (void *data, void *value)
2122 struct client_s *client = data;
2124 (void) value;
2125 client->opts |= OPT_LOCK;
2126 return 0;
2129 static gpg_error_t
2130 iscached_command (assuan_context_t ctx, char *line)
2132 struct client_s *client = assuan_get_pointer (ctx);
2133 gpg_error_t rc;
2134 struct argv_s *args[] = {
2135 &(struct argv_s) {"lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock},
2136 NULL
2139 if (!line || !*line)
2140 return send_error (ctx, GPG_ERR_SYNTAX);
2142 rc = parse_options (&line, args, client);
2143 if (rc)
2144 return send_error (ctx, rc);
2145 else if (!valid_filename (line))
2146 return send_error (ctx, GPG_ERR_INV_VALUE);
2148 rc = cache_iscached (line, NULL);
2149 if (client->opts & OPT_LOCK
2150 && (!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA))
2152 unsigned char md5file[16];
2153 gpg_error_t trc = rc;
2155 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2156 if (memcmp (md5file, client->md5file, 16))
2157 cleanup_client (client);
2159 memcpy (client->md5file, md5file, 16);
2160 rc = do_lock (client, 1);
2161 if (!rc)
2162 rc = trc;
2165 return send_error (ctx, rc);
2168 static gpg_error_t
2169 clearcache_command (assuan_context_t ctx, char *line)
2171 gpg_error_t rc = 0, all_rc = 0;
2172 unsigned char md5file[16];
2173 int i;
2174 int t;
2175 int all = 0;
2176 struct client_thread_s *once = NULL;
2178 cache_lock ();
2179 MUTEX_LOCK (&cn_mutex);
2180 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
2182 if (!line || !*line)
2183 all = 1;
2185 t = slist_length (cn_thread_list);
2187 for (i = 0; i < t; i++)
2189 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
2190 assuan_peercred_t peer;
2192 if (!thd->cl)
2193 continue;
2195 /* Lock each connected clients' file mutex to prevent any other client
2196 * from accessing the cache entry (the file mutex is locked upon
2197 * command startup). The cache for the entry is not cleared if the
2198 * file mutex is locked by another client to prevent this function
2199 * from blocking.
2201 if (all)
2203 if (thd->cl->filename)
2205 rc = do_validate_peer (ctx, thd->cl->filename, &peer);
2206 all_rc = !all_rc ? rc : all_rc;
2207 if (rc)
2209 rc = 0;
2210 continue;
2214 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
2215 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2217 if (pthread_equal (pthread_self (), thd->tid))
2218 rc = 0;
2219 else
2221 if (!thd->cl->filename ||
2222 cache_iscached (thd->cl->filename,
2223 NULL) == GPG_ERR_NO_DATA)
2225 rc = 0;
2226 continue;
2229 cache_defer_clear (thd->cl->md5file);
2232 else if (gpg_err_code (rc) == GPG_ERR_NO_DATA)
2234 rc = 0;
2235 continue;
2238 if (!rc)
2240 rc = cache_clear (thd->cl->md5file);
2241 cache_unlock_mutex (thd->cl->md5file, 0);
2244 if (rc)
2245 all_rc = rc;
2247 rc = 0;
2249 /* A single data filename was specified. Lock only this data file
2250 * mutex and free the cache entry. */
2251 else
2253 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2254 rc = do_validate_peer (ctx, line, &peer);
2256 if (!rc && !memcmp (thd->cl->md5file, md5file, sizeof (md5file)))
2258 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->md5file, -1, 0,
2259 -1);
2260 if (gpg_err_code (rc) == GPG_ERR_LOCKED)
2262 if (pthread_equal (pthread_self (), thd->tid))
2263 rc = 0;
2266 if (!rc)
2268 once = thd;
2269 rc = cache_clear (thd->cl->md5file);
2270 cache_unlock_mutex (thd->cl->md5file, 0);
2272 else
2274 cache_defer_clear (thd->cl->md5file);
2277 break;
2282 /* Only connected clients' cache entries have been cleared. Now clear any
2283 * remaining cache entries without clients but only if there wasn't an
2284 * error from above since this would defeat the locking check of the
2285 * remaining entries. */
2286 if (!all_rc && all)
2288 cache_clear (NULL);
2291 /* No clients are using the specified file. */
2292 else if (!all_rc && !rc && !once)
2294 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, line, strlen (line));
2295 rc = cache_clear (md5file);
2298 /* Release the connection mutex. */
2299 pthread_cleanup_pop (1);
2300 cache_unlock ();
2302 if (!rc)
2303 send_status_all (STATUS_CACHE, NULL);
2305 /* One or more files were locked while clearing all cache entries. */
2306 if (all_rc)
2307 rc = all_rc;
2309 return send_error (ctx, rc);
2312 static gpg_error_t
2313 cachetimeout_command (assuan_context_t ctx, char *line)
2315 int timeout;
2316 char **req = str_split (line, " ", 0);
2317 char *p;
2318 gpg_error_t rc = 0;
2319 assuan_peercred_t peer;
2321 if (!req || !*req || !req[1])
2323 strv_free (req);
2324 return send_error (ctx, GPG_ERR_SYNTAX);
2327 errno = 0;
2328 timeout = (int) strtol (req[1], &p, 10);
2329 if (errno != 0 || *p || timeout < -1)
2331 strv_free (req);
2332 return send_error (ctx, GPG_ERR_SYNTAX);
2335 rc = do_validate_peer (ctx, req[0], &peer);
2336 if (!rc)
2338 unsigned char md5file[16];
2340 gcry_md_hash_buffer (GCRY_MD_MD5, md5file, req[0], strlen (req[0]));
2341 rc = cache_set_timeout (md5file, timeout);
2342 if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
2344 rc = 0;
2345 MUTEX_LOCK (&rcfile_mutex);
2346 config_set_int_param (&global_config, req[0], "cache_timeout",
2347 req[1]);
2348 MUTEX_UNLOCK (&rcfile_mutex);
2352 strv_free (req);
2353 return send_error (ctx, rc);
2356 static gpg_error_t
2357 dump_command (assuan_context_t ctx, char *line)
2359 xmlChar *xml;
2360 int len;
2361 struct client_s *client = assuan_get_pointer (ctx);
2362 gpg_error_t rc;
2364 if (disable_list_and_dump == 1)
2365 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2367 rc = peer_is_invoker(client);
2368 if (rc)
2369 return send_error (ctx, rc);
2371 xmlDocDumpFormatMemory (client->doc, &xml, &len, 1);
2373 if (!xml)
2375 log_write ("%s(%i): %s", __FILE__, __LINE__,
2376 pwmd_strerror (GPG_ERR_ENOMEM));
2377 return send_error (ctx, GPG_ERR_ENOMEM);
2380 pthread_cleanup_push (xmlFree, xml);
2381 rc = xfer_data (ctx, (char *) xml, len);
2382 pthread_cleanup_pop (1);
2383 return send_error (ctx, rc);
2386 static gpg_error_t
2387 getconfig_command (assuan_context_t ctx, char *line)
2389 struct client_s *client = assuan_get_pointer (ctx);
2390 gpg_error_t rc = 0;
2391 char filename[255] = { 0 }, param[747] = { 0 };
2392 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2394 if (!line || !*line)
2395 return send_error (ctx, GPG_ERR_SYNTAX);
2397 if (strchr (line, ' '))
2399 sscanf (line, " %254[^ ] %746c", filename, param);
2400 paramp = param;
2401 fp = filename;
2404 if (fp && !valid_filename (fp))
2405 return send_error (ctx, GPG_ERR_INV_VALUE);
2407 paramp = str_down (paramp);
2408 if (!strcmp (paramp, "passphrase"))
2409 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2411 p = config_get_value (fp ? fp : "global", paramp);
2412 if (!p)
2413 return send_error (ctx, GPG_ERR_UNKNOWN_OPTION);
2415 tmp = expand_homedir (p);
2416 xfree (p);
2417 if (!tmp)
2419 log_write ("%s(%i): %s", __FILE__, __LINE__,
2420 pwmd_strerror (GPG_ERR_ENOMEM));
2421 return send_error (ctx, GPG_ERR_ENOMEM);
2424 p = tmp;
2425 pthread_cleanup_push (xfree, p);
2426 rc = xfer_data (ctx, p, strlen (p));
2427 pthread_cleanup_pop (1);
2428 return send_error (ctx, rc);
2431 struct xpath_s
2433 xmlXPathContextPtr xp;
2434 xmlXPathObjectPtr result;
2435 xmlBufferPtr buf;
2436 char **req;
2439 static void
2440 xpath_command_cleanup (void *arg)
2442 struct xpath_s *xpath = arg;
2444 if (!xpath)
2445 return;
2447 req_cleanup (xpath->req);
2449 if (xpath->buf)
2450 xmlBufferFree (xpath->buf);
2452 if (xpath->result)
2453 xmlXPathFreeObject (xpath->result);
2455 if (xpath->xp)
2456 xmlXPathFreeContext (xpath->xp);
2459 static gpg_error_t
2460 do_xpath (assuan_context_t ctx, char *line)
2462 gpg_error_t rc;
2463 struct client_s *client = assuan_get_pointer (ctx);
2464 struct xpath_s _x = { 0 };
2465 struct xpath_s *xpath = &_x;
2467 if (!line || !*line)
2468 return GPG_ERR_SYNTAX;
2470 if ((xpath->req = str_split (line, "\t", 2)) == NULL)
2472 if (strv_printf (&xpath->req, "%s", line) == 0)
2473 return GPG_ERR_ENOMEM;
2476 xpath->xp = xmlXPathNewContext (client->doc);
2477 if (!xpath->xp)
2479 rc = GPG_ERR_BAD_DATA;
2480 goto fail;
2483 xpath->result =
2484 xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2485 if (!xpath->result)
2487 rc = GPG_ERR_BAD_DATA;
2488 goto fail;
2491 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2493 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2494 goto fail;
2497 rc = recurse_xpath_nodeset (client, client->doc, xpath->result->nodesetval,
2498 (xmlChar *) xpath->req[1], &xpath->buf, 0,
2499 NULL);
2500 if (rc)
2501 goto fail;
2502 else if (!xpath->req[1] && !xmlBufferLength (xpath->buf))
2504 rc = GPG_ERR_NO_DATA;
2505 goto fail;
2507 else if (xpath->req[1])
2509 rc = 0;
2510 goto fail;
2513 pthread_cleanup_push (xpath_command_cleanup, &xpath);
2514 rc = xfer_data (ctx, (char *) xmlBufferContent (xpath->buf),
2515 xmlBufferLength (xpath->buf));
2516 pthread_cleanup_pop (0);
2517 fail:
2518 xpath_command_cleanup (xpath);
2519 return rc;
2522 static gpg_error_t
2523 xpath_command (assuan_context_t ctx, char *line)
2525 struct client_s *client = assuan_get_pointer (ctx);
2526 gpg_error_t rc;
2527 struct argv_s *args[] = {
2528 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2529 NULL
2532 if (disable_list_and_dump == 1)
2533 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2535 rc = peer_is_invoker(client);
2536 if (rc)
2537 return send_error (ctx, rc);
2539 rc = parse_options (&line, args, client);
2540 if (rc)
2541 return send_error (ctx, rc);
2543 if (client->opts & OPT_INQUIRE)
2545 unsigned char *result;
2546 size_t len;
2548 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2549 if (rc)
2550 return send_error (ctx, rc);
2552 pthread_cleanup_push (xfree, result);
2553 rc = do_xpath (ctx, (char *)result);
2554 pthread_cleanup_pop (1);
2556 else
2557 rc = do_xpath (ctx, line);
2559 return send_error (ctx, rc);
2562 static gpg_error_t
2563 do_xpathattr (assuan_context_t ctx, char *line)
2565 struct client_s *client = assuan_get_pointer (ctx);
2566 gpg_error_t rc;
2567 char **req = NULL;
2568 int cmd = 0; //SET
2569 struct xpath_s _x = { 0 };
2570 struct xpath_s *xpath = &_x;
2572 if (!line || !*line)
2573 return GPG_ERR_SYNTAX;
2575 if ((req = str_split (line, " ", 3)) == NULL)
2576 return GPG_ERR_ENOMEM;
2578 if (!req[0])
2580 rc = GPG_ERR_SYNTAX;
2581 goto fail;
2584 if (!strcasecmp (req[0], "SET"))
2585 cmd = 0;
2586 else if (!strcasecmp (req[0], "DELETE"))
2587 cmd = 1;
2588 else
2590 rc = GPG_ERR_SYNTAX;
2591 goto fail;
2594 if (!req[1] || !req[2])
2596 rc = GPG_ERR_SYNTAX;
2597 goto fail;
2600 if ((xpath->req = str_split (req[2], "\t", 3)) == NULL)
2602 rc = GPG_ERR_ENOMEM;
2603 goto fail;
2606 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd))
2608 rc = GPG_ERR_SYNTAX;
2609 goto fail;
2612 xpath->xp = xmlXPathNewContext (client->doc);
2613 if (!xpath->xp)
2615 rc = GPG_ERR_BAD_DATA;
2616 goto fail;
2619 xpath->result = xmlXPathEvalExpression ((xmlChar *) xpath->req[0], xpath->xp);
2620 if (!xpath->result)
2622 rc = GPG_ERR_BAD_DATA;
2623 goto fail;
2626 if (xmlXPathNodeSetIsEmpty (xpath->result->nodesetval))
2628 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2629 goto fail;
2632 rc = recurse_xpath_nodeset (client, client->doc, xpath->result->nodesetval,
2633 (xmlChar *) xpath->req[1], &xpath->buf, cmd,
2634 (xmlChar *) req[1]);
2636 fail:
2637 xpath_command_cleanup (xpath);
2638 strv_free (req);
2639 return rc;
2642 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2643 static gpg_error_t
2644 xpathattr_command (assuan_context_t ctx, char *line)
2646 struct client_s *client = assuan_get_pointer (ctx);
2647 gpg_error_t rc;
2648 struct argv_s *args[] = {
2649 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
2650 NULL
2653 if (disable_list_and_dump == 1)
2654 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
2656 rc = peer_is_invoker(client);
2657 if (rc)
2658 return send_error (ctx, rc);
2660 rc = parse_options (&line, args, client);
2661 if (rc)
2662 return send_error (ctx, rc);
2664 if (client->opts & OPT_INQUIRE)
2666 unsigned char *result;
2667 size_t len;
2669 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2670 if (rc)
2671 return send_error (ctx, rc);
2673 pthread_cleanup_push (xfree, result);
2674 rc = do_xpathattr (ctx, (char *)result);
2675 pthread_cleanup_pop (1);
2677 else
2678 rc = do_xpathattr (ctx, line);
2680 return send_error (ctx, rc);
2683 static gpg_error_t
2684 do_import (struct client_s *client, const char *root_element,
2685 unsigned char *content)
2687 char **dst_path = NULL;
2688 xmlDocPtr doc = NULL;
2689 xmlNodePtr n, root, copy;
2690 gpg_error_t rc;
2692 if (!content || !*content)
2694 xfree (content);
2695 return GPG_ERR_SYNTAX;
2698 if (root_element)
2699 dst_path = str_split (root_element, "\t", 0);
2701 if (dst_path && !valid_element_path (dst_path, 0))
2703 if (dst_path)
2704 strv_free (dst_path);
2706 return GPG_ERR_INV_VALUE;
2709 struct string_s *str = string_new_content ((char *)content);
2710 str = string_prepend (str, "<pwmd>");
2711 str = string_append (str, "</pwmd>");
2712 doc = xmlReadDoc ((xmlChar *) str->str, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2713 string_free (str, 1);
2714 if (!doc)
2716 rc = GPG_ERR_BAD_DATA;
2717 goto fail;
2720 root = xmlDocGetRootElement (doc);
2721 xmlNodePtr root_orig = root->children;
2722 root = root->children;
2723 rc = validate_import (client, root);
2724 if (rc)
2725 goto fail;
2729 again:
2730 if (dst_path)
2732 char **path = strv_dup (dst_path);
2733 if (!path)
2735 log_write ("%s(%i): %s", __FILE__, __LINE__,
2736 pwmd_strerror (GPG_ERR_ENOMEM));
2737 rc = GPG_ERR_ENOMEM;
2738 goto fail;
2741 xmlChar *a = xmlGetProp (root, (xmlChar *) "_name");
2742 if (!a)
2744 strv_free (path);
2745 rc = GPG_ERR_INV_VALUE;
2746 goto fail;
2749 if (strv_printf (&path, "%s", (char *) a) == 0)
2751 xmlFree (a);
2752 rc = GPG_ERR_ENOMEM;
2753 goto fail;
2756 xmlFree (a);
2757 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 0);
2758 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2760 strv_free (path);
2761 goto fail;
2764 if (!rc)
2766 n = find_elements (client, client->doc, n->children, path + 1, &rc,
2767 NULL, NULL, NULL, 0, 0, NULL, 1);
2768 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2770 strv_free (path);
2771 goto fail;
2773 else if (!rc)
2775 xmlUnlinkNode (n);
2776 xmlFreeNode (n);
2777 strv_free (path);
2778 path = NULL;
2779 goto again;
2783 if (rc == GPG_ERR_ELEMENT_NOT_FOUND)
2785 n = create_element_path (client, &path, &rc, NULL);
2786 if (rc)
2787 goto fail;
2790 if (root->children)
2792 copy = xmlCopyNodeList (root->children);
2793 n = xmlAddChildList (n, copy);
2794 if (!n)
2795 rc = GPG_ERR_ENOMEM;
2798 strv_free (path);
2800 else
2802 char **path = NULL;
2804 /* Check if the content root element can create a DTD root element. */
2805 if (!xmlStrEqual ((xmlChar *) "element", root->name))
2807 rc = GPG_ERR_SYNTAX;
2808 goto fail;
2811 xmlChar *a;
2813 if ((a = xmlGetProp (root, (xmlChar *) "_name")) == NULL)
2815 rc = GPG_ERR_SYNTAX;
2816 goto fail;
2819 char *tmp = str_dup ((char *) a);
2820 xmlFree (a);
2821 int literal = is_literal_element (&tmp);
2823 if (!valid_xml_element ((xmlChar *) tmp) || literal)
2825 xfree (tmp);
2826 rc = GPG_ERR_INV_VALUE;
2827 goto fail;
2830 if (strv_printf (&path, "%s", tmp) == 0)
2832 xfree (tmp);
2833 rc = GPG_ERR_ENOMEM;
2834 goto fail;
2837 xfree (tmp);
2838 n = find_root_element (client, client->doc, &path, &rc, NULL, 0, 1);
2839 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2841 rc = GPG_ERR_BAD_DATA;
2842 goto fail;
2845 /* Overwriting the existing tree. */
2846 if (!rc)
2848 xmlUnlinkNode (n);
2849 xmlFreeNodeList (n);
2852 rc = 0;
2853 xmlSetProp (root, (xmlChar *) "_name", (xmlChar *) path[0]);
2854 n = xmlCopyNode (root, 1);
2855 n = xmlAddChildList (xmlDocGetRootElement (client->doc), n);
2856 strv_free (path);
2859 if (n && !rc)
2860 rc = update_element_mtime (client, n->parent);
2862 for (root = root_orig->next; root; root = root->next)
2864 if (root->type == XML_ELEMENT_NODE)
2865 break;
2868 root_orig = root;
2870 while (root);
2872 fail:
2873 if (doc)
2874 xmlFreeDoc (doc);
2876 if (dst_path)
2877 strv_free (dst_path);
2879 return rc;
2882 static gpg_error_t
2883 parse_import_opt_root (void *data, void *value)
2885 struct client_s *client = data;
2887 client->import_root = str_dup (value);
2888 return client->import_root ? 0 : GPG_ERR_ENOMEM;
2891 static gpg_error_t
2892 import_command (assuan_context_t ctx, char *line)
2894 gpg_error_t rc;
2895 struct client_s *client = assuan_get_pointer (ctx);
2896 unsigned char *result;
2897 size_t len;
2898 struct argv_s *args[] = {
2899 &(struct argv_s) {"root", OPTION_TYPE_ARG, parse_import_opt_root},
2900 NULL
2903 xfree (client->import_root);
2904 client->import_root = NULL;
2905 rc = parse_options (&line, args, client);
2906 if (rc)
2907 return send_error (ctx, rc);
2909 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
2910 if (rc)
2912 xfree (client->import_root);
2913 client->import_root = NULL;
2914 return send_error (ctx, rc);
2917 rc = do_import (client, client->import_root, result);
2918 xfree (client->import_root);
2919 client->import_root = NULL;
2920 return send_error (ctx, rc);
2923 static gpg_error_t
2924 do_lock (struct client_s *client, int add)
2926 gpg_error_t rc = lock_file_mutex (client, add);
2928 if (!rc)
2929 client->flags |= FLAG_LOCK_CMD;
2931 return rc;
2934 static gpg_error_t
2935 lock_command (assuan_context_t ctx, char *line)
2937 struct client_s *client = assuan_get_pointer (ctx);
2938 gpg_error_t rc = do_lock (client, 0);
2940 return send_error (ctx, rc);
2943 static gpg_error_t
2944 unlock_command (assuan_context_t ctx, char *line)
2946 struct client_s *client = assuan_get_pointer (ctx);
2947 gpg_error_t rc;
2949 rc = unlock_file_mutex (client, 0);
2950 return send_error (ctx, rc);
2953 static gpg_error_t
2954 option_command (assuan_context_t ctx, char *line)
2956 struct client_s *client = assuan_get_pointer (ctx);
2957 gpg_error_t rc = 0;
2958 char namebuf[255] = { 0 };
2959 char *name = namebuf;
2960 char *value = NULL, *p, *tmp = NULL;
2962 p = strchr (line, '=');
2963 if (!p)
2964 strncpy (namebuf, line, sizeof(namebuf));
2965 else
2967 strncpy (namebuf, line, strlen (line)-strlen (p));
2968 value = p+1;
2971 log_write2 ("OPTION name='%s' value='%s'", name, value);
2973 if (strcasecmp (name, (char *) "log_level") == 0)
2975 long l = 0;
2977 if (value)
2979 l = strtol (value, NULL, 10);
2981 if (l < 0 || l > 2)
2982 return send_error (ctx, GPG_ERR_INV_VALUE);
2985 MUTEX_LOCK (&rcfile_mutex);
2986 config_set_int_param (&global_config, "global", "log_level", value);
2987 MUTEX_UNLOCK (&rcfile_mutex);
2989 else if (strcasecmp (name, (char *) "lock-timeout") == 0)
2991 long n = 0;
2993 if (value)
2995 n = strtol (value, &tmp, 10);
2996 if (tmp && *tmp)
2997 return send_error (ctx, GPG_ERR_INV_VALUE);
3000 client->lock_timeout = n;
3002 else if (strcasecmp (name, (char *) "NAME") == 0)
3004 if (value && strchr (value, ' '))
3005 rc = GPG_ERR_INV_VALUE;
3006 else
3008 tmp = pthread_getspecific (thread_name_key);
3009 xfree (tmp);
3010 MUTEX_LOCK (&cn_mutex);
3011 xfree (client->thd->name);
3012 client->thd->name = NULL;
3014 if (!value || !*value)
3015 pthread_setspecific (thread_name_key, str_dup (""));
3016 else
3018 client->thd->name = str_dup (value);
3019 pthread_setspecific (thread_name_key, str_dup (value));
3022 MUTEX_UNLOCK (&cn_mutex);
3025 else if (strcasecmp (name, "disable-pinentry") == 0)
3027 int n = 1;
3029 if (value && *value)
3031 n = (int) strtol (value, &tmp, 10);
3032 if (*tmp || n < 0 || n > 1)
3033 return send_error (ctx, GPG_ERR_INV_VALUE);
3036 if (n)
3037 client->flags |= FLAG_NO_PINENTRY;
3038 else
3039 client->flags &= ~FLAG_NO_PINENTRY;
3041 else
3042 rc = GPG_ERR_UNKNOWN_OPTION;
3044 return send_error (ctx, rc);
3047 static gpg_error_t
3048 do_rename (assuan_context_t ctx, char *line)
3050 struct client_s *client = assuan_get_pointer (ctx);
3051 gpg_error_t rc;
3052 char **req, **src, *dst;
3053 xmlNodePtr n, ndst;
3055 req = str_split (line, " ", 0);
3057 if (!req || !req[0] || !req[1])
3059 strv_free (req);
3060 return GPG_ERR_SYNTAX;
3063 dst = req[1];
3064 is_literal_element (&dst);
3066 if (!valid_xml_element ((xmlChar *) dst))
3068 strv_free (req);
3069 return GPG_ERR_INV_VALUE;
3072 if (strchr (req[0], '\t'))
3073 src = str_split (req[0], "\t", 0);
3074 else
3075 src = str_split (req[0], " ", 0);
3077 if (!src || !*src)
3079 rc = GPG_ERR_SYNTAX;
3080 goto fail;
3083 n = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3084 if (src[1] && n)
3085 n = find_elements (client, client->doc, n->children, src + 1, &rc, NULL, NULL,
3086 NULL, 0, 0, NULL, 0);
3088 if (!n)
3089 goto fail;
3091 rc = is_element_owner (client, n);
3092 if (rc)
3093 goto fail;
3095 xmlChar *a = xmlGetProp (n, (xmlChar *) "_name");
3096 if (!a)
3098 rc = GPG_ERR_ENOMEM;
3099 goto fail;
3102 /* To prevent unwanted effects:
3104 * <root name="a"><b/></root>
3106 * RENAME a<TAB>b b
3108 if (xmlStrEqual (a, (xmlChar *) dst))
3110 xmlFree (a);
3111 rc = GPG_ERR_AMBIGUOUS_NAME;
3112 goto fail;
3115 xmlFree (a);
3116 char **tmp = NULL;
3117 if (src[1])
3119 char **p;
3121 for (p = src; *p; p++)
3123 if (!*(p + 1))
3124 break;
3125 strv_printf (&tmp, "%s", *p);
3129 strv_printf (&tmp, "!%s", dst);
3130 ndst = find_root_element (client, client->doc, &tmp, &rc, NULL, 0, 0);
3131 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3133 strv_free (tmp);
3134 goto fail;
3137 if (tmp[1] && ndst)
3138 ndst = find_elements (client, client->doc, ndst->children, tmp + 1, &rc, NULL,
3139 NULL, NULL, 0, 0, NULL, 0);
3141 strv_free (tmp);
3142 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3143 goto fail;
3145 rc = 0;
3147 /* Target may exist:
3149 * <root name="a"/>
3150 * <root name="b" target="a"/>
3152 * RENAME b a
3154 * Would need to do:
3155 * RENAME !b a
3157 if (ndst == n)
3159 rc = GPG_ERR_AMBIGUOUS_NAME;
3160 goto fail;
3163 if (ndst)
3165 rc = is_element_owner (client, ndst);
3166 if (rc)
3167 goto fail;
3169 unlink_node (client, ndst);
3170 xmlFreeNodeList (ndst);
3173 rc = add_attribute (client, n, "_name", dst);
3175 fail:
3176 strv_free (req);
3177 strv_free (src);
3178 return rc;
3181 static gpg_error_t
3182 rename_command (assuan_context_t ctx, char *line)
3184 struct client_s *client = assuan_get_pointer (ctx);
3185 gpg_error_t rc;
3186 struct argv_s *args[] = {
3187 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3188 NULL
3191 rc = parse_options (&line, args, client);
3192 if (rc)
3193 return send_error (ctx, rc);
3195 if (client->opts & OPT_INQUIRE)
3197 unsigned char *result;
3198 size_t len;
3200 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3201 if (rc)
3202 return send_error (ctx, rc);
3204 pthread_cleanup_push (xfree, result);
3205 rc = do_rename (ctx, (char *)result);
3206 pthread_cleanup_pop (1);
3208 else
3209 rc = do_rename (ctx, line);
3211 return send_error (ctx, rc);
3214 static gpg_error_t
3215 do_copy (assuan_context_t ctx, char *line)
3217 struct client_s *client = assuan_get_pointer (ctx);
3218 gpg_error_t rc;
3219 char **req, **src = NULL, **dst = NULL;
3220 xmlNodePtr nsrc, ndst, new = NULL;
3222 req = str_split (line, " ", 0);
3223 if (!req || !req[0] || !req[1])
3225 strv_free (req);
3226 return GPG_ERR_SYNTAX;
3229 if (strchr (req[0], '\t'))
3230 src = str_split (req[0], "\t", 0);
3231 else
3232 src = str_split (req[0], " ", 0);
3234 if (!src || !*src)
3236 rc = GPG_ERR_SYNTAX;
3237 goto fail;
3240 if (strchr (req[1], '\t'))
3241 dst = str_split (req[1], "\t", 0);
3242 else
3243 dst = str_split (req[1], " ", 0);
3245 if (!dst || !*dst)
3247 rc = GPG_ERR_SYNTAX;
3248 goto fail;
3251 if (!valid_element_path (dst, 0))
3253 rc = GPG_ERR_INV_VALUE;
3254 goto fail;
3257 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3258 if (nsrc && src[1])
3259 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc, NULL,
3260 NULL, NULL, 0, 0, NULL, 0);
3262 if (!nsrc)
3263 goto fail;
3265 new = xmlCopyNodeList (nsrc);
3266 if (!new)
3268 rc = GPG_ERR_ENOMEM;
3269 goto fail;
3272 int create = 0;
3273 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3274 if (ndst && dst[1])
3276 if (ndst->children)
3277 ndst = find_elements (client, client->doc, ndst->children, dst + 1, &rc, NULL,
3278 NULL, create_target_elements_cb, 0, 0, NULL, 0);
3279 else
3280 create = 1;
3282 else
3283 create = 1;
3285 if (!ndst && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3286 goto fail;
3287 else if (!ndst || create)
3289 ndst = create_element_path (client, &dst, &rc, NULL);
3290 if (!ndst)
3291 goto fail;
3294 rc = is_element_owner (client, ndst);
3295 if (rc)
3296 goto fail;
3298 /* Merge any attributes from the src node to the initial dst node. */
3299 for (xmlAttrPtr attr = new->properties; attr; attr = attr->next)
3301 if (xmlStrEqual (attr->name, (xmlChar *) "_name"))
3302 continue;
3304 xmlAttrPtr a = xmlHasProp (ndst, attr->name);
3305 if (a)
3306 xmlRemoveProp (a);
3308 xmlChar *tmp = xmlNodeGetContent (attr->children);
3309 xmlNewProp (ndst, attr->name, tmp);
3310 xmlFree (tmp);
3311 rc = add_attribute (client, ndst, NULL, NULL);
3314 xmlNodePtr n = ndst->children;
3315 xmlUnlinkNode (n);
3316 xmlFreeNodeList (n);
3317 ndst->children = NULL;
3319 if (new->children)
3321 n = xmlCopyNodeList (new->children);
3322 if (!n)
3324 rc = GPG_ERR_ENOMEM;
3325 goto fail;
3328 n = xmlAddChildList (ndst, n);
3329 if (!n)
3331 rc = GPG_ERR_ENOMEM;
3332 goto fail;
3335 rc = update_element_mtime (client, xmlDocGetRootElement (client->doc) ==
3336 ndst->parent ? ndst : ndst->parent);
3339 fail:
3340 if (new)
3342 xmlUnlinkNode (new);
3343 xmlFreeNodeList (new);
3346 if (req)
3347 strv_free (req);
3349 if (src)
3350 strv_free (src);
3352 if (dst)
3353 strv_free (dst);
3355 return rc;
3358 static gpg_error_t
3359 copy_command (assuan_context_t ctx, char *line)
3361 struct client_s *client = assuan_get_pointer (ctx);
3362 gpg_error_t rc;
3363 struct argv_s *args[] = {
3364 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3365 NULL
3368 rc = parse_options (&line, args, client);
3369 if (rc)
3370 return send_error (ctx, rc);
3372 if (client->opts & OPT_INQUIRE)
3374 unsigned char *result;
3375 size_t len;
3377 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3378 if (rc)
3379 return send_error (ctx, rc);
3381 pthread_cleanup_push (xfree, result);
3382 rc = do_copy (ctx, (char *)result);
3383 pthread_cleanup_pop (1);
3385 else
3386 rc = do_copy (ctx, line);
3388 return send_error (ctx, rc);
3391 static gpg_error_t
3392 do_move (assuan_context_t ctx, char *line)
3394 struct client_s *client = assuan_get_pointer (ctx);
3395 gpg_error_t rc;
3396 char **req, **src = NULL, **dst = NULL;
3397 xmlNodePtr nsrc, ndst = NULL;
3399 req = str_split (line, " ", 0);
3401 if (!req || !req[0] || !req[1])
3403 strv_free (req);
3404 return GPG_ERR_SYNTAX;
3407 if (strchr (req[0], '\t'))
3408 src = str_split (req[0], "\t", 0);
3409 else
3410 src = str_split (req[0], " ", 0);
3412 if (!src || !*src)
3414 rc = GPG_ERR_SYNTAX;
3415 goto fail;
3418 if (strchr (req[1], '\t'))
3419 dst = str_split (req[1], "\t", 0);
3420 else
3421 dst = str_split (req[1], " ", 0);
3423 nsrc = find_root_element (client, client->doc, &src, &rc, NULL, 0, 0);
3424 if (nsrc && src[1])
3425 nsrc = find_elements (client, client->doc, nsrc->children, src + 1, &rc,
3426 NULL, NULL, NULL, 0, 0, NULL, 0);
3428 if (!nsrc)
3429 goto fail;
3431 rc = is_element_owner (client, nsrc);
3432 if (rc)
3433 goto fail;
3435 if (dst)
3437 if (!valid_element_path (dst, 0))
3439 rc = GPG_ERR_INV_VALUE;
3440 goto fail;
3443 ndst = find_root_element (client, client->doc, &dst, &rc, NULL, 0, 0);
3444 if (ndst && dst[1])
3445 ndst = find_elements (client, client->doc, ndst->children, dst + 1,
3446 &rc, NULL, NULL, NULL, 0, 0, NULL, 0);
3448 else
3449 ndst = xmlDocGetRootElement (client->doc);
3451 for (xmlNodePtr n = ndst; n; n = n->parent)
3453 if (n == nsrc)
3455 rc = GPG_ERR_CONFLICT;
3456 goto fail;
3460 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3461 goto fail;
3463 rc = 0;
3465 if (ndst)
3467 xmlChar *a = node_has_attribute (nsrc, (xmlChar *) "_name");
3469 xmlNodePtr dup = find_element (client, ndst->children, (char *) a,
3470 NULL, &rc);
3471 xmlFree (a);
3473 if (rc)
3474 goto fail;
3476 if (dup)
3478 if (dup == nsrc)
3479 goto fail;
3481 if (ndst == xmlDocGetRootElement (client->doc))
3483 xmlNodePtr n = nsrc;
3484 int match = 0;
3486 while (n->parent && n->parent != ndst)
3487 n = n->parent;
3489 xmlChar *a = node_has_attribute (n, (xmlChar *) "_name");
3490 xmlChar *b = node_has_attribute (nsrc, (xmlChar *) "_name");
3492 if (xmlStrEqual (a, b))
3494 match = 1;
3495 xmlUnlinkNode (nsrc);
3496 xmlUnlinkNode (n);
3497 xmlFreeNodeList (n);
3500 xmlFree (a);
3501 xmlFree (b);
3503 if (!match)
3505 xmlUnlinkNode (dup);
3506 xmlFreeNodeList (dup);
3509 else
3510 xmlUnlinkNode (dup);
3514 if (!ndst && dst)
3516 xmlChar *name = node_has_attribute (nsrc, (xmlChar *) "_name");
3518 if (nsrc->parent == xmlDocGetRootElement (client->doc)
3519 && !strcmp ((char *) name, *dst))
3521 xmlFree (name);
3522 rc = GPG_ERR_CONFLICT;
3523 goto fail;
3526 xmlFree (name);
3527 ndst = create_element_path (client, &dst, &rc, nsrc);
3530 if (!ndst)
3531 goto fail;
3533 update_element_mtime (client, nsrc->parent);
3534 xmlUnlinkNode (nsrc);
3535 ndst = xmlAddChildList (ndst, nsrc);
3537 if (!ndst)
3538 rc = GPG_ERR_ENOMEM;
3539 else
3540 update_element_mtime (client, ndst->parent);
3542 fail:
3543 if (req)
3544 strv_free (req);
3546 if (src)
3547 strv_free (src);
3549 if (dst)
3550 strv_free (dst);
3552 return rc;
3555 static gpg_error_t
3556 move_command (assuan_context_t ctx, char *line)
3558 struct client_s *client = assuan_get_pointer (ctx);
3559 gpg_error_t rc;
3560 struct argv_s *args[] = {
3561 &(struct argv_s) {"inquire", OPTION_TYPE_NOARG, parse_opt_inquire},
3562 NULL
3565 rc = parse_options (&line, args, client);
3566 if (rc)
3567 return send_error (ctx, rc);
3569 if (client->opts & OPT_INQUIRE)
3571 unsigned char *result;
3572 size_t len;
3574 rc = assuan_inquire (ctx, "DATA", &result, &len, 0);
3575 if (rc)
3576 return send_error (ctx, rc);
3578 pthread_cleanup_push (xfree, result);
3579 rc = do_move (ctx, (char *)result);
3580 pthread_cleanup_pop (1);
3582 else
3583 rc = do_move (ctx, line);
3585 return send_error (ctx, rc);
3588 static gpg_error_t
3589 ls_command (assuan_context_t ctx, char *line)
3591 gpg_error_t rc;
3592 char *tmp = str_asprintf ("%s/data", homedir);
3593 char *dir = expand_homedir (tmp);
3594 DIR *d = opendir (dir);
3596 rc = gpg_error_from_errno (errno);
3597 xfree (tmp);
3599 if (!d)
3601 xfree (dir);
3602 return send_error (ctx, rc);
3605 size_t len =
3606 offsetof (struct dirent, d_name) +pathconf (dir, _PC_NAME_MAX) + 1;
3607 struct dirent *p = xmalloc (len), *cur = NULL;
3608 char *list = NULL;
3610 xfree (dir);
3611 pthread_cleanup_push (xfree, p);
3612 pthread_cleanup_push (closedir, d);
3613 rc = 0;
3615 while (!readdir_r (d, p, &cur) && cur)
3617 if (cur->d_name[0] == '.' && cur->d_name[1] == '\0')
3618 continue;
3619 else if (cur->d_name[0] == '.' && cur->d_name[1] == '.'
3620 && cur->d_name[2] == '\0')
3621 continue;
3623 tmp = str_asprintf ("%s%s\n", list ? list : "", cur->d_name);
3625 if (!tmp)
3627 if (list)
3628 xfree (list);
3630 rc = GPG_ERR_ENOMEM;
3631 break;
3634 xfree (list);
3635 list = tmp;
3638 pthread_cleanup_pop (1); // closedir (d)
3639 pthread_cleanup_pop (1); // xfree (p)
3641 if (rc)
3642 return send_error (ctx, rc);
3644 if (!list)
3645 return send_error (ctx, GPG_ERR_NO_DATA);
3647 list[strlen (list) - 1] = 0;
3648 pthread_cleanup_push (xfree, list);
3649 rc = xfer_data (ctx, list, strlen (list));
3650 pthread_cleanup_pop (1);
3651 return send_error (ctx, rc);
3654 static gpg_error_t
3655 bye_notify (assuan_context_t ctx, char *line)
3657 struct client_s *cl = assuan_get_pointer (ctx);
3659 cl->thd->state = CLIENT_STATE_DISCON;
3661 #ifdef WITH_GNUTLS
3662 if (cl->thd->remote)
3664 int rc;
3668 struct timeval tv = { 0, 50000 };
3670 rc = gnutls_bye (cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3671 if (rc == GNUTLS_E_AGAIN)
3672 select (0, NULL, NULL, NULL, &tv);
3674 while (rc == GNUTLS_E_AGAIN);
3676 #endif
3678 /* This will let assuan_process_next() return. */
3679 fcntl (cl->thd->fd, F_SETFL, O_NONBLOCK);
3680 cl->last_rc = 0; // BYE command result
3681 return 0;
3684 static gpg_error_t
3685 reset_notify (assuan_context_t ctx, char *line)
3687 struct client_s *client = assuan_get_pointer (ctx);
3689 if (client)
3690 cleanup_client (client);
3692 return 0;
3696 * This is called before every Assuan command.
3698 static gpg_error_t
3699 command_startup (assuan_context_t ctx, const char *name)
3701 struct client_s *client = assuan_get_pointer (ctx);
3702 gpg_error_t rc;
3703 struct command_table_s *cmd = NULL;
3705 log_write2 ("command='%s'", name);
3706 client->last_rc = client->opts = 0;
3708 for (int i = 0; command_table[i]; i++)
3710 if (!strcasecmp (name, command_table[i]->name))
3712 if (command_table[i]->ignore_startup)
3713 return 0;
3714 cmd = command_table[i];
3715 break;
3719 client->last_rc = rc = gpg_error (file_modified (client, cmd));
3720 if (!rc)
3721 update_client_state (client, CLIENT_STATE_COMMAND);
3723 return rc;
3727 * This is called after every Assuan command.
3729 static void
3730 command_finalize (assuan_context_t ctx, gpg_error_t rc)
3732 struct client_s *client = assuan_get_pointer (ctx);
3734 if (!(client->flags & FLAG_LOCK_CMD))
3735 unlock_file_mutex (client, 0);
3737 log_write2 (_("command completed: rc=%u"), rc ? rc : client->last_rc);
3738 client->last_rc = gpg_error (GPG_ERR_UNKNOWN_COMMAND);
3739 #ifdef WITH_GNUTLS
3740 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
3741 #endif
3742 update_client_state (client, CLIENT_STATE_IDLE);
3745 static gpg_error_t
3746 help_command (assuan_context_t ctx, char *line)
3748 gpg_error_t rc;
3749 int i;
3751 if (!line || !*line)
3753 char *tmp;
3754 char *help = str_dup (_("Usage: HELP [<COMMAND>]\n"
3755 "For commands that take an element path as an argument, each element is "
3756 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3757 "\n" "COMMANDS:"));
3759 for (i = 0; command_table[i]; i++)
3761 if (!command_table[i]->help)
3762 continue;
3764 tmp = str_asprintf ("%s %s", help, command_table[i]->name);
3765 xfree (help);
3766 help = tmp;
3769 tmp = strip_texi_and_wrap (help);
3770 xfree (help);
3771 pthread_cleanup_push (xfree, tmp);
3772 rc = xfer_data (ctx, tmp, strlen (tmp));
3773 pthread_cleanup_pop (1);
3774 return send_error (ctx, rc);
3777 for (i = 0; command_table[i]; i++)
3779 if (!strcasecmp (line, command_table[i]->name))
3781 char *help, *tmp;
3783 if (!command_table[i]->help)
3784 break;
3786 help = strip_texi_and_wrap (command_table[i]->help);
3787 tmp = str_asprintf (_("Usage: %s"), help);
3788 xfree (help);
3789 pthread_cleanup_push (xfree, tmp);
3790 rc = xfer_data (ctx, tmp, strlen (tmp));
3791 pthread_cleanup_pop (1);
3792 return send_error (ctx, rc);
3796 return send_error (ctx, GPG_ERR_INV_NAME);
3799 static void
3800 new_command (const char *name, int ignore, int unlock,
3801 gpg_error_t (*handler) (assuan_context_t, char *),
3802 const char *help)
3804 int i = 0;
3806 if (command_table)
3807 for (i = 0; command_table[i]; i++);
3809 command_table =
3810 xrealloc (command_table, (i + 2) * sizeof (struct command_table_s *));
3811 command_table[i] = xcalloc (1, sizeof (struct command_table_s));
3812 command_table[i]->name = name;
3813 command_table[i]->handler = handler;
3814 command_table[i]->ignore_startup = ignore;
3815 command_table[i]->unlock = unlock;
3816 command_table[i++]->help = help;
3817 command_table[i] = NULL;
3820 void
3821 deinit_commands ()
3823 int i;
3825 for (i = 0; command_table[i]; i++)
3826 xfree (command_table[i]);
3828 xfree (command_table);
3831 static int
3832 sort_commands (const void *arg1, const void *arg2)
3834 struct command_table_s *const *a = arg1;
3835 struct command_table_s *const *b = arg2;
3837 if (!*a || !*b)
3838 return 0;
3839 else if (*a && !*b)
3840 return 1;
3841 else if (!*a && *b)
3842 return -1;
3844 return strcmp ((*a)->name, (*b)->name);
3847 // FIXME cleanup/implement options
3848 static gpg_error_t
3849 passwd_command (assuan_context_t ctx, char *line)
3851 struct client_s *client = assuan_get_pointer (ctx);
3852 gpg_error_t rc;
3853 struct argv_s *args[] = {
3854 &(struct argv_s) {"no-passphrase", OPTION_TYPE_NOARG, parse_opt_no_passphrase},
3855 NULL
3858 rc = peer_is_invoker (client);
3859 if (rc == GPG_ERR_EACCES)
3860 return send_error (ctx, rc);
3862 if (client->flags & FLAG_NEW)
3863 return send_error (ctx, GPG_ERR_INV_STATE);
3865 rc = parse_options (&line, args, client);
3866 if (rc)
3867 return send_error (ctx, rc);
3869 rc = crypto_init_ctx (client->crypto, client->flags & FLAG_NO_PINENTRY);
3870 if (rc)
3871 return send_error (ctx, rc);
3873 if (!rc && client->opts & OPT_RESET)
3875 rc = cache_clear (client->md5file);
3876 if (!rc)
3877 send_status_all (STATUS_CACHE, NULL);
3880 if (!rc)
3881 rc = crypto_passwd (client, client->crypto);
3883 crypto_cleanup_non_keys (client->crypto);
3884 return send_error (ctx, rc);
3887 static gpg_error_t
3888 parse_opt_data (void *data, void *value)
3890 struct client_s *client = data;
3892 (void) value;
3893 client->opts |= OPT_DATA;
3894 return 0;
3897 static gpg_error_t
3898 send_client_list (assuan_context_t ctx)
3900 struct client_s *client = assuan_get_pointer (ctx);
3901 gpg_error_t rc = 0;
3902 char buf[ASSUAN_LINELENGTH];
3904 if (client->opts & OPT_VERBOSE)
3906 unsigned i, t;
3907 char **list = NULL;
3908 char *line;
3909 int with_state = config_get_boolean ("global", "send_state");
3911 MUTEX_LOCK (&cn_mutex);
3912 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
3913 t = slist_length (cn_thread_list);
3915 for (i = 0; i < t; i++)
3917 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
3918 pthread_t self = pthread_self ();
3920 if (!strv_printf (&list, "%p %s %s %s %u %u %u",
3921 thd->tid,
3922 thd->name ? thd->name : "-",
3923 thd->cl && thd->cl->filename
3924 ? thd->cl->filename : "/",
3925 #ifdef WITH_GNUTLS
3926 thd->remote ? thd->peeraddr : "-",
3927 #else
3928 "-",
3929 #endif
3930 thd->cl && thd->cl->flags & FLAG_HAS_LOCK ? 1 : 0,
3931 self == thd->tid ? 1 : 0,
3932 with_state ? thd->state : CLIENT_STATE_UNKNOWN
3935 strv_free (list);
3936 rc = GPG_ERR_ENOMEM;
3937 break;
3941 pthread_cleanup_pop (1);
3942 if (rc)
3943 return rc;
3945 line = strv_join ("\n", list);
3946 strv_free (list);
3947 pthread_cleanup_push (xfree, line);
3948 rc = xfer_data (ctx, line, strlen (line));
3949 pthread_cleanup_pop (1);
3950 return rc;
3953 if (client->opts & OPT_DATA)
3955 MUTEX_LOCK (&cn_mutex);
3956 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
3957 snprintf (buf, sizeof (buf), "%i", slist_length (cn_thread_list));
3958 pthread_cleanup_pop (1);
3959 rc = xfer_data (ctx, buf, strlen (buf));
3961 else
3962 rc = send_status (ctx, STATUS_CLIENTS, NULL);
3964 return rc;
3967 static gpg_error_t
3968 getinfo_command (assuan_context_t ctx, char *line)
3970 struct client_s *client = assuan_get_pointer (ctx);
3971 gpg_error_t rc;
3972 char buf[ASSUAN_LINELENGTH];
3973 struct argv_s *args[] = {
3974 &(struct argv_s) {"data", OPTION_TYPE_NOARG, parse_opt_data},
3975 &(struct argv_s) {"verbose", OPTION_TYPE_NOARG, parse_opt_verbose},
3976 NULL
3979 rc = parse_options (&line, args, client);
3980 if (rc)
3981 return send_error (ctx, rc);
3983 if (!strcasecmp (line, "clients"))
3985 rc = send_client_list (ctx);
3987 else if (!strcasecmp (line, "cache"))
3989 if (client->opts & OPT_DATA)
3991 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
3992 rc = xfer_data (ctx, buf, strlen (buf));
3994 else
3995 rc = send_status (ctx, STATUS_CACHE, NULL);
3997 else if (!strcasecmp (line, "pid"))
3999 char buf[32];
4000 pid_t pid = getpid ();
4002 snprintf (buf, sizeof (buf), "%i", pid);
4003 rc = xfer_data (ctx, buf, strlen (buf));
4005 else if (!strcasecmp (line, "version"))
4007 char *buf = str_asprintf ("0x%06x %s", VERSION_HEX,
4008 #ifdef WITH_GNUTLS
4009 "GNUTLS "
4010 #endif
4011 "");
4012 rc = xfer_data (ctx, buf, strlen (buf));
4013 xfree (buf);
4015 else if (!strcasecmp (line, "last_error"))
4017 if (client->last_error)
4018 rc = xfer_data (ctx, client->last_error, strlen (client->last_error));
4019 else
4020 rc = GPG_ERR_NO_DATA;
4022 else if (!strcasecmp (line, "user"))
4024 char *user = NULL;
4026 #ifdef WITH_GNUTLS
4027 if (client->thd->remote)
4028 user = str_asprintf ("#%s", client->thd->tls->fp);
4029 else
4030 user = get_username (client->thd->peer->uid);
4031 #else
4032 user = get_username (client->thd->peer->uid);
4033 #endif
4034 if (user)
4036 pthread_cleanup_push (xfree, user);
4037 rc = xfer_data (ctx, user, strlen (user));
4038 pthread_cleanup_pop (1);
4040 else
4041 rc = GPG_ERR_NO_DATA;
4043 else
4044 rc = gpg_error (GPG_ERR_SYNTAX);
4046 return send_error (ctx, rc);
4049 static gpg_error_t
4050 parse_listkeys_opt_secret_only (void *data, void *value)
4052 struct client_s *client = data;
4054 (void) value;
4055 client->opts |= OPT_SECRET_ONLY;
4056 return 0;
4059 static gpg_error_t
4060 keyinfo_command (assuan_context_t ctx, char *line)
4062 struct client_s *client = assuan_get_pointer (ctx);
4063 gpg_error_t rc = 0;
4064 char **keys = NULL, **p;
4066 if (!(client->flags & FLAG_OPEN))
4067 return send_error (ctx, GPG_ERR_INV_STATE);
4069 if (client->flags & FLAG_NEW)
4070 return send_error (ctx, GPG_ERR_NO_DATA);
4072 p = strv_catv(keys, client->crypto->pubkey);
4073 if (p)
4075 keys = p;
4076 for (p = client->crypto->sigkey; p && *p; p++)
4078 if (!strv_printf(&keys, "S%s", *p))
4080 strv_free (keys);
4081 return send_error (ctx, GPG_ERR_ENOMEM);
4085 else
4086 rc = GPG_ERR_ENOMEM;
4088 if (!rc)
4090 line = strv_join ("\n", keys);
4091 strv_free (keys);
4092 pthread_cleanup_push (xfree, line);
4093 if (line)
4094 rc = xfer_data (ctx, line, strlen (line));
4095 else
4096 rc = GPG_ERR_ENOMEM;
4098 pthread_cleanup_pop (1);
4100 else
4101 strv_free (keys);
4103 return send_error (ctx, rc);
4106 static gpg_error_t
4107 kill_command (assuan_context_t ctx, char *line)
4109 #ifdef HAVE_PTHREAD_CANCEL
4110 struct client_s *client = assuan_get_pointer (ctx);
4111 gpg_error_t rc;
4113 if (!line || !*line)
4114 return send_error (ctx, GPG_ERR_SYNTAX);
4116 rc = peer_is_invoker (client);
4117 if (!rc)
4119 unsigned i, t;
4121 MUTEX_LOCK (&cn_mutex);
4122 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
4123 t = slist_length (cn_thread_list);
4124 rc = GPG_ERR_ESRCH;
4126 for (i = 0; i < t; i++)
4128 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
4129 char *tmp = str_asprintf ("%p", thd->tid);
4131 if (strcmp (line, tmp))
4133 xfree (tmp);
4134 continue;
4137 xfree (tmp);
4138 rc = pthread_cancel (thd->tid);
4139 break;
4142 pthread_cleanup_pop (1);
4145 return send_error (ctx, rc);
4146 #else
4147 return send_error (ctx, GPG_ERR_NOT_IMPLEMENTED);
4148 #endif
4151 static gpg_error_t
4152 listkeys_command (assuan_context_t ctx, char *line)
4154 struct client_s *client = assuan_get_pointer (ctx);
4155 struct crypto_s *crypto = NULL;
4156 char **pattern = NULL;
4157 gpgme_key_t *keys = NULL;
4158 char **result = NULL;
4159 gpg_error_t rc;
4160 struct argv_s *args[] = {
4161 &(struct argv_s) {"secret-only", OPTION_TYPE_NOARG,
4162 parse_listkeys_opt_secret_only },
4163 NULL
4166 rc = peer_is_invoker(client);
4167 if (rc)
4168 return send_error (ctx, rc);
4170 rc = parse_options (&line, args, client);
4171 if (rc)
4172 return send_error (ctx, rc);
4174 rc = crypto_init (&crypto, client->ctx, client->filename,
4175 client->flags & FLAG_NO_PINENTRY);
4176 if (rc)
4177 return send_error (ctx, rc);
4179 pthread_cleanup_push (crypto_cleanup, crypto);
4180 pattern = str_split (line, ",", 0);
4181 pthread_cleanup_push (strv_free, pattern);
4182 rc = crypto_list_keys (crypto, pattern, client->opts & OPT_SECRET_ONLY,
4183 &keys);
4184 pthread_cleanup_pop (1);
4185 pthread_cleanup_pop (1);
4186 if (!rc)
4188 int i;
4190 for (i = 0; keys[i]; i++)
4192 char *p = crypto_key_info (keys[i]);
4193 char **r;
4195 if (!p)
4197 rc = GPG_ERR_ENOMEM;
4198 break;
4201 r = strv_cat (result, p);
4202 if (!r)
4204 rc = GPG_ERR_ENOMEM;
4205 break;
4208 result = r;
4211 if (!i)
4212 rc = GPG_ERR_NO_DATA;
4214 crypto_free_key_list (keys);
4217 if (!rc)
4219 line = strv_join ("\n", result);
4220 strv_free (result);
4221 pthread_cleanup_push (xfree, line);
4222 if (!line)
4223 rc = GPG_ERR_ENOMEM;
4224 else
4225 rc = xfer_data (ctx, line, strlen (line));
4227 pthread_cleanup_pop (1);
4229 else
4230 strv_free (result);
4232 return send_error (ctx, rc);
4235 void
4236 init_commands ()
4238 /* !BEGIN-HELP-TEXT!
4240 * This comment is used as a marker to generate the offline documentation
4241 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4242 * script to determine where commands begin and end.
4244 new_command("HELP", 1, 1, help_command, _(
4245 "HELP [<COMMAND>]\n"
4246 "Show available commands or command specific help text."
4249 new_command("KILL", 1, 0, kill_command, _(
4250 "KILL <thread_id>\n"
4251 "Terminates the client identified by @var{thread_id} and releases any file "
4252 "lock or other resources it has held. @xref{GETINFO} for details about listing "
4253 "connected clients.\n"
4256 new_command("LISTKEYS", 1, 0, listkeys_command, _(
4257 "LISTKEYS [--secret-only] [pattern[,<pattern>]]\n"
4258 "Returns a new line separated list of key information matching a comma "
4259 "separated list of @var{pattern}'s. When option @option{--secret-only} is "
4260 "specified, only keys matching @var{pattern} that also have a secret "
4261 "available key will be returned."
4264 new_command("KEYINFO", 1, 1, keyinfo_command, _(
4265 "KEYINFO\n"
4266 "Returns a new line separated list of key ID's that the currently opened "
4267 "data file has recipients and signers for. If the key is a signing key it "
4268 "will be prefixed with an @key{S}. If the file is a new one the error code "
4269 "GPG_ERR_NO_DATA is returned."
4272 new_command("GETINFO", 1, 1, getinfo_command, _(
4273 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4274 "Get server and other information: @var{CACHE} returns the number of cached "
4275 "documents via a status message. @var{CLIENTS} returns the number of "
4276 "connected clients via a status message or a list of connected clients when "
4277 "the @option{--verbose} parameter is used. The list contains space delimited "
4278 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4279 "file lock status, whether the current client is self and a client state. "
4280 "Client state @code{1} indicates that the client is idle, @code{2} means the "
4281 "client is in a command and @code{3} means the client is disconnecting. This "
4282 "line is always returned with a data response. @var{PID} returns the process "
4283 "ID number of the server via a data response. @var{VERSION} returns the server "
4284 "version number and compile-time features with a data response with each "
4285 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4286 "the last failed command when available. @var{USER} returns the username or "
4287 "@abbr{TLS} hash of the connected client. @xref{Status Messages}. "
4288 "\n"
4289 "When the @option{--data} option is specified then the result will be sent "
4290 "via a data response rather than a status message."
4293 new_command("PASSWD", 0, 0, passwd_command, _(
4294 "PASSWD\n"
4295 "Changes the passphrase of the secret key required to open the current "
4296 "data file."
4297 "\n"
4298 "This command is not available for non-invoking clients "
4299 "(@pxref{Access Control})."
4302 new_command("OPEN", 1, 1, open_command, _(
4303 "OPEN [--lock] <filename>\n"
4304 "Opens @var{filename}. When the @var{filename} is not found on the "
4305 "file-system then a new document will be created. If the file is found, it "
4306 "is looked for in the file cache and when found no passphrase will be "
4307 "required to open it. When not cached, @cite{pinentry(1)} will be used to "
4308 "retrieve the passphrase for decryption unless @option{disable-pinentry} "
4309 "(@pxref{OPTION}) was specified in which case @command{pwmd} will "
4310 "@emph{INQUIRE} the client for the passphrase."
4311 "\n"
4312 "When the @option{--lock} option is passed then the file mutex will be "
4313 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4314 "file had been opened."
4317 new_command("SAVE", 0, 0, save_command, _(
4318 "SAVE [--inquire-keyparam] [--keyid=<keyid>[,...] | [--inquire-keyid]] [--sign-keyid=<fingerprint>[,...] | [--inquire-sign-keyid]]\n"
4319 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4320 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4321 "new one or the option @option{--inquire-keyparam} was passed, a new keypair "
4322 "will be generated and @cite{pinentry(1)} will be used to prompt for the "
4323 "passphrase to encrypt with."
4324 "\n"
4325 "The @option{--inquire-keyparam} option will send an "
4326 "@emph{INQUIRE} to the client to obtain the key parameters to use for "
4327 "generating the new keypair. The inquired data is expected to be in "
4328 "@cite{gnupg} @abbr{XML} format. See the @cite{gnupg} documentation for "
4329 "details. Note that when this option is specified a new keypair will be "
4330 "generated reguardless if the file is a new one and that the passphrase for "
4331 "the current file will be required before generating the new keypair. This "
4332 "option is not available for non-invoking clients (@pxref{Access Control})."
4333 "\n"
4334 "You can encrypt the data file to a recipient other than the one that it "
4335 "was encrypted with by passing the @option{--keyid} or "
4336 "@option{--inquire-keyid} option with "
4337 "the key ID of a public encryption key as its argument. Use the "
4338 "@command{LISTKEYS} command (@pxref{LISTKEYS}) to show key information by "
4339 "pattern. The @option{--sign-keyid} or @option{--inquire-sign-keyid} option "
4340 "may also be used to sign the data "
4341 "file with an alternate key by specifying the fingerprint of a secret key. "
4342 "A passphrase to decrypt the data file "
4343 "will be required if one or more of the original encryption or signing keys "
4344 "are not found in either of these two options' arguments. The original "
4345 "encryption or signing keys will be used when either of these options are "
4346 "not specified."
4347 "\n"
4348 "Note that the @option{--keyid} option requires a key ID and not a "
4349 "fingerprint and the @option{--sign-keyid} option requires a fingerprint do "
4350 "to the " "way recipients and signers are obtained when @command{OPEN}'ing a "
4351 "file. These options are not available for non-invoking clients "
4352 "(@pxref{Access Control}) when the recipients or signers do not match those "
4353 "that were used when the file was @code{OPEN}'ed."
4356 new_command("ISCACHED", 1, 0, iscached_command, _(
4357 "ISCACHED [--lock] <filename>\n"
4358 "An @emph{OK} response is returned if the specified @var{filename} is found "
4359 "in the file cache. If not found in the cache but exists on the filesystem "
4360 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4361 "returned."
4362 "\n"
4363 "The @option{--lock} option will lock the file mutex of @var{filename} when "
4364 "the file exists; it does not need to be opened nor cached. The lock will be "
4365 "released when the client exits or sends the @code{UNLOCK} command "
4366 "(@pxref{UNLOCK})."
4369 new_command("CLEARCACHE", 1, 1, clearcache_command, _(
4370 "CLEARCACHE [<filename>]\n"
4371 "Clears a file cache entry for all or the specified @var{filename}."
4374 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command, _(
4375 "CACHETIMEOUT <filename> <seconds>\n"
4376 "The time in @var{seconds} until @var{filename} will be removed from the "
4377 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4378 "the passphrase for each @code{OPEN} command (@pxref{OPEN}) or @code{SAVE} "
4379 "(@pxref{SAVE}) command. @xref{Configuration}, and the @code{cache_timeout} "
4380 "parameter."
4383 new_command("LIST", 0, 1, list_command, _(
4384 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4385 "If no element path is given then a newline separated list of root elements "
4386 "is returned with a data response. If given, then all reachable elements "
4387 "of the specified element path are returned unless the @option{--no-recurse} "
4388 "option is specified. If specified, only the child elements of the element "
4389 "path are returned without recursing into grandchildren. Each resulting "
4390 "element is prefixed with the literal @code{!} character when the element "
4391 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4392 "\n"
4393 "When the @option{--verbose} option is passed then each element path "
4394 "returned will have zero or more flags appened to it. These flags are "
4395 "delimited from the element path by a single space character. A flag itself "
4396 "is a single character. Flag @code{P} indicates that access to the element "
4397 "is denied. Flag @code{+} indicates that there are child nodes of "
4398 "the current element path. Flag @code{E} indicates that an element of an "
4399 "element path contained in a @var{target} attribute could not be found. Flag "
4400 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4401 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4402 "of the @var{target} attribute contained in the current element (see below)."
4403 "\n"
4404 "The @option{--with-target} option implies @option{--verbose} and will append "
4405 "an additional flag @code{T} followed by a single space then an element path. "
4406 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4407 "current element when it contains a @var{target} attribute. When no "
4408 "@var{target} attribute is found then no @code{T} flag will be appended."
4409 "\n"
4410 "The @option{--no-recurse} option limits the amount of data returned to only "
4411 "the listing of children of the specified element path and not any "
4412 "grandchildren."
4413 "\n"
4414 "The @option{--all} option lists the entire element tree for each root "
4415 "element. This option also implies option @option{--verbose}."
4416 "\n"
4417 "When the @option{--inquire} option is passed then all remaining non-option "
4418 "arguments are retrieved via a server @emph{INQUIRE}."
4421 new_command("REALPATH", 0, 1, realpath_command, _(
4422 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4423 "Resolves all @code{target} attributes of the specified element path and "
4424 "returns the result with a data response. @xref{Target Attribute}, for details."
4425 "\n"
4426 "When the @option{--inquire} option is passed then all remaining non-option "
4427 "arguments are retrieved via a server @emph{INQUIRE}."
4430 new_command("STORE", 0, 1, store_command, _(
4431 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4432 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4433 "\n"
4434 "Creates a new element path or modifies the @var{content} of an existing "
4435 "element. If only a single element is specified then a new root element is "
4436 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4437 "set to the final @key{TAB} delimited element. If no @var{content} is "
4438 "specified after the final @key{TAB}, then the content of an existing "
4439 "element will be removed; or empty if creating a new element."
4440 "\n"
4441 "The only restriction of an element name is that it not contain whitespace "
4442 "or begin with the literal element character @code{!} unless specifying a "
4443 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4444 "the @key{TAB} delimited elements. It is recommended that the content of an "
4445 "element be base64 encoded when it contains control or @key{TAB} characters "
4446 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
4449 new_command("RENAME", 0, 1, rename_command, _(
4450 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4451 "Renames the specified @var{element} to the new @var{value}. If an element of "
4452 "the same name as the @var{value} already exists it will be overwritten."
4453 "\n"
4454 "When the @option{--inquire} option is passed then all remaining non-option "
4455 "arguments are retrieved via a server @emph{INQUIRE}."
4458 new_command("COPY", 0, 1, copy_command, _(
4459 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4460 "Copies the entire element tree starting from the child node of the source "
4461 "element, to the destination element path. If the destination element path "
4462 "does not exist then it will be created; otherwise it is overwritten."
4463 "\n"
4464 "Note that attributes from the source element are merged into the "
4465 "destination element when the destination element path exists. When an "
4466 "attribute of the same name exists in both the source and destination "
4467 "elements then the destination attribute will be updated to the source "
4468 "attribute value."
4469 "\n"
4470 "When the @option{--inquire} option is passed then all remaining non-option "
4471 "arguments are retrieved via a server @emph{INQUIRE}."
4474 new_command("MOVE", 0, 1, move_command, _(
4475 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4476 "Moves the source element path to the destination element path. If the "
4477 "destination is not specified then it will be moved to the root node of the "
4478 "document. If the destination is specified and exists then it will be "
4479 "overwritten; otherwise non-existing elements of the destination element "
4480 "path will be created."
4481 "\n"
4482 "When the @option{--inquire} option is passed then all remaining non-option "
4483 "arguments are retrieved via a server @emph{INQUIRE}."
4486 new_command("DELETE", 0, 1, delete_command, _(
4487 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4488 "Removes the specified element path and all of its children. This may break "
4489 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4490 "refers to this element or any of its children."
4491 "\n"
4492 "When the @option{--inquire} option is passed then all remaining non-option "
4493 "arguments are retrieved via a server @emph{INQUIRE}."
4496 new_command("GET", 0, 1, get_command, _(
4497 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4498 "Retrieves the content of the specified element. The content is returned "
4499 "with a data response."
4500 "\n"
4501 "When the @option{--inquire} option is passed then all remaining non-option "
4502 "arguments are retrieved via a server @emph{INQUIRE}."
4505 new_command("ATTR", 0, 1, attr_command, _(
4506 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4507 "@table @asis\n"
4508 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4509 "\n"
4510 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4511 "element. When no @var{value} is specified any existing value will be removed."
4512 "\n"
4513 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4514 "\n"
4515 " Removes an @var{attribute} from an element."
4516 "\n"
4517 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4518 "\n"
4519 " Retrieves a newline separated list of attributes names and values "
4520 "from the specified element. Each attribute name and value is space delimited."
4521 "\n"
4522 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4523 "\n"
4524 " Retrieves the value of an @var{attribute} from an element."
4525 "@end table\n"
4526 "\n"
4527 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4528 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4529 "commands instead."
4530 "\n"
4531 "The @code{_mtime} attribute is updated each time an element is modified by "
4532 "either storing content, editing attributes or by deleting a child element. "
4533 "The @code{_ctime} attribute is created for each new element in an element "
4534 "path."
4535 "\n"
4536 "When the @option{--inquire} option is passed then all remaining non-option "
4537 "arguments are retrieved via a server @emph{INQUIRE}."
4538 "\n"
4539 "@xref{Target Attribute}, for details about this special attribute."
4542 new_command("XPATH", 0, 1, xpath_command, _(
4543 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4544 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4545 "specified it is assumed the expression is a request to return a result. "
4546 "Otherwise, the result is set to the @var{value} argument and the document is "
4547 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4548 "is assumed to be empty and the document is updated. For example:"
4549 "@sp 1\n"
4550 "@example\n"
4551 "XPATH //element[@@_name='password']@key{TAB}\n"
4552 "@end example\n"
4553 "@sp 1"
4554 "would clear the content of all @code{password} elements in the data file "
4555 "while leaving off the trailing @key{TAB} would return all @code{password} "
4556 "elements in @abbr{XML} format."
4557 "\n"
4558 "When the @option{--inquire} option is passed then all remaining non-option "
4559 "arguments are retrieved via a server @emph{INQUIRE}."
4560 "\n"
4561 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4562 "expression syntax."
4565 new_command("XPATHATTR", 0, 1, xpathattr_command, _(
4566 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4567 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4568 "attributes and does not return a result. For the @var{SET} operation the "
4569 "@var{value} is optional but the field is required. If not specified then "
4570 "the attribute value will be empty. For example:"
4571 "@sp 1"
4572 "@example\n"
4573 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4574 "@end example\n"
4575 "@sp 1"
4576 "would create an @code{password} attribute for each @code{password} element "
4577 "found in the document. The attribute value will be empty but still exist."
4578 "\n"
4579 "When the @option{--inquire} option is passed then all remaining non-option "
4580 "arguments are retrieved via a server @emph{INQUIRE}."
4581 "\n"
4582 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4583 "expression syntax."
4586 new_command("IMPORT", 0, 1, import_command, _(
4587 "IMPORT [--root [!]element[<TAB>[!]child[..]]] <content>\n"
4588 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4589 "\n"
4590 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4591 "argument is raw @abbr{XML} data. The content is created as a child of "
4592 "the element path specified with the @option{--root} option or at the "
4593 "document root when not specified. Existing elements of the same name will "
4594 "be overwritten."
4595 "\n"
4596 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4597 "for details."
4600 new_command("DUMP", 0, 1, dump_command, _(
4601 "DUMP\n"
4602 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4603 "dumping a specific node."
4606 new_command("LOCK", 0, 0, lock_command, _(
4607 "LOCK\n"
4608 "Locks the mutex associated with the opened file. This prevents other clients "
4609 "from sending commands to the same opened file until the client "
4610 "that sent this command either disconnects or sends the @code{UNLOCK} "
4611 "command. @xref{UNLOCK}."
4614 new_command("UNLOCK", 1, 0, unlock_command, _(
4615 "UNLOCK\n"
4616 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4617 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4618 "@pxref{ISCACHED})."
4621 new_command("GETCONFIG", 1, 1, getconfig_command, _(
4622 "GETCONFIG [filename] <parameter>\n"
4623 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4624 "data response. If no file has been opened then the value for @var{filename} "
4625 "or the default from the @samp{global} section will be returned. If a file "
4626 "has been opened and no @var{filename} is specified, a value previously "
4627 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4630 new_command("OPTION", 1, 1, option_command, _(
4631 "OPTION <NAME>=<VALUE>\n"
4632 "Sets a client option @var{name} to @var{value}. The value for an option is "
4633 "kept for the duration of the connection."
4634 "\n"
4635 "@table @asis\n"
4636 "@item DISABLE-PINENTRY\n"
4637 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
4638 "server inquire is sent to the client to obtain the passphrase. This option "
4639 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4640 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
4641 "\n"
4642 "@item NAME\n"
4643 "Associates the thread ID of the connection with the specified textual "
4644 "representation. Useful for debugging log messages. May not contain whitespace."
4645 "\n"
4646 "@item LOCK-TIMEOUT\n"
4647 "When not @code{0}, the duration in tenths of a second to wait for the file "
4648 "mutex which has been locked by another thread to be released before returning "
4649 "an error. When @code{-1}, then an error will be returned immediately."
4650 "@end table\n"
4653 new_command("LS", 1, 1, ls_command, _(
4654 "LS\n"
4655 "Returns a newline separated list of data files stored in the data directory "
4656 "@file{HOMEDIR/data} (@pxref{Invoking}) with a data response."
4659 new_command("RESET", 1, 1, NULL, _(
4660 "RESET\n"
4661 "Closes the currently opened file but keeps any previously set client options."
4664 new_command("NOP", 1, 1, NULL, _(
4665 "NOP\n"
4666 "Does nothing. Always returns successfully."
4669 /* !END-HELP-TEXT! */
4670 new_command ("CANCEL", 1, 1, NULL, NULL);
4671 new_command ("END", 1, 1, NULL, NULL);
4672 new_command ("BYE", 1, 1, NULL, NULL);
4674 int i;
4675 for (i = 0; command_table[i]; i++);
4676 qsort (command_table, i - 1, sizeof (struct command_table_s *),
4677 sort_commands);
4680 gpg_error_t
4681 register_commands (assuan_context_t ctx)
4683 int i = 0, rc;
4685 for (; command_table[i]; i++)
4687 if (!command_table[i]->handler)
4688 continue;
4690 rc = assuan_register_command (ctx, command_table[i]->name,
4691 command_table[i]->handler,
4692 command_table[i]->help);
4693 if (rc)
4694 return rc;
4697 rc = assuan_register_bye_notify (ctx, bye_notify);
4698 if (rc)
4699 return rc;
4701 rc = assuan_register_reset_notify (ctx, reset_notify);
4702 if (rc)
4703 return rc;
4705 rc = assuan_register_pre_cmd_notify (ctx, command_startup);
4706 if (rc)
4707 return rc;
4709 return assuan_register_post_cmd_notify (ctx, command_finalize);