1 /*
2 
3                           Firewall Builder
4 
5                  Copyright (C) 2008 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 #include "config.h"
27 #include "global.h"
28 #include "utils.h"
29 #include "utils_no_qt.h"
30 
31 #include "CompilerDriver.h"
32 #include "FWBSettings.h"
33 #include "FWWindow.h"
34 #include "FirewallInstallerCisco.h"
35 #include "FirewallInstallerJuniper.h"
36 #include "FirewallInstallerProcurve.h"
37 #include "FirewallInstallerUnx.h"
38 #include "events.h"
39 #include "instBatchOptionsDialog.h"
40 #include "instDialog.h"
41 #include "instOptionsDialog.h"
42 
43 #include "fwbuilder/Resources.h"
44 #include "fwbuilder/FWObjectDatabase.h"
45 #include "fwbuilder/Firewall.h"
46 #include "fwbuilder/XMLTools.h"
47 #include "fwbuilder/Interface.h"
48 #include "fwbuilder/Management.h"
49 
50 #include <errno.h>
51 #include <iostream>
52 
53 #include <QTextCodec>
54 #include <QTimer>
55 #include <QMessageBox>
56 #include <QtDebug>
57 
58 
59 using namespace std;
60 using namespace libfwbuilder;
61 
runInstaller(Firewall * fw,bool installing_many_firewalls)62 bool instDialog::runInstaller(Firewall *fw, bool installing_many_firewalls)
63 {
64     cnf.fwobj = fw;
65     cnf.maddr = "";
66 
67     if (fwbdebug)
68         qDebug() << "instDialog::runInstaller: built-in installer"
69                  << fw->getName().c_str()
70                  << " cnf.user=" << cnf.user;
71 
72     if (!getInstOptions(fw, installing_many_firewalls))
73     {
74         QTimer::singleShot( 0, this, SLOT(mainLoopInstall()));
75         this->opCancelled(fw);
76         return false;
77     }
78 
79 //    args.clear();
80 
81 /* need to save settings so that if the user just changed ssh/scp, the
82  * wrapper will pick changes up
83  */
84     st->save();
85 
86     summary();
87 
88     if (checkSSHPathConfiguration(fw))
89     {
90         setTitle(
91             1,
92             QObject::tr("Installing policy rules on firewall '%1'").
93             arg(QString::fromUtf8(fw->getName().c_str() )));
94 
95         currentSearchString = tr("Install firewall: ");
96         currentFirewallsBar->setValue(install_list_initial_size -
97                                       install_fw_list.size());
98         currentProgressBar->reset();
99         currentProgressBar->setFormat("%v/%m");
100 
101         QTreeWidgetItem* item = opListMapping[fw->getId()];
102         assert(item!=NULL);
103 
104         currentFWLabel->setText(QString::fromUtf8(fw->getName().c_str()));
105         m_dialog->fwWorkList->scrollToItem(item);
106         setInProcessState(item);
107         item->setText(1, tr("Installing ..."));
108         currentLabel->setText(tr("Installing ..."));
109 
110         qApp->processEvents();
111 
112         addToLog("\n");
113 
114         if (fwbdebug)
115             qDebug() << "instDialog::runInstaller:" << " cnf.user=" << cnf.user;
116 
117         if (installer!=NULL)
118             delete installer;
119 
120         if (isCiscoFamily())
121             installer = new FirewallInstallerCisco(this, &cnf, fwb_prompt);
122         else if (isProcurve())
123                 installer = new FirewallInstallerProcurve(this, &cnf, fwb_prompt);
124 		else if (isJuniper())
125                 installer = new FirewallInstallerJuniper(this, &cnf, fwb_prompt);
126         else
127                 installer = new FirewallInstallerUnx(this, &cnf, fwb_prompt);
128 
129         if (!installer->packInstallJobsList(fw))
130         {
131             QTimer::singleShot( 0, this, SLOT(mainLoopInstall()));
132             return false;
133         }
134 
135         currentProgressBar->setValue(0);
136 
137         disconnect(currentStopButton, SIGNAL(clicked()));
138         connect(currentStopButton, SIGNAL(clicked()),
139                 this, SLOT(stopInstall()));
140 
141         installer->runJobs();
142     }
143     else
144     {
145         opListMapping[fw->getId()]->setText(1, tr("Failure"));
146         setFailureState(opListMapping[fw->getId()]);
147 
148         addToLog("Firewall policy installation failed\n");
149 
150         QTimer::singleShot( 0, this, SLOT(mainLoopInstall()));
151     }
152     return true;
153 }
154 
stopInstall()155 void instDialog::stopInstall()
156 {
157     currentStopButton->setEnabled(false);
158 
159     disconnect(currentStopButton, SIGNAL(clicked()));
160 
161     stopProcessFlag = true; // likely unused
162     proc.terminate(); // try to close proc.
163     QTimer::singleShot(1000, &proc, SLOT(kill())); //if it doesn't respond, kill it
164 
165     if (installer != NULL)
166     {
167         if (fwbdebug)
168             qDebug() << "instDialog::stopInstall  killing installer";
169         installer->terminate();
170         delete installer;
171         installer = NULL;
172     }
173 
174     // to terminate whole install sequence rather than just current
175     // process, clear the list.
176     for (list<Firewall*>::iterator i=install_fw_list.begin();
177          i!=install_fw_list.end(); ++i)
178         opCancelled(*i);
179 
180     install_fw_list.clear();
181     setFinishEnabled(currentPage(), true);
182 }
183 
installerFinished(int ret_code,QProcess::ExitStatus status)184 void instDialog::installerFinished(int ret_code, QProcess::ExitStatus status)
185 {
186     if( fwbdebug) qDebug("instDialog::installerFinished "
187                          "exit code = %d exit_status=%d",
188                          ret_code, status);
189     // run readFromStdout() and processEvents() to make sure all
190     // events that pass output from the external installer script have
191     // been processed. Otherwise the output from the next installer
192     // pass in batch install mixes with the tail of the output from
193     // the previous one.
194     readFromStdout();
195     qApp->processEvents();
196     if (ret_code==0 && status==QProcess::NormalExit)
197         installerSuccess();
198     else
199         installerError();
200 }
201 
installerSuccess()202 void instDialog::installerSuccess()
203 {
204     opSuccess(cnf.fwobj);
205 //    project->updateLastInstalledTimestamp(cnf.fwobj);
206     QCoreApplication::postEvent(
207         mw, new updateLastInstalledTimestampEvent(project->db()->getFileName().c_str(),
208                                                   cnf.fwobj->getId()));
209 
210     currentProgressBar->setValue(currentProgressBar->maximum());
211 
212     addToLog("Firewall policy successfully installed\n");
213 
214     QTimer::singleShot( 1000, this, SLOT(mainLoopInstall()));
215 }
216 
installerError()217 void instDialog::installerError()
218 {
219     opError(cnf.fwobj);
220     currentProgressBar->setValue(currentProgressBar->maximum());
221     opListMapping[cnf.fwobj->getId()]->setText(1, tr("Failure"));
222 
223     addToLog("Firewall policy installation failed\n");
224 
225     QTimer::singleShot( 0, this, SLOT(mainLoopInstall()));
226 }
227 
228