]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/ndb/dns.c
audiohda: fix syntax error
[plan9front.git] / sys / src / cmd / ndb / dns.c
1 #include <u.h>
2 #include <libc.h>
3 #include <auth.h>
4 #include <fcall.h>
5 #include <bio.h>
6 #include <ip.h>
7 #include "dns.h"
8
9 enum
10 {
11         Maxrequest=             1024,
12         Maxreply=               8192,           /* was 512 */
13         Maxrrr=                 32,             /* was 16 */
14         Maxfdata=               8192,
15
16         Defmaxage=              60*60,  /* default domain name max. age */
17
18         Qdir=                   0,
19         Qdns=                   1,
20 };
21
22 typedef struct Mfile    Mfile;
23 typedef struct Job      Job;
24 typedef struct Network  Network;
25
26 int vers;               /* incremented each clone/attach */
27
28 /* holds data to be returned via read of /net/dns, perhaps multiple reads */
29 struct Mfile
30 {
31         Mfile           *next;          /* next free mfile */
32
33         char            *user;
34         Qid             qid;
35         int             fid;
36
37         int             type;           /* reply type */
38         char            reply[Maxreply];
39         ushort          rr[Maxrrr];     /* offset of rr's */
40         ushort          nrr;            /* number of rr's */
41 };
42
43 /*
44  *  active local requests
45  */
46 struct Job
47 {
48         Job     *next;
49         int     flushed;
50         Fcall   request;
51         Fcall   reply;
52 };
53 Lock    joblock;
54 Job     *joblist;
55
56 struct {
57         Lock;
58         Mfile   *inuse;         /* active mfile's */
59 } mfalloc;
60
61 Cfg     cfg;
62 int     debug;
63 int     maxage = Defmaxage;
64 int     mfd[2];
65 int     needrefresh;
66 ulong   now;
67 vlong   nowns;
68 int     sendnotifies;
69 char    *trace;
70 int     traceactivity;
71 char    *zonerefreshprogram;
72
73 char    *logfile = "dns";       /* or "dns.test" */
74 char    *dbfile;
75 char    *dnsuser;
76 char    mntpt[Maxpath];
77
78 int     addforwtarg(char *);
79 int     fillreply(Mfile*, int);
80 void    freejob(Job*);
81 void    io(void);
82 void    mountinit(char*, char*);
83 Job*    newjob(void);
84 void    rattach(Job*, Mfile*);
85 void    rauth(Job*);
86 void    rclunk(Job*, Mfile*);
87 void    rcreate(Job*, Mfile*);
88 void    rflush(Job*);
89 void    ropen(Job*, Mfile*);
90 void    rread(Job*, Mfile*);
91 void    rremove(Job*, Mfile*);
92 void    rstat(Job*, Mfile*);
93 void    rversion(Job*);
94 char*   rwalk(Job*, Mfile*);
95 void    rwrite(Job*, Mfile*, Request*);
96 void    rwstat(Job*, Mfile*);
97 void    sendmsg(Job*, char*);
98 void    setext(char*, int, char*);
99
100 static char *lookupquery(Job*, Mfile*, Request*, char*, char*, int, int);
101 static char *respond(Job*, Mfile*, RR*, char*, int, int);
102
103 void
104 usage(void)
105 {
106         fprint(2, "usage: %s [-FnorRs] [-a maxage] [-f ndb-file] [-N target] "
107                 "[-T forwip] [-x netmtpt] [-z refreshprog]\n", argv0);
108         exits("usage");
109 }
110
111 void
112 main(int argc, char *argv[])
113 {
114         char servefile[Maxpath], ext[Maxpath];
115         Dir *dir;
116
117         setnetmtpt(mntpt, sizeof mntpt, nil);
118         ext[0] = 0;
119         ARGBEGIN{
120         case 'a':
121                 maxage = atol(EARGF(usage()));
122                 if (maxage <= 0)
123                         maxage = Defmaxage;
124                 break;
125         case 'd':
126                 debug = 1;
127                 traceactivity = 1;
128                 break;
129         case 'f':
130                 dbfile = EARGF(usage());
131                 break;
132         case 'F':
133                 cfg.justforw = cfg.resolver = 1;
134                 break;
135         case 'n':
136                 sendnotifies = 1;
137                 break;
138         case 'N':
139                 target = atol(EARGF(usage()));
140                 if (target < 1000)
141                         target = 1000;
142                 break;
143         case 'o':
144                 cfg.straddle = 1;       /* straddle inside & outside networks */
145                 break;
146         case 'r':
147                 cfg.resolver = 1;
148                 break;
149         case 'R':
150                 norecursion = 1;
151                 break;
152         case 's':
153                 cfg.serve = 1;          /* serve network */
154                 cfg.cachedb = 1;
155                 break;
156         case 'T':
157                 addforwtarg(EARGF(usage()));
158                 break;
159         case 'x':
160                 setnetmtpt(mntpt, sizeof mntpt, EARGF(usage()));
161                 setext(ext, sizeof ext, mntpt);
162                 break;
163         case 'z':
164                 zonerefreshprogram = EARGF(usage());
165                 break;
166         default:
167                 usage();
168                 break;
169         }ARGEND
170         if(argc != 0)
171                 usage();
172
173         rfork(RFREND|RFNOTEG);
174
175         cfg.inside = (*mntpt == '\0' || strcmp(mntpt, "/net") == 0);
176
177         /* start syslog before we fork */
178         fmtinstall('F', fcallfmt);
179         dninit();
180         dnslog("starting %s%sdns %s%s%son %s",
181                 (cfg.straddle? "straddling ": ""),
182                 (cfg.cachedb? "caching ": ""),
183                 (cfg.serve?   "udp server ": ""),
184                 (cfg.justforw? "forwarding-only ": ""),
185                 (cfg.resolver? "resolver ": ""), mntpt);
186
187         opendatabase();
188         now = time(nil);                /* open time files before we fork */
189         nowns = nsec();
190         dnsuser = estrdup(getuser());
191
192         snprint(servefile, sizeof servefile, "#s/dns%s", ext);
193         dir = dirstat(servefile);
194         if (dir)
195                 sysfatal("%s exists; another dns instance is running",
196                         servefile);
197         free(dir);
198         mountinit(servefile, mntpt);    /* forks, parent exits */
199
200         srand(truerand());
201         db2cache(1);
202
203         if (cfg.straddle && !seerootns())
204                 dnslog("straddle server misconfigured; can't see root name servers");
205
206         if(cfg.serve)
207                 dnudpserver(mntpt);
208         if(sendnotifies)
209                 notifyproc();
210
211         io();
212         _exits(0);
213 }
214
215 /*
216  *  if a mount point is specified, set the cs extension to be the mount point
217  *  with '_'s replacing '/'s
218  */
219 void
220 setext(char *ext, int n, char *p)
221 {
222         int i, c;
223
224         n--;
225         for(i = 0; i < n; i++){
226                 c = p[i];
227                 if(c == 0)
228                         break;
229                 if(c == '/')
230                         c = '_';
231                 ext[i] = c;
232         }
233         ext[i] = 0;
234 }
235
236 void
237 mountinit(char *service, char *mntpt)
238 {
239         int f;
240         int p[2];
241         char buf[32];
242
243         if(pipe(p) < 0)
244                 sysfatal("pipe failed: %r");
245
246         /*
247          *  make a /srv/dns
248          */
249         if((f = create(service, OWRITE|ORCLOSE, 0666)) < 0)
250                 sysfatal("create %s failed: %r", service);
251         snprint(buf, sizeof buf, "%d", p[1]);
252         if(write(f, buf, strlen(buf)) != strlen(buf))
253                 sysfatal("write %s failed: %r", service);
254
255         /* copy namespace to avoid a deadlock */
256         switch(rfork(RFFDG|RFPROC|RFNAMEG)){
257         case 0:                 /* child: start main proc */
258                 close(p[1]);
259                 procsetname("%s", mntpt);
260                 break;
261         case -1:
262                 sysfatal("fork failed: %r");
263         default:                /* parent: make /srv/dns, mount it, exit */
264                 close(p[0]);
265
266                 /*
267                  *  put ourselves into the file system
268                  */
269                 if(mount(p[1], -1, mntpt, MAFTER, "") == -1)
270                         fprint(2, "dns mount failed: %r\n");
271                 _exits(0);
272         }
273         mfd[0] = mfd[1] = p[0];
274 }
275
276 Mfile*
277 newfid(int fid, int needunused)
278 {
279         Mfile *mf;
280
281         lock(&mfalloc);
282         for(mf = mfalloc.inuse; mf != nil; mf = mf->next)
283                 if(mf->fid == fid){
284                         unlock(&mfalloc);
285                         if(needunused)
286                                 return nil;
287                         return mf;
288                 }
289         mf = emalloc(sizeof(*mf));
290         mf->fid = fid;
291         mf->qid.vers = vers;
292         mf->qid.type = QTDIR;
293         mf->qid.path = 0LL;
294         mf->user = estrdup("none");
295         mf->next = mfalloc.inuse;
296         mfalloc.inuse = mf;
297         unlock(&mfalloc);
298         return mf;
299 }
300
301 void
302 freefid(Mfile *mf)
303 {
304         Mfile **l;
305
306         lock(&mfalloc);
307         for(l = &mfalloc.inuse; *l != nil; l = &(*l)->next)
308                 if(*l == mf){
309                         *l = mf->next;
310                         free(mf->user);
311                         memset(mf, 0, sizeof *mf);      /* cause trouble */
312                         free(mf);
313                         unlock(&mfalloc);
314                         return;
315                 }
316         unlock(&mfalloc);
317         sysfatal("freeing unused fid");
318 }
319
320 Mfile*
321 copyfid(Mfile *mf, int fid)
322 {
323         Mfile *nmf;
324
325         nmf = newfid(fid, 1);
326         if(nmf == nil)
327                 return nil;
328         nmf->fid = fid;
329         free(nmf->user);                        /* estrdup("none") */
330         nmf->user = estrdup(mf->user);
331         nmf->qid.type = mf->qid.type;
332         nmf->qid.path = mf->qid.path;
333         nmf->qid.vers = vers++;
334         return nmf;
335 }
336
337 Job*
338 newjob(void)
339 {
340         Job *job;
341
342         job = emalloc(sizeof *job);
343         lock(&joblock);
344         job->next = joblist;
345         joblist = job;
346         job->request.tag = -1;
347         unlock(&joblock);
348         return job;
349 }
350
351 void
352 freejob(Job *job)
353 {
354         Job **l;
355
356         lock(&joblock);
357         for(l = &joblist; *l; l = &(*l)->next)
358                 if(*l == job){
359                         *l = job->next;
360                         memset(job, 0, sizeof *job);    /* cause trouble */
361                         free(job);
362                         break;
363                 }
364         unlock(&joblock);
365 }
366
367 void
368 flushjob(int tag)
369 {
370         Job *job;
371
372         lock(&joblock);
373         for(job = joblist; job; job = job->next)
374                 if(job->request.tag == tag && job->request.type != Tflush){
375                         job->flushed = 1;
376                         break;
377                 }
378         unlock(&joblock);
379 }
380
381 void
382 io(void)
383 {
384         volatile long n;
385         volatile uchar mdata[IOHDRSZ + Maxfdata];
386         Job *volatile job;
387         Mfile *volatile mf;
388         volatile Request req;
389
390         memset(&req, 0, sizeof req);
391         /*
392          *  a slave process is sometimes forked to wait for replies from other
393          *  servers.  The master process returns immediately via a longjmp
394          *  through 'mret'.
395          */
396         if(setjmp(req.mret))
397                 putactivity(0);
398         req.isslave = 0;
399         while((n = read9pmsg(mfd[0], mdata, sizeof mdata)) != 0){
400                 if(n < 0){
401                         dnslog("error reading 9P from %s: %r", mntpt);
402                         break;
403                 }
404
405                 stats.qrecvd9prpc++;
406                 job = newjob();
407                 if(convM2S(mdata, n, &job->request) != n){
408                         dnslog("format error %ux %ux %ux %ux %ux",
409                                 mdata[0], mdata[1], mdata[2], mdata[3], mdata[4]);
410                         freejob(job);
411                         break;
412                 }
413                 mf = newfid(job->request.fid, 0);
414                 if(debug)
415                         dnslog("%F", &job->request);
416
417                 getactivity(&req, 0);
418                 req.aborttime = timems() + Maxreqtm;
419                 req.from = "9p";
420
421                 switch(job->request.type){
422                 default:
423                         warning("unknown request type %d", job->request.type);
424                         break;
425                 case Tversion:
426                         rversion(job);
427                         break;
428                 case Tauth:
429                         rauth(job);
430                         break;
431                 case Tflush:
432                         rflush(job);
433                         break;
434                 case Tattach:
435                         rattach(job, mf);
436                         break;
437                 case Twalk:
438                         rwalk(job, mf);
439                         break;
440                 case Topen:
441                         ropen(job, mf);
442                         break;
443                 case Tcreate:
444                         rcreate(job, mf);
445                         break;
446                 case Tread:
447                         rread(job, mf);
448                         break;
449                 case Twrite:
450                         /* &req is handed to dnresolve() */
451                         rwrite(job, mf, &req);
452                         break;
453                 case Tclunk:
454                         rclunk(job, mf);
455                         break;
456                 case Tremove:
457                         rremove(job, mf);
458                         break;
459                 case Tstat:
460                         rstat(job, mf);
461                         break;
462                 case Twstat:
463                         rwstat(job, mf);
464                         break;
465                 }
466
467                 freejob(job);
468
469                 /*
470                  *  slave processes die after replying
471                  */
472                 if(req.isslave){
473                         putactivity(0);
474                         _exits(0);
475                 }
476
477                 putactivity(0);
478         }
479 }
480
481 void
482 rversion(Job *job)
483 {
484         if(job->request.msize > IOHDRSZ + Maxfdata)
485                 job->reply.msize = IOHDRSZ + Maxfdata;
486         else
487                 job->reply.msize = job->request.msize;
488         job->reply.version = "9P2000";
489         if(strncmp(job->request.version, "9P", 2) != 0)
490                 job->reply.version = "unknown";
491         sendmsg(job, nil);
492 }
493
494 void
495 rauth(Job *job)
496 {
497         sendmsg(job, "dns: authentication not required");
498 }
499
500 /*
501  *  don't flush till all the slaves are done
502  */
503 void
504 rflush(Job *job)
505 {
506         flushjob(job->request.oldtag);
507         sendmsg(job, 0);
508 }
509
510 void
511 rattach(Job *job, Mfile *mf)
512 {
513         if(mf->user != nil)
514                 free(mf->user);
515         mf->user = estrdup(job->request.uname);
516         mf->qid.vers = vers++;
517         mf->qid.type = QTDIR;
518         mf->qid.path = 0LL;
519         job->reply.qid = mf->qid;
520         sendmsg(job, 0);
521 }
522
523 char*
524 rwalk(Job *job, Mfile *mf)
525 {
526         int i, nelems;
527         char *err;
528         char **elems;
529         Mfile *nmf;
530         Qid qid;
531
532         err = 0;
533         nmf = nil;
534         elems  = job->request.wname;
535         nelems = job->request.nwname;
536         job->reply.nwqid = 0;
537
538         if(job->request.newfid != job->request.fid){
539                 /* clone fid */
540                 nmf = copyfid(mf, job->request.newfid);
541                 if(nmf == nil){
542                         err = "clone bad newfid";
543                         goto send;
544                 }
545                 mf = nmf;
546         }
547         /* else nmf will be nil */
548
549         qid = mf->qid;
550         if(nelems > 0)
551                 /* walk fid */
552                 for(i=0; i<nelems && i<MAXWELEM; i++){
553                         if((qid.type & QTDIR) == 0){
554                                 err = "not a directory";
555                                 break;
556                         }
557                         if (strcmp(elems[i], "..") == 0 ||
558                             strcmp(elems[i], ".") == 0){
559                                 qid.type = QTDIR;
560                                 qid.path = Qdir;
561 Found:
562                                 job->reply.wqid[i] = qid;
563                                 job->reply.nwqid++;
564                                 continue;
565                         }
566                         if(strcmp(elems[i], "dns") == 0){
567                                 qid.type = QTFILE;
568                                 qid.path = Qdns;
569                                 goto Found;
570                         }
571                         err = "file does not exist";
572                         break;
573                 }
574
575 send:
576         if(nmf != nil && (err!=nil || job->reply.nwqid<nelems))
577                 freefid(nmf);
578         if(err == nil)
579                 mf->qid = qid;
580         sendmsg(job, err);
581         return err;
582 }
583
584 void
585 ropen(Job *job, Mfile *mf)
586 {
587         int mode;
588         char *err;
589
590         err = 0;
591         mode = job->request.mode;
592         if(mf->qid.type & QTDIR)
593                 if(mode)
594                         err = "permission denied";
595         job->reply.qid = mf->qid;
596         job->reply.iounit = 0;
597         sendmsg(job, err);
598 }
599
600 void
601 rcreate(Job *job, Mfile *mf)
602 {
603         USED(mf);
604         sendmsg(job, "creation permission denied");
605 }
606
607 void
608 rread(Job *job, Mfile *mf)
609 {
610         int i, n;
611         long clock;
612         ulong cnt;
613         vlong off;
614         char *err;
615         uchar buf[Maxfdata];
616         Dir dir;
617
618         n = 0;
619         err = nil;
620         off = job->request.offset;
621         cnt = job->request.count;
622         *buf = '\0';
623         job->reply.data = (char*)buf;
624         if(mf->qid.type & QTDIR){
625                 clock = time(nil);
626                 if(off == 0){
627                         memset(&dir, 0, sizeof dir);
628                         dir.name = "dns";
629                         dir.qid.type = QTFILE;
630                         dir.qid.vers = vers;
631                         dir.qid.path = Qdns;
632                         dir.mode = 0666;
633                         dir.length = 0;
634                         dir.uid = dir.gid = dir.muid = mf->user;
635                         dir.atime = dir.mtime = clock;          /* wrong */
636                         n = convD2M(&dir, buf, sizeof buf);
637                 }
638         } else if (off < 0)
639                 err = "negative read offset";
640         else {
641                 /* first offset will always be zero */
642                 for(i = 1; i <= mf->nrr; i++)
643                         if(mf->rr[i] > off)
644                                 break;
645                 if(i <= mf->nrr) {
646                         if(off + cnt > mf->rr[i])
647                                 n = mf->rr[i] - off;
648                         else
649                                 n = cnt;
650                         assert(n >= 0);
651                         job->reply.data = mf->reply + off;
652                 }
653         }
654         job->reply.count = n;
655         sendmsg(job, err);
656 }
657
658 void
659 rwrite(Job *job, Mfile *mf, Request *req)
660 {
661         int rooted, wantsav, send;
662         ulong cnt;
663         char *err, *p, *atype;
664         char errbuf[ERRMAX];
665
666         err = nil;
667         cnt = job->request.count;
668         send = 1;
669         if(mf->qid.type & QTDIR)
670                 err = "can't write directory";
671         else if (job->request.offset != 0)
672                 err = "writing at non-zero offset";
673         else if(cnt >= Maxrequest)
674                 err = "request too long";
675         else
676                 send = 0;
677         if (send)
678                 goto send;
679
680         job->request.data[cnt] = 0;
681         if(cnt > 0 && job->request.data[cnt-1] == '\n')
682                 job->request.data[cnt-1] = 0;
683
684         if(strcmp(mf->user, "none") == 0 || strcmp(mf->user, dnsuser) != 0)
685                 goto query;     /* skip special commands if not owner */
686
687         /*
688          *  special commands
689          */
690         if(debug)
691                 dnslog("rwrite got: %s", job->request.data);
692         send = 1;
693         if(strcmp(job->request.data, "debug")==0)
694                 debug ^= 1;
695         else if(strcmp(job->request.data, "dump")==0)
696                 dndump("/lib/ndb/dnsdump");
697         else if(strcmp(job->request.data, "refresh")==0)
698                 needrefresh = 1;
699         else if(strcmp(job->request.data, "stats")==0)
700                 dnstats("/lib/ndb/dnsstats");
701         else if(strncmp(job->request.data, "target ", 7)==0){
702                 target = atol(job->request.data + 7);
703                 dnslog("target set to %ld", target);
704         } else
705                 send = 0;
706         if (send)
707                 goto send;
708
709 query:
710         /*
711          *  kill previous reply
712          */
713         mf->nrr = 0;
714         mf->rr[0] = 0;
715
716         /*
717          *  break up request (into a name and a type)
718          */
719         atype = strchr(job->request.data, ' ');
720         if(atype == 0){
721                 snprint(errbuf, sizeof errbuf, "illegal request %s",
722                         job->request.data);
723                 err = errbuf;
724                 goto send;
725         } else
726                 *atype++ = 0;
727
728         /*
729          *  tracing request
730          */
731         if(strcmp(atype, "trace") == 0){
732                 if(trace)
733                         free(trace);
734                 if(*job->request.data)
735                         trace = estrdup(job->request.data);
736                 else
737                         trace = 0;
738                 goto send;
739         }
740
741         /* normal request: domain [type] */
742         stats.qrecvd9p++;
743         mf->type = rrtype(atype);
744         if(mf->type < 0){
745                 snprint(errbuf, sizeof errbuf, "unknown type %s", atype);
746                 err = errbuf;
747                 goto send;
748         }
749
750         p = atype - 2;
751         if(p >= job->request.data && *p == '.'){
752                 rooted = 1;
753                 *p = 0;
754         } else
755                 rooted = 0;
756
757         p = job->request.data;
758         if(*p == '!'){
759                 wantsav = 1;
760                 p++;
761         } else
762                 wantsav = 0;
763
764         err = lookupquery(job, mf, req, errbuf, p, wantsav, rooted);
765 send:
766         job->reply.count = cnt;
767         sendmsg(job, err);
768 }
769
770 /*
771  * dnsdebug calls
772  *      rr = dnresolve(buf, Cin, type, &req, nil, 0, Recurse, rooted, nil);
773  * which generates a UDP query, which eventually calls
774  *      dnserver(&reqmsg, &repmsg, &req, buf, rcode);
775  * which calls
776  *      rp = dnresolve(name, Cin, type, req, &mp->an, 0, recurse, 1, nil);
777  *
778  * but here we just call dnresolve directly.
779  */
780 static char *
781 lookupquery(Job *job, Mfile *mf, Request *req, char *errbuf, char *p,
782         int wantsav, int rooted)
783 {
784         int status;
785         RR *rp, *neg;
786
787         status = Rok;
788         rp = dnresolve(p, Cin, mf->type, req, nil, 0, Recurse, rooted, &status);
789
790         neg = rrremneg(&rp);
791         if(neg){
792                 status = neg->negrcode;
793                 rrfreelist(neg);
794         }
795
796         return respond(job, mf, rp, errbuf, status, wantsav);
797 }
798
799 static char *
800 respond(Job *job, Mfile *mf, RR *rp, char *errbuf, int status, int wantsav)
801 {
802         long n;
803         RR *tp;
804
805         if(rp == nil)
806                 switch(status){
807                 case Rname:
808                         return "name does not exist";
809                 case Rserver:
810                         return "dns failure";
811                 case Rok:
812                 default:
813                         snprint(errbuf, ERRMAX,
814                                 "resource does not exist; negrcode %d", status);
815                         return errbuf;
816                 }
817
818         lock(&joblock);
819         if(!job->flushed){
820                 /* format data to be read later */
821                 n = 0;
822                 mf->nrr = 0;
823                 for(tp = rp; mf->nrr < Maxrrr-1 && n < Maxreply && tp &&
824                     tsame(mf->type, tp->type); tp = tp->next){
825                         mf->rr[mf->nrr++] = n;
826                         if(wantsav)
827                                 n += snprint(mf->reply+n, Maxreply-n, "%Q", tp);
828                         else
829                                 n += snprint(mf->reply+n, Maxreply-n, "%R", tp);
830                 }
831                 mf->rr[mf->nrr] = n;
832         }
833         unlock(&joblock);
834
835         rrfreelist(rp);
836
837         return nil;
838 }
839
840 void
841 rclunk(Job *job, Mfile *mf)
842 {
843         freefid(mf);
844         sendmsg(job, 0);
845 }
846
847 void
848 rremove(Job *job, Mfile *mf)
849 {
850         USED(mf);
851         sendmsg(job, "remove permission denied");
852 }
853
854 void
855 rstat(Job *job, Mfile *mf)
856 {
857         Dir dir;
858         uchar buf[IOHDRSZ+Maxfdata];
859
860         memset(&dir, 0, sizeof dir);
861         if(mf->qid.type & QTDIR){
862                 dir.name = ".";
863                 dir.mode = DMDIR|0555;
864         } else {
865                 dir.name = "dns";
866                 dir.mode = 0666;
867         }
868         dir.qid = mf->qid;
869         dir.length = 0;
870         dir.uid = dir.gid = dir.muid = mf->user;
871         dir.atime = dir.mtime = time(nil);
872         job->reply.nstat = convD2M(&dir, buf, sizeof buf);
873         job->reply.stat = buf;
874         sendmsg(job, 0);
875 }
876
877 void
878 rwstat(Job *job, Mfile *mf)
879 {
880         USED(mf);
881         sendmsg(job, "wstat permission denied");
882 }
883
884 void
885 sendmsg(Job *job, char *err)
886 {
887         int n;
888         uchar mdata[IOHDRSZ + Maxfdata];
889         char ename[ERRMAX];
890
891         if(err){
892                 job->reply.type = Rerror;
893                 snprint(ename, sizeof ename, "dns: %s", err);
894                 job->reply.ename = ename;
895         }else
896                 job->reply.type = job->request.type+1;
897         job->reply.tag = job->request.tag;
898         n = convS2M(&job->reply, mdata, sizeof mdata);
899         if(n == 0){
900                 warning("sendmsg convS2M of %F returns 0", &job->reply);
901                 abort();
902         }
903         lock(&joblock);
904         if(job->flushed == 0)
905                 if(write(mfd[1], mdata, n)!=n)
906                         sysfatal("mount write");
907         unlock(&joblock);
908         if(debug)
909                 dnslog("%F %d", &job->reply, n);
910 }
911
912 /*
913  *  the following varies between dnsdebug and dns
914  */
915 void
916 logreply(int id, uchar *addr, DNSmsg *mp)
917 {
918         RR *rp;
919
920         dnslog("%d: rcvd %I flags:%s%s%s%s%s", id, addr,
921                 mp->flags & Fauth? " auth": "",
922                 mp->flags & Ftrunc? " trunc": "",
923                 mp->flags & Frecurse? " rd": "",
924                 mp->flags & Fcanrec? " ra": "",
925                 (mp->flags & (Fauth|Rmask)) == (Fauth|Rname)? " nx": "");
926         for(rp = mp->qd; rp != nil; rp = rp->next)
927                 dnslog("%d: rcvd %I qd %s", id, addr, rp->owner->name);
928         for(rp = mp->an; rp != nil; rp = rp->next)
929                 dnslog("%d: rcvd %I an %R", id, addr, rp);
930         for(rp = mp->ns; rp != nil; rp = rp->next)
931                 dnslog("%d: rcvd %I ns %R", id, addr, rp);
932         for(rp = mp->ar; rp != nil; rp = rp->next)
933                 dnslog("%d: rcvd %I ar %R", id, addr, rp);
934 }
935
936 void
937 logsend(int id, int subid, uchar *addr, char *sname, char *rname, int type)
938 {
939         char buf[12];
940
941         dnslog("[%d] %d.%d: sending to %I/%s %s %s",
942                 getpid(), id, subid, addr, sname, rname,
943                 rrname(type, buf, sizeof buf));
944 }
945
946 RR*
947 getdnsservers(int class)
948 {
949         return dnsservers(class);
950 }