-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup-DGS-1100.php
163 lines (144 loc) · 4.41 KB
/
backup-DGS-1100.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
<?php
define('DEFAULT_BACKUP_DIR', '/var/backups');
define('DEFAULT_CONFIG_FILE', '/etc/backup-DSG-1100.xml');
define('DEFAULT_LOG_FACILITY', LOG_USER);
define('DEFAULT_DEBUG', FALSE);
define('DEFAULT_OUTPUT_FILENAMES', false);
define('DEFAULT_SAVE_PASSWORD_IN_CONFIG_FILE', TRUE);
define('DEFAULT_BACKUP_FIRMWARE', FALSE);
/*
* Supporting functions
*/
/**
* Return value of a configuration parameter from config file
*
* @param DOMDocument $domObj
* @param string $setting
* @return mixed|NULL return value of xml setting. NULL in error
*/
function getConfigSetting($domObj, $setting)
{
$nodeList = $domObj->getElementsByTagName($setting);
if ((! is_object($nodeList)) || (get_class($nodeList) !== 'DOMNodeList')) {
return NULL;
}
switch ($nodeList->length) {
case 0:
syslog(LOG_DEBUG, "Config parameter $setting was not found");
return NULL;
break;
case 1:
$value = $nodeList->getIndex(0)->nodeValue;
syslog(LOG_DEBUG, "Returning $value for settting $setting");
return $value;
break;
default:
$msg = "Could not determine paramter $setting value. Possible syntax error in config file";
syslog(LOG_ERR, $msg);
fwrite(STDERR, PHP_EOL . $msg);
return NULL;
}
}
/**
* Setup base resources
*/
// openlog(pathinfo(__FILE__,PATHINFO_BASENAME), LOG_CONS, $facility);
/*
* Getting options from CLI
*/
$shortOpts = 'c:o';
$longOpts = array(
'config:',
'output'
);
$cliOptions = getopt($shortOpts, $longOpts);
if (! is_array($cliOptions)) {
$msg = 'Could not parse CLI options';
syslog(LOG_ERR, $msg);
fwrite(STDERR, PHP_EOL . $msg);
exit(2);
}
var_dump($cliOptions);
foreach ($cliOptions as $optKey => $optValue) {
switch ($optKey) {
case 'c':
case 'config':
$cliConfigFile = $optValue;
break;
case 'o':
case 'output':
$cliOutputEnabled = true;
break;
}
}
/**
* Loading configuration
*/
$configFile = isset($cliConfigFile) ? $cliConfigFile : DEFAULT_CONFIG_FILE;
if (! is_readable($configFile)) {
$msg = "$configFile does not exitst or is unreadable";
syslog(LOG_ERR, $msg);
fwrite(STDERR, PHP_EOL . $msg);
exit(2);
}
$domDocumentObj = new DOMDocument();
$cfgXMLStr = file_get_contents($configFile);
if (! is_string($cfgXMLStr)) {
$msg = "Failed to read $configFile file";
syslog(LOG_ERR, $msg);
fwrite(STDERR, $msg);
exit(2);
}
if (! $domDocumentObj->loadXML($cfgXMLStr)) {
$msg = "Failed to import XML to DOMDocument";
syslog(LOG_ERR, $msg);
fwrite(STDERR, $msg);
exit(2);
}
// Backup directory
$backupDir = (! is_null(getConfigSetting($domDocumentObj, 'backupDirectory'))) ? (string) getConfigSetting($domDocumentObj, 'backupDirectory') : DEFAULT_BACKUP_DIR;
// Syslog Facility
$logFacility = (! is_null(getConfigSetting($domDocumentObj, 'logFacility'))) ? (string) getConfigSetting($domDocumentObj, 'logFacility') : DEFAULT_LOG_FACILITY;
// Save Password in the backup
$savePasswordsInConfigurationBackup = (! is_null(getConfigSetting($domDocumentObj, 'savePasswordsInConfigurationBackup'))) ? (bool) getConfigSetting($domDocumentObj, 'savePasswordsInConfigurationBackup') : DEFAULT_SAVE_PASSWORD_IN_CONFIG_FILE;
// Firmware
$backupFirmware = (! is_null(getConfigSetting($domDocumentObj, 'backupFirmware'))) ? (bool) getConfigSetting($domDocumentObj, 'backupFirmware') : DEFAULT_BACKUP_FIRMWARE;
// IP address
$IPv4 = getConfigSetting($domDocumentObj, 'IPv4');
if (is_null($IPv4)) {
$msg = "Failed to get IPv4 parameter";
syslog(LOG_ERR, $msg);
fwrite(STDERR, PHP_EOL . $msg);
exit(2);
}
if (filter_var($IPv4, FILTER_VALIDATE_IP) === FALSE) {
$msg = "IPv4 parater is not valid IPv4 address";
syslog(LOG_ERR, $msg);
fwrite(STDERR, PHP_EOL . $msg);
exit(2);
}
// Switch password
$switchPassword = getConfigSetting($domDocumentObj, 'password');
if (is_null($switchPassword)) {
$msg = "Failed to get password parameter";
syslog(LOG_ERR, $msg);
fwrite(STDERR, PHP_EOL . $msg);
exit(2);
}
$switchPasswordMD5 = md5($switchPassword);
$cookieFile = tempnam(sys_get_temp_dir(), '');
$curlOptsCommon = array(
CURLOPT_COOKIESESSION => TRUE,
CURLOPT_COOKIEFILE => $cookieFile,
CURLOPT_COOKIEJAR => $cookieFile
);
$curlRes = curl_init();
$curlOpts = array(
CURLOPT_URL => "http://$IPv4/cgi/login.cgi",
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query(array(
'pass' => $switchPasswordMD5
))
);
curl_setopt_array($curlRes, ($curlOptsCommon + $curlOpts));
curl_exec($curlRes);