]> git.defcon.no Git - hermes/blob - guc-clients/assignphone
Adding the first few local CLI-clients as examples
[hermes] / guc-clients / assignphone
1 #!/usr/bin/perl
2 use strict;
3
4 use Getopt::Long;
5 use Net::LDAP;
6 use Net::LDAP::Control::Paged;
7 use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED );
8 use LWP;
9 use JSON;
10 use AppConfig;
11
12 my $api_key;
13
14 my $help;
15 my $username;
16 my $configfile;
17 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
18 my ($user, $sipuser, $mac);
19
20 my $config = AppConfig->new({ CREATE => 1 });
21
22 $config->define("api_url=s");
23 $config->define("api_keyfile=s");
24
25 GetOptions(
26 "help" => \$help,
27 "macaddress=s" => \$mac,
28 "username=s" => \$username,
29 "configfile=s" => \$configfile,
30 );
31
32 if (
33 (not $username) ||
34 (not $mac) ||
35 (( $configfile ) && ( not -f $configfile ))
36 )
37 {
38 $help = 1;
39 }
40
41 $config->file( $configfile );
42
43 if ( ( not $config->api_url ) ||
44 ( not $config->api_keyfile ) ||
45 ( $config->api_keyfile && not -f $config->api_keyfile ) )
46 {
47 $help = 1;
48 }
49
50 if ( $help ) {
51 print <<END_HELP;
52 WARNING: This tool assumes that only one domain
53 is registered with Kamailio. For Multidomain-setup,
54 this tool must be rewritten!
55
56 Verify that the following options are set:
57 --configfile=s|--config|-c
58 --username=s|--user|-u
59 --macaddress=s|--mac|-m
60
61 Verify the contents of the configuration file.
62 Verify that the key-file exists.
63 END_HELP
64 exit; }
65
66 open KEY, "<" . $config->api_keyfile;
67 chomp( $api_key = <KEY> );
68 close KEY;
69
70 if ( not $username =~ m/\w+/ )
71 { print "Illegal username\n"; exit; }
72
73 # Do stuff to the MAC adress.
74 $mac =~ s/[:-]//g if ( $mac =~ m/((?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2})/i);
75 $mac = lc $mac;
76 if ( not $mac =~ m/^[a-f0-9]{12}/ )
77 {
78 printf("Malformed MAC adress.\n");
79 exit;
80 }
81
82 $g_ua = LWP::UserAgent->new;
83 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
84
85 login_apikey();
86
87 # First: fetch a supported domain from the API...
88 $data = exec_apinode("domain/list", undef);
89 if ( $data->{'response'} eq 'ok' )
90 {
91 $domain = $data->{'list'}[0];
92 }
93 else
94 {
95 printf("Unable to get domain name. Aborting\n");
96 logout();
97 exit;
98 }
99 $sipuser = $username . '@' . $domain;
100
101 $data = exec_apinode("user/get", { 'user' => $sipuser });
102 if ( not $data->{'response'} eq 'ok' )
103 {
104 printf("Failed to verify that user exists. Aborting\n");
105 logout();
106 exit;
107 }
108
109 $data = exec_apinode("phone/add", { 'user' => $sipuser, 'mac' => $mac });
110
111 printf("Failed to add phone+user, cause given: '%s'\n", $data->{'cause'})
112 if ( not $data->{'response'} eq 'ok' );
113
114 printf("Assigned phone with mac '%s' to user '%s'\n", $mac, $sipuser)
115 if ( $data->{'response'} eq 'ok' );
116
117 logout();
118 ################################################################################################
119 sub exec_apinode($$)
120 {
121 my $node = shift;
122 my $param = shift;
123
124 my ( $response, $data );
125
126 $session = "" if not defined $session;
127 $auth_key = "" if not defined $auth_key;
128 my $url = $config->api_url . "/" . $node;
129
130 $param->{'session'} = $session;
131 $param->{'auth_key'} = $auth_key;
132
133 $response = $g_ua->post( $url, $param );
134 if ( $response->is_success )
135 {
136 if ( $response->content =~ m/\s*{/ )
137 {
138 $data = decode_json( $response->content);
139 }
140 else
141 {
142 $data = $response->content;
143 }
144
145 }
146 return $data;
147 }
148
149 sub login_apikey
150 {
151 my $response = $g_ua->post( $config->api_url . "/auth/login",
152 [ "api_key" => $api_key ] );
153
154 my $data = decode_json( $response->content) if $response->is_success;
155 die("HTTP error") unless $response->is_success;
156
157 if ( $data->{'response'} eq "ok" )
158 {
159 $session = $data->{'session'};
160 $auth_key = $data->{'auth_key'};
161 }
162 else
163 {
164 print "Unable to log in to Hermes API\n";
165 exit;
166 }
167 undef $data; undef $response;
168 }
169
170 sub logout
171 {
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;
176 }