Files
fwbuilder/src/ipfw/ipfw.cpp
T

212 lines
4.9 KiB
C++
Raw Normal View History

2007-12-25 22:25:59 +00:00
/*
Firewall Builder
Copyright (C) 2002 NetCitadel, LLC
Author: Vadim Kurland vadim@vk.crocodile.org
2008-07-05 17:40:12 +00:00
$Id$
2007-12-25 22:25:59 +00:00
This program is free software which we release under the GNU General Public
License. You may redistribute and/or modify this program under the terms
of that 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.
To get a copy of the GNU General Public License, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#include <fstream>
#include <algorithm>
#include <functional>
#include <iostream>
#include <stdexcept>
#ifndef _WIN32
# include <unistd.h>
# include <pwd.h>
#else
# include <direct.h>
# include <stdlib.h>
# include <io.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
2008-08-03 16:47:47 +00:00
#include <cstring>
2007-12-25 22:25:59 +00:00
#include <assert.h>
2019-07-27 17:30:21 +02:00
#include "version.h"
2009-09-23 17:00:48 +00:00
#include "CompilerDriver_ipfw.h"
2007-12-25 22:25:59 +00:00
#include "fwbuilder/Resources.h"
#include "fwbuilder/FWObjectDatabase.h"
#include "fwbuilder/FWException.h"
2009-09-23 17:00:48 +00:00
#include "fwbuilder/Cluster.h"
2007-12-25 22:25:59 +00:00
#include "fwbuilder/Firewall.h"
2011-01-26 13:27:57 -08:00
#include "fwbuilder/Constants.h"
2007-12-25 22:25:59 +00:00
#include <QCoreApplication>
2009-09-23 17:00:48 +00:00
#include <QStringList>
#include <QTextCodec>
2007-12-25 22:25:59 +00:00
#include "../common/init.cpp"
using namespace std;
using namespace libfwbuilder;
using namespace fwcompiler;
int fwbdebug = 0;
2018-10-04 16:20:02 +02:00
FWObjectDatabase *objdb = nullptr;
2007-12-25 22:25:59 +00:00
2007-12-25 22:25:59 +00:00
class UpgradePredicate: public XMLTools::UpgradePredicate
{
public:
virtual bool operator()(const string&) const
2007-12-25 22:25:59 +00:00
{
cout << "Data file has been created in the old version of Firewall Builder. Use fwbuilder GUI to convert it." << endl;
2007-12-25 22:25:59 +00:00
return false;
}
};
2007-12-25 22:25:59 +00:00
void usage(const char *name)
{
cout << "Firewall Builder: policy compiler for ipfw" << endl;
cout << "Version " << VERSION << endl;
cout << "Usage: " << name << " [-x] [-v] [-V] [-f filename.xml] [-o output.fw] [-d destdir] [-m] firewall_object_name" << endl;
2007-12-25 22:25:59 +00:00
}
2009-09-23 17:00:48 +00:00
int main(int argc, char **argv)
2007-12-25 22:25:59 +00:00
{
QCoreApplication app(argc, argv, false);
2007-12-25 22:25:59 +00:00
2009-09-23 17:00:48 +00:00
// compilers always write file names into manifest in Utf8
QTextCodec::setCodecForLocale(QTextCodec::codecForName("Utf8"));
2007-12-25 22:25:59 +00:00
2009-09-23 17:00:48 +00:00
QStringList args = app.arguments();
2007-12-25 22:25:59 +00:00
2009-09-23 17:00:48 +00:00
if (args.size()<=1)
2007-12-25 22:25:59 +00:00
{
usage(argv[0]);
exit(1);
}
2009-09-23 17:00:48 +00:00
QString last_arg;
string filename;
2007-12-25 22:25:59 +00:00
2009-09-23 17:00:48 +00:00
for (int idx=0; idx < args.size(); idx++)
2007-12-25 22:25:59 +00:00
{
2009-09-23 17:00:48 +00:00
QString arg = args.at(idx);
last_arg = arg;
if (arg == "-V")
2007-12-25 22:25:59 +00:00
{
usage(argv[0]);
2009-09-23 17:00:48 +00:00
exit(0);
}
if (arg == "-f")
{
idx++;
filename = string(args.at(idx).toLatin1().constData());
continue;
2007-12-25 22:25:59 +00:00
}
}
2009-09-23 17:00:48 +00:00
if (filename.empty())
2007-12-25 22:25:59 +00:00
{
usage(argv[0]);
exit(1);
}
init(argv);
try {
2011-01-26 13:27:57 -08:00
new Resources(Constants::getResourcesFilePath());
2007-12-25 22:25:59 +00:00
/* create database */
objdb = new FWObjectDatabase();
/* load the data file */
UpgradePredicate upgrade_predicate;
2009-09-23 17:00:48 +00:00
cout << " *** Loading data ...";
2007-12-25 22:25:59 +00:00
objdb->setReadOnly( false );
2011-01-26 13:27:57 -08:00
objdb->load( Constants::getStandardObjectsFilePath(),
&upgrade_predicate, Constants::getDTDDirectory());
2007-12-25 22:25:59 +00:00
objdb->setFileName("");
FWObjectDatabase *ndb = new FWObjectDatabase();
2011-01-26 13:27:57 -08:00
ndb->load(filename, &upgrade_predicate, Constants::getDTDDirectory());
2018-10-04 16:20:02 +02:00
objdb->merge(ndb, nullptr);
2007-12-25 22:25:59 +00:00
delete ndb;
objdb->setFileName(filename);
objdb->reIndex();
cout << " done\n";
2007-12-25 22:25:59 +00:00
FWObject *slib = objdb->getById(FWObjectDatabase::STANDARD_LIB_ID);
if (slib && slib->isReadOnly()) slib->setReadOnly(false);
2007-12-25 22:25:59 +00:00
CompilerDriver_ipfw *driver = new CompilerDriver_ipfw(objdb);
if (!driver->prepare(args))
{
2009-09-23 17:00:48 +00:00
usage(argv[0]);
exit(1);
}
driver->compile();
2013-06-26 15:41:53 +02:00
//int ret = (driver->getStatus() == BaseCompiler::FWCOMPILER_SUCCESS) ? 0 : 1;
int ret = driver->getStatus();
delete driver;
2009-09-23 17:00:48 +00:00
delete objdb;
return ret;
2007-12-25 22:25:59 +00:00
} catch(const FWException &ex)
{
2007-12-25 22:25:59 +00:00
cerr << ex.toString() << endl;
return 1;
#if __GNUC__ >= 3
/* need to check version because std::ios::failure does not seem to be
* supported in gcc 2.9.5 on FreeBSD 4.10 */
} catch (const std::ios::failure &e) {
cerr << "Error while opening or writing to the output file" << endl;
return 1;
#endif
} catch (const std::string &s) {
cerr << s;
return 1;
} catch (const std::exception &ex) {
cerr << ex.what();
return 1;
} catch (...) {
cerr << "Unsupported exception";
2007-12-25 22:25:59 +00:00
return 1;
}
}