18 Maxhost= 64, /* maximum host name size */
19 Maxservice= 64, /* maximum service name size */
20 Maxactive= 200, /* maximum number of active slave procs */
26 typedef struct Mfile Mfile;
27 typedef struct Mlist Mlist;
28 typedef struct Network Network;
29 typedef struct Flushreq Flushreq;
30 typedef struct Job Job;
32 int vers; /* incremented each clone/attach */
36 int busy; /* fid in use */
37 int ref; /* cleanup when drops to zero */
52 * result of the last lookup
84 jmp_buf masterjmp; /* return through here after a slave process has been created */
85 int *isslave; /* *isslave non-zero means this is a slave process */
86 long active; /* number of active slaves */
93 void rattach(Job*, Mfile*);
94 char* rwalk(Job*, Mfile*);
95 void ropen(Job*, Mfile*);
96 void rcreate(Job*, Mfile*);
97 void rread(Job*, Mfile*);
98 void rwrite(Job*, Mfile*);
99 void rclunk(Job*, Mfile*);
100 void rremove(Job*, Mfile*);
101 void rstat(Job*, Mfile*);
102 void rwstat(Job*, Mfile*);
104 void sendmsg(Job*, char*);
106 void mountinit(char*, char*);
111 char *genquery(Mfile*, char*);
112 char* ipinfoquery(Mfile*, char**, int);
113 int needproto(Network*, Ndbtuple*);
116 void readipinterfaces(void);
118 char* estrdup(char*);
121 void setext(char*, int, char*);
122 void cleanmf(Mfile*);
124 QLock dblock; /* mutex on database operations */
125 QLock netlock; /* mutex for netinit() */
127 char *logfile = "cs";
130 char netndb[Maxpath];
133 * Network specific translators
135 Ndbtuple* iplookup(Network*, char*, char*);
136 char* iptrans(Ndbtuple*, Network*, char*, char*, int);
137 Ndbtuple* telcolookup(Network*, char*, char*);
138 char* telcotrans(Ndbtuple*, Network*, char*, char*, int);
139 Ndbtuple* dnsiplookup(char*, Ndbs*, int);
144 Ndbtuple *(*lookup)(Network*, char*, char*);
145 char *(*trans)(Ndbtuple*, Network*, char*, char*, int);
147 char considered; /* flag: ignored for "net!"? */
148 char fasttimeouthack; /* flag. was for IL */
160 * net doesn't apply to (r)udp, icmp(v6), or telco (for speed).
162 Network network[] = {
163 { "il", iplookup, iptrans, 0, 1, 1, 0, },
164 { "tcp", iplookup, iptrans, 0, 0, 0, 0, },
165 { "il", iplookup, iptrans, 0, 0, 1, 0, },
166 { "udp", iplookup, iptrans, 1, 0, 0, 0, },
167 { "icmp", iplookup, iptrans, 1, 0, 1, 0, },
168 { "icmpv6", iplookup, iptrans, 1, 0, 0, 1, },
169 { "rudp", iplookup, iptrans, 1, 0, 1, 0, },
170 { "ssh", iplookup, iptrans, 1, 0, 0, 0, },
171 { "telco", telcolookup, telcotrans, 1, 0, 0, 0, },
177 static Ndbtuple* myipinfo(Ndb *db, char **list, int n);
181 Network *netlist; /* networks ordered by preference */
185 nstrcpy(char *to, char *from, int len)
187 strncpy(to, from, len);
194 fprint(2, "usage: %s [-dn] [-f ndb-file] [-x netmtpt]\n", argv0);
199 * based on libthread's threadsetname, but drags in less library code.
200 * actually just sets the arguments displayed.
203 procsetname(char *fmt, ...)
211 cmdname = vsmprint(fmt, arg);
215 snprint(buf, sizeof buf, "#p/%d/args", getpid());
216 if((fd = open(buf, OWRITE)) >= 0){
217 write(fd, cmdname, strlen(cmdname)+1);
224 main(int argc, char *argv[])
227 char ext[Maxpath], servefile[Maxpath];
230 setnetmtpt(mntpt, sizeof(mntpt), nil);
240 dbfile = EARGF(usage());
246 setnetmtpt(mntpt, sizeof(mntpt), EARGF(usage()));
247 setext(ext, sizeof(ext), mntpt);
253 snprint(netndb, sizeof(netndb), "%s/ndb", mntpt);
255 fmtinstall('E', eipfmt);
256 fmtinstall('I', eipfmt);
257 fmtinstall('M', eipfmt);
258 fmtinstall('F', fcallfmt);
264 snprint(servefile, sizeof(servefile), "#s/cs%s", ext);
265 unmount(servefile, mntpt);
268 rfork(RFREND|RFNOTEG);
269 csuser = estrdup(getuser());
270 mountinit(servefile, mntpt);
277 * if a mount point is specified, set the cs extention to be the mount point
278 * with '_'s replacing '/'s
281 setext(char *ext, int n, char *p)
286 for(i = 0; i < n; i++){
298 mountinit(char *service, char *mntpt)
305 error("pipe failed");
310 f = create(service, OWRITE|ORCLOSE, 0666);
313 snprint(buf, sizeof(buf), "%d", p[1]);
314 if(write(f, buf, strlen(buf)) != strlen(buf))
315 error("write /srv/cs");
317 switch(rfork(RFFDG|RFPROC|RFNAMEG)){
320 procsetname("%s", mntpt);
323 error("fork failed");
326 * put ourselves into the file system
329 if(mount(p[1], -1, mntpt, MAFTER, "") < 0)
330 error("mount failed");
333 mfd[0] = mfd[1] = p[0];
339 db = ndbopen(dbfile);
341 error("can't open network database");
343 for(netdb = db; netdb != nil; netdb = netdb->next)
344 if(strcmp(netdb->file, netndb) == 0)
347 netdb = ndbopen(netndb);
350 db = ndbcat(netdb, db);
361 for(f = mlist; f != nil; f = f->next)
362 if(f->mf.busy && f->mf.fid == fid)
364 else if(ff == nil && !f->mf.busy && !f->mf.ref)
367 ff = emalloc(sizeof *f);
372 memset(mf, 0, sizeof *mf);
382 job = emalloc(sizeof *job);
386 job->request.tag = -1;
397 for(l = &joblist; *l != nil; l = &(*l)->next){
413 for(job = joblist; job != nil; job = job->next){
414 if(job->request.tag == tag && job->request.type != Tflush){
428 uchar mdata[IOHDRSZ + Maxfdata];
432 * if we ask dns to fulfill requests,
433 * a slave process is created to wait for replies. The
434 * master process returns immediately via a longjmp
435 * through 'masterjmp'.
437 * *isslave is a pointer into the call stack to a variable
438 * that tells whether or not the current process is a slave.
440 slaveflag = 0; /* init slave variable */
441 isslave = &slaveflag;
445 n = read9pmsg(mfd[0], mdata, sizeof mdata);
451 if(convM2S(mdata, n, &job->request) != n){
452 syslog(1, logfile, "format error %ux %ux %ux %ux %ux",
453 mdata[0], mdata[1], mdata[2], mdata[3], mdata[4]);
458 mf = newfid(job->request.fid);
460 syslog(0, logfile, "%F", &job->request);
462 switch(job->request.type){
464 syslog(1, logfile, "unknown request type %d", job->request.type);
511 * slave processes die after replying
515 syslog(0, logfile, "slave death %d", getpid());
525 if(job->request.msize > IOHDRSZ + Maxfdata)
526 job->reply.msize = IOHDRSZ + Maxfdata;
528 job->reply.msize = job->request.msize;
529 if(strncmp(job->request.version, "9P2000", 6) != 0)
530 sendmsg(job, "unknown 9P version");
532 job->reply.version = "9P2000";
540 sendmsg(job, "cs: authentication not required");
544 * don't flush till all the slaves are done
549 flushjob(job->request.oldtag);
554 rattach(Job *job, Mfile *mf)
558 mf->user = estrdup(job->request.uname);
560 mf->qid.vers = vers++;
561 mf->qid.type = QTDIR;
563 job->reply.qid = mf->qid;
569 rwalk(Job *job, Mfile *mf)
580 elems = job->request.wname;
581 nelems = job->request.nwname;
582 job->reply.nwqid = 0;
584 if(job->request.newfid != job->request.fid){
586 nmf = newfid(job->request.newfid);
589 err = "clone to used channel";
593 nmf->user = estrdup(mf->user);
594 nmf->fid = job->request.newfid;
595 nmf->qid.vers = vers++;
598 /* else nmf will be nil */
603 for(i=0; i<nelems && i<MAXWELEM; i++){
604 if((qid.type & QTDIR) == 0){
605 err = "not a directory";
608 if(strcmp(elems[i], "..") == 0 || strcmp(elems[i], ".") == 0){
612 job->reply.wqid[i] = qid;
616 if(strcmp(elems[i], "cs") == 0){
621 err = "file does not exist";
627 if(nmf != nil && (err!=nil || job->reply.nwqid<nelems)){
641 ropen(Job *job, Mfile *mf)
647 mode = job->request.mode;
648 if(mf->qid.type & QTDIR){
650 err = "permission denied";
652 job->reply.qid = mf->qid;
653 job->reply.iounit = 0;
658 rcreate(Job *job, Mfile *mf)
661 sendmsg(job, "creation permission denied");
665 rread(Job *job, Mfile *mf)
668 long off, toff, clock;
675 off = job->request.offset;
676 cnt = job->request.count;
679 if(mf->qid.type & QTDIR){
682 memset(&dir, 0, sizeof dir);
684 dir.qid.type = QTFILE;
692 dir.atime = clock; /* wrong */
693 dir.mtime = clock; /* wrong */
694 n = convD2M(&dir, buf, sizeof buf);
696 job->reply.data = (char*)buf;
701 /* look for an answer at the right offset */
703 for(i = 0; mf->reply[i] != nil && i < mf->nreply; i++){
710 break; /* got something to return */
712 /* try looking up more answers */
713 if(lookup(mf) == 0 || job->flushed){
720 /* give back a single reply (or part of one) */
721 job->reply.data = mf->reply[i] + (off - toff);
722 if(cnt > toff - off + n)
728 job->reply.count = n;
731 if(--mf->ref == 0 && mf->busy == 0)
756 for(i = 0; i < mf->nreply; i++){
762 mf->nextnet = netlist;
766 rwrite(Job *job, Mfile *mf)
774 cnt = job->request.count;
775 if(mf->qid.type & QTDIR){
776 err = "can't write directory";
779 if(cnt >= Maxrequest){
780 err = "request too long";
783 job->request.data[cnt] = 0;
785 if(strcmp(mf->user, "none") == 0 || strcmp(mf->user, csuser) != 0)
786 goto query; /* skip special commands if not owner */
791 if(strncmp(job->request.data, "debug", 5)==0){
793 syslog(1, logfile, "debug %d", debug);
798 * toggle ipv6 lookups
800 if(strncmp(job->request.data, "ipv6", 4)==0){
802 syslog(1, logfile, "ipv6lookups %d", ipv6lookups);
807 * add networks to the default list
809 if(strncmp(job->request.data, "add ", 4)==0){
810 if(job->request.data[cnt-1] == '\n')
811 job->request.data[cnt-1] = 0;
812 netadd(job->request.data+4);
820 if(strncmp(job->request.data, "refresh", 7)==0){
827 err = "query already in progress";
832 /* start transaction with a clean slate */
836 * look for a general query
838 if(*job->request.data == '!'){
839 err = genquery(mf, job->request.data+1);
844 syslog(0, logfile, "write %s", job->request.data);
848 n = getfields(job->request.data, field, 4, 1, "!");
851 mf->net = estrdup("net");
852 mf->host = estrdup(field[0]);
855 mf->rem = estrdup(field[3]);
858 mf->serv = estrdup(field[2]);
861 mf->host = estrdup(field[1]);
862 mf->net = estrdup(field[0]);
867 * do the first net worth of lookup
870 rerrstr(curerr, sizeof curerr);
875 if(--mf->ref == 0 && mf->busy == 0)
879 job->reply.count = cnt;
884 rclunk(Job *job, Mfile *mf)
896 rremove(Job *job, Mfile *mf)
899 sendmsg(job, "remove permission denied");
903 rstat(Job *job, Mfile *mf)
906 uchar buf[IOHDRSZ+Maxfdata];
908 memset(&dir, 0, sizeof dir);
909 if(mf->qid.type & QTDIR){
911 dir.mode = DMDIR|0555;
921 dir.atime = dir.mtime = time(0);
922 job->reply.nstat = convD2M(&dir, buf, sizeof buf);
923 job->reply.stat = buf;
928 rwstat(Job *job, Mfile *mf)
931 sendmsg(job, "wstat permission denied");
935 sendmsg(Job *job, char *err)
938 uchar mdata[IOHDRSZ + Maxfdata];
942 job->reply.type = Rerror;
943 snprint(ename, sizeof(ename), "cs: %s", err);
944 job->reply.ename = ename;
946 job->reply.type = job->request.type+1;
948 job->reply.tag = job->request.tag;
949 n = convS2M(&job->reply, mdata, sizeof mdata);
951 syslog(1, logfile, "sendmsg convS2M of %F returns 0", &job->reply);
955 if(job->flushed == 0)
956 if(write(mfd[1], mdata, n)!=n)
957 error("mount write");
960 syslog(0, logfile, "%F %d", &job->reply, n);
966 syslog(1, logfile, "%s: %r", s);
973 return ipcmp(ip, IPnoaddr) != 0 && ipcmp(ip, v4prefix) != 0;
977 readipinterfaces(void)
980 ipifcs = readipifc(mntpt, ipifcs, -1);
985 * get the system name
990 char eaddr[16], buf[Maxpath];
1002 * environment has priority.
1004 * on the sgi power the default system name
1005 * is the ip address. ignore that.
1008 p = getenv("sysname");
1009 if(p != nil && *p != 0){
1011 if(strcmp(attr, "ip") != 0) {
1018 /* try configured interfaces */
1020 t = s.t = myipinfo(db, &attr, 1);
1024 /* try ethernet interfaces */
1027 f = open(mntpt, OREAD);
1029 n = dirreadall(f, &d);
1032 for(f = 0; f < n; f++){
1033 if((d[f].mode & DMDIR) == 0 || strncmp(d[f].name, "ether", 5) != 0)
1035 snprint(buf, sizeof buf, "%s/%s", mntpt, d[f].name);
1036 if(myetheraddr(addr, buf) >= 0){
1037 snprint(eaddr, sizeof(eaddr), "%E", addr);
1038 free(ndbgetvalue(db, &s, "ether", eaddr, "sys", &t));
1047 /* nothing else worked, use ip address */
1049 t = s.t = myipinfo(db, &attr, 1);
1054 /* found in database */
1055 if((tt = ndbfindattr(t, s.t, "sys")) != nil)
1056 mysysname = estrdup(tt->val);
1057 else if((tt = ndbfindattr(t, s.t, "ip")) != nil)
1058 mysysname = estrdup(tt->val);
1061 if(mysysname == nil)
1065 /* set /dev/sysname if we now know it */
1066 f = open("/dev/sysname", OWRITE);
1068 write(f, mysysname, strlen(mysysname));
1074 * Set up a list of default networks by looking for
1078 netinit(int background)
1080 char clone[Maxpath];
1084 switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
1093 /* add the mounted networks to the default list */
1094 for(np = network; np->net != nil; np++){
1097 snprint(clone, sizeof(clone), "%s/%s/clone", mntpt, np->net);
1098 if(access(clone, AEXIST) < 0)
1109 /* find out what our ip addresses are */
1112 /* set the system name if we need to, these days ip is all we have */
1116 syslog(0, logfile, "mysysname %s", mysysname?mysysname:"???");
1125 * add networks to the standard list
1134 n = getfields(p, field, 12, 1, " ");
1135 for(i = 0; i < n; i++){
1136 for(np = network; np->net != nil; np++){
1137 if(strcmp(field[i], np->net) != 0)
1153 lookforproto(Ndbtuple *t, char *proto)
1155 for(; t != nil; t = t->entry)
1156 if(strcmp(t->attr, "proto") == 0 && strcmp(t->val, proto) == 0)
1162 * lookup a request. the network "net" means we should pick the
1163 * best network to get there.
1171 char reply[Maxreply];
1175 /* open up the standard db files */
1179 error("can't open mf->network database\n");
1182 return 0; /* must have been a genquery */
1185 if(strcmp(mf->net, "net") == 0){
1187 * go through set of default nets
1189 for(np = mf->nextnet; np != nil && rv == 0; np = np->next){
1190 nt = (*np->lookup)(np, mf->host, mf->serv);
1193 hack = np->fasttimeouthack && !lookforproto(nt, np->net);
1194 for(t = nt; mf->nreply < Nreply && t != nil; t = t->entry){
1195 cp = (*np->trans)(t, np, mf->serv, mf->rem, hack);
1197 /* avoid duplicates */
1198 for(i = 0; i < mf->nreply; i++)
1199 if(strcmp(mf->reply[i], cp) == 0)
1201 if(i == mf->nreply){
1202 /* save the reply */
1203 mf->replylen[mf->nreply] = strlen(cp);
1204 mf->reply[mf->nreply++] = cp;
1217 * if not /net, we only get one lookup
1223 * look for a specific network
1225 for(np = network; np->net != nil; np++){
1226 if(np->fasttimeouthack)
1228 if(strcmp(np->net, mf->net) == 0)
1236 nt = (*np->lookup)(np, mf->host, mf->serv);
1237 for(t = nt; mf->nreply < Nreply && t != nil; t = t->entry){
1238 cp = (*np->trans)(t, np, mf->serv, mf->rem, 0);
1240 mf->replylen[mf->nreply] = strlen(cp);
1241 mf->reply[mf->nreply++] = cp;
1249 * not a known network, don't translate host or service
1252 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s",
1253 mntpt, mf->net, mf->host, mf->serv);
1255 snprint(reply, sizeof(reply), "%s/%s/clone %s",
1256 mntpt, mf->net, mf->host);
1257 mf->reply[0] = estrdup(reply);
1258 mf->replylen[0] = strlen(reply);
1265 * translate an ip service name into a port number. If it's a numeric port
1266 * number, look for restricted access.
1268 * the service '*' needs no translation.
1271 ipserv(Network *np, char *name, char *buf, int blen)
1279 /* '*' means any service */
1280 if(strcmp(name, "*") == 0){
1281 nstrcpy(buf, name, blen);
1285 /* see if it's numeric or symbolic */
1286 for(p = name; *p; p++){
1289 else if(isalpha(*p) || *p == '-' || *p == '$')
1297 p = ndbgetvalue(db, &s, np->net, name, "port", &t);
1301 /* look up only for tcp ports < 1024 to get the restricted
1304 if(atoi(name) < 1024 && strcmp(np->net, "tcp") == 0)
1305 p = ndbgetvalue(db, &s, "port", name, "port", &t);
1311 for(nt = t; nt != nil; nt = nt->entry)
1312 if(strcmp(nt->attr, "restricted") == 0)
1316 snprint(buf, blen, "%s%s", p, restr ? "!r" : "");
1323 myipinfo(Ndb *db, char **list, int n)
1332 for(ifc = ipifcs; ifc != nil; ifc = ifc->next){
1333 for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
1334 snprint(ip, sizeof(ip), "%I", lifc->ip);
1335 nt = ndbipinfo(db, "ip", ip, list, n);
1336 t = ndbconcatenate(t, nt);
1339 qunlock(&ipifclock);
1345 * reorder according to our interfaces
1348 ipreorder(Ndbtuple *t)
1351 uchar ip[IPaddrlen];
1352 uchar net[IPaddrlen];
1353 uchar tnet[IPaddrlen];
1361 for(ifc = ipifcs; ifc != nil; ifc = ifc->next){
1362 for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
1363 maskip(lifc->ip, lifc->mask, net);
1364 for(nt = t; nt != nil; nt = nt->entry){
1365 if(strcmp(nt->attr, "ip") != 0)
1367 if(parseip(ip, nt->val) == -1)
1369 maskip(ip, lifc->mask, tnet);
1370 if(memcmp(net, tnet, IPaddrlen) == 0){
1371 qunlock(&ipifclock);
1372 return ndbreorder(t, nt);
1377 qunlock(&ipifclock);
1385 uchar ip[IPaddrlen];
1386 return parseip(ip, s) != -1 && isv4(ip);
1390 ndbline(Ndbtuple *t)
1394 for(nt = t; nt != nil; nt = nt->entry){
1395 if(nt->entry == nil)
1398 nt->line = nt->entry;
1404 * lookup an ip destination
1407 iplookuphost(Network *np, char *host)
1409 char *attr, *dnsname;
1414 * turn '[ip address]' into just 'ip address'
1417 char tmp[Maxhost], *x;
1419 nstrcpy(tmp, host, sizeof tmp);
1421 if((x = strchr(++host, ']')) != nil)
1425 /* for dial strings with no host */
1426 if(strcmp(host, "*") == 0)
1427 return ndbline(ndbnew("ip", "*"));
1430 * hack till we go v6 :: = 0.0.0.0
1432 if(strcmp("::", host) == 0)
1433 return ndbline(ndbnew("ip", "*"));
1436 * just accept addresses
1438 attr = ipattr(host);
1439 if(strcmp(attr, "ip") == 0)
1440 return ndbline(ndbnew("ip", host));
1443 * give the domain name server the first opportunity to
1444 * resolve domain names. if that fails try the database.
1447 if(strcmp(attr, "dom") == 0)
1448 t = dnsiplookup(host, &s, !np->v4only);
1450 for(nt = ndbsearch(db, &s, attr, host); nt != nil; nt = ndbsnext(&s, attr, host)){
1451 if(ndbfindattr(nt, s.t, "ip") == nil){
1455 t = ndbconcatenate(t, ndbreorder(nt, s.t));
1460 if(strcmp(attr, "dom") != 0){
1461 dnsname = ndbgetvalue(db, &s, attr, host, "dom", nil);
1463 t = dnsiplookup(dnsname, &s, !np->v4only);
1468 t = dnsiplookup(host, &s, !np->v4only);
1474 * reorder the tuple to have the matched line first and
1475 * save that in the request structure.
1477 return ndbreorder(t, s.t);
1482 iplookup(Network *np, char *host, char *serv)
1484 Ndbtuple *l, *t, *nt;
1485 char ts[Maxservice], *attr;
1488 * start with the service since it's the most likely to fail
1489 * and costs the least
1491 if(serv == nil || ipserv(np, serv, ts, sizeof ts) == nil){
1492 werrstr("can't translate service");
1497 * '$' means the rest of the name is an attribute that we
1498 * need to search for
1500 werrstr("can't translate address");
1504 l = myipinfo(db, &attr, 1);
1505 for(nt = l; nt != nil; nt = nt->entry){
1506 if(strcmp(nt->attr, attr) == 0)
1507 t = ndbconcatenate(t, iplookuphost(np, nt->val));
1511 t = iplookuphost(np, host);
1513 return ipreorder(t);
1518 * translate an ip address
1521 iptrans(Ndbtuple *t, Network *np, char *serv, char *rem, int hack)
1523 char ts[Maxservice];
1524 char reply[Maxreply];
1527 if(strcmp(t->attr, "ip") != 0)
1530 if(serv == nil || ipserv(np, serv, ts, sizeof ts) == nil){
1531 werrstr("can't translate service");
1536 snprint(x, sizeof(x), "!%s", rem);
1541 snprint(reply, sizeof(reply), "%s/%s/clone %s%s",
1542 mntpt, np->net, ts, x);
1544 if(np->v4only && !isv4str(t->val))
1547 if(np->v6only && isv4str(t->val))
1550 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s%s%s",
1551 mntpt, np->net, t->val, ts, x, hack? "!fasttimeout": "");
1554 return estrdup(reply);
1558 * lookup a telephone number
1561 telcolookup(Network *np, char *host, char *serv)
1568 werrstr("can't translate address");
1569 free(ndbgetvalue(db, &s, "sys", host, "telco", &t));
1571 return ndbnew("telco", host);
1573 return ndbreorder(t, s.t);
1577 * translate a telephone address
1580 telcotrans(Ndbtuple *t, Network *np, char *serv, char *rem, int)
1582 char reply[Maxreply];
1585 if(strcmp(t->attr, "telco") != 0)
1589 snprint(x, sizeof(x), "!%s", rem);
1593 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s%s", mntpt, np->net,
1596 snprint(reply, sizeof(reply), "%s/%s/clone %s%s", mntpt, np->net,
1598 return estrdup(reply);
1602 * create a slave process to handle a request to avoid one request blocking
1603 * another. parent returns to job loop.
1609 return; /* we're already a slave process */
1610 if(ainc(&active) >= Maxactive){
1614 switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
1621 syslog(0, logfile, "slave %d", getpid());
1622 procsetname("%s", host);
1625 longjmp(masterjmp, 1);
1631 dnsip6lookup(char *mntpt, char *buf, Ndbtuple *t)
1635 t6 = dnsquery(mntpt, buf, "ipv6"); /* lookup AAAA dns RRs */
1639 /* convert ipv6 attr to ip */
1640 for (tt = t6; tt != nil; tt = tt->entry)
1641 if (strcmp(tt->attr, "ipv6") == 0)
1642 strcpy(tt->attr, "ip");
1644 /* append t6 list to t list */
1645 return ndbconcatenate(t, t6);
1649 * call the dns process and have it try to translate a name
1652 dnsiplookup(char *host, Ndbs *s, int v6)
1661 werrstr("too much activity");
1665 if(strcmp(ipattr(host), "ip") == 0)
1666 t = dnsquery(mntpt, host, "ptr");
1668 t = dnsquery(mntpt, host, "ip");
1669 /* special case: query ipv6 (AAAA dns RR) too */
1670 if (v6 && ipv6lookups)
1671 t = dnsip6lookup(mntpt, host, t);
1676 rerrstr(buf, sizeof buf);
1677 if(strstr(buf, "exist") != nil)
1678 werrstr("can't translate address: %s", buf);
1679 else if(strstr(buf, "dns failure") != nil)
1680 werrstr("temporary problem: %s", buf);
1688 qmatch(Ndbtuple *t, char **attr, char **val, int n)
1693 for(i = 1; i < n; i++){
1695 for(nt = t; nt != nil; nt = nt->entry)
1696 if(strcmp(attr[i], nt->attr) == 0)
1697 if(strcmp(val[i], "*") == 0
1698 || strcmp(val[i], nt->val) == 0){
1709 qreply(Mfile *mf, Ndbtuple *t)
1715 for(nt = t; mf->nreply < Nreply && nt != nil; nt = nt->entry){
1716 s_append(s, nt->attr);
1718 s_append(s, nt->val);
1720 if(nt->line != nt->entry){
1721 mf->replylen[mf->nreply] = s_len(s);
1722 mf->reply[mf->nreply++] = estrdup(s_to_c(s));
1736 * generic query lookup. The query is of one of the following
1739 * attr1=val1 attr2=val2 attr3=val3 ...
1741 * returns the matching tuple
1743 * ipinfo attr=val attr1 attr2 attr3 ...
1745 * is like ipinfo and returns the attr{1-n}
1746 * associated with the ip address.
1749 genquery(Mfile *mf, char *query)
1753 char *attr[Maxattr];
1758 n = getfields(query, attr, nelem(attr), 1, " ");
1762 if(strcmp(attr[0], "ipinfo") == 0)
1763 return ipinfoquery(mf, attr, n);
1766 for(i = 0; i < n; i++){
1767 p = strchr(attr[i], '=');
1774 /* give dns a chance */
1775 if((strcmp(attr[0], "dom") == 0 || strcmp(attr[0], "ip") == 0) && val[0]){
1776 t = dnsiplookup(val[0], &s, ipv6lookups);
1778 if(qmatch(t, attr, val, n)){
1787 /* first pair is always the key. It can't be a '*' */
1788 t = ndbsearch(db, &s, attr[0], val[0]);
1790 /* search is the and of all the pairs */
1792 if(qmatch(t, attr, val, n)){
1799 t = ndbsnext(&s, attr[0], val[0]);
1806 * resolve an ip address
1809 ipresolve(char *attr, char *host)
1811 Ndbtuple *t, *nt, **l;
1813 t = iplookup(&network[Ntcp], host, "*");
1814 for(l = &t; *l != nil; ){
1816 if(strcmp(nt->attr, "ip") != 0){
1822 nstrcpy(nt->attr, attr, sizeof(nt->attr));
1829 ipinfoquery(Mfile *mf, char **list, int n)
1832 uchar resolve[Maxattr];
1833 Ndbtuple *t, *nt, *tt, **l;
1842 /* get search attribute=value, or assume myip */
1844 if((val = strchr(attr, '=')) != nil){
1857 * don't let ndbipinfo resolve the addresses, we're
1861 for(i = 0; i < n; i++)
1862 if(*list[i] == '@'){ /* @attr=val ? */
1864 resolve[i] = 1; /* we'll resolve it */
1870 t = myipinfo(db, list, n);
1872 t = ndbipinfo(db, attr, val, list, n);
1877 /* remove duplicates */
1878 for(nt = t; nt != nil; nt = nt->entry){
1879 for(l = &nt->entry; (tt = *l) != nil;){
1880 if(strcmp(nt->attr, tt->attr) != 0
1881 || strcmp(nt->val, tt->val) != 0){
1892 for(l = &t; *l != nil;){
1895 /* already an address? */
1896 if(strcmp(ipattr(nt->val), "ip") == 0){
1901 /* user wants it resolved? */
1902 for(i = 0; i < n; i++)
1903 if(strcmp(list[i], nt->attr) == 0)
1905 if(i >= n || resolve[i] == 0){
1910 /* resolve address and replace entry */
1911 *l = ipresolve(nt->attr, nt->val);
1921 /* make it all one line */
1936 error("out of memory");
1950 error("out of memory");
1951 memmove(p, s, size);