Blame src/trustlist.c

Packit Service 672cf4
/* trustlist.c - Trust item listing.
Packit Service 6c01f9
   Copyright (C) 2000 Werner Koch (dd9jn)
Packit Service 6c01f9
   Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH
Packit Service 6c01f9
Packit Service 6c01f9
   This file is part of GPGME.
Packit Service 6c01f9
Packit Service 6c01f9
   GPGME is free software; you can redistribute it and/or modify it
Packit Service 6c01f9
   under the terms of the GNU Lesser General Public License as
Packit Service 6c01f9
   published by the Free Software Foundation; either version 2.1 of
Packit Service 6c01f9
   the License, or (at your option) any later version.
Packit Service 6c01f9
Packit Service 6c01f9
   GPGME is distributed in the hope that it will be useful, but
Packit Service 6c01f9
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 6c01f9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 6c01f9
   Lesser General Public License for more details.
Packit Service 6c01f9
Packit Service 6c01f9
   You should have received a copy of the GNU Lesser General Public
Packit Service 6c01f9
   License along with this program; if not, write to the Free Software
Packit Service 6c01f9
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
Packit Service 6c01f9
   02111-1307, USA.  */
Packit Service 672cf4
Packit Service 672cf4
#if HAVE_CONFIG_H
Packit Service 672cf4
#include <config.h>
Packit Service 672cf4
#endif
Packit Service 672cf4
#include <stdlib.h>
Packit Service 672cf4
#include <string.h>
Packit Service 672cf4
#include <assert.h>
Packit Service 672cf4
#include <errno.h>
Packit Service 672cf4
Packit Service 672cf4
#include "gpgme.h"
Packit Service 672cf4
#include "debug.h"
Packit Service 672cf4
#include "util.h"
Packit Service 672cf4
#include "context.h"
Packit Service 672cf4
#include "ops.h"
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
struct trust_queue_item_s
Packit Service 672cf4
{
Packit Service 672cf4
  struct trust_queue_item_s *next;
Packit Service 672cf4
  gpgme_trust_item_t item;
Packit Service 672cf4
};
Packit Service 672cf4
Packit Service 672cf4
typedef struct
Packit Service 672cf4
{
Packit Service 672cf4
  /* Something new is available.  */
Packit Service 672cf4
  int trust_cond;
Packit Service 672cf4
  struct trust_queue_item_s *trust_queue;
Packit Service 672cf4
} *op_data_t;
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
static gpgme_error_t
Packit Service 672cf4
trustlist_status_handler (void *priv, gpgme_status_code_t code, char *args)
Packit Service 672cf4
{
Packit Service 672cf4
  (void)priv;
Packit Service 672cf4
  (void)code;
Packit Service 672cf4
  (void)args;
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* This handler is used to parse the output of --list-trust-path:
Packit Service 672cf4
   Format:
Packit Service 672cf4
   level:keyid:type:recno:ot:val:mc:cc:name:
Packit Service 672cf4
   With TYPE = U for a user ID
Packit Service 672cf4
               K for a key
Packit Service 672cf4
   The RECNO is either the one of the dir record or the one of the uid
Packit Service 6c01f9
   record.  OT is the the usual trust letter and only availabel on K
Packit Service 672cf4
   lines.  VAL is the calculated validity MC is the marginal trust
Packit Service 672cf4
   counter and only available on U lines CC is the same for the
Packit Service 672cf4
   complete count NAME ist the username and only printed on U
Packit Service 672cf4
   lines.  */
Packit Service 672cf4
static gpgme_error_t
Packit Service 672cf4
trustlist_colon_handler (void *priv, char *line)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_ctx_t ctx = (gpgme_ctx_t) priv;
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
  char *p, *pend;
Packit Service 672cf4
  int field = 0;
Packit Service 672cf4
  gpgme_trust_item_t item = NULL;
Packit Service 672cf4
Packit Service 672cf4
  if (!line)
Packit Service 672cf4
    return 0; /* EOF */
Packit Service 672cf4
Packit Service 672cf4
  for (p = line; p; p = pend)
Packit Service 672cf4
    {
Packit Service 672cf4
      field++;
Packit Service 672cf4
      pend = strchr (p, ':');
Packit Service 672cf4
      if (pend)
Packit Service 672cf4
	*pend++ = 0;
Packit Service 672cf4
Packit Service 672cf4
      switch (field)
Packit Service 672cf4
	{
Packit Service 672cf4
	case 1: /* level */
Packit Service 672cf4
	  err = _gpgme_trust_item_new (&item);
Packit Service 672cf4
	  if (err)
Packit Service 672cf4
	    return err;
Packit Service 672cf4
	  item->level = atoi (p);
Packit Service 672cf4
	  break;
Packit Service 672cf4
	case 2: /* long keyid */
Packit Service 672cf4
	  if (strlen (p) == DIM(item->keyid) - 1)
Packit Service 672cf4
	    strcpy (item->keyid, p);
Packit Service 672cf4
	  break;
Packit Service 672cf4
	case 3: /* type */
Packit Service 672cf4
	  item->type = *p == 'K'? 1 : *p == 'U'? 2 : 0;
Packit Service 672cf4
	  break;
Packit Service 672cf4
	case 5: /* owner trust */
Packit Service 672cf4
	  item->_owner_trust[0] = *p;
Packit Service 672cf4
	  break;
Packit Service 672cf4
	case 6: /* validity */
Packit Service 672cf4
	  item->_validity[0] = *p;
Packit Service 672cf4
	  break;
Packit Service 672cf4
	case 9: /* user ID */
Packit Service 672cf4
	  item->name = strdup (p);
Packit Service 672cf4
	  if (!item->name)
Packit Service 672cf4
	    {
Packit Service 672cf4
              int saved_err = gpg_error_from_syserror ();
Packit Service 672cf4
	      gpgme_trust_item_unref (item);
Packit Service 672cf4
	      return saved_err;
Packit Service 672cf4
	    }
Packit Service 672cf4
	  break;
Packit Service 672cf4
        }
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  if (item)
Packit Service 672cf4
    _gpgme_engine_io_event (ctx->engine, GPGME_EVENT_NEXT_TRUSTITEM, item);
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
void
Packit Service 672cf4
_gpgme_op_trustlist_event_cb (void *data, gpgme_event_io_t type,
Packit Service 672cf4
			      void *type_data)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_ctx_t ctx = (gpgme_ctx_t) data;
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
  void *hook;
Packit Service 672cf4
  op_data_t opd;
Packit Service 672cf4
  gpgme_trust_item_t item = (gpgme_trust_item_t) type_data;
Packit Service 672cf4
  struct trust_queue_item_s *q, *q2;
Packit Service 672cf4
Packit Service 672cf4
  assert (type == GPGME_EVENT_NEXT_TRUSTITEM);
Packit Service 672cf4
Packit Service 672cf4
  err = _gpgme_op_data_lookup (ctx, OPDATA_TRUSTLIST, &hook, -1, NULL);
Packit Service 672cf4
  opd = hook;
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return;
Packit Service 672cf4
Packit Service 672cf4
  q = malloc (sizeof *q);
Packit Service 672cf4
  if (!q)
Packit Service 672cf4
    {
Packit Service 672cf4
      gpgme_trust_item_unref (item);
Packit Service 672cf4
      /* FIXME: GPGME_Out_Of_Core; */
Packit Service 672cf4
      return;
Packit Service 672cf4
    }
Packit Service 672cf4
  q->item = item;
Packit Service 672cf4
  q->next = NULL;
Packit Service 672cf4
  /* FIXME: Use a tail pointer */
Packit Service 672cf4
  q2 = opd->trust_queue;
Packit Service 672cf4
  if (!q2)
Packit Service 672cf4
    opd->trust_queue = q;
Packit Service 672cf4
  else
Packit Service 672cf4
    {
Packit Service 672cf4
      while (q2->next)
Packit Service 672cf4
	q2 = q2->next;
Packit Service 672cf4
      q2->next = q;
Packit Service 672cf4
    }
Packit Service 672cf4
  /* FIXME: unlock queue */
Packit Service 672cf4
  opd->trust_cond = 1;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_trustlist_start (gpgme_ctx_t ctx, const char *pattern, int max_level)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err = 0;
Packit Service 672cf4
  void *hook;
Packit Service 672cf4
  op_data_t opd;
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_BEG2 (DEBUG_CTX, "gpgme_op_trustlist_start", ctx,
Packit Service 672cf4
	      "pattern=%s, max_level=%i", pattern, max_level);
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx || !pattern || !*pattern)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
Packit Service 672cf4
Packit Service 672cf4
  err = _gpgme_op_reset (ctx, 2);
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return TRACE_ERR (err);
Packit Service 672cf4
Packit Service 672cf4
  err = _gpgme_op_data_lookup (ctx, OPDATA_TRUSTLIST, &hook,
Packit Service 672cf4
			       sizeof (*opd), NULL);
Packit Service 672cf4
  opd = hook;
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return TRACE_ERR (err);
Packit Service 672cf4
Packit Service 672cf4
  _gpgme_engine_set_status_handler (ctx->engine,
Packit Service 672cf4
				    trustlist_status_handler, ctx);
Packit Service 672cf4
  err = _gpgme_engine_set_colon_line_handler (ctx->engine,
Packit Service 672cf4
					      trustlist_colon_handler, ctx);
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return TRACE_ERR (err);
Packit Service 672cf4
Packit Service 672cf4
  err = _gpgme_engine_op_trustlist (ctx->engine, pattern);
Packit Service 672cf4
  return TRACE_ERR (err);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_trustlist_next (gpgme_ctx_t ctx, gpgme_trust_item_t *r_item)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
  void *hook;
Packit Service 672cf4
  op_data_t opd;
Packit Service 672cf4
  struct trust_queue_item_s *q;
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_BEG (DEBUG_CTX, "gpgme_op_trustlist_next", ctx);
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx || !r_item)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
Packit Service 672cf4
  *r_item = NULL;
