--- /dev/null
+/*
+ main.c - regex policy daemon
+
+ Copyright (C)
+ 2007, Christian Thaeter <ct@pipapo.org>
+
+ This program 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.
+
+ This program 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "rxpd.c"
+
+void
+usage(void)
+{
+ /*
+ rxpd [options] ..files..
+ -v verbosity
+ -V version
+ -d daemonize
+ -D debug
+ -b dir basedir for rules
+ -q quiet
+ -t port tcp
+ -u name unix
+ -p policy access policies
+ */
+
+
+}
+
+
+
+
+int
+main (int argc, char** argv)
+{
+ struct rxpd_base* base = rxpd_init ("./");
+
+ // parse commandline args
+
+ // read files
+ rxpd_file_load (base, "test");
+
+ // initialize listening connections
+
+ // eventloop
+
+
+
+ //
+ rxpd_destroy ();
+}
+/*
+ rxpd.c - regex policy daemon
+ Copyright (C)
+ 2007, Christian Thaeter <ct@pipapo.org>
+ This program 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.
-#define RXPD_CMD(cmd) #cmd":"
-enum RXPD_COMMANDS;
-#undef RXPD_CMD
+ This program 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.
-// int stat(const char *path, struct stat *buf);
+ 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "rxpd.h"
+
+#include <string.h>
+#include <stdlib.h>
+
+static struct rxpd_base global_base = {NULL};
+
+struct rxpd_base*
+rxpd_init (char* rulesdir)
+{
+ if (global_base.rulesdir)
+ return NULL;
+
+ global_base.rulesdir = strdup (rulesdir);
+ if (!global_base.rulesdir) abort();
+
+ psplay_init_root (&global_base.files, rxpd_file_cmp, rxpd_file_delete);
+ return &global_base;
+}
+
+void
+rxpd_destroy (void)
+{
+ if (global_base.rulesdir)
+ {
+ free (global_base.rulesdir);
+ psplay_destroy_root (&global_base.files);
+ }
+}
+
+
+//
+struct rxpd_rule*
+rxpd_rule_new (const char* buf)
+{
+ struct rxpd_rule* self = malloc (sizeof (struct rxpd_rule));
+ if (self)
+ {
+ llist_init (&self->node);
+
+ if (*buf != '#')
+ {
+ int err;
+ char* rxstart = strchr (buf, ':') + 1;
+
+ err = regcomp (&self->rx, rxstart, REG_EXTENDED|REG_ICASE|REG_NOSUB);
+
+ if (!err)
+ {
+ self->string = strdup (buf);
+ if (!self->string) abort();
+ }
+ else
+ {
+ regfree (&self->rx);
+ char ebuf[256];
+ ebuf[0] = '#';
+ size_t len = regerror (err, NULL, ebuf+1, 255);
+ self->string = malloc(len + strlen(buf) + 2);
+ if (!self->string) abort();
+ strcpy (self->string, ebuf);
+ strcat (self->string, ": ");
+ strcat (self->string, buf);
+ }
+ }
+ }
+ return self;
+}
+
+void
+rxpd_rule_delete (struct rxpd_rule* rule)
+{
+ if (rule)
+ {
+ llist_unlink (&rule->node);
+ if (rule->string[0] != '#')
+ regfree (&rule->rx);
+ free (rule->string);
+ free(rule);
+ }
+}
+
+//
+
+
+int
+rxpd_file_load (struct rxpd_base* base, const char* filename)
+{
+ char buf[2048];
+
+ // TODO better filenname validation / error handling
+ if (!filename ||
+ strchr (filename, '/') ||
+ strlen (filename) + strlen (base->rulesdir) > 2047)
+ abort();
+
+ strcpy (buf, base->rulesdir);
+ strcat (buf, filename);
+ filename = strdup (buf);
+
+ struct rxpd_file* file = malloc (sizeof (struct rxpd_file));
+ if (!file || !filename) abort();
+
+ psplay_init (&file->node, filename);
+ llist_init (&file->rules);
+
+ FILE* f = fopen (filename, "r");
+ // TODO error handling
+ if (!f) abort();
+
+ while (fgets (buf, 2048, f))
+ {
+ struct rxpd_rule* rule;
+ rule = rxpd_rule_new (buf);
+ if (!rule)
+ abort();
+
+ llist_insert_tail (&file->rules, &rule->node);
+ }
+
+ fclose (f);
+
+ psplay_insert (&base->files, &file->node);
+
+ return 0;
+}
+
+void
+rxpd_file_delete (PSplay f)
+{
+ if (f)
+ {
+ struct rxpd_file* file = (struct rxpd_file*)f;
+ LLIST_WHILE_HEAD (&file->rules, n)
+ {
+ struct rxpd_rule* node = (struct rxpd_rule*)n;
+ rxpd_rule_delete (node);
+ }
+ free ((void*)file->node.key);
+ free (f);
+ }
+}
+
+int
+rxpd_file_cmp (const void* A, const void* B)
+{
+ return strcmp (A, B);
+}
+/*
+ rxpd.h - regex policy daemon
+
+ Copyright (C)
+ 2007, Christian Thaeter <ct@pipapo.org>
+
+ This program 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.
+
+ This program 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
#ifndef RXPD_H
#define RXPD_H
#include <sys/stat.h>
#include <unistd.h>
#include <regex.h>
-#include <>
+//#include <>
#include "llist.h"
+#include "psplay.h"
+
#define RXPD_COMMANDS \
{ \
enum RXPD_COMMANDS;
#undef RXPD_CMD
-struct base
+struct rxpd_base;
+struct rxpd_file;
+struct rxpd_rule;
+
+struct rxpd_base
{
char* rulesdir;
- void* files;
+ psplayroot files;
};
-struct file
-{
- char* filename;
- struct stat last_stat;
- llist rules;
-};
+struct rxpd_base*
+rxpd_init (char* rulesdir);
+void
+rxpd_destroy (void);
-struct rule
+
+
+//
+struct rxpd_rule
{
+ llist node;
char* string;
- char* regex;
regex_t rx;
- llist node;
};
+struct rxpd_rule*
+rxpd_rule_new (const char* buf);
+
+void
+rxpd_rule_delete (struct rxpd_rule*);
+
+
+
+//
+struct rxpd_file
+{
+ psplay node;
+ //TODO later struct stat last_stat;
+ llist rules;
+};
+
+int
+rxpd_file_load (struct rxpd_base* base, const char* filename);
+
+void
+rxpd_file_delete (PSplay file);
+
+int
+rxpd_file_cmp (const void* A, const void* B);