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