summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2001-11-06 15:42:37 +0000
committerWerner Koch <wk@gnupg.org>2001-11-06 15:42:37 +0000
commit360de5aa05c035bb1c83f6e6b1ede1d6af7b425d (patch)
treefe9de7be7440968458092e297500406c2d828756
parent092da669edd653ff3cac1fed3fdf35491352972e (diff)
downloadlibassuan-360de5aa05c035bb1c83f6e6b1ede1d6af7b425d.tar.gz
First chunk of code for the Assuan library
-rw-r--r--src/Makefile.am35
-rw-r--r--src/assuan-buffer.c44
-rw-r--r--src/assuan-defs.h73
-rw-r--r--src/assuan-handler.c253
-rw-r--r--src/assuan-pipe-server.c67
-rw-r--r--src/assuan-util.c85
-rw-r--r--src/assuan.h93
7 files changed, 650 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
new file mode 100644
index 0000000..c7f4495
--- /dev/null
+++ b/src/Makefile.am
@@ -0,0 +1,35 @@
+# Assuan Makefile for test purposes
+# Copyright (C) 2001 Free Software Foundation, Inc.
+#
+# This file is part of GnuPG.
+#
+# GnuPG is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# GnuPG is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+
+## Process this file with automake to produce Makefile.in
+
+INCLUDES = -I.. -I$(top_srcdir)/include
+
+noinst_LIBRARIES = libassuan.a
+
+
+#libassuan_a_LDFLAGS =
+libassuan_a_SOURCES = \
+ assuan.h \
+ assuan-defs.h \
+ assuan-util.c \
+ assuan-buffer.c \
+ assuan-handler.c \
+ assuan-pipe-server.c
+
diff --git a/src/assuan-buffer.c b/src/assuan-buffer.c
new file mode 100644
index 0000000..c767a0e
--- /dev/null
+++ b/src/assuan-buffer.c
@@ -0,0 +1,44 @@
+/* assuan-buffer.c - read and send data
+ * Copyright (C) 2001 Free Software Foundation, Inc.
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#include <config.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "assuan-defs.h"
+
+
+int
+_assuan_read_line (ASSUAN_CONTEXT ctx)
+{
+
+
+ return -1;
+}
+
+
+
+
+int
+_assuan_write_line (ASSUAN_CONTEXT ctx)
+{
+ return -1;
+}
+
diff --git a/src/assuan-defs.h b/src/assuan-defs.h
new file mode 100644
index 0000000..3402a91
--- /dev/null
+++ b/src/assuan-defs.h
@@ -0,0 +1,73 @@
+/* assuan-defs.c - Internal definitions to Assuan
+ * Copyright (C) 2001 Free Software Foundation, Inc.
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#ifndef ASSUAN_DEFS_H
+#define ASSUAN_DEFS_H
+
+#include "assuan.h"
+
+
+struct assuan_context_s {
+ AssuanError err_no;
+ const char *err_str;
+
+ struct {
+ int fd;
+ } inbound;
+
+ struct {
+ int fd;
+ } outbound;
+
+ int input_fd; /* set by INPUT command */
+ int output_fd; /* set by OUTPUT command */
+
+
+
+};
+
+
+/*-- assuan-handler.c --*/
+int _assuan_register_std_commands (ASSUAN_CONTEXT ctx);
+
+
+/*-- assuan-util.c --*/
+void *_assuan_malloc (size_t n);
+void *_assuan_calloc (size_t n, size_t m);
+void *_assuan_realloc (void *p, size_t n);
+void _assuan_free (void *p);
+
+#define xtrymalloc(a) _assuan_malloc ((a))
+#define xtrycalloc(a,b) _assuan_calloc ((a),(b))
+#define xtryrealloc(a,b) _assuan_realloc((a),(b))
+#define xfree(a) _assuan_free ((a))
+
+int _assuan_set_error (ASSUAN_CONTEXT ctx, int err, const char *text);
+#define set_error(c,e,t) _assuan_set_error ((c), ASSUAN_ ## e, (t))
+
+
+#endif /*ASSUAN_DEFS_H*/
+
+
+
+
+
+
+
diff --git a/src/assuan-handler.c b/src/assuan-handler.c
new file mode 100644
index 0000000..aeeb336
--- /dev/null
+++ b/src/assuan-handler.c
@@ -0,0 +1,253 @@
+/* assuan-handler.c - dispatch commands
+ * Copyright (C) 2001 Free Software Foundation, Inc.
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#include <config.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "assuan-defs.h"
+
+#define digitp(a) ((a) >= '0' && (a) <= '9')
+
+
+static int
+dummy_handler (ASSUAN_CONTEXT ctx, char *line)
+{
+ fprintf (stderr, "DBG-assuan: dummy handler called\n");
+ return set_error (ctx, Server_Fault, "no handler registered");
+}
+
+
+static int
+std_handler_nop (ASSUAN_CONTEXT ctx, char *line)
+{
+ fprintf (stderr, "DBG-assuan: processing a NOP `%s'\n", line);
+ return 0; /* okay */
+}
+
+static int
+std_handler_cancel (ASSUAN_CONTEXT ctx, char *line)
+{
+ fprintf (stderr, "DBG-assuan: processing a CANCEL `%s'\n", line);
+ return set_error (ctx, Not_Implemented, NULL);
+}
+
+static int
+std_handler_bye (ASSUAN_CONTEXT ctx, char *line)
+{
+ fprintf (stderr, "DBG-assuan: processing a BYE `%s'\n", line);
+ return set_error (ctx, Not_Implemented, NULL);
+}
+
+static int
+std_handler_auth (ASSUAN_CONTEXT ctx, char *line)
+{
+ fprintf (stderr, "DBG-assuan: processing a AUTH `%s'\n", line);
+ return set_error (ctx, Not_Implemented, NULL);
+}
+
+static int
+std_handler_reset (ASSUAN_CONTEXT ctx, char *line)
+{
+ fprintf (stderr, "DBG-assuan: processing a RESET `%s'\n", line);
+ return set_error (ctx, Not_Implemented, NULL);
+}
+
+static int
+std_handler_end (ASSUAN_CONTEXT ctx, char *line)
+{
+ fprintf (stderr, "DBG-assuan: processing a END `%s'\n", line);
+ return set_error (ctx, Not_Implemented, NULL);
+}
+
+static int
+parse_cmd_input_output (ASSUAN_CONTEXT ctx, char *line, int *rfd)
+{
+ char *endp;
+
+ if (strncmp (line, "FD=", 3))
+ return set_error (ctx, Syntax_Error, "FD=<n> expected");
+ line += 3;
+ if (!digitp (*line))
+ return set_error (ctx, Syntax_Error, "number required");
+ *rfd = strtoul (line, &endp, 10);
+ if (*endp)
+ return set_error (ctx, Syntax_Error, "garbage found");
+ if (*rfd == ctx->inbound.fd)
+ return set_error (ctx, Parameter_Conflict, "fd same as inbound fd");
+ if (*rfd == ctx->outbound.fd)
+ return set_error (ctx, Parameter_Conflict, "fd same as outbound fd");
+ return 0;
+}
+
+/* Format is INPUT FD=<n> */
+static int
+std_handler_input (ASSUAN_CONTEXT ctx, char *line)
+{
+ int rc, fd;
+
+ fprintf (stderr, "DBG-assuan: processing a INPUT `%s'\n", line);
+
+ rc = parse_cmd_input_output (ctx, line, &fd);
+ if (rc)
+ return rc;
+ ctx->input_fd = fd;
+ return 0;
+}
+
+/* Format is OUTPUT FD=<n> */
+static int
+std_handler_output (ASSUAN_CONTEXT ctx, char *line)
+{
+ int rc, fd;
+
+ rc = parse_cmd_input_output (ctx, line, &fd);
+ if (rc)
+ return rc;
+ ctx->output_fd = fd;
+ return 0;
+}
+
+
+
+
+
+/* This is a table with the standard commands and handler for them.
+ The table is used to initialize a new context and assuciate strings
+ and handlers with cmd_ids */
+static struct {
+ const char *name;
+ int cmd_id;
+ int (*handler)(ASSUAN_CONTEXT, char *line);
+ int always; /* always initializethis command */
+} std_cmd_table[] = {
+ { "NOP", ASSUAN_CMD_NOP, std_handler_nop, 1 },
+ { "CANCEL", ASSUAN_CMD_CANCEL, std_handler_cancel, 1 },
+ { "BYE", ASSUAN_CMD_BYE, std_handler_bye, 1 },
+ { "AUTH", ASSUAN_CMD_AUTH, std_handler_auth, 1 },
+ { "RESET", ASSUAN_CMD_RESET, std_handler_reset, 1 },
+ { "END", ASSUAN_CMD_END, std_handler_end, 1 },
+
+ { "INPUT", ASSUAN_CMD_INPUT, std_handler_input },
+ { "OUTPUT", ASSUAN_CMD_OUTPUT, std_handler_output },
+ { NULL }
+};
+
+
+
+static const char *
+std_cmd_name (int cmd_id)
+{
+ int i;
+
+ for (i=0; std_cmd_table[i].name; i++)
+ if (std_cmd_table[i].cmd_id == cmd_id)
+ return std_cmd_table[i].name;
+ return NULL;
+}
+
+
+
+/**
+ * assuan_register_command:
+ * @ctx: the server context
+ * @cmd_id: An ID value for the command
+ * @cmd_name: A string with the command name
+ * @handler: The handler function to be called
+ *
+ * Register a handler to be used for a given command.
+ *
+ * The @cmd_name must be %NULL for all @cmd_ids below
+ * %ASSUAN_CMD_USER becuase predefined values are used.
+ *
+ * Return value:
+ **/
+int
+assuan_register_command (ASSUAN_CONTEXT ctx,
+ int cmd_id, const char *cmd_name,
+ int (*handler)(ASSUAN_CONTEXT, char *))
+{
+ if (cmd_name && cmd_id < ASSUAN_CMD_USER)
+ return ASSUAN_Invalid_Value;
+
+ if (!cmd_name)
+ cmd_name = std_cmd_name (cmd_id);
+
+ if (!cmd_name)
+ return ASSUAN_Invalid_Value;
+
+ fprintf (stderr, "DBG-assuan: registering %d as `%s'\n", cmd_id, cmd_name);
+
+ return 0;
+}
+
+/* Helper to register the standards commands */
+int
+_assuan_register_std_commands (ASSUAN_CONTEXT ctx)
+{
+ int i, rc;
+
+ for (i=0; std_cmd_table[i].name; i++)
+ {
+ if (std_cmd_table[i].always)
+ {
+ rc = assuan_register_command (ctx, std_cmd_table[i].cmd_id, NULL,
+ std_cmd_table[i].handler);
+ if (rc)
+ return rc;
+ }
+ }
+ return 0;
+}
+
+
+
+/* Process the special data lines. The "D " has already been removed
+ from the line. As all handlers this function may modify the line. */
+static int
+handle_data_line (ASSUAN_CONTEXT ctx, char *line)
+{
+ return set_error (ctx, Not_Implemented, NULL);
+}
+
+
+/* Parse the line, break out the command, find it in the command
+ table, remove leading and white spaces from the arguments, all the
+ handler with the argument line and return the error */
+static int
+dispatch_command (ASSUAN_CONTEXT ctx, char *line)
+{
+ if (*line == 'D' && line[1] == ' ') /* divert to special handler */
+ return handle_data_line (ctx, line+2);
+
+
+ return set_error (ctx, Not_Implemented, NULL);
+}
+
+
+
+
+
+
+
+
+
+
diff --git a/src/assuan-pipe-server.c b/src/assuan-pipe-server.c
new file mode 100644
index 0000000..3dd0ab0
--- /dev/null
+++ b/src/assuan-pipe-server.c
@@ -0,0 +1,67 @@
+/* assuan-pipe-server.c - Assuan server working over a pipe
+ * Copyright (C) 2001 Free Software Foundation, Inc.
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#include <config.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "assuan-defs.h"
+
+
+int
+assuan_init_pipe_server (ASSUAN_CONTEXT *r_ctx, int filedes[2])
+{
+ ASSUAN_CONTEXT ctx;
+ int rc;
+
+ *r_ctx = NULL;
+ ctx = xtrycalloc (1, sizeof *ctx);
+ if (!ctx)
+ return ASSUAN_Out_Of_Core;
+ ctx->input_fd = -1;
+ ctx->output_fd = -1;
+
+ ctx->inbound.fd = filedes[0];
+ ctx->outbound.fd = filedes[0];
+
+ rc = _assuan_register_std_commands (ctx);
+ if (rc)
+ xfree (ctx);
+ else
+ *r_ctx = ctx;
+ return rc;
+}
+
+void
+assuan_deinit_pipe_server (ASSUAN_CONTEXT ctx)
+{
+ xfree (ctx);
+}
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/assuan-util.c b/src/assuan-util.c
new file mode 100644
index 0000000..849cd3f
--- /dev/null
+++ b/src/assuan-util.c
@@ -0,0 +1,85 @@
+/* assuan-util.c - Utility functions for Assuan
+ * Copyright (C) 2001 Free Software Foundation, Inc.
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#include <config.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "assuan-defs.h"
+
+
+static void *(*alloc_func)(size_t n) = malloc;
+static void *(*realloc_func)(void *p, size_t n) = realloc;
+static void (*free_func)(void*) = free;
+
+
+
+void
+assuan_set_malloc_hooks ( void *(*new_alloc_func)(size_t n),
+ void *(*new_realloc_func)(void *p, size_t n),
+ void (*new_free_func)(void*) )
+{
+ alloc_func = new_alloc_func;
+ realloc_func = new_realloc_func;
+ free_func = new_free_func;
+}
+
+void *
+_assuan_malloc (size_t n)
+{
+ return alloc_func (n);
+}
+
+void *
+_assuan_realloc (void *a, size_t n)
+{
+ return realloc_func (a, n);
+}
+
+void *
+_assuan_calloc (size_t n, size_t m)
+{
+ void *p = _assuan_malloc (n*m);
+ if (p)
+ memset (p, 0, n* m);
+ return p;
+}
+
+void
+_assuan_free (void *p)
+{
+ if (p)
+ free_func (p);
+}
+
+
+
+/* Store the error in the context so that the error sending function
+ can take out a descriptive text. We wight also want to store a
+ standard text when TEXT is NULL. Use the macro set_error instead of
+ this function. */
+int
+_assuan_set_error (ASSUAN_CONTEXT ctx, int err, const char *text)
+{
+ ctx->err_no = err;
+ ctx->err_str = text;
+ return err;
+}
+
diff --git a/src/assuan.h b/src/assuan.h
new file mode 100644
index 0000000..6fa0952
--- /dev/null
+++ b/src/assuan.h
@@ -0,0 +1,93 @@
+/* assuan.c - Definitions for the Assuna protocol
+ * Copyright (C) 2001 Free Software Foundation, Inc.
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#ifndef ASSUAN_H
+#define ASSUAN_H
+
+#ifdef __cplusplus
+extern "C" {
+#if 0
+ }
+#endif
+#endif
+
+typedef enum {
+ ASSUAN_No_Error = 0,
+ ASSUAN_General_Error = 1,
+ ASSUAN_Out_Of_Core = 2,
+ ASSUAN_Invalid_Value = 3,
+
+ /* error codes above 99 are meant as status codes */
+ ASSUAN_Unknown_Command = 100,
+ ASSUAN_Not_Implemented = 101,
+ ASSUAN_Server_Fault = 102,
+ ASSUAN_Syntax_Error = 103,
+ ASSUAN_Parameter_Error = 104,
+ ASSUAN_Parameter_Conflict = 105,
+
+
+ ASSUAN_Cert_Revoked = 301,
+ ASSUAN_No_CRL_For_Cert = 302,
+ ASSUNA_CRL_Too_Old = 303,
+
+} AssuanError;
+
+/* This is a list of pre-registered ASSUAN commands */
+typedef enum {
+ ASSUAN_CMD_NOP = 0,
+ ASSUAN_CMD_CANCEL, /* cancel the current request */
+ ASSUAN_CMD_BYE,
+ ASSUAN_CMD_AUTH,
+ ASSUAN_CMD_RESET,
+ ASSUAN_CMD_DATA,
+ ASSUAN_CMD_END,
+ ASSUAN_CMD_INPUT,
+ ASSUAN_CMD_OUTPUT,
+
+ ASSUAN_CMD_USER = 256 /* Other commands should be used with this offset*/
+} AssuanCommand;
+
+
+struct assuan_context_s;
+typedef struct assuan_context_s *ASSUAN_CONTEXT;
+
+/*-- assuan-handler --*/
+int assuan_register_command (ASSUAN_CONTEXT ctx,
+ int cmd_id, const char *cmd_string,
+ int (*handler)(ASSUAN_CONTEXT, char *));
+
+
+
+/*-- assuan-pipe-server.c --*/
+
+
+
+
+/*-- assuan-util.c --*/
+void assuan_set_malloc_hooks ( void *(*new_alloc_func)(size_t n),
+ void *(*new_realloc_func)(void *p, size_t n),
+ void (*new_free_func)(void*) );
+
+
+
+#ifdef __cplusplus
+}
+#endif
+#endif /*ASSUAN_H*/