From 5c7303cb660e3fc1da8b1930d4e3b904777a6c27 Mon Sep 17 00:00:00 2001 From: Jon Langseth Date: Mon, 16 Apr 2012 12:14:09 +0200 Subject: [PATCH] Tool to add SIP user without involving FEIDE ;) --- guc-clients/addsipuser | 242 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 242 insertions(+) create mode 100755 guc-clients/addsipuser diff --git a/guc-clients/addsipuser b/guc-clients/addsipuser new file mode 100755 index 0000000..cacbad2 --- /dev/null +++ b/guc-clients/addsipuser @@ -0,0 +1,242 @@ +#!/usr/bin/perl +use strict; + +#TODO: Add support for assigning phone number +#TODO: Add support for overriding default domain ... + +use Getopt::Long; +use Net::LDAP; +use Net::LDAP::Control::Paged; +use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED ); +use LWP; +use JSON; +use AppConfig; +use Text::Iconv; + +my $utf2iso = Text::Iconv->new("utf-8","latin1"); +my $api_key; + +my $help; +my $username; +my $configfile; +my $dryrun = 0; +my ($g_ua, $session, $api_key, $auth_key, $data, $domain); +my ($user, $displayname, $phone, $mail, $sipuser, $linetext); + +my $config = AppConfig->new({ CREATE => 1 }); + +$config->define("api_url=s"); +$config->define("api_keyfile=s"); + +#$config->define("numbers_local_prefix=s"); +#$config->define("numbers_local_series=s"); +#$config->define("numbers_countrycode=s"); + +$config->define("default_domain=s"); + +#TODO: Move this to config file. +$config->default_domain("hig.no"); + +GetOptions( + "help" => \$help, + "username=s" => \$username, + "displayname=s" => \$displayname, + "configfile=s" => \$configfile, + "dryrun" => \$dryrun, +); + +if ( + (not $username) || + (not $displayname) || + (( $configfile ) && ( not -f $configfile )) +) +{ + $help = 1; +} + +$config->file( $configfile ); + +if ( ( not $config->api_url ) || + ( not $config->api_keyfile ) || + ( not $config->default_domain ) || + ( $config->api_keyfile && not -f $config->api_keyfile ) +) +{ + $help = 1; +} + +if ( $help ) { +print <api_keyfile; +chomp( $api_key = ); +close KEY; + +if ( not $username =~ m/\w+/ ) +{ print "Illegal username\n"; exit; } + +$sipuser = $username . "@" . $config->default_domain; +$linetext = $username; + +print "Data so far:" . "\n"; +print " Username: " . $username . "\n"; +print " Displayname: " . $displayname . "\n"; +print " Line-text: " . $linetext . "\n"; +print " SIP address: " . $sipuser . "\n"; + +# Convert the displayname to Latin1/ISO-8859-1 +#$displayname = $utf2iso->convert($displayname); + +$g_ua = LWP::UserAgent->new; +$g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant + +login_apikey(); + +$data = exec_apinode("user/available", { "user" => $sipuser }); +if ( not $data->{'response'} eq 'ok' ) +{ + printf("Unable to add user, Hermes response to available query is: %s\n", $data->{'cause'}); + exit; +} +undef $data; + +if ( $dryrun ) { + print("Dryrun specified. All OK so far, stopping before add.\n"); + logout(); + exit; +} + +$data = exec_apinode("user/add_local", { + "user" => $sipuser, + "displayname" => $displayname, + "email" => 'null@null.nul', #TODO: API MUST BE UPDATED +}); +if ( not $data->{'response'} eq 'ok' ) +{ + printf("Unable to add user, Hermes response to add_local query is: %s\n", $data->{'cause'}); + exit; +} +else +{ + printf("Added user, login information:\n"); + printf("username: %s@%s\nauthid: %s\npassword: %s\n" . + "registrar: %s:%d\nproxy: %s:%d\ndisplayname: %s\n" . + "email: %s\npermission: %s\n", + $data->{'user'}->{'username'}, + $data->{'user'}->{'domain'}, + $data->{'user'}->{'authid'}, + $data->{'user'}->{'password'}, + $data->{'user'}->{'registrar'}, + $data->{'user'}->{'r_port'}, + $data->{'user'}->{'proxy'}, + $data->{'user'}->{'p_port'}, + $data->{'user'}->{'displayname'}, + $data->{'user'}->{'email'}, + $data->{'user'}->{'permittedcalls'}, + ); + $domain = $data->{'user'}->{'domain'}, + $phone .= "\@" . $domain; +} +undef $data; + +##TODO## $data = exec_apinode("alias/add", { +##TODO## "destination" => $sipuser, +##TODO## "alias" => $phone, +##TODO## }); +##TODO## if ( not $data->{'response'} eq 'ok' ) +##TODO## { +##TODO## printf("Unable to add E164 number, Hermes response to add_local query is: %s\n", $data->{'cause'}); +##TODO## printf("Attempting to roll back user %s: ", $sipuser); +##TODO## exec_apinode("alias/remove", { "alias" => $mail }); +##TODO## exec_apinode("user/remove", { "user" => $sipuser }); +##TODO## exit; +##TODO## } +##TODO## else +##TODO## { printf("e164: %s\n", $phone); } +##TODO## undef $data; +##TODO## +##TODO## $data = exec_apinode("user/update", { +##TODO## "user" => $sipuser, +##TODO## "linetext" => $linetext, +##TODO## }); +##TODO## if ( not $data->{'response'} eq 'ok' ) +##TODO## { +##TODO## printf("Did not update 'linetext' element."); +##TODO## } +##TODO## + +# During testing: +#exec_apinode("alias/remove", { "alias" => $mail }); +#exec_apinode("alias/remove", { "alias" => $phone }); +#exec_apinode("user/remove", { "user" => $sipuser }); + +logout(); +################################################################################################ +sub exec_apinode($$) +{ + my $node = shift; + my $param = shift; + + my ( $response, $data ); + + $session = "" if not defined $session; + $auth_key = "" if not defined $auth_key; + my $url = $config->api_url . "/" . $node; + + $param->{'session'} = $session; + $param->{'auth_key'} = $auth_key; + + $response = $g_ua->post( $url, $param ); + if ( $response->is_success ) + { + if ( $response->content =~ m/\s*{/ ) + { + $data = decode_json( $response->content); + } + else + { + $data = $response->content; + } + + } + return $data; +} + +sub login_apikey +{ + my $response = $g_ua->post( $config->api_url . "/auth/login", + [ "api_key" => $api_key ] ); + + my $data = decode_json( $response->content) if $response->is_success; + die("HTTP error") unless $response->is_success; + + if ( $data->{'response'} eq "ok" ) + { + $session = $data->{'session'}; + $auth_key = $data->{'auth_key'}; + } + else + { + print "Unable to log in to Hermes API\n"; + exit; + } + undef $data; undef $response; +} + +sub logout +{ + my $response = $g_ua->post( $config->api_url . "/auth/logout", + [ "session" => $session ] ); + die("HTTP error") unless $response->is_success; + undef $session; undef $auth_key; +} -- 2.39.2