]>
git.defcon.no Git - hermes/blob - guc-clients/assignphone
4b06f66eb6f56678e798d87c598d1825c0961d65
6 use Net
::LDAP
::Control
::Paged
;
7 use Net
::LDAP
::Constant
qw( LDAP_CONTROL_PAGED );
16 my $configfile = undef;
18 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
19 my ($user, $sipuser, $mac);
21 my $config = AppConfig
->new({ CREATE
=> 1 });
23 $config->define("api_url=s");
24 $config->define("api_keyfile=s");
27 "/usr/local/etc/hermes/hermes_config",
28 "/usr/local/etc/hermes/config",
30 $ENV{"HOME"} . "/.hermes/config",
31 $ENV{"HOME"} . "/.hermes_config",
32 ) { $configfile = $_ if ( -f
$_ ); }
36 "macaddress=s" => \
$mac,
37 "username=s" => \
$username,
38 "configfile=s" => \
$configfile,
45 (( $configfile ) && ( not -f
$configfile ))
51 $config->file( $configfile );
53 if ( ( not $config->api_url ) ||
54 ( not $config->api_keyfile ) ||
55 ( $config->api_keyfile && not -f
$config->api_keyfile ) )
62 WARNING: This tool assumes that only one domain
63 is registered with Kamailio. For Multidomain-setup,
64 this tool must be rewritten!
66 Verify that the following options are set:
67 --configfile=s|--config|-c
68 --username=s|--user|-u
69 --macaddress=s|--mac|-m
72 Verify the contents of the configuration file.
73 Verify that the key-file exists.
77 open KEY, "<" . $config->api_keyfile;
78 chomp( $api_key = <KEY> );
81 if ( not $username =~ m/\w+/ )
82 { print "Illegal username\n"; exit; }
84 # Do stuff to the MAC adress.
85 $mac =~ s/[:-]//g if ( $mac =~ m/((?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2})/i);
87 if ( not $mac =~ m/^[a-f0-9]{12}/ )
89 printf("Malformed MAC adress.\n");
93 $g_ua = LWP::UserAgent->new;
94 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
98 # First: fetch a supported domain from the API...
99 $data = exec_apinode("domain/list", undef);
100 if ( $data->{'response'} eq 'ok' )
102 $domain = $data->{'list'}[0];
106 printf("Unable to get domain name. Aborting\n");
110 $sipuser = $username . '@' . $domain;
112 $data = exec_apinode("user/get", { 'user' => $sipuser });
113 if ( not $data->{'response'} eq 'ok' )
115 printf("Failed to verify that user exists. Aborting\n");
121 print("Dryrun specified. All OK so far, stopping before add.\n");
126 $data = exec_apinode("phone/add", { 'user' => $sipuser, 'mac' => $mac });
128 printf("Failed to add phone+user, cause given: '%s'\n", $data->{'cause'})
129 if ( not $data->{'response'} eq 'ok' );
131 printf("Assigned phone with mac '%s' to user '%s'\n", $mac, $sipuser)
132 if ( $data->{'response'} eq 'ok' );
135 ################################################################################################
141 my ( $response, $data );
143 $session = "" if not defined $session;
144 $auth_key = "" if not defined $auth_key;
145 my $url = $config->api_url . "/" . $node;
147 $param->{'session'} = $session;
148 $param->{'auth_key'} = $auth_key;
150 $response = $g_ua->post( $url, $param );
151 if ( $response->is_success )
153 if ( $response->content =~ m/\s*{/ )
155 $data = decode_json( $response->content);
159 $data = $response->content;
168 my $response = $g_ua->post( $config->api_url . "/auth/login",
169 [ "api_key" => $api_key ] );
171 my $data = decode_json( $response->content) if $response->is_success;
172 die("HTTP error") unless $response->is_success;
174 if ( $data->{'response'} eq "ok" )
176 $session = $data->{'session'};
177 $auth_key = $data->{'auth_key'};
181 print "Unable to log in to Hermes API\n";
184 undef $data; undef $response;
189 my $response = $g_ua->post( $config->api_url . "/auth/logout",
190 [ "session" => $session ] );
191 die("HTTP error") unless $response->is_success;
192 undef $session; undef $auth_key;