1: /* 2: * Copyright (c) 1983 Regents of the University of California. 3: * All rights reserved. The Berkeley software License Agreement 4: * specifies the terms and conditions for redistribution. 5: */ 6: 7: #if !defined(lint) && defined(DOSCCS) 8: char copyright[] = 9: "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 10: All rights reserved.\n"; 11: 12: /* static char sccsid[] = "@(#)mkhosts.c 5.1 (Berkeley) 5/28/85"; */ 13: static char sccsid[] = "@(#)mkhosts.c 1.2 (2.11BSD) 1995/08/17"; 14: #endif 15: 16: #include <ctype.h> 17: #include <sys/param.h> 18: #include <sys/file.h> 19: #include <stdio.h> 20: #include <netdb.h> 21: #include <ndbm.h> 22: 23: void keylower(); 24: char buf[BUFSIZ]; 25: 26: main(argc, argv) 27: char *argv[]; 28: { 29: DBM *dp; 30: register struct hostent *hp; 31: struct hostent *hp2; 32: datum key, content; 33: register char *cp, *tp, **sp; 34: register int *nap; 35: int naliases, naddrs; 36: int verbose = 0, entries = 0, maxlen = 0, error = 0; 37: char tempname[BUFSIZ], newname[BUFSIZ]; 38: char lowname[MAXHOSTNAMELEN + 1]; 39: 40: if (argc > 1 && strcmp(argv[1], "-v") == 0) { 41: verbose++; 42: argv++, argc--; 43: } 44: if (argc != 2) { 45: fprintf(stderr, "usage: mkhosts [ -v ] file\n"); 46: exit(1); 47: } 48: if (access(argv[1], R_OK) < 0) { 49: perror(argv[1]); 50: exit(1); 51: } 52: umask(0); 53: 54: sprintf(tempname, "%s.new", argv[1]); 55: dp = dbm_open(tempname, O_WRONLY|O_CREAT|O_EXCL, 0644); 56: if (dp == NULL) { 57: fprintf(stderr, "dbm_open failed: "); 58: perror(argv[1]); 59: exit(1); 60: } 61: sethostfile(argv[1]); 62: sethostent(1); 63: while (hp = gethostent()) { 64: cp = buf; 65: tp = hp->h_name; 66: while (*cp++ = *tp++) 67: ; 68: nap = (int *)cp; 69: cp += sizeof (int); 70: 71: keylower(lowname, hp->h_name); 72: key.dptr = lowname; 73: key.dsize = strlen(lowname); 74: /* 75: key.dptr = hp->h_name; 76: key.dsize = strlen(hp->h_name); 77: */ 78: hp2 = (struct hostent *)fetchhost(dp, key); 79: if (hp2) { 80: merge(hp, hp2); 81: hp = hp2; 82: } 83: naliases = 0; 84: for (sp = hp->h_aliases; *sp; sp++) { 85: tp = *sp; 86: while (*cp++ = *tp++) 87: ; 88: naliases++; 89: } 90: bcopy((char *)&naliases, (char *)nap, sizeof(int)); 91: bcopy((char *)&hp->h_addrtype, cp, sizeof (int)); 92: cp += sizeof (int); 93: bcopy((char *)&hp->h_length, cp, sizeof (int)); 94: cp += sizeof (int); 95: for (naddrs = 0, sp = hp->h_addr_list; *sp; sp++) { 96: bcopy(*sp, cp, hp->h_length); 97: cp += hp->h_length; 98: naddrs++; 99: } 100: content.dptr = buf; 101: content.dsize = cp - buf; 102: if (verbose) 103: printf("store %s, %d aliases %d addresses\n", hp->h_name, naliases, naddrs); 104: if (dbm_store(dp, key, content, DBM_REPLACE) < 0) { 105: perror(hp->h_name); 106: goto err; 107: } 108: for (sp = hp->h_aliases; *sp; sp++) { 109: keylower(lowname, *sp); 110: key.dptr = lowname; 111: key.dsize = strlen(lowname); 112: /* 113: key.dptr = *sp; 114: key.dsize = strlen(*sp); 115: */ 116: if (dbm_store(dp, key, content, DBM_REPLACE) < 0) { 117: perror(*sp); 118: goto err; 119: } 120: } 121: for (sp = hp->h_addr_list; *sp; sp++) { 122: key.dptr = *sp; 123: key.dsize = hp->h_length; 124: if (dbm_store(dp, key, content, DBM_REPLACE) < 0) { 125: perror("dbm_store host address"); 126: goto err; 127: } 128: } 129: entries++; 130: if (cp - buf > maxlen) 131: maxlen = cp - buf; 132: } 133: endhostent(); 134: dbm_close(dp); 135: 136: sprintf(tempname, "%s.new.pag", argv[1]); 137: sprintf(newname, "%s.pag", argv[1]); 138: if (rename(tempname, newname) < 0) { 139: perror("rename .pag"); 140: exit(1); 141: } 142: sprintf(tempname, "%s.new.dir", argv[1]); 143: sprintf(newname, "%s.dir", argv[1]); 144: if (rename(tempname, newname) < 0) { 145: perror("rename .dir"); 146: exit(1); 147: } 148: printf("%d host entries, maximum length %d\n", entries, maxlen); 149: exit(0); 150: err: 151: sprintf(tempname, "%s.new.pag", argv[1]); 152: unlink(tempname); 153: sprintf(tempname, "%s.new.dir", argv[1]); 154: unlink(tempname); 155: exit(1); 156: } 157: 158: /* following code lifted from libc/net/hosttable/gethnamadr.c */ 159: 160: #define MAXALIASES 35 161: #define MAXADDRS 10 162: 163: static struct hostent host2; 164: static char *hstaliases[MAXALIASES]; 165: static char *hstaddrs[MAXADDRS]; 166: static char buf2[BUFSIZ]; 167: 168: static struct hostent * 169: fetchhost(dp, key) 170: DBM *dp; 171: datum key; 172: { 173: register char *cp, **ap; 174: register int naddrs; 175: int naliases; 176: 177: key = dbm_fetch(dp, key); 178: if (key.dptr == 0) 179: return ((struct hostent *)NULL); 180: bcopy(key.dptr, buf2, key.dsize); 181: cp = buf2; 182: host2.h_name = cp; 183: while (*cp++) 184: ; 185: bcopy(cp, (char *)&naliases, sizeof(int)); 186: cp += sizeof (int); 187: for (ap = hstaliases; naliases > 0; naliases--) { 188: *ap++ = cp; 189: while (*cp++) 190: ; 191: } 192: *ap = (char *)NULL; 193: host2.h_aliases = hstaliases; 194: bcopy(cp, (char *)&host2.h_addrtype, sizeof (int)); 195: cp += sizeof (int); 196: bcopy(cp, (char *)&host2.h_length, sizeof (int)); 197: cp += sizeof (int); 198: host2.h_addr_list = hstaddrs; 199: naddrs = (key.dsize - (cp - buf2)) / host2.h_length; 200: if (naddrs > MAXADDRS) 201: naddrs = MAXADDRS; 202: for (ap = hstaddrs; naddrs; naddrs--) { 203: *ap++ = cp; 204: cp += host2.h_length; 205: } 206: *ap = (char *)NULL; 207: return (&host2); 208: } 209: 210: merge(hp2, hp) 211: struct hostent *hp2, *hp; 212: { 213: register char **sp, **sp2; 214: char **endalias, **endadr, **hp2ali, **hp2adr; 215: long l; 216: 217: hp2ali = &hp2->h_aliases[0]; 218: hp2adr = &hp2->h_addr_list[0]; 219: 220: for (sp = hp->h_addr_list; *sp; sp++) 221: ; 222: endadr = sp; 223: for (sp = hp->h_aliases; *sp; sp++) 224: ; 225: endalias = sp; 226: for (sp = hp->h_aliases; *sp && *hp2ali; sp++) { 227: for (sp2 = hp2ali; *sp2; sp2++) { 228: if (!strcmp(*sp2, *sp)) 229: break; 230: } 231: if (*sp2 == (char *)NULL) { 232: *endalias++ = *hp2ali++; 233: *endalias = (char *)NULL; 234: } 235: } 236: for (sp = hp->h_addr_list; *sp && *hp2adr; sp++) { 237: for (sp2 = hp2adr; *sp2; sp2++) { 238: if (!bcmp(*sp2, *sp, hp->h_length)) 239: break; 240: } 241: if (*sp2 == (char *)NULL) { 242: *endadr++ = *hp2adr++; 243: *endadr = (char *)NULL; 244: } 245: } 246: } 247: 248: void 249: keylower(out, in) 250: register char *out, *in; 251: { 252: 253: while (*in) 254: { 255: if (isupper(*in)) 256: *out++ = tolower(*in++); 257: else 258: *out++ = *in++; 259: } 260: *out++ = '\0'; 261: }