]>
git.defcon.no Git - hermes/blob - guc-clients/feide2sipuser
6 use Net
::LDAP
::Control
::Paged
;
7 use Net
::LDAP
::Constant
qw( LDAP_CONTROL_PAGED );
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");
36 "username=s" => \
$username,
37 "configfile=s" => \
$configfile,
43 (( $configfile ) && ( not -f
$configfile ))
49 $config->file( $configfile );
51 if ( ( not $config->api_url ) ||
52 ( not $config->api_keyfile ) ||
53 ( $config->api_keyfile && not -f
$config->api_keyfile ) ||
54 ( not $config->feide_server ) ||
55 ( not $config->feide_login ) ||
56 ( not $config->feide_passwd ) ||
57 ( not $config->feide_people ) )
64 Verify that the following options are set:
69 Verify the contents of the configuration file.
70 Verify that the key-file exists.
74 open KEY, "<" . $config->api_keyfile;
75 chomp( $api_key = <KEY> );
78 if ( not $username =~ m/\w+/ )
79 { print "Illegal username\n"; exit; }
82 # Kobler til LDAP-server
83 my $ldap = Net::LDAP->new($config->feide_server, version => 3)
84 or die "Can't connect to LDAP server: " . $config->feide_server . "!\n";
85 my $msg = $ldap->bind (dn => $config->feide_login, password => $config->feide_passwd);
87 die ("LDAP error: ", $msg->error_text((), "\n"));
90 $msg = $ldap->search( base => $config->feide_people,
92 filter => "(uid=". $username .")",
94 die("Getting information for " . $username . " failed..." ) if ( $msg->count != 1 );
97 $displayname = $user->get_value("displayName");
98 $phone = $user->get_value("telephoneNumber");
99 $mail = $user->get_value("mail");
100 $sipuser = $user->get_value("eduPersonPrincipalName");
102 my $short_test = "^" . $config->numbers_local_series;
103 my $long_test = "^" . $config->numbers_local_prefix . $config->numbers_local_series;
105 if ( $phone =~ m/$short_test/ )
107 print "Phone number is in 5-digit local format. Completing E164.\n";
108 $linetext = $config->numbers_local_prefix . $phone;
109 $phone = $config->numbers_countrycode . $config->numbers_local_prefix . $phone;
111 elsif ( $phone =~ m/$long_test/ )
113 print "Phone number is in 8-digit local format. Adding CC.\n" ;
115 $phone = $config->numbers_countrycode . $phone;
119 print "Unable to recognize Phone number. Check FEIDE\n";
123 print "Data so far:" . "\n";
124 print " Username: " . $username . "\n";
125 print " Displayname: " . $displayname . "\n";
126 print " Phone: " . $phone . "\n";
127 print " Line-text: " . $linetext . "\n";
128 print " Mail: " . $mail . "\n";
129 print " SIP address: " . $sipuser . "\n";
131 $msg = $ldap->unbind;
132 if ( $msg->code()) { die ("LDAP error: ", $msg->error_text((), "\n")); }
134 $g_ua = LWP::UserAgent->new;
135 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
140 $data = exec_apinode("user/available", { "user" => $sipuser });
141 if ( not $data->{'response'} eq 'ok' )
143 printf("Unable to add user, Hermes response to available query is: %s\n", $data->{'cause'});
149 print("Dryrun specified. All OK so far, stopping before add.\n");
154 $data = exec_apinode("user/add_local", {
156 "displayname" => $displayname,
159 if ( not $data->{'response'} eq 'ok' )
161 printf("Unable to add user, Hermes response to add_local query is: %s\n", $data->{'cause'});
166 printf("Added user, login information:\n");
167 printf("username: %s@%s\nauthid: %s\npassword: %s\n" .
168 "registrar: %s:%d\nproxy: %s:%d\ndisplayname: %s\n" .
169 "email: %s\npermission: %s\n",
170 $data->{'user'}->{'username'},
171 $data->{'user'}->{'domain'},
172 $data->{'user'}->{'authid'},
173 $data->{'user'}->{'password'},
174 $data->{'user'}->{'registrar'},
175 $data->{'user'}->{'r_port'},
176 $data->{'user'}->{'proxy'},
177 $data->{'user'}->{'p_port'},
178 $data->{'user'}->{'displayname'},
179 $data->{'user'}->{'email'},
180 $data->{'user'}->{'permittedcalls'},
182 $domain = $data->{'user'}->{'domain'},
183 $phone .= "\@" . $domain;
187 unless ( $mail eq $sipuser )
189 $data = exec_apinode("alias/add", {
190 "destination" => $sipuser,
193 if ( not $data->{'response'} eq 'ok' )
195 printf("Unable to add alias, Hermes response to add_local query is: %s\n", $data->{'cause'});
196 printf("Attempting to roll back user %s: ", $sipuser);
197 undef $data; $data = exec_apinode("user/remove", { "user" => $sipuser });
198 printf("%s\n", $data->{'response'});
202 { printf("alias: %s\n", $mail); }
206 $data = exec_apinode("alias/add", {
207 "destination" => $sipuser,
210 if ( not $data->{'response'} eq 'ok' )
212 printf("Unable to add E164 number, Hermes response to add_local query is: %s\n", $data->{'cause'});
213 printf("Attempting to roll back user %s: ", $sipuser);
214 exec_apinode("alias/remove", { "alias" => $mail });
215 exec_apinode("user/remove", { "user" => $sipuser });
219 { printf("e164: %s\n", $phone); }
222 $data = exec_apinode("user/update", {
224 "linetext" => $linetext,
226 if ( not $data->{'response'} eq 'ok' )
228 printf("Did not update 'linetext' element.");
232 #exec_apinode("alias/remove", { "alias" => $mail });
233 #exec_apinode("alias/remove", { "alias" => $phone });
234 #exec_apinode("user/remove", { "user" => $sipuser });
237 ################################################################################################
243 my ( $response, $data );
245 $session = "" if not defined $session;
246 $auth_key = "" if not defined $auth_key;
247 my $url = $config->api_url . "/" . $node;
249 $param->{'session'} = $session;
250 $param->{'auth_key'} = $auth_key;
252 $response = $g_ua->post( $url, $param );
253 if ( $response->is_success )
255 if ( $response->content =~ m/\s*{/ )
257 $data = decode_json( $response->content);
261 $data = $response->content;
270 my $response = $g_ua->post( $config->api_url . "/auth/login",
271 [ "api_key" => $api_key ] );
273 my $data = decode_json( $response->content) if $response->is_success;
274 die("HTTP error") unless $response->is_success;
276 if ( $data->{'response'} eq "ok" )
278 $session = $data->{'session'};
279 $auth_key = $data->{'auth_key'};
283 print "Unable to log in to Hermes API\n";
286 undef $data; undef $response;
291 my $response = $g_ua->post( $config->api_url . "/auth/logout",
292 [ "session" => $session ] );
293 die("HTTP error") unless $response->is_success;
294 undef $session; undef $auth_key;