2 rxpd_rule.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.
25 rxpd_rule_new (const char* buf)
27 struct rxpd_rule* self = rxpd_malloc (sizeof (struct rxpd_rule));
29 llist_init (&self->node);
30 self->atime = (time_t)-1;
35 char* namestart = strchr (buf, ':');
36 char* rxstart = namestart? strchr (namestart+1, ':') : NULL;
39 self->string = rxpd_strdup ("#ERROR: Syntax error, line was neither a comment nor a rule");
45 self->atime = atoi (buf);
47 /* atime was zero or not set */
48 self->atime = time (NULL)-1;
51 // TODO regflags from base
52 err = regcomp (&self->rx, rxstart+1, REG_EXTENDED|REG_ICASE|REG_NOSUB);
55 self->string = rxpd_strdup (namestart+1);
60 size_t len = regerror (err, NULL, ebuf, 256);
61 self->string = rxpd_malloc (len + strlen(namestart+1) + 14);
62 strcpy (self->string, "#ERROR: ");
63 strcat (self->string, ebuf);
64 strcat (self->string, " in '");
65 strcat (self->string, namestart+1);
66 strcat (self->string, "'");
71 self->string = rxpd_strdup (buf);
77 rxpd_rule_copy (const struct rxpd_rule* src)
79 struct rxpd_rule* self = rxpd_malloc (sizeof (struct rxpd_rule));
81 llist_init (&self->node);
82 self->string = rxpd_strdup (src->string);
83 self->atime = src->atime;
85 if (*self->string != '#')
88 char* rxstart = strchr (self->string, ':');
90 // TODO regflags from base
91 err = regcomp (&self->rx, rxstart+1, REG_EXTENDED|REG_ICASE|REG_NOSUB);
93 rxpd_die ("unexpected regcomp error\n");
100 rxpd_rule_comment (struct rxpd_rule* self, const char* comment)
105 if (self->atime != -1)
106 len = snprintf (NULL, 0, "#%s: %ld:%s", comment, self->atime, self->string);
107 else if (self->string[0] != '#')
108 len = snprintf (NULL, 0, "#%s: :%s", comment, self->string);
110 len = snprintf (NULL, 0, "#%s: %s", comment, self->string);
115 char* dst = rxpd_malloc (len+1);
117 if (self->atime != -1)
118 snprintf (dst, len+1, "#%s: %ld:%s", comment, self->atime, self->string);
119 else if (self->string[0] != '#')
120 snprintf (dst, len+1, "#%s: :%s", comment, self->string);
122 snprintf (dst, len+1, "#%s: %s", comment, self->string);
124 if (self->string[0] != '#')
136 rxpd_rule_delete (struct rxpd_rule* rule)
140 llist_unlink (&rule->node);
141 if (rule->string[0] != '#')