ndb.c 5.9 KB

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