Beispiel #1
0
/**
 * assuan_accept:
 * @ctx: context
 * 
 * Cancel any existing connection and wait for a connection from a
 * client.  The initial handshake is performed which may include an
 * initial authentication or encryption negotiation.
 * 
 * Return value: 0 on success or an error if the connection could for
 * some reason not be established.
 **/
assuan_error_t
assuan_accept (assuan_context_t ctx)
{
  int rc;
  const char *p, *pend;

  if (!ctx)
    return _assuan_error (ASSUAN_Invalid_Value);

  if (ctx->pipe_mode > 1)
    return -1; /* second invocation for pipemode -> terminate */
  ctx->finish_handler (ctx);

  rc = ctx->accept_handler (ctx);
  if (rc)
    return rc;

  /* Send the hello. */
  p = ctx->hello_line;
  if (p && (pend = strchr (p, '\n')))
    { /* This is a multi line hello.  Send all but the last line as
         comments. */
      do
        {
          rc = _assuan_write_line (ctx, "# ", p, pend - p);
          if (rc)
            return rc;
          p = pend + 1;
          pend = strchr (p, '\n');
        }
      while (pend);
      rc = _assuan_write_line (ctx, "OK ", p, strlen (p));
    }
  else if (p)
    rc = assuan_write_line (ctx, p);
  else
    rc = assuan_write_line (ctx, "OK Pleased to meet you");
  if (rc)
    return rc;
  
  if (ctx->pipe_mode)
    ctx->pipe_mode = 2;
  
  return 0;
}
gpg_error_t
assuan_write_line (assuan_context_t ctx, const char *line)
{
  size_t len;
  const char *str;

  if (! ctx)
    return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);

  /* Make sure that we never take a LF from the user - this might
     violate the protocol. */
  str = strchr (line, '\n');
  len = str ? (str - line) : strlen (line);

  if (str)
    _assuan_log_control_channel (ctx, 1,
                                 "supplied line with LF - truncated",
                                 NULL, 0, NULL, 0);

  return _assuan_write_line (ctx, NULL, line, len);
}
assuan_error_t 
assuan_write_line (assuan_context_t ctx, const char *line)
{
  size_t len;
  const char *s;

  if (!ctx)
    return ASSUAN_Invalid_Value;

  /* Make sure that we never take a LF from the user - this might
     violate the protocol. */
  s = strchr (line, '\n');
  len = s? (s-line) : strlen (line);

  if (ctx->log_fp && s)
    fprintf (ctx->log_fp, "%s[%u.%p] DBG: -> "
             "[supplied line contained a LF -truncated]\n",
             assuan_get_assuan_log_prefix (),
             (unsigned int)getpid (), (void *)ctx);

  return _assuan_write_line (ctx, NULL, line, len);
}