Packit Service 672cf4
  if (!ctx)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
Packit Service 672cf4
Packit Service 672cf4
  err = _gpgme_op_data_lookup (ctx, OPDATA_TRUSTLIST, &hook, -1, NULL);
Packit Service 672cf4
  opd = hook;
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return TRACE_ERR (err);
Packit Service 672cf4
  if (opd == NULL)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
Packit Service 672cf4
Packit Service 672cf4
  if (!opd->trust_queue)
Packit Service 672cf4
    {
Packit Service 672cf4
      err = _gpgme_wait_on_condition (ctx, &opd->trust_cond, NULL);
Packit Service 672cf4
      if (err)
Packit Service 672cf4
	return TRACE_ERR (err);
Packit Service 672cf4
      if (!opd->trust_cond)
Packit Service 672cf4
	return TRACE_ERR (gpg_error (GPG_ERR_EOF));
Packit Service 672cf4
      opd->trust_cond = 0;
Packit Service 672cf4
      assert (opd->trust_queue);
Packit Service 672cf4
    }
Packit Service 672cf4
  q = opd->trust_queue;
Packit Service 672cf4
  opd->trust_queue = q->next;
Packit Service 672cf4
Packit Service 672cf4
  *r_item = q->item;
Packit Service 672cf4
  free (q);
