ndb.c 5.7 KB

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