public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Matthias Fulz <mfulz@olznet.de>
To: libc-help@sourceware.org
Subject: Idea / Patch to add very simple uid filtering to resolv.conf
Date: Tue, 12 Sep 2023 14:25:16 +0200	[thread overview]
Message-ID: <2a1c4925-7ad4-4673-9954-72d556d0b8f1@olznet.de> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 1112 bytes --]

Hi Guys,

I have done some patch to be able to filter lines under /etc/resolv.conf 
by specific uids. I had the need for my system, as I've some users that 
are only accessing specific network devices by uid matching routing rules.

Therefore I've to select different nameservers depending on the user and 
added following to resolv.conf:
uid+int any existing resolv line
ae:
uid+1000 nameserver 1.1.1.1
uid-1001 nameserver 8.8.8.8

This will be interpreted the following way:
uid+ -> that config line will only be added to resolv context if the uid 
matches the one after the +
uid- -> that config line will NOT be added resolv context if the uid 
matches the one after the -

Further if the uid is not added to the line the behavior would be the 
same as it was before the patch.
Would be nice if this could be included, or at least please explain why 
not for my understanding as I can't see any reason that would be against it

I was first thinking about doing this by implementing a nss service but 
came to the conclusion, that would be overkill for that little config 
extension.

--
Thanks & BR,
Matthias

[-- Attachment #2: uid_resolv.patch --]
[-- Type: text/x-patch, Size: 2828 bytes --]

diff --color -Nur glibc/resolv/res_init.c glibc_b/resolv/res_init.c
--- glibc/resolv/res_init.c	2023-09-11 23:15:02.377718008 +0200
+++ glibc_b/resolv/res_init.c	2023-09-12 14:15:53.532479227 +0200
@@ -258,6 +258,8 @@
   char *cp;
   size_t buffer_size = 0;
   bool haveenv = false;
+  char struid[24];
+  unsigned int uid = getuid();
 
   /* Allow user to override the local domain definition.  */
   if ((cp = getenv ("LOCALDOMAIN")) != NULL)
@@ -303,6 +305,11 @@
    && ((line)[sizeof (name) - 1] == ' '           \
        || (line)[sizeof (name) - 1] == '\t'))
 
+#define MATCH_STR(line, name)                       \
+  (!strncmp ((line), name, strlen (name))     \
+   && ((line)[strlen (name)] == ' '           \
+       || (line)[strlen (name)] == '\t'))
+
   if (fp != NULL)
     {
       /* No threads use this stream.  */
@@ -324,6 +331,56 @@
           /* Skip comments.  */
           if (*parser->buffer == ';' || *parser->buffer == '#')
             continue;
+          /* check for user specific config */
+          if (!strncmp(parser->buffer, "uid", sizeof("uid") - 1))
+            {
+              cp = parser->buffer + sizeof ("uid") - 1;
+              if (*cp == '+' || *cp == '-')
+                {
+                  /* Get uid for comparism with user speicific configs */
+                  snprintf(struid, 24, "uid%c%u", *cp, uid); 
+                }
+              else
+                continue;
+
+              if (MATCH_STR (parser->buffer, struid))
+                {
+                  if (*cp == '-') // Remove for user
+                    continue;
+
+                  cp = parser->buffer + strlen(struid);
+                  while (*cp == ' ' || *cp == '\t')
+                    cp++;
+                  if ((*cp == '\0') || (*cp == '\n'))
+                    continue;
+
+                  cp = __strdup (cp);
+                  if (cp == NULL)
+                    return false;
+                  snprintf(parser->buffer, strlen(cp), "%s", cp);
+                  free(cp);
+                }
+              else
+                {
+                  if (*cp == '+') // Remove for user
+                    continue;
+
+                  while (*cp != ' ' && *cp != '\t')
+                    {
+                      cp++;
+                    }
+                  while (*cp == ' ' || *cp == '\t')
+                    cp++;
+                  if (*cp == '\0' || *cp == '\n')
+                    continue;
+                  
+                  cp = __strdup (cp++);
+                  if (cp == NULL)
+                    return false;
+                  snprintf(parser->buffer, strlen(cp), "%s", cp);
+                  free(cp);
+                }
+            }
           /* Read default domain name.  */
           if (MATCH (parser->buffer, "domain"))
             {

             reply	other threads:[~2023-09-12 12:25 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-12 12:25 Matthias Fulz [this message]
2023-09-12 14:50 ` Florian Weimer
2023-09-12 15:34 mfulz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2a1c4925-7ad4-4673-9954-72d556d0b8f1@olznet.de \
    --to=mfulz@olznet.de \
    --cc=libc-help@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).