1 /*
2 
3                           Firewall Builder
4 
5                  Copyright (C) 2004 NetCitadel, LLC
6 
7   Author:  Vadim Kurland     vadim@fwbuilder.org
8 
9   $Id$
10 
11   This program is free software which we release under the GNU General Public
12   License. You may redistribute and/or modify this program under the terms
13   of that license as published by the Free Software Foundation; either
14   version 2 of the License, or (at your option) any later version.
15 
16   This program is distributed in the hope that it will be useful,
17   but WITHOUT ANY WARRANTY; without even the implied warranty of
18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   GNU General Public License for more details.
20 
21   To get a copy of the GNU General Public License, write to the Free Software
22   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 
24 */
25 
26 
27 #ifndef __INSTDIALOG_H_
28 #define __INSTDIALOG_H_
29 
30 
31 #include "config.h"
32 
33 #include <ui_instdialog_q.h>
34 #include "instConf.h"
35 #include "fakeWizard.h"
36 
37 #include "instOptionsDialog.h"
38 #include "FirewallInstaller.h"
39 #include "ProjectPanel.h"
40 
41 #include <qstring.h>
42 #include <qstringlist.h>
43 #include <qprocess.h>
44 #include <QRegExp>
45 
46 #include <fstream>
47 #include <set>
48 #include <map>
49 #include <list>
50 
51 #include "fwbuilder/FWObjectDatabase.h"
52 #include "fwcompiler/BaseCompiler.h"
53 
54 class FirewallInstaller;
55 class instBatchOptionsDialog;
56 
57 class QEventLoop;
58 class QTextEdit;
59 class QListViewItem;
60 class QCheckListItem;
61 class QPushButton;
62 class QProgressBar;
63 class QStringList;
64 class QTreeWidgetItem;
65 class QTextCharFormat;
66 //class QCheckTableItem;
67 
68 namespace libfwbuilder
69 {
70     class Firewall;
71     class Cluster;
72 }
73 
74 enum BatchOperation {BATCH_INSTALL, BATCH_COMPILE} ;
75 enum Page1Operation {INST_DLG_COMPILE, INST_DLG_INSTALL, INST_DLG_INSPECT};
76 enum WizardPageNumber {CHOOSE_OBJECTS=0, COMPILE_INSTALL=1 };
77 
78 #define FIREWALL_NAME_COLUMN 0
79 #define COMPILE_CHECKBOX_COLUMN 1
80 #define INSTALL_CHECKBOX_COLUMN 2
81 #define LAST_MODIFIED_COLUMN 3
82 #define LAST_COMPILED_COLUMN 4
83 #define LAST_INSTALLED_COLUMN 5
84 
85 
86 class instDialog : public QDialog, public FakeWizard
87 {
88 
89     Q_OBJECT;
90 
91     Ui::instDialog_q *m_dialog;
92     instConf cnf;
93     Page1Operation page_1_op;
94     FirewallInstaller *installer;
95     ProjectPanel *project;
96     instOptionsDialog *inst_opt_dlg;
97     bool compile_only;
98     bool compile_complete;
99 
100     // proc is used to launch external oprocess, such as compiler or
101     // user-defined installer script
102     QProcess       proc;
103 
104     QString fwb_prompt;
105 
106     std::list<libfwbuilder::Firewall*> firewalls;
107     std::list<libfwbuilder::Cluster*> clusters;
108 
109     std::list<libfwbuilder::Firewall*> compile_fw_list;
110     std::list<libfwbuilder::Firewall*>::size_type compile_list_initial_size;
111     std::list<libfwbuilder::Firewall*> install_fw_list;
112     std::list<libfwbuilder::Firewall*>::size_type install_list_initial_size;
113 
114     std::map<int,QTreeWidgetItem*> opListMapping;
115     std::list<QRegExp> error_re;
116     std::list<QRegExp> warning_re;
117     std::map<libfwbuilder::Firewall*, fwcompiler::BaseCompiler::termination_status>
118         compile_status;
119 
120     QString path; //path of the program to execute
121 //    QStringList args; //arguments for that program
122 
123     bool creatingTable;
124 
125     BatchOperation operation;
126 
127     int processedRules;
128     int lastPage;
129     bool stopProcessFlag;
130     bool rejectDialogFlag;
131     bool compileFlag;
132     bool customScriptFlag;
133     bool showSelectedFlag;
134     bool finished;
135     bool onlySelected;
136     bool secondPageVisited;
137     bool canceledAll;
138     bool isAutoCompiling;
139 
140     QTextCharFormat normal_format;
141     QTextCharFormat error_format;
142     QTextCharFormat warning_format;
143     QTextCharFormat highlight_format;
144 
145     QPushButton *currentSaveButton;
146     QPushButton *currentStopButton;
147     QProgressBar *currentProgressBar;
148     QProgressBar *currentFirewallsBar;
149     QLabel *currentLabel;
150     QLabel *currentFWLabel;
151     QString currentSearchString;
152 
153 
154     void fillCompileSelectList();
155     void setSelectStateAll(int column, Qt::CheckState);
156 
157     void fillCompileOpList();
158     void fillCompileUIList();
159     void fillInstallOpList();
160     void fillInstallUIList();
161 
162     bool checkSSHPathConfiguration(libfwbuilder::Firewall*);
163 
164     //libfwbuilder::Firewall *findFirewallbyTableItem(QTableWidgetItem *item);
165 
166     void setSuccessState(QTreeWidgetItem *item);
167     void setWarningState(QTreeWidgetItem *item);
168     void setFailureState(QTreeWidgetItem *item);
169     void setErrorState(QTreeWidgetItem *item);
170     void setInProcessState(QTreeWidgetItem *item);
171 
172     bool checkIfNeedToCompile(libfwbuilder::Firewall *fw);
173     bool checkIfNeedToInstall(libfwbuilder::Firewall *fw);
174     QTreeWidgetItem *createTreeItem(QTreeWidgetItem* parent,
175                                     libfwbuilder::Firewall *fw);
176     void setFlags(QTreeWidgetItem* item);
177 
178     virtual void closeEvent(QCloseEvent *event);
179 
180     int findFilesToInspect(QStringList &files);
181 
182  public:
183 
184     instDialog(QWidget *p);
185     virtual ~instDialog();
186 
187     void summary();
188     void opSuccess(libfwbuilder::Firewall *fw);
189     void opWarning(libfwbuilder::Firewall *fw);
190     void opError(libfwbuilder::Firewall *fw);
191     void opCancelled(libfwbuilder::Firewall *fw);
192 
page(int n)193     QWidget* page(int n) { return m_dialog->stackedWidget->widget(n); }
194 
195     void displayCommand(const QStringList &args);
196     bool runCompiler(libfwbuilder::Firewall *fw);
197     bool runInstaller(libfwbuilder::Firewall *fw, bool installing_many_firewalls = true);
198 
199     QStringList prepareArgForCompiler(libfwbuilder::Firewall *fw);
200     bool tableHasCheckedItems();
201     QString getActivationCmd();
202 
203     void enableStopButton();
204     void disableStopButton();
205 
206     void setUpProcessToCompile();
207     void setUpProcessToInstall();
208     bool executeCommand(const QString &path, QStringList &args);
209 
isFinished()210     bool isFinished() { return finished; }
211 
212 protected:
213 
214     virtual void showEvent( QShowEvent *ev);
215     virtual void hideEvent( QHideEvent *ev);
216 
217     bool getInstOptions(libfwbuilder::Firewall *fw, bool installing_many_firewalls = true);
218 
219     void prepareInstConf(libfwbuilder::Firewall *fw);
220 
221     void blockInstallForFirewall(libfwbuilder::Firewall *fw);
222 
223     void readInstallerOptionsFromSettings();
224     void readInstallerOptionsFromFirewallObject(libfwbuilder::Firewall *fw);
225     void readInstallerOptionsFromDialog(libfwbuilder::Firewall *fw,
226                                                 instOptionsDialog *dlg);
227     bool verifyManagementAddress();
228 
229     void storeInstallerOptions();
230     void findFirewalls();
231 
232     bool isCiscoFamily();
233     bool isProcurve();
234 	bool isJuniper();
235 
236     void interpretLogLine(const QString &buf);
237 
238 public slots:
239 
240     void show(ProjectPanel *project, bool install, bool onlySelected, std::set<libfwbuilder::Firewall*> fws);
241     void autoCompile(ProjectPanel *project);
242 
243     void compilerFinished(int ret_code, QProcess::ExitStatus);
244     void installerFinished(int ret_code, QProcess::ExitStatus);
245     void installerSuccess();
246     void installerError();
247 
248     void showPage(const int page);
249 
250     void finishClicked();
251     void cancelClicked();
252 
253     void addToLog(const QString &buf);
254     void updateProgressBar(int n,bool setsize);
255 
256     void saveLog();
257     void inspectFiles();
258 
259     void readFromStdout();
260     void selectAllFirewalls();
261     void deselectAllFirewalls();
262 
263     void nextClicked();
264     void backClicked();
265 
266     void mainLoopCompile();
267     void mainLoopInstall();
268 
269     void stopCompile();
270     void stopInstall();
271 
272     void tableItemChanged(QTreeWidgetItem * item, int column);
273     void findFirewallInCompileLog(QTreeWidgetItem* item);
274 
275     void logItemClicked(QUrl);
276 
277 signals:
278     void activateRule(ProjectPanel*, QString, QString, int);
279     void currentFirewallsBarValueChanged(int);
280     void currentFirewallsBarMaximumValueChanged(int);
281     void autoCompileDone();
282 };
283 
284 
285 #endif
286