pax_global_header00006660000000000000000000000064131651305340014513gustar00rootroot0000000000000052 comment=62e86babac9f734ba031a547501cbe8e5940d83b vpnc-scripts-20171005/000077500000000000000000000000001316513053400144075ustar00rootroot00000000000000vpnc-scripts-20171005/COPYING000066400000000000000000000432541316513053400154520ustar00rootroot00000000000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. vpnc-scripts-20171005/netunshare.c000066400000000000000000000007171316513053400167340ustar00rootroot00000000000000#define _GNU_SOURCE #include #include #include #include #ifndef CLONE_NEWNET #error CLONE_NEWNET not defined #endif int main(int argc, char *argv[]) { if (argc < 1) { fprintf(stderr, "usage: %s []\n", basename(argv[0])); return 1; } argv++; if (unshare(CLONE_NEWNET) == -1) { perror("unshare"); return 1; } execv(argv[0], argv); /* Failed... */ perror("execve"); return 1; } vpnc-scripts-20171005/vpnc-script000077500000000000000000000631571316513053400166210ustar00rootroot00000000000000#!/bin/sh # # Originally part of vpnc source code: # © 2005-2012 Maurice Massar, Jörg Mayer, Antonio Borneo et al. # © 2009-2012 David Woodhouse # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # ################ # # List of parameters passed through environment #* reason -- why this script was called, one of: pre-init connect disconnect reconnect #* VPNGATEWAY -- vpn gateway address (always present) #* TUNDEV -- tunnel device (always present) #* INTERNAL_IP4_ADDRESS -- address (always present) #* INTERNAL_IP4_MTU -- mtu (often unset) #* INTERNAL_IP4_NETMASK -- netmask (often unset) #* INTERNAL_IP4_NETMASKLEN -- netmask length (often unset) #* INTERNAL_IP4_NETADDR -- address of network (only present if netmask is set) #* INTERNAL_IP4_DNS -- list of dns servers #* INTERNAL_IP4_NBNS -- list of wins servers #* INTERNAL_IP6_ADDRESS -- IPv6 address #* INTERNAL_IP6_NETMASK -- IPv6 netmask #* INTERNAL_IP6_DNS -- IPv6 list of dns servers #* CISCO_DEF_DOMAIN -- default domain name #* CISCO_BANNER -- banner from server #* CISCO_SPLIT_INC -- number of networks in split-network-list #* CISCO_SPLIT_INC_%d_ADDR -- network address #* CISCO_SPLIT_INC_%d_MASK -- subnet mask (for example: 255.255.255.0) #* CISCO_SPLIT_INC_%d_MASKLEN -- subnet masklen (for example: 24) #* CISCO_SPLIT_INC_%d_PROTOCOL -- protocol (often just 0) #* CISCO_SPLIT_INC_%d_SPORT -- source port (often just 0) #* CISCO_SPLIT_INC_%d_DPORT -- destination port (often just 0) #* CISCO_IPV6_SPLIT_INC -- number of networks in IPv6 split-network-list #* CISCO_IPV6_SPLIT_INC_%d_ADDR -- IPv6 network address #* CISCO_IPV6_SPLIT_INC_$%d_MASKLEN -- IPv6 subnet masklen # FIXMEs: # Section A: route handling # 1) The 3 values CISCO_SPLIT_INC_%d_PROTOCOL/SPORT/DPORT are currently being ignored # In order to use them, we'll probably need os specific solutions # * Linux: iptables -t mangle -I PREROUTING -j ROUTE --oif $TUNDEV # This would be an *alternative* to changing the routes (and thus 2) and 3) # shouldn't be relevant at all) # 2) There are two different functions to set routes: generic routes and the # default route. Why isn't the defaultroute handled via the generic route case? # 3) In the split tunnel case, all routes but the default route might get replaced # without getting restored later. We should explicitely check and save them just # like the defaultroute # 4) Replies to a dhcp-server should never be sent into the tunnel # Section B: Split DNS handling # 1) Maybe dnsmasq can do something like that # 2) Parse dns packets going out via tunnel and redirect them to original dns-server #env | sort #set -x # =========== script (variable) setup ==================================== PATH=/sbin:/usr/sbin:$PATH OS="`uname -s`" HOOKS_DIR=/etc/vpnc DEFAULT_ROUTE_FILE=/var/run/vpnc/defaultroute RESOLV_CONF_BACKUP=/var/run/vpnc/resolv.conf-backup SCRIPTNAME=`basename $0` # some systems, eg. Darwin & FreeBSD, prune /var/run on boot if [ ! -d "/var/run/vpnc" ]; then mkdir -p /var/run/vpnc [ -x /sbin/restorecon ] && /sbin/restorecon /var/run/vpnc fi # stupid SunOS: no blubber in /usr/local/bin ... (on stdout) IPROUTE="`which ip 2> /dev/null | grep '^/'`" if ifconfig --help 2>&1 | grep BusyBox > /dev/null; then ifconfig_syntax_inet="" else ifconfig_syntax_inet="inet" fi if [ "$OS" = "Linux" ]; then ifconfig_syntax_ptp="pointopoint" route_syntax_gw="gw" route_syntax_del="del" route_syntax_netmask="netmask" else ifconfig_syntax_ptp="" route_syntax_gw="" route_syntax_del="delete" route_syntax_netmask="-netmask" fi if [ "$OS" = "SunOS" ]; then route_syntax_interface="-interface" ifconfig_syntax_ptpv6="$INTERNAL_IP6_ADDRESS" else route_syntax_interface="" ifconfig_syntax_ptpv6="" fi grep ^hosts /etc/nsswitch.conf|grep resolve >/dev/null 2>&1 if [ $? = 0 ];then RESOLVEDENABLED=1 else RESOLVEDENABLED=0 fi if [ -r /etc/openwrt_release ] && [ -n "$OPENWRT_INTERFACE" ]; then . /etc/functions.sh include /lib/network MODIFYRESOLVCONF=modify_resolvconf_openwrt RESTORERESOLVCONF=restore_resolvconf_openwrt elif [ -x /usr/bin/busctl ] && [ ${RESOLVEDENABLED} = 1 ]; then # For systemd-resolved (version 229 and above) MODIFYRESOLVCONF=modify_resolved_manager RESTORERESOLVCONF=restore_resolved_manager elif [ -x /sbin/resolvconf ]; then # Optional tool on Debian, Ubuntu, Gentoo and FreeBSD MODIFYRESOLVCONF=modify_resolvconf_manager RESTORERESOLVCONF=restore_resolvconf_manager elif [ -x /sbin/netconfig ]; then # tool on Suse after 11.1 MODIFYRESOLVCONF=modify_resolvconf_suse_netconfig RESTORERESOLVCONF=restore_resolvconf_suse_netconfig elif [ -x /sbin/modify_resolvconf ]; then # Mandatory tool on Suse earlier than 11.1 MODIFYRESOLVCONF=modify_resolvconf_suse RESTORERESOLVCONF=restore_resolvconf_suse elif [ -x /usr/sbin/unbound-control ] && /usr/sbin/unbound-control status > /dev/null 2>&1; then MODIFYRESOLVCONF=modify_resolvconf_unbound RESTORERESOLVCONF=restore_resolvconf_unbound else # Generic for any OS MODIFYRESOLVCONF=modify_resolvconf_generic RESTORERESOLVCONF=restore_resolvconf_generic fi # =========== script hooks ================================================= run_hooks() { HOOK="$1" if [ -d ${HOOKS_DIR}/${HOOK}.d ]; then for script in ${HOOKS_DIR}/${HOOK}.d/* ; do [ -f $script ] && . $script done fi } # =========== tunnel interface handling ==================================== do_ifconfig() { if [ -n "$INTERNAL_IP4_MTU" ]; then MTU=$INTERNAL_IP4_MTU elif [ -n "$IPROUTE" ]; then MTUDEV=`$IPROUTE route get "$VPNGATEWAY" | sed -ne 's/^.*dev \([a-z0-9]*\).*$/\1/p'` MTU=`$IPROUTE link show "$MTUDEV" | sed -ne 's/^.*mtu \([[:digit:]]\+\).*$/\1/p'` if [ -n "$MTU" ]; then MTU=`expr $MTU - 88` fi fi if [ -z "$MTU" ]; then MTU=1412 fi # Point to point interface require a netmask of 255.255.255.255 on some systems if [ -n "$IPROUTE" ]; then $IPROUTE link set dev "$TUNDEV" up mtu "$MTU" $IPROUTE addr add "$INTERNAL_IP4_ADDRESS/32" peer "$INTERNAL_IP4_ADDRESS" dev "$TUNDEV" else ifconfig "$TUNDEV" ${ifconfig_syntax_inet} "$INTERNAL_IP4_ADDRESS" $ifconfig_syntax_ptp "$INTERNAL_IP4_ADDRESS" netmask 255.255.255.255 mtu ${MTU} up fi if [ -n "$INTERNAL_IP4_NETMASK" ]; then set_network_route $INTERNAL_IP4_NETADDR $INTERNAL_IP4_NETMASK $INTERNAL_IP4_NETMASKLEN fi # If the netmask is provided, it contains the address _and_ netmask if [ -n "$INTERNAL_IP6_ADDRESS" ] && [ -z "$INTERNAL_IP6_NETMASK" ]; then INTERNAL_IP6_NETMASK="$INTERNAL_IP6_ADDRESS/128" fi if [ -n "$INTERNAL_IP6_NETMASK" ]; then if [ -n "$IPROUTE" ]; then $IPROUTE -6 addr add $INTERNAL_IP6_NETMASK dev $TUNDEV else # Unlike for Legacy IP, we don't specify the dest_address # here on *BSD. OpenBSD for one will refuse to accept # incoming packets to that address if we do. # OpenVPN does the same (gives dest_address for Legacy IP # but not for IPv6). # Only Solaris needs it; hence $ifconfig_syntax_ptpv6 ifconfig "$TUNDEV" inet6 $INTERNAL_IP6_NETMASK $ifconfig_syntax_ptpv6 mtu $MTU up fi fi } destroy_tun_device() { case "$OS" in NetBSD|OpenBSD) # and probably others... ifconfig "$TUNDEV" destroy ;; FreeBSD) ifconfig "$TUNDEV" destroy > /dev/null 2>&1 & ;; esac } # =========== route handling ==================================== if [ -n "$IPROUTE" ]; then fix_ip_get_output () { sed -e 's/ /\n/g' | \ sed -ne '1p;/via/{N;p};/dev/{N;p};/src/{N;p};/mtu/{N;p}' } set_vpngateway_route() { $IPROUTE route add `$IPROUTE route get "$VPNGATEWAY" | fix_ip_get_output` $IPROUTE route flush cache } del_vpngateway_route() { $IPROUTE route $route_syntax_del "$VPNGATEWAY" $IPROUTE route flush cache } set_default_route() { $IPROUTE route | grep '^default' | fix_ip_get_output > "$DEFAULT_ROUTE_FILE" $IPROUTE route replace default dev "$TUNDEV" $IPROUTE route flush cache } set_network_route() { NETWORK="$1" NETMASK="$2" NETMASKLEN="$3" $IPROUTE route replace "$NETWORK/$NETMASKLEN" dev "$TUNDEV" $IPROUTE route flush cache } reset_default_route() { if [ -s "$DEFAULT_ROUTE_FILE" ]; then $IPROUTE route replace `cat "$DEFAULT_ROUTE_FILE"` $IPROUTE route flush cache rm -f -- "$DEFAULT_ROUTE_FILE" fi } del_network_route() { NETWORK="$1" NETMASK="$2" NETMASKLEN="$3" $IPROUTE route $route_syntax_del "$NETWORK/$NETMASKLEN" dev "$TUNDEV" $IPROUTE route flush cache } set_ipv6_default_route() { # We don't save/restore IPv6 default route; just add a higher-priority one. $IPROUTE -6 route add default dev "$TUNDEV" metric 1 $IPROUTE -6 route flush cache } set_ipv6_network_route() { NETWORK="$1" NETMASKLEN="$2" $IPROUTE -6 route replace "$NETWORK/$NETMASKLEN" dev "$TUNDEV" $IPROUTE route flush cache } reset_ipv6_default_route() { $IPROUTE -6 route del default dev "$TUNDEV" $IPROUTE route flush cache } del_ipv6_network_route() { NETWORK="$1" NETMASKLEN="$2" $IPROUTE -6 route del "$NETWORK/$NETMASKLEN" dev "$TUNDEV" $IPROUTE -6 route flush cache } else # use route command get_default_gw() { # isn't -n supposed to give --numeric output? # apperently not... # Get rid of lines containing IPv6 addresses (':') netstat -r -n | awk '/:/ { next; } /^(default|0\.0\.0\.0)/ { print $2; }' } set_vpngateway_route() { route add -host "$VPNGATEWAY" $route_syntax_gw "`get_default_gw`" } del_vpngateway_route() { route $route_syntax_del -host "$VPNGATEWAY" $route_syntax_gw "`get_default_gw`" } set_default_route() { DEFAULTGW="`get_default_gw`" echo "$DEFAULTGW" > "$DEFAULT_ROUTE_FILE" route $route_syntax_del default $route_syntax_gw "$DEFAULTGW" route add default $route_syntax_gw "$INTERNAL_IP4_ADDRESS" $route_syntax_interface } set_network_route() { NETWORK="$1" NETMASK="$2" NETMASKLEN="$3" del_network_route "$NETWORK" "$NETMASK" "$NETMASKLEN" route add -net "$NETWORK" $route_syntax_netmask "$NETMASK" $route_syntax_gw "$INTERNAL_IP4_ADDRESS" $route_syntax_interface } reset_default_route() { if [ -s "$DEFAULT_ROUTE_FILE" ]; then route $route_syntax_del default $route_syntax_gw "`get_default_gw`" $route_syntax_interface route add default $route_syntax_gw `cat "$DEFAULT_ROUTE_FILE"` rm -f -- "$DEFAULT_ROUTE_FILE" fi } del_network_route() { case "$OS" in Linux|NetBSD|OpenBSD|Darwin|SunOS) # and probably others... # routes are deleted automatically on device shutdown return ;; esac NETWORK="$1" NETMASK="$2" NETMASKLEN="$3" route $route_syntax_del -net "$NETWORK" $route_syntax_netmask "$NETMASK" $route_syntax_gw "$INTERNAL_IP4_ADDRESS" } set_ipv6_default_route() { route add -inet6 default "$INTERNAL_IP6_ADDRESS" $route_syntax_interface } set_ipv6_network_route() { NETWORK="$1" NETMASK="$2" route add -inet6 -net "$NETWORK/$NETMASK" "$INTERNAL_IP6_ADDRESS" $route_syntax_interface : } reset_ipv6_default_route() { route $route_syntax_del -inet6 default "$INTERNAL_IP6_ADDRESS" : } del_ipv6_network_route() { NETWORK="$1" NETMASK="$2" route $route_syntax_del -inet6 "$NETWORK/$NETMASK" "$INTERNAL_IP6_ADDRESS" : } fi # =========== resolv.conf handling ==================================== # =========== resolv.conf handling for any OS ========================= modify_resolvconf_generic() { grep '^#@VPNC_GENERATED@' /etc/resolv.conf > /dev/null 2>&1 || cp -- /etc/resolv.conf "$RESOLV_CONF_BACKUP" NEW_RESOLVCONF="#@VPNC_GENERATED@ -- this file is generated by vpnc # and will be overwritten by vpnc # as long as the above mark is intact" DOMAINS="$CISCO_DEF_DOMAIN" exec 6< "$RESOLV_CONF_BACKUP" while read LINE <&6 ; do case "$LINE" in # omit; we will overwrite these nameserver*) ;; # extract listed domains and prepend to list domain* | search*) DOMAINS="${LINE#* } $DOMAINS" ;; # retain other lines *) NEW_RESOLVCONF="$NEW_RESOLVCONF $LINE" ;; esac done exec 6<&- for i in $INTERNAL_IP4_DNS ; do NEW_RESOLVCONF="$NEW_RESOLVCONF nameserver $i" done # note that "search" is mutually exclusive with "domain"; # "search" allows multiple domains to be listed, so use that if [ -n "$DOMAINS" ]; then NEW_RESOLVCONF="$NEW_RESOLVCONF search $DOMAINS" fi echo "$NEW_RESOLVCONF" > /etc/resolv.conf if [ "$OS" = "Darwin" ]; then case "`uname -r`" in # Skip for pre-10.4 systems 4.*|5.*|6.*|7.*) ;; # 10.4 and later require use of scutil for DNS to work properly *) OVERRIDE_PRIMARY="" if [ -n "$CISCO_SPLIT_INC" ]; then if [ $CISCO_SPLIT_INC -lt 1 ]; then # Must override for correct default route # Cannot use multiple DNS matching in this case OVERRIDE_PRIMARY='d.add OverridePrimary # 1' fi # Overriding the default gateway breaks split routing OVERRIDE_GATEWAY="" # Not overriding the default gateway breaks usage of # INTERNAL_IP4_DNS. Prepend INTERNAL_IP4_DNS to list # of used DNS servers SERVICE=`echo "show State:/Network/Global/IPv4" | scutil | grep -oE '[a-fA-F0-9]{8}-([a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}'` SERVICE_DNS=`echo "show State:/Network/Service/$SERVICE/DNS" | scutil | grep -oE '([0-9]{1,3}[\.]){3}[0-9]{1,3}' | xargs` if [ X"$SERVICE_DNS" != X"$INTERNAL_IP4_DNS" ]; then scutil >/dev/null 2>&1 <<-EOF open get State:/Network/Service/$SERVICE/DNS d.add ServerAddresses * $INTERNAL_IP4_DNS $SERVICE_DNS set State:/Network/Service/$SERVICE/DNS close EOF fi else # No split routing. Override default gateway OVERRIDE_GATEWAY="d.add Router $INTERNAL_IP4_ADDRESS" fi # Uncomment the following if/fi pair to use multiple # DNS matching when available. When multiple DNS matching # is present, anything reading the /etc/resolv.conf file # directly will probably not work as intended. #if [ -z "$CISCO_DEF_DOMAIN" ]; then # Cannot use multiple DNS matching without a domain OVERRIDE_PRIMARY='d.add OverridePrimary # 1' #fi scutil >/dev/null 2>&1 <<-EOF open d.init d.add ServerAddresses * $INTERNAL_IP4_DNS set State:/Network/Service/$TUNDEV/DNS d.init $OVERRIDE_GATEWAY d.add Addresses * $INTERNAL_IP4_ADDRESS d.add SubnetMasks * 255.255.255.255 d.add InterfaceName $TUNDEV $OVERRIDE_PRIMARY set State:/Network/Service/$TUNDEV/IPv4 close EOF if [ -n "$CISCO_DEF_DOMAIN" ]; then scutil >/dev/null 2>&1 <<-EOF open get State:/Network/Service/$TUNDEV/DNS d.add DomainName $CISCO_DEF_DOMAIN d.add SearchDomains * $CISCO_DEF_DOMAIN d.add SupplementalMatchDomains * $CISCO_DEF_DOMAIN set State:/Network/Service/$TUNDEV/DNS close EOF fi ;; esac fi } restore_resolvconf_generic() { if [ ! -f "$RESOLV_CONF_BACKUP" ]; then return fi grep '^#@VPNC_GENERATED@' /etc/resolv.conf > /dev/null 2>&1 && cat "$RESOLV_CONF_BACKUP" > /etc/resolv.conf rm -f -- "$RESOLV_CONF_BACKUP" if [ "$OS" = "Darwin" ]; then case "`uname -r`" in # Skip for pre-10.4 systems 4.*|5.*|6.*|7.*) ;; # 10.4 and later require use of scutil for DNS to work properly *) scutil >/dev/null 2>&1 <<-EOF open remove State:/Network/Service/$TUNDEV/IPv4 remove State:/Network/Service/$TUNDEV/DNS close EOF # Split routing required prepending of INTERNAL_IP4_DNS # to list of used DNS servers if [ -n "$CISCO_SPLIT_INC" ]; then SERVICE=`echo "show State:/Network/Global/IPv4" | scutil | grep -oE '[a-fA-F0-9]{8}-([a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}'` SERVICE_DNS=`echo "show State:/Network/Service/$SERVICE/DNS" | scutil | grep -oE '([0-9]{1,3}[\.]){3}[0-9]{1,3}' | xargs` if [ X"$SERVICE_DNS" != X"$INTERNAL_IP4_DNS" ]; then scutil >/dev/null 2>&1 <<-EOF open get State:/Network/Service/$SERVICE/DNS d.add ServerAddresses * ${SERVICE_DNS##$INTERNAL_IP4_DNS} set State:/Network/Service/$SERVICE/DNS close EOF fi fi ;; esac fi } # === resolv.conf handling via /sbin/netconfig (Suse 11.1) ===================== # Suse provides a script that modifies resolv.conf. Use it because it will # restart/reload all other services that care about it (e.g. lwresd). [unclear if this is still true, but probably --mlk] modify_resolvconf_suse_netconfig() { /sbin/netconfig modify -s vpnc -i "$TUNDEV" <<-EOF INTERFACE='$TUNDEV' DNSSERVERS='$INTERNAL_IP4_DNS' DNSDOMAIN='$CISCO_DEF_DOMAIN' EOF } # Restore resolv.conf to old contents on Suse restore_resolvconf_suse_netconfig() { /sbin/netconfig remove -s vpnc -i "$TUNDEV" } # === resolv.conf handling via /sbin/modify_resolvconf (Suse) ===================== # Suse provides a script that modifies resolv.conf. Use it because it will # restart/reload all other services that care about it (e.g. lwresd). modify_resolvconf_suse() { FULL_SCRIPTNAME=`readlink -f $0` RESOLV_OPTS='' test -n "$INTERNAL_IP4_DNS" && RESOLV_OPTS="-n \"$INTERNAL_IP4_DNS\"" test -n "$CISCO_DEF_DOMAIN" && RESOLV_OPTS="$RESOLV_OPTS -d $CISCO_DEF_DOMAIN" test -n "$RESOLV_OPTS" && eval /sbin/modify_resolvconf modify -s vpnc -p $SCRIPTNAME -f $FULL_SCRIPTNAME -e $TUNDEV $RESOLV_OPTS -t \"This file was created by $SCRIPTNAME\" } # Restore resolv.conf to old contents on Suse restore_resolvconf_suse() { FULL_SCRIPTNAME=`readlink -f $0` /sbin/modify_resolvconf restore -s vpnc -p $SCRIPTNAME -f $FULL_SCRIPTNAME -e $TUNDEV } # === resolv.conf handling via UCI (OpenWRT) ========= modify_resolvconf_openwrt() { add_dns $OPENWRT_INTERFACE $INTERNAL_IP4_DNS } restore_resolvconf_openwrt() { remove_dns $OPENWRT_INTERFACE } # === resolv.conf handling via /sbin/resolvconf (Debian, Ubuntu, Gentoo)) ========= modify_resolvconf_manager() { NEW_RESOLVCONF="" for i in $INTERNAL_IP4_DNS; do NEW_RESOLVCONF="$NEW_RESOLVCONF nameserver $i" done if [ -n "$CISCO_DEF_DOMAIN" ]; then NEW_RESOLVCONF="$NEW_RESOLVCONF domain $CISCO_DEF_DOMAIN" fi echo "$NEW_RESOLVCONF" | /sbin/resolvconf -a $TUNDEV } restore_resolvconf_manager() { /sbin/resolvconf -d $TUNDEV } AF_INET=2 get_if_index() { local link link="$(ip link show dev "$1")" || return $? echo ${link} | awk -F: '{print $1}' } busctl_call() { local dest node dest=org.freedesktop.resolve1 node=/org/freedesktop/resolve1 busctl call "$dest" "${node}" "${dest}.Manager" "$@" } busctl_set_nameservers() { local if_index addresses args addr if_index=$1 shift addresses="$@" args="$if_index $#" for addr in ${addresses}; do args="$args ${AF_INET} 4 $(echo $addr | sed 's/[.]/ /g')" done busctl_call SetLinkDNS 'ia(iay)' ${args} } busctl_set_search() { local if_index domains args domain if_index=$1 shift domains="$@" args="$if_index $#" for domain in ${domains}; do args="$args ${domain} false" done busctl_call SetLinkDomains 'ia(sb)' ${args} } modify_resolved_manager() { local if_index if_index=$(get_if_index $TUNDEV) busctl_set_nameservers $if_index $INTERNAL_IP4_DNS if [ -n "$CISCO_DEF_DOMAIN" ]; then busctl_set_search $if_index $CISCO_DEF_DOMAIN fi } restore_resolved_manager() { local if_index if_index=$(get_if_index $TUNDEV) busctl_call RevertLink 'i' $if_index } # === resolv.conf handling via unbound ========= modify_resolvconf_unbound() { if [ -n "$CISCO_DEF_DOMAIN" ]; then /usr/sbin/unbound-control forward_add +i ${CISCO_DEF_DOMAIN} ${INTERNAL_IP4_DNS} /usr/sbin/unbound-control flush_requestlist /usr/sbin/unbound-control flush_zone ${CISCO_DEF_DOMAIN} fi } restore_resolvconf_unbound() { if [ -n "$CISCO_DEF_DOMAIN" ]; then /usr/sbin/unbound-control forward_remove +i ${CISCO_DEF_DOMAIN} /usr/sbin/unbound-control flush_zone ${CISCO_DEF_DOMAIN} /usr/sbin/unbound-control flush_requestlist fi } # ========= Toplevel state handling ======================================= kernel_is_2_6_or_above() { case `uname -r` in 1.*|2.[012345]*) return 1 ;; *) return 0 ;; esac } do_pre_init() { if [ "$OS" = "Linux" ]; then if (exec 6< /dev/net/tun) > /dev/null 2>&1 ; then : else # can't open /dev/net/tun test -e /proc/sys/kernel/modprobe && `cat /proc/sys/kernel/modprobe` tun 2>/dev/null # fix for broken devfs in kernel 2.6.x if [ "`readlink /dev/net/tun`" = misc/net/tun \ -a ! -e /dev/net/misc/net/tun -a -e /dev/misc/net/tun ] ; then ln -sf /dev/misc/net/tun /dev/net/tun fi # make sure tun device exists if [ ! -e /dev/net/tun ]; then mkdir -p /dev/net mknod -m 0640 /dev/net/tun c 10 200 [ -x /sbin/restorecon ] && /sbin/restorecon /dev/net/tun fi # workaround for a possible latency caused by udev, sleep max. 10s if kernel_is_2_6_or_above ; then for x in `seq 100` ; do (exec 6<> /dev/net/tun) > /dev/null 2>&1 && break; sleep 0.1 done fi fi elif [ "$OS" = "FreeBSD" ]; then if ! kldstat -q -m if_tun > /dev/null; then kldload if_tun fi if ! ifconfig $TUNDEV > /dev/null; then ifconfig $TUNDEV create fi elif [ "$OS" = "GNU/kFreeBSD" ]; then if [ ! -e /dev/tun ]; then kldload if_tun fi elif [ "$OS" = "NetBSD" ]; then : elif [ "$OS" = "OpenBSD" ]; then if ! ifconfig $TUNDEV > /dev/null; then ifconfig $TUNDEV create fi : elif [ "$OS" = "SunOS" ]; then : elif [ "$OS" = "Darwin" ]; then : fi } do_connect() { if [ -n "$CISCO_BANNER" ]; then echo "Connect Banner:" echo "$CISCO_BANNER" | while read LINE ; do echo "|" "$LINE" ; done echo fi set_vpngateway_route do_ifconfig if [ -n "$CISCO_SPLIT_INC" ]; then i=0 while [ $i -lt $CISCO_SPLIT_INC ] ; do eval NETWORK="\${CISCO_SPLIT_INC_${i}_ADDR}" eval NETMASK="\${CISCO_SPLIT_INC_${i}_MASK}" eval NETMASKLEN="\${CISCO_SPLIT_INC_${i}_MASKLEN}" if [ "$NETWORK" != "0.0.0.0" ]; then set_network_route "$NETWORK" "$NETMASK" "$NETMASKLEN" else set_default_route fi i=`expr $i + 1` done for i in $INTERNAL_IP4_DNS ; do echo "$i" | grep : >/dev/null || \ set_network_route "$i" "255.255.255.255" "32" done elif [ -n "$INTERNAL_IP4_ADDRESS" ]; then set_default_route fi if [ -n "$CISCO_IPV6_SPLIT_INC" ]; then i=0 while [ $i -lt $CISCO_IPV6_SPLIT_INC ] ; do eval NETWORK="\${CISCO_IPV6_SPLIT_INC_${i}_ADDR}" eval NETMASKLEN="\${CISCO_IPV6_SPLIT_INC_${i}_MASKLEN}" if [ $NETMASKLEN -lt 128 ]; then set_ipv6_network_route "$NETWORK" "$NETMASKLEN" else set_ipv6_default_route fi i=`expr $i + 1` done for i in $INTERNAL_IP4_DNS ; do if echo "$i" | grep : >/dev/null; then set_ipv6_network_route "$i" "128" fi done elif [ -n "$INTERNAL_IP6_NETMASK" -o -n "$INTERNAL_IP6_ADDRESS" ]; then set_ipv6_default_route fi if [ -n "$INTERNAL_IP4_DNS" ]; then $MODIFYRESOLVCONF fi } do_disconnect() { if [ -n "$CISCO_SPLIT_INC" ]; then i=0 while [ $i -lt $CISCO_SPLIT_INC ] ; do eval NETWORK="\${CISCO_SPLIT_INC_${i}_ADDR}" eval NETMASK="\${CISCO_SPLIT_INC_${i}_MASK}" eval NETMASKLEN="\${CISCO_SPLIT_INC_${i}_MASKLEN}" if [ "$NETWORK" != "0.0.0.0" ]; then # FIXME: This doesn't restore previously overwritten # routes. del_network_route "$NETWORK" "$NETMASK" "$NETMASKLEN" else reset_default_route fi i=`expr $i + 1` done for i in $INTERNAL_IP4_DNS ; do del_network_route "$i" "255.255.255.255" "32" done else reset_default_route fi if [ -n "$CISCO_IPV6_SPLIT_INC" ]; then i=0 while [ $i -lt $CISCO_IPV6_SPLIT_INC ] ; do eval NETWORK="\${CISCO_IPV6_SPLIT_INC_${i}_ADDR}" eval NETMASKLEN="\${CISCO_IPV6_SPLIT_INC_${i}_MASKLEN}" if [ $NETMASKLEN -eq 0 ]; then reset_ipv6_default_route else del_ipv6_network_route "$NETWORK" "$NETMASKLEN" fi i=`expr $i + 1` done for i in $INTERNAL_IP6_DNS ; do del_ipv6_network_route "$i" "128" done elif [ -n "$INTERNAL_IP6_NETMASK" -o -n "$INTERNAL_IP6_ADDRESS" ]; then reset_ipv6_default_route fi del_vpngateway_route if [ -n "$INTERNAL_IP4_DNS" ]; then $RESTORERESOLVCONF fi if [ -n "$IPROUTE" ]; then if [ -n "$INTERNAL_IP4_ADDRESS" ]; then $IPROUTE addr del "$INTERNAL_IP4_ADDRESS/255.255.255.255" peer "$INTERNAL_IP4_ADDRESS" dev "$TUNDEV" fi # If the netmask is provided, it contains the address _and_ netmask if [ -n "$INTERNAL_IP6_ADDRESS" ] && [ -z "$INTERNAL_IP6_NETMASK" ]; then INTERNAL_IP6_NETMASK="$INTERNAL_IP6_ADDRESS/128" fi if [ -n "$INTERNAL_IP6_NETMASK" ]; then $IPROUTE -6 addr del $INTERNAL_IP6_NETMASK dev $TUNDEV fi else if [ -n "$INTERNAL_IP4_ADDRESS" ]; then ifconfig "$TUNDEV" 0.0.0.0 fi if [ -n "$INTERNAL_IP6_ADDRESS" ] && [ -z "$INTERNAL_IP6_NETMASK" ]; then INTERNAL_IP6_NETMASK="$INTERNAL_IP6_ADDRESS/128" fi if [ -n "$INTERNAL_IP6_NETMASK" ]; then ifconfig "$TUNDEV" inet6 del $INTERNAL_IP6_NETMASK fi fi destroy_tun_device } #### Main if [ -z "$reason" ]; then echo "this script must be called from vpnc" 1>&2 exit 1 fi case "$reason" in pre-init) run_hooks pre-init do_pre_init ;; connect) run_hooks connect do_connect run_hooks post-connect ;; disconnect) run_hooks disconnect do_disconnect run_hooks post-disconnect ;; reconnect) run_hooks reconnect ;; *) echo "unknown reason '$reason'. Maybe vpnc-script is out of date" 1>&2 exit 1 ;; esac exit 0 vpnc-scripts-20171005/vpnc-script-ptrtd000077500000000000000000000117251316513053400177460ustar00rootroot00000000000000#!/bin/sh # # © 2009 David Woodhouse # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # ################ # # This is a replacement for the standard vpnc-script for use with vpnc # and openconnect. It sets up VPN routing which doesn't screw over the # _normal_ routing of the box. # It sets up a new network namespace for the VPN to use, and it runs # pTRTd (http://www.litech.org/ptrtd/) in that namespace. Connections # to the VPN are actually made as IPv6 connections, and pTRTd handles # converting them to Legacy IP and forwarding them. # The full range of Legacy IP addresses on the VPN is available to the # host through a tiny corner of the IPv6 address space; for this purpose # we use fec0:0:0:ffff:0:0:xxyy:zzww to represent the address xx.yy.zz.ww # on the VPN. # TODO: Either use totd (ftp://ftp.pasta.cs.uit.no/pub/Vermicelli/) or # preferably extend dnsmasq to handle DNS for us. We want the following: # - A queries for non-existent VPN hosts return NXDOMAIN (no munging) # - A queries for _extant_ VPN hosts return NOERROR instead of the result. # - AAAA queries return the A result, converted to the above IPv6 space. # - PTR queries within the IPv6 space converted appropriately. connect_parent() { # XXX: How do we work out what it _really_ is? export PARENT_NETNS=$$ # XXX: Make sure it doesn't exist in this namespace already export RETURNDEV=x$TUNDEV ./netunshare $0 $@ & CHILDPID=$! # XXX: If we do this too soon (before the unshare), we're just # giving it to our _own_ netns. which achieves nothing. # So give it away until we _can't_ give it away any more. while /sbin/ip link set $TUNDEV netns $CHILDPID 2>/dev/null; do sleep 0.1 done # Wait for the ptrtd tundev to appear in this namespace while ! ip link show $RETURNDEV >/dev/null 2>&1 ; do sleep 1 done /sbin/ip link set $RETURNDEV up /sbin/ip addr add fe80::1 dev $RETURNDEV /sbin/ip route add fec0:0:0:ffff::/64 dev $RETURNDEV # XXX: Local hack -- my company's VPN server returns # "foo.company.com" instead of just "company.com". CISCO_DEF_DOMAIN=`echo $CISCO_DEF_DOMAIN | cut -f2- -d.` # Work out the IPv6 address of the nameservers... IPV6NS= for NS in $INTERNAL_IP4_DNS; do A=`echo $NS | cut -f1 -d.` B=`echo $NS | cut -f2 -d.` C=`echo $NS | cut -f3 -d.` D=`echo $NS | cut -f4 -d.` THISNS=`printf fec0:0:0:ffff:0:0:%02x%02x:%02x%02x $A $B $C $D` IPV6NS="$THISNS $IPV6NS" DNSMASQ_CMDLINE="-S /$CISCO_DEF_DOMAIN/$IPV6NS $DNSMASQ_CMDLINE" done echo IPv6 DNS: $IPV6NS # XXX: Add totd-like capability to dnsmasq dnsmasq $DNSMASQ_CMDLINE } connect() { if [ -z "$PARENT_NETNS" ]; then connect_parent exit 0 fi # Wait for the tundev to appear in this namespace while ! ip link show $TUNDEV >/dev/null 2>&1 ; do sleep 0.1 done # Set up Legacy IP in the new namespace /sbin/ip link set lo up /sbin/ip link set $TUNDEV up /sbin/ip -4 addr add $INTERNAL_IP4_ADDRESS dev $TUNDEV /sbin/ip -4 route add default dev $TUNDEV if [ "$INTERNAL_IP4_MTU" != "" ]; then /sbin/ip link set $TUNDEV mtu $INTERNAL_IP4_MTU fi # ifconfig # route # For debugging, really. Lets you ssh into the netns with # ssh fec0:0:0:ffff:0:0:7f00:1 /usr/sbin/sshd -D & SSHD_PID=$! # Start ptrtd ptrtd -i tun:$RETURNDEV -d >/dev/null 2>&1 & PTRTD_PID=$! # Wait for the ptrtd to make its device while ! ip link show $TUNDEV >/dev/null 2>&1 ; do sleep 0.1 done # Now give the ptrtd device back to the parent ip link set $RETURNDEV down /sbin/ip link set $RETURNDEV netns $PARENT_NETNS #Hm, this doesn't work because the tundev doesn't go away when it should #while ip link show $TUNDEV 2> /dev/null ; do # sleep 1 #done # Wait for ptrtd to die (which it will when disconnect() kills its tun) wait $PTRTD_PID kill -TERM $SSHD_PID # Wait a while to avoid tun BUG() if we quit and the netns goes away # before vpnc/openconnect closes its tun fd. sleep 1 } disconnect() { RETURNDEV=x$TUNDEV # This will kill ptrtd inside the netns, leaving the script to clean up ip link del $RETURNDEV # XXX: Undo the dnsmasq stuff. killall dnsmasq # XXX: properly. } case $reason in connect) connect ;; disconnect) disconnect ;; esac vpnc-scripts-20171005/vpnc-script-sshd000077500000000000000000000235651316513053400175570ustar00rootroot00000000000000#!/bin/sh # # © 2009 David Woodhouse # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # ################ # # This is a replacement for the standard vpnc-script used with vpnc and # openconnect. It sets up VPN routing which doesn't screw over the # _normal_ routing of the box. # # It sets up a new network namespace for the VPN to use, and it runs # a Secure Shell dæmon inside that namespace, with full access to all # routes on the VPN. # # It links the 'real' network namespace of the computer to this new one # by an IPv6 site-local connection -- you can ssh into the 'VPN namespace' # by connecting to the host 'fec0::1'. # # You don't need any IPv6 configuration or connectivity for this; you # only need to have IPv6 support in your kernel. The use of IPv6 is purely # local to your machine. # # This gives you effectively the same service as if your company used a # SSH "bastion host" for access, instead of a VPN. It's just that the # bastion host is a special network namespace in your _own_ machine. # # Since your connection to it is _private_, though, you can run a few # services that a secure bastion host could not -- like a web proxy, # for example. You can also just forward certain points so that, for # example, connections to port 25 on your bastion host are automatically # forwarded inside the VPN to your internal mail server. There should # be a sample xinetd.conf with this script which shows how to do that # using 'netcat' started from xinetd. # # It probably helps if you think of the VPN namespace as if it was a # separate machine. From the network point of view, that's what it is. # It just happens to share the file system (and a lot of other stuff) # with your _real_ computer. # # You can configure various other services to use this for connections into # your VPN, as follows... # # SOCKS # # SSH has a built-in SOCKS server. If you run 'ssh -D 1080 fec0::1', SSH # will listen on port 1080 and will forward connections through the SSH # connection and give you full SOCKS access to the VPN. # # It might make sense to make this script automatically start a SSH # connection with SOCKS enabled, if you want that to be available. # # SSH # # The OpenSSH client is capable of connecting to a SSH server by running # and arbitrary command and using its stdin/stdout, instead of having to # make a direct TCP connection to the server. # # So you can configure it to SSH into the VPN 'namespace' and use the # 'netcat' command for certain connections. You can add something like # this to your ~/.ssh/config: # # Host *.example.internal # ProxyCommand ssh fec0::1 exec nc %h %p # # (This also works if your company has made the mistake of overloading the # public 'company.com' domain for internal purposes, instead of doing the # sensible thing and using a _separate_ domain.) # # MAIL # # Like SSH, most decent mail clients are able to run a command to connect # to their IMAP server instead of being limited to a direct TCP connection. # # Commands you might want to use could look like... # ssh $MAILSRV exec /usr/sbin/dovecot --exec-mail imap # ssh $MAILSRV exec /usr/sbin/wu-imapd # ssh fec0::1 openssl s_client -quiet -connect $MAILSRV:993 -crlf 2>/dev/null # # Where '$MAILSRV' is the name of your mail server, of course. # # Note that the first two assume that you've set up SSH as described above, # so that SSH connections to the mail server work transparently. For the # latter, you probably need to redirect stderr to /dev/null to avoid # spurious output from openssl configuring your mail client (openssl doesn't # seem to take the -quiet option very seriously). # # For mail clients which _cannot_ simply run an external command for their # connection, first file a bug and then see the 'PORT FORWARDING' section # below. # # WEB # # Firefox and most other browsers should understand a 'proxy autoconfig' # file and that can tell it to use SOCKS (see above) for certain domains. # A suitable PAC file might look like this: # # function FindProxyForURL(url, host) # { # if (dnsDomainIs(host, "company.com")) # return "SOCKS5 localhost:1080"; # # return "DIRECT"; # } # # PORT FORWARDING # # You can use SSH to forward certain ports, of course -- but there's another, # simpler option. # # The included example of xinetd configuration will accept connections on # port 25 and 993 of the host fec0::1, and will automatically forward them # using netcat to the appropriate hosts within your VPN. This can be extended # to forward other ports. # # OTHER SERVICES # # Most other services should also be available through SSH, through the # SOCKS proxy, or by port forwarding in some way. If all else fails, you # can just ssh into the vpn namespace (ssh fec0::1) and have a shell with # complete access. # # BREAKING OUT OF THE VPN # # If you ssh _into_ your machine from the VPN side, you'll get a shell in # the VPN namespace. To 'break out' from there, you may want to ssh to # fec0::2 which is the normal machine. # # CONTROLLING ACCESS TO THE VPN # # One serious flaw with the _traditional_ VPN setup is that it allows # _all_ processes and users on the machine to have free access to the # VPN, instead of only the user who is supposed to have access. The # approach implemented here allows you to fix that, by running the # SSHD in the VPN namespace with a separate configuration that allows # only certain users to connect to it. # # (Be aware that using port forwarding or using SSH to run a SOCKS proxy # will negate that benefit, of course) # # David Woodhouse # 2009-06-06 IP=/sbin/ip SCRIPTNAME=`basename $0` NETNSNAME=$SCRIPTNAME # XINETDCONF=`dirname $0`/xinetd.netns.conf PS4=" \$\$+ " connect_parent() { export PARENT_NETNS=$$ $IP link set $TUNDEV down if ! $IP link set $TUNDEV netns $$; then echo "Setting network namespace for $TUNDEV failed" echo "Perhaps you don't have network namespace support in your kernel?" exit 1 fi $IP netns delete $NETNSNAME >/dev/null 2>&1 if ! $IP netns add $NETNSNAME; then echo "Creating network namespace $NETNSNAME failed" echo "Perhaps you don't have network namespace support in your kernel?" exit 1 fi $IP link add dev $TUNDEV-vpnssh%d type veth # XXX: Assume vpnssh0 and vpnssh1; ip doesn't tell us! LOCALDEV=$TUNDEV-vpnssh0 export REMOTEDEV=$TUNDEV-vpnssh1 $IP netns exec $NETNSNAME $0 $@ & CHILDPID=$! # XXX: If we do this too soon (before the unshare), we're just # giving it to our _own_ netns. which achieves nothing. # So give it away until we _can't_ give it away any more. while $IP link set $REMOTEDEV netns $CHILDPID 2>/dev/null; do sleep 0.1 done # Give away the real VPN tun device too $IP link set $TUNDEV netns $CHILDPID $IP link set $LOCALDEV up $IP addr add fec0::2/64 dev $LOCALDEV echo "VPN now accessible through 'ssh fec0::1'" if ! grep -q 127.0.0.1 /etc/resolv.conf; then echo "WARNING: Your host needs to be running a local dnsmasq or named" echo "WARNING: and /etc/resolv.conf needs to point to 127.0.0.1" # XXX: We could probably fix that for ourselves... fi } connect() { if [ -z "$PARENT_NETNS" ]; then connect_parent exit 0 fi # This is the child, which remains running in the background # Wait for the tundev to appear in this namespace while ! ip link show $TUNDEV >/dev/null 2>&1 ; do sleep 0.1 done # Set up Legacy IP in the new namespace $IP link set lo up $IP link set $TUNDEV up if [ -n "$INTERNAL_IP4_ADDRESS" ]; then $IP -4 addr add $INTERNAL_IP4_ADDRESS dev $TUNDEV $IP -4 route add default dev $TUNDEV fi if [ -n "$INTERNAL_IP6_ADDRESS" ]; then $IP -6 addr add $INTERNAL_IP6_ADDRESS dev $TUNDEV $IP -6 route add default dev $TUNDEV fi if [ "$INTERNAL_IP4_MTU" != "" ]; then $IP link set $TUNDEV mtu $INTERNAL_IP4_MTU fi # Set up the veth back to the real system $IP link set $REMOTEDEV up $IP -6 addr add fec0::1/64 dev $REMOTEDEV # Run dnsmasq to provide DNS service for this namespace. # The host needs to be running its own local nameserver/dnsmasq and # /etc/resolv.conf should be pointing to 127.0.0.1 already. DNSMASQ_ARGS="--port=53 -k -R" for NS in $INTERNAL_IP4_DNS; do DNSMASQ_ARGS="$DNSMASQ_ARGS -S $NS" done for NS in $INTERNAL_IP6_DNS; do DNSMASQ_ARGS="$DNSMASQ_ARGS -S $NS" done /usr/sbin/dnsmasq $DNSMASQ_ARGS & DNSMASQ_PID=$! # Set up sshd /usr/sbin/sshd -D & SSHD_PID=$! XINETD_PID= if [ "$XINETDCONF" != "" ] && [ -r "$XINETDCONF" ]; then /usr/sbin/xinetd -dontfork -f $XINETDCONF & XINETD_PID=$! fi # Wait for the veth link to be closed... while ip link show $REMOTEDEV >/dev/null 2>&1 ; do sleep 1 done kill -TERM $DNSMASQ_PID kill -TERM $SSHD_PID if [ "$XINETD_PID" != "" ]; then kill -TERM $XINETD_PID fi # Wait a while to avoid tun BUG() if we quit and the netns goes away # before vpnc/openconnect closes its tun fd. sleep 1 } disconnect() { # Kill our end of the veth link, leaving the child script to clean up $IP link del $TUNDEV-vpnssh0 while ! $IP netns delete $NETNSNAME >/dev/null 2>&1 ; do sleep 0.1 done } case $reason in connect) connect ;; disconnect) disconnect ;; esac vpnc-scripts-20171005/vpnc-script-win.js000066400000000000000000000145731316513053400200220ustar00rootroot00000000000000// vpnc-script-win.js // // Sets up the Network interface and the routes // needed by vpnc. // -------------------------------------------------------------- // Utilities // -------------------------------------------------------------- function echo(msg) { WScript.echo(msg); } function run(cmd) { return (ws.Exec(cmd).StdOut.ReadAll()); } function getDefaultGateway() { if (run("route print").match(/0\.0\.0\.0 *(0|128)\.0\.0\.0 *([0-9\.]*)/)) { return (RegExp.$2); } return (""); } function waitForInterface() { var if_route = new RegExp(env("INTERNAL_IP4_ADDRESS") + " *255.255.255.255"); for (var i = 0; i < 5; i++) { echo("Waiting for interface to come up..."); WScript.Sleep(2000); if (run("route print").match(if_route)) { return true; } } return false; } // -------------------------------------------------------------- // Script starts here // -------------------------------------------------------------- var internal_ip4_netmask = "255.255.255.0" var ws = WScript.CreateObject("WScript.Shell"); var env = ws.Environment("Process"); // How to add the default internal route // 0 - As interface gateway when setting properties // 1 - As a 0.0.0.0/0 route with a lower metric than the default route // 2 - As 0.0.0.0/1 + 128.0.0.0/1 routes (override the default route cleanly) var REDIRECT_GATEWAY_METHOD = 0; switch (env("reason")) { case "pre-init": break; case "connect": var gw = getDefaultGateway(); var address_array = env("INTERNAL_IP4_ADDRESS").split("."); var netmask_array = env("INTERNAL_IP4_NETMASK").split("."); // Calculate the first usable address in subnet var internal_gw_array = new Array( address_array[0] & netmask_array[0], address_array[1] & netmask_array[1], address_array[2] & netmask_array[2], (address_array[3] & netmask_array[3]) + 1 ); var internal_gw = internal_gw_array.join("."); echo("VPN Gateway: " + env("VPNGATEWAY")); echo("Internal Address: " + env("INTERNAL_IP4_ADDRESS")); echo("Internal Netmask: " + env("INTERNAL_IP4_NETMASK")); echo("Internal Gateway: " + internal_gw); echo("Interface: \"" + env("TUNDEV") + "\""); if (env("INTERNAL_IP4_MTU")) { echo("MTU: " + env("INTERNAL_IP4_MTU")); run("netsh interface ipv4 set subinterface \"" + env("TUNDEV") + "\" mtu=" + env("INTERNAL_IP4_MTU") + " store=active"); if (env("INTERNAL_IP6_ADDRESS")) { run("netsh interface ipv6 set subinterface \"" + env("TUNDEV") + "\" mtu=" + env("INTERNAL_IP4_MTU") + " store=active"); } } echo("Configuring \"" + env("TUNDEV") + "\" interface for Legacy IP..."); if (!env("CISCO_SPLIT_INC") && REDIRECT_GATEWAY_METHOD != 2) { // Interface metric must be set to 1 in order to add a route with metric 1 since Windows Vista run("netsh interface ip set interface \"" + env("TUNDEV") + "\" metric=1"); } if (env("CISCO_SPLIT_INC") || REDIRECT_GATEWAY_METHOD > 0) { run("netsh interface ip set address \"" + env("TUNDEV") + "\" static " + env("INTERNAL_IP4_ADDRESS") + " " + env("INTERNAL_IP4_NETMASK")); } else { // The default route will be added automatically run("netsh interface ip set address \"" + env("TUNDEV") + "\" static " + env("INTERNAL_IP4_ADDRESS") + " " + env("INTERNAL_IP4_NETMASK") + " " + internal_gw + " 1"); } // Add direct route for the VPN gateway to avoid routing loops run("route add " + env("VPNGATEWAY") + " mask 255.255.255.255 " + gw); if (env("INTERNAL_IP4_NBNS")) { var wins = env("INTERNAL_IP4_NBNS").split(/ /); for (var i = 0; i < wins.length; i++) { run("netsh interface ip add wins \"" + env("TUNDEV") + "\" " + wins[i] + " index=" + (i+1)); } } if (env("INTERNAL_IP4_DNS")) { var dns = env("INTERNAL_IP4_DNS").split(/ /); for (var i = 0; i < dns.length; i++) { run("netsh interface ip add dns \"" + env("TUNDEV") + "\" " + dns[i] + " index=" + (i+1)); } } echo("done."); // Add internal network routes echo("Configuring Legacy IP networks:"); if (env("CISCO_SPLIT_INC")) { // Waiting for the interface to be configured before to add routes if (!waitForInterface()) { echo("Interface does not seem to be up."); } for (var i = 0 ; i < parseInt(env("CISCO_SPLIT_INC")); i++) { var network = env("CISCO_SPLIT_INC_" + i + "_ADDR"); var netmask = env("CISCO_SPLIT_INC_" + i + "_MASK"); var netmasklen = env("CISCO_SPLIT_INC_" + i + "_MASKLEN"); run("route add " + network + " mask " + netmask + " " + internal_gw); } } else if (REDIRECT_GATEWAY_METHOD > 0) { // Waiting for the interface to be configured before to add routes if (!waitForInterface()) { echo("Interface does not seem to be up."); } if (REDIRECT_GATEWAY_METHOD == 1) { run("route add 0.0.0.0 mask 0.0.0.0 " + internal_gw + " metric 1"); } else { run("route add 0.0.0.0 mask 128.0.0.0 " + internal_gw); run("route add 128.0.0.0 mask 128.0.0.0 " + internal_gw); } } echo("Route configuration done."); if (env("INTERNAL_IP6_ADDRESS")) { echo("Configuring \"" + env("TUNDEV") + "\" interface for IPv6..."); run("netsh interface ipv6 set address \"" + env("TUNDEV") + "\" " + env("INTERNAL_IP6_ADDRESS") + " store=active"); echo("done."); // Add internal network routes echo("Configuring IPv6 networks:"); if (env("INTERNAL_IP6_NETMASK") && !env("INTERNAL_IP6_NETMASK").match("/128$")) { run("netsh interface ipv6 add route " + env("INTERNAL_IP6_NETMASK") + " \"" + env("TUNDEV") + "\" fe80::8 store=active") } if (env("CISCO_IPV6_SPLIT_INC")) { for (var i = 0 ; i < parseInt(env("CISCO_IPV6_SPLIT_INC")); i++) { var network = env("CISCO_IPV6_SPLIT_INC_" + i + "_ADDR"); var netmasklen = env("CISCO_SPLIT_INC_" + i + "_MASKLEN"); run("netsh interface ipv6 add route " + network + "/" + netmasklen + " \"" + env("TUNDEV") + "\" fe80::8 store=active") } } else { echo("Setting default IPv6 route through VPN."); run("netsh interface ipv6 add route 2000::/3 \"" + env("TUNDEV") + "\" fe80::8 store=active"); } echo("IPv6 route configuration done."); } if (env("CISCO_BANNER")) { echo("--------------------------------------------------"); echo(env("CISCO_BANNER")); echo("--------------------------------------------------"); } break; case "disconnect": // Delete direct route for the VPN gateway to avoid run("route delete " + env("VPNGATEWAY") + " mask 255.255.255.255"); } vpnc-scripts-20171005/xinetd.netns.conf000066400000000000000000000025011316513053400176750ustar00rootroot00000000000000# # This is the master xinetd configuration file. Settings in the # default section will be inherited by all service configurations # unless explicitly overridden in the service configuration. See # xinetd.conf in the man pages for a more detailed explanation of # these attributes. defaults { # The next two items are intended to be a quick access place to # temporarily enable or disable services. # # enabled = # disabled = # Define general logging characteristics. log_type = SYSLOG daemon info log_on_failure = HOST log_on_success = PID HOST DURATION EXIT # Define access restriction defaults # # no_access = # only_from = # max_load = 0 cps = 50 10 instances = 50 per_source = 10 # Address and networking defaults # # bind = # mdns = yes v6only = no # setup environmental attributes # # passenv = groups = yes umask = 002 # Generally, banners are not used. This sets up their global defaults # # banner = # banner_fail = # banner_success = } service smtp { disable = no port = 25 socket_type = stream protocol = tcp wait = no user = root server = /usr/bin/nc server_args = smtp.company.internal 25 # bind = fec0::1 } service imaps { disable = no port = 993 socket_type = stream protocol = tcp wait = no user = root server = /usr/bin/nc server_args = imap.company.internal 993 # bind = fec0::1 }