]> git.defcon.no Git - hermes/blob - guc-clients/listsipusers
First stab at a naive permissions-tool
[hermes] / guc-clients / listsipusers
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 $api_key;
17
18 my $help;
19 my $username;
20 my $configfile = undef;
21 my $dryrun = 0;
22 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
23 my ($user, $displayname, $phone, $mail, $sipuser, $linetext);
24
25 my $config = AppConfig->new({ CREATE => 1 });
26
27 $config->define("api_url=s");
28 $config->define("api_keyfile=s");
29
30 foreach (
31 "/usr/local/etc/hermes/hermes_config",
32 "/usr/local/etc/hermes/config",
33 "/etc/hermes/config",
34 $ENV{"HOME"} . "/.hermes/config",
35 $ENV{"HOME"} . "/.hermes_config",
36 ) { $configfile = $_ if ( -f $_ ); }
37
38 GetOptions(
39 "help" => \$help,
40 "configfile=s" => \$configfile,
41 );
42
43 if (
44 (not $configfile) ||
45 (( $configfile ) && ( not -f $configfile ))
46 )
47 {
48 $help = 1;
49 }
50
51 $config->file( $configfile );
52
53 if ( ( not $config->api_url ) ||
54 ( not $config->api_keyfile ) ||
55 ( $config->api_keyfile && not -f $config->api_keyfile )
56 )
57 {
58 $help = 1;
59 }
60
61 if ( $help ) {
62 print <<END_HELP;
63 Verify that the following options are set:
64 --configfile=s
65
66 Verify the contents of the configuration file.
67 Verify that the key-file exists.
68 END_HELP
69 exit; }
70
71 open KEY, "<" . $config->api_keyfile;
72 chomp( $api_key = <KEY> );
73 close KEY;
74
75 $g_ua = LWP::UserAgent->new;
76 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
77
78 login_apikey();
79
80 $data = exec_apinode("user/list", { });
81 if ( not $data->{'response'} eq 'ok' )
82 {
83 printf("Unable to fetch user list: %s\n", $data->{'cause'});
84 exit;
85 }
86
87 my $t = $data->{'list'};
88 my @users = @$t;
89
90 foreach my $u ( @users )
91 {
92 printf("%-24s", $u->{'user'});
93 printf("%-12s", $u->{'status'});
94 my $alias_data = exec_apinode("alias/list", { 'destination' => $u->{'user'} });
95 if ( $alias_data->{'response'} eq 'ok' )
96 {
97 #print Dumper($alias_data);
98 my $t = $alias_data->{'aliases'};
99 foreach my $a ( sort {lc $a->{'alias'} cmp lc $b->{'alias'}} @$t )
100 {
101 printf("\t%-24s", $a->{'alias'});
102 }
103 }
104 else
105 {
106 }
107 print "\n";
108 }
109
110 undef $data;
111
112 logout();
113 ################################################################################################
114 sub exec_apinode($$)
115 {
116 my $node = shift;
117 my $param = shift;
118
119 my ( $response, $data );
120
121 $session = "" if not defined $session;
122 $auth_key = "" if not defined $auth_key;
123 my $url = $config->api_url . "/" . $node;
124
125 $param->{'session'} = $session;
126 $param->{'auth_key'} = $auth_key;
127
128 $response = $g_ua->post( $url, $param );
129 if ( $response->is_success )
130 {
131 if ( $response->content =~ m/\s*{/ )
132 {
133 $data = decode_json( $response->content);
134 }
135 else
136 {
137 $data = $response->content;
138 }
139
140 }
141 return $data;
142 }
143
144 sub login_apikey
145 {
146 my $response = $g_ua->post( $config->api_url . "/auth/login",
147 [ "api_key" => $api_key ] );
148
149 my $data = decode_json( $response->content) if $response->is_success;
150 die("HTTP error") unless $response->is_success;
151
152 if ( $data->{'response'} eq "ok" )
153 {
154 $session = $data->{'session'};
155 $auth_key = $data->{'auth_key'};
156 }
157 else
158 {
159 print "Unable to log in to Hermes API\n";
160 exit;
161 }
162 undef $data; undef $response;
163 }
164
165 sub logout
166 {
167 my $response = $g_ua->post( $config->api_url . "/auth/logout",
168 [ "session" => $session ] );
169 die("HTTP error") unless $response->is_success;
170 undef $session; undef $auth_key;
171 }