2 * this currently only works for ethernet bootp's -- presotto
11 static void check72(Info *iip);
23 for(ifc = ipifcs; ifc; ifc = ifc->next){
24 for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
27 maskip(ip, lifc->mask, x);
28 if(memcmp(x, lifc->net, IPaddrlen) == 0)
41 extern uchar xmyipaddr[IPaddrlen];
43 if(memcmp(ip, xmyipaddr, IPaddrlen) == 0)
46 for(ifc = ipifcs; ifc; ifc = ifc->next){
47 for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next)
48 if(memcmp(ip, lifc->ip, IPaddrlen) == 0)
57 setipaddr(uchar *addr, char *ip)
59 if(ipcmp(addr, IPnoaddr) == 0)
64 setipmask(uchar *mask, char *ip)
66 if(ipcmp(mask, IPnoaddr) == 0)
67 parseipmask(mask, ip);
71 * do an ipinfo with defaults
74 lookupip(uchar *ipaddr, Info *iip, int gate)
81 db = ndbopen(ndbfile);
83 fprint(2, "can't open db\n");
106 memset(iip, 0, sizeof(*iip));
107 snprint(ip, sizeof(ip), "%I", ipaddr);
108 t = ndbipinfo(db, "ip", ip, attrs, p - attrs);
112 for(nt = t; nt != nil; nt = nt->entry){
113 if(strcmp(nt->attr, "ip") == 0)
114 setipaddr(iip->ipaddr, nt->val);
116 if(strcmp(nt->attr, "ipmask") == 0)
117 setipmask(iip->ipmask, nt->val);
119 if(strcmp(nt->attr, "fs") == 0)
120 setipaddr(iip->fsip, nt->val);
122 if(strcmp(nt->attr, "auth") == 0)
123 setipaddr(iip->auip, nt->val);
125 if(strcmp(nt->attr, "tftp") == 0)
126 setipaddr(iip->tftp, nt->val);
128 if(strcmp(nt->attr, "tftp2") == 0)
129 setipaddr(iip->tftp2, nt->val);
131 if(strcmp(nt->attr, "ipgw") == 0)
132 setipaddr(iip->gwip, nt->val);
134 if(strcmp(nt->attr, "ether") == 0){
135 if(memcmp(iip->etheraddr, noetheraddr, 6) == 0)
136 parseether(iip->etheraddr, nt->val);
140 if(strcmp(nt->attr, "dhcp") == 0){
141 if(iip->dhcpgroup[0] == 0)
142 strcpy(iip->dhcpgroup, nt->val);
145 if(strcmp(nt->attr, "bootf") == 0){
146 if(iip->bootf[0] == 0)
147 strcpy(iip->bootf, nt->val);
150 if(strcmp(nt->attr, "bootf2") == 0){
151 if(iip->bootf2[0] == 0)
152 strcpy(iip->bootf2, nt->val);
155 if(strcmp(nt->attr, "vendor") == 0){
156 if(iip->vendor[0] == 0)
157 strcpy(iip->vendor, nt->val);
160 if(strcmp(nt->attr, "dom") == 0){
161 if(iip->domain[0] == 0)
162 strcpy(iip->domain, nt->val);
165 if(strcmp(nt->attr, "rootpath") == 0){
166 if(iip->rootpath[0] == 0)
167 strcpy(iip->rootpath, nt->val);
171 maskip(iip->ipaddr, iip->ipmask, iip->ipnet);
175 static uchar zeroes[6];
178 * lookup info about a client in the database. Find an address on the
182 lookup(Bootp *bp, Info *iip, Info *riip)
187 char *hwval, hwbuf[33];
188 uchar ciaddr[IPaddrlen];
191 db = ndbopen(ndbfile);
193 fprint(2, "can't open db\n");
197 memset(iip, 0, sizeof(*iip));
199 /* client knows its address? */
200 v4tov6(ciaddr, bp->ciaddr);
202 if(lookupip(ciaddr, iip, 0) < 0)
203 return -1; /* don't know anything about it */
207 if(!samenet(riip->ipaddr, iip)){
208 warning(0, "%I not on %I", ciaddr, riip->ipnet);
213 * see if this is a masquerade, i.e., if the ether
214 * address doesn't match what we expected it to be.
216 if(memcmp(iip->etheraddr, zeroes, 6) != 0)
217 if(memcmp(bp->chaddr, iip->etheraddr, 6) != 0)
218 warning(0, "ciaddr %I rcvd from %E instead of %E",
219 ciaddr, bp->chaddr, iip->etheraddr);
224 if(bp->hlen > Maxhwlen)
230 snprint(hwbuf, sizeof(hwbuf), "%E", bp->chaddr);
233 syslog(0, blog, "not ethernet %E, htype %d, hlen %d",
234 bp->chaddr, bp->htype, bp->hlen);
239 * use hardware address to find an ip address on
242 t = ndbsearch(db, &s, hwattr, hwval);
244 for(nt = t; nt; nt = nt->entry){
245 if(strcmp(nt->attr, "ip") != 0)
247 parseip(ciaddr, nt->val);
248 if(lookupip(ciaddr, iip, 0) < 0)
250 if(samenet(riip->ipaddr, iip)){
256 t = ndbsnext(&s, hwattr, hwval);
262 * interface to ndbipinfo
265 lookupinfo(uchar *ipaddr, char **attr, int n)
269 sprint(ip, "%I", ipaddr);
270 return ndbipinfo(db, "ip", ip, attr, n);
274 * return the ip addresses for a type of server for system ip
277 lookupserver(char *attr, uchar **ipaddrs, Ndbtuple *t)
282 for(nt = t; rv < 2 && nt != nil; nt = nt->entry)
283 if(strcmp(nt->attr, attr) == 0){
284 parseip(ipaddrs[rv], nt->val);
291 * just lookup the name
294 lookupname(char *val, Ndbtuple *t)
298 for(nt = t; nt != nil; nt = nt->entry)
299 if(strcmp(nt->attr, "dom") == 0){
300 strcpy(val, nt->val);
305 uchar slash120[IPaddrlen] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
306 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0 };
307 uchar net72[IPaddrlen] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
308 0x0, 0x0, 0xff, 0xff, 135, 104, 72, 0 };
313 uchar net[IPaddrlen];
315 maskip(iip->ipaddr, slash120, net);
316 if(ipcmp(net, net72) == 0)
317 syslog(0, blog, "check72 %I %M gw %I", iip->ipaddr, iip->ipmask, iip->gwip);