]>
git.defcon.no Git - hermes/blob - guc-clients/feide2sipuser
48e20c8a207c083b31a3c60c0b845a3b2e88fab7
6 use Net
::LDAP
::Control
::Paged
;
7 use Net
::LDAP
::Constant
qw( LDAP_CONTROL_PAGED );
17 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
18 my ($user, $displayname, $phone, $mail, $sipuser, $linetext);
20 my $config = AppConfig
->new({ CREATE
=> 1 });
22 $config->define("api_url=s");
23 $config->define("api_keyfile=s");
24 $config->define("feide_server=s");
25 $config->define("feide_login=s");
26 $config->define("feide_passwd=s");
27 $config->define("feide_people=s");
29 $config->define("numbers_local_prefix=s");
30 $config->define("numbers_local_series=s");
31 $config->define("numbers_countrycode=s");
35 "username=s" => \
$username,
36 "configfile=s" => \
$configfile,
41 (( $configfile ) && ( not -f
$configfile ))
47 $config->file( $configfile );
49 if ( ( not $config->api_url ) ||
50 ( not $config->api_keyfile ) ||
51 ( $config->api_keyfile && not -f
$config->api_keyfile ) ||
52 ( not $config->feide_server ) ||
53 ( not $config->feide_login ) ||
54 ( not $config->feide_passwd ) ||
55 ( not $config->feide_people ) )
62 Verify that the following options are set:
66 Verify the contents of the configuration file.
67 Verify that the key-file exists.
71 open KEY, "<" . $config->api_keyfile;
72 chomp( $api_key = <KEY> );
75 if ( not $username =~ m/\w+/ )
76 { print "Illegal username\n"; exit; }
79 # Kobler til LDAP-server
80 my $ldap = Net::LDAP->new($config->feide_server, version => 3)
81 or die "Can't connect to LDAP server: " . $config->feide_server . "!\n";
82 my $msg = $ldap->bind (dn => $config->feide_login, password => $config->feide_passwd);
84 die ("LDAP error: ", $msg->error_text((), "\n"));
87 $msg = $ldap->search( base => $config->feide_people,
89 filter => "(uid=". $username .")",
91 die("Getting information for " . $username . " failed..." ) if ( $msg->count != 1 );
94 $displayname = $user->get_value("displayName");
95 $phone = $user->get_value("telephoneNumber");
96 $mail = $user->get_value("mail");
97 $sipuser = $user->get_value("eduPersonPrincipalName");
99 my $short_test = "^" . $config->numbers_local_series;
100 my $long_test = "^" . $config->numbers_local_prefix . $config->numbers_local_series;
102 if ( $phone =~ m/$short_test/ )
104 print "Phone number is in 5-digit local format. Completing E164.\n";
105 $linetext = $config->numbers_local_prefix . $phone;
106 $phone = $config->numbers_countrycode . $config->numbers_local_prefix . $phone;
108 elsif ( $phone =~ m/$long_test/ )
110 print "Phone number is in 8-digit local format. Adding CC.\n" ;
112 $phone = $config->numbers_countrycode . $phone;
116 print "Unable to recognize Phone number. Check FEIDE\n";
120 print "Data so far:" . "\n";
121 print " Username: " . $username . "\n";
122 print " Displayname: " . $displayname . "\n";
123 print " Phone: " . $phone . "\n";
124 print " Line-text: " . $linetext . "\n";
125 print " Mail: " . $mail . "\n";
126 print " SIP address: " . $sipuser . "\n";
128 $msg = $ldap->unbind;
129 if ( $msg->code()) { die ("LDAP error: ", $msg->error_text((), "\n")); }
131 $g_ua = LWP::UserAgent->new;
132 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
136 $data = exec_apinode("user/available", { "user" => $sipuser });
137 if ( not $data->{'response'} eq 'ok' )
139 printf("Unable to add user, Hermes response to available query is: %s\n", $data->{'cause'});
144 $data = exec_apinode("user/add_local", {
146 "displayname" => $displayname,
149 if ( not $data->{'response'} eq 'ok' )
151 printf("Unable to add user, Hermes response to add_local query is: %s\n", $data->{'cause'});
156 printf("Added user, login information:\n");
157 printf("username: %s@%s\nauthid: %s\npassword: %s\n" .
158 "registrar: %s:%d\nproxy: %s:%d\ndisplayname: %s\n" .
159 "email: %s\npermission: %s\n",
160 $data->{'user'}->{'username'},
161 $data->{'user'}->{'domain'},
162 $data->{'user'}->{'authid'},
163 $data->{'user'}->{'password'},
164 $data->{'user'}->{'registrar'},
165 $data->{'user'}->{'r_port'},
166 $data->{'user'}->{'proxy'},
167 $data->{'user'}->{'p_port'},
168 $data->{'user'}->{'displayname'},
169 $data->{'user'}->{'email'},
170 $data->{'user'}->{'permittedcalls'},
172 $domain = $data->{'user'}->{'domain'},
173 $phone .= "\@" . $domain;
177 unless ( $mail eq $sipuser )
179 $data = exec_apinode("alias/add", {
180 "destination" => $sipuser,
183 if ( not $data->{'response'} eq 'ok' )
185 printf("Unable to add alias, Hermes response to add_local query is: %s\n", $data->{'cause'});
186 printf("Attempting to roll back user %s: ", $sipuser);
187 undef $data; $data = exec_apinode("user/remove", { "user" => $sipuser });
188 printf("%s\n", $data->{'response'});
192 { printf("alias: %s\n", $mail); }
196 $data = exec_apinode("alias/add", {
197 "destination" => $sipuser,
200 if ( not $data->{'response'} eq 'ok' )
202 printf("Unable to add E164 number, Hermes response to add_local query is: %s\n", $data->{'cause'});
203 printf("Attempting to roll back user %s: ", $sipuser);
204 exec_apinode("alias/remove", { "alias" => $mail });
205 exec_apinode("user/remove", { "user" => $sipuser });
209 { printf("e164: %s\n", $phone); }
212 $data = exec_apinode("user/update", {
214 "linetext" => $linetext,
216 if ( not $data->{'response'} eq 'ok' )
218 printf("Did not update 'linetext' element.");
222 #exec_apinode("alias/remove", { "alias" => $mail });
223 #exec_apinode("alias/remove", { "alias" => $phone });
224 #exec_apinode("user/remove", { "user" => $sipuser });
227 ################################################################################################
233 my ( $response, $data );
235 $session = "" if not defined $session;
236 $auth_key = "" if not defined $auth_key;
237 my $url = $config->api_url . "/" . $node;
239 $param->{'session'} = $session;
240 $param->{'auth_key'} = $auth_key;
242 $response = $g_ua->post( $url, $param );
243 if ( $response->is_success )
245 if ( $response->content =~ m/\s*{/ )
247 $data = decode_json( $response->content);
251 $data = $response->content;
260 my $response = $g_ua->post( $config->api_url . "/auth/login",
261 [ "api_key" => $api_key ] );
263 my $data = decode_json( $response->content) if $response->is_success;
264 die("HTTP error") unless $response->is_success;
266 if ( $data->{'response'} eq "ok" )
268 $session = $data->{'session'};
269 $auth_key = $data->{'auth_key'};
273 print "Unable to log in to Hermes API\n";
276 undef $data; undef $response;
281 my $response = $g_ua->post( $config->api_url . "/auth/logout",
282 [ "session" => $session ] );
283 die("HTTP error") unless $response->is_success;
284 undef $session; undef $auth_key;