home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / n / bind / bind-4.001 / bind-4~ / bind-4.9.3-BETA9 / named / db_secure.c < prev    next >
C/C++ Source or Header  |  1994-07-23  |  4KB  |  171 lines

  1. #ifndef LINT
  2. static char rcsid[] = "$Id: db_secure.c,v 1.6 1994/07/23 23:23:56 vixie Exp $";
  3. #endif
  4.  
  5. /* this file was contributed by Gregory Neil Shapiro of WPI in August 1993 */
  6.  
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <arpa/nameser.h>
  12. #include <arpa/inet.h>
  13. #include <syslog.h>
  14.  
  15. #include "named.h"
  16.  
  17. #ifdef SECURE_ZONES
  18.  
  19. #ifndef SECURE_ZONE_RR
  20. #define SECURE_ZONE_RR "secure_zone"
  21. #endif
  22. #ifndef MASK_SEP
  23. #define MASK_SEP ':'
  24. #endif
  25.  
  26. int
  27. build_secure_netlist(zp)
  28.     struct zoneinfo *zp;
  29. {
  30.     struct netinfo *ntp = NULL, **netlistp, **end;
  31.     char buf[BUFSIZ];
  32.     struct hashbuf *htp;
  33.     struct namebuf *snp;
  34.     struct databuf *dp;
  35.     char *fname, *dname, dnbuf[MAXDNAME];
  36.     int errs = 0, securezone = 0;
  37.  
  38.     if (zp->secure_nets) {
  39.         free_netlist(&zp->secure_nets);
  40.     }
  41.     netlistp = &zp->secure_nets;
  42.     end = netlistp;
  43.     strcat(strcat(strcpy(dnbuf, SECURE_ZONE_RR), "."), zp->z_origin);
  44.  
  45.     dname = dnbuf;
  46.     htp = hashtab;
  47.     if ((snp = nlookup(dname, &htp, &fname, 0)) == NULL) {
  48.         dprintf(1, (ddt,
  49.                 "build_secure_netlist(%s): FAIL on nlookup %s\n",
  50.                 zp->z_origin, dname));
  51.         zp->secure_nets=NULL;
  52.         return(0);
  53.     }
  54.     /* A parent's RR's aren't valid */
  55.     if (strcasecmp(snp->n_dname, SECURE_ZONE_RR)) {
  56.       zp->secure_nets=NULL;
  57.       return(0);
  58.     }
  59.     /* Collect secure nets into secure_nets */
  60.     for (dp = snp->n_data; dp != NULL; dp = dp->d_next) {
  61.         char *maskptr = NULL;
  62.         if (!match(dp, C_ANY, T_TXT)) {
  63.             continue;
  64.         }
  65.         bzero(buf, sizeof(buf));
  66.         bcopy(dp->d_data+1, buf, dp->d_size-1);
  67.         maskptr=strchr(buf, MASK_SEP);
  68.         if (maskptr) {
  69.             *maskptr++ = 0;
  70.         }
  71.         dprintf(3, (ddt,
  72.                 "build_secure_netlist(%s): Found secure zone %s\n",
  73.                 zp->z_origin, buf));
  74.         if (ntp == NULL) {
  75.             ntp = (struct netinfo *)malloc(sizeof(struct netinfo));
  76.             if (!ntp) {
  77.                 dprintf(1, (ddt,
  78.                     "build_secure_netlist (%s): malloc fail\n",
  79.                         zp->z_origin));
  80.                 syslog(LOG_ERR,
  81.                     "build_secure_netlist (%s): Out of Memory",
  82.                        zp->z_origin);
  83.                 if (!securezone) {
  84.                   zp->secure_nets=NULL;
  85.                 }
  86.                 return(1);
  87.             }
  88.         }
  89.         if (!inet_aton(buf, &ntp->my_addr)) {
  90.             dprintf(1, (ddt,
  91.                 "build_secure_netlist (%s): Bad address: %s\n",
  92.                     zp->z_origin, buf));
  93.             syslog(LOG_ERR,
  94.                    "build_secure_netlist (%s): Bad address: %s", 
  95.                    zp->z_origin, buf);
  96.             errs++;
  97.             continue;    
  98.         }
  99.         if (maskptr && *maskptr) {
  100.             if (*maskptr == 'h' || *maskptr == 'H') {
  101.                 ntp->mask = (u_int32_t)-1;
  102.             } else {
  103.                               if (!inet_aton(maskptr,
  104.                          (struct in_addr *)&ntp->mask)) {
  105.                     dprintf(1, (ddt,
  106.                    "build_secure_netlist (%s): Bad mask: %s\n",
  107.                             zp->z_origin, maskptr));
  108.                     syslog(LOG_ERR,
  109.                      "build_secure_netlist (%s): Bad mask: %s",
  110.                            zp->z_origin, maskptr);
  111.                     errs++;
  112.                     continue;
  113.                 }    
  114.             }    
  115.         } else {
  116.             ntp->mask = net_mask(ntp->my_addr);
  117.         }
  118.         if (ntp->my_addr.s_addr & ~(ntp->mask)) {
  119.             dprintf(1, (ddt, 
  120.          "build_secure_netlist (%s): addr (%s) is not in mask (x%x)\n",
  121.                     zp->z_origin,
  122.                     inet_ntoa(ntp->my_addr),
  123.                     ntp->mask));
  124.             syslog(LOG_WARNING, 
  125.            "build_secure_netlist (%s): addr (%s) is not in mask (x%x)",
  126.                    zp->z_origin,
  127.                    inet_ntoa(ntp->my_addr),
  128.                    ntp->mask);
  129.             errs++;
  130.         }
  131.         ntp->next = NULL;
  132.         ntp->addr = ntp->my_addr.s_addr & ntp->mask;
  133.  
  134.         /* Check for duplicates */
  135.         if (addr_on_netlist(ntp->my_addr, *netlistp)) {
  136.             dprintf(1, (ddt, 
  137.                "build_secure_netlist (%s): duplicate address %s\n",
  138.                     zp->z_origin, inet_ntoa(ntp->my_addr)));
  139.             syslog(LOG_WARNING, 
  140.                "build_secure_netlist (%s): duplicate address %s\n",
  141.                    zp->z_origin, inet_ntoa(ntp->my_addr));
  142.             errs++;
  143.             continue;
  144.         }
  145.         *end = ntp;
  146.         end = &ntp->next;
  147.         ntp = NULL;
  148.         securezone++;
  149.     }
  150.     if (ntp) {
  151.         free((char *)ntp);
  152.     }
  153.     if (!securezone) {
  154.       zp->secure_nets=NULL;
  155.     }
  156.  
  157. #ifdef DEBUG
  158.     if (debug > 1) {
  159.         for (ntp = *netlistp;  ntp != NULL;  ntp = ntp->next) {
  160.             fprintf(ddt, "ntp x%x addr x%x mask x%x",
  161.                 ntp, ntp->addr, ntp->mask);
  162.             fprintf(ddt, " my_addr x%x", ntp->my_addr);
  163.             fprintf(ddt, " %s", inet_ntoa(ntp->my_addr));
  164.             fprintf(ddt, " next x%x\n", ntp->next);
  165.         }
  166.     }
  167. #endif
  168.     return(errs);
  169. }
  170. #endif /*SECURE_ZONES*/
  171.