1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102 |
- /*
- * Network news transport protocol (NNTP) file server.
- *
- * Unfortunately, the file system differs from that expected
- * by Charles Forsyth's rin news reader. This is partially out
- * of my own laziness, but it makes the bookkeeping here
- * a lot easier.
- */
- #include <u.h>
- #include <libc.h>
- #include <bio.h>
- #include <auth.h>
- #include <fcall.h>
- #include <thread.h>
- #include <9p.h>
- typedef struct Netbuf Netbuf;
- typedef struct Group Group;
- struct Netbuf {
- Biobuf br;
- Biobuf bw;
- int lineno;
- int fd;
- int code; /* last response code */
- int auth; /* Authorization required? */
- char response[128]; /* last response */
- Group *currentgroup;
- char *addr;
- char *user;
- char *pass;
- ulong extended; /* supported extensions */
- };
- struct Group {
- char *name;
- Group *parent;
- Group **kid;
- int num;
- int nkid;
- int lo, hi;
- int canpost;
- int isgroup; /* might just be piece of hierarchy */
- ulong mtime;
- ulong atime;
- };
- /*
- * First eight fields are, in order:
- * article number, subject, author, date, message-ID,
- * references, byte count, line count
- * We don't support OVERVIEW.FMT; when I see a server with more
- * interesting fields, I'll implement support then. In the meantime,
- * the standard defines the first eight fields.
- */
- /* Extensions */
- enum {
- Nxover = (1<<0),
- Nxhdr = (1<<1),
- Nxpat = (1<<2),
- Nxlistgp = (1<<3),
- };
- Group *root;
- Netbuf *net;
- ulong now;
- int netdebug;
- int readonly;
- void*
- erealloc(void *v, ulong n)
- {
- v = realloc(v, n);
- if(v == nil)
- sysfatal("out of memory reallocating %lud", n);
- setmalloctag(v, getcallerpc(&v));
- return v;
- }
- void*
- emalloc(ulong n)
- {
- void *v;
- v = malloc(n);
- if(v == nil)
- sysfatal("out of memory allocating %lud", n);
- memset(v, 0, n);
- setmalloctag(v, getcallerpc(&n));
- return v;
- }
- char*
- estrdup(char *s)
- {
- int l;
- char *t;
- if (s == nil)
- return nil;
- l = strlen(s)+1;
- t = emalloc(l);
- memcpy(t, s, l);
- setmalloctag(t, getcallerpc(&s));
- return t;
- }
- char*
- estrdupn(char *s, int n)
- {
- int l;
- char *t;
- l = strlen(s);
- if(l > n)
- l = n;
- t = emalloc(l+1);
- memmove(t, s, l);
- t[l] = '\0';
- setmalloctag(t, getcallerpc(&s));
- return t;
- }
- char*
- Nrdline(Netbuf *n)
- {
- char *p;
- int l;
- n->lineno++;
- Bflush(&n->bw);
- if((p = Brdline(&n->br, '\n')) == nil){
- werrstr("nntp eof");
- return nil;
- }
- p[l=Blinelen(&n->br)-1] = '\0';
- if(l > 0 && p[l-1] == '\r')
- p[l-1] = '\0';
- if(netdebug)
- fprint(2, "-> %s\n", p);
- return p;
- }
- int
- nntpresponse(Netbuf *n, int e, char *cmd)
- {
- int r;
- char *p;
- for(;;){
- p = Nrdline(n);
- if(p==nil){
- strcpy(n->response, "early nntp eof");
- return -1;
- }
- r = atoi(p);
- if(r/100 == 1){ /* BUG? */
- fprint(2, "%s\n", p);
- continue;
- }
- break;
- }
- strecpy(n->response, n->response+sizeof(n->response), p);
- if((r=atoi(p)) == 0){
- close(n->fd);
- n->fd = -1;
- fprint(2, "bad nntp response: %s\n", p);
- werrstr("bad nntp response");
- return -1;
- }
- n->code = r;
- if(0 < e && e<10 && r/100 != e){
- fprint(2, "%s: expected %dxx: got %s\n", cmd, e, n->response);
- return -1;
- }
- if(10 <= e && e<100 && r/10 != e){
- fprint(2, "%s: expected %dx: got %s\n", cmd, e, n->response);
- return -1;
- }
- if(100 <= e && r != e){
- fprint(2, "%s: expected %d: got %s\n", cmd, e, n->response);
- return -1;
- }
- return r;
- }
- int nntpauth(Netbuf*);
- int nntpxcmdprobe(Netbuf*);
- int nntpcurrentgroup(Netbuf*, Group*);
- /* XXX: bug OVER/XOVER et al. */
- static struct {
- ulong n;
- char *s;
- } extensions [] = {
- { Nxover, "OVER" },
- { Nxhdr, "HDR" },
- { Nxpat, "PAT" },
- { Nxlistgp, "LISTGROUP" },
- { 0, nil }
- };
- static int indial;
- int
- nntpconnect(Netbuf *n)
- {
- n->currentgroup = nil;
- close(n->fd);
- if((n->fd = dial(n->addr, nil, nil, nil)) < 0){
- snprint(n->response, sizeof n->response, "dial: %r");
- return -1;
- }
- Binit(&n->br, n->fd, OREAD);
- Binit(&n->bw, n->fd, OWRITE);
- if(nntpresponse(n, 20, "greeting") < 0)
- return -1;
- readonly = (n->code == 201);
- indial = 1;
- if(n->auth != 0)
- nntpauth(n);
- // nntpxcmdprobe(n);
- indial = 0;
- return 0;
- }
- int
- nntpcmd(Netbuf *n, char *cmd, int e)
- {
- int tried;
- tried = 0;
- for(;;){
- if(netdebug)
- fprint(2, "<- %s\n", cmd);
- Bprint(&n->bw, "%s\r\n", cmd);
- if(nntpresponse(n, e, cmd)>=0 && (e < 0 || n->code/100 != 5))
- return 0;
- /* redial */
- if(indial || tried++ || nntpconnect(n) < 0)
- return -1;
- }
- }
- int
- nntpauth(Netbuf *n)
- {
- char cmd[256];
- snprint(cmd, sizeof cmd, "AUTHINFO USER %s", n->user);
- if (nntpcmd(n, cmd, -1) < 0 || n->code != 381) {
- fprint(2, "Authentication failed: %s\n", n->response);
- return -1;
- }
- snprint(cmd, sizeof cmd, "AUTHINFO PASS %s", n->pass);
- if (nntpcmd(n, cmd, -1) < 0 || n->code != 281) {
- fprint(2, "Authentication failed: %s\n", n->response);
- return -1;
- }
- return 0;
- }
- int
- nntpxcmdprobe(Netbuf *n)
- {
- int i;
- char *p;
- n->extended = 0;
- if (nntpcmd(n, "LIST EXTENSIONS", 0) < 0 || n->code != 202)
- return 0;
- while((p = Nrdline(n)) != nil) {
- if (strcmp(p, ".") == 0)
- break;
- for(i=0; extensions[i].s != nil; i++)
- if (cistrcmp(extensions[i].s, p) == 0) {
- n->extended |= extensions[i].n;
- break;
- }
- }
- return 0;
- }
- /* XXX: searching, lazy evaluation */
- static int
- overcmp(void *v1, void *v2)
- {
- int a, b;
- a = atoi(*(char**)v1);
- b = atoi(*(char**)v2);
- if(a < b)
- return -1;
- else if(a > b)
- return 1;
- return 0;
- }
- enum {
- XoverChunk = 100,
- };
- char *xover[XoverChunk];
- int xoverlo;
- int xoverhi;
- int xovercount;
- Group *xovergroup;
- char*
- nntpover(Netbuf *n, Group *g, int m)
- {
- int i, lo, hi, mid, msg;
- char *p;
- char cmd[64];
- if (g->isgroup == 0) /* BUG: should check extension capabilities */
- return nil;
- if(g != xovergroup || m < xoverlo || m >= xoverhi){
- lo = (m/XoverChunk)*XoverChunk;
- hi = lo+XoverChunk;
-
- if(lo < g->lo)
- lo = g->lo;
- else if (lo > g->hi)
- lo = g->hi;
- if(hi < lo || hi > g->hi)
- hi = g->hi;
-
- if(nntpcurrentgroup(n, g) < 0)
- return nil;
-
- if(lo == hi)
- snprint(cmd, sizeof cmd, "XOVER %d", hi);
- else
- snprint(cmd, sizeof cmd, "XOVER %d-%d", lo, hi-1);
- if(nntpcmd(n, cmd, 224) < 0)
- return nil;
- for(i=0; (p = Nrdline(n)) != nil; i++) {
- if(strcmp(p, ".") == 0)
- break;
- if(i >= XoverChunk)
- sysfatal("news server doesn't play by the rules");
- free(xover[i]);
- xover[i] = emalloc(strlen(p)+2);
- strcpy(xover[i], p);
- strcat(xover[i], "\n");
- }
- qsort(xover, i, sizeof(xover[0]), overcmp);
- xovercount = i;
- xovergroup = g;
- xoverlo = lo;
- xoverhi = hi;
- }
- lo = 0;
- hi = xovercount;
- /* search for message */
- while(lo < hi){
- mid = (lo+hi)/2;
- msg = atoi(xover[mid]);
- if(m == msg)
- return xover[mid];
- else if(m < msg)
- hi = mid;
- else
- lo = mid+1;
- }
- return nil;
- }
- /*
- * Return the new Group structure for the group name.
- * Destroys name.
- */
- static int printgroup(char*,Group*);
- Group*
- findgroup(Group *g, char *name, int mk)
- {
- int lo, hi, m;
- char *p, *q;
- static int ngroup;
- for(p=name; *p; p=q){
- if(q = strchr(p, '.'))
- *q++ = '\0';
- else
- q = p+strlen(p);
- lo = 0;
- hi = g->nkid;
- while(hi-lo > 1){
- m = (lo+hi)/2;
- if(strcmp(p, g->kid[m]->name) < 0)
- hi = m;
- else
- lo = m;
- }
- assert(lo==hi || lo==hi-1);
- if(lo==hi || strcmp(p, g->kid[lo]->name) != 0){
- if(mk==0)
- return nil;
- if(g->nkid%16 == 0)
- g->kid = erealloc(g->kid, (g->nkid+16)*sizeof(g->kid[0]));
- /*
- * if we're down to a single place 'twixt lo and hi, the insertion might need
- * to go at lo or at hi. strcmp to find out. the list needs to stay sorted.
- */
- if(lo==hi-1 && strcmp(p, g->kid[lo]->name) < 0)
- hi = lo;
- if(hi < g->nkid)
- memmove(g->kid+hi+1, g->kid+hi, sizeof(g->kid[0])*(g->nkid-hi));
- g->nkid++;
- g->kid[hi] = emalloc(sizeof(*g));
- g->kid[hi]->parent = g;
- g = g->kid[hi];
- g->name = estrdup(p);
- g->num = ++ngroup;
- g->mtime = time(0);
- }else
- g = g->kid[lo];
- }
- if(mk)
- g->isgroup = 1;
- return g;
- }
- static int
- printgroup(char *s, Group *g)
- {
- if(g->parent == g)
- return 0;
- if(printgroup(s, g->parent))
- strcat(s, ".");
- strcat(s, g->name);
- return 1;
- }
- static char*
- Nreaddata(Netbuf *n)
- {
- char *p, *q;
- int l;
- p = nil;
- l = 0;
- for(;;){
- q = Nrdline(n);
- if(q==nil){
- free(p);
- return nil;
- }
- if(strcmp(q, ".")==0)
- return p;
- if(q[0]=='.')
- q++;
- p = erealloc(p, l+strlen(q)+1+1);
- strcpy(p+l, q);
- strcat(p+l, "\n");
- l += strlen(p+l);
- }
- }
- /*
- * Return the output of a HEAD, BODY, or ARTICLE command.
- */
- char*
- nntpget(Netbuf *n, Group *g, int msg, char *retr)
- {
- char *s;
- char cmd[1024];
- if(g->isgroup == 0){
- werrstr("not a group");
- return nil;
- }
- if(strcmp(retr, "XOVER") == 0){
- s = nntpover(n, g, msg);
- if(s == nil)
- s = "";
- return estrdup(s);
- }
- if(nntpcurrentgroup(n, g) < 0)
- return nil;
- sprint(cmd, "%s %d", retr, msg);
- nntpcmd(n, cmd, 0);
- if(n->code/10 != 22)
- return nil;
- return Nreaddata(n);
- }
- int
- nntpcurrentgroup(Netbuf *n, Group *g)
- {
- char cmd[1024];
- if(n->currentgroup != g){
- strcpy(cmd, "GROUP ");
- printgroup(cmd, g);
- if(nntpcmd(n, cmd, 21) < 0)
- return -1;
- n->currentgroup = g;
- }
- return 0;
- }
- void
- nntprefreshall(Netbuf *n)
- {
- char *f[10], *p;
- int hi, lo, nf;
- Group *g;
- if(nntpcmd(n, "LIST", 21) < 0)
- return;
- while(p = Nrdline(n)){
- if(strcmp(p, ".")==0)
- break;
- nf = getfields(p, f, nelem(f), 1, "\t\r\n ");
- if(nf != 4){
- int i;
- for(i=0; i<nf; i++)
- fprint(2, "%s%s", i?" ":"", f[i]);
- fprint(2, "\n");
- fprint(2, "syntax error in group list, line %d", n->lineno);
- return;
- }
- g = findgroup(root, f[0], 1);
- hi = strtol(f[1], 0, 10)+1;
- lo = strtol(f[2], 0, 10);
- if(g->hi != hi){
- g->hi = hi;
- if(g->lo==0)
- g->lo = lo;
- g->canpost = f[3][0] == 'y';
- g->mtime = time(0);
- }
- }
- }
- void
- nntprefresh(Netbuf *n, Group *g)
- {
- char cmd[1024];
- char *f[5];
- int lo, hi;
- if(g->isgroup==0)
- return;
- if(time(0) - g->atime < 30)
- return;
- strcpy(cmd, "GROUP ");
- printgroup(cmd, g);
- if(nntpcmd(n, cmd, 21) < 0){
- n->currentgroup = nil;
- return;
- }
- n->currentgroup = g;
- if(tokenize(n->response, f, nelem(f)) < 4){
- fprint(2, "error reading GROUP response");
- return;
- }
- /* backwards from LIST! */
- hi = strtol(f[3], 0, 10)+1;
- lo = strtol(f[2], 0, 10);
- if(g->hi != hi){
- g->mtime = time(0);
- if(g->lo==0)
- g->lo = lo;
- g->hi = hi;
- }
- g->atime = time(0);
- }
- char*
- nntppost(Netbuf *n, char *msg)
- {
- char *p, *q;
- if(nntpcmd(n, "POST", 34) < 0)
- return n->response;
- for(p=msg; *p; p=q){
- if(q = strchr(p, '\n'))
- *q++ = '\0';
- else
- q = p+strlen(p);
- if(p[0]=='.')
- Bputc(&n->bw, '.');
- Bwrite(&n->bw, p, strlen(p));
- Bputc(&n->bw, '\r');
- Bputc(&n->bw, '\n');
- }
- Bprint(&n->bw, ".\r\n");
- if(nntpresponse(n, 0, nil) < 0)
- return n->response;
- if(n->code/100 != 2)
- return n->response;
- return nil;
- }
- /*
- * Because an expanded QID space makes thngs much easier,
- * we sleazily use the version part of the QID as more path bits.
- * Since we make sure not to mount ourselves cached, this
- * doesn't break anything (unless you want to bind on top of
- * things in this file system). In the next version of 9P, we'll
- * have more QID bits to play with.
- *
- * The newsgroup is encoded in the top 15 bits
- * of the path. The message number is the bottom 17 bits.
- * The file within the message directory is in the version [sic].
- */
- enum { /* file qids */
- Qhead,
- Qbody,
- Qarticle,
- Qxover,
- Nfile,
- };
- char *filename[] = {
- "header",
- "body",
- "article",
- "xover",
- };
- char *nntpname[] = {
- "HEAD",
- "BODY",
- "ARTICLE",
- "XOVER",
- };
- #define GROUP(p) (((p)>>17)&0x3FFF)
- #define MESSAGE(p) ((p)&0x1FFFF)
- #define FILE(v) ((v)&0x3)
- #define PATH(g,m) ((((g)&0x3FFF)<<17)|((m)&0x1FFFF))
- #define POST(g) PATH(0,g,0)
- #define VERS(f) ((f)&0x3)
- typedef struct Aux Aux;
- struct Aux {
- Group *g;
- int n;
- int ispost;
- int file;
- char *s;
- int ns;
- int offset;
- };
- static void
- fsattach(Req *r)
- {
- Aux *a;
- char *spec;
- spec = r->ifcall.aname;
- if(spec && spec[0]){
- respond(r, "invalid attach specifier");
- return;
- }
- a = emalloc(sizeof *a);
- a->g = root;
- a->n = -1;
- r->fid->aux = a;
-
- r->ofcall.qid = (Qid){0, 0, QTDIR};
- r->fid->qid = r->ofcall.qid;
- respond(r, nil);
- }
- static char*
- fsclone(Fid *ofid, Fid *fid)
- {
- Aux *a;
- a = emalloc(sizeof(*a));
- *a = *(Aux*)ofid->aux;
- fid->aux = a;
- return nil;
- }
- static char*
- fswalk1(Fid *fid, char *name, Qid *qid)
- {
- char *p;
- int i, isdotdot, n;
- Aux *a;
- Group *ng;
- isdotdot = strcmp(name, "..")==0;
- a = fid->aux;
- if(a->s) /* file */
- return "protocol botch";
- if(a->n != -1){
- if(isdotdot){
- *qid = (Qid){PATH(a->g->num, 0), 0, QTDIR};
- fid->qid = *qid;
- a->n = -1;
- return nil;
- }
- for(i=0; i<Nfile; i++){
- if(strcmp(name, filename[i])==0){
- if(a->s = nntpget(net, a->g, a->n, nntpname[i])){
- *qid = (Qid){PATH(a->g->num, a->n), Qbody, 0};
- fid->qid = *qid;
- a->file = i;
- return nil;
- }else
- return "file does not exist";
- }
- }
- return "file does not exist";
- }
- if(isdotdot){
- a->g = a->g->parent;
- *qid = (Qid){PATH(a->g->num, 0), 0, QTDIR};
- fid->qid = *qid;
- return nil;
- }
- if(a->g->isgroup && !readonly && a->g->canpost
- && strcmp(name, "post")==0){
- a->ispost = 1;
- *qid = (Qid){PATH(a->g->num, 0), 0, 0};
- fid->qid = *qid;
- return nil;
- }
- if(ng = findgroup(a->g, name, 0)){
- a->g = ng;
- *qid = (Qid){PATH(a->g->num, 0), 0, QTDIR};
- fid->qid = *qid;
- return nil;
- }
- n = strtoul(name, &p, 0);
- if('0'<=name[0] && name[0]<='9' && *p=='\0' && a->g->lo<=n && n<a->g->hi){
- a->n = n;
- *qid = (Qid){PATH(a->g->num, n+1-a->g->lo), 0, QTDIR};
- fid->qid = *qid;
- return nil;
- }
- return "file does not exist";
- }
- static void
- fsopen(Req *r)
- {
- Aux *a;
- a = r->fid->aux;
- if((a->ispost && (r->ifcall.mode&~OTRUNC) != OWRITE)
- || (!a->ispost && r->ifcall.mode != OREAD))
- respond(r, "permission denied");
- else
- respond(r, nil);
- }
- static void
- fillstat(Dir *d, Aux *a)
- {
- char buf[32];
- Group *g;
- memset(d, 0, sizeof *d);
- d->uid = estrdup("nntp");
- d->gid = estrdup("nntp");
- g = a->g;
- d->atime = d->mtime = g->mtime;
- if(a->ispost){
- d->name = estrdup("post");
- d->mode = 0222;
- d->qid = (Qid){PATH(g->num, 0), 0, 0};
- d->length = a->ns;
- return;
- }
- if(a->s){ /* article file */
- d->name = estrdup(filename[a->file]);
- d->mode = 0444;
- d->qid = (Qid){PATH(g->num, a->n+1-g->lo), a->file, 0};
- return;
- }
- if(a->n != -1){ /* article directory */
- sprint(buf, "%d", a->n);
- d->name = estrdup(buf);
- d->mode = DMDIR|0555;
- d->qid = (Qid){PATH(g->num, a->n+1-g->lo), 0, QTDIR};
- return;
- }
- /* group directory */
- if(g->name[0])
- d->name = estrdup(g->name);
- else
- d->name = estrdup("/");
- d->mode = DMDIR|0555;
- d->qid = (Qid){PATH(g->num, 0), g->hi-1, QTDIR};
- }
- static int
- dirfillstat(Dir *d, Aux *a, int i)
- {
- int ndir;
- Group *g;
- char buf[32];
- memset(d, 0, sizeof *d);
- d->uid = estrdup("nntp");
- d->gid = estrdup("nntp");
- g = a->g;
- d->atime = d->mtime = g->mtime;
- if(a->n != -1){ /* article directory */
- if(i >= Nfile)
- return -1;
- d->name = estrdup(filename[i]);
- d->mode = 0444;
- d->qid = (Qid){PATH(g->num, a->n), i, 0};
- return 0;
- }
- /* hierarchy directory: child groups */
- if(i < g->nkid){
- d->name = estrdup(g->kid[i]->name);
- d->mode = DMDIR|0555;
- d->qid = (Qid){PATH(g->kid[i]->num, 0), g->kid[i]->hi-1, QTDIR};
- return 0;
- }
- i -= g->nkid;
- /* group directory: post file */
- if(g->isgroup && !readonly && g->canpost){
- if(i < 1){
- d->name = estrdup("post");
- d->mode = 0222;
- d->qid = (Qid){PATH(g->num, 0), 0, 0};
- return 0;
- }
- i--;
- }
- /* group directory: child articles */
- ndir = g->hi - g->lo;
- if(i < ndir){
- sprint(buf, "%d", g->lo+i);
- d->name = estrdup(buf);
- d->mode = DMDIR|0555;
- d->qid = (Qid){PATH(g->num, i+1), 0, QTDIR};
- return 0;
- }
- return -1;
- }
- static void
- fsstat(Req *r)
- {
- Aux *a;
- a = r->fid->aux;
- if(r->fid->qid.path == 0 && (r->fid->qid.type & QTDIR))
- nntprefreshall(net);
- else if(a->g->isgroup)
- nntprefresh(net, a->g);
- fillstat(&r->d, a);
- respond(r, nil);
- }
- static void
- fsread(Req *r)
- {
- int offset, n;
- Aux *a;
- char *p, *ep;
- Dir d;
- a = r->fid->aux;
- if(a->s){
- readstr(r, a->s);
- respond(r, nil);
- return;
- }
- if(r->ifcall.offset == 0)
- offset = 0;
- else
- offset = a->offset;
- p = r->ofcall.data;
- ep = r->ofcall.data+r->ifcall.count;
- for(; p+2 < ep; p += n){
- if(dirfillstat(&d, a, offset) < 0)
- break;
- n=convD2M(&d, (uchar*)p, ep-p);
- free(d.name);
- free(d.uid);
- free(d.gid);
- free(d.muid);
- if(n <= BIT16SZ)
- break;
- offset++;
- }
- a->offset = offset;
- r->ofcall.count = p - r->ofcall.data;
- respond(r, nil);
- }
- static void
- fswrite(Req *r)
- {
- Aux *a;
- long count;
- vlong offset;
- a = r->fid->aux;
- if(r->ifcall.count == 0){ /* commit */
- respond(r, nntppost(net, a->s));
- free(a->s);
- a->ns = 0;
- a->s = nil;
- return;
- }
- count = r->ifcall.count;
- offset = r->ifcall.offset;
- if(a->ns < count+offset+1){
- a->s = erealloc(a->s, count+offset+1);
- a->ns = count+offset;
- a->s[a->ns] = '\0';
- }
- memmove(a->s+offset, r->ifcall.data, count);
- r->ofcall.count = count;
- respond(r, nil);
- }
- static void
- fsdestroyfid(Fid *fid)
- {
- Aux *a;
- a = fid->aux;
- if(a==nil)
- return;
- if(a->ispost && a->s)
- nntppost(net, a->s);
- free(a->s);
- free(a);
- }
- Srv nntpsrv = {
- .destroyfid= fsdestroyfid,
- .attach= fsattach,
- .clone= fsclone,
- .walk1= fswalk1,
- .open= fsopen,
- .read= fsread,
- .write= fswrite,
- .stat= fsstat,
- };
- void
- usage(void)
- {
- fprint(2, "usage: nntpsrv [-a] [-s service] [-m mtpt] [nntp.server]\n");
- exits("usage");
- }
- void
- dumpgroups(Group *g, int ind)
- {
- int i;
- print("%*s%s\n", ind*4, "", g->name);
- for(i=0; i<g->nkid; i++)
- dumpgroups(g->kid[i], ind+1);
- }
- void
- main(int argc, char **argv)
- {
- int auth, x;
- char *mtpt, *service, *where, *user;
- Netbuf n;
- UserPasswd *up;
- mtpt = "/mnt/news";
- service = nil;
- memset(&n, 0, sizeof n);
- user = nil;
- auth = 0;
- ARGBEGIN{
- case 'D':
- chatty9p++;
- break;
- case 'N':
- netdebug = 1;
- break;
- case 'a':
- auth = 1;
- break;
- case 'u':
- user = EARGF(usage());
- break;
- case 's':
- service = EARGF(usage());
- break;
- case 'm':
- mtpt = EARGF(usage());
- break;
- default:
- usage();
- }ARGEND
- if(argc > 1)
- usage();
- if(argc==0)
- where = "$nntp";
- else
- where = argv[0];
- now = time(0);
- net = &n;
- if(auth) {
- n.auth = 1;
- if(user)
- up = auth_getuserpasswd(auth_getkey, "proto=pass service=nntp server=%q user=%q", where, user);
- else
- up = auth_getuserpasswd(auth_getkey, "proto=pass service=nntp server=%q", where);
- if(up == nil)
- sysfatal("no password: %r");
- n.user = up->user;
- n.pass = up->passwd;
- }
- n.addr = netmkaddr(where, "tcp", "nntp");
- root = emalloc(sizeof *root);
- root->name = estrdup("");
- root->parent = root;
- n.fd = -1;
- if(nntpconnect(&n) < 0)
- sysfatal("nntpconnect: %s", n.response);
- x=netdebug;
- netdebug=0;
- nntprefreshall(&n);
- netdebug=x;
- // dumpgroups(root, 0);
- postmountsrv(&nntpsrv, service, mtpt, MREPL);
- exits(nil);
- }
|