]> git.defcon.no Git - hermes/blob - guc-clients/showsipuser
First stab at a naive permissions-tool
[hermes] / guc-clients / showsipuser
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 use Data::Dumper;
17
18 my $utf2iso = Text::Iconv->new("utf-8","latin1");
19 my $api_key;
20
21 my $help;
22 my $username;
23 my $configfile = undef;
24 my $dryrun = 0;
25 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
26 my ($user, $displayname, $phone, $mail, $sipuser, $linetext);
27
28 my $config = AppConfig->new({ CREATE => 1 });
29
30 $config->define("api_url=s");
31 $config->define("api_keyfile=s");
32
33 $config->define("default_domain=s");
34 $config->default_domain("hig.no");
35
36 foreach (
37 "/usr/local/etc/hermes/hermes_config",
38 "/usr/local/etc/hermes/config",
39 "/etc/hermes/config",
40 $ENV{"HOME"} . "/.hermes/config",
41 $ENV{"HOME"} . "/.hermes_config",
42 ) { $configfile = $_ if ( -f $_ ); }
43
44 GetOptions(
45 "help" => \$help,
46 "username=s" => \$username,
47 "configfile=s" => \$configfile,
48 );
49
50 if (
51 (not $username) ||
52 (not $configfile) ||
53 (( $configfile ) && ( not -f $configfile ))
54 )
55 {
56 $help = 1;
57 }
58
59 $config->file( $configfile );
60
61 if ( ( not $config->api_url ) ||
62 ( not $config->api_keyfile ) ||
63 ( not $config->default_domain ) ||
64 ( $config->api_keyfile && not -f $config->api_keyfile )
65 )
66 {
67 $help = 1;
68 }
69
70 if ( $help ) {
71 print <<END_HELP;
72 Verify that the following options are set:
73 --configfile=s|--conf|-c path to hermes_config
74 --username=s|--user|-u username, in 'user' or 'user\@example.com' form
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 $g_ua = LWP::UserAgent->new;
86 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
87
88 $username = $username . "@" . $config->default_domain if ( not $username =~ m/\w\@\w/ );
89
90 login_apikey();
91
92 $data = exec_apinode("user/get", { 'user' => $username });
93 if ( not $data->{'response'} eq 'ok' )
94 {
95 printf("Unable to fetch user: %s\n", $data->{'cause'});
96 exit;
97 }
98
99 my $user = $data->{'user'};
100
101 printf("\n");
102 printf("Subscriber....: %s\n", $username);
103 printf("Displayname...: %s\n", $user->{'displayname'});
104 printf("E-mail........: %s\n", $user->{'email'});
105 printf("\n");
106 printf("Auth username.: %s\n", $user->{'authid'});
107 printf("Auth password.: %s\n", $user->{'password'});
108 printf("Domain........: %s\n", $user->{'domain'});
109 printf("Registrar.....: %s\n", $user->{'registrar'});
110 printf("Proxy.........: %s\n", $user->{'proxy'});
111 printf("Permissions...: %s\n", $user->{'permittedcalls'});
112 printf("Aliases:\n");
113 my $alias_data = exec_apinode("alias/list", { 'destination' => $username });
114 if ( $alias_data->{'response'} eq 'ok' )
115 {
116 #print Dumper($alias_data);
117 my $count = 0;
118 my $t = $alias_data->{'aliases'};
119 foreach my $a ( sort {lc $a->{'alias'} cmp lc $b->{'alias'}} @$t )
120 {
121 printf("\t\t%s\n", $a->{'alias'});
122 $count++;
123 }
124 printf("\t\t None\n") if ( $count < 1 );
125 }
126 else
127 {
128 printf("\t\t None\n");
129 }
130 printf("Phones:\n");
131 my $phone_data = exec_apinode("phone/get", { 'user' => $username });
132 if ( $phone_data->{'response'} eq 'ok' )
133 {
134 my $t = $phone_data->{'list'};
135 my $count = 0;
136 foreach my $p ( @$t )
137 {
138 printf("\t\t%s\n", $p );
139 $count++;
140 }
141 printf("\t\t None\n") if ( $count < 1 );
142 }
143 else
144 {
145 printf("\t None\n");
146 }
147
148 printf("\n");
149
150 undef $data;
151
152 logout();
153 ################################################################################################
154 sub exec_apinode($$)
155 {
156 my $node = shift;
157 my $param = shift;
158
159 my ( $response, $data );
160
161 $session = "" if not defined $session;
162 $auth_key = "" if not defined $auth_key;
163 my $url = $config->api_url . "/" . $node;
164
165 $param->{'session'} = $session;
166 $param->{'auth_key'} = $auth_key;
167
168 $response = $g_ua->post( $url, $param );
169 if ( $response->is_success )
170 {
171 if ( $response->content =~ m/\s*{/ )
172 {
173 $data = decode_json( $response->content);
174 }
175 else
176 {
177 $data = $response->content;
178 }
179
180 }
181 return $data;
182 }
183
184 sub login_apikey
185 {
186 my $response = $g_ua->post( $config->api_url . "/auth/login",
187 [ "api_key" => $api_key ] );
188
189 my $data = decode_json( $response->content) if $response->is_success;
190 die("HTTP error") unless $response->is_success;
191
192 if ( $data->{'response'} eq "ok" )
193 {
194 $session = $data->{'session'};
195 $auth_key = $data->{'auth_key'};
196 }
197 else
198 {
199 print "Unable to log in to Hermes API\n";
200 exit;
201 }
202 undef $data; undef $response;
203 }
204
205 sub logout
206 {
207 my $response = $g_ua->post( $config->api_url . "/auth/logout",
208 [ "session" => $session ] );
209 die("HTTP error") unless $response->is_success;
210 undef $session; undef $auth_key;
211 }