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