2 main.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.
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
37 " Regex Policy Daemon ("PACKAGE_STRING")\n\n"
39 " 2007, Christian Thaeter <ct@pipapo.org>\n\n"
40 " This is free software. You may redistribute copies of it under the terms of\n"
41 " the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n"
42 " There is NO WARRANTY, to the extent permitted by law.\n\n"
43 " http://www.pipapo.org/pipawiki/RegexPolicyDaemon\n"
51 rxpd [OPTIONS] RULES..
56 -b dir basedir for rules
60 -p policy access policies
65 -l log log hits to logfile
68 -U user switch to user
75 main (int argc, char** argv)
77 struct rxpd_base* rxpd;
79 openlog (PACKAGE_NAME, LOG_PID, LOG_DAEMON);
81 rxpd = rxpd_init (event_init ());
83 rxpd_log (rxpd, LOG_NOTICE, PACKAGE_STRING" starting up\n");
85 // parse commandline args
90 while ((opt = getopt (argc, argv, "vVdDb:qt:u:p:i46rl:h")) != -1)
94 if (rxpd->verbosity < LOG_DEBUG)
104 if (rxpd->verbosity < LOG_INFO)
105 rxpd->verbosity = LOG_INFO;
110 rxpd->basedir = rxpd_strdup (optarg);
112 rxpd_die ("basedir already set\n");
115 rxpd->verbosity = LOG_ALERT;
119 int port = atoi (optarg);
120 if (port > 0 && port < 65536)
122 rxpd_socket_new_tcp4 (rxpd, NULL, port);
123 // TODO error handling
127 rxpd_log (rxpd, LOG_ALERT, "Illegal port number\n");
131 // TODO rxpd_socket_new_tcp6 (rxpd, NULL, 2374)
133 #if 0 /*not yet implemented*/
135 //rxpd_socket_new_unix (rxpd, NULL, 2374);
140 rxpd->policy = rxpd_file_new (rxpd, optarg);
142 rxpd_die ("policy already set\n");
145 rxpd->regflags |= REG_ICASE;
147 #if 0 /*not yet implemented*/
162 rxpd_log (rxpd, LOG_ALERT, "Unknown option\n");
168 rxpd_log (rxpd, LOG_ALERT, "Basedir not set (use -b BASEDIR)\n");
172 if (llist_is_empty (&rxpd->sockets))
174 rxpd_log (rxpd, LOG_ALERT, "No listening sockets (use -t TCP or -u UNIX)\n");
180 if (rxpd_file_load (rxpd->policy))
181 rxpd_log (rxpd, LOG_INFO, "Loaded policy '%s'\n", rxpd->policy->filename);
184 rxpd_log (rxpd, LOG_ALERT, "Failed loading policy file\n");
189 for (int i = optind; i < argc; ++i)
191 if (!rxpd_file_load (rxpd_file_new (rxpd, argv[i])))
193 //rxpd_log (rxpd, LOG_ALERT, "Failed loading file '%s'\n");
198 if (rxpd->daemonize && daemon(1, 0))
199 rxpd_die ("Couldn't daemonize\n");
201 LLIST_FOREACH (&rxpd->sockets, n)
203 struct rxpd_socket* socket = (struct rxpd_socket*)n;
204 rxpd_socket_schedule (socket);
210 rxpd_log (rxpd, LOG_NOTICE, PACKAGE_STRING" exited\n");