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 if (pth_init() == FALSE)
78 rxpd_die ("pth initialization failed\n");
80 struct rxpd_base* rxpd;
82 openlog (PACKAGE_NAME, LOG_PID, LOG_DAEMON);
86 rxpd_log (rxpd, LOG_NOTICE, PACKAGE_STRING" starting up\n");
88 // parse commandline args
93 while ((opt = getopt (argc, argv, "vVdDb:qt:u:p:i46rl:h")) != -1)
97 if (rxpd->verbosity < LOG_DEBUG)
107 if (rxpd->verbosity < LOG_INFO)
108 rxpd->verbosity = LOG_INFO;
113 rxpd->basedir = rxpd_strdup (optarg);
115 rxpd_die ("basedir already set\n");
118 rxpd->verbosity = LOG_ALERT;
122 int port = atoi (optarg);
123 if (port > 0 && port < 65536)
125 rxpd_socket_new_tcp4 (rxpd, NULL, port);
126 // TODO error handling
130 rxpd_log (rxpd, LOG_ALERT, "Illegal port number\n");
134 // TODO rxpd_socket_new_tcp6 (rxpd, NULL, 2374)
136 #if 0 /*not yet implemented*/
138 //rxpd_socket_new_unix (rxpd, NULL, 2374);
143 rxpd->policy = rxpd_file_new (rxpd, optarg);
145 rxpd_die ("policy already set\n");
148 rxpd->regflags |= REG_ICASE;
150 #if 0 /*not yet implemented*/
165 rxpd_log (rxpd, LOG_ALERT, "Unknown option\n");
171 rxpd_log (rxpd, LOG_ALERT, "Basedir not set (use -b BASEDIR)\n");
175 if (llist_is_empty (&rxpd->sockets))
177 rxpd_log (rxpd, LOG_ALERT, "No listening sockets (use -t TCP or -u UNIX)\n");
183 if (rxpd_file_load (rxpd->policy))
184 rxpd_log (rxpd, LOG_INFO, "Loaded policy '%s'\n", rxpd->policy->filename);
187 rxpd_log (rxpd, LOG_ALERT, "Failed loading policy file\n");
192 for (int i = optind; i < argc; ++i)
194 if (!rxpd_file_load (rxpd_file_new (rxpd, argv[i])))
196 //rxpd_log (rxpd, LOG_ALERT, "Failed loading file '%s'\n");
201 if (rxpd->daemonize && daemon(1, 0))
202 rxpd_die ("Couldn't daemonize\n");
204 LLIST_FOREACH (&rxpd->sockets, n)
206 struct rxpd_socket* socket = (struct rxpd_socket*)n;
207 rxpd_socket_spawn (socket);
210 LLIST_FOREACH (&rxpd->sockets, n)
212 struct rxpd_socket* socket = (struct rxpd_socket*)n;
213 rxpd_socket_delete (rxpd_socket_join (socket));
217 rxpd_log (rxpd, LOG_NOTICE, PACKAGE_STRING" exited\n");