]>
git.defcon.no Git - hermes/blob - guc-clients/assignphone
6 use Net
::LDAP
::Control
::Paged
;
7 use Net
::LDAP
::Constant
qw( LDAP_CONTROL_PAGED );
16 my $configfile = undef;
19 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
20 my ($user, $sipuser, $mac);
22 my $config = AppConfig
->new({ CREATE
=> 1 });
24 $config->define("api_url=s");
25 $config->define("api_keyfile=s");
28 "/usr/local/etc/hermes/hermes_config",
29 "/usr/local/etc/hermes/config",
31 $ENV{"HOME"} . "/.hermes/config",
32 $ENV{"HOME"} . "/.hermes_config",
33 ) { $configfile = $_ if ( -f
$_ ); }
37 "macaddress=s" => \
$mac,
38 "username=s" => \
$username,
39 "configfile=s" => \
$configfile,
47 (( $configfile ) && ( not -f
$configfile ))
53 $config->file( $configfile );
55 if ( ( not $config->api_url ) ||
56 ( not $config->api_keyfile ) ||
57 ( $config->api_keyfile && not -f
$config->api_keyfile ) )
64 WARNING: This tool assumes that only one domain
65 is registered with Kamailio. For Multidomain-setup,
66 this tool must be rewritten!
68 Verify that the following options are set:
69 --configfile=s|--config|-c
70 --username=s|--user|-u
71 --macaddress=s|--mac|-m
75 Verify the contents of the configuration file.
76 Verify that the key-file exists.
80 open KEY, "<" . $config->api_keyfile;
81 chomp( $api_key = <KEY> );
84 if ( not $username =~ m/\w+/ )
85 { print "Illegal username\n"; exit; }
87 # Do stuff to the MAC adress.
88 $mac =~ s/[:-]//g if ( $mac =~ m/((?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2})/i);
90 if ( not $mac =~ m/^[a-f0-9]{12}/ )
92 printf("Malformed MAC adress.\n");
96 $g_ua = LWP::UserAgent->new;
97 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
101 # First: fetch a supported domain from the API...
102 $data = exec_apinode("domain/list", undef);
103 if ( $data->{'response'} eq 'ok' )
105 $domain = $data->{'list'}[0];
109 printf("Unable to get domain name. Aborting\n");
113 $sipuser = $username . '@' . $domain;
115 $data = exec_apinode("user/get", { 'user' => $sipuser });
116 if ( not $data->{'response'} eq 'ok' )
118 printf("Failed to verify that user exists. Aborting\n");
124 $data = exec_apinode("phone/get", { 'user' => $sipuser, 'mac' => $mac });
125 if ( not $data->{'response'} eq 'ok' )
127 printf("Unable to remove phone+user, lookup gave: '%s'\n", $data->{'cause'});
133 print("Dryrun specified. All OK so far, stopping before add.\n");
139 $data = exec_apinode("phone/remove", { 'user' => $sipuser, 'mac' => $mac });
141 printf("Failed to remove phone+user, cause given: '%s'\n", $data->{'cause'})
142 if ( not $data->{'response'} eq 'ok' );
144 printf("Removed assigned phone with mac '%s' from user '%s'\n", $mac, $sipuser)
145 if ( $data->{'response'} eq 'ok' );
149 $data = exec_apinode("phone/add", { 'user' => $sipuser, 'mac' => $mac });
151 printf("Failed to add phone+user, cause given: '%s'\n", $data->{'cause'})
152 if ( not $data->{'response'} eq 'ok' );
154 printf("Assigned phone with mac '%s' to user '%s'\n", $mac, $sipuser)
155 if ( $data->{'response'} eq 'ok' );
158 ################################################################################################
164 my ( $response, $data );
166 $session = "" if not defined $session;
167 $auth_key = "" if not defined $auth_key;
168 my $url = $config->api_url . "/" . $node;
170 $param->{'session'} = $session;
171 $param->{'auth_key'} = $auth_key;
173 $response = $g_ua->post( $url, $param );
174 if ( $response->is_success )
176 if ( $response->content =~ m/\s*{/ )
178 $data = decode_json( $response->content);
182 $data = $response->content;
191 my $response = $g_ua->post( $config->api_url . "/auth/login",
192 [ "api_key" => $api_key ] );
194 my $data = decode_json( $response->content) if $response->is_success;
195 die("HTTP error") unless $response->is_success;
197 if ( $data->{'response'} eq "ok" )
199 $session = $data->{'session'};
200 $auth_key = $data->{'auth_key'};
204 print "Unable to log in to Hermes API\n";
207 undef $data; undef $response;
212 my $response = $g_ua->post( $config->api_url . "/auth/logout",
213 [ "session" => $session ] );
214 die("HTTP error") unless $response->is_success;
215 undef $session; undef $auth_key;