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);
140 Ndbtuple* dnsiplookup(char*, Ndbs*, int);
141 Ndbtuple* myipinfo(Ndb *db, char **list, int n);
146 Ndbtuple *(*lookup)(Network*, char*, char*);
147 char *(*trans)(Ndbtuple*, Network*, char*, char*, int);
149 char considered; /* flag: ignored for "net!"? */
150 char fasttimeout; /* flag. was for IL */
151 char ipvers; /* flag: V4, V6 */
164 * net doesn't apply to (r)udp, icmp(v6), or telco (for speed).
166 Network network[] = {
167 { "il", iplookup, iptrans, 0, 1, V4, },
168 { "tcp", iplookup, iptrans, 0, 0, V4|V6, },
169 { "il", iplookup, iptrans, 0, 0, V4, },
170 { "udp", iplookup, iptrans, 1, 0, V4|V6, },
171 { "icmp", iplookup, iptrans, 1, 0, V4, },
172 { "icmpv6", iplookup, iptrans, 1, 0, V6, },
173 { "rudp", iplookup, iptrans, 1, 0, V4, },
174 { "ssh", iplookup, iptrans, 1, 0, V4|V6, },
175 { "telco", telcolookup, telcotrans, 1, 0, 0, },
182 int lookipvers = V4|V6;
186 Network *netlist; /* networks ordered by preference */
190 nstrcpy(char *to, char *from, int len)
192 strncpy(to, from, len);
199 fprint(2, "usage: %s [-dn] [-f ndb-file] [-x netmtpt]\n", argv0);
204 * based on libthread's threadsetname, but drags in less library code.
205 * actually just sets the arguments displayed.
208 procsetname(char *fmt, ...)
216 cmdname = vsmprint(fmt, arg);
220 snprint(buf, sizeof buf, "#p/%d/args", getpid());
221 if((fd = open(buf, OWRITE)) >= 0){
222 write(fd, cmdname, strlen(cmdname)+1);
229 main(int argc, char *argv[])
232 char ext[Maxpath], servefile[Maxpath];
235 setnetmtpt(mntpt, sizeof(mntpt), nil);
245 dbfile = EARGF(usage());
251 setnetmtpt(mntpt, sizeof(mntpt), EARGF(usage()));
252 setext(ext, sizeof(ext), mntpt);
258 snprint(netndb, sizeof(netndb), "%s/ndb", mntpt);
260 fmtinstall('E', eipfmt);
261 fmtinstall('I', eipfmt);
262 fmtinstall('M', eipfmt);
263 fmtinstall('F', fcallfmt);
269 snprint(servefile, sizeof(servefile), "#s/cs%s", ext);
270 unmount(servefile, mntpt);
273 rfork(RFREND|RFNOTEG);
274 csuser = estrdup(getuser());
275 mountinit(servefile, mntpt);
282 * if a mount point is specified, set the cs extention to be the mount point
283 * with '_'s replacing '/'s
286 setext(char *ext, int n, char *p)
291 for(i = 0; i < n; i++){
303 mountinit(char *service, char *mntpt)
310 error("pipe failed");
315 f = create(service, OWRITE|ORCLOSE, 0666);
318 snprint(buf, sizeof(buf), "%d", p[1]);
319 if(write(f, buf, strlen(buf)) != strlen(buf))
320 error("write /srv/cs");
322 switch(rfork(RFFDG|RFPROC|RFNAMEG)){
325 procsetname("%s", mntpt);
328 error("fork failed");
331 * put ourselves into the file system
334 if(mount(p[1], -1, mntpt, MAFTER, "") < 0)
335 error("mount failed");
338 mfd[0] = mfd[1] = p[0];
344 db = ndbopen(dbfile);
346 error("can't open network database");
348 for(netdb = db; netdb != nil; netdb = netdb->next)
349 if(strcmp(netdb->file, netndb) == 0)
352 netdb = ndbopen(netndb);
355 db = ndbcat(netdb, db);
366 for(f = mlist; f != nil; f = f->next)
367 if(f->mf.busy && f->mf.fid == fid)
369 else if(ff == nil && !f->mf.busy && !f->mf.ref)
372 ff = emalloc(sizeof *f);
377 memset(mf, 0, sizeof *mf);
387 job = emalloc(sizeof *job);
391 job->request.tag = -1;
402 for(l = &joblist; *l != nil; l = &(*l)->next){
418 for(job = joblist; job != nil; job = job->next){
419 if(job->request.tag == tag && job->request.type != Tflush){
433 uchar mdata[IOHDRSZ + Maxfdata];
437 * if we ask dns to fulfill requests,
438 * a slave process is created to wait for replies. The
439 * master process returns immediately via a longjmp
440 * through 'masterjmp'.
442 * *isslave is a pointer into the call stack to a variable
443 * that tells whether or not the current process is a slave.
445 slaveflag = 0; /* init slave variable */
446 isslave = &slaveflag;
450 n = read9pmsg(mfd[0], mdata, sizeof mdata);
456 if(convM2S(mdata, n, &job->request) != n){
457 syslog(1, logfile, "format error %ux %ux %ux %ux %ux",
458 mdata[0], mdata[1], mdata[2], mdata[3], mdata[4]);
463 mf = newfid(job->request.fid);
465 syslog(0, logfile, "%F", &job->request);
467 switch(job->request.type){
469 syslog(1, logfile, "unknown request type %d", job->request.type);
516 * slave processes die after replying
520 syslog(0, logfile, "slave death %d", getpid());
530 if(job->request.msize > IOHDRSZ + Maxfdata)
531 job->reply.msize = IOHDRSZ + Maxfdata;
533 job->reply.msize = job->request.msize;
534 if(strncmp(job->request.version, "9P2000", 6) != 0)
535 sendmsg(job, "unknown 9P version");
537 job->reply.version = "9P2000";
545 sendmsg(job, "cs: authentication not required");
549 * don't flush till all the slaves are done
554 flushjob(job->request.oldtag);
559 rattach(Job *job, Mfile *mf)
563 mf->user = estrdup(job->request.uname);
565 mf->qid.vers = vers++;
566 mf->qid.type = QTDIR;
568 job->reply.qid = mf->qid;
574 rwalk(Job *job, Mfile *mf)
585 elems = job->request.wname;
586 nelems = job->request.nwname;
587 job->reply.nwqid = 0;
589 if(job->request.newfid != job->request.fid){
591 nmf = newfid(job->request.newfid);
594 err = "clone to used channel";
598 nmf->user = estrdup(mf->user);
599 nmf->fid = job->request.newfid;
600 nmf->qid.vers = vers++;
603 /* else nmf will be nil */
608 for(i=0; i<nelems && i<MAXWELEM; i++){
609 if((qid.type & QTDIR) == 0){
610 err = "not a directory";
613 if(strcmp(elems[i], "..") == 0 || strcmp(elems[i], ".") == 0){
617 job->reply.wqid[i] = qid;
621 if(strcmp(elems[i], "cs") == 0){
626 err = "file does not exist";
632 if(nmf != nil && (err!=nil || job->reply.nwqid<nelems)){
646 ropen(Job *job, Mfile *mf)
652 mode = job->request.mode;
653 if(mf->qid.type & QTDIR){
655 err = "permission denied";
657 job->reply.qid = mf->qid;
658 job->reply.iounit = 0;
663 rcreate(Job *job, Mfile *mf)
666 sendmsg(job, "creation permission denied");
670 rread(Job *job, Mfile *mf)
673 long off, toff, clock;
680 off = job->request.offset;
681 cnt = job->request.count;
684 if(mf->qid.type & QTDIR){
687 memset(&dir, 0, sizeof dir);
689 dir.qid.type = QTFILE;
697 dir.atime = clock; /* wrong */
698 dir.mtime = clock; /* wrong */
699 n = convD2M(&dir, buf, sizeof buf);
701 job->reply.data = (char*)buf;
706 /* look for an answer at the right offset */
708 for(i = 0; mf->reply[i] != nil && i < mf->nreply; i++){
715 break; /* got something to return */
717 /* try looking up more answers */
718 if(lookup(mf) == 0 || job->flushed){
725 /* give back a single reply (or part of one) */
726 job->reply.data = mf->reply[i] + (off - toff);
727 if(cnt > toff - off + n)
733 job->reply.count = n;
736 if(--mf->ref == 0 && mf->busy == 0)
761 for(i = 0; i < mf->nreply; i++){
767 mf->nextnet = netlist;
771 rwrite(Job *job, Mfile *mf)
779 cnt = job->request.count;
780 if(mf->qid.type & QTDIR){
781 err = "can't write directory";
784 if(cnt >= Maxrequest){
785 err = "request too long";
788 job->request.data[cnt] = 0;
790 if(strcmp(mf->user, "none") == 0 || strcmp(mf->user, csuser) != 0)
791 goto query; /* skip special commands if not owner */
796 if(strncmp(job->request.data, "debug", 5)==0){
798 syslog(1, logfile, "debug %d", debug);
803 * toggle ipv6 lookups
805 if(strncmp(job->request.data, "ipv6", 4)==0){
807 syslog(1, logfile, "ipv6lookups %d", (lookipvers & V6) != 0);
812 * add networks to the default list
814 if(strncmp(job->request.data, "add ", 4)==0){
815 if(job->request.data[cnt-1] == '\n')
816 job->request.data[cnt-1] = 0;
817 netadd(job->request.data+4);
825 if(strncmp(job->request.data, "refresh", 7)==0){
832 err = "query already in progress";
837 /* start transaction with a clean slate */
841 * look for a general query
843 if(*job->request.data == '!'){
844 err = genquery(mf, job->request.data+1);
849 syslog(0, logfile, "write %s", job->request.data);
853 n = getfields(job->request.data, field, 4, 1, "!");
856 mf->net = estrdup("net");
857 mf->host = estrdup(field[0]);
860 mf->rem = estrdup(field[3]);
863 mf->serv = estrdup(field[2]);
866 mf->host = estrdup(field[1]);
867 mf->net = estrdup(field[0]);
872 * do the first net worth of lookup
875 rerrstr(curerr, sizeof curerr);
880 if(--mf->ref == 0 && mf->busy == 0)
884 job->reply.count = cnt;
889 rclunk(Job *job, Mfile *mf)
901 rremove(Job *job, Mfile *mf)
904 sendmsg(job, "remove permission denied");
908 rstat(Job *job, Mfile *mf)
911 uchar buf[IOHDRSZ+Maxfdata];
913 memset(&dir, 0, sizeof dir);
914 if(mf->qid.type & QTDIR){
916 dir.mode = DMDIR|0555;
926 dir.atime = dir.mtime = time(0);
927 job->reply.nstat = convD2M(&dir, buf, sizeof buf);
928 job->reply.stat = buf;
933 rwstat(Job *job, Mfile *mf)
936 sendmsg(job, "wstat permission denied");
940 sendmsg(Job *job, char *err)
943 uchar mdata[IOHDRSZ + Maxfdata];
947 job->reply.type = Rerror;
948 snprint(ename, sizeof(ename), "cs: %s", err);
949 job->reply.ename = ename;
951 job->reply.type = job->request.type+1;
953 job->reply.tag = job->request.tag;
954 n = convS2M(&job->reply, mdata, sizeof mdata);
956 syslog(1, logfile, "sendmsg convS2M of %F returns 0", &job->reply);
960 if(job->flushed == 0)
961 if(write(mfd[1], mdata, n)!=n)
962 error("mount write");
965 syslog(0, logfile, "%F %d", &job->reply, n);
971 syslog(1, logfile, "%s: %r", s);
976 readipinterfaces(void)
984 ipifcs = readipifc(mntpt, ipifcs, -1);
985 for(ifc = ipifcs; ifc != nil; ifc = ifc->next){
986 for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
998 * get the system name
1003 char eaddr[16], buf[Maxpath];
1011 if(mysysname != nil)
1015 * environment has priority.
1017 * on the sgi power the default system name
1018 * is the ip address. ignore that.
1021 p = getenv("sysname");
1022 if(p != nil && *p != 0){
1024 if(strcmp(attr, "ip") != 0) {
1031 /* try configured interfaces */
1033 t = s.t = myipinfo(db, &attr, 1);
1037 /* try ethernet interfaces */
1040 f = open(mntpt, OREAD);
1042 n = dirreadall(f, &d);
1045 for(f = 0; f < n; f++){
1046 if((d[f].mode & DMDIR) == 0 || strncmp(d[f].name, "ether", 5) != 0)
1048 snprint(buf, sizeof buf, "%s/%s", mntpt, d[f].name);
1049 if(myetheraddr(addr, buf) >= 0){
1050 snprint(eaddr, sizeof(eaddr), "%E", addr);
1051 free(ndbgetvalue(db, &s, "ether", eaddr, "sys", &t));
1060 /* nothing else worked, use ip address */
1062 t = s.t = myipinfo(db, &attr, 1);
1067 /* found in database */
1068 if((tt = ndbfindattr(t, s.t, "sys")) != nil)
1069 mysysname = estrdup(tt->val);
1070 else if((tt = ndbfindattr(t, s.t, "ip")) != nil)
1071 mysysname = estrdup(tt->val);
1074 if(mysysname == nil)
1078 /* set /dev/sysname if we now know it */
1079 f = open("/dev/sysname", OWRITE);
1081 write(f, mysysname, strlen(mysysname));
1087 * Set up a list of default networks by looking for
1091 netinit(int background)
1093 char clone[Maxpath];
1097 if(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT) != 0)
1102 /* add the mounted networks to the default list */
1103 for(np = network; np->net != nil; np++){
1106 snprint(clone, sizeof(clone), "%s/%s/clone", mntpt, np->net);
1107 if(access(clone, AEXIST) < 0)
1118 /* find out what our ip addresses are */
1121 /* set the system name if we need to, these days ip is all we have */
1125 syslog(0, logfile, "mysysname %s", mysysname?mysysname:"???");
1134 * add networks to the standard list
1143 n = getfields(p, field, 12, 1, " ");
1144 for(i = 0; i < n; i++){
1145 for(np = network; np->net != nil; np++){
1146 if(strcmp(field[i], np->net) != 0)
1162 lookforproto(Ndbtuple *t, char *proto)
1164 for(; t != nil; t = t->entry)
1165 if(strcmp(t->attr, "proto") == 0 && strcmp(t->val, proto) == 0)
1171 * lookup a request. the network "net" means we should pick the
1172 * best network to get there.
1180 char reply[Maxreply];
1181 int i, rv, fasttimeout;
1183 /* open up the standard db files */
1187 error("can't open mf->network database\n");
1190 return 0; /* must have been a genquery */
1193 if(strcmp(mf->net, "net") == 0){
1195 * go through set of default nets
1197 for(np = mf->nextnet; np != nil && rv == 0; np = np->next){
1198 nt = (*np->lookup)(np, mf->host, mf->serv);
1201 fasttimeout = np->fasttimeout && !lookforproto(nt, np->net);
1202 for(t = nt; mf->nreply < Nreply && t != nil; t = t->entry){
1203 cp = (*np->trans)(t, np, mf->serv, mf->rem, fasttimeout);
1205 /* avoid duplicates */
1206 for(i = 0; i < mf->nreply; i++)
1207 if(strcmp(mf->reply[i], cp) == 0)
1209 if(i == mf->nreply){
1210 /* save the reply */
1211 mf->replylen[mf->nreply] = strlen(cp);
1212 mf->reply[mf->nreply++] = cp;
1225 * if not /net, we only get one lookup
1231 * look for a specific network
1233 for(np = network; np->net != nil; np++){
1236 if(strcmp(np->net, mf->net) == 0)
1244 nt = (*np->lookup)(np, mf->host, mf->serv);
1245 for(t = nt; mf->nreply < Nreply && t != nil; t = t->entry){
1246 cp = (*np->trans)(t, np, mf->serv, mf->rem, 0);
1248 mf->replylen[mf->nreply] = strlen(cp);
1249 mf->reply[mf->nreply++] = cp;
1257 * not a known network, don't translate host or service
1260 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s",
1261 mntpt, mf->net, mf->host, mf->serv);
1263 snprint(reply, sizeof(reply), "%s/%s/clone %s",
1264 mntpt, mf->net, mf->host);
1265 mf->reply[0] = estrdup(reply);
1266 mf->replylen[0] = strlen(reply);
1273 * translate an ip service name into a port number. If it's a numeric port
1274 * number, look for restricted access.
1276 * the service '*' needs no translation.
1279 ipserv(Network *np, char *name, char *buf, int blen)
1287 /* '*' means any service */
1288 if(strcmp(name, "*") == 0){
1289 nstrcpy(buf, name, blen);
1293 /* see if it's numeric or symbolic */
1294 for(p = name; *p; p++){
1297 else if(isalpha(*p) || *p == '-' || *p == '$')
1305 p = ndbgetvalue(db, &s, np->net, name, "port", &t);
1309 /* look up only for tcp ports < 1024 to get the restricted
1312 if(atoi(name) < 1024 && strcmp(np->net, "tcp") == 0)
1313 p = ndbgetvalue(db, &s, "port", name, "port", &t);
1319 for(nt = t; nt != nil; nt = nt->entry)
1320 if(strcmp(nt->attr, "restricted") == 0)
1324 snprint(buf, blen, "%s%s", p, restr ? "!r" : "");
1331 myipinfo(Ndb *db, char **list, int n)
1340 for(ifc = ipifcs; ifc != nil; ifc = ifc->next){
1341 for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
1342 snprint(ip, sizeof(ip), "%I", lifc->ip);
1343 nt = ndbipinfo(db, "ip", ip, list, n);
1344 t = ndbconcatenate(t, nt);
1347 qunlock(&ipifclock);
1353 * reorder according to our interfaces
1356 ipreorder(Ndbtuple *t)
1359 uchar ip[IPaddrlen];
1360 uchar net[IPaddrlen];
1361 uchar tnet[IPaddrlen];
1369 for(ifc = ipifcs; ifc != nil; ifc = ifc->next){
1370 for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
1371 maskip(lifc->ip, lifc->mask, net);
1372 for(nt = t; nt != nil; nt = nt->entry){
1373 if(strcmp(nt->attr, "ip") != 0)
1375 if(parseip(ip, nt->val) == -1)
1377 maskip(ip, lifc->mask, tnet);
1378 if(memcmp(net, tnet, IPaddrlen) == 0){
1379 qunlock(&ipifclock);
1380 return ndbreorder(t, nt);
1385 qunlock(&ipifclock);
1391 ndbline(Ndbtuple *t)
1395 for(nt = t; nt != nil; nt = nt->entry){
1396 if(nt->entry == nil)
1399 nt->line = nt->entry;
1405 * lookup an ip destination
1408 iplookuphost(Network *np, char *host)
1410 char *attr, *dnsname;
1415 * turn '[ip address]' into just 'ip address'
1418 char tmp[Maxhost], *x;
1420 nstrcpy(tmp, host, sizeof tmp);
1422 if((x = strchr(++host, ']')) != nil)
1426 /* for dial strings with no host */
1427 if(strcmp(host, "*") == 0)
1428 return ndbline(ndbnew("ip", "*"));
1431 * hack till we go v6 :: = 0.0.0.0
1433 if(strcmp("::", host) == 0)
1434 return ndbline(ndbnew("ip", "*"));
1437 * just accept addresses
1439 attr = ipattr(host);
1440 if(strcmp(attr, "ip") == 0)
1441 return ndbline(ndbnew("ip", host));
1444 * give the domain name server the first opportunity to
1445 * resolve domain names. if that fails try the database.
1448 if(strcmp(attr, "dom") == 0)
1449 t = dnsiplookup(host, &s, np->ipvers);
1451 for(nt = ndbsearch(db, &s, attr, host); nt != nil; nt = ndbsnext(&s, attr, host)){
1452 if(ndbfindattr(nt, s.t, "ip") == nil){
1456 t = ndbconcatenate(t, ndbreorder(nt, s.t));
1461 if(strcmp(attr, "dom") != 0){
1462 dnsname = ndbgetvalue(db, &s, attr, host, "dom", nil);
1464 t = dnsiplookup(dnsname, &s, np->ipvers);
1469 t = dnsiplookup(host, &s, np->ipvers);
1475 * reorder the tuple to have the matched line first and
1476 * save that in the request structure.
1478 return ndbreorder(t, s.t);
1483 iplookup(Network *np, char *host, char *serv)
1485 Ndbtuple *l, *t, *nt;
1486 char ts[Maxservice], *attr;
1489 * start with the service since it's the most likely to fail
1490 * and costs the least
1492 if(serv == nil || ipserv(np, serv, ts, sizeof ts) == nil){
1493 werrstr("can't translate service");
1498 * '$' means the rest of the name is an attribute that we
1499 * need to search for
1501 werrstr("can't translate address");
1505 l = myipinfo(db, &attr, 1);
1506 for(nt = l; nt != nil; nt = nt->entry){
1507 if(strcmp(nt->attr, attr) == 0)
1508 t = ndbconcatenate(t, iplookuphost(np, nt->val));
1512 t = iplookuphost(np, host);
1514 return ipreorder(t);
1519 * translate an ip address
1522 iptrans(Ndbtuple *t, Network *np, char *serv, char *rem, int fasttimeout)
1524 char ts[Maxservice];
1525 char reply[Maxreply];
1527 uchar ip[IPaddrlen];
1529 if(strcmp(t->attr, "ip") != 0)
1532 if(serv == nil || ipserv(np, serv, ts, sizeof ts) == nil){
1533 werrstr("can't translate service");
1538 snprint(x, sizeof(x), "!%s", rem);
1543 snprint(reply, sizeof(reply), "%s/%s/clone %s%s",
1544 mntpt, np->net, ts, x);
1546 if(parseip(ip, t->val) == -1)
1548 if((np->ipvers & confipvers & (isv4(ip) ? V4 : V6)) == 0)
1550 snprint(reply, sizeof(reply), "%s/%s/clone %I!%s%s%s",
1551 mntpt, np->net, ip, ts, x, fasttimeout? "!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 ndbline(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);
1633 static QLock mountlock;
1642 snprint(buf, sizeof(buf), "%s/dns", mntpt);
1643 if(access(buf, AEXIST) == 0)
1645 if(strcmp(mntpt, "/net") == 0)
1646 snprint(buf, sizeof(buf), "/srv/dns");
1648 snprint(buf, sizeof(buf), "/srv/dns%s", mntpt);
1649 while((p = strchr(buf+8, '/')) != nil)
1652 if((fd = open(buf, ORDWR)) < 0){
1654 qunlock(&mountlock);
1657 if(mount(fd, -1, mntpt, MAFTER, "") < 0){
1663 qunlock(&mountlock);
1668 dnsip6lookup(char *mntpt, char *buf, Ndbtuple *t)
1672 t6 = dnsquery(mntpt, buf, "ipv6"); /* lookup AAAA dns RRs */
1676 /* convert ipv6 attr to ip */
1677 for (tt = t6; tt != nil; tt = tt->entry)
1678 if (strcmp(tt->attr, "ipv6") == 0)
1679 strcpy(tt->attr, "ip");
1681 /* append t6 list to t list */
1682 return ndbconcatenate(t, t6);
1686 * call the dns process and have it try to translate a name
1689 dnsiplookup(char *host, Ndbs *s, int ipvers)
1694 ipvers &= confipvers & lookipvers;
1696 werrstr("no ip address");
1703 werrstr("too much activity");
1712 if(strcmp(ipattr(host), "ip") == 0)
1713 t = dnsquery(mntpt, host, "ptr");
1717 t = dnsquery(mntpt, host, "ip");
1719 t = dnsip6lookup(mntpt, host, t);
1724 rerrstr(buf, sizeof buf);
1725 if(strstr(buf, "exist") != nil)
1726 werrstr("can't translate address: %s", buf);
1727 else if(strstr(buf, "dns failure") != nil)
1728 werrstr("temporary problem: %s", buf);
1736 qmatch(Ndbtuple *t, char **attr, char **val, int n)
1741 for(i = 1; i < n; i++){
1743 for(nt = t; nt != nil; nt = nt->entry)
1744 if(strcmp(attr[i], nt->attr) == 0)
1745 if(strcmp(val[i], "*") == 0
1746 || strcmp(val[i], nt->val) == 0){
1757 qreply(Mfile *mf, Ndbtuple *t)
1763 for(nt = t; mf->nreply < Nreply && nt != nil; nt = nt->entry){
1764 s_append(s, nt->attr);
1766 s_append(s, nt->val);
1768 if(nt->line != nt->entry){
1769 mf->replylen[mf->nreply] = s_len(s);
1770 mf->reply[mf->nreply++] = estrdup(s_to_c(s));
1784 * generic query lookup. The query is of one of the following
1787 * attr1=val1 attr2=val2 attr3=val3 ...
1789 * returns the matching tuple
1791 * ipinfo attr=val attr1 attr2 attr3 ...
1793 * is like ipinfo and returns the attr{1-n}
1794 * associated with the ip address.
1797 genquery(Mfile *mf, char *query)
1801 char *attr[Maxattr];
1806 n = getfields(query, attr, nelem(attr), 1, " ");
1810 if(strcmp(attr[0], "ipinfo") == 0)
1811 return ipinfoquery(mf, attr, n);
1814 for(i = 0; i < n; i++){
1815 p = strchr(attr[i], '=');
1822 /* give dns a chance */
1823 if((strcmp(attr[0], "dom") == 0 || strcmp(attr[0], "ip") == 0) && val[0]){
1824 t = dnsiplookup(val[0], &s, lookipvers);
1826 if(qmatch(t, attr, val, n)){
1835 /* first pair is always the key. It can't be a '*' */
1836 t = ndbsearch(db, &s, attr[0], val[0]);
1838 /* search is the and of all the pairs */
1840 if(qmatch(t, attr, val, n)){
1847 t = ndbsnext(&s, attr[0], val[0]);
1854 * resolve an ip address
1857 ipresolve(char *attr, char *host)
1859 Ndbtuple *t, *nt, **l;
1861 t = iplookup(&network[Ntcp], host, "*");
1862 for(l = &t; *l != nil; ){
1864 if(strcmp(nt->attr, "ip") != 0){
1870 nstrcpy(nt->attr, attr, sizeof(nt->attr));
1880 ndbdedup(Ndbtuple *t)
1882 Ndbtuple *tt, *nt, **l;
1884 for(nt = t; nt != nil; nt = nt->entry){
1885 for(l = &nt->entry; (tt = *l) != nil;){
1886 if(strcmp(nt->attr, tt->attr) != 0
1887 || strcmp(nt->val, tt->val) != 0){
1900 ipinfoquery(Mfile *mf, char **list, int n)
1903 uchar resolve[Maxattr];
1904 Ndbtuple *t, *nt, **l;
1913 /* get search attribute=value, or assume myip */
1915 if((val = strchr(attr, '=')) != nil){
1928 * don't let ndbipinfo resolve the addresses, we're
1932 for(i = 0; i < n; i++)
1933 if(*list[i] == '@'){ /* @attr=val ? */
1935 resolve[i] = 1; /* we'll resolve it */
1941 t = myipinfo(db, list, n);
1943 t = ndbipinfo(db, attr, val, list, n);
1950 for(l = &t; *l != nil;){
1953 /* already an address? */
1954 if(strcmp(ipattr(nt->val), "ip") == 0){
1959 /* user wants it resolved? */
1960 for(i = 0; i < n; i++)
1961 if(strcmp(list[i], nt->attr) == 0)
1963 if(i >= n || resolve[i] == 0){
1968 /* resolve address and replace entry */
1969 *l = ipresolve(nt->attr, nt->val);
1980 /* make it all one line */
1995 error("out of memory");
2009 error("out of memory");
2010 memmove(p, s, size);