]> git.defcon.no Git - hermes/blob - user.php
Adding ignore file, initially ignore the running config file
[hermes] / user.php
1 <?php
2 require_once('config.php');
3 require_once('lib/user_functions.php');
4 require_once('lib/common_functions.php');
5 require_once('lib/db_functions.php');
6 require_once('lib/phone_functions.php');
7
8 $config = get_config();
9
10 $config['sql_link'] = @mysql_connect(
11 $config['sql_server'],
12 $config['sql_username'],
13 $config['sql_password']
14 );
15 if ( !$config['sql_link'] )
16 {
17 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
18 exit;
19 }
20
21 //*************************************************************************************
22 switch ( $_SERVER['PATH_INFO'] )
23 {
24 case "/get":
25 // Required GET parameters:
26 // user: authentication username, SIP-username without domain component
27 // domain: Domain/realm of the user. username + '@' + domain == SIP address.
28 if ( array_key_exists( 'username', $_GET) && array_key_exists( 'domain', $_GET ) )
29 {
30 $username = $_GET['username'];
31 $domain = $_GET['domain'];
32 // Now, do funky stuff.
33 /*
34 Test if user exists in both 'kamailio.subscribers' and 'provision.users'
35 * Return 'response' => 'ok', 'type' => 'local', 'user' => complete user object.
36 Test if user exists in 'provision.user' only
37 * Return 'response' => 'ok', 'type' => 'remote', 'user' => complete user object.
38 If user does is neither local nor remote
39 * Return 'response' => 'failed' with 'cause' => 'nonexistant'
40 On failure, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
41
42 */
43 // Dummy-response:
44 $userdata = get_userdata( $username, $domain );
45 if ( $userdata )
46 {
47 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
48 }
49 else
50 {
51 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Request for user ' . $username . '@' . $domain . ' failed.'));
52 }
53 }
54 else
55 print json_encode ( array( 'response' => 'invalid') );
56 break;
57 case "/list":
58 /*
59 Simply list all users in user@domain format
60 Perform a search operation if 'search' exists as a GET-parameter
61 * The search should try to do a "smart search" on SIP-usernames:
62 * Try to search with names in in username@domain format
63 * Do the search with wildcards before and after input text.
64 * The search must be done in the provisioning tables, to be able
65 to match non-local users.
66 * SQL SELECT CONCAT() WHERE CONCAT() must be used *shrug*
67 */
68 $search = null;
69 if ( array_key_exists ( 'search', $_GET ) )
70 $search = $_GET['search']; // TODO: Add some sanitation and input validation!
71 $list = list_users( $search );
72 print json_encode( array( 'response' => 'ok', 'list' => $list ));
73 break;
74 case "/add_local":
75 /*
76 What to do??
77 Required parameters should be...
78 username
79 domain
80 displayname
81 email
82
83 Verify that domain is local (lookup in the 'kamailio.domain' table.
84 Verify that the username is available (nonexistant for domain in kamilio.subscribers (and provision.users?))
85 * Autocreate password
86 * Add username, domain, email and created password to the 'kamailio.subscriber' table
87 * Get the registrar+port, proxy+port from the 'provision.servers' table.
88 * standard dialplan from configuration.
89 * Add to the 'provision.users' table:
90 username -> username
91 password -> generated password
92 displayname -> displayname
93 domain -> domain
94 registrar -> provision.servers.registrar
95 r_port -> provision.servers.r_port
96 proxy -> provision.servers.proxy
97 p_port -> provision.servers.p_port
98 authid -> username
99 dialplan -> standard dialplan
100 linetext -> username
101 * Return 'response' => 'ok' with a full user object in JSON format.
102 If any of the tests fail, return 'response' => 'failed' with 'cause' => "description" on JSON format.
103
104 */
105 // Test required parameters:
106 if ( array_key_exists( 'username', $_GET)
107 && array_key_exists( 'domain', $_GET )
108 && array_key_exists( 'displayname', $_GET )
109 && array_key_exists( 'email', $_GET ) )
110 {
111 $username = $_GET['username'];
112 $domain = $_GET['domain'];
113 $password = generate_password();
114 $displayname = $_GET['displayname'];
115 $email = $_GET['email'];
116
117 if ( !is_kamailio_domain( $domain ) )
118 {
119 print json_encode ( array( 'response' => 'failed', 'cause' => 'nxdomain', 'detail' => 'The selected domain is not local' ));
120 break;
121 }
122
123 $servers = get_servers( $domain );
124 if ( !$servers )
125 {
126 print json_encode( array( 'response' => 'failed', 'cause' => 'servfail', 'detail' => 'Servers lookup failed for domain '. $domain ) );
127 break;
128 }
129 $registrar = $servers['registrar'];
130 $r_port = $servers['r_port'];
131 $proxy = $servers['proxy'];
132 $p_port = $servers['p_port'];
133 $authid = $username;
134 $linetext = $username;
135 $dialplan = $config['standard_dialplan'];
136
137 if ( is_provision_user ( $username, $domain ) )
138 {
139 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
140 break;
141 }
142 if ( is_kamailio_subscriber ( $username, $domain ) )
143 {
144 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
145 break;
146 }
147 $kam_res = add_kamailio_subscriber( $username, $domain, $password, $email );
148 if ( !$kam_res )
149 {
150 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add kamailio subscriber.' ) );
151 break;
152 }
153 $pro_res = add_provision_user( $username, $password, $domain, $authid, $registrar, $r_port, $proxy, $p_port, $displayname, $dialplan, $linetext );
154 if ( !$pro_res )
155 {
156 // Rollback data added to Kamailio! Try to simulate atomicity, or atleast maintain integrity...
157 delete_kamailio_subscriber( $username, $domain );
158 // Give errormessage, and quit.
159 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add user for provisioning. Rolled back kamailio subscriber' ) );
160 break;
161 }
162 $userdata = get_userdata( $username, $domain );
163 if ( !$userdata )
164 {
165 // Rollback data added to Kamailio! Try to simulate atomicity, or atleast maintain integrity...
166 delete_kamailio_subscriber( $username, $domain );
167 delete_provision_user( $username, $domain );
168 // Give errormessage, and quit.
169 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to read recently added data. Operations rolled back' ) );
170
171 }
172 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
173 }
174 else
175 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
176 break;
177 case "/add_remote":
178 /*
179 Required parameters should be...
180 username
181 password
182 domain
183 displayname
184 registrar
185 Optional parameters
186 r_port
187 proxy
188 p_port
189 authid
190 dialplan
191 linetext
192
193 Verify that the username+domain is not already registered in 'provision.users'.
194 * If r_port is empty, set to 5060
195 * If proxy/port is empty, set to registrar/port
196 * If authid is empty, set to username
197 * If dialplan is empty, set to standard dialplan
198 * If linetext is empty, set to username@domain
199 * Add to the 'provision.users' table:
200 username -> username
201 password -> supplied password
202 displayname -> displayname
203 domain -> domain
204 registrar -> registrar
205 r_port -> r_port
206 proxy -> proxy
207 p_port -> p_port
208 authid -> authid
209 dialplan -> dialplan
210 linetext -> linetext
211 * Return 'response' => 'ok' with a full user object in JSON format.
212 If any of the tests fail, return 'response' => 'failed' with 'cause' => "description" in JSON format.
213 */
214
215
216 // Test required parameters:
217 if ( array_key_exists( 'username', $_GET)
218 && array_key_exists( 'password', $_GET )
219 && array_key_exists( 'displayname', $_GET )
220 && array_key_exists( 'domain', $_GET )
221 && array_key_exists( 'registrar', $_GET ) )
222 {
223 $username = $_GET['username'];
224 $password = $_GET['password'];
225 $domain = $_GET['domain'];
226 $displayname = $_GET['displayname'];
227 $registrar = $_GET['registrar'];
228 $r_port = ( array_key_exists('r_port', $_GET) ) ? $_GET['r_port'] : 5060;
229
230 $proxy = ( array_key_exists('proxy', $_GET) ) ? $_GET['proxy'] : $registrar;
231 $p_port = ( array_key_exists('p_port', $_GET) ) ? $_GET['p_port'] : $r_port;
232 $authid = ( array_key_exists('authid', $_GET) ) ? $_GET['authid'] : $username;
233 $dialplan = ( array_key_exists('dialplan', $_GET) ) ? $_GET['dialplan'] : $config['standard_dialplan'];
234 $linetext = ( array_key_exists('linetext', $_GET) ) ? $_GET['linetext'] : $username . '@' . $domain;
235
236 if ( is_provision_user ( $username, $domain ) )
237 {
238 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
239 break;
240 }
241 if ( is_kamailio_subscriber ( $username, $domain ) )
242 {
243 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
244 break;
245 }
246
247 $pro_res = add_provision_user( $username, $password, $domain, $authid, $registrar, $r_port, $proxy, $p_port, $displayname, $dialplan, $linetext );
248 if ( !$pro_res )
249 {
250 // Give errormessage, and quit.
251 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add user for provisioning.' ) );
252 break;
253 }
254 $userdata = get_userdata( $username, $domain );
255 if ( !$userdata )
256 {
257 // Rollback data added!
258 delete_provision_user( $username, $domain );
259 // Give errormessage, and quit.
260 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to read recently added data. Operations rolled back' ) );
261
262 }
263 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
264 }
265 else
266 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
267 break;
268 case "/remove":
269 /*
270 Required parameters should be...
271 username
272 domain
273
274 * Verify that no associations/relations exist in 'provision.phones'
275 * Verify that the user exists in 'provision.users'
276 * Remove from 'provision.users'
277 * Test to see of user exists in 'kamailio.subscriber'.
278 * Remove from 'kamailio.subscribers'
279 * Return response' => 'ok', 'type' => 'local'
280 * If not in 'kamailio.subscribers'
281 * Return response' => 'ok', 'type' => 'remote'
282 * If associations exist, return 'response' => 'failed', 'cause' => 'inuse'
283 * If no such user exists, return 'response' => 'failed' with 'cause' => 'nonexistant'
284 * On other failures, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
285 */
286 if ( array_key_exists( 'username', $_GET) && array_key_exists( 'domain', $_GET ) )
287 {
288 $username = $_GET['username'];
289 $domain = $_GET['domain'];
290 if ( get_user_phones ( $username, $domain ) )
291 {
292 print json_encode( array( 'response' => 'failed', 'cause' => 'inuse', 'detail' => 'User has associated provisioning. Remove and retry.' ) );
293 break;
294 }
295 if ( is_provision_user( $username, $domain ) || is_kamailio_subscriber( $username, $domain ) )
296 {
297 delete_provision_user( $username, $domain );
298 delete_kamailio_subscriber( $username, $domain );
299 print json_encode( array ( 'response' => 'ok', 'detail' => 'User ' . $username . '@' . $domain . ' deleted.'));
300 break;
301 }
302 else
303 {
304 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Unable to remove nonexistant user.'));
305 break;
306 }
307
308 break;
309
310 }
311 break;
312 case "/gen_pw":
313 print generate_password();
314 break;
315 default:
316 print json_encode ( array( 'response' => 'invalid') );
317 }
318 mysql_close( $config['sql_link'] );
319 ?>