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