mkhosts.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <ndb.h>
  13. #include <ip.h>
  14. typedef struct x
  15. {
  16. Ndbtuple *t;
  17. Ndbtuple *it;
  18. Ndbtuple *nt;
  19. } X;
  20. X x[4096];
  21. int nx;
  22. char *domname = "research.att.com";
  23. int domnamlen;
  24. char*
  25. upper(char *x)
  26. {
  27. char *p;
  28. int c;
  29. for(p = x; c = *p; p++)
  30. *p = toupper(c);
  31. return x;
  32. }
  33. void
  34. printArecord(int fd, X *p)
  35. {
  36. Ndbtuple *nt;
  37. char *c;
  38. char *dom = 0;
  39. char *curdom = 0;
  40. int i, cdlen = 0;
  41. int mxweight = 0;
  42. if(p->nt) {
  43. return;
  44. }
  45. for(nt=p->t; nt; nt = nt->entry) {
  46. /* we are only going to handle things in the specified domain */
  47. c = strchr(nt->val, '.');
  48. if (c==0 || strcmp(++c, domname)!=0)
  49. continue;
  50. i = c - nt->val - 1;
  51. if(strcmp(nt->attr, "dom") == 0) {
  52. curdom = nt->val;
  53. cdlen = i;
  54. if (dom == 0) {
  55. dom = curdom;
  56. fprint(fd, "%-.*s%.*s IN A %s\n", i, nt->val, 15-i, " ", p->it->val);
  57. } else
  58. fprint(fd, "%-.*s%.*s IN CNAME %s.\n", i, nt->val, 15-i, " ", dom);
  59. } else if(strcmp(nt->attr, "mx") == 0) {
  60. if (curdom != 0)
  61. fprint(fd, "%-.*s%.*s MX %d %s.\n", cdlen, curdom, 15-cdlen, " ", mxweight++, nt->val);
  62. }
  63. }
  64. }
  65. void
  66. printentry(int fd, X *p)
  67. {
  68. Ndbtuple *nt;
  69. if(p->nt)
  70. return;
  71. fprint(fd, "%s ", p->it->val);
  72. for(nt = p->t; nt; nt = nt->entry)
  73. if(strcmp(nt->attr, "dom") == 0)
  74. fprint(fd, " %s", nt->val);
  75. for(nt = p->t; nt; nt = nt->entry)
  76. if(strcmp(nt->attr, "sys") == 0)
  77. fprint(fd, " %s", nt->val);
  78. fprint(fd, "\n");
  79. }
  80. void
  81. printsys(int fd, X *p)
  82. {
  83. Ndbtuple *nt;
  84. for(nt = p->t; nt; nt = nt->entry)
  85. if(strcmp(nt->attr, "dom") == 0)
  86. fprint(fd, "%s\n", nt->val);
  87. }
  88. void
  89. printtxt(int fd, X *p)
  90. {
  91. int i;
  92. Ndbtuple *nt;
  93. if(p->nt){
  94. for(;;){
  95. i = strlen(p->it->val);
  96. if(strcmp(p->it->val+i-2, ".0") == 0)
  97. p->it->val[i-2] = 0;
  98. else
  99. break;
  100. }
  101. fprint(fd, "\nNET : %s : %s\n", p->it->val, upper(p->nt->val));
  102. return;
  103. }
  104. fprint(fd, "HOST : %s :", p->it->val);
  105. i = 0;
  106. for(nt = p->t; nt; nt = nt->entry)
  107. if(strcmp(nt->attr, "dom") == 0){
  108. if(i++ == 0)
  109. fprint(fd, " %s", upper(nt->val));
  110. else
  111. fprint(fd, ", %s", upper(nt->val));
  112. }
  113. fprint(fd, "\n");
  114. }
  115. void
  116. parse(char *file)
  117. {
  118. int i;
  119. Ndb *db;
  120. Ndbtuple *t, *nt, *tt, *ipnett;
  121. char *p;
  122. db = ndbopen(file);
  123. if(db == 0)
  124. exits("no database");
  125. while(t = ndbparse(db)){
  126. for(nt = t; nt; nt = nt->entry){
  127. if(strcmp(nt->attr, "ip") == 0)
  128. break;
  129. if(strcmp(nt->attr, "flavor") == 0
  130. && strcmp(nt->val, "console") == 0)
  131. return;
  132. }
  133. if(nt == 0){
  134. ndbfree(t);
  135. continue;
  136. }
  137. /* dump anything not on our nets */
  138. ipnett = 0;
  139. for(tt = t; tt; tt = tt->entry){
  140. if(strcmp(tt->attr, "ipnet") == 0){
  141. ipnett = tt;
  142. break;
  143. }
  144. if(strcmp(tt->attr, "dom") == 0){
  145. i = strlen(tt->val);
  146. p = tt->val+i-domnamlen;
  147. if(p >= tt->val && strcmp(p, domname) == 0)
  148. break;
  149. }
  150. }
  151. if(tt == 0){
  152. ndbfree(t);
  153. continue;
  154. }
  155. for(; nt; nt = nt->entry){
  156. if(strcmp(nt->attr, "ip") != 0)
  157. continue;
  158. x[nx].it = nt;
  159. x[nx].nt = ipnett;
  160. x[nx++].t = t;
  161. }
  162. }
  163. }
  164. void
  165. main(int argc, char *argv[])
  166. {
  167. int i, fd;
  168. char fn[128];
  169. if (argc>1)
  170. domname = argv[1];
  171. domnamlen = strlen(domname);
  172. if(argc > 2){
  173. for(i = 2; i < argc; i++)
  174. parse(argv[i]);
  175. } else {
  176. parse("/lib/ndb/local");
  177. parse("/lib/ndb/friends");
  178. }
  179. // sprint(fn, "/lib/ndb/hosts.%-.21s", domname);
  180. // fd = create(fn, OWRITE, 0664);
  181. // if(fd < 0){
  182. // fprint(2, "can't create %s: %r\n", fn);
  183. // exits("boom");
  184. // }
  185. // for(i = 0; i < nx; i++)
  186. // printentry(fd, &x[i]);
  187. // close(fd);
  188. //
  189. sprint(fn, "/lib/ndb/db.%-.24s", domname);
  190. fd = create(fn, OWRITE, 0664);
  191. if(fd < 0){
  192. fprint(2, "can't create %s: %r\n", fn);
  193. exits("boom");
  194. }
  195. fprint(fd, "; This file is generated automatically, do not edit!\n");
  196. for(i = 0; i < nx; i++)
  197. printArecord(fd, &x[i]);
  198. close(fd);
  199. sprint(fn, "/lib/ndb/equiv.%-.21s", domname);
  200. fd = create(fn, OWRITE, 0664);
  201. if(fd < 0){
  202. fprint(2, "can't create %s: %r\n", fn);
  203. exits("boom");
  204. }
  205. for(i = 0; i < nx; i++)
  206. printsys(fd, &x[i]);
  207. close(fd);
  208. sprint(fn, "/lib/ndb/txt.%-.23s", domname);
  209. fd = create(fn, OWRITE, 0664);
  210. if(fd < 0){
  211. fprint(2, "can't create %s: %r\n", fn);
  212. exits("boom");
  213. }
  214. for(i = 0; i < nx; i++)
  215. printtxt(fd, &x[i]);
  216. close(fd);
  217. exits(0);
  218. }