]>
git.defcon.no Git - hermes/blob - guc-clients/assignphone
91b10a9d32184f25b1736691e4664e0fd8be1235
6 use Net
::LDAP
::Control
::Paged
;
7 use Net
::LDAP
::Constant
qw( LDAP_CONTROL_PAGED );
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");
28 "macaddress=s" => \
$mac,
29 "username=s" => \
$username,
30 "configfile=s" => \
$configfile,
37 (( $configfile ) && ( not -f
$configfile ))
43 $config->file( $configfile );
45 if ( ( not $config->api_url ) ||
46 ( not $config->api_keyfile ) ||
47 ( $config->api_keyfile && not -f
$config->api_keyfile ) )
54 WARNING: This tool assumes that only one domain
55 is registered with Kamailio. For Multidomain-setup,
56 this tool must be rewritten!
58 Verify that the following options are set:
59 --configfile=s|--config|-c
60 --username=s|--user|-u
61 --macaddress=s|--mac|-m
64 Verify the contents of the configuration file.
65 Verify that the key-file exists.
69 open KEY, "<" . $config->api_keyfile;
70 chomp( $api_key = <KEY> );
73 if ( not $username =~ m/\w+/ )
74 { print "Illegal username\n"; exit; }
76 # Do stuff to the MAC adress.
77 $mac =~ s/[:-]//g if ( $mac =~ m/((?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2})/i);
79 if ( not $mac =~ m/^[a-f0-9]{12}/ )
81 printf("Malformed MAC adress.\n");
85 $g_ua = LWP::UserAgent->new;
86 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
90 # First: fetch a supported domain from the API...
91 $data = exec_apinode("domain/list", undef);
92 if ( $data->{'response'} eq 'ok' )
94 $domain = $data->{'list'}[0];
98 printf("Unable to get domain name. Aborting\n");
102 $sipuser = $username . '@' . $domain;
104 $data = exec_apinode("user/get", { 'user' => $sipuser });
105 if ( not $data->{'response'} eq 'ok' )
107 printf("Failed to verify that user exists. Aborting\n");
113 print("Dryrun specified. All OK so far, stopping before add.\n");
118 $data = exec_apinode("phone/add", { 'user' => $sipuser, 'mac' => $mac });
120 printf("Failed to add phone+user, cause given: '%s'\n", $data->{'cause'})
121 if ( not $data->{'response'} eq 'ok' );
123 printf("Assigned phone with mac '%s' to user '%s'\n", $mac, $sipuser)
124 if ( $data->{'response'} eq 'ok' );
127 ################################################################################################
133 my ( $response, $data );
135 $session = "" if not defined $session;
136 $auth_key = "" if not defined $auth_key;
137 my $url = $config->api_url . "/" . $node;
139 $param->{'session'} = $session;
140 $param->{'auth_key'} = $auth_key;
142 $response = $g_ua->post( $url, $param );
143 if ( $response->is_success )
145 if ( $response->content =~ m/\s*{/ )
147 $data = decode_json( $response->content);
151 $data = $response->content;
160 my $response = $g_ua->post( $config->api_url . "/auth/login",
161 [ "api_key" => $api_key ] );
163 my $data = decode_json( $response->content) if $response->is_success;
164 die("HTTP error") unless $response->is_success;
166 if ( $data->{'response'} eq "ok" )
168 $session = $data->{'session'};
169 $auth_key = $data->{'auth_key'};
173 print "Unable to log in to Hermes API\n";
176 undef $data; undef $response;
181 my $response = $g_ua->post( $config->api_url . "/auth/logout",
182 [ "session" => $session ] );
183 die("HTTP error") unless $response->is_success;
184 undef $session; undef $auth_key;