ndb.c 6.0 KB

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