Subversion Repositories sysadmin_scripts

Rev

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

Rev 176 Rev 177
Line 29... Line 29...
29
#       the logs, looking for. See sub verifyUp for details.
29
#       the logs, looking for. See sub verifyUp for details.
30
#    v0.2.2 - 20250430 RWR
30
#    v0.2.2 - 20250430 RWR
31
#       If user enters a partial connection name (ie, doesn't match subdirectory of
31
#       If user enters a partial connection name (ie, doesn't match subdirectory of
32
#       $configDirs), use connection name as a filter to match existing. Basically
32
#       $configDirs), use connection name as a filter to match existing. Basically
33
#       a poor man's search function
33
#       a poor man's search function
-
 
34
#    v0.3.0 - 20250922 RWR
-
 
35
#       Added ability to run a script in the openvpn config directory prior to making
-
 
36
#       the vpn connection. Basically targeting TOTP connections, where we might want
-
 
37
#       to use oathtool to generate the key. hardcoded as preconnect, in the configuration
-
 
38
#       directory.
-
 
39
#       can also be set using the --pre or -p flags
34
 
40
 
35
# On secure systems, you can set this up with the setuid bit to run as root
41
# On secure systems, you can set this up with the setuid bit to run as root
36
# chown root:root vpn
42
# chown root:root vpn
37
# chmod u+s vpn
43
# chmod u+s vpn
38
 
44
 
Line 45... Line 51...
45
 
51
 
46
my $configDirs = '/etc/openvpn';
52
my $configDirs = '/etc/openvpn';
47
my $logDir = '/var/log/openvpn';
53
my $logDir = '/var/log/openvpn';
48
my $pidDir = '/var/run/openvpn';
54
my $pidDir = '/var/run/openvpn';
49
my $statusDir = '/var/run/openvpn';
55
my $statusDir = '/var/run/openvpn';
-
 
56
my $preConnectScript = 'preconnect';
50
my $timeOut = 60 * 60; # number of seconds of inactivity to close session
57
my $timeOut = 60 * 60; # number of seconds of inactivity to close session
51
 
58
 
52
# These variables are for getOpt, and control the operation of the script
59
# These variables are for getOpt, and control the operation of the script
53
# I left them all global
60
# I left them all global
54
my $kill = '';
61
my $kill = '';
Line 57... Line 64...
57
my $quiet = '';
64
my $quiet = '';
58
my $verbose = '';
65
my $verbose = '';
59
my $help = 0;
66
my $help = 0;
60
my $man = 0;
67
my $man = 0;
61
my $chdir = 0;
68
my $chdir = 0;
-
 
69
my $preConnectScript = 'preconnect';
62
 
70
 
63
 
71
 
64
# check if Directories exist and, if root, creates them if needed 
72
# check if Directories exist and, if root, creates them if needed 
65
sub validateDirectories {
73
sub validateDirectories {
66
   my @errors;
74
   my @errors;
Line 192... Line 200...
192
 
200
 
193
# start a connection. Can only be done as root user.
201
# start a connection. Can only be done as root user.
194
sub startConnection {
202
sub startConnection {
195
   my $destination = shift;
203
   my $destination = shift;
196
   my $exitString = 'Unknown Exit Status';
204
   my $exitString = 'Unknown Exit Status';
-
 
205
   my $toRun = "$configDirs/$destination/$preConnectScript";
197
   my $configFile = "$configDirs/$destination/$destination.ovpn";
206
   my $configFile = "$configDirs/$destination/$destination.ovpn";
198
   my $p12 =  "$configDirs/$destination/$destination.p12";
207
   my $p12 =  "$configDirs/$destination/$destination.p12";
199
   return '' unless -d "$configDirs/$destination"; # they did not give a known configuration
208
   return '' unless -d "$configDirs/$destination"; # they did not give a known configuration
200
   chdir( "$configDirs/$destination" ) if $chdir;
209
   chdir( "$configDirs/$destination" ) if $chdir;
-
 
210
   # run the pre connect script, if it is here and executable
-
 
211
   if ( -e $toRun && -x $toRun ) {
-
 
212
      print "Running $toRun\n" if $verbose;
-
 
213
      `$toRun`;
-
 
214
   }
201
   if ( -f $configFile ) {
215
   if ( -f $configFile ) {
202
      # we found the config file
216
      # we found the config file
203
      if ( &getPid( $destination ) ) { # make sure it is not already running
217
      if ( &getPid( $destination ) ) { # make sure it is not already running
204
         return 'The connection was already active';
218
         return 'The connection was already active';
205
      }
219
      }
Line 262... Line 276...
262
   'start|s=s' => \$destination, 
276
   'start|s=s' => \$destination, 
263
   'timeout|t=i' => \$timeOut,
277
   'timeout|t=i' => \$timeOut,
264
   'quiet|q' => \$quiet,
278
   'quiet|q' => \$quiet,
265
   'chdir|c' => \$chdir,
279
   'chdir|c' => \$chdir,
266
   'verbose|v' => \$verbose,
280
   'verbose|v' => \$verbose,
-
 
281
   'pre|p=s' => \$preConnectScript,
267
   'help|?' => \$help,
282
   'help|?' => \$help,
268
   'man' => \$man
283
   'man' => \$man
269
);
284
);
270
 
285
 
271
pod2usage(1) if $help;
286
pod2usage(1) if $help;
Line 329... Line 344...
329
 
344
 
330
=item B<--timeout|-t> I<seconds>
345
=item B<--timeout|-t> I<seconds>
331
 
346
 
332
Set idle timeout, in seconds
347
Set idle timeout, in seconds
333
 
348
 
-
 
349
=item B<--pre|-p> I<seconds>
-
 
350
 
-
 
351
Sets a script to be executed prior to making the connection. Must be located in the config directory and executable
-
 
352
 
334
=item B<--version>
353
=item B<--version>
335
 
354
 
336
Display version information
355
Display version information
337
 
356
 
338
=item B<--chdir|-c>
357
=item B<--chdir|-c>