1 /*
2 
3                           Firewall Builder
4 
5                  Copyright (C) 2008 NetCitadel, LLC
6 
7   Author:  alek@codeminders.com
8 
9 
10                  Copyright (C) 2013 UNINETT AS
11 
12   Author:  Sirius Bakke <sirius.bakke@uninett.no>
13 
14   $Id$
15 
16   This program is free software which we release under the GNU General Public
17   License. You may redistribute and/or modify this program under the terms
18   of that license as published by the Free Software Foundation; either
19   version 2 of the License, or (at your option) any later version.
20 
21   This program is distributed in the hope that it will be useful,
22   but WITHOUT ANY WARRANTY; without even the implied warranty of
23   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24   GNU General Public License for more details.
25 
26   To get a copy of the GNU General Public License, write to the Free Software
27   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 
29 */
30 
31 #ifndef PROJECTPANEL_H
32 #define PROJECTPANEL_H
33 
34 #include "ui_projectpanel_q.h"
35 
36 #include <set>
37 #include <map>
38 
39 
40 
41 namespace libfwbuilder {
42     class FWObjectDatabase;
43     class Firewall;
44     class PolicyRule;
45     class RuleSet;
46     class Rule;
47     class RuleElement;
48     class FWObject;
49     class FWReference;
50 };
51 
52 class QWidget;
53 class QMdiSubWindow;
54 class QTextEdit;
55 class QUndoStack;
56 
57 class ObjectTreeView;
58 class ObjectManipulator;
59 class findDialog;
60 class FWWindow;
61 class RuleSetView;
62 class RCS;
63 
64 class FWBTree;
65 
66 #define DEFAULT_H_SPLITTER_POSITION 250
67 #define DEFAULT_V_SPLITTER_POSITION 450
68 
69 namespace DiffType {
70     enum Type {
71         None,
72         Add,
73         Remove,
74         Edit,
75         Move
76     };
77 }
78 
79 class ProjectPanel: public QWidget {
80     Q_OBJECT;
81 
82     FWWindow *mainW;
83     RCS *rcs;
84     bool systemFile;
85     bool safeMode;
86     bool editingStandardLib;
87     bool editingTemplateLib;
88     bool ruleSetRedrawPending;
89     bool ready;
90 
91     libfwbuilder::FWObjectDatabase *objdb, *origObjdb;
92 
93     findDialog *fd;
94 
95     QTimer *autosaveTimer;
96     std::map<libfwbuilder::FWObject*, RuleSetView*> ruleSetViews;
97     int ruleSetTabIndex;
98 
99     libfwbuilder::FWObject *visibleFirewall;
100     libfwbuilder::RuleSet *visibleRuleSet ;
101     std::vector<libfwbuilder::FWObject*> firewalls;
102 
103     int lastFirewallIdx;
104     bool changingTabs;
105     QString noFirewalls;
106     bool loading_state;
107 
108     // set of object IDs for objects that have been modified recently.
109     // Method updateLastModifiedTimestampForAllFirewalls() uses this
110     // to update lastModified timestamp of all firewalls using these objects.
111     std::set<int> lastModifiedTimestampChangePool;
112 
113     // IDs of objects that need to be updated in the tree. Method
114     // updateObjectInTree() processes them. The key in the map is object ID,
115     // value is a boolean flag, true means need to update the object and
116     // subtree under it.
117     std::map<int, bool> updateObjectsInTreePool;
118 
119     bool treeReloadPending;
120     QHash<int, int> m_diffLog;
121     QHash<QString, QString> m_renamedGroups;
122     QHash<QPair<int, DiffType::Type>, int> m_statistics;
123 
124 public:
125 
126     QMdiSubWindow *mdiWindow;
127     Ui::ProjectPanel_q *m_panel;
128     QSet<QString> copySet;
129 
130     QUndoStack *undoStack;
131 
readyStatus(bool f)132     void readyStatus(bool f) { ready=f; }
133 
getCurrentRuleSet()134     libfwbuilder::RuleSet* getCurrentRuleSet () {return visibleRuleSet;};
135     RuleSetView* getCurrentRuleSetView() ;
136 
137     void openRuleSet(libfwbuilder::FWObject *obj, bool immediately=false);
closeRuleSet(libfwbuilder::FWObject * obj)138     void closeRuleSet(libfwbuilder::FWObject *obj)
139     { if ((libfwbuilder::FWObject*)(visibleRuleSet)==obj) visibleRuleSet=NULL;};
140 
141     ProjectPanel(QWidget *parent);
142     ~ProjectPanel();
143 
144 
145     void initMain(FWWindow *main);
146     void reset();
147 
148     void loadObjects();
149     void loadObjects(libfwbuilder::FWObjectDatabase *db);
150     void clearObjects();
db()151     libfwbuilder::FWObjectDatabase* db() { return objdb; };
origDb()152     libfwbuilder::FWObjectDatabase* origDb() { return origObjdb; }
hasObject(libfwbuilder::FWObject * obj)153     bool hasObject(libfwbuilder::FWObject* obj)
154     { return objdb->findInIndex(obj->getId()); };
155 
156 
setDiffType(int id,enum DiffType::Type type)157     void setDiffType(int id, enum DiffType::Type type) { m_diffLog.insert(id, type); }
getDiffType(int id)158     int getDiffType(int id) { return m_diffLog.value(id, DiffType::None); }
getDiffLog()159     const QHash<int, int>& getDiffLog() { return m_diffLog; }
getRenamedGroups()160     QHash<QString, QString>& getRenamedGroups() { return m_renamedGroups; }
getStatistics()161     QHash<QPair<int, DiffType::Type>, int>& getStatistics() { return m_statistics; }
getStatistics(int ruleSetId,DiffType::Type diffType)162     int getStatistics(int ruleSetId, DiffType::Type diffType)
163     { return m_statistics.value(QPair<int, DiffType::Type>(ruleSetId, diffType), 0); }
164 
165     const QString getTemporaryDirPath() const;
166 
167     // libfwbuilder::RuleElement* getRE(libfwbuilder::Rule* r, int col );
168 
169     //wrapers for some ObjectManipulator functions
170 
171     libfwbuilder::FWObject*  getCurrentLib();
172 
173     libfwbuilder::FWObject* createObject(const QString &objType,
174                                          const QString &objName,
175                                          libfwbuilder::FWObject *copyFrom=NULL);
176 
177     libfwbuilder::FWObject* createObject(libfwbuilder::FWObject *parent,
178                                          const QString &objType,
179                                          const QString &objName,
180                                          libfwbuilder::FWObject *copyFrom=NULL);
181 
182     void updateObjectInTree(libfwbuilder::FWObject *obj, bool subtree=false);
183 
getWindow()184     FWWindow* getWindow (){ return mainW;}
185     void moveObject(libfwbuilder::FWObject *target,
186                     libfwbuilder::FWObject *obj);
187 
188     void moveObject(const QString &targetLibName,
189                     libfwbuilder::FWObject *obj);
190 
191     void registerModifiedObject(libfwbuilder::FWObject *o);
192     void registerObjectToUpdateInTree(libfwbuilder::FWObject *o, bool update_subtree);
193     void registerTreeReloadRequest();
194     void registerRuleSetRedrawRequest();
195 
196     libfwbuilder::FWObject* pasteTo(libfwbuilder::FWObject *target,
197                                     libfwbuilder::FWObject *obj);
198 
199     ObjectTreeView* getCurrentObjectTree();
200 
201     void findAllFirewalls(std::list<libfwbuilder::Firewall *> &fws);
202 
203     void showDeletedObjects(bool f);
204     void select();
205     void unselect();
206 
207     void copyObj();
208     bool isManipulatorSelected();
209     void cutObj();
210     void pasteObj();
211 
212     void deleteObj();
213     libfwbuilder::FWObject* getSelectedObject();
214     void reopenCurrentItemParent();
215 
216     void setManipulatorFocus();
217     void clearManipulatorFocus();
218 
219     //find dialog functions wrapers
220     void setFDObject(libfwbuilder::FWObject *o);
221 
222     void resetFD();
223 
224     void clearFirewallTabs();
225     void closeRuleSetPanel();
226     void ensureObjectVisibleInRules(libfwbuilder::FWReference *obj);
227 
getVisibleFirewall()228     libfwbuilder::FWObject* getVisibleFirewall() { return visibleFirewall; }
getRuleSetViews(libfwbuilder::FWObject * o)229     RuleSetView* getRuleSetViews(libfwbuilder::FWObject *o)
230     {return ruleSetViews[o];};
231 
232     int  findFirewallInList(libfwbuilder::FWObject *f);
233     void updateFirewallName();
234     void selectRules();
235     void unselectRules();
236     void editCopy();
237     void editCut();
238     void editDelete();
239     void editPaste();
240 
241     bool saveIfModified(bool include_discard_button=true);
242 
243     QString chooseNewFileName(const QString &fname, const QString &title);
244     void setFileName(const QString &fname);
245     void restoreDepends(libfwbuilder::FWObject *obj_old,
246                         libfwbuilder::FWObject *nobj,
247                         const std::map<int,
248                         libfwbuilder::FWObject *> &objByIds);
249 
250     QString getPageTitle(bool file_path=false);
251 
252     void setActive();
253 
254  private:
255 
256  public slots:
257     void newObject();
258 
259     virtual void lockObject();
260     virtual void unlockObject();
261 
262     virtual void insertRule();
263     virtual void addRuleAfterCurrent();
264     virtual void moveRule();
265     virtual void moveRuleUp();
266     virtual void moveRuleDown();
267     virtual void removeRule();
268 
269     virtual void copyRule();
270     virtual void cutRule();
271     virtual void pasteRuleAbove();
272     virtual void pasteRuleBelow();
273 
274     virtual void reopenFirewall();
275     virtual void redrawRuleSets();
276     virtual void restoreRuleSetTab();
277 
278     virtual void fileProp();
279     virtual bool fileNew();
280 
281     virtual void fileClose();
282     virtual void fileSave();
283     virtual void fileSaveAs();
284     virtual void fileCommit();
285     virtual void fileDiscard();
286     virtual void fileAddToRCS();
287 
288     virtual void fileImport();
289     virtual void fileCompare();
290     virtual void fileExport();
291 
292     virtual void compile(std::set<libfwbuilder::Firewall*> vf);
293     virtual void compile();
294     virtual void install(std::set<libfwbuilder::Firewall*> vf);
295     virtual void install();
296     virtual void inspect(std::set<libfwbuilder::Firewall*> vf);
297 
298     void splitterMoved ( int pos, int index );
299 
300     virtual void autoSave();
301     virtual void compileThis();
302     virtual void installThis();
303     virtual void inspectThis();
304     virtual void inspectAll();
305     virtual void addRule();
306     virtual void diffThis();
307 
308     void updateLastModifiedTimestampForAllFirewalls();
309     void updateObjectInTree();
310     void reloadTree();
311 
312     void aboutToActivate();
313 
314     void splitterPositionChanged(int, int);
315 
316 public:
317     QString getFileName();
318     bool editingLibrary();
319     void createRCS( const QString &filename);
320     bool loadFromRCS(RCS *rcs);
321     void loadStandardObjects();
322     bool loadFile(const QString &fileName, bool load_rcs_head);
323 
324     void save();
325 
326     void saveState();
327     void loadState(bool open_objects=true);
328 
329     void saveMainSplitter();
330     void loadMainSplitter();
331 
332     void loadOpenedRuleSet();
333     void saveOpenedRuleSet();
334 
335     void saveLastOpenedLib();
336     void loadLastOpenedLib();
337     void loadFirstNonStandardLib();
338 
339     bool checkin(bool unlock);
340     libfwbuilder::FWObject* loadLibrary(const std::string &libfpath);
341 
342     bool exportLibraryTest(std::list<libfwbuilder::FWObject*> &selectedLibs);
343     void exportLibraryTo(QString fname,
344                          std::list<libfwbuilder::FWObject*> &selectedLibs,
345                          bool rof);
346 
347     void findExternalRefs(libfwbuilder::FWObject *lib,
348                        libfwbuilder::FWObject *root,
349                        std::list<libfwbuilder::FWReference*> &extRefs);
350 
setSafeMode(bool f)351     void setSafeMode(bool f) { safeMode=f; }
352     void setupAutoSave();
353     RCS * getRCS();
354     QString printHeader();
355 
356     void toggleViewTree(bool f);
357 
358 protected:
359     int oldState ;
360     virtual void showEvent(QShowEvent *ev);
361     virtual void hideEvent(QHideEvent *ev);
362     virtual void closeEvent(QCloseEvent *ev);
363     virtual void resizeEvent(QResizeEvent *ev);
364     virtual bool event(QEvent *ev);
365 
366     void setMainSplitterPosition(int w1, int w2);
367     void collapseTree();
368     void collapseRules();
369 };
370 
371 #endif
372