mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] these 2 patches might be useful for "private" use
@ 2022-04-02 23:32 naruto canada
  0 siblings, 0 replies; only message in thread
From: naruto canada @ 2022-04-02 23:32 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 1540 bytes --]

hi

I have kept these 2 patches for many years.
I use them daily for many years.
They enable "implied" wildcard interpretation of /etc/hosts.
I am not asking for them to be included into musl,
because they have no safety check, and code quality
sadly lacking too. However, they are good for private use.
I share them with musl community and release them into public.
Since /etc/hosts is mostly admin's responsibility, and should not
be writable by user programs, it should be ok if read only.

for example:
cat /etc/hosts
127.0.0.1 adservice.google.com
127.0.0.1 doubleclick.net
127.0.0.1 facebook.com
127.0.0.1 facebook.net
127.0.0.1 google-analytics.com
127.0.0.1 googleadservices.com
127.0.0.1 googleapis.com
127.0.0.1 googlesyndication.com
127.0.0.1 googletagmanager.com
127.0.0.1 googletagservices.com
127.0.0.1 googleusercontent.com
127.0.0.1 somecountry
The above will be interpreted as:
127.0.0.1 *.adservice.google.com
127.0.0.1 *.doubleclick.net
127.0.0.1 *.facebook.com
127.0.0.1 *.facebook.net
127.0.0.1 *.google-analytics.com
127.0.0.1 *.googleadservices.com
127.0.0.1 *.googleapis.com
127.0.0.1 *.googlesyndication.com
127.0.0.1 *.googletagmanager.com
127.0.0.1 *.googletagservices.com
127.0.0.1 *.googleusercontent.com
127.0.0.1 *.somecountry

NOTE: max line length is 255 hard coded, you may change that easily.
NOTE: there are many ways to achieve the same result with other tools,
I think of it as one more layer of protection (easier than patching browsers)
NOTE: only works if you compile your user land with musl-libc.

[-- Attachment #2: musl-1.1.21.etc.hosts.lookup.patch --]
[-- Type: application/octet-stream, Size: 2212 bytes --]

--- org/src/network/lookup_name.c
+++ musl-1.1.21/src/network/lookup_name.c
@@ -6,6 +6,7 @@
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
+#include <strings.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <pthread.h>
@@ -63,11 +64,26 @@
 	}
 	while (fgets(line, sizeof line, f) && cnt < MAXADDRS) {
 		char *p, *z;
-
+/*
 		if ((p=strchr(line, '#'))) *p++='\n', *p=0;
 		for(p=line+1; (p=strstr(p, name)) &&
 			(!isspace(p[-1]) || !isspace(p[l])); p++);
 		if (!p) continue;
+*/
+                if (strchr(line, '#')) continue; // skip comment
+                char t1[256]; char t2[256]; int i,j; // t2 is name to be lookup
+                j=strlen(line); for (i=0;i<j;i++) t1[i]=(line)[j-1-i]; t1[j]=0; // reverse string
+                j=strlen(name); for (i=0;i<j;i++) t2[i]=(name)[j-1-i]; t2[j]=0; // reverse string
+                                   j=strlen(t1);   for (i=0;i<j;i++) if (t1[i]==' ') t1[i]=0;  // stops at space
+                if (t1[0]=='\n') { j=strlen(t1)-1; for (i=0;i<j;i++) t1[i]=t1[i+1]; t1[j]=0; } // skip newline
+                // fprintf(stderr,"[%s][%s]\n",t1,t2);
+                // if (t2[strlen(t2)-1]=='.') fprintf(stderr," [BS name!]\n"); // skip BS
+                if (t2[strlen(t2)-1]=='.') continue;                        // skip BS
+                if (strncasecmp(t1,t2,strlen(t1))==0) {
+                   if (strlen(t2)==strlen(t1)) {fprintf(stderr," [%s][musl case1]\n",name);goto here;}
+                   if ((strlen(t2)-1)>strlen(t1))
+                      if (t2[strlen(t1)]=='.') {fprintf(stderr," [%s][musl case2]\n",name);goto here;}
+                } continue; here:
 
 		/* Isolate IP address to parse */
 		for (p=line; *p && !isspace(*p); p++);
@@ -87,9 +103,11 @@
 		for (; *p && isspace(*p); p++);
 		for (z=p; *z && !isspace(*z); z++);
 		*z = 0;
-		if (is_valid_hostname(p)) memcpy(canon, p, z-p+1);
+//		if (is_valid_hostname(p)) { memcpy(canon, p, z-p+1); fprintf(stderr,"[%s][%s][%d]\n",name,p,z-p+1); }
+                if (is_valid_hostname(name)) memcpy(canon, name, strlen(name));
 	}
 	__fclose_ca(f);
