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