Coder Social home page Coder Social logo

nss-host2ips's Introduction

NSS-Host2ips

Warning: This module is still testing, please use it carefully in the real environment.

Description

This is a NSS module which can map a single hostname to multiple IPs.

Motivation

I have a laptop and a NAS in my dormitory, and I need to switch my Wireguard VPN on my laptop frequently when I go out/get back, so I have to define three different host names mapping for public/private/wireguard ip for NAS, and use it depending on where I am or whether VPN is enabled. That is so tired!

Therefore, I come up with the idea that I can map a hostname to a fallback ip list, it can search through every fallback ip and check if the conditions are met, e.g, network interface.

Now I can happily use a single hostname to connect to my machine without worrying about which hostname I should use!

Installation

make 
sudo make install

Configuration

The configuration file default is /etc/host2ips

And you have to modify /etc/nsswitch.conf to insert the module.

e.g. /etc/nsswitch.conf

hosts:      host2ips files dns

Syntax

Sample of the config file:

# my host
host foo.bar
192.168.0.66   eth0      192.168.0.77
10.0.0.66      wg0       10.0.0.77
# my host2
host foo2.bar
192.168.1.22   eth1      192.168.1.23
10.0.1.22      wg1       -
123.123.123.1  -         -
  • Comment begins with #, currently does not support mixing comment and config
  • To define new host, begins with ‘host’ and the hostname
  • A host can have multiple fallback ips, the ip is used when all the conditions are satified
    • inteface name: specify the interface name
    • interface ip: specify the interface ip
  • - denotes don’t-care value

warning: don’t put any trailing spaces in each line

Todo

  • write test scripts
  • support host aliases
  • support IPv6
  • support more NSS lookup functions, e.g, gethostbyaddr()
  • support putting comments after line

Reference

nss-host2ips's People

Contributors

cycatz avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

nss-host2ips's Issues

Segmentation fault when executing /usr/bin/getent

$ sudo coredumpctl debug  /usr/bin/getent           
          PID: 893988 (getent)
           UID: 1000 (cycatz)
           GID: 1000 (cycatz)
        Signal: 11 (SEGV)
     Timestamp: Sat 2023-01-28 00:42:25 CST (2 days ago)
  Command Line: getent hosts
    Executable: /usr/bin/getent
 Control Group: /user.slice/user-1000.slice/session-1.scope
          Unit: session-1.scope
         Slice: user-1000.slice
       Session: 1
     Owner UID: 1000 (cycatz)
       Boot ID: ee455e767b6e4874a7a9da8069c5e340
    Machine ID: 6c03f7a4927e4eedb6a74a6f1c3f7633
      Hostname: air.staque.xyz
       Storage: /var/lib/systemd/coredump/core.getent.1000.ee455e767b6e4874a7a9da8069c5e340.893988.1674888145000000.zst (present)
  Size on Disk: 47.3K
       Message: Process 893988 (getent) of user 1000 dumped core.

                Module libnss_host2ips.so.2 without build-id.
                Stack trace of thread 893988:
                #0  0x00007fe322598301 nss_host2ips_free_host_list (libnss_host2ips.so.2 + 0x1301)
                #1  0x00007fe322598745 _nss_host2ips_endhostent_locked (libnss_host2ips.so.2 + 0x1745)
                #2  0x00007fe322598773 _nss_host2ips_endhostent (libnss_host2ips.so.2 + 0x1773)
                #3  0x00007fe3226d15cb __nss_endent (libc.so.6 + 0x1325cb)
                #4  0x00007fe3226b82b3 endhostent (libc.so.6 + 0x1192b3)
                #5  0x00005558b13c8339 hosts_keys (getent + 0x4339)
                #6  0x00005558b13c7109 main (getent + 0x3109)
                #7  0x00007fe3225c2290 __libc_start_call_main (libc.so.6 + 0x23290)
                #8  0x00007fe3225c234a __libc_start_main_impl (libc.so.6 + 0x2334a)
                #9  0x00005558b13c71d5 _start (getent + 0x31d5)
                ELF object binary architecture: AMD x86-64
(gdb) bt
#0  0x00007fe322598301 in nss_host2ips_free_host_list (host_list=0x5558b332b8b0) at src/parser.c:45
#1  0x00007fe322598745 in _nss_host2ips_endhostent_locked () at src/hosts.c:114
#2  0x00007fe322598773 in _nss_host2ips_endhostent () at src/hosts.c:124
#3  0x00007fe3226d15cb in __nss_endent (func_name=func_name@entry=0x7fe32273a251 "endhostent", lookup_fct=<optimized out>,
    nip=nip@entry=0x7fe32277fb80 <nip>, startp=startp@entry=0x7fe32277fb70 <startp>, last_nip=last_nip@entry=0x7fe32277fb78 <last_nip>, res=res@entry=1)
    at getnssent_r.c:139
#4  0x00007fe3226b82b3 in endhostent () at ../nss/getXXent_r.c:142
#5  0x00005558b13c8339 in hosts_keys (number=<optimized out>, key=<optimized out>) at getent.c:305
#6  0x00005558b13c7109 in main (argc=<optimized out>, argv=0x7ffcc666eab8) at getent.c:994
(gdb) frame 0
#0  0x00007fe322598301 in nss_host2ips_free_host_list (host_list=0x5558b332b8b0) at src/parser.c:45
45	            info_next = host_info->info_next;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.