+        if (cnt==0) fprintf(stderr,"![%s][musl not found]\n",name);
 	return cnt ? cnt : badfam;
 }
 

[-- Attachment #3: musl-1.2.2.etc.hosts.lookup.patch --]
[-- Type: application/octet-stream, Size: 2215 bytes --]

--- old/src/network/lookup_name.c
+++ new/src/network/lookup_name.c
@@ -14,6 +14,7 @@
 #include "lookup.h"
 #include "stdio_impl.h"
 #include "syscall.h"
+#include <strings.h>
 
 static int is_valid_hostname(const char *host)
 {
@@ -63,11 +64,26 @@
 	}
 	while (fgets(line, sizeof line, f) && cnt < MAXADDRS) {
 		char *p, *z;
-
+/*
 		if ((p=strchr(line, '#'))) *p++='\n', *p=0;
 		for(p=line+1; (p=strstr(p, name)) &&
 			(!isspace(p[-1]) || !isspace(p[l])); p++);
 		if (!p) continue;
+*/
+                if (strchr(line, '#')) continue; // skip comment
+                char t1[256]; char t2[256]; int i,j; // t2 is name to be lookup
+                j=strlen(line); for (i=0;i<j;i++) t1[i]=(line)[j-1-i]; t1[j]=0; // reverse string
+                j=strlen(name); for (i=0;i<j;i++) t2[i]=(name)[j-1-i]; t2[j]=0; // reverse string
+                                   j=strlen(t1);   for (i=0;i<j;i++) if (t1[i]==' ') t1[i]=0;  // stops at space
+                if (t1[0]=='\n') { j=strlen(t1)-1; for (i=0;i<j;i++) t1[i]=t1[i+1]; t1[j]=0; } // skip newline
+                // fprintf(stderr,"[%s][%s]\n",t1,t2);
+                // if (t2[strlen(t2)-1]=='.') fprintf(stderr," [BS name!]\n"); // skip BS
+                if (t2[strlen(t2)-1]=='.') continue;                        // skip BS
+                if (strncasecmp(t1,t2,strlen(t1))==0) {
+                   if (strlen(t2)==strlen(t1)) {fprintf(stderr," [%s][musl case1]\n",name);goto here;}
+                   if ((strlen(t2)-1)>strlen(t1))
+                      if (t2[strlen(t1)]=='.') {fprintf(stderr," [%s][musl case2]\n",name);goto here;}
+                } continue; here:
 
 		/* Isolate IP address to parse */
 		for (p=line; *p && !isspace(*p); p++);
@@ -92,9 +108,13 @@
 		if (is_valid_hostname(p)) {
 			have_canon = 1;
 			memcpy(canon, p, z-p+1);
+                        fprintf(stderr," [%s][%s][%d][musl found]\n",name,p,z-p+1);
+                        if (is_valid_hostname(name)) memcpy(canon, name, strlen(name));
 		}
+                if (is_valid_hostname(name)) memcpy(canon, name, strlen(name));
 	}
 	__fclose_ca(f);
+        if (cnt==0) fprintf(stderr,"![%s][musl not found]\n",name);
 	return cnt ? cnt : badfam;
 }
 

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-04-02 23:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-02 23:32 [musl] these 2 patches might be useful for "private" use naruto canada

Code repositories for project(s) associated with this inbox:

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).