Packit Service 672cf4
  if ((*r_item)->type == 1)
Packit Service 672cf4
    {
Packit Service 6c01f9
      TRACE_SUC5 ("trust_item=%p: %s: owner trust %s with level %i "
Packit Service 6c01f9
		  "and validity 0x%x", *r_item, (*r_item)->keyid,
Packit Service 672cf4
		  (*r_item)->owner_trust, (*r_item)->level,
Packit Service 672cf4
		  (*r_item)->validity);
Packit Service 672cf4
    }
Packit Service 672cf4
  else if ((*r_item)->type == 2)
Packit Service 672cf4
    {
Packit Service 6c01f9
      TRACE_SUC5 ("trust_item=%p: %s: UID %s with level %i "
Packit Service 6c01f9
		  "and validity 0x%x", *r_item, (*r_item)->keyid,
Packit Service 672cf4
		  (*r_item)->name, (*r_item)->level, (*r_item)->validity);
Packit Service 672cf4
    }
Packit Service 672cf4
  else
Packit Service 672cf4
    {
Packit Service 6c01f9
      TRACE_SUC5 ("trust_item=%p: %s: unknown type %i with level %i "
Packit Service 6c01f9
		  "and validity 0x%x", *r_item, (*r_item)->keyid,
Packit Service 672cf4
		  (*r_item)->type, (*r_item)->level, (*r_item)->validity);
Packit Service 672cf4
    }
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Terminate a pending trustlist operation within CTX.  */
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_trustlist_end (gpgme_ctx_t ctx)
Packit Service 672cf4
{
Packit Service 6c01f9
  TRACE (DEBUG_CTX, "gpgme_op_trustlist_end", ctx);
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx)
Packit Service 672cf4
    return gpg_error (GPG_ERR_INV_VALUE);
Packit Service 672cf4
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}