mxdial.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include "common.h"
  2. #include <ndb.h>
  3. #include <smtp.h> /* to publish dial_string_parse */
  4. enum
  5. {
  6. Nmx= 16,
  7. Maxstring= 256,
  8. };
  9. typedef struct Mx Mx;
  10. struct Mx
  11. {
  12. char host[256];
  13. char ip[24];
  14. int pref;
  15. };
  16. static Mx mx[Nmx];
  17. Ndb *db;
  18. extern int debug;
  19. static int mxlookup(DS*, char*);
  20. static int mxlookup1(DS*, char*);
  21. static int compar(void*, void*);
  22. static int callmx(DS*, char*, char*);
  23. static void expand_meta(DS *ds);
  24. extern int cistrcmp(char*, char*);
  25. int
  26. mxdial(char *addr, char *ddomain, char *gdomain)
  27. {
  28. int fd;
  29. DS ds;
  30. char err[Errlen];
  31. addr = netmkaddr(addr, 0, "smtp");
  32. dial_string_parse(addr, &ds);
  33. /* try connecting to destination or any of it's mail routers */
  34. fd = callmx(&ds, addr, ddomain);
  35. /* try our mail gateway */
  36. rerrstr(err, sizeof(err));
  37. if(fd < 0 && gdomain && strstr(err, "can't translate") != 0)
  38. fd = dial(netmkaddr(gdomain, 0, "smtp"), 0, 0, 0);
  39. return fd;
  40. }
  41. static int
  42. timeout(void *x, char *msg)
  43. {
  44. if(strstr(msg, "alarm"))
  45. return 1;
  46. return 0;
  47. }
  48. /*
  49. * take an address and return all the mx entries for it,
  50. * most preferred first
  51. */
  52. static int
  53. callmx(DS *ds, char *dest, char *domain)
  54. {
  55. int fd, i, nmx;
  56. char addr[Maxstring];
  57. /* get a list of mx entries */
  58. nmx = mxlookup(ds, domain);
  59. if(nmx < 0){
  60. /* dns isn't working, don't just dial */
  61. return -1;
  62. }
  63. if(nmx == 0){
  64. if(debug)
  65. fprint(2, "mxlookup returns nothing\n");
  66. return dial(dest, 0, 0, 0);
  67. }
  68. /* refuse to honor loopback addresses given by dns */
  69. for(i = 0; i < nmx; i++){
  70. if(strcmp(mx[i].ip, "127.0.0.1") == 0){
  71. if(debug)
  72. fprint(2, "mxlookup returns loopback\n");
  73. werrstr("illegal: domain lists 127.0.0.1 as mail server");
  74. return -1;
  75. }
  76. }
  77. /* sort by preference */
  78. if(nmx > 1)
  79. qsort(mx, nmx, sizeof(Mx), compar);
  80. /* dial each one in turn */
  81. for(i = 0; i < nmx; i++){
  82. snprint(addr, sizeof(addr), "%s/%s!%s!%s", ds->netdir, ds->proto,
  83. mx[i].host, ds->service);
  84. if(debug)
  85. fprint(2, "mxdial trying %s\n", addr);
  86. atnotify(timeout, 1);
  87. alarm(10*1000);
  88. fd = dial(addr, 0, 0, 0);
  89. alarm(0);
  90. atnotify(timeout, 0);
  91. if(fd >= 0)
  92. return fd;
  93. }
  94. return -1;
  95. }
  96. /*
  97. * call the dns process and have it try to resolve the mx request
  98. *
  99. * this routine knows about the firewall and tries inside and outside
  100. * dns's seperately.
  101. */
  102. static int
  103. mxlookup(DS *ds, char *domain)
  104. {
  105. int n;
  106. /* just in case we find no domain name */
  107. strcpy(domain, ds->host);
  108. if(ds->netdir){
  109. n = mxlookup1(ds, domain);
  110. } else {
  111. ds->netdir = "/net";
  112. n = mxlookup1(ds, domain);
  113. if(n == 0) {
  114. ds->netdir = "/net.alt";
  115. n = mxlookup1(ds, domain);
  116. }
  117. }
  118. return n;
  119. }
  120. static int
  121. mxlookup1(DS *ds, char *domain)
  122. {
  123. char buf[1024];
  124. char dnsname[Maxstring];
  125. char *fields[4];
  126. int i, n, fd, nmx;
  127. snprint(dnsname, sizeof dnsname, "%s/dns", ds->netdir);
  128. fd = open(dnsname, ORDWR);
  129. if(fd < 0)
  130. return 0;
  131. nmx = 0;
  132. snprint(buf, sizeof(buf), "%s mx", ds->host);
  133. if(debug)
  134. fprint(2, "sending %s '%s'\n", dnsname, buf);
  135. n = write(fd, buf, strlen(buf));
  136. if(n < 0){
  137. rerrstr(buf, sizeof buf);
  138. if(debug)
  139. fprint(2, "dns: %s\n", buf);
  140. if(strstr(buf, "dns failure")){
  141. /* if dns fails for the mx lookup, we have to stop */
  142. close(fd);
  143. return -1;
  144. }
  145. } else {
  146. /*
  147. * get any mx entries
  148. */
  149. seek(fd, 0, 0);
  150. while(nmx < Nmx && (n = read(fd, buf, sizeof(buf)-1)) > 0){
  151. buf[n] = 0;
  152. if(debug)
  153. fprint(2, "dns mx: %s\n", buf);
  154. n = getfields(buf, fields, 4, 1, " \t");
  155. if(n < 4)
  156. continue;
  157. if(strchr(domain, '.') == 0)
  158. strcpy(domain, fields[0]);
  159. strncpy(mx[nmx].host, fields[3], sizeof(mx[n].host)-1);
  160. mx[nmx].pref = atoi(fields[2]);
  161. nmx++;
  162. }
  163. if(debug)
  164. fprint(2, "dns mx; got %d entries\n", nmx);
  165. }
  166. /*
  167. * no mx record? try name itself.
  168. */
  169. /*
  170. * BUG? If domain has no dots, then we used to look up ds->host
  171. * but return domain instead of ds->host in the list. Now we return
  172. * ds->host. What will this break?
  173. */
  174. if(nmx == 0){
  175. mx[0].pref = 1;
  176. strncpy(mx[0].host, ds->host, sizeof(mx[0].host));
  177. nmx++;
  178. }
  179. /*
  180. * look up all ip addresses
  181. */
  182. for(i = 0; i < nmx; i++){
  183. seek(fd, 0, 0);
  184. snprint(buf, sizeof buf, "%s ip", mx[i].host);
  185. mx[i].ip[0] = 0;
  186. if(write(fd, buf, strlen(buf)) < 0)
  187. goto no;
  188. seek(fd, 0, 0);
  189. if((n = read(fd, buf, sizeof buf-1)) < 0)
  190. goto no;
  191. buf[n] = 0;
  192. if(getfields(buf, fields, 4, 1, " \t") < 3)
  193. goto no;
  194. strncpy(mx[i].ip, fields[2], sizeof(mx[i].ip)-1);
  195. continue;
  196. no:
  197. /* remove mx[i] and go around again */
  198. nmx--;
  199. mx[i] = mx[nmx];
  200. i--;
  201. }
  202. return nmx;
  203. }
  204. static int
  205. compar(void *a, void *b)
  206. {
  207. return ((Mx*)a)->pref - ((Mx*)b)->pref;
  208. }
  209. /* break up an address to its component parts */
  210. void
  211. dial_string_parse(char *str, DS *ds)
  212. {
  213. char *p, *p2;
  214. strncpy(ds->buf, str, sizeof(ds->buf));
  215. ds->buf[sizeof(ds->buf)-1] = 0;
  216. p = strchr(ds->buf, '!');
  217. if(p == 0) {
  218. ds->netdir = 0;
  219. ds->proto = "net";
  220. ds->host = ds->buf;
  221. } else {
  222. if(*ds->buf != '/'){
  223. ds->netdir = 0;
  224. ds->proto = ds->buf;
  225. } else {
  226. for(p2 = p; *p2 != '/'; p2--)
  227. ;
  228. *p2++ = 0;
  229. ds->netdir = ds->buf;
  230. ds->proto = p2;
  231. }
  232. *p = 0;
  233. ds->host = p + 1;
  234. }
  235. ds->service = strchr(ds->host, '!');
  236. if(ds->service)
  237. *ds->service++ = 0;
  238. if(*ds->host == '$')
  239. expand_meta(ds);
  240. }
  241. static void
  242. expand_meta(DS *ds)
  243. {
  244. char buf[128], cs[128], *net, *p;
  245. int fd, n;
  246. net = ds->netdir;
  247. if(!net)
  248. net = "/net";
  249. if(debug)
  250. fprint(2, "expanding %s!%s\n", net, ds->host);
  251. snprint(cs, sizeof(cs), "%s/cs", net);
  252. if((fd = open(cs, ORDWR)) == -1){
  253. if(debug)
  254. fprint(2, "open %s: %r\n", cs);
  255. syslog(0, "smtp", "cannot open %s: %r", cs);
  256. return;
  257. }
  258. snprint(buf, sizeof(buf), "!ipinfo %s", ds->host+1); // +1 to skip $
  259. if(write(fd, buf, strlen(buf)) <= 0){
  260. if(debug)
  261. fprint(2, "write %s: %r\n", cs);
  262. syslog(0, "smtp", "%s to %s - write failed: %r", buf, cs);
  263. close(fd);
  264. return;
  265. }
  266. seek(fd, 0, 0);
  267. if((n = read(fd, ds->expand, sizeof(ds->expand)-1)) < 0){
  268. if(debug)
  269. fprint(2, "read %s: %r\n", cs);
  270. syslog(0, "smtp", "%s - read failed: %r", cs);
  271. close(fd);
  272. return;
  273. }
  274. close(fd);
  275. ds->expand[n] = 0;
  276. if((p = strchr(ds->expand, '=')) == nil){
  277. if(debug)
  278. fprint(2, "response %s: %s\n", cs, ds->expand);
  279. syslog(0, "smtp", "%q from %s - bad response: %r", ds->expand, cs);
  280. return;
  281. }
  282. ds->host = p+1;
  283. /* take only first one returned (quasi-bug) */
  284. if((p = strchr(ds->host, ' ')) != nil)
  285. *p = 0;
  286. }