Minor HELP output fixes.
[pwmd.git] / src / commands.c
blob341e40a48ada6de712138984b412d37cbe9358d1
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012
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 #ifdef WITH_LIBACL
38 #include <sys/acl.h>
39 #endif
41 #include "pwmd-error.h"
42 #include <gcrypt.h>
44 #include "mem.h"
45 #include "xml.h"
46 #include "common.h"
47 #include "rcfile.h"
48 #include "cache.h"
49 #include "util-misc.h"
50 #include "commands.h"
51 #include "mutex.h"
53 /* Flags needed to be retained for a client across commands. */
54 #define FLAG_NEW 0x0001
55 #define FLAG_HAS_LOCK 0x0002
56 #define FLAG_LOCK_CMD 0x0004
57 #define FLAG_NO_PINENTRY 0x0008
58 #define FLAG_OPEN 0x0010
59 #define FLAG_KEEP_LOCK 0x0020
61 /* These are command option flags. */
62 #define OPT_INQUIRE 0x0001
63 #define OPT_NO_PASSPHRASE 0x0002
64 #define OPT_RESET 0x0004
65 #define OPT_LIST_RECURSE 0x0008
66 #define OPT_LIST_VERBOSE 0x0010
67 #define OPT_LOCK 0x0020
68 #define OPT_LOCK_ON_OPEN 0x0040
69 #define OPT_SIGN 0x0080
70 #define OPT_LIST_ALL 0x0100
71 #define OPT_LIST_WITH_TARGET 0x0200
72 #define OPT_DATA 0x0400
74 struct command_table_s {
75 const char *name;
76 gpg_error_t (*handler)(assuan_context_t, char *line);
77 const char *help;
78 int ignore_startup;
79 int unlock; // unlock the file mutex after validating the checksum
82 static struct command_table_s **command_table;
84 static gpg_error_t do_lock(struct client_s *client, int add);
85 static gpg_error_t validate_checksum(struct client_s *, struct cache_data_s *);
86 static gpg_error_t update_checksum(struct client_s *client);
88 static gpg_error_t unlock_file_mutex(struct client_s *client, int remove)
90 gpg_error_t rc = 0;
92 // OPEN: keep the lock for the same file being reopened.
93 if (client->flags&FLAG_KEEP_LOCK && client->flags&FLAG_HAS_LOCK)
94 return 0;
96 if (!(client->flags&FLAG_HAS_LOCK))
97 return GPG_ERR_NOT_LOCKED;
99 rc = cache_unlock_mutex(client->md5file, remove);
100 if (rc)
101 rc = GPG_ERR_INV_STATE;
102 else
103 client->flags &= ~(FLAG_HAS_LOCK|FLAG_LOCK_CMD);
105 return rc;
108 static gpg_error_t lock_file_mutex(struct client_s *client, int add)
110 gpg_error_t rc = 0;
111 int timeout = config_get_integer(client->filename, "cache_timeout");
113 if (client->flags&FLAG_HAS_LOCK)
114 return 0;
116 rc = cache_lock_mutex(client->ctx, client->md5file,
117 client->lock_timeout, add, timeout);
118 if (!rc)
119 client->flags |= FLAG_HAS_LOCK;
121 return rc;
124 static gpg_error_t file_modified(struct client_s *client,
125 struct command_table_s *cmd)
127 gpg_error_t rc = 0;
129 if (!(client->flags&FLAG_OPEN))
130 return GPG_ERR_INV_STATE;
132 rc = lock_file_mutex(client, 0);
133 if (!rc || rc == GPG_ERR_NO_DATA) {
134 rc = validate_checksum(client, NULL);
135 if (gpg_err_code(rc) == GPG_ERR_ENOENT)
136 rc = 0;
139 if ((cmd->unlock && !(client->flags&FLAG_HAS_LOCK)) || rc)
140 unlock_file_mutex(client, 0);
142 return rc;
145 static gpg_error_t parse_xml(assuan_context_t ctx, int new)
147 struct client_s *client = assuan_get_pointer(ctx);
148 int cached = client->doc != NULL;
150 if (new) {
151 client->doc = new_document();
152 if (client->doc) {
153 xmlDocDumpFormatMemory(client->doc,
154 (xmlChar **)&client->crypto->plaintext,
155 (int *)&client->crypto->plaintext_len, 0);
156 if (!client->crypto->plaintext) {
157 xmlFreeDoc(client->doc);
158 client->doc = NULL;
162 else if (!cached)
163 client->doc = parse_doc((char *)client->crypto->plaintext,
164 client->crypto->plaintext_len);
166 return !client->doc ? GPG_ERR_ENOMEM : 0;
169 static void free_client(struct client_s *client)
171 if (client->doc)
172 xmlFreeDoc(client->doc);
174 xfree(client->crc);
175 xfree(client->filename);
176 xfree(client->last_error);
178 if (client->crypto) {
179 cleanup_crypto_stage2(client->crypto);
180 if (client->crypto->pkey_sexp)
181 gcry_sexp_release(client->crypto->pkey_sexp);
183 client->crypto->pkey_sexp = NULL;
184 memset(client->crypto->sign_grip, 0, sizeof(client->crypto->sign_grip));
187 if (client->xml_error)
188 xmlResetError(client->xml_error);
191 void cleanup_client(struct client_s *client)
193 assuan_context_t ctx = client->ctx;
194 struct client_thread_s *thd = client->thd;
195 struct crypto_s *crypto = client->crypto;
196 long lock_timeout = client->lock_timeout;
198 unlock_file_mutex(client, client->flags&FLAG_NEW);
199 free_client(client);
200 memset(client, 0, sizeof(struct client_s));
201 client->crypto = crypto;
202 client->ctx = ctx;
203 client->thd = thd;
204 client->lock_timeout = lock_timeout;
207 static gpg_error_t open_finalize(assuan_context_t ctx)
209 struct client_s *client = assuan_get_pointer(ctx);
210 gpg_error_t rc = 0;
211 struct cache_data_s *cdata = cache_get_data(client->md5file);
212 int cached = 0;
214 client->crypto->filename = str_dup(client->filename);
215 if (cdata || client->flags&FLAG_NEW) {
216 if (!(client->flags&FLAG_NEW)) {
217 rc = decrypt_xml(client->crypto, cdata->doc, cdata->doclen);
218 if (rc)
219 return rc;
222 cached = cdata != NULL;
223 goto done;
226 rc = decrypt_data(client->ctx, client->crypto);
228 done:
229 if (!rc) {
230 rc = parse_xml(ctx, client->flags&FLAG_NEW);
231 if (!rc) {
232 int timeout = config_get_integer(client->filename,
233 "cache_timeout");
235 if (!cached) {
236 cdata = xcalloc(1, sizeof(struct cache_data_s));
237 rc = encrypt_xml(NULL, cache_key, cache_keysize,
238 GCRY_CIPHER_AES, client->crypto->plaintext,
239 client->crypto->plaintext_len, &cdata->doc,
240 &cdata->doclen, &cache_iv, &cache_blocksize, 0);
241 if (!rc && !(client->flags&FLAG_NEW)) {
242 rc = gcry_sexp_build((gcry_sexp_t *)&cdata->pubkey, NULL,
243 "%S", client->crypto->pkey_sexp);
247 if (cdata->sigkey)
248 gcry_sexp_release(cdata->sigkey);
250 if (!rc)
251 rc = gcry_sexp_build((gcry_sexp_t *)&cdata->sigkey, NULL,
252 "%S", client->crypto->sigpkey_sexp);
254 if (!rc && !cache_add_file(client->md5file, (client->flags&FLAG_NEW)
255 ? NULL : client->crypto->grip, cdata, timeout)) {
256 if (!cached) {
257 free_cache_data_once(cdata);
258 xmlFreeDoc(client->doc);
259 client->doc = NULL;
261 rc = GPG_ERR_ENOMEM;
264 if (!rc)
265 client->flags |= FLAG_OPEN;
269 if (!rc)
270 update_checksum(client);
272 if (!rc && (client->opts&OPT_LOCK_ON_OPEN))
273 rc = do_lock(client, 0);
275 return rc;
278 static void req_cleanup(void *arg)
280 if (!arg)
281 return;
283 strv_free((char **)arg);
286 static gpg_error_t parse_open_opt_lock(void * data, void * value)
288 struct client_s *client = data;
290 client->opts |= OPT_LOCK_ON_OPEN;
291 return 0;
294 static gpg_error_t parse_opt_pinentry(void * data, void * value)
296 struct client_s *client = data;
298 client->flags |= FLAG_NO_PINENTRY;
299 return 0;
302 static gpg_error_t parse_opt_inquire(void * data, void * value)
304 struct client_s *client = data;
306 (void)value;
307 client->opts |= OPT_INQUIRE;
308 return 0;
311 static gpg_error_t update_checksum(struct client_s *client)
313 unsigned char *crc;
314 size_t len;
315 struct cache_data_s *cdata;
316 gpg_error_t rc = get_checksum(client->filename, &crc, &len);
318 if (rc)
319 return rc;
321 xfree(client->crc);
322 client->crc = crc;
323 cdata = cache_get_data(client->md5file);
324 if (cdata) {
325 xfree(cdata->crc);
326 cdata->crc = xmalloc(len);
327 memcpy(cdata->crc, crc, len);
330 return 0;
333 static gpg_error_t validate_checksum(struct client_s *client,
334 struct cache_data_s *cdata)
336 unsigned char *crc;
337 size_t len;
338 gpg_error_t rc;
339 int n = 0;
341 if (cdata && !cdata->crc)
342 return GPG_ERR_CHECKSUM;
344 rc = get_checksum(client->filename, &crc, &len);
345 if (rc)
346 return rc;
348 if (cdata)
349 n = memcmp(cdata->crc, crc, len);
350 else if (client->crc)
351 n = memcmp(client->crc, crc, len);
353 xfree(crc);
354 return n ? GPG_ERR_CHECKSUM : 0;
357 static gpg_error_t do_open(assuan_context_t ctx, const char *filename,
358 const char *password)
360 struct client_s *client = assuan_get_pointer(ctx);
361 struct cache_data_s *cdata;
362 gpg_error_t rc = 0;
363 int done = 0;
365 if (!valid_filename(filename))
366 return GPG_ERR_INV_VALUE;
368 gcry_md_hash_buffer(GCRY_MD_MD5, client->md5file, filename, strlen(filename));
369 client->filename = str_dup(filename);
370 if (!client->filename)
371 return GPG_ERR_ENOMEM;
373 // Cached document?
374 cdata = cache_get_data(client->md5file);
375 if (cdata && cdata->doc) {
376 int defer;
378 rc = validate_checksum(client, cdata);
379 /* This will check that the key is cached in the agent which needs to
380 * be determined for files that share a keygrip. */
381 if (!rc) {
382 rc = cache_iscached(client->filename, &defer);
383 if (!rc && defer)
384 rc = GPG_ERR_KEY_EXPIRED;
387 #ifdef WITH_GNUTLS
388 if (!rc && client->thd->remote &&
389 config_get_boolean(NULL, "tcp_require_key"))
390 rc = GPG_ERR_KEY_EXPIRED;
391 #endif
393 if (!rc && !password) {
394 rc = read_data_header(client->filename, &client->crypto->hdr,
395 NULL, NULL);
396 if (!rc) {
397 gcry_sexp_build(&client->crypto->pkey_sexp, NULL, "%S",
398 cdata->pubkey);
399 gcry_pk_get_keygrip(client->crypto->pkey_sexp, client->crypto->grip);
400 gcry_sexp_build(&client->crypto->sigpkey_sexp, NULL, "%S",
401 cdata->sigkey);
402 gcry_pk_get_keygrip(client->crypto->sigpkey_sexp, client->crypto->sign_grip);
403 rc = open_finalize(ctx);
404 done = 1;
408 /* There was an error accessing the file so clear the cache entry. The
409 * real error will be returned from read_data_file() since the file
410 * may have only disappeared. */
411 if (!done) {
412 log_write("%s: %s", filename, pwmd_strerror(rc));
413 rc = cache_clear(client->md5file);
414 send_status_all(STATUS_CACHE, NULL);
418 if (done || rc)
419 return rc;
421 rc = read_data_file(client->filename, client->crypto);
422 if (rc) {
423 if (gpg_err_source(rc) != GPG_ERR_SOURCE_DEFAULT ||
424 gpg_err_code(rc) != GPG_ERR_ENOENT) {
425 log_write("%s: %s", client->filename, pwmd_strerror(rc));
426 return rc;
429 client->flags |= FLAG_NEW;
430 memset(client->crypto->grip, 0, sizeof(client->crypto->grip));
431 memset(client->crypto->sign_grip, 0, sizeof(client->crypto->sign_grip));
432 rc = open_finalize(ctx);
433 return rc;
436 if (password) {
437 rc = set_agent_passphrase(client->crypto, password, strlen(password));
438 if (rc)
439 return rc;
442 rc = open_finalize(ctx);
443 return rc;
446 static gpg_error_t open_command(assuan_context_t ctx, char *line)
448 gpg_error_t rc;
449 struct client_s *client = assuan_get_pointer(ctx);
450 char **req, *password = NULL, *filename;
451 unsigned char md5file[16];
452 int same_file = 0;
453 struct argv_s *args[] = {
454 &(struct argv_s) { "lock", OPTION_TYPE_NOARG, parse_open_opt_lock },
455 &(struct argv_s) { "no-pinentry", OPTION_TYPE_NOARG,
456 parse_opt_pinentry },
457 NULL
460 client->flags &= ~(FLAG_NO_PINENTRY);
461 rc = parse_options(&line, args, client);
462 if (rc)
463 return send_error(ctx, rc);
465 req = str_split(line, " ", 2);
466 if (!req)
467 return send_error(ctx, GPG_ERR_SYNTAX);
469 #ifdef WITH_GNUTLS
470 if (client->thd->remote) {
471 rc = tls_validate_access(client, req[0]);
472 if (rc) {
473 if (rc == GPG_ERR_INV_USER_ID)
474 log_write(_("client validation failed for file '%s'"), req[0]);
475 strv_free(req);
476 return send_error(ctx, rc);
479 /* Remote pinentries are not supported. */
480 client->flags |= FLAG_NO_PINENTRY;
482 #endif
484 pthread_cleanup_push(req_cleanup, req);
485 filename = req[0];
486 gcry_md_hash_buffer(GCRY_MD_MD5, md5file, filename, strlen(filename));
487 /* This client may have locked a different file with ISCACHED --lock than
488 * the current filename. This will remove that lock. */
489 same_file = !memcmp(md5file, client->md5file, 16) ? 1 : 0;
490 if (client->flags&FLAG_OPEN ||
491 (client->flags&FLAG_HAS_LOCK && !same_file)) {
492 typeof(client->opts) opts = client->opts;
493 typeof(client->flags) flags = client->flags;
495 if (same_file)
496 client->flags |= FLAG_KEEP_LOCK;
498 cleanup_client(client);
499 client->opts = opts;
500 client->flags |= flags;
501 client->flags &= ~(FLAG_LOCK_CMD|FLAG_NEW);
502 if (!same_file)
503 client->flags &= ~(FLAG_HAS_LOCK);
506 /* Need to lock the mutex here because file_modified() cannot without
507 * knowing the filename. */
508 memcpy(client->md5file, md5file, 16);
509 rc = lock_file_mutex(client, 1);
510 if (!rc) {
511 password = req[1] && *req[1] ? req[1] : NULL;
512 rc = set_pinentry_mode(client->crypto->agent,
513 (client->flags&FLAG_NO_PINENTRY) ? "loopback" : "ask");
514 if (!rc) {
515 rc = do_open(ctx, filename, password);
516 if (rc)
517 cleanup_client(client);
519 cleanup_crypto_stage1(client->crypto);
523 pthread_cleanup_pop(1);
525 if (!rc && client->flags & FLAG_NEW)
526 rc = send_status(ctx, STATUS_NEWFILE, NULL);
528 (void)kill_scd(client->crypto->agent);
529 return send_error(ctx, rc);
532 static gpg_error_t parse_save_opt_no_passphrase(void * data, void * value)
534 struct client_s *client = data;
536 (void)value;
537 client->opts |= OPT_NO_PASSPHRASE;
538 return 0;
541 static gpg_error_t parse_save_opt_cipher(void * data, void * value)
543 struct client_s *client = data;
544 int algo = cipher_string_to_gcrypt((char *)value);
545 file_header_t *hdr = &client->crypto->save.hdr;
547 if (algo == -1)
548 return GPG_ERR_INV_VALUE;
550 hdr->flags = set_cipher_flag(hdr->flags, algo);
551 return 0;
554 static gpg_error_t parse_save_opt_keygrip(void * data, void * value)
556 struct client_s *client = data;
558 if (client->crypto->save.pkey)
559 gcry_sexp_release(client->crypto->save.pkey);
561 client->crypto->save.pkey = NULL;
562 return get_pubkey(client->crypto, value, &client->crypto->save.pkey);
565 static gpg_error_t parse_save_opt_sign_keygrip(void * data, void * value)
567 struct client_s *client = data;
569 if (client->crypto->save.sigpkey)
570 gcry_sexp_release(client->crypto->save.sigpkey);
572 client->crypto->save.sigpkey = NULL;
573 return get_pubkey(client->crypto, value, &client->crypto->save.sigpkey);
576 static gpg_error_t parse_opt_s2k_count(void * data, void * value)
578 struct client_s *client = data;
579 char *v = value;
581 if (!v || !*v)
582 return GPG_ERR_INV_VALUE;
584 client->crypto->save.s2k_count = strtoul(v, NULL, 10);
585 return 0;
588 static gpg_error_t parse_save_opt_iterations(void * data, void * value)
590 struct client_s *client = data;
591 char *v = value, *p;
592 uint64_t n;
594 if (!v || !*v)
595 return GPG_ERR_INV_VALUE;
597 errno = 0;
598 n = strtoull(v, &p, 10);
599 if (n == UINT64_MAX && errno)
600 return gpg_error_from_syserror();
601 else if (p && *p)
602 return GPG_ERR_INV_VALUE;
604 client->crypto->save.hdr.iterations = n;
605 return 0;
608 static gpg_error_t save_finalize(assuan_context_t ctx)
610 struct client_s *client = assuan_get_pointer(ctx);
611 gpg_error_t rc;
612 xmlChar *xmlbuf = NULL;
613 int xmlbuflen;
614 gcry_sexp_t pubkey = client->crypto->save.pkey;
615 gcry_sexp_t sigkey = client->crypto->save.sigpkey;
616 struct cache_data_s *cdata;
618 xmlDocDumpFormatMemory(client->doc, &xmlbuf, &xmlbuflen, 0);
619 if (!xmlbuf)
620 return GPG_ERR_ENOMEM;
622 if (!pubkey)
623 pubkey = client->crypto->pkey_sexp;
625 if (!sigkey) {
626 sigkey = client->crypto->sigpkey_sexp;
627 if (!sigkey) {
628 gcry_sexp_build(&client->crypto->save.sigpkey, 0, "%S", pubkey);
629 sigkey = client->crypto->save.sigpkey;
633 pthread_cleanup_push(xmlFree, xmlbuf);
634 rc = encrypt_data_file(client->ctx, client->crypto, pubkey, sigkey,
635 client->filename, xmlbuf, xmlbuflen);
636 if (pubkey == client->crypto->save.pkey) {
637 if (!rc) {
638 gcry_sexp_release(client->crypto->pkey_sexp);
639 client->crypto->pkey_sexp = client->crypto->save.pkey;
640 gcry_pk_get_keygrip(client->crypto->pkey_sexp, client->crypto->grip);
642 else
643 gcry_sexp_release(pubkey);
645 client->crypto->save.pkey = NULL;
648 if (!rc)
649 gcry_pk_get_keygrip(sigkey, client->crypto->sign_grip);
651 if (sigkey == client->crypto->save.sigpkey) {
652 if (!rc) {
653 if (client->crypto->sigpkey_sexp)
654 gcry_sexp_release(client->crypto->sigpkey_sexp);
656 client->crypto->sigpkey_sexp = client->crypto->save.sigpkey;
657 gcry_pk_get_keygrip(client->crypto->sigpkey_sexp, client->crypto->sign_grip);
659 else
660 gcry_sexp_release(sigkey);
662 client->crypto->save.sigpkey = NULL;
665 if (!rc) {
666 /* This is safe since it is a fast operation. */
667 cache_lock();
668 pthread_cleanup_push(cleanup_cache_mutex, NULL);
669 cdata = cache_get_data(client->md5file);
670 int cached = cdata != NULL;
672 if (!cdata)
673 cdata = xcalloc(1, sizeof(struct cache_data_s));
675 /* Update in case of any --keygrip argument */
676 if (cdata->pubkey)
677 gcry_sexp_release(cdata->pubkey);
679 gcry_sexp_build((gcry_sexp_t *)&cdata->pubkey, NULL, "%S",
680 client->crypto->pkey_sexp);
682 if (cdata->sigkey)
683 gcry_sexp_release(cdata->sigkey);
685 gcry_sexp_build((gcry_sexp_t *)&cdata->sigkey, NULL, "%S",
686 client->crypto->sigpkey_sexp);
688 gcry_free(cdata->doc);
689 cdata->doc = NULL;
690 rc = encrypt_xml(NULL, cache_key, cache_keysize, GCRY_CIPHER_AES,
691 xmlbuf, xmlbuflen, &cdata->doc, &cdata->doclen, &cache_iv,
692 &cache_blocksize, 0);
694 rc = cache_set_data(client->md5file, cdata, client->crypto->grip);
695 if (!rc && (!cached || (client->flags&FLAG_NEW)))
696 send_status_all(STATUS_CACHE, NULL);
698 if (!rc) {
699 update_checksum(client);
700 client->flags &= ~(FLAG_NEW);
702 else
703 rc = GPG_ERR_ENOMEM;
705 pthread_cleanup_pop(1); // mutex unlock
708 pthread_cleanup_pop(1); // xmlFree
709 return rc;
712 static gpg_error_t parse_opt_reset(void * data, void * value)
714 struct client_s *client = data;
716 (void)value;
717 client->opts |= OPT_RESET;
718 return 0;
721 static gpg_error_t save_command(assuan_context_t ctx, char *line)
723 struct client_s *client = assuan_get_pointer(ctx);
724 gpg_error_t rc;
725 struct stat st;
726 struct argv_s *args[] = {
727 &(struct argv_s) { "no-passphrase", OPTION_TYPE_NOARG,
728 parse_save_opt_no_passphrase },
729 &(struct argv_s) { "cipher", OPTION_TYPE_ARG, parse_save_opt_cipher },
730 &(struct argv_s) { "inquire-keyparam", OPTION_TYPE_NOARG, parse_opt_inquire },
731 &(struct argv_s) { "keygrip", OPTION_TYPE_ARG, parse_save_opt_keygrip },
732 &(struct argv_s) { "sign-keygrip", OPTION_TYPE_ARG,
733 parse_save_opt_sign_keygrip },
734 &(struct argv_s) { "s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count },
735 &(struct argv_s) { "reset", OPTION_TYPE_NOARG, parse_opt_reset },
736 &(struct argv_s) { "cipher-iterations", OPTION_TYPE_ARG, parse_save_opt_iterations },
737 NULL
740 cleanup_save(&client->crypto->save);
741 memcpy(&client->crypto->save.hdr, &client->crypto->hdr, sizeof(file_header_t));
742 client->crypto->save.s2k_count = config_get_ulong(client->filename, "s2k_count");
744 if (client->flags&FLAG_NEW)
745 client->crypto->save.hdr.iterations =
746 config_get_ulonglong(client->filename, "cipher_iterations");
748 rc = parse_options(&line, args, client);
749 if (rc)
750 return send_error(ctx, rc);
752 if ((client->opts&OPT_NO_PASSPHRASE) && !(client->flags&FLAG_NEW)
753 && !(client->opts&OPT_INQUIRE))
754 return send_error(ctx, GPG_ERR_WRONG_KEY_USAGE);
756 if (lstat(client->filename, &st) == -1 && errno != ENOENT)
757 return send_error(ctx, gpg_error_from_syserror());
759 if (errno != ENOENT && !S_ISREG(st.st_mode)) {
760 log_write("%s: %s", client->filename, pwmd_strerror(GPG_ERR_ENOANO));
761 return send_error(ctx, GPG_ERR_ENOANO);
764 int defer;
765 rc = cache_iscached(client->filename, &defer);
766 if (!rc && defer) {
767 log_write("%s: %s", client->filename, pwmd_strerror(GPG_ERR_KEY_EXPIRED));
768 client->opts |= OPT_RESET;
771 if (client->opts&OPT_RESET) {
772 rc = cache_clear(client->md5file);
773 if (rc)
774 return send_error(ctx, rc);
776 send_status_all(STATUS_CACHE, NULL);
779 rc = update_element_mtime(xmlDocGetRootElement(client->doc));
780 if (rc)
781 return send_error(ctx, rc);
783 if (!rc && !client->crypto->save.pkey &&
784 (client->flags&FLAG_NEW || client->opts&OPT_INQUIRE)) {
785 rc = send_status(client->ctx, STATUS_GENKEY, NULL);
786 if (!rc) {
787 struct inquire_data_s idata = {0};
788 char *params = client->opts&OPT_INQUIRE ? NULL
789 : default_key_params(client->crypto);
791 pthread_cleanup_push(xfree, params);
792 idata.crypto = client->crypto;
794 if (params) {
795 idata.line = params;
796 idata.len = strlen(params);
798 else
799 idata.preset = 1;
801 client->crypto->agent->inquire_data = &idata;
802 client->crypto->agent->inquire_cb = NULL;
803 rc = generate_key(client->crypto, params,
804 (client->opts&OPT_NO_PASSPHRASE), 1);
805 pthread_cleanup_pop(1);
809 if (!rc)
810 rc = save_finalize(ctx);
812 cleanup_crypto_stage1(client->crypto);
813 (void)kill_scd(client->crypto->agent);
814 return send_error(ctx, rc);
817 static gpg_error_t do_delete(assuan_context_t ctx, char *line)
819 struct client_s *client = assuan_get_pointer(ctx);
820 gpg_error_t rc;
821 char **req;
822 xmlNodePtr n;
824 if (strchr(line, '\t'))
825 req = str_split(line, "\t", 0);
826 else
827 req = str_split(line, " ", 0);
829 if (!req || !*req)
830 return GPG_ERR_SYNTAX;
832 n = find_root_element(client->doc, &req, &rc, NULL, 0, 0);
833 if (!n) {
834 strv_free(req);
835 return rc;
839 * No sub-node defined. Remove the entire node (root element).
841 if (!req[1]) {
842 if (n) {
843 rc = unlink_node(n);
844 xmlFreeNode(n);
847 strv_free(req);
848 return rc;
851 n = find_elements(client->doc, n->children, req+1, &rc, NULL, NULL, NULL, 0, 0, NULL, 0);
852 strv_free(req);
853 if (!n)
854 return rc;
856 if (n) {
857 rc = unlink_node(n);
858 xmlFreeNode(n);
861 return rc;
864 static gpg_error_t delete_command(assuan_context_t ctx, char *line)
866 struct client_s *client = assuan_get_pointer(ctx);
867 gpg_error_t rc;
868 struct argv_s *args[] = {
869 &(struct argv_s) { "inquire", OPTION_TYPE_NOARG, parse_opt_inquire },
870 NULL
873 rc = parse_options(&line, args, client);
874 if (rc)
875 return send_error(ctx, rc);
877 if (client->opts&OPT_INQUIRE) {
878 unsigned char *result;
879 size_t len;
881 rc = assuan_inquire(ctx, "DATA", &result, &len, 0);
882 if (rc)
883 return send_error(ctx, rc);
885 line = (char *)result;
888 rc = do_delete(ctx, line);
890 if (client->opts&OPT_INQUIRE)
891 xfree(line);
893 return send_error(ctx, rc);
896 static gpg_error_t store_command(assuan_context_t ctx, char *line)
898 struct client_s *client = assuan_get_pointer(ctx);
899 gpg_error_t rc;
900 size_t len;
901 unsigned char *result;
902 char **req;
903 xmlNodePtr n, parent;
904 int has_content;
905 char *content = NULL;
907 rc = assuan_inquire(ctx, "DATA", &result, &len, 0);
908 if (rc)
909 return send_error(ctx, rc);
911 req = str_split((char *)result, "\t", 0);
912 xfree(result);
914 if (!req || !*req)
915 return send_error(ctx, GPG_ERR_SYNTAX);
917 len = strv_length(req);
918 has_content = line[strlen(line)-1] != '\t' && len > 1;
919 if (*(req+1) && !valid_element_path(req, has_content)) {
920 strv_free(req);
921 return send_error(ctx, GPG_ERR_INV_VALUE);
924 if (has_content || !*req[len-1]) {
925 has_content = 1;
926 content = req[len-1];
927 req[len-1] = NULL;
930 again:
931 n = find_root_element(client->doc, &req, &rc, NULL, 0, 0);
932 if (rc && rc == GPG_ERR_ELEMENT_NOT_FOUND) {
933 rc = new_root_element(client->doc, *req);
934 if (rc) {
935 strv_free(req);
936 return send_error(ctx, GPG_ERR_SYNTAX);
939 goto again;
942 if (!n) {
943 strv_free(req);
944 return send_error(ctx, rc);
947 parent = n;
949 if (req[1] && *req[1]) {
950 if (!n->children)
951 parent = create_elements_cb(n, req+1, &rc, NULL);
952 else
953 parent = find_elements(client->doc, n->children, req+1, &rc,
954 NULL, NULL, create_elements_cb, 0, 0, NULL, 0);
957 if (!rc && len > 1) {
958 n = find_text_node(parent->children);
959 if (n)
960 xmlNodeSetContent(n, (xmlChar *)content);
961 else
962 xmlNodeAddContent(parent, (xmlChar *)content);
964 update_element_mtime(parent);
967 xfree(content);
968 strv_free(req);
969 return send_error(ctx, rc);
972 static gpg_error_t xfer_data(assuan_context_t ctx, const char *line,
973 int total)
975 int to_send;
976 int sent = 0;
977 gpg_error_t rc;
978 int progress = config_get_integer("global", "xfer_progress");
979 int flush = 0;
981 progress = progress>0 ? (progress/ASSUAN_LINELENGTH)*ASSUAN_LINELENGTH : 0;
982 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
983 rc = send_status(ctx, STATUS_XFER, "%li %li", sent, total);
985 if (rc)
986 return rc;
988 again:
989 do {
990 if (sent + to_send > total)
991 to_send = total - sent;
993 rc = assuan_send_data(ctx, flush ? NULL : (char *)line+sent,
994 flush ? 0 : to_send);
995 if (!rc) {
996 sent += flush ? 0 : to_send;
998 if ((progress && !(sent % progress) && sent != total) ||
999 (sent == total && flush))
1000 rc = send_status(ctx, STATUS_XFER, "%li %li", sent, total);
1002 if (!flush && !rc && sent == total) {
1003 flush = 1;
1004 goto again;
1007 } while (!rc && sent < total);
1009 return rc;
1012 static gpg_error_t do_get(assuan_context_t ctx, char *line)
1014 struct client_s *client = assuan_get_pointer(ctx);
1015 gpg_error_t rc;
1016 char **req;
1017 xmlNodePtr n;
1019 req = str_split(line, "\t", 0);
1021 if (!req || !*req) {
1022 strv_free(req);
1023 return GPG_ERR_SYNTAX;
1026 n = find_root_element(client->doc, &req, &rc, NULL, 0, 0);
1027 if (!n) {
1028 strv_free(req);
1029 return rc;
1032 if (req[1])
1033 n = find_elements(client->doc, n->children, req+1, &rc, NULL, NULL, NULL, 0, 0, NULL, 0);
1035 strv_free(req);
1036 if (rc)
1037 return rc;
1039 if (!n || !n->children)
1040 return GPG_ERR_NO_DATA;
1042 n = find_text_node(n->children);
1043 if (!n || !n->content || !*n->content)
1044 return GPG_ERR_NO_DATA;
1046 rc = xfer_data(ctx, (char *)n->content, xmlStrlen(n->content));
1047 return rc;
1050 static gpg_error_t get_command(assuan_context_t ctx, char *line)
1052 struct client_s *client = assuan_get_pointer(ctx);
1053 gpg_error_t rc;
1054 struct argv_s *args[] = {
1055 &(struct argv_s) { "inquire", OPTION_TYPE_NOARG, parse_opt_inquire },
1056 NULL
1059 rc = parse_options(&line, args, client);
1060 if (rc)
1061 return send_error(ctx, rc);
1063 if (client->opts&OPT_INQUIRE) {
1064 unsigned char *result;
1065 size_t len;
1067 rc = assuan_inquire(ctx, "DATA", &result, &len, 0);
1068 if (rc)
1069 return send_error(ctx, rc);
1071 line = (char *)result;
1074 rc = do_get(ctx, line);
1076 if (client->opts&OPT_INQUIRE)
1077 xfree(line);
1079 return send_error(ctx, rc);
1082 static void list_command_cleanup1(void *arg);
1083 static gpg_error_t realpath_command(assuan_context_t ctx, char *line)
1085 struct string_s *string = NULL;
1086 gpg_error_t rc;
1087 struct client_s *client = assuan_get_pointer(ctx);
1088 struct argv_s *args[] = {
1089 &(struct argv_s) { "inquire", OPTION_TYPE_NOARG, parse_opt_inquire },
1090 NULL
1093 rc = parse_options(&line, args, client);
1094 if (rc)
1095 return send_error(ctx, rc);
1097 if (client->opts&OPT_INQUIRE) {
1098 unsigned char *result;
1099 size_t len;
1101 rc = assuan_inquire(ctx, "DATA", &result, &len, 0);
1102 if (rc)
1103 return send_error(ctx, rc);
1105 line = (char *)result;
1108 rc = build_realpath(client->doc, line, &string);
1109 if (!rc) {
1110 pthread_cleanup_push(list_command_cleanup1, string);
1111 rc = xfer_data(ctx, string->str, string->len);
1112 pthread_cleanup_pop(1);
1115 if (client->opts&OPT_INQUIRE)
1116 xfree(line);
1118 return send_error(ctx, rc);
1121 static void list_command_cleanup1(void *arg)
1123 if (arg)
1124 string_free((struct string_s *)arg, 1);
1127 static void list_command_cleanup2(void *arg)
1129 struct element_list_s *elements = arg;
1131 if (elements) {
1132 if (elements->list) {
1133 int total = slist_length(elements->list);
1134 int i;
1136 for (i = 0; i < total; i++) {
1137 char *tmp = slist_nth_data(elements->list, i);
1138 xfree(tmp);
1141 slist_free(elements->list);
1144 if (elements->prefix)
1145 xfree(elements->prefix);
1147 if (elements->req)
1148 strv_free(elements->req);
1150 xfree(elements);
1154 static gpg_error_t parse_list_opt_norecurse(void * data, void * value)
1156 struct client_s *client = data;
1158 client->opts &= ~(OPT_LIST_RECURSE);
1159 return 0;
1162 static gpg_error_t parse_list_opt_verbose(void * data, void * value)
1164 struct client_s *client = data;
1166 client->opts |= OPT_LIST_VERBOSE;
1167 return 0;
1170 static gpg_error_t parse_list_opt_target(void * data, void * value)
1172 struct client_s *client = data;
1174 client->opts |= OPT_LIST_WITH_TARGET;
1175 return 0;
1178 static gpg_error_t parse_list_opt_all(void * data, void * value)
1180 struct client_s *client = data;
1182 client->opts |= OPT_LIST_ALL;
1183 return 0;
1186 static gpg_error_t list_path_once(struct client_s *client, char *line,
1187 struct element_list_s *elements, struct string_s *result)
1189 gpg_error_t rc;
1191 elements->req = str_split(line, " ", 0);
1192 if (!elements->req)
1193 strv_printf(&elements->req, "%s", line);
1195 rc = create_path_list(client->doc, elements, *elements->req);
1196 if (rc == GPG_ERR_ELOOP && elements->verbose)
1197 rc = 0;
1199 if (!rc) {
1200 if (elements) {
1201 int total = slist_length(elements->list);
1202 int i;
1204 if (!total)
1205 rc = GPG_ERR_NO_DATA;
1207 if (!rc) {
1208 if (!rc) {
1209 for (i = 0; i < total; i++) {
1210 char *tmp = slist_nth_data(elements->list, i);
1212 string_append_printf(result, "%s%s", tmp,
1213 i+1 == total ? "" : "\n");
1218 else
1219 rc = GPG_ERR_NO_DATA;
1222 return rc;
1225 static int has_list_flag(char *path, char *flags)
1227 char *p = path;
1229 while (*p && *++p != ' ');
1231 if (!*p)
1232 return 0;
1234 for (; *p; p++) {
1235 char *f;
1237 for (f = flags; *f && *f != ' '; f++) {
1238 if (*p == *f)
1239 return 1;
1243 return 0;
1246 static gpg_error_t do_list(assuan_context_t ctx, char *line)
1248 struct client_s *client = assuan_get_pointer(ctx);
1249 gpg_error_t rc;
1250 struct element_list_s *elements = NULL;
1252 elements = xcalloc(1, sizeof(struct element_list_s));
1253 if (!elements)
1254 return GPG_ERR_ENOMEM;
1256 elements->recurse = client->opts&OPT_LIST_RECURSE;
1257 elements->verbose = (client->opts&OPT_LIST_VERBOSE)|(client->opts&OPT_LIST_WITH_TARGET);
1258 elements->with_target = client->opts&OPT_LIST_WITH_TARGET;
1260 if (!line || !*line) {
1261 struct string_s *str = NULL;
1263 pthread_cleanup_push(list_command_cleanup2, elements);
1264 rc = list_root_elements(client->doc, &str, elements->verbose,
1265 elements->with_target);
1266 pthread_cleanup_pop(1);
1267 pthread_cleanup_push(list_command_cleanup1, str);
1269 if (!rc) {
1270 if (client->opts&OPT_LIST_ALL) {
1271 char **roots = str_split(str->str, "\n", 0);
1272 char **p;
1274 pthread_cleanup_push(req_cleanup, roots);
1275 string_truncate(str, 0);
1277 for (p = roots; *p; p++) {
1278 if (strchr(*p, ' ')) {
1279 if (has_list_flag(*p, "EO")) {
1280 string_append_printf(str, "%s%s", *p,
1281 *(p+1) ? "\n" : "");
1282 continue;
1286 elements = xcalloc(1, sizeof(struct element_list_s));
1287 if (!elements) {
1288 rc = GPG_ERR_ENOMEM;
1289 break;
1292 elements->recurse = client->opts&OPT_LIST_RECURSE;
1293 elements->verbose = (client->opts&OPT_LIST_VERBOSE)|(client->opts&OPT_LIST_WITH_TARGET);
1294 elements->with_target = client->opts&OPT_LIST_WITH_TARGET;
1295 pthread_cleanup_push(list_command_cleanup2, elements);
1296 rc = list_path_once(client, *p, elements, str);
1297 pthread_cleanup_pop(1);
1298 if (rc)
1299 break;
1301 if (*(p+1))
1302 string_append(str, "\n");
1305 pthread_cleanup_pop(1);
1308 if (!rc)
1309 rc = xfer_data(ctx, str->str, str->len);
1312 pthread_cleanup_pop(1);
1313 return rc;
1316 pthread_cleanup_push(list_command_cleanup2, elements);
1317 struct string_s *str = string_new(NULL);
1318 pthread_cleanup_push(list_command_cleanup1, str);
1319 rc = list_path_once(client, line, elements, str);
1320 if (!rc)
1321 rc = xfer_data(ctx, str->str, str->len);
1323 pthread_cleanup_pop(1);
1324 pthread_cleanup_pop(1);
1325 return rc;
1328 static gpg_error_t list_command(assuan_context_t ctx, char *line)
1330 struct client_s *client = assuan_get_pointer(ctx);
1331 gpg_error_t rc;
1332 struct argv_s *args[] = {
1333 &(struct argv_s) { "no-recurse", OPTION_TYPE_NOARG, parse_list_opt_norecurse },
1334 &(struct argv_s) { "verbose", OPTION_TYPE_NOARG, parse_list_opt_verbose },
1335 &(struct argv_s) { "inquire", OPTION_TYPE_NOARG, parse_opt_inquire },
1336 &(struct argv_s) { "with-target", OPTION_TYPE_NOARG, parse_list_opt_target},
1337 &(struct argv_s) { "all", OPTION_TYPE_NOARG, parse_list_opt_all },
1338 NULL
1341 if (disable_list_and_dump == 1)
1342 return send_error(ctx, GPG_ERR_NOT_IMPLEMENTED);
1344 client->opts |= OPT_LIST_RECURSE;
1345 rc = parse_options(&line, args, client);
1346 if (rc)
1347 return send_error(ctx, rc);
1349 if (client->opts&OPT_LIST_ALL)
1350 client->opts |= OPT_LIST_RECURSE|OPT_LIST_VERBOSE;
1352 if (client->opts&OPT_INQUIRE) {
1353 unsigned char *result;
1354 size_t len;
1356 rc = assuan_inquire(ctx, "DATA", &result, &len, 0);
1357 if (rc)
1358 return send_error(ctx, rc);
1360 line = (char *)result;
1363 rc = do_list(ctx, line);
1365 if (client->opts&OPT_INQUIRE)
1366 xfree(line);
1368 return send_error(ctx, rc);
1372 * req[0] - element path
1374 static gpg_error_t attribute_list(assuan_context_t ctx, char **req)
1376 struct client_s *client = assuan_get_pointer(ctx);
1377 char **attrlist = NULL;
1378 int i = 0;
1379 char **path = NULL;
1380 xmlAttrPtr a;
1381 xmlNodePtr n, an;
1382 char *line;
1383 gpg_error_t rc;
1385 if (!req || !req[0])
1386 return GPG_ERR_SYNTAX;
1388 if ((path = str_split(req[0], "\t", 0)) == NULL) {
1390 * The first argument may be only a root element.
1392 if ((path = str_split(req[0], " ", 0)) == NULL)
1393 return GPG_ERR_SYNTAX;
1396 n = find_root_element(client->doc, &path, &rc, NULL, 0, 0);
1398 if (!n) {
1399 strv_free(path);
1400 return rc;
1403 if (path[1]) {
1404 n = find_elements(client->doc, n->children, path+1, &rc,
1405 NULL, NULL, NULL, 0, 0, NULL, 0);
1407 if (!n) {
1408 strv_free(path);
1409 return rc;
1413 strv_free(path);
1415 for (a = n->properties; a; a = a->next) {
1416 char **pa;
1418 if ((pa = xrealloc(attrlist, (i + 2) * sizeof(char *))) == NULL) {
1419 if (attrlist)
1420 strv_free(attrlist);
1422 log_write("%s(%i): %s", __FILE__, __LINE__, pwmd_strerror(GPG_ERR_ENOMEM));
1423 return GPG_ERR_ENOMEM;
1426 attrlist = pa;
1427 an = a->children;
1428 attrlist[i] = str_asprintf("%s %s", (char *)a->name,
1429 an && an->content ? (char *)an->content : "");
1431 if (!attrlist[i]) {
1432 strv_free(attrlist);
1433 log_write("%s(%i): %s", __FILE__, __LINE__, pwmd_strerror(GPG_ERR_ENOMEM));
1434 return GPG_ERR_ENOMEM;
1437 attrlist[++i] = NULL;
1440 if (!attrlist)
1441 return GPG_ERR_NO_DATA;
1443 line = strv_join("\n", attrlist);
1445 if (!line) {
1446 log_write("%s(%i): %s", __FILE__, __LINE__, pwmd_strerror(GPG_ERR_ENOMEM));
1447 strv_free(attrlist);
1448 return GPG_ERR_ENOMEM;
1451 pthread_cleanup_push(xfree, line);
1452 pthread_cleanup_push(req_cleanup, attrlist);
1453 rc = xfer_data(ctx, line, strlen(line));
1454 pthread_cleanup_pop(1);
1455 pthread_cleanup_pop(1);
1456 return rc;
1460 * req[0] - attribute
1461 * req[1] - element path
1463 static gpg_error_t attribute_delete(struct client_s *client, char **req)
1465 xmlNodePtr n;
1466 char **path = NULL;
1467 gpg_error_t rc;
1469 if (!req || !req[0] || !req[1])
1470 return GPG_ERR_SYNTAX;
1472 if (!strcmp(req[0], "_name"))
1473 return GPG_ERR_INV_ATTR;
1475 if ((path = str_split(req[1], "\t", 0)) == NULL) {
1477 * The first argument may be only a root element.
1479 if ((path = str_split(req[1], " ", 0)) == NULL)
1480 return GPG_ERR_SYNTAX;
1483 n = find_root_element(client->doc, &path, &rc, NULL, 0, 0);
1484 if (!n)
1485 goto fail;
1487 if (path[1]) {
1488 n = find_elements(client->doc, n->children, path+1, &rc,
1489 NULL, NULL, NULL, 0, 0, NULL, 0);
1490 if (!n)
1491 goto fail;
1494 rc = delete_attribute(n, (xmlChar *)req[0]);
1496 fail:
1497 strv_free(path);
1498 return rc;
1501 static xmlNodePtr create_element_path(struct client_s *client,
1502 char ***elements, gpg_error_t *rc, xmlNodePtr parent)
1504 char **req = *elements;
1505 char **req_orig = strv_dup(req);
1506 xmlNodePtr n = NULL;
1508 *rc = 0;
1510 if (!req_orig) {
1511 *rc = GPG_ERR_ENOMEM;
1512 log_write("%s(%i): %s", __FILE__, __LINE__, pwmd_strerror(GPG_ERR_ENOMEM));
1513 goto fail;
1516 again:
1517 n = find_root_element(client->doc, &req, rc, NULL, 0, 0);
1518 if (!n) {
1519 if (*rc != GPG_ERR_ELEMENT_NOT_FOUND)
1520 goto fail;
1522 *rc = new_root_element(client->doc, req[0]);
1523 if (*rc)
1524 goto fail;
1526 goto again;
1528 else if (n == parent) {
1529 *rc = GPG_ERR_CONFLICT;
1530 goto fail;
1533 if (req[1]) {
1534 if (!n->children)
1535 n = create_target_elements_cb(n, req+1, rc, NULL);
1536 else
1537 n = find_elements(client->doc, n->children, req+1, rc, NULL, NULL,
1538 create_target_elements_cb, 0, 0, parent, 0);
1540 if (!n)
1541 goto fail;
1544 * Reset the position of the element tree now that the elements
1545 * have been created.
1547 strv_free(req);
1548 req = req_orig;
1549 req_orig = NULL;
1550 n = find_root_element(client->doc, &req, rc, NULL, 0, 0);
1551 if (!n)
1552 goto fail;
1554 n = find_elements(client->doc, n->children, req+1, rc,
1555 NULL, NULL, NULL, 0, 0, NULL, 0);
1556 if (!n)
1557 goto fail;
1560 fail:
1561 if (req_orig)
1562 strv_free(req_orig);
1564 *elements = req;
1565 return n;
1569 * Creates a "target" attribute. When other commands encounter an element with
1570 * this attribute, the element path is modified to the target value. If the
1571 * source element path doesn't exist when using 'ATTR SET target', it is
1572 * created, but the destination element path must exist.
1574 * req[0] - source element path
1575 * req[1] - destination element path
1577 static gpg_error_t target_attribute(struct client_s *client, char **req)
1579 char **src, **dst, *line = NULL, **odst = NULL;
1580 gpg_error_t rc;
1581 xmlNodePtr n;
1583 if (!req || !req[0] || !req[1])
1584 return GPG_ERR_SYNTAX;
1586 if ((src = str_split(req[0], "\t", 0)) == NULL) {
1588 * The first argument may be only a root element.
1590 if ((src = str_split(req[0], " ", 0)) == NULL)
1591 return GPG_ERR_SYNTAX;
1594 if (!valid_element_path(src, 0))
1595 return GPG_ERR_INV_VALUE;
1597 if ((dst = str_split(req[1], "\t", 0)) == NULL) {
1599 * The first argument may be only a root element.
1601 if ((dst = str_split(req[1], " ", 0)) == NULL) {
1602 rc = GPG_ERR_SYNTAX;
1603 goto fail;
1607 odst = strv_dup(dst);
1608 if (!odst) {
1609 rc = GPG_ERR_ENOMEM;
1610 goto fail;
1613 n = find_root_element(client->doc, &dst, &rc, NULL, 0, 0);
1615 * Make sure the destination element path exists.
1617 if (!n)
1618 goto fail;
1620 if (dst[1]) {
1621 n = find_elements(client->doc, n->children, dst+1, &rc,
1622 NULL, NULL, NULL, 0, 0, NULL, 0);
1623 if (!n)
1624 goto fail;
1627 rc = validate_target_attribute(client->doc, req[0], n);
1628 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
1629 goto fail;
1631 n = create_element_path(client, &src, &rc, NULL);
1632 if (rc)
1633 goto fail;
1635 line = strv_join("\t", odst);
1636 if (!line) {
1637 rc = GPG_ERR_ENOMEM;
1638 goto fail;
1641 rc = add_attribute(n, "target", line);
1643 fail:
1644 xfree(line);
1645 strv_free(src);
1646 strv_free(dst);
1647 strv_free(odst);
1648 return rc;
1652 * req[0] - attribute
1653 * req[1] - element path
1655 static gpg_error_t attribute_get(assuan_context_t ctx, char **req)
1657 struct client_s *client = assuan_get_pointer(ctx);
1658 xmlNodePtr n;
1659 xmlChar *a;
1660 char **path= NULL;
1661 gpg_error_t rc;
1663 if (!req || !req[0] || !req[1])
1664 return GPG_ERR_SYNTAX;
1666 if (strchr(req[1], '\t')) {
1667 if ((path = str_split(req[1], "\t", 0)) == NULL)
1668 return GPG_ERR_SYNTAX;
1670 else {
1671 if ((path = str_split(req[1], " ", 0)) == NULL)
1672 return GPG_ERR_SYNTAX;
1675 n = find_root_element(client->doc, &path, &rc, NULL, 0, 0);
1677 if (!n)
1678 goto fail;
1680 if (path[1]) {
1681 n = find_elements(client->doc, n->children, path+1, &rc,
1682 NULL, NULL, NULL, 0, 0, NULL, 0);
1684 if (!n)
1685 goto fail;
1688 strv_free(path);
1690 if ((a = xmlGetProp(n, (xmlChar *)req[0])) == NULL)
1691 return GPG_ERR_NOT_FOUND;
1693 pthread_cleanup_push(xmlFree, a);
1695 if (*a)
1696 rc = xfer_data(ctx, (char *)a, xmlStrlen(a));
1697 else
1698 rc = GPG_ERR_NO_DATA;
1700 pthread_cleanup_pop(1);
1701 return rc;
1703 fail:
1704 strv_free(path);
1705 return rc;
1709 * req[0] - attribute
1710 * req[1] - element path
1711 * req[2] - value
1713 static gpg_error_t attribute_set(struct client_s *client, char **req)
1715 char **path = NULL;
1716 gpg_error_t rc;
1717 xmlNodePtr n;
1719 if (!req || !req[0] || !req[1])
1720 return GPG_ERR_SYNTAX;
1723 * Reserved attribute names.
1725 if (!strcmp(req[0], "_name"))
1726 return GPG_ERR_INV_ATTR;
1727 else if (!strcmp(req[0], "target"))
1728 return target_attribute(client, req + 1);
1730 if ((path = str_split(req[1], "\t", 0)) == NULL) {
1732 * The first argument may be only a root element.
1734 if ((path = str_split(req[1], " ", 0)) == NULL)
1735 return GPG_ERR_SYNTAX;
1738 n = find_root_element(client->doc, &path, &rc, NULL, 0, 0);
1740 if (!n)
1741 goto fail;
1743 if (path[1]) {
1744 n = find_elements(client->doc, n->children, path+1, &rc,
1745 NULL, NULL, NULL, 0, 0, NULL, 0);
1747 if (!n)
1748 goto fail;
1751 rc = add_attribute(n, req[0], req[2]);
1753 fail:
1754 strv_free(path);
1755 return rc;
1759 * req[0] - command
1760 * req[1] - attribute name or element path if command is LIST
1761 * req[2] - element path
1762 * req[2] - element path or value
1765 static gpg_error_t do_attr(assuan_context_t ctx, char *line)
1767 struct client_s *client = assuan_get_pointer(ctx);
1768 gpg_error_t rc = 0;
1769 char **req;
1771 req = str_split(line, " ", 4);
1772 if (!req || !req[0] || !req[1]) {
1773 strv_free(req);
1774 return GPG_ERR_SYNTAX;
1777 pthread_cleanup_push(req_cleanup, req);
1779 if (strcasecmp(req[0], "SET") == 0)
1780 rc = attribute_set(client, req+1);
1781 else if (strcasecmp(req[0], "GET") == 0)
1782 rc = attribute_get(ctx, req+1);
1783 else if (strcasecmp(req[0], "DELETE") == 0)
1784 rc = attribute_delete(client, req+1);
1785 else if (strcasecmp(req[0], "LIST") == 0)
1786 rc = attribute_list(ctx, req+1);
1787 else
1788 rc = GPG_ERR_SYNTAX;
1790 pthread_cleanup_pop(1);
1791 return rc;
1794 static gpg_error_t attr_command(assuan_context_t ctx, char *line)
1796 struct client_s *client = assuan_get_pointer(ctx);
1797 gpg_error_t rc;
1798 struct argv_s *args[] = {
1799 &(struct argv_s) { "inquire", OPTION_TYPE_NOARG, parse_opt_inquire },
1800 NULL
1803 rc = parse_options(&line, args, client);
1804 if (rc)
1805 return send_error(ctx, rc);
1807 if (client->opts&OPT_INQUIRE) {
1808 unsigned char *result;
1809 size_t len;
1811 rc = assuan_inquire(ctx, "DATA", &result, &len, 0);
1812 if (rc)
1813 return send_error(ctx, rc);
1815 line = (char *)result;
1818 rc = do_attr(ctx, line);
1820 if (client->opts&OPT_INQUIRE)
1821 xfree(line);
1823 return send_error(ctx, rc);
1826 static gpg_error_t parse_iscached_opt_lock(void * data, void * value)
1828 struct client_s *client = data;
1830 (void)value;
1831 client->opts |= OPT_LOCK;
1832 return 0;
1835 static gpg_error_t iscached_command(assuan_context_t ctx, char *line)
1837 struct client_s *client = assuan_get_pointer(ctx);
1838 gpg_error_t rc;
1839 unsigned char md5file[16];
1840 struct argv_s *args[] = {
1841 &(struct argv_s) { "lock", OPTION_TYPE_NOARG, parse_iscached_opt_lock },
1842 NULL
1845 if (!line || !*line)
1846 return send_error(ctx, GPG_ERR_SYNTAX);
1848 rc = parse_options(&line, args, client);
1849 if (rc)
1850 return send_error(ctx, rc);
1851 else if (!valid_filename(line))
1852 return send_error(ctx, GPG_ERR_INV_VALUE);
1854 rc = cache_iscached(line, NULL);
1855 if (client->opts&OPT_LOCK && (!rc || gpg_err_code(rc) == GPG_ERR_NO_DATA)) {
1856 gpg_error_t trc = rc;
1857 gcry_md_hash_buffer(GCRY_MD_MD5, md5file, line, strlen(line));
1858 if (memcmp(md5file, client->md5file, 16))
1859 cleanup_client(client);
1861 memcpy(client->md5file, md5file, 16);
1862 rc = do_lock(client, 1);
1863 if (!rc)
1864 rc = trc;
1867 return send_error(ctx, rc);
1870 static gpg_error_t clearcache_command(assuan_context_t ctx, char *line)
1872 gpg_error_t rc = 0, all_rc = 0;
1873 unsigned char md5file[16];
1874 int i;
1875 int t;
1876 int all = 0;
1877 struct client_thread_s *once = NULL;
1879 cache_lock();
1880 MUTEX_LOCK(&cn_mutex);
1881 pthread_cleanup_push(cleanup_mutex_cb, &cn_mutex);
1883 if (!line || !*line)
1884 all = 1;
1886 t = slist_length(cn_thread_list);
1888 for (i = 0; i < t; i++) {
1889 struct client_thread_s *thd = slist_nth_data(cn_thread_list, i);
1891 if (!thd->cl)
1892 continue;
1894 /* Lock each connected clients' file mutex to prevent any other client
1895 * from accessing the cache entry (the file mutex is locked upon
1896 * command startup). The cache for the entry is not cleared if the
1897 * file mutex is locked by another client to prevent this function
1898 * from blocking.
1900 if (all) {
1901 rc = cache_lock_mutex(thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
1902 if (gpg_err_code(rc) == GPG_ERR_LOCKED) {
1903 if (pthread_equal(pthread_self(), thd->tid))
1904 rc = 0;
1905 else {
1906 if (!thd->cl->filename ||
1907 cache_iscached(thd->cl->filename, NULL) == GPG_ERR_NO_DATA) {
1908 rc = 0;
1909 continue;
1912 cache_defer_clear(thd->cl->md5file);
1915 else if (gpg_err_code(rc) == GPG_ERR_NO_DATA) {
1916 rc = 0;
1917 continue;
1920 if (!rc) {
1921 rc = cache_clear(thd->cl->md5file);
1922 if (rc)
1923 all_rc = rc;
1925 cache_unlock_mutex(thd->cl->md5file, 0);
1927 else
1928 all_rc = rc;
1930 rc = 0;
1932 /* A single data filename was specified. Lock only this data file
1933 * mutex and free the cache entry. */
1934 else {
1935 gcry_md_hash_buffer(GCRY_MD_MD5, md5file, line, strlen(line));
1937 if (!memcmp(thd->cl->md5file, md5file, sizeof(md5file))) {
1938 rc = cache_lock_mutex(thd->cl->ctx, thd->cl->md5file, -1, 0, -1);
1939 if (gpg_err_code(rc) == GPG_ERR_LOCKED) {
1940 if (pthread_equal(pthread_self(), thd->tid))
1941 rc = 0;
1944 if (!rc) {
1945 once = thd;
1946 rc = cache_clear(thd->cl->md5file);
1947 cache_unlock_mutex(thd->cl->md5file, 0);
1949 else
1950 cache_defer_clear(thd->cl->md5file);
1952 break;
1957 /* Only connected clients' cache entries have been cleared. Now clear any
1958 * remaining cache entries without clients but only if there wasn't an
1959 * error from above since this would defeat the locking check of the
1960 * remaining entries. */
1961 if (!all_rc && all)
1962 cache_clear(NULL);
1963 /* No clients are using the specified file. */
1964 else if (!all_rc && !rc && !once) {
1965 gcry_md_hash_buffer(GCRY_MD_MD5, md5file, line, strlen(line));
1966 rc = cache_clear(md5file);
1969 /* Release the connection mutex. */
1970 pthread_cleanup_pop(1);
1971 cache_unlock();
1973 if (!rc)
1974 send_status_all(STATUS_CACHE, NULL);
1976 /* One or more files were locked while clearing all cache entries. */
1977 if (all_rc)
1978 rc = all_rc;
1980 return send_error(ctx, rc);
1983 static gpg_error_t cachetimeout_command(assuan_context_t ctx, char *line)
1985 unsigned char md5file[16];
1986 int timeout;
1987 char **req = str_split(line, " ", 0);
1988 char *p;
1989 gpg_error_t rc = 0;
1991 if (!req || !*req || !req[1]) {
1992 strv_free(req);
1993 return send_error(ctx, GPG_ERR_SYNTAX);
1996 errno = 0;
1997 timeout = (int)strtol(req[1], &p, 10);
1998 if (errno != 0 || *p != 0 || timeout < -1) {
1999 strv_free(req);
2000 return send_error(ctx, GPG_ERR_SYNTAX);
2003 gcry_md_hash_buffer(GCRY_MD_MD5, md5file, req[0], strlen(req[0]));
2005 rc = cache_set_timeout(md5file, timeout);
2006 if (!rc) {
2007 MUTEX_LOCK(&rcfile_mutex);
2008 config_set_int_param(&global_config, req[0], "cache_timeout", req[1]);
2009 MUTEX_UNLOCK(&rcfile_mutex);
2012 strv_free(req);
2013 return send_error(ctx, rc);
2016 static gpg_error_t dump_command(assuan_context_t ctx, char *line)
2018 xmlChar *xml;
2019 int len;
2020 struct client_s *client = assuan_get_pointer(ctx);
2021 gpg_error_t rc;
2023 if (disable_list_and_dump == 1)
2024 return send_error(ctx, GPG_ERR_NOT_IMPLEMENTED);
2026 xmlDocDumpFormatMemory(client->doc, &xml, &len, 1);
2028 if (!xml) {
2029 log_write("%s(%i): %s", __FILE__, __LINE__, pwmd_strerror(GPG_ERR_ENOMEM));
2030 return send_error(ctx, GPG_ERR_ENOMEM);
2033 pthread_cleanup_push(xmlFree, xml);
2034 rc = xfer_data(ctx, (char *)xml, len);
2035 pthread_cleanup_pop(1);
2036 return send_error(ctx, rc);
2039 static gpg_error_t getconfig_command(assuan_context_t ctx, char *line)
2041 struct client_s *client = assuan_get_pointer(ctx);
2042 gpg_error_t rc = 0;
2043 char filename[255]={0}, param[747]={0};
2044 char *p, *tmp = NULL, *fp = client->filename, *paramp = line;
2046 if (!line || !*line)
2047 return send_error(ctx, GPG_ERR_SYNTAX);
2049 if (strchr(line, ' ')) {
2050 sscanf(line, " %254[^ ] %746c", filename, param);
2051 paramp = param;
2052 fp = filename;
2055 if (fp && !valid_filename(fp))
2056 return send_error(ctx, GPG_ERR_INV_VALUE);
2058 paramp = str_down(paramp);
2059 if (!strcmp(paramp, "cipher") && fp) {
2060 struct crypto_s *crypto;
2062 rc = init_client_crypto(&crypto);
2063 if (!rc) {
2064 rc = read_data_header(fp, &crypto->hdr, NULL, NULL);
2065 if (!rc) {
2066 const char *t = gcry_cipher_algo_name(cipher_to_gcrypt(crypto->hdr.flags));
2067 if (t) {
2068 tmp = str_dup(t);
2069 if (tmp)
2070 str_down(tmp);
2075 cleanup_crypto(&crypto);
2076 if (rc && gpg_err_code(rc) != GPG_ERR_ENOENT)
2077 return send_error(ctx, rc);
2079 if (!rc && tmp)
2080 goto done;
2082 else if (!strcmp(paramp, "cipher_iterations") && fp) {
2083 struct crypto_s *crypto;
2085 rc = init_client_crypto(&crypto);
2086 if (!rc) {
2087 rc = read_data_header(fp, &crypto->hdr, NULL, NULL);
2088 if (!rc) {
2089 tmp = str_asprintf("%llu",
2090 (unsigned long long)crypto->hdr.iterations);
2091 if (!tmp)
2092 rc = GPG_ERR_ENOMEM;
2096 cleanup_crypto(&crypto);
2097 if (rc && gpg_err_code(rc) != GPG_ERR_ENOENT)
2098 return send_error(ctx, rc);
2100 if (!rc && tmp)
2101 goto done;
2103 else if (!strcmp(paramp, "passphrase"))
2104 return send_error(ctx, GPG_ERR_UNKNOWN_OPTION);
2106 p = config_get_value(fp ? fp : "global", paramp);
2107 if (!p)
2108 return send_error(ctx, GPG_ERR_UNKNOWN_OPTION);
2110 tmp = expand_homedir(p);
2111 xfree(p);
2112 if (!tmp) {
2113 log_write("%s(%i): %s", __FILE__, __LINE__, pwmd_strerror(GPG_ERR_ENOMEM));
2114 return send_error(ctx, GPG_ERR_ENOMEM);
2117 done:
2118 p = tmp;
2119 pthread_cleanup_push(xfree, p);
2120 rc = xfer_data(ctx, p, strlen(p));
2121 pthread_cleanup_pop(1);
2122 return send_error(ctx, rc);
2125 struct xpath_s {
2126 xmlXPathContextPtr xp;
2127 xmlXPathObjectPtr result;
2128 xmlBufferPtr buf;
2129 char **req;
2132 static void xpath_command_cleanup(void *arg)
2134 struct xpath_s *xpath = arg;
2136 if (!xpath)
2137 return;
2139 req_cleanup(xpath->req);
2141 if (xpath->buf)
2142 xmlBufferFree(xpath->buf);
2144 if (xpath->result)
2145 xmlXPathFreeObject(xpath->result);
2147 if (xpath->xp)
2148 xmlXPathFreeContext(xpath->xp);
2151 static gpg_error_t do_xpath(assuan_context_t ctx, char *line)
2153 gpg_error_t rc;
2154 struct client_s *client = assuan_get_pointer(ctx);
2155 struct xpath_s _x = {0};
2156 struct xpath_s *xpath = &_x;
2158 if ((xpath->req = str_split(line, "\t", 2)) == NULL) {
2159 if (strv_printf(&xpath->req, "%s", line) == 0)
2160 return GPG_ERR_ENOMEM;
2163 xpath->xp = xmlXPathNewContext(client->doc);
2164 if (!xpath->xp) {
2165 rc = GPG_ERR_BAD_DATA;
2166 goto fail;
2169 xpath->result = xmlXPathEvalExpression((xmlChar *)xpath->req[0], xpath->xp);
2170 if (!xpath->result) {
2171 rc = GPG_ERR_BAD_DATA;
2172 goto fail;
2175 if (xmlXPathNodeSetIsEmpty(xpath->result->nodesetval)) {
2176 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2177 goto fail;
2180 rc = recurse_xpath_nodeset(client->doc, xpath->result->nodesetval,
2181 (xmlChar *)xpath->req[1], &xpath->buf, 0, NULL);
2182 if (rc)
2183 goto fail;
2184 else if (!xpath->req[1] && !xmlBufferLength(xpath->buf)) {
2185 rc = GPG_ERR_NO_DATA;
2186 goto fail;
2188 else if (xpath->req[1]) {
2189 rc = 0;
2190 goto fail;
2193 pthread_cleanup_push(xpath_command_cleanup, &xpath);
2194 rc = xfer_data(ctx, (char *)xmlBufferContent(xpath->buf),
2195 xmlBufferLength(xpath->buf));
2196 pthread_cleanup_pop(0);
2197 fail:
2198 xpath_command_cleanup(xpath);
2199 return rc;
2202 static gpg_error_t xpath_command(assuan_context_t ctx, char *line)
2204 struct client_s *client = assuan_get_pointer(ctx);
2205 gpg_error_t rc;
2206 struct argv_s *args[] = {
2207 &(struct argv_s) { "inquire", OPTION_TYPE_NOARG, parse_opt_inquire },
2208 NULL
2211 if (disable_list_and_dump == 1)
2212 return send_error(ctx, GPG_ERR_NOT_IMPLEMENTED);
2214 rc = parse_options(&line, args, client);
2215 if (rc)
2216 return send_error(ctx, rc);
2218 if (client->opts&OPT_INQUIRE) {
2219 unsigned char *result;
2220 size_t len;
2222 rc = assuan_inquire(ctx, "DATA", &result, &len, 0);
2223 if (rc)
2224 return send_error(ctx, rc);
2226 line = (char *)result;
2229 if (!line || !*line)
2230 rc = GPG_ERR_SYNTAX;
2232 if (!rc)
2233 rc = do_xpath(ctx, line);
2235 if (client->opts&OPT_INQUIRE)
2236 xfree(line);
2238 return send_error(ctx, rc);
2241 static gpg_error_t do_xpathattr(assuan_context_t ctx, char *line)
2243 struct client_s *client = assuan_get_pointer(ctx);
2244 gpg_error_t rc;
2245 char **req = NULL;
2246 int cmd = 0; //SET
2247 struct xpath_s _x = {0};
2248 struct xpath_s *xpath = &_x;
2250 if ((req = str_split(line, " ", 3)) == NULL)
2251 return GPG_ERR_ENOMEM;
2253 if (!req[0]) {
2254 rc = GPG_ERR_SYNTAX;
2255 goto fail;
2258 if (!strcasecmp(req[0], "SET"))
2259 cmd = 0;
2260 else if (!strcasecmp(req[0], "DELETE"))
2261 cmd = 1;
2262 else {
2263 rc = GPG_ERR_SYNTAX;
2264 goto fail;
2267 if (!req[1] || !req[2]) {
2268 rc = GPG_ERR_SYNTAX;
2269 goto fail;
2272 if ((xpath->req = str_split(req[2], "\t", 3)) == NULL) {
2273 rc = GPG_ERR_ENOMEM;
2274 goto fail;
2277 if (!xpath->req[0] || (!xpath->req[1] && !cmd) || (xpath->req[1] && cmd)) {
2278 rc = GPG_ERR_SYNTAX;
2279 goto fail;
2282 xpath->xp = xmlXPathNewContext(client->doc);
2283 if (!xpath->xp) {
2284 rc = GPG_ERR_BAD_DATA;
2285 goto fail;
2288 xpath->result = xmlXPathEvalExpression((xmlChar *)xpath->req[0], xpath->xp);
2289 if (!xpath->result) {
2290 rc = GPG_ERR_BAD_DATA;
2291 goto fail;
2294 if (xmlXPathNodeSetIsEmpty(xpath->result->nodesetval)) {
2295 rc = GPG_ERR_ELEMENT_NOT_FOUND;
2296 goto fail;
2299 rc = recurse_xpath_nodeset(client->doc, xpath->result->nodesetval,
2300 (xmlChar *)xpath->req[1], &xpath->buf, cmd, (xmlChar *)req[1]);
2302 fail:
2303 xpath_command_cleanup(xpath);
2304 strv_free(req);
2305 return rc;
2308 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2309 static gpg_error_t xpathattr_command(assuan_context_t ctx, char *line)
2311 struct client_s *client = assuan_get_pointer(ctx);
2312 gpg_error_t rc;
2313 struct argv_s *args[] = {
2314 &(struct argv_s) { "inquire", OPTION_TYPE_NOARG, parse_opt_inquire },
2315 NULL
2318 if (disable_list_and_dump == 1)
2319 return send_error(ctx, GPG_ERR_NOT_IMPLEMENTED);
2321 rc = parse_options(&line, args, client);
2322 if (rc)
2323 return send_error(ctx, rc);
2325 if (client->opts&OPT_INQUIRE) {
2326 unsigned char *result;
2327 size_t len;
2329 rc = assuan_inquire(ctx, "DATA", &result, &len, 0);
2330 if (rc)
2331 return send_error(ctx, rc);
2333 line = (char *)result;
2336 if (!line || !*line)
2337 rc = GPG_ERR_SYNTAX;
2339 if (!rc)
2340 rc = do_xpathattr(ctx, line);
2342 if (client->opts&OPT_INQUIRE)
2343 xfree(line);
2345 return send_error(ctx, rc);
2348 static gpg_error_t do_import(struct client_s *client, unsigned char *line)
2350 char **req, **path = NULL, **path_orig = NULL, *content;
2351 xmlDocPtr doc = NULL;
2352 xmlNodePtr n, root, copy;
2353 gpg_error_t rc;
2355 req = str_split((char *)line, "\t", 2);
2356 xfree(line);
2357 if (!req || !*req)
2358 return GPG_ERR_SYNTAX;
2360 content = req[0];
2361 path = str_split(req[1], "\t", 0);
2362 if (!content || !*content) {
2363 rc = GPG_ERR_SYNTAX;
2364 goto fail;
2367 if (path && !valid_element_path(path, 0)) {
2368 rc = GPG_ERR_INV_VALUE;
2369 goto fail;
2372 doc = xmlReadDoc((xmlChar *)content, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2373 if (!doc) {
2374 rc = GPG_ERR_BAD_DATA;
2375 goto fail;
2378 root = xmlDocGetRootElement(doc);
2379 rc = validate_import(root);
2380 if (rc)
2381 goto fail;
2383 if (path) {
2384 path_orig = strv_dup(path);
2385 if (!path_orig) {
2386 log_write("%s(%i): %s", __FILE__, __LINE__, pwmd_strerror(GPG_ERR_ENOMEM));
2387 rc = GPG_ERR_ENOMEM;
2388 goto fail;
2391 xmlChar *a = xmlGetProp(root, (xmlChar *)"_name");
2392 if (!a) {
2393 strv_free(path_orig);
2394 rc = GPG_ERR_ENOMEM;
2395 goto fail;
2398 if (strv_printf(&path, "%s", (char *)a) == 0) {
2399 xmlFree(a);
2400 strv_free(path_orig);
2401 rc = GPG_ERR_ENOMEM;
2402 goto fail;
2405 xmlFree(a);
2406 n = find_root_element(client->doc, &path, &rc, NULL, 0, 0);
2408 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND) {
2409 strv_free(path_orig);
2410 goto fail;
2413 if (!rc) {
2414 n = find_elements(client->doc, n->children, path+1, &rc, NULL, NULL, NULL, 0, 0, NULL, 1);
2416 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND) {
2417 strv_free(path_orig);
2418 goto fail;
2420 else if (!rc) {
2421 xmlNodePtr parent = n->parent;
2423 xmlUnlinkNode(n);
2424 xmlFreeNode(n);
2425 n = parent;
2429 strv_free(path);
2430 path = path_orig;
2432 if (rc == GPG_ERR_ELEMENT_NOT_FOUND) {
2433 n = create_element_path(client, &path, &rc, NULL);
2435 if (rc)
2436 goto fail;
2439 copy = xmlCopyNodeList(root);
2440 n = xmlAddChildList(n, copy);
2441 if (!n)
2442 rc = GPG_ERR_BAD_DATA;
2444 else {
2445 /* Check if the content root element can create a DTD root element. */
2446 if (!xmlStrEqual((xmlChar *)"element", root->name)) {
2447 rc = GPG_ERR_SYNTAX;
2448 goto fail;
2451 xmlChar *a;
2453 if ((a = xmlGetProp(root, (xmlChar *)"_name")) == NULL) {
2454 rc = GPG_ERR_SYNTAX;
2455 goto fail;
2458 char *tmp = str_dup((char *)a);
2459 xmlFree(a);
2460 int literal = is_literal_element(&tmp);
2462 if (!valid_xml_element((xmlChar *)tmp) || literal) {
2463 xfree(tmp);
2464 rc = GPG_ERR_INV_VALUE;
2465 goto fail;
2468 if (strv_printf(&path, "%s", tmp) == 0) {
2469 xfree(tmp);
2470 rc = GPG_ERR_ENOMEM;
2471 goto fail;
2474 xfree(tmp);
2475 n = find_root_element(client->doc, &path, &rc, NULL, 0, 1);
2477 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND) {
2478 rc = GPG_ERR_BAD_DATA;
2479 goto fail;
2482 /* Overwriting the existing tree. */
2483 if (!rc) {
2484 xmlUnlinkNode(n);
2485 xmlFreeNodeList(n);
2488 rc = 0;
2489 xmlSetProp(root, (xmlChar *)"_name", (xmlChar *)path[0]);
2490 n = xmlCopyNode(root, 1);
2491 n = xmlAddChildList(xmlDocGetRootElement(client->doc), n);
2494 if (n && !rc)
2495 rc = update_element_mtime(n->parent);
2497 fail:
2498 if (doc)
2499 xmlFreeDoc(doc);
2501 if (path)
2502 strv_free(path);
2504 strv_free(req);
2505 return rc;
2508 static gpg_error_t import_command(assuan_context_t ctx, char *line)
2510 gpg_error_t rc;
2511 struct client_s *client = assuan_get_pointer(ctx);
2512 unsigned char *result;
2513 size_t len;
2515 rc = assuan_inquire(ctx, "DATA", &result, &len, 0);
2516 if (rc)
2517 return send_error(ctx, rc);
2519 rc = do_import(client, result);
2520 return send_error(ctx, rc);
2523 static gpg_error_t do_lock(struct client_s *client, int add)
2525 gpg_error_t rc = lock_file_mutex(client, add);
2527 if (!rc)
2528 client->flags |= FLAG_LOCK_CMD;
2530 return rc;
2533 static gpg_error_t lock_command(assuan_context_t ctx, char *line)
2535 struct client_s *client = assuan_get_pointer(ctx);
2536 gpg_error_t rc = do_lock(client, 0);
2538 return send_error(ctx, rc);
2541 static gpg_error_t unlock_command(assuan_context_t ctx, char *line)
2543 struct client_s *client = assuan_get_pointer(ctx);
2544 gpg_error_t rc;
2546 rc = unlock_file_mutex(client, 0);
2547 return send_error(ctx, rc);
2550 static gpg_error_t option_command(assuan_context_t ctx, const char *name,
2551 const char *value)
2553 struct client_s *client = assuan_get_pointer(ctx);
2554 struct agent_s *agent = client->crypto->agent;
2555 gpg_error_t rc = 0;
2557 log_write1("OPTION name='%s' value='%s'", name, value);
2559 if (strcasecmp(name, (char *)"log_level") == 0) {
2560 long l = 0;
2562 if (value) {
2563 l = strtol(value, NULL, 10);
2565 if (l < 0 || l > 2)
2566 return gpg_error(GPG_ERR_INV_VALUE);
2569 MUTEX_LOCK(&rcfile_mutex);
2570 config_set_int_param(&global_config, "global", "log_level", value);
2571 MUTEX_UNLOCK(&rcfile_mutex);
2572 goto done;
2574 else if (strcasecmp(name, (char *)"lock-timeout") == 0) {
2575 long n = 0;
2576 char *p = NULL;
2578 if (value) {
2579 n = strtol(value, &p, 10);
2580 if (p && *p)
2581 return gpg_error(GPG_ERR_INV_VALUE);
2584 client->lock_timeout = n;
2585 goto done;
2587 else if (strcasecmp(name, (char *)"NAME") == 0) {
2588 char *tmp = pthread_getspecific(thread_name_key);
2590 if (tmp)
2591 xfree(tmp);
2593 if (!value || !*value) {
2594 pthread_setspecific(thread_name_key, str_dup(""));
2595 goto done;
2598 pthread_setspecific(thread_name_key, str_dup(value));
2599 goto done;
2601 else if (strcasecmp(name, (char *)"lc-messages") == 0) {
2602 rc = set_agent_option(client->crypto->agent, "lc-messages", value);
2603 if (!rc) {
2604 xfree(agent->lc_messages);
2605 agent->lc_messages = str_dup(value);
2608 else if (strcasecmp(name, (char *)"lc-ctype") == 0) {
2609 rc = set_agent_option(client->crypto->agent, "lc-ctype", value);
2610 if (!rc) {
2611 xfree(agent->lc_ctype);
2612 agent->lc_ctype = str_dup(value);
2615 else if (strcasecmp(name, (char *)"ttyname") == 0) {
2616 rc = set_agent_option(client->crypto->agent, "ttyname", value);
2617 if (!rc) {
2618 xfree(agent->ttyname);
2619 agent->ttyname = str_dup(value);
2622 else if (strcasecmp(name, (char *)"ttytype") == 0) {
2623 rc = set_agent_option(client->crypto->agent, "ttytype", value);
2624 if (!rc) {
2625 xfree(agent->ttytype);
2626 agent->ttytype = str_dup(value);
2629 else if (strcasecmp(name, (char *)"display") == 0) {
2630 rc = set_agent_option(client->crypto->agent, "display", value);
2631 if (!rc) {
2632 xfree(agent->display);
2633 agent->display = str_dup(value);
2636 else if (strcasecmp(name, (char *)"desc") == 0) {
2637 if (client->crypto->agent->desc)
2638 xfree(client->crypto->agent->desc);
2640 client->crypto->agent->desc = str_dup(value);
2641 if (!client->crypto->agent->desc)
2642 rc = GPG_ERR_ENOMEM;
2644 #if 0
2645 else if (strcasecmp(name, "pinentry_timeout") == 0) {
2646 char *p = NULL;
2647 int n;
2649 if (!value)
2650 goto done;
2652 n = (int)strtol(value, &p, 10);
2654 if (*p || n < 0)
2655 return gpg_error(GPG_ERR_INV_VALUE);
2657 MUTEX_LOCK(&rcfile_mutex);
2658 config_set_int_param(global_config, client->filename ? client->filename :
2659 "global", "pinentry_timeout", value);
2660 MUTEX_UNLOCK(&rcfile_mutex);
2661 goto done;
2663 #endif
2664 else
2665 return gpg_error(GPG_ERR_UNKNOWN_OPTION);
2667 done:
2668 return rc;
2671 static gpg_error_t do_rename(assuan_context_t ctx, char *line)
2673 struct client_s *client = assuan_get_pointer(ctx);
2674 gpg_error_t rc;
2675 char **req, **src, *dst;
2676 xmlNodePtr n, ndst;
2678 req = str_split(line, " ", 0);
2680 if (!req || !req[0] || !req[1]) {
2681 strv_free(req);
2682 return GPG_ERR_SYNTAX;
2685 dst = req[1];
2686 is_literal_element(&dst);
2688 if (!valid_xml_element((xmlChar *)dst)) {
2689 strv_free(req);
2690 return GPG_ERR_INV_VALUE;
2693 if (strchr(req[0], '\t'))
2694 src = str_split(req[0], "\t", 0);
2695 else
2696 src = str_split(req[0], " ", 0);
2698 if (!src || !*src) {
2699 rc = GPG_ERR_SYNTAX;
2700 goto fail;
2703 n = find_root_element(client->doc, &src, &rc, NULL, 0, 0);
2704 if (src[1] && n)
2705 n = find_elements(client->doc, n->children, src+1, &rc, NULL, NULL,
2706 NULL, 0, 0, NULL, 0);
2708 if (!n)
2709 goto fail;
2711 xmlChar *a = xmlGetProp(n, (xmlChar *)"_name");
2712 if (!a) {
2713 rc = GPG_ERR_ENOMEM;
2714 goto fail;
2717 /* To prevent unwanted effects:
2719 * <root name="a"><b/></root>
2721 * RENAME a<TAB>b b
2723 if (xmlStrEqual(a, (xmlChar *)dst)) {
2724 xmlFree(a);
2725 rc = GPG_ERR_AMBIGUOUS_NAME;
2726 goto fail;
2729 xmlFree(a);
2730 char **tmp = NULL;
2731 if (src[1]) {
2732 char **p;
2734 for (p = src; *p; p++) {
2735 if (!*(p+1))
2736 break;
2737 strv_printf(&tmp, "%s", *p);
2741 strv_printf(&tmp, "!%s", dst);
2742 ndst = find_root_element(client->doc, &tmp, &rc, NULL, 0, 0);
2743 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND) {
2744 strv_free(tmp);
2745 goto fail;
2748 if (tmp[1] && ndst)
2749 ndst = find_elements(client->doc, ndst->children, tmp+1, &rc, NULL,
2750 NULL, NULL, 0, 0, NULL, 0);
2752 strv_free(tmp);
2753 if (!ndst && rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2754 goto fail;
2756 rc = 0;
2758 /* Target may exist:
2760 * <root name="a"/>
2761 * <root name="b" target="a"/>
2763 * RENAME b a
2765 * Would need to do:
2766 * RENAME !b a
2768 if (ndst == n) {
2769 rc = GPG_ERR_AMBIGUOUS_NAME;
2770 goto fail;
2773 if (ndst) {
2774 unlink_node(ndst);
2775 xmlFreeNodeList(ndst);
2778 rc = add_attribute(n, "_name", dst);
2780 fail:
2781 strv_free(req);
2782 strv_free(src);
2783 return rc;
2786 static gpg_error_t rename_command(assuan_context_t ctx, char *line)
2788 struct client_s *client = assuan_get_pointer(ctx);
2789 gpg_error_t rc;
2790 struct argv_s *args[] = {
2791 &(struct argv_s) { "inquire", OPTION_TYPE_NOARG, parse_opt_inquire },
2792 NULL
2795 rc = parse_options(&line, args, client);
2796 if (rc)
2797 return send_error(ctx, rc);
2799 if (client->opts&OPT_INQUIRE) {
2800 unsigned char *result;
2801 size_t len;
2803 rc = assuan_inquire(ctx, "DATA", &result, &len, 0);
2804 if (rc)
2805 return send_error(ctx, rc);
2807 line = (char *)result;
2810 rc = do_rename(ctx, line);
2812 if (client->opts&OPT_INQUIRE)
2813 xfree(line);
2815 return send_error(ctx, rc);
2818 static gpg_error_t do_copy(assuan_context_t ctx, char *line)
2820 struct client_s *client = assuan_get_pointer(ctx);
2821 gpg_error_t rc;
2822 char **req, **src = NULL, **dst = NULL;
2823 xmlNodePtr nsrc, ndst, new = NULL;
2825 req = str_split(line, " ", 0);
2826 if (!req || !req[0] || !req[1]) {
2827 strv_free(req);
2828 return GPG_ERR_SYNTAX;
2831 if (strchr(req[0], '\t'))
2832 src = str_split(req[0], "\t", 0);
2833 else
2834 src = str_split(req[0], " ", 0);
2836 if (!src || !*src) {
2837 rc = GPG_ERR_SYNTAX;
2838 goto fail;
2841 if (strchr(req[1], '\t'))
2842 dst = str_split(req[1], "\t", 0);
2843 else
2844 dst = str_split(req[1], " ", 0);
2846 if (!dst || !*dst) {
2847 rc = GPG_ERR_SYNTAX;
2848 goto fail;
2851 if (!valid_element_path(dst, 0)) {
2852 rc = GPG_ERR_INV_VALUE;
2853 goto fail;
2856 nsrc = find_root_element(client->doc, &src, &rc, NULL, 0, 0);
2857 if (nsrc && src[1])
2858 nsrc = find_elements(client->doc, nsrc->children, src+1, &rc, NULL,
2859 NULL, NULL, 0, 0, NULL, 0);
2861 if (!nsrc)
2862 goto fail;
2864 new = xmlCopyNodeList(nsrc);
2865 if (!new) {
2866 rc = GPG_ERR_ENOMEM;
2867 goto fail;
2870 int create = 0;
2871 ndst = find_root_element(client->doc, &dst, &rc, NULL, 0, 0);
2872 if (ndst && dst[1]) {
2873 if (ndst->children)
2874 ndst = find_elements(client->doc, ndst->children, dst+1, &rc, NULL,
2875 NULL, create_target_elements_cb, 0, 0, NULL, 0);
2876 else
2877 create = 1;
2879 else
2880 create = 1;
2882 if (!ndst && rc != GPG_ERR_ELEMENT_NOT_FOUND)
2883 goto fail;
2884 else if (create) {
2885 ndst = create_element_path(client, &dst, &rc, NULL);
2886 if (!ndst)
2887 goto fail;
2890 /* Merge any attributes from the src node to the initial dst node. */
2891 for (xmlAttrPtr attr = new->properties; attr; attr = attr->next) {
2892 if (xmlStrEqual(attr->name, (xmlChar *)"_name"))
2893 continue;
2895 xmlAttrPtr a = xmlHasProp(ndst, attr->name);
2896 if (a)
2897 xmlRemoveProp(a);
2899 xmlChar *tmp = xmlNodeGetContent(attr->children);
2900 xmlNewProp(ndst, attr->name, tmp);
2901 xmlFree(tmp);
2902 rc = add_attribute(ndst, NULL, NULL);
2905 xmlNodePtr n = ndst->children;
2906 xmlUnlinkNode(n);
2907 xmlFreeNodeList(n);
2908 ndst->children = NULL;
2910 if (new->children) {
2911 n = xmlCopyNodeList(new->children);
2912 if (!n) {
2913 rc = GPG_ERR_ENOMEM;
2914 goto fail;
2917 n = xmlAddChildList(ndst, n);
2918 if (!n) {
2919 rc = GPG_ERR_ENOMEM;
2920 goto fail;
2923 rc = update_element_mtime(xmlDocGetRootElement(client->doc) ==
2924 ndst->parent ? ndst : ndst->parent);
2927 fail:
2928 if (new) {
2929 xmlUnlinkNode(new);
2930 xmlFreeNodeList(new);
2933 if (req)
2934 strv_free(req);
2936 if (src)
2937 strv_free(src);
2939 if (dst)
2940 strv_free(dst);
2942 return rc;
2945 static gpg_error_t copy_command(assuan_context_t ctx, char *line)
2947 struct client_s *client = assuan_get_pointer(ctx);
2948 gpg_error_t rc;
2949 struct argv_s *args[] = {
2950 &(struct argv_s) { "inquire", OPTION_TYPE_NOARG, parse_opt_inquire },
2951 NULL
2954 rc = parse_options(&line, args, client);
2955 if (rc)
2956 return send_error(ctx, rc);
2958 if (client->opts&OPT_INQUIRE) {
2959 unsigned char *result;
2960 size_t len;
2962 rc = assuan_inquire(ctx, "DATA", &result, &len, 0);
2963 if (rc)
2964 return send_error(ctx, rc);
2966 line = (char *)result;
2969 rc = do_copy(ctx, line);
2971 if (client->opts&OPT_INQUIRE)
2972 xfree(line);
2974 return send_error(ctx, rc);
2977 static gpg_error_t do_move(assuan_context_t ctx, char *line)
2979 struct client_s *client = assuan_get_pointer(ctx);
2980 gpg_error_t rc;
2981 char **req, **src = NULL, **dst = NULL;
2982 xmlNodePtr nsrc, ndst = NULL;
2984 req = str_split(line, " ", 0);
2986 if (!req || !req[0] || !req[1]) {
2987 strv_free(req);
2988 return GPG_ERR_SYNTAX;
2991 if (strchr(req[0], '\t'))
2992 src = str_split(req[0], "\t", 0);
2993 else
2994 src = str_split(req[0], " ", 0);
2996 if (!src || !*src) {
2997 rc = GPG_ERR_SYNTAX;
2998 goto fail;
3001 if (strchr(req[1], '\t'))
3002 dst = str_split(req[1], "\t", 0);
3003 else
3004 dst = str_split(req[1], " ", 0);
3006 nsrc = find_root_element(client->doc, &src, &rc, NULL, 0, 0);
3007 if (nsrc && src[1])
3008 nsrc = find_elements(client->doc, nsrc->children, src+1, &rc, NULL,
3009 NULL, NULL, 0, 0, NULL, 0);
3011 if (!nsrc)
3012 goto fail;
3014 if (dst) {
3015 if (!valid_element_path(dst, 0)) {
3016 rc = GPG_ERR_INV_VALUE;
3017 goto fail;
3020 ndst = find_root_element(client->doc, &dst, &rc, NULL, 0, 0);
3021 if (ndst && dst[1])
3022 ndst = find_elements(client->doc, ndst->children, dst+1, &rc, NULL,
3023 NULL, NULL, 0, 0, NULL, 0);
3025 else
3026 ndst = xmlDocGetRootElement(client->doc);
3028 for (xmlNodePtr n = ndst; n; n = n->parent) {
3029 if (n == nsrc) {
3030 rc = GPG_ERR_CONFLICT;
3031 goto fail;
3035 if (rc && rc != GPG_ERR_ELEMENT_NOT_FOUND)
3036 goto fail;
3038 rc = 0;
3040 if (ndst) {
3041 xmlChar *a = node_has_attribute(nsrc, (xmlChar *)"_name");
3042 xmlNodePtr dup = find_element(ndst->children, (char *)a, NULL);
3044 xmlFree(a);
3045 if (dup) {
3046 if (dup == nsrc)
3047 goto fail;
3049 if (ndst == xmlDocGetRootElement(client->doc)) {
3050 xmlNodePtr n = nsrc;
3051 int match = 0;
3053 while (n->parent && n->parent != ndst)
3054 n = n->parent;
3056 xmlChar *a = node_has_attribute(n, (xmlChar *)"_name");
3057 xmlChar *b = node_has_attribute(nsrc, (xmlChar *)"_name");
3059 if (xmlStrEqual(a, b)) {
3060 match = 1;
3061 xmlUnlinkNode(nsrc);
3062 xmlUnlinkNode(n);
3063 xmlFreeNodeList(n);
3066 xmlFree(a);
3067 xmlFree(b);
3069 if (!match) {
3070 xmlUnlinkNode(dup);
3071 xmlFreeNodeList(dup);
3074 else
3075 xmlUnlinkNode(dup);
3079 if (!ndst && dst) {
3080 xmlChar *name = node_has_attribute(nsrc, (xmlChar *)"_name");
3082 if (nsrc->parent == xmlDocGetRootElement(client->doc)
3083 && !strcmp((char *)name, *dst)) {
3084 xmlFree(name);
3085 rc = GPG_ERR_CONFLICT;
3086 goto fail;
3089 xmlFree(name);
3090 ndst = create_element_path(client, &dst, &rc, nsrc);
3093 if (!ndst)
3094 goto fail;
3096 update_element_mtime(nsrc->parent);
3097 xmlUnlinkNode(nsrc);
3098 ndst = xmlAddChildList(ndst, nsrc);
3100 if (!ndst)
3101 rc = GPG_ERR_ENOMEM;
3103 update_element_mtime(ndst->parent);
3105 fail:
3106 if (req)
3107 strv_free(req);
3109 if (src)
3110 strv_free(src);
3112 if (dst)
3113 strv_free(dst);
3115 return rc;
3118 static gpg_error_t move_command(assuan_context_t ctx, char *line)
3120 struct client_s *client = assuan_get_pointer(ctx);
3121 gpg_error_t rc;
3122 struct argv_s *args[] = {
3123 &(struct argv_s) { "inquire", OPTION_TYPE_NOARG, parse_opt_inquire },
3124 NULL
3127 rc = parse_options(&line, args, client);
3128 if (rc)
3129 return send_error(ctx, rc);
3131 if (client->opts&OPT_INQUIRE) {
3132 unsigned char *result;
3133 size_t len;
3135 rc = assuan_inquire(ctx, "DATA", &result, &len, 0);
3136 if (rc)
3137 return send_error(ctx, rc);
3139 line = (char *)result;
3142 rc = do_move(ctx, line);
3144 if (client->opts&OPT_INQUIRE)
3145 xfree(line);
3147 return send_error(ctx, rc);
3150 static gpg_error_t ls_command(assuan_context_t ctx, char *line)
3152 gpg_error_t rc;
3153 char *tmp = str_asprintf("%s/data", homedir);
3154 char *dir = expand_homedir(tmp);
3155 DIR *d = opendir(dir);
3157 rc = gpg_error_from_syserror();
3158 xfree(tmp);
3160 if (!d) {
3161 xfree(dir);
3162 return send_error(ctx, rc);
3165 size_t len = offsetof(struct dirent, d_name)+pathconf(dir, _PC_NAME_MAX)+1;
3166 struct dirent *p = xmalloc(len), *cur = NULL;
3167 char *list = NULL;
3169 xfree(dir);
3170 rc = 0;
3172 while (!readdir_r(d, p, &cur) && cur) {
3173 if (cur->d_name[0] == '.' && cur->d_name[1] == '\0')
3174 continue;
3175 else if (cur->d_name[0] == '.' && cur->d_name[1] == '.' && cur->d_name[2] == '\0')
3176 continue;
3178 tmp = str_asprintf("%s%s\n", list ? list : "", cur->d_name);
3180 if (!tmp) {
3181 if (list)
3182 xfree(list);
3184 rc = GPG_ERR_ENOMEM;
3185 break;
3188 xfree(list);
3189 list = tmp;
3192 closedir(d);
3193 xfree(p);
3195 if (rc)
3196 return send_error(ctx, rc);
3198 if (!list)
3199 return send_error(ctx, GPG_ERR_NO_DATA);
3201 list[strlen(list)-1] = 0;
3202 rc = xfer_data(ctx, list, strlen(list));
3203 xfree(list);
3204 return send_error(ctx, rc);
3207 static gpg_error_t bye_notify(assuan_context_t ctx, char *line)
3209 struct client_s *cl = assuan_get_pointer(ctx);
3211 #ifdef WITH_GNUTLS
3212 if (cl->thd->remote) {
3213 int rc;
3215 do {
3216 rc = gnutls_bye(cl->thd->tls->ses, GNUTLS_SHUT_RDWR);
3217 if (rc == GNUTLS_E_AGAIN)
3218 usleep(50000);
3219 } while (rc == GNUTLS_E_AGAIN);
3221 #endif
3223 /* This will let assuan_process_next() return. */
3224 fcntl(cl->thd->fd, F_SETFL, O_NONBLOCK);
3225 cl->last_rc = 0; // BYE command result
3226 return 0;
3229 static gpg_error_t reset_notify(assuan_context_t ctx, char *line)
3231 struct client_s *client = assuan_get_pointer(ctx);
3233 if (client)
3234 cleanup_client(client);
3236 return 0;
3240 * This is called before every Assuan command.
3242 static gpg_error_t command_startup(assuan_context_t ctx, const char *name)
3244 struct client_s *client = assuan_get_pointer(ctx);
3245 gpg_error_t rc;
3246 struct command_table_s *cmd = NULL;
3248 log_write1("command='%s'", name);
3249 client->last_rc = client->opts = 0;
3251 for (int i = 0; command_table[i]; i++) {
3252 if (!strcasecmp(name, command_table[i]->name)) {
3253 if (command_table[i]->ignore_startup)
3254 return 0;
3255 cmd = command_table[i];
3256 break;
3260 client->last_rc = rc = gpg_error(file_modified(client, cmd));
3261 return rc;
3265 * This is called after every Assuan command.
3267 static void command_finalize(assuan_context_t ctx, gpg_error_t rc)
3269 struct client_s *client = assuan_get_pointer(ctx);
3271 if (!(client->flags&FLAG_LOCK_CMD))
3272 unlock_file_mutex(client, 0);
3274 log_write1(_("command completed: rc=%u"), client->last_rc);
3275 client->last_rc = gpg_error(GPG_ERR_UNKNOWN_COMMAND);
3278 static gpg_error_t help_command(assuan_context_t ctx, char *line)
3280 gpg_error_t rc;
3281 int i;
3283 if (!line || !*line) {
3284 char *tmp;
3285 char *help = str_dup(_(
3286 "Usage: HELP [<COMMAND>]\n"
3287 "For commands that take an element path as an argument, each element is "
3288 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3289 "\n"
3290 "COMMANDS:"));
3292 for (i = 0; command_table[i]; i++) {
3293 if (!command_table[i]->help)
3294 continue;
3296 tmp = str_asprintf("%s %s", help, command_table[i]->name);
3297 xfree(help);
3298 help = tmp;
3301 tmp = strip_texi_and_wrap(help);
3302 xfree(help);
3303 rc = xfer_data(ctx, tmp, strlen(tmp));
3304 xfree(tmp);
3305 return send_error(ctx, rc);
3308 for (i = 0; command_table[i]; i++) {
3309 if (!strcasecmp(line, command_table[i]->name)) {
3310 char *help, *tmp;
3312 if (!command_table[i]->help)
3313 break;
3315 help = strip_texi_and_wrap(command_table[i]->help);
3316 tmp = str_asprintf(_("Usage: %s"), help);
3317 xfree(help);
3318 rc = xfer_data(ctx, tmp, strlen(tmp));
3319 xfree(tmp);
3320 return send_error(ctx, rc);
3324 return send_error(ctx, GPG_ERR_INV_NAME);
3327 static void new_command(const char *name, int ignore, int unlock,
3328 gpg_error_t (*handler)(assuan_context_t, char *), const char *help)
3330 int i = 0;
3332 if (command_table)
3333 for (i = 0; command_table[i]; i++);
3335 command_table = xrealloc(command_table, (i+2)*sizeof(struct command_table_s *));
3336 command_table[i] = xcalloc(1, sizeof(struct command_table_s));
3337 command_table[i]->name = name;
3338 command_table[i]->handler = handler;
3339 command_table[i]->ignore_startup = ignore;
3340 command_table[i]->unlock = unlock;
3341 command_table[i++]->help = help;
3342 command_table[i] = NULL;
3345 void deinit_commands()
3347 int i;
3349 for (i = 0; command_table[i]; i++)
3350 xfree(command_table[i]);
3352 xfree(command_table);
3355 static int sort_commands(const void *arg1, const void *arg2)
3357 struct command_table_s* const *a = arg1;
3358 struct command_table_s* const *b = arg2;
3360 if (!*a || !*b)
3361 return 0;
3362 else if (*a && !*b)
3363 return 1;
3364 else if (!*a && *b)
3365 return -1;
3367 return strcmp((*a)->name, (*b)->name);
3370 static gpg_error_t passwd_command(assuan_context_t ctx, char *line)
3372 struct client_s *client = assuan_get_pointer(ctx);
3373 gpg_error_t rc;
3374 struct argv_s *args[] = {
3375 &(struct argv_s) { "reset", OPTION_TYPE_NOARG, parse_opt_reset },
3376 &(struct argv_s) { "s2k-count", OPTION_TYPE_ARG, parse_opt_s2k_count },
3377 NULL
3380 if (client->flags&FLAG_NEW)
3381 return send_error(ctx, GPG_ERR_INV_STATE);
3383 client->crypto->save.s2k_count = config_get_ulong(client->filename, "s2k_count");
3384 rc = parse_options(&line, args, client);
3385 if (rc)
3386 return send_error(ctx, rc);
3388 if (!rc && client->opts&OPT_RESET) {
3389 rc = cache_clear(client->md5file);
3390 if (!rc)
3391 send_status_all(STATUS_CACHE, NULL);
3394 if (!rc) {
3395 if (client->crypto->save.s2k_count)
3396 rc = send_to_agent(client->crypto->agent, NULL, NULL,
3397 "OPTION s2k-count=%lu", client->crypto->save.s2k_count);
3399 if (!rc)
3400 rc = agent_passwd(client->crypto);
3403 return send_error(ctx, rc);
3406 static gpg_error_t parse_keygrip_opt_sign(void * data, void * value)
3408 struct client_s *client = data;
3410 (void)value;
3411 client->opts |= OPT_SIGN;
3412 return 0;
3415 static gpg_error_t keygrip_command(assuan_context_t ctx, char *line)
3417 struct client_s *client = assuan_get_pointer(ctx);
3418 gpg_error_t rc;
3419 struct crypto_s *crypto;
3420 struct argv_s *args[] = {
3421 &(struct argv_s) { "sign", OPTION_TYPE_NOARG, parse_keygrip_opt_sign },
3422 NULL
3425 if (!line || !*line)
3426 return send_error(ctx, GPG_ERR_SYNTAX);
3428 rc = parse_options(&line, args, client);
3429 if (rc)
3430 return send_error(ctx, rc);
3432 if (!valid_filename(line))
3433 return send_error(ctx, GPG_ERR_INV_VALUE);
3435 rc = init_client_crypto(&crypto);
3436 if (rc)
3437 return send_error(ctx, rc);
3439 rc = read_data_file(line, crypto);
3440 if (!rc) {
3441 char *hexgrip = NULL;
3443 if (client->opts&OPT_SIGN) {
3444 if (valid_keygrip(crypto->sign_grip, sizeof(crypto->sign_grip)))
3445 hexgrip = bin2hex(crypto->sign_grip, sizeof(crypto->sign_grip));
3448 if (!hexgrip)
3449 hexgrip = bin2hex(crypto->grip, sizeof(crypto->grip));
3451 if (!hexgrip)
3452 rc = GPG_ERR_ENOMEM;
3453 else
3454 rc = xfer_data(ctx, hexgrip, strlen(hexgrip));
3456 xfree(hexgrip);
3459 cleanup_crypto(&crypto);
3460 return send_error(ctx, rc);
3463 static gpg_error_t parse_opt_data(void * data, void * value)
3465 struct client_s *client = data;
3467 (void)value;
3468 client->opts |= OPT_DATA;
3469 return 0;
3472 static gpg_error_t getinfo_command(assuan_context_t ctx, char *line)
3474 struct client_s *client = assuan_get_pointer(ctx);
3475 gpg_error_t rc;
3476 char buf[ASSUAN_LINELENGTH];
3477 struct argv_s *args[] = {
3478 &(struct argv_s) { "data", OPTION_TYPE_NOARG, parse_opt_data },
3479 NULL
3482 rc = parse_options(&line, args, client);
3483 if (rc)
3484 return send_error(ctx, rc);
3486 if (!strcasecmp(line, "clients")) {
3487 if (client->opts&OPT_DATA) {
3488 MUTEX_LOCK(&cn_mutex);
3489 snprintf(buf, sizeof(buf), "%i", slist_length(cn_thread_list));
3490 MUTEX_UNLOCK(&cn_mutex);
3491 rc = xfer_data(ctx, buf, strlen(buf));
3493 else
3494 rc = send_status(ctx, STATUS_CLIENTS, NULL);
3496 else if (!strcasecmp(line, "cache")) {
3497 if (client->opts&OPT_DATA) {
3498 snprintf(buf, sizeof(buf), "%u", cache_file_count());
3499 rc = xfer_data(ctx, buf, strlen(buf));
3501 else
3502 rc = send_status(ctx, STATUS_CACHE, NULL);
3504 else if (!strcasecmp(line, "pid")) {
3505 char buf[32];
3506 pid_t pid = getpid();
3508 snprintf(buf, sizeof(buf), "%i", pid);
3509 rc = xfer_data(ctx, buf, strlen(buf));
3511 else if (!strcasecmp(line, "version")) {
3512 char *buf = str_asprintf("0x%06x %s", VERSION_HEX,
3513 #ifdef WITH_LIBACL
3514 "ACL "
3515 #endif
3516 #ifdef WITH_GNUTLS
3517 "GNUTLS "
3518 #endif
3519 "");
3520 rc = xfer_data(ctx, buf, strlen(buf));
3521 xfree(buf);
3523 else if (!strcasecmp(line, "last_error")) {
3524 if (client->last_error)
3525 rc = xfer_data(ctx, client->last_error, strlen(client->last_error));
3526 else
3527 rc = GPG_ERR_NO_DATA;
3529 else
3530 rc = gpg_error(GPG_ERR_SYNTAX);
3532 return send_error(ctx, rc);
3535 static gpg_error_t send_data_cb(void * user, const void *buf, size_t len)
3537 assuan_context_t ctx = user;
3539 return assuan_send_data(ctx, buf, len);
3542 static gpg_error_t send_status_cb(void * user, const char *line)
3544 assuan_context_t ctx = user;
3545 char keyword[200], *k;
3546 const char *p;
3548 for (p = line, k = keyword; *p; p++) {
3549 if (isspace(*p))
3550 break;
3552 *k++ = *p;
3555 *k = 0;
3556 if (*p == '#')
3557 p++;
3559 while (isspace(*p))
3560 p++;
3562 return assuan_write_status(ctx, keyword, *p ? p : NULL);
3565 static gpg_error_t agent_command(assuan_context_t ctx, char *line)
3567 gpg_error_t rc;
3568 struct client_s *client = assuan_get_pointer(ctx);
3570 if (!line || !*line)
3571 return send_error(ctx, GPG_ERR_SYNTAX);
3573 assuan_set_flag(client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 1);
3574 rc = assuan_transact(client->crypto->agent->ctx, line, send_data_cb,
3575 client->ctx, agent_loopback_cb, client->crypto, send_status_cb,
3576 client->ctx);
3577 if (gpg_err_code(rc) == GPG_ERR_ASS_CANCELED) {
3578 char *line;
3579 size_t len;
3581 rc = assuan_write_line(client->crypto->agent->ctx, "CAN");
3582 if (!rc) {
3583 rc = assuan_read_line(client->crypto->agent->ctx, &line, &len);
3584 if (!rc)
3585 rc = gpg_error(GPG_ERR_ASS_CANCELED);
3589 assuan_set_flag(client->crypto->agent->ctx, ASSUAN_CONVEY_COMMENTS, 0);
3590 return send_error(ctx, rc);
3593 void init_commands()
3595 /* !BEGIN-HELP-TEXT!
3597 * This comment is used as a marker to generate the offline documentation
3598 * found in doc/pwmd.info. The indentation needs to be kept for the awk
3599 * script to determine where commands begin and end.
3601 new_command("HELP", 1, 1, help_command, _(
3602 "HELP [<COMMAND>]\n"
3603 "Show available commands or command specific help text."
3606 new_command("AGENT", 1, 1, agent_command, _(
3607 "AGENT <command>\n"
3608 "Send a @command{gpg-agent} protocol @var{command} directly to the "
3609 "@command{gpg-agent}."
3612 new_command("GETINFO", 1, 1, getinfo_command, _(
3613 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
3614 "Get server and other information: @var{cache} returns the number of cached "
3615 "documents via a status message. @var{clients} returns the number of "
3616 "connected clients via a status message. @var{pid} returns the process ID "
3617 "number of the server via a data response. @var{VERSION} returns the server "
3618 "version number and compile-time features with a data response with each "
3619 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
3620 "the last failed command when available. @xref{Status Messages}. "
3621 "\n"
3622 "When the @option{--data} option is specified then the result will be send "
3623 "via a data response rather than a status message."
3626 new_command("PASSWD", 0, 0, passwd_command, _(
3627 "PASSWD [--reset] [--s2k-count=N]\n"
3628 "Changes the passphrase of the secret key required to open the current "
3629 "file. When the @option{--reset} option is passed then the cache entry for "
3630 "the current file will be reset and the passphrase, if any, will be required "
3631 "during the next @code{OPEN}. @xref{OPEN}."
3632 "\n"
3633 "The @option{--s2k-count} option sets number of hash iterations for a "
3634 "passphrase and must be either @code{0} to use the calibrated count of the "
3635 "machine (the default), or a value greater than or equal to @code{65536}. "
3636 "@xref{SAVE}."
3639 new_command("KEYGRIP", 1, 1, keygrip_command, _(
3640 "KEYGRIP [--sign] <filename>\n"
3641 "Returns the hex encoded keygrip of the specified @var{filename} with a "
3642 "data response."
3643 "\n"
3644 "When the @option{--sign} option is specified then the key used for signing "
3645 "of the specified @var{filename} will be returned."
3648 new_command("OPEN", 1, 1, open_command, _(
3649 "OPEN [--lock] [--no-pinentry] <filename> [<passphrase>]\n"
3650 "Opens @var{filename} using @var{passphrase}. When the filename is not "
3651 "found on the file-system then a new document will be created. If the file "
3652 "is found, it is looked for in the file cache. If cached and no "
3653 "@var{passphrase} was specified then the cached document is opened. When not "
3654 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
3655 "for decryption unless @option{--no-pinentry} was specified (see below)."
3656 "\n"
3657 "When the @option{--lock} option is passed then the file mutex will be "
3658 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
3659 "file has been opened."
3660 "\n"
3661 "By default, a pinentry is used to retrieve a passphrase when required. "
3662 "Passing @option{--no-pinentry} will disable pinentry use for the rest of "
3663 "the session. When pinentry use is disabled but required for some operation "
3664 "then a server @emph{INQUIRE} will be send to the client to retrieve the "
3665 "passphrase. See the @code{OPTION} command (@pxref{OPTION}), for pinentry "
3666 "specific options."
3669 new_command("SAVE", 0, 0, save_command, _(
3670 "SAVE [--no-passphrase] [--reset] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring [--sign-keygrip=hexstring]]\n"
3671 "Writes the @abbr{XML} document to disk. The file written to is the file that "
3672 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
3673 "new one or the option @option{--inquire-keyparam} was passed, then a new "
3674 "keypair will be generated and a pinentry will be used to prompt for the "
3675 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
3676 "passed, in which case the data file will not be passphrase protected."
3677 "\n"
3678 "The @option{--reset} option will clear the cache entry for the current file "
3679 "before saving."
3680 "\n"
3681 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
3682 "an alternate cipher. The default is @code{aes256}. See the Configuration "
3683 "(@pxref{Configuration}) for available ciphers."
3684 "\n"
3685 "The @option{--cipher-iterations} option specifies the number of times to "
3686 "encrypt the XML data. The default is 0 although 1 iteration is still done."
3687 "\n"
3688 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
3689 "the client to obtain the key paramaters to use when generating a new "
3690 "keypair. The inquired data is expected to be an S-expression. If not "
3691 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
3692 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
3693 "that when this option is specified a new keypair will be generated "
3694 "reguardless if the file is a new one or not."
3695 "\n"
3696 "You can encrypt the data file to a public key other than the one that it "
3697 "was originally encrypted with by passing the @option{--keygrip} option with "
3698 "the hex encoded keygrip of the public key as its argument. The keygrip may "
3699 "be of any key that @command{gpg-agent} knows about. The "
3700 "@option{--sign-keygrip} option may also be used to sign with an alternate "
3701 "secret key. This option may be needed when using a smartcard."
3702 "\n"
3703 "The @option{--s2k-count} option sets number of hash iterations for a "
3704 "passphrase. A value less-than @code{65536} will use the machine calibrated "
3705 "value which is the default. This setting only affects new files. To change "
3706 "the setting, use the @code{PASSWD} command (@pxref{PASSWD})."
3709 new_command("ISCACHED", 1, 0, iscached_command, _(
3710 "ISCACHED [--lock] <filename>\n"
3711 "An @emph{OK} response is returned if the specified @var{filename} is found "
3712 "in the file cache. If not found in the cache but exists on the filesystem "
3713 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
3714 "returned."
3715 "\n"
3716 "The @option{lock} option will lock the file mutex of @var{filename} when the "
3717 "file exists; it does not need to be opened nor cached."
3720 new_command("CLEARCACHE", 1, 1, clearcache_command, _(
3721 "CLEARCACHE [<filename>]\n"
3722 "Clears a file cache entry for all or the specified @var{filename}."
3725 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command, _(
3726 "CACHETIMEOUT <filename> <seconds>\n"
3727 "The time in @var{seconds} until @var{filename} will be removed from the "
3728 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
3729 "the passphrase for each @code{OPEN} command (@pxref{OPEN}). "
3730 "@xref{Configuration}, and the @code{cache_timeout} parameter."
3733 new_command("LIST", 0, 1, list_command, _(
3734 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
3735 "If no element path is given then a newline separated list of root elements "
3736 "is returned with a data response. If given, then all reachable elements "
3737 "of the specified element path are returned unless the @option{--no-recurse} "
3738 "option is specified. If specified, only the child elements of the element "
3739 "path are returned without recursing into grandchildren. Each resulting "
3740 "element is prefixed with the literal @code{!} character when the element "
3741 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
3742 "\n"
3743 "When the @option{--verbose} option is passed then each element path "
3744 "returned will have zero or more flags appened to it. These flags are "
3745 "delimited from the element path by a single space character. A flag itself "
3746 "is a single character. Flag @code{+} indicates that there are child nodes of "
3747 "the current element path. Flag @code{E} indicates that an element of an "
3748 "element path contained in a @var{target} attribute could not be found. Flag "
3749 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
3750 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
3751 "of a @var{target} attribute (see below)."
3752 "\n"
3753 "The @option{--with-target} option implies @option{--verbose} and will append "
3754 "an additional flag @code{T} followed by a single space then an element path. "
3755 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
3756 "current element when it contains a @var{target} attribute. When no "
3757 "@var{target} attribute is found then no flag will be appended."
3758 "\n"
3759 "The @option{--no-recurse} option limits the amount of data returned to only "
3760 "the listing of children of the specified element path and not any "
3761 "grandchildren."
3762 "\n"
3763 "The @option{--all} option lists the entire element tree for each root "
3764 "element. This option also implies option @option{--verbose}."
3765 "\n"
3766 "When the @option{--inquire} option is passed then all remaining non-option "
3767 "arguments are retrieved via a server @emph{INQUIRE}."
3770 new_command("REALPATH", 0, 1, realpath_command, _(
3771 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
3772 "Resolves all @code{target} attributes of the specified element path and "
3773 "returns the result with a data response. @xref{Target Attribute}, for details."
3774 "\n"
3775 "When the @option{--inquire} option is passed then all remaining non-option "
3776 "arguments are retrieved via a server @emph{INQUIRE}."
3779 new_command("STORE", 0, 1, store_command, _(
3780 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
3781 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
3782 "\n"
3783 "Creates a new element path or modifies the @var{content} of an existing "
3784 "element. If only a single element is specified then a new root element is "
3785 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
3786 "set to the final @key{TAB} delimited element. If no @var{content} is "
3787 "specified after the final @key{TAB}, then the content of the element will "
3788 "be removed, or empty when creating a new element."
3789 "\n"
3790 "The only restriction of an element name is that it not contain whitespace "
3791 "or begin with the literal element character @code{!} unless specifying a "
3792 "literal element (@pxref{Target Attribute}). There is no whitespace between "
3793 "the @key{TAB} delimited elements. It is recommended that the content of an "
3794 "element be base64 encoded when it contains control or @key{TAB} characters "
3795 "to prevent @abbr{XML} and @command{pwmd} parsing errors."
3798 new_command("RENAME", 0, 1, rename_command, _(
3799 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
3800 "Renames the specified @var{element} to the new @var{value}. If an element of "
3801 "the same name as the @var{value} already exists it will be overwritten."
3802 "\n"
3803 "When the @option{--inquire} option is passed then all remaining non-option "
3804 "arguments are retrieved via a server @emph{INQUIRE}."
3807 new_command("COPY", 0, 1, copy_command, _(
3808 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
3809 "Copies the entire element tree starting from the child node of the source "
3810 "element, to the destination element path. If the destination element path "
3811 "does not exist then it will be created; otherwise it is overwritten."
3812 "\n"
3813 "Note that attributes from the source element are merged into the "
3814 "destination element when the destination element path exists. When an "
3815 "attribute of the same name exists in both the source and destination "
3816 "elements then the destination attribute will be updated to the source "
3817 "attribute value."
3818 "\n"
3819 "When the @option{--inquire} option is passed then all remaining non-option "
3820 "arguments are retrieved via a server @emph{INQUIRE}."
3823 new_command("MOVE", 0, 1, move_command, _(
3824 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
3825 "Moves the source element path to the destination element path. If the "
3826 "destination is not specified then it will be moved to the root node of the "
3827 "document. If the destination is specified and exists then it will be "
3828 "overwritten; otherwise it will be created."
3829 "\n"
3830 "When the @option{--inquire} option is passed then all remaining non-option "
3831 "arguments are retrieved via a server @emph{INQUIRE}."
3834 new_command("DELETE", 0, 1, delete_command, _(
3835 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
3836 "Removes the specified element path and all of its children. This may break "
3837 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
3838 "refers to this element or any of its children."
3839 "\n"
3840 "When the @option{--inquire} option is passed then all remaining non-option "
3841 "arguments are retrieved via a server @emph{INQUIRE}."
3844 new_command("GET", 0, 1, get_command, _(
3845 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
3846 "Retrieves the content of the specified element. The content is returned "
3847 "with a data response."
3848 "\n"
3849 "When the @option{--inquire} option is passed then all remaining non-option "
3850 "arguments are retrieved via a server @emph{INQUIRE}."
3853 new_command("ATTR", 0, 1, attr_command, _(
3854 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
3855 "@table @asis\n"
3856 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
3857 "\n"
3858 " Stores or updates an @var{attribute} name and optional @var{value} of an "
3859 "element. When no @var{value} is specified any existing value will be removed."
3860 "\n"
3861 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
3862 "\n"
3863 " Removes an @var{attribute} from an element."
3864 "\n"
3865 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
3866 "\n"
3867 " Retrieves a newline separated list of attributes names and values "
3868 "from the specified element. Each attribute name and value is space delimited."
3869 "\n"
3870 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
3871 "\n"
3872 " Retrieves the value of an @var{attribute} from an element."
3873 "@end table\n"
3874 "\n"
3875 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
3876 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
3877 "commands instead."
3878 "\n"
3879 "The @code{_mtime} attribute is updated each time an element is modified by "
3880 "either storing content, editing attributes or by deleting a child element. "
3881 "The @code{_ctime} attribute is created for each new element in an element "
3882 "path."
3883 "\n"
3884 "When the @option{--inquire} option is passed then all remaining non-option "
3885 "arguments are retrieved via a server @emph{INQUIRE}."
3886 "\n"
3887 "@xref{Target Attribute}, for details about this special attribute."
3890 new_command("XPATH", 0, 1, xpath_command, _(
3891 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
3892 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
3893 "specified, it is assumed the expression is a request to return a result. "
3894 "Otherwise, the result is set to the @var{value} argument and the document is "
3895 "updated. If there is no @var{value} after the @key{TAB} character, the value "
3896 "is assumed to be empty and the document is updated. For example:"
3897 "@sp 1\n"
3898 "@example\n"
3899 "XPATH //element[@@_name='password']@key{TAB}\n"
3900 "@end example\n"
3901 "@sp 1"
3902 "would clear the content of all @code{password} elements in the data file "
3903 "while leaving off the trailing @key{TAB} would return all @code{password} "
3904 "elements in @abbr{XML} format."
3905 "\n"
3906 "When the @option{--inquire} option is passed then all remaining non-option "
3907 "arguments are retrieved via a server @emph{INQUIRE}."
3908 "\n"
3909 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
3910 "expression syntax."
3913 new_command("XPATHATTR", 0, 1, xpathattr_command, _(
3914 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
3915 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
3916 "attributes and does not return a result. For the @var{SET} operation the "
3917 "@var{value} is optional but the field is required. If not specified then "
3918 "the attribute value will be empty. For example:"
3919 "@sp 1"
3920 "@example\n"
3921 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
3922 "@end example\n"
3923 "@sp 1"
3924 "would create an @code{password} attribute for each @code{password} element "
3925 "found in the document. The attribute value will be empty but still exist."
3926 "\n"
3927 "When the @option{--inquire} option is passed then all remaining non-option "
3928 "arguments are retrieved via a server @emph{INQUIRE}."
3929 "\n"
3930 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
3931 "expression syntax."
3934 new_command("IMPORT", 0, 1, import_command, _(
3935 "IMPORT <content>[<TAB>[!]element[<TAB>[!]child[..]]]\n"
3936 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
3937 "\n"
3938 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
3939 "argument is raw @abbr{XML} data. The content is created as a child of the "
3940 "specified element path and will overwrite an existing element of the same "
3941 "name. If an element of the element path does not exist then it will be "
3942 "created."
3943 "\n"
3944 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
3945 "for details."
3948 new_command("DUMP", 0, 1, dump_command, _(
3949 "DUMP\n"
3950 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
3951 "dumping a specific node."
3954 new_command("LOCK", 0, 0, lock_command, _(
3955 "LOCK\n"
3956 "Locks the mutex associated with the opened file. This prevents other clients "
3957 "from sending commands to the same opened file until the client "
3958 "that sent this command either disconnects or sends the @code{UNLOCK} "
3959 "command. @xref{UNLOCK}."
3962 new_command("UNLOCK", 1, 0, unlock_command, _(
3963 "UNLOCK\n"
3964 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
3965 "a commands' @option{lock} option. @xref{LOCK}."
3968 new_command("GETCONFIG", 1, 1, getconfig_command, _(
3969 "GETCONFIG [filename] <parameter>\n"
3970 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
3971 "data response. If no file has been opened then the value for @var{filename} "
3972 "or the default from the @samp{global} section will be returned. If a file "
3973 "has been opened and no @var{filename} is specified, a value previously "
3974 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
3977 new_command("OPTION", 1, 1, NULL, _(
3978 "OPTION <NAME>=<VALUE>\n"
3979 "Sets a client option @var{name} to @var{value}. The value for an option is "
3980 "kept for the duration of the connection."
3981 "\n"
3982 "@table @asis\n"
3983 "@item TTYNAME\n"
3984 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
3985 "\n"
3986 "@item TTYTYPE\n"
3987 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
3988 "\n"
3989 "@item DISPLAY\n"
3990 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
3991 "\n"
3992 "@item DESC\n"
3993 " Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
3994 "\n"
3995 "@item LC-CTYPE\n"
3996 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
3997 "\n"
3998 "@item LC-MESSAGES\n"
3999 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4000 "\n"
4001 "@item NAME\n"
4002 " Associates the thread ID of the connection with the specified textual "
4003 "representation. Useful for debugging log messages."
4004 "\n"
4005 "@item LOCK-TIMEOUT\n"
4006 " When not @code{0}, the duration in tenths of a second to wait for the file "
4007 "mutex which has been locked by another thread to be released before returning "
4008 "an error. When @code{-1}, then an error will be returned immediately."
4009 "@end table\n"
4012 new_command("LS", 1, 1, ls_command, _(
4013 "LS\n"
4014 "Lists the available data files stored in the data directory "
4015 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4018 new_command("RESET", 1, 1, NULL, _(
4019 "RESET\n"
4020 "Closes the currently opened file but keeps any previously set client options."
4023 new_command("NOP", 1, 1, NULL, _(
4024 "NOP\n"
4025 "Does nothing. Always returns successfully."
4028 /* !END-HELP-TEXT! */
4029 new_command("CANCEL", 1, 1, NULL, NULL);
4030 new_command("END", 1, 1, NULL, NULL);
4031 new_command("BYE", 1, 1, NULL, NULL);
4033 int i;
4034 for (i = 0; command_table[i]; i++);
4035 qsort(command_table, i-1, sizeof(struct command_table_s *), sort_commands);
4038 gpg_error_t register_commands(assuan_context_t ctx)
4040 int i = 0, rc;
4042 for (; command_table[i]; i++) {
4043 if (!command_table[i]->handler)
4044 continue;
4046 rc = assuan_register_command (ctx, command_table[i]->name,
4047 command_table[i]->handler, command_table[i]->help);
4048 if (rc)
4049 return rc;
4052 rc = assuan_register_option_handler(ctx, option_command);
4053 if (rc)
4054 return rc;
4056 rc = assuan_register_bye_notify(ctx, bye_notify);
4057 if (rc)
4058 return rc;
4060 rc = assuan_register_reset_notify(ctx, reset_notify);
4061 if (rc)
4062 return rc;
4064 rc = assuan_register_pre_cmd_notify(ctx, command_startup);
4065 if (rc)
4066 return rc;
4068 return assuan_register_post_cmd_notify(ctx, command_finalize);