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
85 jmp_buf masterjmp; /* return through here after a slave process has been created */
86 int *isslave; /* *isslave non-zero means this is a slave process */
87 long active; /* number of active slaves */
94 void rattach(Job*, Mfile*);
95 char* rwalk(Job*, Mfile*);
96 void ropen(Job*, Mfile*);
97 void rcreate(Job*, Mfile*);
98 void rread(Job*, Mfile*);
99 void rwrite(Job*, Mfile*);
100 void rclunk(Job*, Mfile*);
101 void rremove(Job*, Mfile*);
102 void rstat(Job*, Mfile*);
103 void rwstat(Job*, Mfile*);
105 void sendmsg(Job*, char*);
107 void mountinit(char*, char*);
112 char *genquery(Mfile*, char*);
113 char* ipinfoquery(Mfile*, char**, int);
114 int needproto(Network*, Ndbtuple*);
116 Ndbtuple* reorder(Ndbtuple*, Ndbtuple*);
118 void readipinterfaces(void);
120 char* estrdup(char*);
123 void setext(char*, int, char*);
124 void cleanmf(Mfile*);
126 QLock dblock; /* mutex on database operations */
127 QLock netlock; /* mutex for netinit() */
129 char *logfile = "cs";
130 char *paranoiafile = "cs.paranoia";
133 char netndb[Maxpath];
136 * Network specific translators
138 Ndbtuple* iplookup(Network*, char*, char*, int);
139 char* iptrans(Ndbtuple*, Network*, char*, char*, int);
140 Ndbtuple* telcolookup(Network*, char*, char*, int);
141 char* telcotrans(Ndbtuple*, Network*, char*, char*, int);
142 Ndbtuple* dnsiplookup(char*, Ndbs*);
147 Ndbtuple *(*lookup)(Network*, char*, char*, int);
148 char *(*trans)(Ndbtuple*, Network*, char*, char*, int);
149 int considered; /* flag: ignored for "net!"? */
150 int fasttimeouthack; /* flag. was for IL */
159 * net doesn't apply to (r)udp, icmp(v6), or telco (for speed).
161 Network network[] = {
162 { "il", iplookup, iptrans, 0, 1, },
163 { "tcp", iplookup, iptrans, 0, 0, },
164 { "il", iplookup, iptrans, 0, 0, },
165 { "udp", iplookup, iptrans, 1, 0, },
166 { "icmp", iplookup, iptrans, 1, 0, },
167 { "icmpv6", iplookup, iptrans, 1, 0, },
168 { "rudp", iplookup, iptrans, 1, 0, },
169 { "ssh", iplookup, iptrans, 1, 0, },
170 { "telco", telcolookup, telcotrans, 1, 0, },
177 char eaddr[16]; /* ascii ethernet address */
178 char ipaddr[64]; /* ascii internet address */
179 uchar ipa[IPaddrlen]; /* binary internet address */
182 Network *netlist; /* networks ordered by preference */
186 nstrcpy(char *to, char *from, int len)
188 strncpy(to, from, len);
195 fprint(2, "usage: %s [-dn] [-f ndb-file] [-x netmtpt]\n", argv0);
200 * based on libthread's threadsetname, but drags in less library code.
201 * actually just sets the arguments displayed.
204 procsetname(char *fmt, ...)
212 cmdname = vsmprint(fmt, arg);
216 snprint(buf, sizeof buf, "#p/%d/args", getpid());
217 if((fd = open(buf, OWRITE)) >= 0){
218 write(fd, cmdname, strlen(cmdname)+1);
225 main(int argc, char *argv[])
228 char ext[Maxpath], servefile[Maxpath];
231 setnetmtpt(mntpt, sizeof(mntpt), nil);
241 dbfile = EARGF(usage());
247 setnetmtpt(mntpt, sizeof(mntpt), EARGF(usage()));
248 setext(ext, sizeof(ext), mntpt);
254 snprint(netndb, sizeof(netndb), "%s/ndb", mntpt);
256 fmtinstall('E', eipfmt);
257 fmtinstall('I', eipfmt);
258 fmtinstall('M', eipfmt);
259 fmtinstall('F', fcallfmt);
265 snprint(servefile, sizeof(servefile), "#s/cs%s", ext);
266 unmount(servefile, mntpt);
269 rfork(RFREND|RFNOTEG);
270 csuser = estrdup(getuser());
271 mountinit(servefile, mntpt);
278 * if a mount point is specified, set the cs extention to be the mount point
279 * with '_'s replacing '/'s
282 setext(char *ext, int n, char *p)
287 for(i = 0; i < n; i++){
299 mountinit(char *service, char *mntpt)
306 error("pipe failed");
311 f = create(service, OWRITE|ORCLOSE, 0666);
314 snprint(buf, sizeof(buf), "%d", p[1]);
315 if(write(f, buf, strlen(buf)) != strlen(buf))
316 error("write /srv/cs");
318 switch(rfork(RFFDG|RFPROC|RFNAMEG)){
321 procsetname("%s", mntpt);
324 error("fork failed\n");
327 * put ourselves into the file system
330 if(mount(p[1], -1, mntpt, MAFTER, "") < 0)
331 error("mount failed\n");
334 mfd[0] = mfd[1] = p[0];
340 db = ndbopen(dbfile);
342 error("can't open network database");
344 for(netdb = db; netdb; netdb = netdb->next)
345 if(strcmp(netdb->file, netndb) == 0)
348 netdb = ndbopen(netndb);
351 db = ndbcat(netdb, db);
362 for(f = mlist; f; f = f->next)
363 if(f->mf.busy && f->mf.fid == fid)
365 else if(!ff && !f->mf.busy && !f->mf.ref)
368 ff = emalloc(sizeof *f);
373 memset(mf, 0, sizeof *mf);
383 job = emalloc(sizeof *job);
387 job->request.tag = -1;
398 for(l = &joblist; *l; l = &(*l)->next){
414 for(job = joblist; job; job = job->next){
415 if(job->request.tag == tag && job->request.type != Tflush){
429 uchar mdata[IOHDRSZ + Maxfdata];
433 * if we ask dns to fulfill requests,
434 * a slave process is created to wait for replies. The
435 * master process returns immediately via a longjmp
436 * through 'masterjmp'.
438 * *isslave is a pointer into the call stack to a variable
439 * that tells whether or not the current process is a slave.
441 slaveflag = 0; /* init slave variable */
442 isslave = &slaveflag;
446 n = read9pmsg(mfd[0], mdata, sizeof mdata);
452 if(convM2S(mdata, n, &job->request) != n){
453 syslog(1, logfile, "format error %ux %ux %ux %ux %ux",
454 mdata[0], mdata[1], mdata[2], mdata[3], mdata[4]);
459 mf = newfid(job->request.fid);
461 syslog(0, logfile, "%F", &job->request);
463 switch(job->request.type){
465 syslog(1, logfile, "unknown request type %d", job->request.type);
512 * slave processes die after replying
516 syslog(0, logfile, "slave death %d", getpid());
526 if(job->request.msize > IOHDRSZ + Maxfdata)
527 job->reply.msize = IOHDRSZ + Maxfdata;
529 job->reply.msize = job->request.msize;
530 if(strncmp(job->request.version, "9P2000", 6) != 0)
531 sendmsg(job, "unknown 9P version");
533 job->reply.version = "9P2000";
541 sendmsg(job, "cs: authentication not required");
545 * don't flush till all the slaves are done
550 flushjob(job->request.oldtag);
555 rattach(Job *job, Mfile *mf)
559 mf->user = estrdup(job->request.uname);
561 mf->qid.vers = vers++;
562 mf->qid.type = QTDIR;
564 job->reply.qid = mf->qid;
570 rwalk(Job *job, Mfile *mf)
581 elems = job->request.wname;
582 nelems = job->request.nwname;
583 job->reply.nwqid = 0;
585 if(job->request.newfid != job->request.fid){
587 nmf = newfid(job->request.newfid);
590 err = "clone to used channel";
594 nmf->user = estrdup(mf->user);
595 nmf->fid = job->request.newfid;
596 nmf->qid.vers = vers++;
599 /* else nmf will be nil */
604 for(i=0; i<nelems && i<MAXWELEM; i++){
605 if((qid.type & QTDIR) == 0){
606 err = "not a directory";
609 if(strcmp(elems[i], "..") == 0 || strcmp(elems[i], ".") == 0){
613 job->reply.wqid[i] = qid;
617 if(strcmp(elems[i], "cs") == 0){
622 err = "file does not exist";
628 if(nmf != nil && (err!=nil || job->reply.nwqid<nelems)){
642 ropen(Job *job, Mfile *mf)
648 mode = job->request.mode;
649 if(mf->qid.type & QTDIR){
651 err = "permission denied";
653 job->reply.qid = mf->qid;
654 job->reply.iounit = 0;
659 rcreate(Job *job, Mfile *mf)
662 sendmsg(job, "creation permission denied");
666 rread(Job *job, Mfile *mf)
669 long off, toff, clock;
676 off = job->request.offset;
677 cnt = job->request.count;
680 if(mf->qid.type & QTDIR){
683 memset(&dir, 0, sizeof dir);
685 dir.qid.type = QTFILE;
693 dir.atime = clock; /* wrong */
694 dir.mtime = clock; /* wrong */
695 n = convD2M(&dir, buf, sizeof buf);
697 job->reply.data = (char*)buf;
702 /* look for an answer at the right offset */
704 for(i = 0; mf->reply[i] && i < mf->nreply; i++){
711 break; /* got something to return */
713 /* try looking up more answers */
714 if(lookup(mf) == 0 || job->flushed){
721 /* give back a single reply (or part of one) */
722 job->reply.data = mf->reply[i] + (off - toff);
723 if(cnt > toff - off + n)
729 job->reply.count = n;
732 if(--mf->ref == 0 && mf->busy == 0)
757 for(i = 0; i < mf->nreply; i++){
763 mf->nextnet = netlist;
767 rwrite(Job *job, Mfile *mf)
775 cnt = job->request.count;
776 if(mf->qid.type & QTDIR){
777 err = "can't write directory";
780 if(cnt >= Maxrequest){
781 err = "request too long";
784 job->request.data[cnt] = 0;
786 if(strcmp(mf->user, "none") == 0 || strcmp(mf->user, csuser) != 0)
787 goto query; /* skip special commands if not owner */
792 if(strncmp(job->request.data, "debug", 5)==0){
794 syslog(1, logfile, "debug %d", debug);
799 * toggle ipv6 lookups
801 if(strncmp(job->request.data, "ipv6", 4)==0){
803 syslog(1, logfile, "ipv6lookups %d", ipv6lookups);
810 if(strncmp(job->request.data, "paranoia", 8)==0){
812 syslog(1, logfile, "paranoia %d", paranoia);
817 * add networks to the default list
819 if(strncmp(job->request.data, "add ", 4)==0){
820 if(job->request.data[cnt-1] == '\n')
821 job->request.data[cnt-1] = 0;
822 netadd(job->request.data+4);
830 if(strncmp(job->request.data, "refresh", 7)==0){
837 err = "query already in progress";
842 /* start transaction with a clean slate */
846 * look for a general query
848 if(*job->request.data == '!'){
849 err = genquery(mf, job->request.data+1);
854 syslog(0, logfile, "write %s", job->request.data);
856 syslog(0, paranoiafile, "write %s by %s", job->request.data, mf->user);
860 n = getfields(job->request.data, field, 4, 1, "!");
863 mf->net = estrdup("net");
864 mf->host = estrdup(field[0]);
867 mf->rem = estrdup(field[3]);
870 mf->serv = estrdup(field[2]);
873 mf->host = estrdup(field[1]);
874 mf->net = estrdup(field[0]);
879 * do the first net worth of lookup
882 rerrstr(curerr, sizeof curerr);
887 if(--mf->ref == 0 && mf->busy == 0)
891 job->reply.count = cnt;
896 rclunk(Job *job, Mfile *mf)
908 rremove(Job *job, Mfile *mf)
911 sendmsg(job, "remove permission denied");
915 rstat(Job *job, Mfile *mf)
918 uchar buf[IOHDRSZ+Maxfdata];
920 memset(&dir, 0, sizeof dir);
921 if(mf->qid.type & QTDIR){
923 dir.mode = DMDIR|0555;
933 dir.atime = dir.mtime = time(0);
934 job->reply.nstat = convD2M(&dir, buf, sizeof buf);
935 job->reply.stat = buf;
940 rwstat(Job *job, Mfile *mf)
943 sendmsg(job, "wstat permission denied");
947 sendmsg(Job *job, char *err)
950 uchar mdata[IOHDRSZ + Maxfdata];
954 job->reply.type = Rerror;
955 snprint(ename, sizeof(ename), "cs: %s", err);
956 job->reply.ename = ename;
958 job->reply.type = job->request.type+1;
960 job->reply.tag = job->request.tag;
961 n = convS2M(&job->reply, mdata, sizeof mdata);
963 syslog(1, logfile, "sendmsg convS2M of %F returns 0", &job->reply);
967 if(job->flushed == 0)
968 if(write(mfd[1], mdata, n)!=n)
969 error("mount write");
972 syslog(0, logfile, "%F %d", &job->reply, n);
978 syslog(1, logfile, "%s: %r", s);
985 return ipcmp(ip, IPnoaddr) != 0 && ipcmp(ip, v4prefix) != 0;
988 static uchar loopbacknet[IPaddrlen] = {
994 static uchar loopbackmask[IPaddrlen] = {
995 0xff, 0xff, 0xff, 0xff,
996 0xff, 0xff, 0xff, 0xff,
997 0xff, 0xff, 0xff, 0xff,
1002 readipinterfaces(void)
1004 if(myipaddr(ipa, mntpt) != 0)
1005 ipmove(ipa, IPnoaddr);
1006 sprint(ipaddr, "%I", ipa);
1008 syslog(0, logfile, "ipaddr is %s", ipaddr);
1012 * get the system name
1025 /* use environment, ether addr, or ipaddr to get system name */
1028 * environment has priority.
1030 * on the sgi power the default system name
1031 * is the ip address. ignore that.
1034 p = getenv("sysname");
1037 if(strcmp(attr, "ip") != 0)
1038 mysysname = estrdup(p);
1042 * the /net/ndb contains what the network
1043 * figured out from DHCP. use that name if
1046 if(mysysname == 0 && netdb != nil){
1048 for(tt = t = ndbparse(netdb); t != nil; t = t->entry){
1049 if(strcmp(t->attr, "sys") == 0){
1050 mysysname = estrdup(t->val);
1057 /* next network database, ip address, and ether address to find a name */
1061 free(ndbgetvalue(db, &s, "ip", ipaddr, "sys", &t));
1065 f = open(mntpt, OREAD);
1067 n = dirreadall(f, &d);
1070 for(f = 0; f < n; f++){
1071 if((d[f].mode & DMDIR) == 0 || strncmp(d[f].name, "ether", 5) != 0)
1073 snprint(buf, sizeof buf, "%s/%s", mntpt, d[f].name);
1074 if(myetheraddr(addr, buf) >= 0){
1075 snprint(eaddr, sizeof(eaddr), "%E", addr);
1076 free(ndbgetvalue(db, &s, "ether", eaddr, "sys", &t));
1083 for(tt = t; tt != nil; tt = tt->entry){
1084 if(strcmp(tt->attr, "sys") == 0){
1085 mysysname = estrdup(tt->val);
1092 /* nothing else worked, use the ip address */
1093 if(mysysname == 0 && isvalidip(ipa))
1094 mysysname = estrdup(ipaddr);
1097 /* set /dev/sysname if we now know it */
1099 f = open("/dev/sysname", OWRITE);
1101 write(f, mysysname, strlen(mysysname));
1109 * Set up a list of default networks by looking for
1113 netinit(int background)
1115 char clone[Maxpath];
1120 switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
1129 /* add the mounted networks to the default list */
1130 for(np = network; np->net; np++){
1133 snprint(clone, sizeof(clone), "%s/%s/clone", mntpt, np->net);
1134 if(access(clone, AEXIST) < 0)
1145 /* find out what our ip address is */
1148 /* set the system name if we need to, these days ip is all we have */
1152 syslog(0, logfile, "mysysname %s eaddr %s ipaddr %s ipa %I",
1153 mysysname?mysysname:"???", eaddr, ipaddr, ipa);
1162 * add networks to the standard list
1171 n = getfields(p, field, 12, 1, " ");
1172 for(i = 0; i < n; i++){
1173 for(np = network; np->net; np++){
1174 if(strcmp(field[i], np->net) != 0)
1190 lookforproto(Ndbtuple *t, char *proto)
1192 for(; t != nil; t = t->entry)
1193 if(strcmp(t->attr, "proto") == 0 && strcmp(t->val, proto) == 0)
1199 * lookup a request. the network "net" means we should pick the
1200 * best network to get there.
1208 char reply[Maxreply];
1212 /* open up the standard db files */
1216 error("can't open mf->network database\n");
1221 return 0; /* must have been a genquery */
1223 if(strcmp(mf->net, "net") == 0){
1225 * go through set of default nets
1227 for(np = mf->nextnet; np; np = np->next){
1228 nt = (*np->lookup)(np, mf->host, mf->serv, 1);
1231 hack = np->fasttimeouthack && !lookforproto(nt, np->net);
1232 for(t = nt; mf->nreply < Nreply && t; t = t->entry){
1233 cp = (*np->trans)(t, np, mf->serv, mf->rem, hack);
1235 /* avoid duplicates */
1236 for(i = 0; i < mf->nreply; i++)
1237 if(strcmp(mf->reply[i], cp) == 0)
1239 if(i == mf->nreply){
1240 /* save the reply */
1241 mf->replylen[mf->nreply] = strlen(cp);
1242 mf->reply[mf->nreply++] = cp;
1256 * if not /net, we only get one lookup
1261 * look for a specific network
1263 for(np = netlist; np && np->net != nil; np++){
1264 if(np->fasttimeouthack)
1266 if(strcmp(np->net, mf->net) == 0)
1270 if(np && np->net != nil){
1274 nt = (*np->lookup)(np, mf->host, mf->serv, 1);
1275 for(t = nt; mf->nreply < Nreply && t; t = t->entry){
1276 cp = (*np->trans)(t, np, mf->serv, mf->rem, 0);
1278 mf->replylen[mf->nreply] = strlen(cp);
1279 mf->reply[mf->nreply++] = cp;
1287 * not a known network, don't translate host or service
1290 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s",
1291 mntpt, mf->net, mf->host, mf->serv);
1293 snprint(reply, sizeof(reply), "%s/%s/clone %s",
1294 mntpt, mf->net, mf->host);
1295 mf->reply[0] = estrdup(reply);
1296 mf->replylen[0] = strlen(reply);
1303 * translate an ip service name into a port number. If it's a numeric port
1304 * number, look for restricted access.
1306 * the service '*' needs no translation.
1309 ipserv(Network *np, char *name, char *buf, int blen)
1318 /* '*' means any service */
1319 if(strcmp(name, "*")==0){
1320 nstrcpy(buf, name, blen);
1324 /* see if it's numeric or symbolic */
1326 for(p = name; *p; p++){
1329 else if(isalpha(*p) || *p == '-' || *p == '$')
1337 p = ndbgetvalue(db, &s, np->net, name, "port", &t);
1341 /* look up only for tcp ports < 1024 to get the restricted
1344 if(atoi(name) < 1024 && strcmp(np->net, "tcp") == 0)
1345 p = ndbgetvalue(db, &s, "port", name, "port", &t);
1351 for(nt = t; nt; nt = nt->entry)
1352 if(strcmp(nt->attr, "restricted") == 0)
1356 snprint(buf, blen, "%s%s", p, restr ? "!r" : "");
1363 * lookup an ip attribute
1366 ipattrlookup(Ndb *db, char *ipa, char *attr, char *val, int vlen)
1373 t = ndbipinfo(db, "ip", ipa, alist, 1);
1376 for(nt = t; nt != nil; nt = nt->entry){
1377 if(strcmp(nt->attr, attr) == 0){
1378 nstrcpy(val, nt->val, vlen);
1384 /* we shouldn't get here */
1390 * lookup (and translate) an ip destination
1393 iplookup(Network *np, char *host, char *serv, int nolookup)
1395 char *attr, *dnsname;
1398 char ts[Maxservice];
1399 char dollar[Maxhost];
1400 uchar ip[IPaddrlen];
1401 uchar net[IPaddrlen];
1402 uchar tnet[IPaddrlen];
1409 * start with the service since it's the most likely to fail
1410 * and costs the least
1412 werrstr("can't translate address");
1413 if(serv==0 || ipserv(np, serv, ts, sizeof ts) == 0){
1414 werrstr("can't translate service");
1418 /* for dial strings with no host */
1419 if(strcmp(host, "*") == 0)
1420 return ndbnew("ip", "*");
1423 * hack till we go v6 :: = 0.0.0.0
1425 if(strcmp("::", host) == 0)
1426 return ndbnew("ip", "*");
1430 * '$' means the rest of the name is an attribute that we
1431 * need to search for
1434 if(ipattrlookup(db, ipaddr, host+1, dollar, sizeof dollar))
1439 * turn '[ip address]' into just 'ip address'
1445 nstrcpy(dollar, host, sizeof dollar);
1448 if(x = strchr(++host, ']'))
1453 * just accept addresses
1455 attr = ipattr(host);
1456 if(strcmp(attr, "ip") == 0)
1457 return ndbnew("ip", host);
1460 * give the domain name server the first opportunity to
1461 * resolve domain names. if that fails try the database.
1464 werrstr("can't translate address");
1465 if(strcmp(attr, "dom") == 0)
1466 t = dnsiplookup(host, &s);
1468 free(ndbgetvalue(db, &s, attr, host, "ip", &t));
1470 dnsname = ndbgetvalue(db, &s, attr, host, "dom", nil);
1472 t = dnsiplookup(dnsname, &s);
1477 t = dnsiplookup(host, &s);
1482 * reorder the tuple to have the matched line first and
1483 * save that in the request structure.
1485 t = reorder(t, s.t);
1488 * reorder according to our interfaces
1491 for(ifc = ipifcs; ifc != nil; ifc = ifc->next){
1492 for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
1493 maskip(lifc->ip, lifc->mask, net);
1494 for(nt = t; nt; nt = nt->entry){
1495 if(strcmp(nt->attr, "ip") != 0)
1497 parseip(ip, nt->val);
1498 maskip(ip, lifc->mask, tnet);
1499 if(memcmp(net, tnet, IPaddrlen) == 0){
1501 qunlock(&ipifclock);
1507 qunlock(&ipifclock);
1513 * translate an ip address
1516 iptrans(Ndbtuple *t, Network *np, char *serv, char *rem, int hack)
1518 char ts[Maxservice];
1519 char reply[Maxreply];
1522 if(strcmp(t->attr, "ip") != 0)
1525 if(serv == 0 || ipserv(np, serv, ts, sizeof ts) == 0){
1526 werrstr("can't translate service");
1530 snprint(x, sizeof(x), "!%s", rem);
1535 snprint(reply, sizeof(reply), "%s/%s/clone %s%s",
1536 mntpt, np->net, ts, x);
1538 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s%s%s",
1539 mntpt, np->net, t->val, ts, x, hack? "!fasttimeout": "");
1541 return estrdup(reply);
1545 * lookup a telephone number
1548 telcolookup(Network *np, char *host, char *serv, int nolookup)
1553 USED(np, nolookup, serv);
1555 werrstr("can't translate address");
1556 free(ndbgetvalue(db, &s, "sys", host, "telco", &t));
1558 return ndbnew("telco", host);
1560 return reorder(t, s.t);
1564 * translate a telephone address
1567 telcotrans(Ndbtuple *t, Network *np, char *serv, char *rem, int)
1569 char reply[Maxreply];
1572 if(strcmp(t->attr, "telco") != 0)
1576 snprint(x, sizeof(x), "!%s", rem);
1580 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s%s", mntpt, np->net,
1583 snprint(reply, sizeof(reply), "%s/%s/clone %s%s", mntpt, np->net,
1585 return estrdup(reply);
1589 * reorder the tuple to put x's line first in the entry
1592 reorder(Ndbtuple *t, Ndbtuple *x)
1597 /* find start of this entry's line */
1598 for(line = x; line->entry == line->line; line = line->line)
1602 return t; /* already the first line */
1604 /* remove this line and everything after it from the entry */
1605 for(nt = t; nt->entry != line; nt = nt->entry)
1609 /* make that the start of the entry */
1610 for(nt = line; nt->entry; nt = nt->entry)
1617 * create a slave process to handle a request to avoid one request blocking
1618 * another. parent returns to job loop.
1624 return; /* we're already a slave process */
1625 if(ainc(&active) >= Maxactive){
1629 switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
1636 syslog(0, logfile, "slave %d", getpid());
1637 procsetname("%s", host);
1640 longjmp(masterjmp, 1);
1646 dnsip6lookup(char *mntpt, char *buf, Ndbtuple *t)
1650 t6 = dnsquery(mntpt, buf, "ipv6"); /* lookup AAAA dns RRs */
1654 /* convert ipv6 attr to ip */
1655 for (tt = t6; tt != nil; tt = tt->entry)
1656 if (strcmp(tt->attr, "ipv6") == 0)
1657 strcpy(tt->attr, "ip");
1662 /* append t6 list to t list */
1663 for (tt = t; tt->entry != nil; tt = tt->entry)
1670 * call the dns process and have it try to translate a name
1673 dnsiplookup(char *host, Ndbs *s)
1682 werrstr("too mutch activity");
1686 if(strcmp(ipattr(host), "ip") == 0)
1687 t = dnsquery(mntpt, host, "ptr");
1689 t = dnsquery(mntpt, host, "ip");
1690 /* special case: query ipv6 (AAAA dns RR) too */
1692 t = dnsip6lookup(mntpt, host, t);
1697 rerrstr(buf, sizeof buf);
1698 if(strstr(buf, "exist"))
1699 werrstr("can't translate address: %s", buf);
1700 else if(strstr(buf, "dns failure"))
1701 werrstr("temporary problem: %s", buf);
1709 qmatch(Ndbtuple *t, char **attr, char **val, int n)
1714 for(i = 1; i < n; i++){
1716 for(nt = t; nt; nt = nt->entry)
1717 if(strcmp(attr[i], nt->attr) == 0)
1718 if(strcmp(val[i], "*") == 0
1719 || strcmp(val[i], nt->val) == 0){
1730 qreply(Mfile *mf, Ndbtuple *t)
1736 for(nt = t; mf->nreply < Nreply && nt; nt = nt->entry){
1737 s_append(s, nt->attr);
1739 s_append(s, nt->val);
1741 if(nt->line != nt->entry){
1742 mf->replylen[mf->nreply] = s_len(s);
1743 mf->reply[mf->nreply++] = estrdup(s_to_c(s));
1757 * generic query lookup. The query is of one of the following
1760 * attr1=val1 attr2=val2 attr3=val3 ...
1762 * returns the matching tuple
1764 * ipinfo attr=val attr1 attr2 attr3 ...
1766 * is like ipinfo and returns the attr{1-n}
1767 * associated with the ip address.
1770 genquery(Mfile *mf, char *query)
1774 char *attr[Maxattr];
1779 n = getfields(query, attr, nelem(attr), 1, " ");
1783 if(strcmp(attr[0], "ipinfo") == 0)
1784 return ipinfoquery(mf, attr, n);
1787 for(i = 0; i < n; i++){
1788 p = strchr(attr[i], '=');
1795 /* give dns a chance */
1796 if((strcmp(attr[0], "dom") == 0 || strcmp(attr[0], "ip") == 0) && val[0]){
1797 t = dnsiplookup(val[0], &s);
1799 if(qmatch(t, attr, val, n)){
1808 /* first pair is always the key. It can't be a '*' */
1809 t = ndbsearch(db, &s, attr[0], val[0]);
1811 /* search is the and of all the pairs */
1813 if(qmatch(t, attr, val, n)){
1820 t = ndbsnext(&s, attr[0], val[0]);
1827 * resolve an ip address
1830 ipresolve(char *attr, char *host)
1832 Ndbtuple *t, *nt, **l;
1834 t = iplookup(&network[Ntcp], host, "*", 0);
1835 for(l = &t; *l != nil; ){
1837 if(strcmp(nt->attr, "ip") != 0){
1843 nstrcpy(nt->attr, attr, sizeof(nt->attr));
1850 ipinfoquery(Mfile *mf, char **list, int n)
1853 int resolve[Maxattr];
1854 Ndbtuple *t, *nt, **l;
1863 /* get search attribute=value, or assume ip=myipaddr */
1865 if((val = strchr(attr, '=')) != nil){
1878 * don't let ndbipinfo resolve the addresses, we're
1882 for(i = 0; i < n; i++)
1883 if(*list[i] == '@'){ /* @attr=val ? */
1885 resolve[i] = 1; /* we'll resolve it */
1890 t = ndbipinfo(db, attr, val, list, n);
1895 for(l = &t; *l != nil;){
1898 /* already an address? */
1899 if(strcmp(ipattr(nt->val), "ip") == 0){
1904 /* user wants it resolved? */
1905 for(i = 0; i < n; i++)
1906 if(strcmp(list[i], nt->attr) == 0)
1908 if(i >= n || resolve[i] == 0){
1913 /* resolve address and replace entry */
1914 *l = ipresolve(nt->attr, nt->val);
1924 /* make it all one line */
1925 for(nt = t; nt != nil; nt = nt->entry){
1926 if(nt->entry == nil)
1929 nt->line = nt->entry;
1944 error("out of memory");
1958 error("out of memory");
1959 memmove(p, s, size);