Project

General

Profile

Download (7.27 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/*
4
	$Id$
5
	
6
        xmlrpc.php
7
        Copyright (C) 2005 Colin Smith
8
        All rights reserved.
9

    
10
        Redistribution and use in source and binary forms, with or without
11
        modification, are permitted provided that the following conditions are met:
12

    
13
        1. Redistributions of source code must retain the above copyright notice,
14
           this list of conditions and the following disclaimer.
15

    
16
        2. Redistributions in binary form must reproduce the above copyright
17
           notice, this list of conditions and the following disclaimer in the
18
           documentation and/or other materials provided with the distribution.
19

    
20
        THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
        INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
        AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
        AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
        OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
        SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
        INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
        CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
        ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
        POSSIBILITY OF SUCH DAMAGE._Value(
30

    
31
	TODO:
32
		* Expose more functions.
33
*/
34

    
35
require_once("xmlrpc_server.inc");
36
require_once("xmlrpc.inc");
37
require_once("config.inc");
38
require_once("functions.inc");
39
require_once("array_intersect_key.inc");
40

    
41
$xmlrpc_g = array(
42
			"return" => array(
43
						"true" => new XML_RPC_Response(new XML_RPC_Value(true, $XML_RPC_Boolean)),
44
						"false" => new XML_RPC_Response(new XML_RPC_Value(false, $XML_RPC_Boolean)),
45
						"authfail" => new XML_RPC_Response(0, $XML_RPC_erruser+1, "Authentication failure")
46
				)
47
		);
48

    
49
/*
50
 *   pfSense XMLRPC errors
51
 *   $XML_RPC_erruser + 1 = Auth failure
52
 */
53
$XML_RPC_erruser = 200;
54

    
55
/* EXPOSED FUNCTIONS */
56

    
57
$backup_config_section_doc = 'XMLRPC wrapper for backup_config_section. This method must be called with two parameters: a string containing the local system\'s password followed by an array containing the keys to be backed up.';
58
$backup_config_section_sig = array(
59
				array(
60
					$XML_RPC_Struct, // First signature element is return value.
61
					$XML_RPC_String,
62
					$XML_RPC_Array
63
				)
64
			);
65

    
66
function backup_config_section_xmlrpc($raw_params) {
67
	global $config, $xmlrpc_g;
68
	$params = xmlrpc_params_to_php($raw_params);
69
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
70
	$val = array_intersect_key($config, array_flip($params[0]));
71
	return new XML_RPC_Response(XML_RPC_encode($val));
72
}
73

    
74
/*****************************/
75

    
76
$restore_config_section_doc = 'XMLRPC wrapper for restore_config_section. This method must be called with two parameters: a string containing the local system\'s password and an array to merge into the system\'s config. This function returns true upon completion.';
77
$restore_config_section_sig = array(
78
					array(
79
						$XML_RPC_Boolean,
80
						$XML_RPC_String,
81
						$XML_RPC_Struct
82
					)
83
				);
84

    
85
function restore_config_section_xmlrpc($raw_params) {
86
	global $config, $xmlrpc_g;
87
	$params = xmlrpc_params_to_php($raw_params);
88
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
89
	$config = array_merge($config, $params[0]);
90
	$mergedkeys = implode(",", array_keys($params[0]));
91
	write_config("Merged in config ({$mergedkeys} sections) from XMLRPC client.");
92
	return $xmlrpc_g['return']['true'];
93
}
94

    
95
/*****************************/
96

    
97
$filter_configure_doc = 'Basic XMLRPC wrapper for filter_configure. This method must be called with one paramater: a string containing the local system\'s password. This function returns true upon completion.';
98
$filter_configure_sig = array(
99
				array(
100
					$XML_RPC_Boolean,
101
					$XML_RPC_String
102
				)
103
			);
104

    
105
function filter_configure_xmlrpc($raw_params) {
106
	global $xmlrpc_g;
107
	$params = xmlrpc_params_to_php($raw_params);
108
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
109
	filter_configure();
110
	return $xmlrpc_g['return']['true'];
111
}
112

    
113
/*****************************/
114

    
115
$check_firmware_version_doc = 'Basic XMLRPC wrapper for filter_configure. This function will return the output of check_firmware_version upon completion.';
116
$check_firmware_version_sig = array(
117
					array(
118
						$XML_RPC_String,
119
						$XML_RPC_String
120
					)
121
				);
122

    
123
function check_firmware_version_xmlrpc($raw_params) {
124
	return new XML_RPC_Response(new XML_RPC_Value(check_firmware_version(false), $XML_RPC_String));
125
}
126

    
127
/*****************************/
128

    
129
$reboot_doc = 'Basic XMLRPC wrapper for rc.reboot.';
130
$reboot_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
131

    
132
function reboot_xmlrpc($raw_params) {
133
	global $xmlrpc_g;
134
	$params = xmlrpc_params_to_php($raw_params);
135
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
136
	mwexec_bg("/etc/rc.reboot");
137
	return $xmlrpc_g['return']['true'];
138
}
139

    
140
/*****************************/
141

    
142
$get_notices_sig = array(
143
				array(
144
					$XML_RPC_Array, 
145
					$XML_RPC_String
146
				),
147
				array(
148
					$XML_RPC_Array
149
				)
150
			);
151

    
152
function get_notices_xmlrpc($raw_params) {
153
	global $g, $xmlrpc_g;
154
	require_once("notices.inc");
155
	$params = array_pop(xmlrpc_params_to_php($raw_params));
156
	if(!$params) {
157
		$toreturn = get_notices();
158
	} else {
159
		$toreturn = get_notices($params);
160
	}
161
	$response = new XML_RPC_Response(XML_RPC_encode($toreturn));
162
	return $response;
163
}
164

    
165
/*****************************/
166

    
167
$carp_configure_doc = 'Basic XMLRPC wrapper for configuring CARP interfaces.';
168
$carp_configure_sig = array(
169
				array(
170
					$XML_RPC_Boolean,
171
					$XML_RPC_String
172
				)
173
			);
174
			
175
function interfaces_carp_configure_xmlrpc($raw_params) {
176
	global $xmlrpc_g;
177
	$params = xmlrpc_params_to_php($raw_params);
178
	if(!xmlrpc_auth($params)) return new XML_RPC_Response(new XML_RPC_Value("auth_failure", $XML_RPC_String));
179
	interfaces_carp_configure();
180
	interfaces_carp_bring_up_final();
181
	return new XML_RPC_Response(new XML_RPC_Value(true, $XML_RPC_Boolean));
182
}
183

    
184
/*****************************/
185

    
186
$server = new XML_RPC_Server(
187
        array(
188
            'pfsense.interfaces_carp_configure' => array('function' => 'interfaces_carp_configure_xmlrpc',
189
							'signature' => $carp_configure_doc,
190
							'docstring' => $carp_configure_sig),
191
            'pfsense.backup_config_section' => 	array('function' => 'backup_config_section_xmlrpc',
192
							'signature' => $backup_config_section_sig,
193
							'docstring' => $backup_config_section_doc),
194
	    'pfsense.restore_config_section' => array('function' => 'restore_config_section_xmlrpc',
195
							'signature' => $restore_config_section_sig,
196
							'docstring' => $restore_config_section_doc),
197
	    'pfsense.filter_configure' => 	array('function' => 'filter_configure_xmlrpc',
198
							'signature' => $filter_configure_sig,
199
							'docstring' => $filter_configure_doc),
200
	    'pfsense.check_firmware_version' =>	array('function' => 'check_firmware_version_xmlrpc',
201
							'signature' => $check_firmware_version_sig,
202
							'docstring' => $check_firmware_version_doc),
203
	    'pfsense.reboot' =>			array('function' => 'reboot_xmlrpc',
204
							'signature' => $reboot_sig,
205
							'docstring' => $reboot_doc),
206
	    'pfsense.get_notices' =>		array('function' => 'get_notices_xmlrpc',
207
							'signature' => $get_notices_sig)
208
        )
209
);
210
?>
(153-153/153)