3 #include "smtp.h" /* to publish dial_string_parse */
24 static int mxlookup(DS*, char*);
25 static int compar(const void*, const void*);
26 static int callmx(DS*, char*, char*);
27 static void expand_meta(DS *ds);
28 extern int cistrcmp(char*, char*);
31 mxdial(char *addr, char *ddomain, char *gdomain)
37 addr = netmkaddr(addr, 0, "smtp");
38 dial_string_parse(addr, &ds);
40 /* try connecting to destination or any of it's mail routers */
41 fd = callmx(&ds, addr, ddomain);
43 /* try our mail gateway */
44 rerrstr(err, sizeof(err));
45 if(fd < 0 && gdomain && strstr(err, "can't translate") != 0)
46 fd = dial(netmkaddr(gdomain, 0, "smtp"), 0, 0, 0);
52 timeout(void *v, char *msg)
56 if(strstr(msg, "alarm"))
62 * take an address and return all the mx entries for it,
63 * most preferred first
66 callmx(DS *ds, char *dest, char *domain)
71 /* get a list of mx entries */
72 nmx = mxlookup(ds, domain);
74 /* dns isn't working, don't just dial */
79 fprint(2, "mxlookup returns nothing\n");
80 return dial(dest, 0, 0, 0);
83 /* refuse to honor loopback addresses given by dns */
84 for(i = 0; i < nmx; i++){
85 if(strcmp(mx[i].ip, "127.0.0.1") == 0){
87 fprint(2, "mxlookup returns loopback\n");
88 werrstr("illegal: domain lists 127.0.0.1 as mail server");
93 /* sort by preference */
95 qsort(mx, nmx, sizeof(Mx), compar);
99 print("%s %d\n", mx[i].host, mx[i].pref);
101 /* dial each one in turn */
102 for(i = 0; i < nmx; i++){
103 snprint(addr, sizeof(addr), "%s/%s!%s!%s", ds->netdir, ds->proto,
104 mx[i].host, ds->service);
106 fprint(2, "mxdial trying %s (%d)\n", addr, i);
107 atnotify(timeout, 1);
109 fd = dial(addr, 0, 0, 0);
111 atnotify(timeout, 0);
119 * use dns to resolve the mx request
122 mxlookup(DS *ds, char *domain)
125 Ndbtuple *t, *tmx, *tpref, *tip;
129 if((t = dnsquery(nil, ds->host, "mx")) != nil){
130 for(tmx=t; (tmx=ndbfindattr(tmx->entry, nil, "mx")) != nil && nmx<Nmx; ){
131 for(tpref=tmx->line; tpref != tmx; tpref=tpref->line){
132 if(strcmp(tpref->attr, "pref") == 0){
133 strncpy(mx[nmx].host, tmx->val, sizeof(mx[n].host)-1);
134 mx[nmx].pref = atoi(tpref->val);
144 * no mx record? try name itself.
147 * BUG? If domain has no dots, then we used to look up ds->host
148 * but return domain instead of ds->host in the list. Now we return
149 * ds->host. What will this break?
153 strncpy(mx[0].host, ds->host, sizeof(mx[0].host));
158 * look up all ip addresses
160 for(i = 0; i < nmx; i++){
161 if((t = dnsquery(nil, mx[i].host, "ip")) == nil)
163 if((tip = ndbfindattr(t, nil, "ip")) == nil){
167 strncpy(mx[i].ip, tip->val, sizeof(mx[i].ip)-1);
172 /* remove mx[i] and go around again */
181 compar(const void *a, const void *b)
183 return ((Mx*)a)->pref - ((Mx*)b)->pref;
186 /* break up an address to its component parts */
188 dial_string_parse(char *str, DS *ds)
192 strncpy(ds->buf, str, sizeof(ds->buf));
193 ds->buf[sizeof(ds->buf)-1] = 0;
195 p = strchr(ds->buf, '!');
205 for(p2 = p; *p2 != '/'; p2--)
208 ds->netdir = ds->buf;
214 ds->service = strchr(ds->host, '!');
226 char *sys, *smtpserver;
228 /* can't ask cs, so query database directly. */
232 smtpserver = ndbgetvalue(db, &s, "sys", sys, "smtp", nil);
233 snprint(ds->host, 128, "%s", smtpserver);