ndb.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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++ = "vendorclass";
  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. /*
  117. * this is probably wrong for machines with multiple
  118. * ethers. bootp or dhcp requests could come from any
  119. * of the ethers listed in the ndb entry.
  120. */
  121. if(memcmp(iip->etheraddr, noetheraddr, 6) == 0)
  122. parseether(iip->etheraddr, nt->val);
  123. iip->indb = 1;
  124. }
  125. else
  126. if(strcmp(nt->attr, "dhcp") == 0){
  127. if(iip->dhcpgroup[0] == 0)
  128. strcpy(iip->dhcpgroup, nt->val);
  129. }
  130. else
  131. if(strcmp(nt->attr, "bootf") == 0){
  132. if(iip->bootf[0] == 0)
  133. strcpy(iip->bootf, nt->val);
  134. }
  135. else
  136. if(strcmp(nt->attr, "bootf2") == 0){
  137. if(iip->bootf2[0] == 0)
  138. strcpy(iip->bootf2, nt->val);
  139. }
  140. else
  141. if(strcmp(nt->attr, "vendor") == 0){
  142. if(iip->vendor[0] == 0)
  143. strcpy(iip->vendor, nt->val);
  144. }
  145. else
  146. if(strcmp(nt->attr, "dom") == 0){
  147. if(iip->domain[0] == 0)
  148. strcpy(iip->domain, nt->val);
  149. }
  150. else
  151. if(strcmp(nt->attr, "rootpath") == 0){
  152. if(iip->rootpath[0] == 0)
  153. strcpy(iip->rootpath, nt->val);
  154. }
  155. }
  156. ndbfree(t);
  157. maskip(iip->ipaddr, iip->ipmask, iip->ipnet);
  158. return 0;
  159. }
  160. static uchar zeroes[6];
  161. /*
  162. * lookup info about a client in the database. Find an address on the
  163. * same net as riip.
  164. */
  165. int
  166. lookup(Bootp *bp, Info *iip, Info *riip)
  167. {
  168. Ndbtuple *t, *nt;
  169. Ndbs s;
  170. char *hwattr;
  171. char *hwval, hwbuf[33];
  172. uchar ciaddr[IPaddrlen];
  173. if(db == 0)
  174. db = ndbopen(ndbfile);
  175. if(db == 0){
  176. fprint(2, "can't open db\n");
  177. return -1;
  178. }
  179. memset(iip, 0, sizeof(*iip));
  180. /* client knows its address? */
  181. v4tov6(ciaddr, bp->ciaddr);
  182. if(validip(ciaddr)){
  183. if(lookupip(ciaddr, iip, 0) < 0)
  184. return -1; /* don't know anything about it */
  185. check72(iip);
  186. if(!samenet(riip->ipaddr, iip)){
  187. warning(0, "%I not on %I", ciaddr, riip->ipnet);
  188. return -1;
  189. }
  190. /*
  191. * see if this is a masquerade, i.e., if the ether
  192. * address doesn't match what we expected it to be.
  193. */
  194. if(memcmp(iip->etheraddr, zeroes, 6) != 0)
  195. if(memcmp(bp->chaddr, iip->etheraddr, 6) != 0)
  196. warning(0, "ciaddr %I rcvd from %E instead of %E",
  197. ciaddr, bp->chaddr, iip->etheraddr);
  198. return 0;
  199. }
  200. if(bp->hlen > Maxhwlen)
  201. return -1;
  202. switch(bp->htype){
  203. case 1:
  204. hwattr = "ether";
  205. hwval = hwbuf;
  206. snprint(hwbuf, sizeof(hwbuf), "%E", bp->chaddr);
  207. break;
  208. default:
  209. syslog(0, blog, "not ethernet %E, htype %d, hlen %d",
  210. bp->chaddr, bp->htype, bp->hlen);
  211. return -1;
  212. }
  213. /*
  214. * use hardware address to find an ip address on
  215. * same net as riip
  216. */
  217. t = ndbsearch(db, &s, hwattr, hwval);
  218. while(t){
  219. for(nt = t; nt; nt = nt->entry){
  220. if(strcmp(nt->attr, "ip") != 0)
  221. continue;
  222. parseip(ciaddr, nt->val);
  223. if(lookupip(ciaddr, iip, 0) < 0)
  224. continue;
  225. if(samenet(riip->ipaddr, iip)){
  226. ndbfree(t);
  227. return 0;
  228. }
  229. }
  230. ndbfree(t);
  231. t = ndbsnext(&s, hwattr, hwval);
  232. }
  233. return -1;
  234. }
  235. /*
  236. * interface to ndbipinfo
  237. */
  238. Ndbtuple*
  239. lookupinfo(uchar *ipaddr, char **attr, int n)
  240. {
  241. char ip[32];
  242. sprint(ip, "%I", ipaddr);
  243. return ndbipinfo(db, "ip", ip, attr, n);
  244. }
  245. /*
  246. * return the ip addresses for a type of server for system ip
  247. */
  248. int
  249. lookupserver(char *attr, uchar **ipaddrs, Ndbtuple *t)
  250. {
  251. Ndbtuple *nt;
  252. int rv = 0;
  253. for(nt = t; rv < 2 && nt != nil; nt = nt->entry)
  254. if(strcmp(nt->attr, attr) == 0){
  255. parseip(ipaddrs[rv], nt->val);
  256. rv++;
  257. }
  258. return rv;
  259. }
  260. /*
  261. * just lookup the name
  262. */
  263. void
  264. lookupname(char *val, Ndbtuple *t)
  265. {
  266. Ndbtuple *nt;
  267. for(nt = t; nt != nil; nt = nt->entry)
  268. if(strcmp(nt->attr, "dom") == 0){
  269. strcpy(val, nt->val);
  270. break;
  271. }
  272. }
  273. uchar slash120[IPaddrlen] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  274. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0 };
  275. uchar net72[IPaddrlen] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  276. 0x0, 0x0, 0xff, 0xff, 135, 104, 72, 0 };
  277. static void
  278. check72(Info *iip)
  279. {
  280. uchar net[IPaddrlen];
  281. maskip(iip->ipaddr, slash120, net);
  282. if(ipcmp(net, net72) == 0)
  283. syslog(0, blog, "check72 %I %M gw %I", iip->ipaddr, iip->ipmask, iip->gwip);
  284. }