2 rxpd_connection.c - regex policy daemon
5 2007, Christian Thaeter <ct@pipapo.org>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 This program 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 this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 struct rxpd_connection*
25 rxpd_connection_new (struct rxpd_socket* socket)
27 struct rxpd_connection* self;
28 self = rxpd_malloc (sizeof (struct rxpd_connection));
30 self->fd = accept (socket->fd, NULL, 0);
34 self->socket = socket;
37 llist_init (&self->tmp_list);
39 rxpd_buffer_init (&self->in, self);
40 rxpd_buffer_init (&self->out, self);
42 self->connecter = NULL;
45 rxpd_log (socket->base, LOG_INFO, "incoming connection\n");
50 rxpd_connection_delete (struct rxpd_connection* self)
54 // TODO kill connecter if not self
58 LLIST_WHILE_HEAD (&self->tmp_list, n)
60 struct rxpd_rule* node = (struct rxpd_rule*)n;
61 rxpd_rule_delete (node);
67 struct rxpd_connection*
68 rxpd_connection_spawn (struct rxpd_connection* self)
73 rxpd_die ("connection thread already spawned\n");
75 pth_attr_t attr = pth_attr_new ();
77 pth_attr_set (attr, PTH_ATTR_JOINABLE, FALSE);
79 self->connecter = pth_spawn (attr, rxpd_connection_parse_cmd, self);
82 rxpd_die ("failed spawning thread\n");
88 rxpd_connection_check_policy (struct rxpd_connection* self, char* line)
90 struct rxpd_base* base = self->socket->base;
96 if (!self->socket->rxpd_socket_addr (self, buf, line, 256))
98 rxpd_log (base, LOG_ERR, "policy line too long\n");
102 rxpd_log (base, LOG_DEBUG, "policy check '%s'\n", buf);
105 LLIST_FOREACH (&base->policy->rules, n)
107 struct rxpd_rule* rule = (struct rxpd_rule*)n;
108 if (rule->string[0] != '#')
110 if (regexec (&rule->rx, buf, 0, NULL, 0) == 0)
112 match = rule->string;
118 if (!match || strncmp("ACCEPT:", match, sizeof("ACCEPT:")-1) != 0)
120 rxpd_log (base, LOG_WARNING, "access denied '%s'\n", buf);
128 rxpd_connection_parse_cmd (void* ptr)
130 struct rxpd_connection* self = (struct rxpd_connection*) ptr;
131 struct rxpd_base* base = self->socket->base;
134 line = rxpd_buffer_readline (&self->in, 0);
138 rxpd_log (base, LOG_ERR, "no data\n");
139 rxpd_buffer_printf (&self->out, "#ERROR: no data\n");
144 rxpd_log (base, LOG_DEBUG, "parse command '%s'\n", line);
146 static const struct cmd_table
153 #define RXPD_CMD(cmd, _) {RXPD_CMD_##cmd, #cmd":", sizeof (#cmd)},
159 const struct cmd_table* i;
160 for (i = cmds; i->cmd; ++i)
161 if (strncmp (line, i->cmd, i->sz) == 0)
165 rxpd_log (base, LOG_ERR, "no command\n");
166 rxpd_buffer_printf (&self->out, "#ERROR: no command\n");
167 rxpd_connection_delete (self);
171 if (!rxpd_connection_check_policy (self, line))
173 rxpd_buffer_printf (&self->out, "#ERROR: access denied\n");
174 rxpd_connection_delete (self);
181 self->file = (struct rxpd_file*) psplay_find (&base->files, &line[i->sz]);
185 self->file = rxpd_file_new (base, &line[i->sz]);
188 rxpd_log (base, LOG_ERR, "illeagal filename\n");
189 rxpd_buffer_printf (&self->out, "#ERROR: illegal filename\n");
190 rxpd_connection_delete (self);
199 #define RXPD_CMD(cmd, _) \
203 //event_set (&self->ev, self->fd, EV_READ, rxpd_connection_cmd_##cmd, self);
204 //rxpd_connection_cmd_##cmd (fd, 0, ptr);