chiark / gitweb /
fixes, also do uncompressed v6 addr
[authbind.git] / helper.c
index 09250753bd8b28a93a03723a046fc93533eb6854..c3133eb885893f12b9ef979aa270225357b44732 100644 (file)
--- a/helper.c
+++ b/helper.c
@@ -34,8 +34,6 @@
 # define CONFIGDIR "/etc/authbind"
 #endif
 
-static const char *rcsid="$Id$";
-
 static void exiterrno(int e) {
   exit(e>0 && e<128 ? e : ENOSYS);
 }
@@ -48,7 +46,7 @@ static void perrorfail(const char *m) {
 }
 
 static void badusage(void) {
-  fprintf(stderr,"libauthbind's helper: bad usage\n (%s)\n",rcsid);
+  fprintf(stderr,"libauthbind's helper: bad usage\n");
   exit(ENOSYS);
 }
 
@@ -136,19 +134,35 @@ int main(int argc, const char *const *argv) {
   if (errno != ENOENT) exiterrno(errno);
 
   char npbuf[INET_ADDRSTRLEN + INET6_ADDRSTRLEN];
-  np= inet_ntop(af,addr_any,npbuf,addrlen_any);
+  np= inet_ntop(af,addr_any,npbuf,sizeof(npbuf));
   assert(np);
 
   if (af == AF_INET) {
-    snprintf(fnbuf,sizeof(fnbuf)-1,"byaddr/%s%s:%u",np,tophalfchar,hport);
+    snprintf(fnbuf,sizeof(fnbuf)-1,"byaddr/%s%s:%u",tophalfchar,np,hport);
     if (!access(fnbuf,X_OK)) authorised();
     if (errno != ENOENT) exiterrno(errno);
   }
 
-  snprintf(fnbuf,sizeof(fnbuf)-1,"byaddr/%s%s,%u",np,tophalfchar,hport);
+  snprintf(fnbuf,sizeof(fnbuf)-1,"byaddr/%s%s,%u",tophalfchar,np,hport);
   if (!access(fnbuf,X_OK)) authorised();
   if (errno != ENOENT) exiterrno(errno);
 
+  if (af == AF_INET6) {
+    char sbuf[addrlen_any*3+1], *sp = sbuf;
+    const unsigned char *ip = addr_any;
+    int i;
+    for (i=0; i<8; i++) {
+      unsigned val = 0;
+      val |= *ip++;  val <<= 8;
+      val |= *ip++;
+      if (i) *sp++ = ':';
+      sp += sprintf(sp,"%x",val);
+    }
+    snprintf(fnbuf,sizeof(fnbuf)-1,"byaddr/%s%s,%u",tophalfchar,sbuf,hport);
+    if (!access(fnbuf,X_OK)) authorised();
+    if (errno != ENOENT) exiterrno(errno);
+  }
+
   uid= getuid(); if (uid==(uid_t)-1) perrorfail("getuid");
   snprintf(fnbuf,sizeof(fnbuf)-1,"byuid/%s%lu",tophalfchar,(unsigned long)uid);
 
@@ -157,10 +171,11 @@ int main(int argc, const char *const *argv) {
 
   while (fgets(fnbuf,sizeof(fnbuf)-1,file)) {
     unsigned int a1,a2,a3,a4, alen,pmin,pmax;
-    int nchar= -1;
+    int nchar;
 
     if (af == AF_INET &&
-       (sscanf(fnbuf," %u.%u.%u.%u/%u: %u,%u %n",
+       (nchar = -1,
+        sscanf(fnbuf," %u.%u.%u.%u/%u: %u,%u %n",
                &a1,&a2,&a3,&a4,&alen,&pmin,&pmax,&nchar),
         nchar == strlen(fnbuf))) {
 
@@ -176,7 +191,7 @@ int main(int argc, const char *const *argv) {
     } else {
 
       char *comma = strchr(fnbuf,',');
-      if (comma) continue;
+      if (!comma) continue;
       *comma++ = '\0';
 
       char *hyphen = strchr(fnbuf,'-');
@@ -198,6 +213,7 @@ int main(int argc, const char *const *argv) {
          memcmp(addr_any,maxaddr,addrlen_any) > 0)
        continue;
 
+      nchar = -1;
       sscanf(comma," %u-%u %n",
             &pmin,&pmax,&nchar);
       if (nchar != strlen(comma))