]>
git.defcon.no Git - hermes/blob - guc-clients/reset-tool
6 #TODO: Add support for assigning phone number
7 #TODO: Add support for overriding default domain ...
11 use Net
::LDAP
::Control
::Paged
;
12 use Net
::LDAP
::Constant
qw( LDAP_CONTROL_PAGED );
27 my $configfile = undef;
29 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
31 my $config = AppConfig
->new({ CREATE
=> 1 });
33 $config->define("api_url=s");
34 $config->define("api_keyfile=s");
37 "/usr/local/etc/hermes/hermes_config",
38 "/usr/local/etc/hermes/config",
40 $ENV{"HOME"} . "/.hermes/config",
41 $ENV{"HOME"} . "/.hermes_config",
42 ) { $configfile = $_ if ( -f
$_ ); }
46 "configfile=s" => \
$configfile,
47 "username=s" => \
$username,
51 "sipsak=s" => \
$sipsak,
61 (( $configfile ) && ( not -f
$configfile ))
67 $config->file( $configfile );
69 if ( ( not $config->api_url ) ||
70 ( not $config->api_keyfile ) ||
71 ( $config->api_keyfile && not -f
$config->api_keyfile )
79 Verify that the following options are set:
80 --username=s|--user|-u
81 --agent=s|-a User agent to match
82 --file=s|-f SIPSAK template file
83 --proxy=s|-p SIP Proxy to send SIP messages to
84 --sipsak=s|-s Path to sipsak executable
87 Verify the contents of the configuration file.
88 Verify that the key-file exists.
92 open KEY, "<" . $config->api_keyfile;
93 chomp( $api_key = <KEY> );
96 if ( not $username =~ m/\w+/ )
97 { print "Illegal username\n"; exit; }
99 $g_ua = LWP::UserAgent->new;
100 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
104 # First: fetch a supported domain from the API...
105 $data = exec_apinode("domain/list", undef);
106 if ( $data->{'response'} eq 'ok' )
108 $domain = $data->{'list'}[0];
112 printf("Unable to get domain name. Aborting\n");
117 if ( $username =~ /@/ )
119 ( $username, $domain ) = split /@/, $username;
122 $data = exec_apinode("user/location", { 'username' => $username, 'domain' => $domain });
123 if ( not $data->{'response'} eq 'ok' )
125 printf("Unable to fetch location: %s\n", $data->{'cause'});
128 my $t = $data->{'locations'};
131 foreach my $l ( @locations )
133 if ( $l->{'useragent'} =~ m/$agent/ )
135 $l->{'contact'} =~ m/sip:(\w+)@(.*):(\d+)/;
136 my ( $user, $host, $port) = ( $1, $2, $3 );
138 $data = exec_apinode("user/get", { 'username' => $user, 'domain' => $domain });
139 if ( not $data->{'response'} eq 'ok' )
141 printf("Unable to fetch user: %s\n", $data->{'cause'});
145 my $sipuser = $data->{'user'};
146 my $pass = $data->{'user'}->{'password'};
147 #printf("Auth password.: %s\n", $sipuser->{'password'});
149 printf("%s -vvv -G -f %s -p %s -H %s -s sip:%s@%s -a %s\n",
164 ################################################################################################
170 my ( $response, $data );
172 $session = "" if not defined $session;
173 $auth_key = "" if not defined $auth_key;
174 my $url = $config->api_url . "/" . $node;
176 $param->{'session'} = $session;
177 $param->{'auth_key'} = $auth_key;
179 $response = $g_ua->post( $url, $param );
180 if ( $response->is_success )
182 if ( $response->content =~ m/\s*{/ )
184 $data = decode_json( $response->content);
188 $data = $response->content;
197 my $response = $g_ua->post( $config->api_url . "/auth/login",
198 [ "api_key" => $api_key ] );
200 my $data = decode_json( $response->content) if $response->is_success;
201 die("HTTP error") unless $response->is_success;
203 if ( $data->{'response'} eq "ok" )
205 $session = $data->{'session'};
206 $auth_key = $data->{'auth_key'};
210 print "Unable to log in to Hermes API\n";
213 undef $data; undef $response;
218 my $response = $g_ua->post( $config->api_url . "/auth/logout",
219 [ "session" => $session ] );
220 die("HTTP error") unless $response->is_success;
221 undef $session; undef $auth_key;