From e97f326f7b975968e2a79cad09f8ece468957e7b Mon Sep 17 00:00:00 2001 From: Vadim Kurland Date: Wed, 9 Dec 2009 00:59:54 +0000 Subject: [PATCH] fixes #202 implemented library merge function in fwbedit --- build_num | 2 +- doc/ChangeLog | 7 ++ doc/fwbedit.1 | 8 ++ src/fwbedit/fwbedit.cpp | 81 +++++++++++++-------- src/fwbedit/fwbedit.h | 6 +- src/fwbedit/fwbedit.pro | 4 +- src/fwbedit/merge.cpp | 78 ++++++++++++++++++++ src/fwbedit/upgradePredicate.h | 54 ++++++++++++++ src/res/help/en_US/release_notes_3.1.0.html | 10 +++ 9 files changed, 214 insertions(+), 36 deletions(-) create mode 100644 src/fwbedit/merge.cpp create mode 100644 src/fwbedit/upgradePredicate.h diff --git a/build_num b/build_num index 6b235718a..0a0440349 100644 --- a/build_num +++ b/build_num @@ -1 +1 @@ -#define BUILD_NUM 2061 +#define BUILD_NUM 2063 diff --git a/doc/ChangeLog b/doc/ChangeLog index dfe376509..f018bf728 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,10 @@ +2009-12-08 vadim + + * ../src/fwbedit/merge.cpp: fixed bug #2794851 (fwbuilder bug + #202): "Ability to import Library using fwbedit". User can now + merge objects from two files together using fwbedit just like the + "Import library" function in the GUI. + 2009-12-07 vadim * instDialog_ui_ops.cpp (instDialog::getInstOptions): fixed bug diff --git a/doc/fwbedit.1 b/doc/fwbedit.1 index 35ed2c379..6e21e4f7e 100644 --- a/doc/fwbedit.1 +++ b/doc/fwbedit.1 @@ -155,6 +155,14 @@ data file and repairs it if necessary. -f file.fwb: data file +.B merge -f file1.fwb -i file2.fwb + +Objects from the file2.fwb are merged with objects in file1 and +combined object tree saved in file1.fwb + + -f file.fwb: data file #1 + -i file.fwb: data file #2 + .SH ATTRIBUTES FOR THE NEW OBJECTS, BY TYPE .PP diff --git a/src/fwbedit/fwbedit.cpp b/src/fwbedit/fwbedit.cpp index 229ff8da1..ca114e962 100644 --- a/src/fwbedit/fwbedit.cpp +++ b/src/fwbedit/fwbedit.cpp @@ -98,6 +98,7 @@ extern int errno; #include "../common/init.cpp" #include "fwbedit.h" +#include "upgradePredicate.h" using namespace libfwbuilder; using namespace std; @@ -107,6 +108,8 @@ command cmd = NONE; bool autoupgrade_flag = false; string filename = ""; +string filemerge = ""; +int conflict_res = 1; vector platforms; @@ -114,28 +117,6 @@ FWObjectDatabase *objdb = NULL; int fwbdebug = 0; -class UpgradePredicate: public XMLTools::UpgradePredicate -{ - public: - virtual bool operator()(const string&) const - { - bool res=false; - cout << "Data file has been created in the old version of Firewall Builder." << endl << flush; - if (autoupgrade_flag) - { - cout << "Do you want to convert it? (Y/n)" << endl; - int a = getchar(); - if (a=='y' || a=='Y' || a=='\n' ) res= true; - } - else - { - cout << "Use option '-u' to upgrade the file. Alternatively,\nfwbuilder GUI can convert it." << endl; - } - if (res) cout << "Upgrading the file now ..." << endl; - return res; - } -}; - void usage() { cout << "Firewall Builder: general purpose object tree editing tool" @@ -153,8 +134,8 @@ void usage() cout << " add add object to a group" << endl; cout << " remove remove object from a group" << endl; cout << " upgrade upgrade data file" << endl; - cout << " checktree check object tree and repair if necessary" - << endl; + cout << " checktree check object tree and repair if necessary" << endl; + cout << " merge merge one data file into another" << endl; cout << endl; cout << "Options:" << endl; @@ -221,17 +202,30 @@ void usage() cout << endl; cout << - " upgrade -f file.fwb\n" + " upgrade -f file.fwb\n" "\n" " -f file.fwb: data file\n"; cout << endl; cout << - "checktree -f file.fwb\n" + " checktree -f file.fwb\n" "\n" " -f file.fwb: data file\n"; cout << endl; + cout << + " merge -f file1.fwb -i file2.fwb -c[1|2]\n" + "\n" + " -f file1.fwb: data file #1\n" + " -i file2.fwb: data file #2\n" + " -cN in case of conflict (the same object is found in both files),\n" + " keep the object from the file N (can be '1' or '2').\n" + " Default is '1'.\n" + " Objects from the file2.fwb are merged with objects in file1\n" + " and combined object tree saved in file1.fwb\n"; + + cout << endl; + cout << "Attributes for the new objects, by type:" << endl; cout << endl; cout << " " @@ -410,6 +404,7 @@ int main(int argc, char * const *argv) if (cmd_str=="list") cmd = LIST; if (cmd_str=="upgrade") cmd = UPGRADE; if (cmd_str=="checktree") cmd = STRUCT; + if (cmd_str=="merge") cmd = MERGE; char * const *args = argv; args++; @@ -536,6 +531,19 @@ int main(int argc, char * const *argv) } break; + case MERGE: + // -f file1.fwb -i file2.fwb + while( (opt=getopt(argc, args, "f:i:c:")) != EOF ) + { + switch(opt) + { + case 'f': filename = optarg; break; + case 'i': filemerge = optarg; break; + case 'c': conflict_res = atoi(optarg); break; + } + } + break; + case NONE: break; } @@ -558,11 +566,20 @@ int main(int argc, char * const *argv) objdb = new FWObjectDatabase(); /* load the data file */ - UpgradePredicate upgrade_predicate; + UpgradePredicate upgrade_predicate(autoupgrade_flag); objdb->load(filename, &upgrade_predicate, librespath); - if (cmd == STRUCT) + if (cmd == MERGE) + { + if (filemerge.empty()) + { + usage(); + exit(1); + } + mergeTree(objdb, filemerge, conflict_res); + } + else if (cmd == STRUCT) { checkAndRepairTree(objdb); } @@ -649,16 +666,16 @@ int main(int argc, char * const *argv) } } catch(FWException &ex) { - cerr << ex.toString() << endl; + cerr << ex.toString() << endl; exit(1); } catch (std::string s) { - cerr << s; + cerr << s; exit(1); } catch (std::exception ex) { - cerr << ex.what(); + cerr << ex.what(); exit(1); } catch (...) { - cerr << "Unsupported exception"; + cerr << "Unsupported exception"; exit(1); } diff --git a/src/fwbedit/fwbedit.h b/src/fwbedit/fwbedit.h index 5a44cc696..9595c9855 100644 --- a/src/fwbedit/fwbedit.h +++ b/src/fwbedit/fwbedit.h @@ -37,7 +37,7 @@ // can't use 'DELETE' in this enum because it is degined somewhere on windows typedef enum { NONE, ADDGRP, REMGRP, DELOBJECT, NEWOBJECT, MODOBJECT, - LIST, STRUCT, UPGRADE} command; + LIST, STRUCT, UPGRADE, MERGE} command; class OperandsError : public std::exception {}; @@ -69,6 +69,9 @@ extern void modObject(libfwbuilder::FWObjectDatabase *objdb, extern void checkAndRepairTree(libfwbuilder::FWObjectDatabase *objdb); +extern void mergeTree(libfwbuilder::FWObjectDatabase *objdb, + const std::string &mergefile, int conflict_res); + extern int splitStr(char ch,std::string s, operands * ops); extern std::string getNextOpt(operands &ops); extern std::string fixPath(const std::string &obj_path); @@ -80,4 +83,5 @@ extern void findObjects(const std::string &obj_path, extern bool getBool(std::string s); extern void usage(); +extern std::string librespath; diff --git a/src/fwbedit/fwbedit.pro b/src/fwbedit/fwbedit.pro index 8e7b82be7..88d1aaa70 100644 --- a/src/fwbedit/fwbedit.pro +++ b/src/fwbedit/fwbedit.pro @@ -6,8 +6,8 @@ include(../../qmake.inc) TEMPLATE = app -SOURCES = fwbedit.cpp new_object.cpp repair_tree.cpp list_object.cpp -HEADERS = ../../config.h fwbedit.h +SOURCES = fwbedit.cpp new_object.cpp repair_tree.cpp list_object.cpp merge.cpp +HEADERS = ../../config.h fwbedit.h upgradePredicate.h TARGET = fwbedit diff --git a/src/fwbedit/merge.cpp b/src/fwbedit/merge.cpp new file mode 100644 index 000000000..180bf70a2 --- /dev/null +++ b/src/fwbedit/merge.cpp @@ -0,0 +1,78 @@ +/* + + Firewall Builder + + Copyright (C) 2003 NetCitadel, LLC + + Author: Vadim Kurland vadim@fwbuilder.org + + $Id$ + + 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 + +*/ + +#include "../../config.h" +#include "fwbuilder/libfwbuilder-config.h" +#include "fwbuilder/Constants.h" +#include "fwbuilder/ObjectGroup.h" +#include "fwbuilder/ServiceGroup.h" +#include "fwbuilder/IntervalGroup.h" + +#include "fwbedit.h" +#include "upgradePredicate.h" + +#include + +using namespace libfwbuilder; +using namespace std; + + +class MergeConflictRes : + public libfwbuilder::FWObjectDatabase::ConflictResolutionPredicate +{ + int conflict_res; + public: + MergeConflictRes(int cr) { conflict_res = cr; } + virtual bool askUser(libfwbuilder::FWObject*, libfwbuilder::FWObject*) + { + return (conflict_res == 1); + } +}; + + +void mergeTree(FWObjectDatabase *objdb, const string &mergefile, int conflict_res) +{ + cout << "Merge objects from file " << mergefile << endl; + + UpgradePredicate upgrade_predicate(false); + + try + { + FWObjectDatabase *ndb = new FWObjectDatabase(); + ndb->load(mergefile, &upgrade_predicate, librespath); + + FWObject *dobj = ndb->findInIndex(FWObjectDatabase::DELETED_OBJECTS_ID); + if (dobj) ndb->remove(dobj, false); + + MergeConflictRes mcr(conflict_res); + objdb->merge(ndb, &mcr); + delete ndb; + + } catch(FWException &ex) + { + cerr << ex.toString() << endl; + } +} + diff --git a/src/fwbedit/upgradePredicate.h b/src/fwbedit/upgradePredicate.h new file mode 100644 index 000000000..6bda0099d --- /dev/null +++ b/src/fwbedit/upgradePredicate.h @@ -0,0 +1,54 @@ +/* + + Firewall Builder + + Copyright (C) 2003-2009 NetCitadel, LLC + + Author: Vadim Kurland vadim@fwbuilder.org + + $Id$ + + 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 + +*/ + +#include "fwbuilder/XMLTools.h" + +class UpgradePredicate: public libfwbuilder::XMLTools::UpgradePredicate +{ + bool autoupgrade_flag; + +public: + UpgradePredicate(bool autoupgrade) { autoupgrade_flag = autoupgrade; } + virtual bool operator()(const std::string&) const + { + bool res=false; + std::cout << "Data file has been created in the old version of Firewall Builder." + << std::endl << std::flush; + if (autoupgrade_flag) + { + std::cout << "Do you want to convert it? (Y/n)" << std::endl; + int a = getchar(); + if (a=='y' || a=='Y' || a=='\n' ) res= true; + } + else + { + std::cout << "Use option '-u' to upgrade the file in fwbedit." + " Alternatively, fwbuilder GUI can convert it." << std::endl; + } + if (res) std::cout << "Upgrading the file now ..." << std::endl; + return res; + } +}; + diff --git a/src/res/help/en_US/release_notes_3.1.0.html b/src/res/help/en_US/release_notes_3.1.0.html index c33de9d8a..0281c110f 100644 --- a/src/res/help/en_US/release_notes_3.1.0.html +++ b/src/res/help/en_US/release_notes_3.1.0.html @@ -490,3 +490,13 @@ rule sets of this object rather than in the actual firewalls.

Added support for option "--random" in SNAT rules

+ + + +

Changes in the command linbe tool fwbedit

+ +

+ User can now merge objects from two data files together using + command line tool fwbedit just like the "Import library" + function in the GUI. +