]>
git.defcon.no Git - hermes/blob - guc-clients/feide2sipuser
6 use Net
::LDAP
::Control
::Paged
;
7 use Net
::LDAP
::Constant
qw( LDAP_CONTROL_PAGED );
16 my $configfile = undef;
18 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
19 my ($user, $displayname, $phone, $mail, $sipuser, $linetext);
21 my $config = AppConfig
->new({ CREATE
=> 1 });
23 $config->define("api_url=s");
24 $config->define("api_keyfile=s");
25 $config->define("feide_server=s");
26 $config->define("feide_login=s");
27 $config->define("feide_passwd=s");
28 $config->define("feide_people=s");
30 $config->define("numbers_local_prefix=s");
31 $config->define("numbers_local_series=s");
32 $config->define("numbers_countrycode=s");
35 "/usr/local/etc/hermes/hermes_config",
36 "/usr/local/etc/hermes/config",
38 $ENV{"HOME"} . "/.hermes/config",
39 $ENV{"HOME"} . "/.hermes_config",
40 ) { $configfile = $_ if ( -f
$_ ); }
44 "username=s" => \
$username,
45 "configfile=s" => \
$configfile,
51 (( $configfile ) && ( not -f
$configfile ))
57 $config->file( $configfile );
59 if ( ( not $config->api_url ) ||
60 ( not $config->api_keyfile ) ||
61 ( $config->api_keyfile && not -f
$config->api_keyfile ) ||
62 ( not $config->feide_server ) ||
63 ( not $config->feide_login ) ||
64 ( not $config->feide_passwd ) ||
65 ( not $config->feide_people ) )
72 Verify that the following options are set:
77 Verify the contents of the configuration file.
78 Verify that the key-file exists.
82 open KEY, "<" . $config->api_keyfile;
83 chomp( $api_key = <KEY> );
86 if ( not $username =~ m/\w+/ )
87 { print "Illegal username\n"; exit; }
90 # Kobler til LDAP-server
91 my $ldap = Net::LDAP->new($config->feide_server, version => 3)
92 or die "Can't connect to LDAP server: " . $config->feide_server . "!\n";
93 my $msg = $ldap->bind (dn => $config->feide_login, password => $config->feide_passwd);
95 die ("LDAP error: ", $msg->error_text((), "\n"));
98 $msg = $ldap->search( base => $config->feide_people,
100 filter => "(uid=". $username .")",
102 die("Getting information for " . $username . " failed..." ) if ( $msg->count != 1 );
105 $displayname = $user->get_value("displayName");
106 $phone = $user->get_value("telephoneNumber");
107 $mail = $user->get_value("mail");
108 $sipuser = $user->get_value("eduPersonPrincipalName");
110 my $short_test = "^" . $config->numbers_local_series;
111 my $long_test = "^" . $config->numbers_local_prefix . $config->numbers_local_series;
113 if ( $phone =~ m/$short_test/ )
115 print "Phone number is in 5-digit local format. Completing E164.\n";
116 $linetext = $config->numbers_local_prefix . $phone;
117 $phone = $config->numbers_countrycode . $config->numbers_local_prefix . $phone;
119 elsif ( $phone =~ m/$long_test/ )
121 print "Phone number is in 8-digit local format. Adding CC.\n" ;
123 $phone = $config->numbers_countrycode . $phone;
127 print "Unable to recognize Phone number. Check FEIDE\n";
131 print "Data so far:" . "\n";
132 print " Username: " . $username . "\n";
133 print " Displayname: " . $displayname . "\n";
134 print " Phone: " . $phone . "\n";
135 print " Line-text: " . $linetext . "\n";
136 print " Mail: " . $mail . "\n";
137 print " SIP address: " . $sipuser . "\n";
139 $msg = $ldap->unbind;
140 if ( $msg->code()) { die ("LDAP error: ", $msg->error_text((), "\n")); }
142 $g_ua = LWP::UserAgent->new;
143 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
148 $data = exec_apinode("user/available", { "user" => $sipuser });
149 if ( not $data->{'response'} eq 'ok' )
151 printf("Unable to add user, Hermes response to available query is: %s\n", $data->{'cause'});
157 print("Dryrun specified. All OK so far, stopping before add.\n");
162 $data = exec_apinode("user/add_local", {
164 "displayname" => $displayname,
167 if ( not $data->{'response'} eq 'ok' )
169 printf("Unable to add user, Hermes response to add_local query is: %s\n", $data->{'cause'});
174 printf("Added user, login information:\n");
175 printf("username: %s@%s\nauthid: %s\npassword: %s\n" .
176 "registrar: %s:%d\nproxy: %s:%d\ndisplayname: %s\n" .
177 "email: %s\npermission: %s\n",
178 $data->{'user'}->{'username'},
179 $data->{'user'}->{'domain'},
180 $data->{'user'}->{'authid'},
181 $data->{'user'}->{'password'},
182 $data->{'user'}->{'registrar'},
183 $data->{'user'}->{'r_port'},
184 $data->{'user'}->{'proxy'},
185 $data->{'user'}->{'p_port'},
186 $data->{'user'}->{'displayname'},
187 $data->{'user'}->{'email'},
188 $data->{'user'}->{'permittedcalls'},
190 $domain = $data->{'user'}->{'domain'},
191 $phone .= "\@" . $domain;
195 unless ( $mail eq $sipuser )
197 $data = exec_apinode("alias/add", {
198 "destination" => $sipuser,
201 if ( not $data->{'response'} eq 'ok' )
203 printf("Unable to add alias, Hermes response to add_local query is: %s\n", $data->{'cause'});
204 printf("Attempting to roll back user %s: ", $sipuser);
205 undef $data; $data = exec_apinode("user/remove", { "user" => $sipuser });
206 printf("%s\n", $data->{'response'});
210 { printf("alias: %s\n", $mail); }
214 $data = exec_apinode("alias/add", {
215 "destination" => $sipuser,
218 if ( not $data->{'response'} eq 'ok' )
220 printf("Unable to add E164 number, Hermes response to add_local query is: %s\n", $data->{'cause'});
221 printf("Attempting to roll back user %s: ", $sipuser);
222 exec_apinode("alias/remove", { "alias" => $mail });
223 exec_apinode("user/remove", { "user" => $sipuser });
227 { printf("e164: %s\n", $phone); }
230 $data = exec_apinode("user/update", {
232 "linetext" => $linetext,
234 if ( not $data->{'response'} eq 'ok' )
236 printf("Did not update 'linetext' element.");
240 #exec_apinode("alias/remove", { "alias" => $mail });
241 #exec_apinode("alias/remove", { "alias" => $phone });
242 #exec_apinode("user/remove", { "user" => $sipuser });
245 ################################################################################################
251 my ( $response, $data );
253 $session = "" if not defined $session;
254 $auth_key = "" if not defined $auth_key;
255 my $url = $config->api_url . "/" . $node;
257 $param->{'session'} = $session;
258 $param->{'auth_key'} = $auth_key;
260 $response = $g_ua->post( $url, $param );
261 if ( $response->is_success )
263 if ( $response->content =~ m/\s*{/ )
265 $data = decode_json( $response->content);
269 $data = $response->content;
278 my $response = $g_ua->post( $config->api_url . "/auth/login",
279 [ "api_key" => $api_key ] );
281 my $data = decode_json( $response->content) if $response->is_success;
282 die("HTTP error") unless $response->is_success;
284 if ( $data->{'response'} eq "ok" )
286 $session = $data->{'session'};
287 $auth_key = $data->{'auth_key'};
291 print "Unable to log in to Hermes API\n";
294 undef $data; undef $response;
299 my $response = $g_ua->post( $config->api_url . "/auth/logout",
300 [ "session" => $session ] );
301 die("HTTP error") unless $response->is_success;
302 undef $session; undef $auth_key;