This repository has been archived by the owner on Jun 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
appid-enabler.php
342 lines (257 loc) · 9.11 KB
/
appid-enabler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/**
* Created by PhpStorm.
* User: swaschkut
* Date: 4/19/16
* Time: 9:12 AM
*/
print "\n***********************************************\n";
print "************ APP-ID ENABLE UTILITY ****************\n\n";
require_once("lib/panconfigurator.php");
function display_usage_and_exit($shortMessage = false)
{
global $argv;
print PH::boldText("USAGE: ")."php ".basename(__FILE__)." in=inputfile.xml location=vsys1 ".
"custom_url_category=test\n";
print "php ".basename(__FILE__)." help : more help messages\n";
if( !$shortMessage )
{
print PH::boldText("\nListing available arguments\n\n");
global $supportedArguments;
ksort($supportedArguments);
foreach( $supportedArguments as &$arg )
{
print " - ".PH::boldText($arg['niceName']);
if( isset( $arg['argDesc']))
print '='.$arg['argDesc'];
//."=";
if( isset($arg['shortHelp']))
print "\n ".$arg['shortHelp'];
print "\n\n";
}
print "\n\n";
}
exit(1);
}
function display_error_usage_exit($msg)
{
fwrite(STDERR, PH::boldText("\n**ERROR** ").$msg."\n\n");
display_usage_and_exit(true);
}
function disable_appid( $actions, $xmlDoc1, $configInput, $Connector)
{
$tmp_application_status = DH::findXPath( '/config/shared/application-status/entry', $xmlDoc1 );
if( $tmp_application_status->length == 0 )
{
//THIS is a NON available XPATH | TODO: swaschkut 20190115 validate it!!!
$tmp_application_status = DH::findXPath( '/config/devices/entry/vsys/entry/application-status/entry', $xmlDoc1 );
if( $tmp_application_status->length == 0 )
print PH::boldText( "\nNO disabled APP-IDs available\n" );
}
foreach( $tmp_application_status as $app_status )
{
print "----------------\n";
print "name: ".DH::findAttribute( 'name', $app_status)."\n";
if( $actions == 'display' )
print " - status: ".DH::findAttribute( 'status', $app_status);
elseif( $actions == 'enable' )
{
$apiArgs = Array();
$apiArgs['type'] = 'op';
$apiArgs['cmd'] = '<request><set-application-status-recursive><application>'.DH::findAttribute( 'name', $app_status).'</application><status>enabled</status></set-application-status-recursive></request>';
if( $configInput['type'] == 'api' )
{
$response = $Connector->sendRequest($apiArgs);
print " - status: enable";
}
else
derr( "this script is working only in API mode\n" );
}
print "\n";
}
}
print "\n";
$configType = null;
$configInput = null;
$configOutput = null;
$doActions = null;
$dryRun = false;
$objectslocation = 'shared';
$objectsFilter = null;
$errorMessage = '';
$debugAPI = false;
$supportedArguments = Array();
$supportedArguments['in'] = Array('niceName' => 'in', 'shortHelp' => 'input file or api. ie: in=config.xml or in=api://192.168.1.1 or in=api://0018CAEC3@panorama.company.com', 'argDesc' => '[filename]|[api://IP]|[api://serial@IP]');
$supportedArguments['debugapi'] = Array('niceName' => 'DebugAPI', 'shortHelp' => 'prints API calls when they happen');
$supportedArguments['help'] = Array('niceName' => 'help', 'shortHelp' => 'this message');
$supportedArguments['cycleconnectedfirewalls'] = Array('niceName' => 'cycleConnectedFirewalls', 'shortHelp' => 'a listing of all devices connected to Panorama will be collected through API then each firewall will be queried for overrides' );
$supportedArguments['actions'] = Array('niceName' => 'Actions', 'shortHelp' => 'action to apply on each disabled app-id. ie: actions=display / actions=enable', 'argDesc' => 'action:arg1[,arg2]' );
PH::processCliArgs();
foreach ( PH::$args as $index => &$arg )
{
if( !isset($supportedArguments[$index]) )
{
//var_dump($supportedArguments);
display_error_usage_exit("unsupported argument provided: '$index'");
}
}
if( isset(PH::$args['help']) )
{
display_usage_and_exit();
}
if( ! isset(PH::$args['in']) )
display_error_usage_exit('"in" is missing from arguments');
$configInput = PH::$args['in'];
if( !is_string($configInput) || strlen($configInput) < 1 )
display_error_usage_exit('"in" argument is not a valid string');
if(isset(PH::$args['out']) )
{
$configOutput = PH::$args['out'];
if (!is_string($configOutput) || strlen($configOutput) < 1)
display_error_usage_exit('"out" argument is not a valid string');
}
if( isset(PH::$args['debugapi']) )
{
$debugAPI = true;
}
if( isset(PH::$args['cycleconnectedfirewalls']) )
$cycleConnectedFirewalls = true;
else
$cycleConnectedFirewalls = false;
if( ! isset(PH::$args['actions']) || strtolower( PH::$args['actions'] ) == 'display')
$actions = 'display';
elseif( strtolower( PH::$args['actions'] ) == 'enable' )
$actions = 'enable';
################
//
// What kind of config input do we have.
// File or API ?
//
// <editor-fold desc=" **** input method validation and PANOS vs Panorama auto-detect ****" defaultstate="collapsed" >
$configInput = PH::processIOMethod($configInput, true);
$xmlDoc1 = null;
if( $configInput['status'] == 'fail' )
{
fwrite(STDERR, "\n\n**ERROR** " . $configInput['msg'] . "\n\n");exit(1);
}
if( $configInput['type'] == 'file' )
{
if( !file_exists($configInput['filename']) )
derr("file '{$configInput['filename']}' not found");
$xmlDoc1 = new DOMDocument();
if( ! $xmlDoc1->load($configInput['filename']) )
derr("error while reading xml config file");
}
elseif ( $configInput['type'] == 'api' )
{
if($debugAPI)
$configInput['connector']->setShowApiCalls(true);
print " - Downloading config from API... ";
if( isset(PH::$args['loadpanoramapushedconfig']) )
{
print " - 'loadPanoramaPushedConfig' was requested, downloading it through API...";
$xmlDoc1 = $configInput['connector']->getPanoramaPushedConfig();
}
else
{
$xmlDoc1 = $configInput['connector']->getCandidateConfig();
}
$hostname = $configInput['connector']->info_hostname;
/** @var PanAPIConnector $inputConnector */
$inputConnector = $configInput['connector'];
#$xmlDoc1->save( $offline_folder."/orig/".$hostname."_prod_new.xml" );
print "OK!\n";
}
else
derr('not supported yet');
//
// Determine if PANOS or Panorama
//
$xpathResult1 = DH::findXPath('/config/devices/entry/vsys', $xmlDoc1);
if( $xpathResult1 === FALSE )
derr('XPath error happened');
if( $xpathResult1->length <1 )
{
$xpathResult1 = DH::findXPath('/panorama', $xmlDoc1);
if( $xpathResult1->length <1 )
$configType = 'panorama';
else
$configType = 'pushed_panorama';
}
else
$configType = 'panos';
unset($xpathResult1);
print " - Detected platform type is '{$configType}'\n";
############## actual not used
if( $configType == 'panos' )
$pan = new PANConf();
elseif( $configType == 'panorama' )
$pan = new PanoramaConf();
if( $configInput['type'] == 'api' )
$pan->connector = $configInput['connector'];
// </editor-fold>
################
//
// Location provided in CLI ?
//
if( isset(PH::$args['location']) )
{
$objectslocation = PH::$args['location'];
if( !is_string($objectslocation) || strlen($objectslocation) < 1 )
display_error_usage_exit('"location" argument is not a valid string');
}
else
{
if( $configType == 'panos' )
{
print " - No 'location' provided so using default ='vsys1'\n";
$objectslocation = 'vsys1';
}
elseif( $configType == 'panorama' )
{
print " - No 'location' provided so using default ='shared'\n";
$objectslocation = 'shared';
}
elseif( $configType == 'pushed_panorama' )
{
print " - No 'location' provided so using default ='vsys1'\n";
$objectslocation = 'vsys1';
}
}
##########################################
##########################################
if($cycleConnectedFirewalls && $configType == 'panorama')
{
$firewallSerials = $inputConnector->panorama_getConnectedFirewallsSerials();
$countFW = 0;
foreach( $firewallSerials as $fw )
{
$countFW++;
print " ** Handling FW #{$countFW}/".count($firewallSerials)." : serial/{$fw['serial']} hostname/{$fw['hostname']} **\n";
$tmpConnector = $inputConnector->cloneForPanoramaManagedDevice($fw['serial']);
if($debugAPI)
$tmpConnector->setShowApiCalls(true);
$xmlDoc1 = $tmpConnector->getCandidateConfig();
##############
disable_appid( $actions, $xmlDoc1, $configInput, $tmpConnector );
}
}
else
{
$pan->load_from_domxml($xmlDoc1);
##############
disable_appid( $actions, $xmlDoc1, $configInput, $pan->connector );
}
##############################################
print "\n\n\n";
// save our work !!!
if( $configOutput !== null )
{
if( $configOutput != '/dev/null' )
{
$pan->save_to_file($configOutput);
}
}
print "\n\n************ END OF APP-ID ENABLE UTILITY ************\n";
print "**************************************************\n";
print "\n\n";