]> git.defcon.no Git - hermes/blob - guc-clients/sipalias
Untested tool to add sip-aliases to users/accounts, with remove-option
[hermes] / guc-clients / sipalias
1 #!/usr/bin/perl
2 use strict;
3
4 use Data::Dumper;
5
6 #TODO: Add support for assigning phone number
7 #TODO: Add support for overriding default domain ...
8
9 use Getopt::Long;
10 use Net::LDAP;
11 use Net::LDAP::Control::Paged;
12 use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED );
13 use LWP;
14 use JSON;
15 use AppConfig;
16 use Text::Iconv;
17
18 my $api_key;
19
20 my $help;
21 my $username;
22 my $alias;
23 my $number;
24 my $remove = 0;
25 my $dryrun = 0;
26 my $configfile = undef;
27 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
28
29 my $config = AppConfig->new({ CREATE => 1 });
30
31 $config->define("api_url=s");
32 $config->define("api_keyfile=s");
33
34 $config->define("default_domain=s");
35 $config->default_domain("hig.no");
36
37 $config->define("numbers_local_prefix=s");
38 $config->define("numbers_local_series=s");
39 $config->define("numbers_countrycode=s");
40
41 foreach (
42 "/usr/local/etc/hermes/hermes_config",
43 "/usr/local/etc/hermes/config",
44 "/etc/hermes/config",
45 $ENV{"HOME"} . "/.hermes/config",
46 $ENV{"HOME"} . "/.hermes_config",
47 ) { $configfile = $_ if ( -f $_ ); }
48
49 GetOptions(
50 "help" => \$help,
51 "configfile=s" => \$configfile,
52 "username=s" => \$username,
53 "alias=s" => \$alias,
54 "number=s" => \$number,
55 "remove" => \$remove,
56 "dryrun" => \$dryrun,
57
58 );
59
60 if (
61 (not $username) ||
62 (not $configfile) ||
63 ((not $alias) && (not $number)) ||
64 (($alias) && ($number)) ||
65 (( $configfile ) && ( not -f $configfile ))
66 )
67 {
68 $help = 1;
69 }
70
71 $config->file( $configfile );
72
73 if ( ( not $config->api_url ) ||
74 ( not $config->api_keyfile ) ||
75 ( $config->api_keyfile && not -f $config->api_keyfile )
76 )
77 {
78 $help = 1;
79 }
80
81 if ( $help ) {
82 print <<END_HELP;
83 Verify that the following options are set:
84 --username=s|--user|-u
85 either --alias=s|-a
86 or --number=s|-n
87 --remove
88 --dryrun
89 --csv
90
91 Verify the contents of the configuration file.
92 Verify that the key-file exists.
93 END_HELP
94 exit; }
95
96 open KEY, "<" . $config->api_keyfile;
97 chomp( $api_key = <KEY> );
98 close KEY;
99
100 if ( not $username =~ m/\w+/ )
101 { print "Illegal username\n"; exit; }
102
103 $g_ua = LWP::UserAgent->new;
104 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
105
106 login_apikey();
107
108 # First: fetch a supported domain from the API...
109 $data = exec_apinode("domain/list", undef);
110 if ( $data->{'response'} eq 'ok' )
111 {
112 $domain = $data->{'list'}[0];
113 }
114 else
115 {
116 printf("Unable to get domain name. Aborting\n");
117 logout();
118 exit;
119 }
120
121 if ( $username =~ /@/ )
122 {
123 ( $username, $domain ) = split /@/, $username;
124 }
125
126 $data = exec_apinode("user/available", { 'username' => $username, 'domain' => $domain });
127 if ( $data->{'response'} eq 'ok' )
128 {
129 printf("Username is not registered.\n");
130 logout();
131 exit;
132 }
133
134 if ( not $data->{'cause'} eq 'exists' )
135 {
136 printf("Username lookup failed, cause: %s.\n", $data->{'cause'});
137 logout();
138 exit;
139 }
140 undef $data;
141
142 my $use_alias;
143
144 if ( $number )
145 {
146
147 if ( $number =~ m/^+.\d+$/)
148 {
149 # Prefix country-code unless number includes ....
150 $use_alias = $config->numbers_countrycode unless $number =~ m/^\+\d\d/;
151
152 # Add local prefix if local-series regex matches.
153 my $t = $config->numbers_local_series;
154 $use_alias .= $config->numbers_local_prefix if $number =~ m/^$t$/;
155
156 # Add supplied number
157 $use_alias .= $number;
158
159 # Finally, tack on domain to complete E164 SIP address:
160 $use_alias .= "@" . $domain;
161 }
162 else
163 {
164 printf("Failed number format test. Check input and retry\n");
165 logout();
166 exit;
167 }
168 }
169 elsif ( $alias )
170 {
171 # Add domain to given alias, unless it seems to contain one..
172 $alias .= "@" . $domain if not $alias =~ m/\@\w+/;
173 print $alias . "\n";
174
175 # A fairly naive email-format address checker...
176 if ( $alias =~ m/^([\w-_\+]+\.)*[\w-_\+]+\@([\w-_\+]+\.)+\w+$/ )
177 {
178 $use_alias = $alias;
179 }
180 else
181 {
182 printf("Failed alias format test. Check input and retry\n");
183 logout();
184 exit;
185 }
186 }
187 printf("Alias after expanding options: %s\n", $use_alias);
188
189 # URL-encode any plus-signs in the address...
190 $use_alias =~ s/\+/\%2B/g;
191
192 undef $data;
193 # run alias/list with alias=$use_alias, expect an empty list
194 $data = exec_apinode("alias/list", { 'alias' => $use_alias });
195 if ( not $data->{'response'} eq 'ok' )
196 {
197 printf("Something failed trying to see if alias is in use...\n");
198 logout();
199 exit;
200 }
201 my $t = $data->{'aliases'}; my @aliases = @$t;
202
203 # End of common code, rest is different for add/remove
204 if ( not $remove )
205 {
206 # if list is non-empty, fail/abort, alias already taken
207 if ( not $#aliases == -1 )
208 {
209 printf("Given alias/number already exists, unable to proceed\n");
210 logout();
211 exit;
212 }
213 # run alias/add with $username@$domain as destination and $use_alias as alias
214 undef $data;
215 $data = exec_apinode("alias/add", { 'alias' => $use_alias, 'destination' => $username . "@" . $domain });
216 # fail unless OK is returned.
217 if ( not $data->{'response'} eq 'ok' )
218 {
219 printf("Unable to add alias, Hermes response is: %s\n", $data->{'cause'});
220 logout();
221 exit;
222 }
223 printf("Alias after expansion '%s' added to user '%s'\n", $use_alias, $username . "@" . $domain );
224 }
225 else
226 {
227 if ( not $#aliases == 0 )
228 {
229 printf("Search for alias did not return correct number of results (%d != 1)\n", ($#aliases+1));
230 logout();
231 exit;
232 }
233 if ( not $aliases[0]->{'alias'} eq $use_alias )
234 {
235 printf("Not a match on alias: %s != %s\n", $aliases[0]->{'alias'}, $use_alias );
236 }
237
238 if ( not $aliases[0]->{'destination'} eq $username . "@" . $domain )
239 {
240 printf("Not a match on destination: %s != %s\n",
241 $aliases[0]->{'destination'}, $username . "@" . $domain
242 );
243 }
244 }
245
246 logout();
247 ################################################################################################
248 sub exec_apinode($$)
249 {
250 my $node = shift;
251 my $param = shift;
252
253 my ( $response, $data );
254
255 $session = "" if not defined $session;
256 $auth_key = "" if not defined $auth_key;
257 my $url = $config->api_url . "/" . $node;
258
259 $param->{'session'} = $session;
260 $param->{'auth_key'} = $auth_key;
261
262 $response = $g_ua->post( $url, $param );
263 if ( $response->is_success )
264 {
265 if ( $response->content =~ m/\s*{/ )
266 {
267 $data = decode_json( $response->content);
268 }
269 else
270 {
271 $data = $response->content;
272 }
273
274 }
275 return $data;
276 }
277
278 sub login_apikey
279 {
280 my $response = $g_ua->post( $config->api_url . "/auth/login",
281 [ "api_key" => $api_key ] );
282
283 my $data = decode_json( $response->content) if $response->is_success;
284 die("HTTP error") unless $response->is_success;
285
286 if ( $data->{'response'} eq "ok" )
287 {
288 $session = $data->{'session'};
289 $auth_key = $data->{'auth_key'};
290 }
291 else
292 {
293 print "Unable to log in to Hermes API\n";
294 exit;
295 }
296 undef $data; undef $response;
297 }
298
299 sub logout
300 {
301 my $response = $g_ua->post( $config->api_url . "/auth/logout",
302 [ "session" => $session ] );
303 die("HTTP error") unless $response->is_success;
304 undef $session; undef $auth_key;
305 }