Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login

    Email Notification - OpenVPN Client Connect (Common Name)

    OpenVPN
    20
    138
    39.0k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      serbus
      last edited by

      Hello!

      @PM_13 said in Email Notification - OpenVPN Client Connect (Common Name):

      still cannot figure out why /root/notify.sh execution from command line is not working as it should

      It could be a tcsh-shell-shebang-something issue.

      What about running just:

      /root/notify.sh
      

      John

      Lex parsimoniae

      P 1 Reply Last reply Reply Quote 1
      • P
        PM_13 @serbus
        last edited by

        @serbus Yes that solved the issue and script executes from command line!
        Thanks @Gertjan for all the pointers!

        1 Reply Last reply Reply Quote 0
        • M
          mrvanity
          last edited by

          Hello there!
          The notifications works just fine to my system as well.
          Thanks for that!

          I have a couple of questions.

          1. Can i exclude a specific user from the notifications?
            I mean that i want to be notified for all users connecting , except one.

          2. Is there a chance to modify the script to push the data to a MySql instead of sending emails?

          My regards.

          GertjanG 1 Reply Last reply Reply Quote 0
          • GertjanG
            Gertjan @mrvanity
            last edited by

            @mrvanity said in Email Notification - OpenVPN Client Connect (Common Name):

            Can i exclude a specific user from the notifications?

            The script is PHP ...., so yes, you can.
            But because you asked the question, you can't.
            That is, for now ;)
            PHP is a very old, very know "language", something like Basic in the eighties and nineties. The entire pfSense GUI is written using it (95 % or more). As half or more of all the web content on the net is produced by ... PHP.

            This is the "notify.sh" I was using in the past :

            #!/usr/local/bin/php -q
            <?php
                    require_once("/etc/inc/notices.inc");
                    $local_connect_value = " user_name: " . getenv('common_name') . " vpn_client_ip: " . getenv('ifconfig_pool_remote_ip') . " on " . date('F j, Y, g:i a');
                    if ( strrchr (__FILE__ , 'disconnect') ) {
                    $local_connect_value .= ", during : " . getenv('time_duration') . " seconds, received : " . getenv('bytes_received') . " bytes, send : " . getenv('bytes_sent') ." bytes. DISCONNRECTED.";
                    }
                    notify_all_remote($local_connect_value);
            ?>
            

            The OpenVPN server process, before it call's our script called "notify.sh", initializes some environment variables, so the script can act upon them.

            Sop, step 1 : Goto the OpenVPN server doc, and see what openvpn is all about, and, most important : what goes on when you do this :

            58b44195-b845-4767-a686-6f9208473808-image.png

            then you will know what info is at the scripts disposal.
            The getenv() PHP function will get this info.
            Now you can chose what info you want to mail.

            Btw :I'm actually getitng the 'common_name' (is that the user name ?), so you could :

            .....
            if (getenv('common_name') != "Paul")
            {
             // send all info because it's not Paul //
            }
            ...
            

            No "help me" PM's please. Use the forum, the community will thank you.
            Edit : and where are the logs ??

            noplanN 1 Reply Last reply Reply Quote 0
            • noplanN
              noplan @Gertjan
              last edited by

              @Gertjan said in Email Notification - OpenVPN Client Connect (Common Name):

              if (getenv('common_name') != "Paul")

              didnt we had this discussion some time ago
              with sending mail rports only when IP / user is part of a group ...

              have to look it up
              br from a sunny vacation day NP

              1 Reply Last reply Reply Quote 0
              • M
                mrvanity
                last edited by mrvanity

                Hello,
                Thank you for your answers!
                I modified the script to the one below

                The vpn server restarted normally, so i will check with the spesified user and report back.

                #!/usr/local/bin/php -q
                <?php
                	require_once("/etc/inc/notices.inc");
                	$local_connect_value = " \n user_name: " . getenv('common_name') . " \n vpn_client_ip: " . getenv('ifconfig_pool_remote_ip') ." connected from " . getenv('trusted_ip') . " on " . date('F j, Y, g:i a');
                	if ( strrchr (__FILE__ , 'disconnect') && (getenv('common_name') != "Paul")) {
                	$local_connect_value .= ", \n duration : " . round(((getenv('time_duration'))/3600),2) . "  hours, or " . round(((getenv('time_duration'))/60),2) . "  minutes, or " . getenv('time_duration') . "  seconds,\n upload from vpn-client (received) : " . round(((getenv('bytes_received'))/1048576),2) . " MB, \n download to vpn-client (send) : " . round(((getenv('bytes_sent'))/1048576),2) ." MB. \n DISCONNECTED.";
                notify_all_remote($local_connect_value);
                }
                ?>
                
                

                You think that the modification to send the data to a MySql DB is a doable process?

                My regards.

                GertjanG 1 Reply Last reply Reply Quote 0
                • GertjanG
                  Gertjan @mrvanity
                  last edited by

                  @mrvanity said in Email Notification - OpenVPN Client Connect (Common Name):

                  MySql DB

                  pfSense doesn't have the PHP extension that supports MySQL transactions installed by default.
                  You can check that by running :

                  php -m
                  

                  I have the mysqld module, probably because I installed th pfSense Freeradius package, which includes a mysql backend.
                  You could also install it manually with the pkg install ..... command, as it exists on the pfSense-FreeBSD package server.

                  So yes, it's very possible.

                  Btw : This :

                  @mrvanity said in Email Notification - OpenVPN Client Connect (Common Name):

                  FILE

                  is somewhat special.
                  I executed

                  ln -s notify.sh disconnect.sh
                  

                  so disconnect.sh is a symbolic link to connect.sh

                  lrwxr-xr-x   1 root     wheel           9 Oct 10 11:48 disconnect.sh -> notify.sh
                  

                  Now I can use the vpn server command

                  client-disconnect /root/disconnect.sh
                  

                  The FILE instruction detects that the disconnect.sh is called.

                  Btw : it should be 'disconnect.sh', not 'disconnect'

                  This :
                  if ( strrchr (FILE , 'disconnect.sh') && (getenv('common_name') != "Paul"))
                  becomes true if the user Paul disconnects, and thus the file disconnect.sh was called.
                  The VPN server should be instructed to call that script on disconnect.

                  Got it ?

                  Btw : everyting is to be tested of course ^^

                  No "help me" PM's please. Use the forum, the community will thank you.
                  Edit : and where are the logs ??

                  1 Reply Last reply Reply Quote 0
                  • P
                    PM_13
                    last edited by PM_13

                    So following scripts seems to be working for me:

                    #!/usr/local/bin/php -q
                    <?php
                    	require_once("/etc/inc/notices.inc");
                    	$local_connect_value = " user_name: " . getenv('common_name') . " vpn_client_ip: " . getenv('ifconfig_pool_remote_ip') . " from: " . getenv('trusted_ip') . " on " . date('F j, Y, g:i a');
                    	if ( strrchr (__FILE__ , 'disconnect') ) {
                    	$local_connect_value .= ", duration : " . getenv('time_duration') . " seconds, received : " . getenv('bytes_received') . " bytes, send : " . getenv('bytes_sent') ." bytes. DISCONNECTED.";
                    	}
                    	notify_all_remote($local_connect_value);
                    ?>
                    
                    

                    If I want to add a subject line or add a line in the body field, what would be the best way to do it? I am asking here as I have no knowledge of PHP coding so can use some help.
                    Thanks.

                    1 Reply Last reply Reply Quote 0
                    • S
                      serbus
                      last edited by serbus

                      Hello!

                      You can add content to the end of the current message body with a line like:

                      $local_connect_value .= "\nMore info\n";
                      

                      The notify_all_remote will also send the message to telegram and pushover, but if you only need the smtp message you can use this instead and add a subject :

                      send_smtp_message($local_connect_value, "Subject text", 1);
                      

                      The last parameter 1 is a force value that controls message deduplication, etc...

                      John

                      Lex parsimoniae

                      P 1 Reply Last reply Reply Quote 0
                      • P
                        PM_13 @serbus
                        last edited by

                        @serbus

                        Thanks John!!
                        It worked like a treat, appreciate the pointers 👍 😄

                        1 Reply Last reply Reply Quote 0
                        • noplanN
                          noplan
                          last edited by

                          Cuz im late to the party...
                          So cool Thx... Gonna test it!!

                          1 Reply Last reply Reply Quote 0
                          • B
                            boggie1688
                            last edited by boggie1688

                            Thanks for this thread!

                            1 Reply Last reply Reply Quote 0
                            • B
                              boggie1688
                              last edited by boggie1688

                              This might be a big ask, but is there a way to get GEOIP info for each IP address connecting to the VPN server? http://geoiplookup.net/xml-api/

                              It would be nice to have this info populate into the email.

                              P 1 Reply Last reply Reply Quote 0
                              • P
                                PM_13 @boggie1688
                                last edited by

                                @boggie1688 I tried MaxMind service a while back but that was almost two years back, you may want to check it out.

                                B 1 Reply Last reply Reply Quote 0
                                • B
                                  boggie1688 @PM_13
                                  last edited by

                                  @PM_13

                                  So I don't know PHP at all, I barely can program hello world. However, I did manage this.

                                  #!/usr/local/bin/php -q
                                  <?php

                                  require_once("/etc/inc/notices.inc");
                                      $queryprefix = "http://api.geoiplookup.net/?query=";
                                      $ip= getenv('trusted_ip');
                                      $urlip =($queryprefix.$ip);
                                      $xml = file_get_contents($urlip);
                                  $local_connect_value = " \n user_name: " . getenv('common_name') . " \n vpn_client_ip: " . getenv('ifconfig_pool_remote_ip') ." connected from " . getenv('trusted_ip') . " on " . date('F j, Y, g:i a') . $xml;
                                  if ( strrchr (__FILE__ , 'disconnect') ) {
                                  $local_connect_value .= ", \n duration : " . round(((getenv('time_duration'))/3600),2) . "  hours, or " . round(((getenv('time_duration'))/60),2) . "  minutes, or " . getenv('time_duration') . "  seconds,\n upload from vpn-client (received) : " . round(((getenv('bytes_received'))/1048576),2) . " MB, \n download to vpn-client (send) : " . round(((getenv('bytes_sent'))/1048576),2) ." MB. \n DISCONNECTED.";
                                  }
                                      $user_remote_ip = "VPN User ". getenv('common_name')." connected from " . getenv('trusted_ip');
                                      send_smtp_message($local_connect_value, $user_remote_ip, 1);
                                  

                                  ?>

                                  It grabs the incoming ip address, and looks it up with geoiplookup.net's xml api. Then prints it into the email.

                                  I really would like to clean up the way it prints. given it just dumps the results from the page. But this is the end of my coding ability, pretty short huh?

                                  I can see the XML has tags <IP><HOST><ISP> etc etc, but I don't really know how just print the tags that I want.

                                  P 1 Reply Last reply Reply Quote 0
                                  • P
                                    PM_13 @boggie1688
                                    last edited by

                                    @boggie1688 said in Email Notification - OpenVPN Client Connect (Common Name):

                                    PM_13

                                    @serbus helped me with php syntax to add additional line of text in the email notification (whether vpn client is connecting or disconnecting in a particular event). I don't know php either but John's recommendations were spot on. John thanks again!!

                                    1 Reply Last reply Reply Quote 0
                                    • S
                                      serbus
                                      last edited by

                                      Hello!

                                      Once you have your $xml response you could try something like:

                                      $country = 'Unknown';
                                      if (preg_match ('/<countryname>(.*?)<\/countryname>/', $xml, $match))
                                         {
                                         $country = $match [1];
                                         }
                                      

                                      John

                                      Lex parsimoniae

                                      B 1 Reply Last reply Reply Quote 1
                                      • GertjanG
                                        Gertjan
                                        last edited by

                                        To all : Test your VPN notification first !!

                                        Go outside, de activate the Wifi, activate the VPN , take a big walk, swim, drive or best : take a plane.

                                        When you get back, you'll be in for a surprise.
                                        This surprise includes :
                                        The huge number of mails you received.
                                        Maybe also the mail from your mail host that states that they detect a spam bomb attack .... and that they stopped treating further mails from this 'source'.
                                        You used another outside service : Leave your chair, take a big walk, swim, drive or best : take a plane. ? Fine : they will blacklist you, as their server has to be protected.
                                        Etc.

                                        It's not only about knowing what PHP is : that's an easy one : you wana drive that car ? => Go for the license (and insurance, maintenance, etc)

                                        I'll leave this question open for now : Why did you receive hundreds, if not thousands of nearly 'notification' identical mails ?

                                        The code given as an example above has to be .... worked on.
                                        For example : the call to "geoiplookup.net" : it should be used wisely :
                                        When a the VPN connect script is called, the IP obtained should be compared with the content of a file. this file contains all the IPs found in VPN connect messages.
                                        If the IP is found : do nothing, as the IP is know.
                                        The IP is not in the file : have it looked up, and send the mail.

                                        Now you will receive far less mails, and no one will complain.

                                        And I have a question : Normally, VPN access is given to people that you some how trust.
                                        Why GEO locate them ?
                                        Your kids ?
                                        And then there is the eternal discussion that GEOIP data is quiet useless.
                                        There are no more free IPv4s, it became a rare resource. The last blocks are sold often, broken up, sold again, from anywhere to anywhere. Their actual location isn't really known any more.
                                        And take Apple and IBM for example : back then, they bought and entire /8 (or close to that). And all their IP's are in (example) Redmond, although used world wide.
                                        With IPv6 thinks get better : You can forget the notion of GEOIP as no one can build the database that can hold all the details : there are not enough resources on this planet left to do so. Maintaining and giving access to this database will be even more difficult.

                                        No "help me" PM's please. Use the forum, the community will thank you.
                                        Edit : and where are the logs ??

                                        noplanN B 2 Replies Last reply Reply Quote 1
                                        • noplanN
                                          noplan @Gertjan
                                          last edited by

                                          WORD !

                                          1 Reply Last reply Reply Quote 0
                                          • B
                                            boggie1688 @serbus
                                            last edited by

                                            @serbus Thank you for this, this would have taken me ages to figure out. Specifically the function preg_match. I was googling around and though perhaps XPATH was the right function.

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.