]>
git.defcon.no Git - hermes/blob - guc-clients/assignphone
6 use Net
::LDAP
::Control
::Paged
;
7 use Net
::LDAP
::Constant
qw( LDAP_CONTROL_PAGED );
17 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
18 my ($user, $sipuser, $mac);
20 my $config = AppConfig
->new({ CREATE
=> 1 });
22 $config->define("api_url=s");
23 $config->define("api_keyfile=s");
27 "macaddress=s" => \
$mac,
28 "username=s" => \
$username,
29 "configfile=s" => \
$configfile,
35 (( $configfile ) && ( not -f
$configfile ))
41 $config->file( $configfile );
43 if ( ( not $config->api_url ) ||
44 ( not $config->api_keyfile ) ||
45 ( $config->api_keyfile && not -f
$config->api_keyfile ) )
52 WARNING: This tool assumes that only one domain
53 is registered with Kamailio. For Multidomain-setup,
54 this tool must be rewritten!
56 Verify that the following options are set:
57 --configfile=s|--config|-c
58 --username=s|--user|-u
59 --macaddress=s|--mac|-m
61 Verify the contents of the configuration file.
62 Verify that the key-file exists.
66 open KEY, "<" . $config->api_keyfile;
67 chomp( $api_key = <KEY> );
70 if ( not $username =~ m/\w+/ )
71 { print "Illegal username\n"; exit; }
73 # Do stuff to the MAC adress.
74 $mac =~ s/[:-]//g if ( $mac =~ m/((?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2})/i);
76 if ( not $mac =~ m/^[a-f0-9]{12}/ )
78 printf("Malformed MAC adress.\n");
82 $g_ua = LWP::UserAgent->new;
83 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
87 # First: fetch a supported domain from the API...
88 $data = exec_apinode("domain/list", undef);
89 if ( $data->{'response'} eq 'ok' )
91 $domain = $data->{'list'}[0];
95 printf("Unable to get domain name. Aborting\n");
99 $sipuser = $username . '@' . $domain;
101 $data = exec_apinode("user/get", { 'user' => $sipuser });
102 if ( not $data->{'response'} eq 'ok' )
104 printf("Failed to verify that user exists. Aborting\n");
109 $data = exec_apinode("phone/add", { 'user' => $sipuser, 'mac' => $mac });
111 printf("Failed to add phone+user, cause given: '%s'\n", $data->{'cause'})
112 if ( not $data->{'response'} eq 'ok' );
114 printf("Assigned phone with mac '%s' to user '%s'\n", $mac, $sipuser)
115 if ( $data->{'response'} eq 'ok' );
118 ################################################################################################
124 my ( $response, $data );
126 $session = "" if not defined $session;
127 $auth_key = "" if not defined $auth_key;
128 my $url = $config->api_url . "/" . $node;
130 $param->{'session'} = $session;
131 $param->{'auth_key'} = $auth_key;
133 $response = $g_ua->post( $url, $param );
134 if ( $response->is_success )
136 if ( $response->content =~ m/\s*{/ )
138 $data = decode_json( $response->content);
142 $data = $response->content;
151 my $response = $g_ua->post( $config->api_url . "/auth/login",
152 [ "api_key" => $api_key ] );
154 my $data = decode_json( $response->content) if $response->is_success;
155 die("HTTP error") unless $response->is_success;
157 if ( $data->{'response'} eq "ok" )
159 $session = $data->{'session'};
160 $auth_key = $data->{'auth_key'};
164 print "Unable to log in to Hermes API\n";
167 undef $data; undef $response;
172 my $response = $g_ua->post( $config->api_url . "/auth/logout",
173 [ "session" => $session ] );
174 die("HTTP error") unless $response->is_success;
175 undef $session; undef $auth_key;