]>
git.defcon.no Git - hermes/blob - api/user.php
2 require_once('config.php');
3 require_once('lib/auth_base.php');
4 require_once('lib/user_functions.php');
5 require_once('lib/common_functions.php');
6 require_once('lib/db_functions.php');
7 require_once('lib/phone_functions.php');
8 require_once('lib/alias_functions.php');
10 $config = get_config();
12 $config['sql_link'] = @mysql_connect
(
13 $config['sql_server'],
14 $config['sql_username'],
15 $config['sql_password']
17 if ( !$config['sql_link'] )
19 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
24 //*************************************************************************************
25 switch ( $_SERVER['PATH_INFO'] )
28 // Required GET parameters:
29 // user: authentication username, SIP-username without domain component
30 // domain: Domain/realm of the user. username + '@' + domain == SIP address.
32 if ( array_key_exists('user', $_POST) ||
33 ( array_key_exists('username', $_POST) && array_key_exists('domain', $_POST )))
37 if ( array_key_exists('username', $_POST) )
39 $username = $_POST['username'];
40 $domain = $_POST['domain'];
44 $user = split_sipaddress($_POST['user']);
47 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
50 list ( $username, $domain ) = $user;
53 // Now, do funky stuff.
55 Test if user exists in both 'kamailio.subscribers' and 'provision.users'
56 * Return 'response' => 'ok', 'type' => 'local', 'user' => complete user object.
57 Test if user exists in 'provision.user' only
58 * Return 'response' => 'ok', 'type' => 'remote', 'user' => complete user object.
59 If user does is neither local nor remote
60 * Return 'response' => 'failed' with 'cause' => 'nonexistant'
61 On failure, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
65 $userdata = get_userdata( $username, $domain );
68 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
72 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Request for user ' . $username . '@' . $domain . ' failed.'));
76 print json_encode ( array( 'response' => 'invalid') );
80 Simply list all users in user@domain format
81 Perform a search operation if 'search' exists as a GET-parameter
82 * The search should try to do a "smart search" on SIP-usernames:
83 * Try to search with names in in username@domain format
84 * Do the search with wildcards before and after input text.
85 * The search must be done in the provisioning tables, to be able
86 to match non-local users.
87 * SQL SELECT CONCAT() WHERE CONCAT() must be used *shrug*
90 if ( array_key_exists ( 'search', $_POST ) )
91 $search = $_POST['search']; // TODO: Add some sanitation and input validation!
92 $list = list_users( $search );
93 print json_encode( array( 'response' => 'ok', 'list' => $list ));
98 Required parameters should be...
99 ( username & domain ) | user
103 Verify that domain is local (lookup in the 'kamailio.domain' table.
104 Verify that the username is available (nonexistant for domain in kamilio.subscribers (and provision.users?))
105 * Autocreate password
106 * Add username, domain, email and created password to the 'kamailio.subscriber' table
107 * Get the registrar+port, proxy+port from the 'provision.servers' table.
108 * standard dialplan from configuration.
109 * Add to the 'provision.users' table:
111 password -> generated password
112 displayname -> displayname
114 registrar -> provision.servers.registrar
115 r_port -> provision.servers.r_port
116 proxy -> provision.servers.proxy
117 p_port -> provision.servers.p_port
119 dialplan -> standard dialplan
121 * Return 'response' => 'ok' with a full user object in JSON format.
122 If any of the tests fail, return 'response' => 'failed' with 'cause' => "description" on JSON format.
125 // Test required parameters:
127 ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) ) ||
array_key_exists('user', $_POST) )
128 && array_key_exists( 'displayname', $_POST )
129 && array_key_exists( 'email', $_POST ) )
133 if ( array_key_exists('username', $_POST) )
135 $username = $_POST['username'];
136 $domain = $_POST['domain'];
140 $user = split_sipaddress($_POST['user']);
143 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
146 list ( $username, $domain ) = $user;
149 $password = generate_password();
150 $displayname = $_POST['displayname'];
151 $email = $_POST['email'];
153 if ( !is_kamailio_domain( $domain ) )
155 print json_encode ( array( 'response' => 'failed', 'cause' => 'nxdomain', 'detail' => 'The selected domain is not local' ));
159 $servers = get_servers( $domain );
162 print json_encode( array( 'response' => 'failed', 'cause' => 'servfail', 'detail' => 'Servers lookup failed for domain '. $domain ) );
165 $registrar = $servers['registrar'];
166 $r_port = $servers['r_port'];
167 $proxy = $servers['proxy'];
168 $p_port = $servers['p_port'];
170 $linetext = $username;
171 $dialplan = $config['standard_dialplan'];
173 if ( is_provision_user ( $username, $domain ) )
175 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
178 if ( is_kamailio_subscriber ( $username, $domain ) )
180 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
183 if ( alias_exists ( $username, $domain ) )
185 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'Username exists as an alias' ));
189 $kam_res = add_kamailio_subscriber( $username, $domain, $password, $email );
192 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add kamailio subscriber.' ) );
195 $pro_res = add_provision_user( $username, $password, $domain, $authid, $registrar, $r_port, $proxy, $p_port, $displayname, $dialplan, $linetext );
198 // Rollback data added to Kamailio! Try to simulate atomicity, or atleast maintain integrity...
199 delete_kamailio_subscriber( $username, $domain );
200 // Give errormessage, and quit.
201 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add user for provisioning. Rolled back kamailio subscriber' ) );
204 $userdata = get_userdata( $username, $domain );
207 // Rollback data added to Kamailio! Try to simulate atomicity, or atleast maintain integrity...
208 delete_kamailio_subscriber( $username, $domain );
209 delete_provision_user( $username, $domain );
210 // Give errormessage, and quit.
211 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to read recently added data. Operations rolled back' ) );
214 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
217 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
221 Required parameters should be...
222 ( username & domain ) | user
234 Verify that the domain is not a local kamailio domain (REMOTE user..)
235 Verify that the username+domain is not already registered in 'provision.users'.
236 * If r_port is empty, set to 5060
237 * If proxy/port is empty, set to registrar/port
238 * If authid is empty, set to username
239 * If dialplan is empty, set to standard dialplan
240 * If linetext is empty, set to username@domain
241 * Add to the 'provision.users' table:
243 password -> supplied password
244 displayname -> displayname
246 registrar -> registrar
253 * Return 'response' => 'ok' with a full user object in JSON format.
254 If any of the tests fail, return 'response' => 'failed' with 'cause' => "description" in JSON format.
258 // Test required parameters:
260 ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) ) ||
array_key_exists('user', $_POST) )
261 && array_key_exists( 'displayname', $_POST )
262 && array_key_exists( 'password', $_POST )
263 && array_key_exists( 'registrar', $_POST ) )
267 if ( array_key_exists('username', $_POST) )
269 $username = $_POST['username'];
270 $domain = $_POST['domain'];
274 $user = split_sipaddress($_POST['user']);
277 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
280 list ( $username, $domain ) = $user;
283 $password = $_POST['password'];
284 $displayname = $_POST['displayname'];
285 $registrar = $_POST['registrar'];
286 $r_port = ( array_key_exists('r_port', $_POST) ) ?
$_POST['r_port'] : 5060;
288 $proxy = ( array_key_exists('proxy', $_POST) ) ?
$_POST['proxy'] : $registrar;
289 $p_port = ( array_key_exists('p_port', $_POST) ) ?
$_POST['p_port'] : $r_port;
290 $authid = ( array_key_exists('authid', $_POST) ) ?
$_POST['authid'] : $username;
291 $dialplan = ( array_key_exists('dialplan', $_POST) ) ?
$_POST['dialplan'] : $config['standard_dialplan'];
292 $linetext = ( array_key_exists('linetext', $_POST) ) ?
$_POST['linetext'] : $username . '@' . $domain;
294 if ( is_kamailio_domain( $domain ) )
296 print json_encode ( array( 'response' => 'failed', 'cause' => 'domain', 'detail' => 'The selected domain is local, cannot add remote user' ));
300 if ( is_provision_user ( $username, $domain ) )
302 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
305 if ( is_kamailio_subscriber ( $username, $domain ) )
307 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
311 // Should be impossible to hit this test, all aliases are required to be local.
312 if ( alias_exists ( $username, $domain ) )
314 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'Username exists as an alias' ));
319 $pro_res = add_provision_user( $username, $password, $domain, $authid, $registrar, $r_port, $proxy, $p_port, $displayname, $dialplan, $linetext );
322 // Give errormessage, and quit.
323 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add user for provisioning.' ) );
326 $userdata = get_userdata( $username, $domain );
329 // Rollback data added!
330 delete_provision_user( $username, $domain );
331 // Give errormessage, and quit.
332 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to read recently added data. Operations rolled back' ) );
335 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
338 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
342 Required parameters should be...
343 ( username & domain ) | user
345 * Verify that no associations/relations exist in 'provision.phones'
346 * Verify that the user exists in 'provision.users'
347 * Remove from 'provision.users'
348 * Test to see of user exists in 'kamailio.subscriber'.
349 * Remove from 'kamailio.subscribers'
350 * Return response' => 'ok', 'type' => 'local'
351 * If not in 'kamailio.subscribers'
352 * Return response' => 'ok', 'type' => 'remote'
353 * If associations exist, return 'response' => 'failed', 'cause' => 'inuse'
354 * If no such user exists, return 'response' => 'failed' with 'cause' => 'nonexistant'
355 * On other failures, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
357 if ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) )
358 ||
array_key_exists('user', $_POST) )
363 if ( array_key_exists('username', $_POST) )
365 $username = $_POST['username'];
366 $domain = $_POST['domain'];
370 $user = split_sipaddress($_POST['user']);
373 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
376 list ( $username, $domain ) = $user;
379 if ( get_user_phones ( $username, $domain ) )
381 print json_encode( array( 'response' => 'failed', 'cause' => 'inuse', 'detail' => 'User has associated provisioning. Remove and retry.' ) );
384 if ( is_provision_user( $username, $domain ) ||
is_kamailio_subscriber( $username, $domain ) )
386 delete_provision_user( $username, $domain );
387 delete_kamailio_subscriber( $username, $domain );
388 print json_encode( array ( 'response' => 'ok', 'detail' => 'User ' . $username . '@' . $domain . ' deleted.'));
393 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Unable to remove nonexistant user.'));
400 print json_encode ( array( 'response' => 'invalid') );
404 Required parameters should be...
405 ( username & domain ) | user
408 * Verify that no associations/relations exist in 'provision.phones'
409 * Verify that the user exists ...
410 * Test to see of user exists in 'provision.users'
411 * Test to see of user exists in 'kamailio.subscriber'.
412 * If no such user exists, return 'response' => 'failed' with 'cause' => 'nonexistant'
413 * Update user passwords in 'provision' and 'kamailio' as appropriate
414 * On other failures, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
416 if ( array_key_exists('password', $_POST) &&
417 ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) )
418 ||
array_key_exists('user', $_POST) ))
423 if ( array_key_exists('username', $_POST) )
425 $username = $_POST['username'];
426 $domain = $_POST['domain'];
430 $user = split_sipaddress($_POST['user']);
433 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
436 list ( $username, $domain ) = $user;
438 $password = $_POST['password'];
440 // Check compatibility of password? TODO...
441 // Fetch old password for rollback? TODO...
442 // Verify that user exists for provisioning
443 if ( ! is_provision_user( $username, $domain ) )
445 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => '' . $username . '@' . $domain . ' does not exist.'));
448 if ( is_provision_user( $username, $domain ) )
450 // Update provisioning password
451 if ( update_provision_pw( $username, $domain, $password ) < 0 )
453 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to update provisioning password' ) );
457 // Check for user in kamailio
458 if ( is_kamailio_subscriber( $username, $domain ) )
460 // Update kamailio password
461 if ( update_kamailio_pw( $username, $domain, $password ) < 0 )
463 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to update kamailio password' ) );
467 print json_encode( array ( 'response' => 'ok', 'detail' => 'Password changed for user '.$username.'@'.$domain.'.'));
471 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
475 case "/change_email":
477 Required parameters should be...
478 ( username & domain ) | user
481 if ( array_key_exists('email', $_POST) &&
482 ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) )
483 ||
array_key_exists('user', $_POST) ))
488 if ( array_key_exists('username', $_POST) )
490 $username = $_POST['username'];
491 $domain = $_POST['domain'];
495 $user = split_sipaddress($_POST['user']);
498 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
501 list ( $username, $domain ) = $user;
503 $email = $_POST['email'];
505 // Check for user in kamailio
506 if ( is_kamailio_subscriber( $username, $domain ) )
508 // Update kamailio email
509 if ( update_kamailio_email( $username, $domain, $email ) < 0 )
511 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to update kamailio email' ) );
515 print json_encode( array ( 'response' => 'ok', 'user' => $username.'@'.$domain, 'email' => $email));
519 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
530 Required parameters should be...
531 ( username & domain ) | user
533 * Verify that no associations/relations exist in 'provision.phones'
534 * Verify that the user exists ...
535 * Test to see of user exists in 'provision.users'
536 * Test to see of user exists in 'kamailio.subscriber'.
537 * If no such user exists, return 'response' => 'failed' with 'cause' => 'nonexistant'
538 * Get update parameters, and change as appropriate ;)
539 * On other failures, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
541 if ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) )
542 ||
array_key_exists('user', $_POST) )
547 if ( array_key_exists('username', $_POST) )
549 $username = $_POST['username'];
550 $domain = $_POST['domain'];
554 $user = split_sipaddress($_POST['user']);
557 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
560 list ( $username, $domain ) = $user;
562 if ( ! is_provision_user ( $username, $domain ) )
564 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => '' . $username . '@' . $domain . ' does not exist.'));
571 $params = array('displayname', 'dialplan', 'linetext', 'registrar', 'r_port', 'proxy', 'p_port');
572 foreach ( $params as $p )
574 if ( array_key_exists($p, $_POST ) )
577 $t = update_provision_data($p, $username, $domain, $data);
581 array_push($failed, $p);
585 array_push( $updated, $p);
590 if ( ( $error == 1 ) ||
( $error == 0 ) )
592 $res['response'] = 'ok';
593 $res['skipped'] = $failed;
595 else if ( $error == -1 )
597 $res['response'] = 'failed';
598 $res['cause'] = 'param';
599 $res['detail'] = 'Invalid parameters';
600 $res['failed'] = $failed;
602 else if ( $error == -2 )
604 $res['response'] = 'failed';
605 $res['cause'] = 'dbfail';
606 $res['detail'] = 'Database failure';
607 $res['failed'] = $failed;
610 $res['response'] = 'error'; // Wait, what?
612 $res['updated'] = $updated;
614 print json_encode ( $res );
617 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
620 if ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) )
621 ||
array_key_exists('user', $_POST) )
626 if ( array_key_exists('username', $_POST) )
628 $username = $_POST['username'];
629 $domain = $_POST['domain'];
633 $user = split_sipaddress($_POST['user']);
636 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
639 list ( $username, $domain ) = $user;
641 if ( is_provision_user ( $username, $domain ) )
643 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
646 if ( is_kamailio_subscriber ( $username, $domain ) )
648 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
651 if ( alias_exists ( $username, $domain ) )
653 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'Username exists as an alias' ));
656 print json_encode( array ( 'response' => 'ok', 'cause' => 'nonexistant', 'detail' => '' . $username . '@' . $domain . ' does not exist.'));
660 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
663 print generate_password();
666 print json_encode ( array( 'response' => 'invalid') );
668 mysql_close( $config['sql_link'] );