12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109 |
- /*
- * This file is part of the UCB release of Plan 9. It is subject to the license
- * terms in the LICENSE file found in the top-level directory of this
- * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
- * part of the UCB release of Plan 9, including this file, may be copied,
- * modified, propagated, or distributed except according to the terms contained
- * in the LICENSE file.
- */
- /*
- * 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;
- uint32_t 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 */
- uint32_t mtime;
- uint32_t 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;
- uint32_t now;
- int netdebug;
- int readonly;
- void*
- erealloc(void *v, uint32_t n)
- {
- v = realloc(v, n);
- if(v == nil)
- sysfatal("out of memory reallocating %lu", n);
- setmalloctag(v, getcallerpc());
- return v;
- }
- void*
- emalloc(uint32_t n)
- {
- void *v;
- v = malloc(n);
- if(v == nil)
- sysfatal("out of memory allocating %lu", n);
- memset(v, 0, n);
- setmalloctag(v, getcallerpc());
- 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());
- 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());
- 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 {
- uint32_t 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 %s: %r", n->addr);
- 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(const void *v1, const 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, '.')) != nil)
- *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)) != nil){
- 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')) != nil)
- *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])) != nil){
- *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)) != nil){
- 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, (uint8_t*)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;
- int32_t count;
- int64_t 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);
- }
|