11 char* secureidcheck(char *, char *); /* from /sys/src/cmd/auth/ */
12 extern char* dirls(char *path);
20 fprint(2, "usage: secstored [-R] [-S servername] [-s tcp!*!5356] [-v] [-x netmtpt]\n");
25 getdir(SConn *conn, char *id)
32 snprint(s, Maxmsg, "%s/store/%s", SECSTORE_DIR, id);
34 if((ls = dirls(s)) == nil)
40 snprint(s, Maxmsg, "%d", len);
41 conn->write(conn, (uchar*)s, strlen(s));
43 /* send directory listing in Maxmsg chunks */
49 conn->write(conn, msg, n);
65 if(nl = strchr(f, '\n'))
67 if(strchr(f,'/') != nil || strcmp(f,"..")==0 || strlen(f) >= 300){
68 syslog(0, LOG, "no slashes allowed: %s\n", f);
75 getfile(SConn *conn, char *id, char *gf)
83 return getdir(conn, id);
87 snprint(s, Maxmsg, "%s/store/%s/%s", SECSTORE_DIR, id, gf);
90 syslog(0, LOG, "can't open %s: %r\n", s);
92 conn->write(conn, (uchar*)"-1", 2);
97 syslog(0, LOG, "can't stat %s: %r\n", s);
99 conn->write(conn, (uchar*)"-1", 2);
106 syslog(0, LOG, "%s should be a plain file, not a directory\n", s);
108 conn->write(conn, (uchar*)"-1", 2);
111 if(len < 0 || len > MAXFILESIZE){
112 syslog(0, LOG, "implausible filesize %d for %s\n", len, gf);
114 conn->write(conn, (uchar*)"-3", 2);
117 snprint(s, Maxmsg, "%d", len);
118 conn->write(conn, (uchar*)s, strlen(s));
120 /* send file in Maxmsg chunks */
122 n = read(gd, s, Maxmsg);
124 syslog(0, LOG, "read error on %s: %r\n", gf);
128 conn->write(conn, (uchar*)s, n);
137 putfile(SConn *conn, char *id, char *pf)
144 n = readstr(conn, s);
146 syslog(0, LOG, "remote: %s: %r\n", s);
151 syslog(0, LOG, "remote file %s does not exist\n", pf);
153 }else if(len < 0 || len > MAXFILESIZE){
154 syslog(0, LOG, "implausible filesize %ld for %s\n", len, pf);
158 /* get file in Maxmsg chunks */
159 if(strchr(pf,'/') != nil || strcmp(pf,"..")==0){
160 syslog(0, LOG, "no slashes allowed: %s\n", pf);
163 snprint(s, Maxmsg, "%s/store/%s/%s", SECSTORE_DIR, id, pf);
164 pd = create(s, OWRITE, 0660);
166 syslog(0, LOG, "can't open %s: %r\n", s);
170 n = conn->read(conn, (uchar*)s, Maxmsg);
172 syslog(0, LOG, "empty file chunk\n");
175 nw = write(pd, s, n);
177 syslog(0, LOG, "write error on %s: %r", pf);
188 removefile(SConn *conn, char *id, char *f)
193 snprint(buf, Maxmsg, "%s/store/%s/%s", SECSTORE_DIR, id, f);
195 if((d = dirstat(buf)) == nil){
196 snprint(buf, sizeof buf, "remove failed: %r");
199 }else if(d->mode & DMDIR){
200 snprint(buf, sizeof buf, "can't remove a directory");
208 snprint(buf, sizeof buf, "remove failed: %r");
215 /* given line directory from accept, returns ipaddr!port */
220 char rp[100], ap[500];
222 snprint(rp, sizeof rp, "%s/remote", ldir);
223 fd = open(rp, OREAD);
225 return strdup("?!?");
226 n = read(fd, ap, sizeof ap);
227 if(n <= 0 || n == sizeof ap){
228 fprint(2, "error %d reading %s: %r\n", n, rp);
229 return strdup("?!?");
239 dologin(int fd, char *S, int forceSTA)
250 /* collect the first message */
251 if((conn = newSConn(fd)) == nil)
253 if(readstr(conn, msg) < 0){
254 fprint(2, "remote: %s: %r\n", msg);
255 writerr(conn, "can't read your first message");
260 if(PAKserver(conn, S, msg, &pw) < 0){
262 syslog(0, LOG, "secstore denied for %s", pw->id);
265 if((forceSTA || pw->status&STA) != 0){
266 conn->write(conn, (uchar*)"STA", 3);
267 if(readstr(conn, msg) < 10 || strncmp(msg, "STA", 3) != 0){
268 syslog(0, LOG, "no STA from %s", pw->id);
271 mess = secureidcheck(pw->id, msg+3);
273 syslog(0, LOG, "secureidcheck denied %s because %s", pw->id, mess);
277 conn->write(conn, (uchar*)"OK", 2);
278 syslog(0, LOG, "AUTH %s", pw->id);
280 /* perform operations as asked */
281 while((n = readstr(conn, msg)) > 0){
282 syslog(0, LOG, "[%s] %s", pw->id, msg);
284 if(strncmp(msg, "GET ", 4) == 0){
285 file = validatefile(msg+4);
286 if(file==nil || getfile(conn, pw->id, file) < 0)
289 }else if(strncmp(msg, "PUT ", 4) == 0){
290 file = validatefile(msg+4);
291 if(file==nil || putfile(conn, pw->id, file) < 0){
292 syslog(0, LOG, "failed PUT %s/%s", pw->id, file);
296 }else if(strncmp(msg, "RM ", 3) == 0){
297 file = validatefile(msg+3);
298 if(file==nil || removefile(conn, pw->id, file) < 0){
299 syslog(0, LOG, "failed RM %s/%s", pw->id, file);
303 }else if(strncmp(msg, "CHPASS", 6) == 0){
304 if(readstr(conn, msg) < 0){
305 syslog(0, LOG, "protocol botch CHPASS for %s", pw->id);
306 writerr(conn, "protocol botch while setting PAK");
309 pw->Hi = strtomp(msg, nil, 64, pw->Hi);
310 for(i=0; i < 4 && putPW(pw) < 0; i++)
311 syslog(0, LOG, "password change failed for %s (%d): %r", pw->id, i);
315 }else if(strncmp(msg, "BYE", 3) == 0){
320 writerr(conn, "unrecognized operation");
326 syslog(0, LOG, "%s closed connection without saying goodbye\n", pw->id);
333 writerr(conn, "operation failed");
338 main(int argc, char **argv)
340 int afd, dfd, lcfd, forceSTA = 0;
341 char adir[40], ldir[40], *remote;
342 char *serve = "tcp!*!5356", *p, aserve[128];
343 char *S = "secstore";
348 SECSTORE_DIR = unsharp("#9/secstore");
349 /* setnetmtpt(net, sizeof(net), nil); */
355 serve = EARGF(usage());
365 /* setnetmtpt(net, sizeof(net), p); */
375 switch(rfork(RFNOTEG|RFPROC|RFFDG)) {
377 sysfatal("fork: %r");
384 snprint(aserve, sizeof aserve, "%s", serve);
385 afd = announce(aserve, adir);
387 sysfatal("%s: %r\n", aserve);
388 syslog(0, LOG, "ANNOUNCE %s", aserve);
390 if((lcfd = listen(adir, ldir)) < 0)
391 exits("can't listen");
392 switch(rfork(RFPROC|RFFDG|RFNOWAIT)){
394 fprint(2, "secstore forking: %r\n");
398 /* "/lib/ndb/common.radius does not exist" if db set before fork */
399 db = ndbopen(dbpath=unsharp("#9/ndb/auth"));
401 syslog(0, LOG, "no ndb/auth");
404 syslog(0, LOG, "no ndb/local");
405 db = ndbcat(db, db2);
406 if((dfd = accept(lcfd, ldir)) < 0)
407 exits("can't accept");
408 alarm(30*60*1000); /* 30 min */
409 remote = remoteIP(ldir);
410 syslog(0, LOG, "secstore from %s", remote);
412 dologin(dfd, S, forceSTA);