]> git.defcon.no Git - hermes/blob - guc-clients/addsipuser
cacbad2fd8df8339b2a03ad60fb91a0f80751e32
[hermes] / guc-clients / addsipuser
1 #!/usr/bin/perl
2 use strict;
3
4 #TODO: Add support for assigning phone number
5 #TODO: Add support for overriding default domain ...
6
7 use Getopt::Long;
8 use Net::LDAP;
9 use Net::LDAP::Control::Paged;
10 use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED );
11 use LWP;
12 use JSON;
13 use AppConfig;
14 use Text::Iconv;
15
16 my $utf2iso = Text::Iconv->new("utf-8","latin1");
17 my $api_key;
18
19 my $help;
20 my $username;
21 my $configfile;
22 my $dryrun = 0;
23 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
24 my ($user, $displayname, $phone, $mail, $sipuser, $linetext);
25
26 my $config = AppConfig->new({ CREATE => 1 });
27
28 $config->define("api_url=s");
29 $config->define("api_keyfile=s");
30
31 #$config->define("numbers_local_prefix=s");
32 #$config->define("numbers_local_series=s");
33 #$config->define("numbers_countrycode=s");
34
35 $config->define("default_domain=s");
36
37 #TODO: Move this to config file.
38 $config->default_domain("hig.no");
39
40 GetOptions(
41 "help" => \$help,
42 "username=s" => \$username,
43 "displayname=s" => \$displayname,
44 "configfile=s" => \$configfile,
45 "dryrun" => \$dryrun,
46 );
47
48 if (
49 (not $username) ||
50 (not $displayname) ||
51 (( $configfile ) && ( not -f $configfile ))
52 )
53 {
54 $help = 1;
55 }
56
57 $config->file( $configfile );
58
59 if ( ( not $config->api_url ) ||
60 ( not $config->api_keyfile ) ||
61 ( not $config->default_domain ) ||
62 ( $config->api_keyfile && not -f $config->api_keyfile )
63 )
64 {
65 $help = 1;
66 }
67
68 if ( $help ) {
69 print <<END_HELP;
70 Verify that the following options are set:
71 --configfile=s
72 --username=s
73 --displayname=s
74 --dryrun
75
76 Verify the contents of the configuration file.
77 Verify that the key-file exists.
78 END_HELP
79 exit; }
80
81 open KEY, "<" . $config->api_keyfile;
82 chomp( $api_key = <KEY> );
83 close KEY;
84
85 if ( not $username =~ m/\w+/ )
86 { print "Illegal username\n"; exit; }
87
88 $sipuser = $username . "@" . $config->default_domain;
89 $linetext = $username;
90
91 print "Data so far:" . "\n";
92 print " Username: " . $username . "\n";
93 print " Displayname: " . $displayname . "\n";
94 print " Line-text: " . $linetext . "\n";
95 print " SIP address: " . $sipuser . "\n";
96
97 # Convert the displayname to Latin1/ISO-8859-1
98 #$displayname = $utf2iso->convert($displayname);
99
100 $g_ua = LWP::UserAgent->new;
101 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
102
103 login_apikey();
104
105 $data = exec_apinode("user/available", { "user" => $sipuser });
106 if ( not $data->{'response'} eq 'ok' )
107 {
108 printf("Unable to add user, Hermes response to available query is: %s\n", $data->{'cause'});
109 exit;
110 }
111 undef $data;
112
113 if ( $dryrun ) {
114 print("Dryrun specified. All OK so far, stopping before add.\n");
115 logout();
116 exit;
117 }
118
119 $data = exec_apinode("user/add_local", {
120 "user" => $sipuser,
121 "displayname" => $displayname,
122 "email" => 'null@null.nul', #TODO: API MUST BE UPDATED
123 });
124 if ( not $data->{'response'} eq 'ok' )
125 {
126 printf("Unable to add user, Hermes response to add_local query is: %s\n", $data->{'cause'});
127 exit;
128 }
129 else
130 {
131 printf("Added user, login information:\n");
132 printf("username: %s@%s\nauthid: %s\npassword: %s\n" .
133 "registrar: %s:%d\nproxy: %s:%d\ndisplayname: %s\n" .
134 "email: %s\npermission: %s\n",
135 $data->{'user'}->{'username'},
136 $data->{'user'}->{'domain'},
137 $data->{'user'}->{'authid'},
138 $data->{'user'}->{'password'},
139 $data->{'user'}->{'registrar'},
140 $data->{'user'}->{'r_port'},
141 $data->{'user'}->{'proxy'},
142 $data->{'user'}->{'p_port'},
143 $data->{'user'}->{'displayname'},
144 $data->{'user'}->{'email'},
145 $data->{'user'}->{'permittedcalls'},
146 );
147 $domain = $data->{'user'}->{'domain'},
148 $phone .= "\@" . $domain;
149 }
150 undef $data;
151
152 ##TODO## $data = exec_apinode("alias/add", {
153 ##TODO## "destination" => $sipuser,
154 ##TODO## "alias" => $phone,
155 ##TODO## });
156 ##TODO## if ( not $data->{'response'} eq 'ok' )
157 ##TODO## {
158 ##TODO## printf("Unable to add E164 number, Hermes response to add_local query is: %s\n", $data->{'cause'});
159 ##TODO## printf("Attempting to roll back user %s: ", $sipuser);
160 ##TODO## exec_apinode("alias/remove", { "alias" => $mail });
161 ##TODO## exec_apinode("user/remove", { "user" => $sipuser });
162 ##TODO## exit;
163 ##TODO## }
164 ##TODO## else
165 ##TODO## { printf("e164: %s\n", $phone); }
166 ##TODO## undef $data;
167 ##TODO##
168 ##TODO## $data = exec_apinode("user/update", {
169 ##TODO## "user" => $sipuser,
170 ##TODO## "linetext" => $linetext,
171 ##TODO## });
172 ##TODO## if ( not $data->{'response'} eq 'ok' )
173 ##TODO## {
174 ##TODO## printf("Did not update 'linetext' element.");
175 ##TODO## }
176 ##TODO##
177
178 # During testing:
179 #exec_apinode("alias/remove", { "alias" => $mail });
180 #exec_apinode("alias/remove", { "alias" => $phone });
181 #exec_apinode("user/remove", { "user" => $sipuser });
182
183 logout();
184 ################################################################################################
185 sub exec_apinode($$)
186 {
187 my $node = shift;
188 my $param = shift;
189
190 my ( $response, $data );
191
192 $session = "" if not defined $session;
193 $auth_key = "" if not defined $auth_key;
194 my $url = $config->api_url . "/" . $node;
195
196 $param->{'session'} = $session;
197 $param->{'auth_key'} = $auth_key;
198
199 $response = $g_ua->post( $url, $param );
200 if ( $response->is_success )
201 {
202 if ( $response->content =~ m/\s*{/ )
203 {
204 $data = decode_json( $response->content);
205 }
206 else
207 {
208 $data = $response->content;
209 }
210
211 }
212 return $data;
213 }
214
215 sub login_apikey
216 {
217 my $response = $g_ua->post( $config->api_url . "/auth/login",
218 [ "api_key" => $api_key ] );
219
220 my $data = decode_json( $response->content) if $response->is_success;
221 die("HTTP error") unless $response->is_success;
222
223 if ( $data->{'response'} eq "ok" )
224 {
225 $session = $data->{'session'};
226 $auth_key = $data->{'auth_key'};
227 }
228 else
229 {
230 print "Unable to log in to Hermes API\n";
231 exit;
232 }
233 undef $data; undef $response;
234 }
235
236 sub logout
237 {
238 my $response = $g_ua->post( $config->api_url . "/auth/logout",
239 [ "session" => $session ] );
240 die("HTTP error") unless $response->is_success;
241 undef $session; undef $auth_key;
242 }