Subversion Repositories web_pages

Rev

Rev 16 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 16 Rev 17
Line 28... Line 28...
28
# Provides methods to interact with OPNsense routers via their API.
28
# Provides methods to interact with OPNsense routers via their API.
29
#
29
#
30
# Change History:
30
# Change History:
31
#  v1.0.0 2025-10-01 - RWR
31
#  v1.0.0 2025-10-01 - RWR
32
#    Initial version
32
#    Initial version
-
 
33
#  v1.0.1 2025-10-07 - RWR
-
 
34
#    Fixed bug where script was looking for localport, but was localPort
-
 
35
#    Fixed bug where not all users were returned because opnSense (v24.7)
-
 
36
#       does not send the username, it sends the description. Possible
-
 
37
#       programming error, but quick fix is to use username, then description
-
 
38
 
33
 
39
 
34
package opnsense;
40
package opnsense;
35
use strict;
41
use strict;
36
use warnings;
42
use warnings;
37
 
43
 
Line 128... Line 134...
128
sub getVpnUsers {
134
sub getVpnUsers {
129
    my ($self) = @_;
135
    my ($self) = @_;
130
    my $return = {};
136
    my $return = {};
131
    my $endpoint = "/api/openvpn/export/accounts/$self->{ovpnIndex}";
137
    my $endpoint = "/api/openvpn/export/accounts/$self->{ovpnIndex}";
132
    my $users = $self->apiRequest($endpoint);
138
    my $users = $self->apiRequest($endpoint);
133
    # die "In get_vpn_users, users object:\n" . Dumper($users);
139
    #print "In get_vpn_users, users object:\n" . Dumper($users); die;
134
    foreach my $user ( keys %$users ) {
140
    foreach my $user ( keys %$users ) {
135
      next unless
141
      next unless $user;
-
 
142
      # the username can be in the array users (preferable) or in the description
-
 
143
      my $username = ($users->{$user}->{'users'}->[0] ? $users->{$user}->{'users'}->[0] : $users->{$user}->{'description'} );
-
 
144
      # we do not allow usernames with spaces in our setup
-
 
145
      next if $username =~ m/ /;
136
         $user && 
146
#         $user && 
137
         $users->{$user}->{'users'} &&
147
#         $users->{$user}->{'users'} &&
138
         ref($users->{$user}->{'users'}) eq 'ARRAY'
148
#         ref($users->{$user}->{'users'}) eq 'ARRAY'
139
         && @{$users->{$user}->{'users'}} > 0;
149
#         && @{$users->{$user}->{'users'}} > 0;
140
         # only return the first user in the array
150
         # only return the first user in the array
141
      $return->{$user} = $users->{$user}->{'users'}->[0];
151
      $return->{$user} = $username;
142
    }
152
    }
143
    #die Dumper($return);
153
#    print "In get_vpn_users, return object:\n" . Dumper($return); die;
144
    return $return;
154
    return $return;
145
}
155
}
146
 
156
 
147
#-----------------------------
157
#-----------------------------
148
# getAllUsers: get all users on the system
158
# getAllUsers: get all users on the system
Line 200... Line 210...
200
       p12_password_confirm => "",
210
       p12_password_confirm => "",
201
       random_local_port => 1,
211
       random_local_port => 1,
202
       servers => "\"1\"",
212
       servers => "\"1\"",
203
       plain_config => "",
213
       plain_config => "",
204
       p12_password => "",
214
       p12_password => "",
205
       local_port => $self->{localport},
215
       local_port => $self->{localPort},
206
       cryptoapi => 0
216
       cryptoapi => 0
207
   };
217
   };
208
   $debug = 0;
218
   $debug = 0;
209
   my $return = $self->apiRequest($endpoint, 'POST', $payload);
219
   my $return = $self->apiRequest($endpoint, 'POST', $payload);
210
   return $return;
220
   return $return;