ndb.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * this currently only works for ethernet bootp's -- presotto
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. #include <ip.h>
  7. #include <bio.h>
  8. #include <ndb.h>
  9. #include "dat.h"
  10. static void check72(Info *iip);
  11. Ndb *db;
  12. char *ndbfile;
  13. Iplifc*
  14. findlifc(uchar *ip)
  15. {
  16. uchar x[IPaddrlen];
  17. Ipifc *ifc;
  18. Iplifc *lifc;
  19. for(ifc = ipifcs; ifc; ifc = ifc->next){
  20. for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
  21. if(lifc->net[0] == 0)
  22. continue;
  23. maskip(ip, lifc->mask, x);
  24. if(memcmp(x, lifc->net, IPaddrlen) == 0)
  25. return lifc;
  26. }
  27. }
  28. return nil;
  29. }
  30. int
  31. forme(uchar *ip)
  32. {
  33. Ipifc *ifc;
  34. Iplifc *lifc;
  35. for(ifc = ipifcs; ifc; ifc = ifc->next){
  36. for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next)
  37. if(memcmp(ip, lifc->ip, IPaddrlen) == 0)
  38. return 1;
  39. }
  40. return 0;
  41. }
  42. uchar noetheraddr[6];
  43. static void
  44. setipaddr(uchar *addr, char *ip)
  45. {
  46. if(ipcmp(addr, IPnoaddr) == 0)
  47. parseip(addr, ip);
  48. }
  49. static void
  50. setipmask(uchar *mask, char *ip)
  51. {
  52. if(ipcmp(mask, IPnoaddr) == 0)
  53. parseipmask(mask, ip);
  54. }
  55. /*
  56. * do an ipinfo with defaults
  57. */
  58. int
  59. lookupip(uchar *ipaddr, Info *iip, int gate)
  60. {
  61. char ip[32];
  62. Ndbtuple *t, *nt;
  63. char *attrs[32], **p;
  64. if(db == 0)
  65. db = ndbopen(ndbfile);
  66. if(db == 0){
  67. fprint(2, "can't open db\n");
  68. return -1;
  69. }
  70. p = attrs;
  71. *p++ = "ip";
  72. *p++ = "ipmask";
  73. *p++ = "@ipgw";
  74. if(!gate){
  75. *p++ = "bootf";
  76. *p++ = "bootf2";
  77. *p++ = "@tftp";
  78. *p++ = "@tftp2";
  79. *p++ = "rootpath";
  80. *p++ = "dhcp";
  81. *p++ = "vendor";
  82. *p++ = "ether";
  83. *p++ = "dom";
  84. *p++ = "@fs";
  85. *p++ = "@auth";
  86. }
  87. *p = 0;
  88. memset(iip, 0, sizeof(*iip));
  89. snprint(ip, sizeof(ip), "%I", ipaddr);
  90. t = ndbipinfo(db, "ip", ip, attrs, p - attrs);
  91. if(t == nil)
  92. return -1;
  93. for(nt = t; nt != nil; nt = nt->entry){
  94. if(strcmp(nt->attr, "ip") == 0)
  95. setipaddr(iip->ipaddr, nt->val);
  96. else
  97. if(strcmp(nt->attr, "ipmask") == 0)
  98. setipmask(iip->ipmask, nt->val);
  99. else
  100. if(strcmp(nt->attr, "fs") == 0)
  101. setipaddr(iip->fsip, nt->val);
  102. else
  103. if(strcmp(nt->attr, "auth") == 0)
  104. setipaddr(iip->auip, nt->val);
  105. else
  106. if(strcmp(nt->attr, "tftp") == 0)
  107. setipaddr(iip->tftp, nt->val);
  108. else
  109. if(strcmp(nt->attr, "tftp2") == 0)
  110. setipaddr(iip->tftp2, nt->val);
  111. else
  112. if(strcmp(nt->attr, "ipgw") == 0)
  113. setipaddr(iip->gwip, nt->val);
  114. else
  115. if(strcmp(nt->attr, "ether") == 0){
  116. if(memcmp(iip->etheraddr, noetheraddr, 6) == 0)
  117. parseether(iip->etheraddr, nt->val);
  118. iip->indb = 1;
  119. }
  120. else
  121. if(strcmp(nt->attr, "dhcp") == 0){
  122. if(iip->dhcpgroup[0] == 0)
  123. strcpy(iip->dhcpgroup, nt->val);
  124. }
  125. else
  126. if(strcmp(nt->attr, "bootf") == 0){
  127. if(iip->bootf[0] == 0)
  128. strcpy(iip->bootf, nt->val);
  129. }
  130. else
  131. if(strcmp(nt->attr, "bootf2") == 0){
  132. if(iip->bootf2[0] == 0)
  133. strcpy(iip->bootf2, nt->val);
  134. }
  135. else
  136. if(strcmp(nt->attr, "vendor") == 0){
  137. if(iip->vendor[0] == 0)
  138. strcpy(iip->vendor, nt->val);
  139. }
  140. else
  141. if(strcmp(nt->attr, "dom") == 0){
  142. if(iip->domain[0] == 0)
  143. strcpy(iip->domain, nt->val);
  144. }
  145. else
  146. if(strcmp(nt->attr, "rootpath") == 0){
  147. if(iip->rootpath[0] == 0)
  148. strcpy(iip->rootpath, nt->val);
  149. }
  150. }
  151. ndbfree(t);
  152. maskip(iip->ipaddr, iip->ipmask, iip->ipnet);
  153. return 0;
  154. }
  155. static uchar zeroes[6];
  156. /*
  157. * lookup info about a client in the database. Find an address on the
  158. * same net as riip.
  159. */
  160. int
  161. lookup(Bootp *bp, Info *iip, Info *riip)
  162. {
  163. Ndbtuple *t, *nt;
  164. Ndbs s;
  165. char *hwattr;
  166. char *hwval, hwbuf[33];
  167. uchar ciaddr[IPaddrlen];
  168. if(db == 0)
  169. db = ndbopen(ndbfile);
  170. if(db == 0){
  171. fprint(2, "can't open db\n");
  172. return -1;
  173. }
  174. memset(iip, 0, sizeof(*iip));
  175. /* client knows its address? */
  176. v4tov6(ciaddr, bp->ciaddr);
  177. if(validip(ciaddr)){
  178. if(lookupip(ciaddr, iip, 0) < 0)
  179. return -1; /* don't know anything about it */
  180. check72(iip);
  181. if(!samenet(riip->ipaddr, iip)){
  182. warning(0, "%I not on %I", ciaddr, riip->ipnet);
  183. return -1;
  184. }
  185. /*
  186. * see if this is a masquerade, i.e., if the ether
  187. * address doesn't match what we expected it to be.
  188. */
  189. if(memcmp(iip->etheraddr, zeroes, 6) != 0)
  190. if(memcmp(bp->chaddr, iip->etheraddr, 6) != 0)
  191. warning(0, "ciaddr %I rcvd from %E instead of %E",
  192. ciaddr, bp->chaddr, iip->etheraddr);
  193. return 0;
  194. }
  195. if(bp->hlen > Maxhwlen)
  196. return -1;
  197. switch(bp->htype){
  198. case 1:
  199. hwattr = "ether";
  200. hwval = hwbuf;
  201. snprint(hwbuf, sizeof(hwbuf), "%E", bp->chaddr);
  202. break;
  203. default:
  204. syslog(0, blog, "not ethernet %E, htype %d, hlen %d",
  205. bp->chaddr, bp->htype, bp->hlen);
  206. return -1;
  207. }
  208. /*
  209. * use hardware address to find an ip address on
  210. * same net as riip
  211. */
  212. t = ndbsearch(db, &s, hwattr, hwval);
  213. while(t){
  214. for(nt = t; nt; nt = nt->entry){
  215. if(strcmp(nt->attr, "ip") != 0)
  216. continue;
  217. parseip(ciaddr, nt->val);
  218. if(lookupip(ciaddr, iip, 0) < 0)
  219. continue;
  220. if(samenet(riip->ipaddr, iip)){
  221. ndbfree(t);
  222. return 0;
  223. }
  224. }
  225. ndbfree(t);
  226. t = ndbsnext(&s, hwattr, hwval);
  227. }
  228. return -1;
  229. }
  230. /*
  231. * interface to ndbipinfo
  232. */
  233. Ndbtuple*
  234. lookupinfo(uchar *ipaddr, char **attr, int n)
  235. {
  236. char ip[32];
  237. sprint(ip, "%I", ipaddr);
  238. return ndbipinfo(db, "ip", ip, attr, n);
  239. }
  240. /*
  241. * return the ip addresses for a type of server for system ip
  242. */
  243. int
  244. lookupserver(char *attr, uchar **ipaddrs, Ndbtuple *t)
  245. {
  246. Ndbtuple *nt;
  247. int rv = 0;
  248. for(nt = t; rv < 2 && nt != nil; nt = nt->entry)
  249. if(strcmp(nt->attr, attr) == 0){
  250. parseip(ipaddrs[rv], nt->val);
  251. rv++;
  252. }
  253. return rv;
  254. }
  255. /*
  256. * just lookup the name
  257. */
  258. void
  259. lookupname(char *val, Ndbtuple *t)
  260. {
  261. Ndbtuple *nt;
  262. for(nt = t; nt != nil; nt = nt->entry)
  263. if(strcmp(nt->attr, "dom") == 0){
  264. strcpy(val, nt->val);
  265. break;
  266. }
  267. }
  268. uchar slash120[IPaddrlen] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  269. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0 };
  270. uchar net72[IPaddrlen] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  271. 0x0, 0x0, 0xff, 0xff, 135, 104, 72, 0 };
  272. static void
  273. check72(Info *iip)
  274. {
  275. uchar net[IPaddrlen];
  276. maskip(iip->ipaddr, slash120, net);
  277. if(ipcmp(net, net72) == 0)
  278. syslog(0, blog, "check72 %I %M gw %I", iip->ipaddr, iip->ipmask, iip->gwip);
  279. }