1 /* network login client */
9 enum{ CHK = 16, MAXFILES = 100 };
11 typedef struct AuthConn{
23 fprint(2, "usage: secstore [-cin] [-g getfile] [-p putfile] [-r rmfile] [-s tcp!server!5356] [-u user] [-v]\n");
28 getfile(SConn *conn, char *gf, uchar **buf, ulong *buflen, uchar *key, int nkey)
31 int i, n, nr, nw, len;
33 uchar skey[SHA1dlen], ib[Maxmsg+CHK], *ibr, *ibw, *bufw, *bufe;
38 fprint(2, "simple filenames, not paths like %s\n", gf);
41 memset(&aes, 0, sizeof aes);
43 snprint(s, Maxmsg, "GET %s\n", gf);
44 conn->write(conn, (uchar*)s, strlen(s));
49 if(readstr(conn, s) < 0){
50 fprint(2, "remote: %s\n", s);
55 fprint(2, "remote file %s does not exist\n", gf);
58 fprint(2, "implausible filesize for %s\n", gf);
61 fprint(2, "GET refused for %s\n", gf);
65 *buflen = len - AESbsize - CHK;
66 *buf = bufw = emalloc(len);
70 /* directory listing */
71 if(strcmp(gf,".")==0){
74 for(i=0; i < len; i += n){
75 if((n = conn->read(conn, (uchar*)s, Maxmsg)) <= 0){
76 fprint(2, "empty file chunk\n");
82 memmove((*buf)+i, s, n);
87 /* conn is already encrypted against wiretappers,
88 but gf is also encrypted against server breakin. */
89 if(buf == nil && (fd =create(gf, OWRITE, 0600)) < 0){
90 fprint(2, "can't open %s: %r\n", gf);
96 if((n = conn->read(conn, ibw, Maxmsg)) <= 0){
97 fprint(2, "empty file chunk n=%d nr=%d len=%d: %r\n", n, nr, len);
102 if(!aes.setup){ /* first time, read 16 byte IV */
104 fprint(2, "no IV in file\n");
107 sha = sha1((uchar*)"aescbc file", 11, nil, nil);
108 sha1(key, nkey, skey, sha);
109 setupAESstate(&aes, skey, AESbsize, ibr);
110 memset(skey, 0, sizeof skey);
114 aesCBCdecrypt(ibw-n, n, &aes);
118 nw = write(fd, ibr, n);
120 fprint(2, "write error on %s", gf);
124 assert(bufw+n <= bufe);
125 memmove(bufw, ibr, n);
130 memmove(ib, ibr, ibw-ibr);
131 ibw = ib + (ibw-ibr);
137 if((n != CHK) || (memcmp(ib, "XXXXXXXXXXXXXXXX", CHK) != 0)){
138 fprint(2,"decrypted file failed to authenticate!\n");
144 // This sends a file to the secstore disk that can, in an emergency, be
145 // decrypted by the program aescbc.c.
147 putfile(SConn *conn, char *pf, uchar *buf, ulong len, uchar *key, int nkey)
149 int i, n, fd, ivo, bufi, done;
151 uchar skey[SHA1dlen], b[CHK+Maxmsg], IV[AESbsize];
155 /* create initialization vector */
156 srand(time(0)); /* doesn't need to be unpredictable */
157 for(i=0; i<AESbsize; i++)
158 IV[i] = 0xff & rand();
159 sha = sha1((uchar*)"aescbc file", 11, nil, nil);
160 sha1(key, nkey, skey, sha);
161 setupAESstate(&aes, skey, AESbsize, IV);
162 memset(skey, 0, sizeof skey);
164 snprint(s, Maxmsg, "PUT %s\n", pf);
165 conn->write(conn, (uchar*)s, strlen(s));
169 if((fd = open(pf, OREAD)) < 0){
170 fprint(2, "can't open %s: %r\n", pf);
173 len = seek(fd, 0, 2);
178 if(len > MAXFILESIZE){
179 fprint(2, "implausible filesize %ld for %s\n", len, pf);
184 snprint(s, Maxmsg, "%ld", len+AESbsize+CHK);
185 conn->write(conn, (uchar*)s, strlen(s));
187 /* send IV and file+XXXXX in Maxmsg chunks */
191 for(done = 0; !done; ){
193 n = read(fd, b+ivo, Maxmsg-ivo);
195 fprint(2, "read error on %s: %r\n", pf);
199 if((n = len - bufi) > Maxmsg-ivo)
201 memcpy(b+ivo, buf+bufi, n);
206 if(n < Maxmsg){ /* EOF on input; append XX... */
207 memset(b+n, 'X', CHK);
208 n += CHK; // might push n>Maxmsg
211 aesCBCencrypt(b, n, &aes);
214 conn->write(conn, b, Maxmsg);
216 memmove(b, b+Maxmsg, n);
218 conn->write(conn, b, n);
223 fprint(2, "saved %ld bytes\n", len);
229 removefile(SConn *conn, char *rf)
234 fprint(2, "simple filenames, not paths like %s\n", rf);
238 snprint(buf, Maxmsg, "RM %s\n", rf);
239 conn->write(conn, (uchar*)buf, strlen(buf));
245 cmd(AuthConn *c, char **gf, int *Gflag, char **pf, char **rf)
249 uchar *memfile, *memcur, *memnext;
253 fprint(2, "get %s\n", *gf);
254 if(getfile(c->conn, *gf, *Gflag ? &memfile : nil, &len, (uchar*)c->pass, c->passlen) < 0)
257 // write one line at a time, as required by /mnt/factotum/ctl
260 memnext = (uchar*)strchr((char*)memcur, '\n');
262 write(1, memcur, memnext-memcur+1);
263 len -= memnext-memcur+1;
266 write(1, memcur, len);
277 fprint(2, "put %s\n", *pf);
278 if(putfile(c->conn, *pf, nil, 0, (uchar*)c->pass, c->passlen) < 0)
284 fprint(2, "rm %s\n", *rf);
285 if(removefile(c->conn, *rf) < 0)
290 c->conn->write(c->conn, (uchar*)"BYE", 3);
294 c->conn->free(c->conn);
299 chpasswd(AuthConn *c, char *id)
302 int rv = -1, newpasslen = 0;
305 char *newpass, *passck;
306 char *list, *cur, *next, *hexHi;
307 char *f[8], prompt[128];
311 // changing our password is vulnerable to connection failure
313 snprint(prompt, sizeof(prompt), "new password for %s: ", id);
314 newpass = getpassm(prompt);
317 if(strlen(newpass) >= 7)
319 else if(strlen(newpass) == 0){
320 fprint(2, "!password change aborted\n");
323 print("!password must be at least 7 characters\n");
325 newpasslen = strlen(newpass);
326 snprint(prompt, sizeof(prompt), "retype password: ");
327 passck = getpassm(prompt);
329 fprint(2, "getpassmwd failed\n");
332 if(strcmp(passck, newpass) != 0){
333 fprint(2, "passwords didn't match\n");
337 c->conn->write(c->conn, (uchar*)"CHPASS", strlen("CHPASS"));
338 hexHi = PAK_Hi(id, newpass, H, Hi);
339 c->conn->write(c->conn, (uchar*)hexHi, strlen(hexHi));
344 if(getfile(c->conn, ".", (uchar **)(void*)&list, &len, nil, 0) < 0){
345 fprint(2, "directory listing failed.\n");
349 /* Loop over files and reencrypt them; try to keep going after error */
350 for(cur=list; (next=strchr(cur, '\n')) != nil; cur=next+1){
352 if(tokenize(cur, f, nelem(f))< 1)
354 fprint(2, "reencrypting '%s'\n", f[0]);
355 if(getfile(c->conn, f[0], &memfile, &len, (uchar*)c->pass, c->passlen) < 0){
356 fprint(2, "getfile of '%s' failed\n", f[0]);
359 if(putfile(c->conn, f[0], memfile, len, (uchar*)newpass, newpasslen) < 0)
360 fprint(2, "putfile of '%s' failed\n", f[0]);
364 c->conn->write(c->conn, (uchar*)"BYE", 3);
369 memset(newpass, 0, newpasslen);
372 c->conn->free(c->conn);
377 login(char *id, char *dest, int pass_stdin, int pass_nvram)
381 char *S, *PINSTA = nil, *nl, s[Maxmsg+1], *pass;
384 fprint(2, "tried to login with nil dest\n");
387 c = emalloc(sizeof(*c));
389 /* if(readnvram(&nvr, 0) < 0) */
390 exits("readnvram: %r");
391 strecpy(c->pass, c->pass+sizeof c->pass, nvr.config);
394 n = readn(0, s, Maxmsg-2); // so len(PINSTA)<Maxmsg-3
396 exits("no password on standard input");
398 nl = strchr(s, '\n');
401 PINSTA = estrdup(nl);
402 nl = strchr(PINSTA, '\n');
406 strecpy(c->pass, c->pass+sizeof c->pass, s);
410 fprint(2, "dialing %s\n", dest);
411 if((fd = dial(dest, nil, nil, nil)) < 0){
412 fprint(2, "can't dial %s\n", dest);
416 if((c->conn = newSConn(fd)) == nil){
421 if(!pass_stdin && !pass_nvram){
422 pass = getpassm("secstore password: ");
423 if(strlen(pass) >= sizeof c->pass){
424 fprint(2, "password too long, skipping secstore login\n");
425 exits("password too long");
427 strcpy(c->pass, pass);
428 memset(pass, 0, strlen(pass));
432 fprint(2, "null password, skipping secstore login\n");
433 exits("no password");
435 if(PAKclient(c->conn, id, c->pass, &S) >= 0)
437 c->conn->free(c->conn);
439 exits("invalid password on standard input");
441 exits("invalid password in nvram");
442 // and let user try retyping the password
444 fprint(2, "Enter an empty password to quit.\n");
446 c->passlen = strlen(c->pass);
447 fprint(2, "%s\n", S);
449 if(readstr(c->conn, s) < 0){
450 c->conn->free(c->conn);
454 if(strcmp(s, "STA") == 0){
458 strncpy(s+3, PINSTA, (sizeof s)-3);
460 exits("missing PIN+SecureID on standard input");
463 pass = getpassm("STA PIN+SecureID: ");
464 strncpy(s+3, pass, (sizeof s)-4);
465 memset(pass, 0, strlen(pass));
470 fprint(2, "%ld\n", sn);
471 c->conn->write(c->conn, (uchar*)s, sn+3);
474 if(strcmp(s, "OK") != 0){
475 fprint(2, "%s\n", s);
476 c->conn->free(c->conn);
484 main(int argc, char **argv)
486 int chpass = 0, pass_stdin = 0, pass_nvram = 0, rc;
487 int ngfile = 0, npfile = 0, nrfile = 0, Gflag[MAXFILES+1];
488 char *gfile[MAXFILES], *pfile[MAXFILES], *rfile[MAXFILES];
489 char *serve, *tcpserve, *user;
494 memset(Gflag, 0, sizeof Gflag);
495 fmtinstall('B', mpfmt);
496 fmtinstall('H', encodefmt);
506 if(ngfile >= MAXFILES)
507 exits("too many gfiles");
508 gfile[ngfile++] = ARGF();
509 if(gfile[ngfile-1] == nil)
519 if(npfile >= MAXFILES)
520 exits("too many pfiles");
521 pfile[npfile++] = ARGF();
522 if(pfile[npfile-1] == nil)
526 if(nrfile >= MAXFILES)
527 exits("too many rfiles");
528 rfile[nrfile++] = ARGF();
529 if(rfile[nrfile-1] == nil)
533 serve = EARGF(usage());
536 user = EARGF(usage());
549 if(argc!=0 || user==nil)
552 if(chpass && (ngfile || npfile || nrfile)){
553 fprint(2, "Get, put, and remove invalid with password change.\n");
557 rc = strlen(serve)+sizeof("tcp!!99990");
558 tcpserve = emalloc(rc);
559 if(strchr(serve,'!'))
560 strcpy(tcpserve, serve);
562 snprint(tcpserve, rc, "tcp!%s!5356", serve);
563 c = login(user, tcpserve, pass_stdin, pass_nvram);
566 fprint(2, "secstore authentication failed\n");
567 exits("secstore authentication failed");
570 rc = chpasswd(c, user);
572 rc = cmd(c, gfile, Gflag, pfile, rfile);
574 fprint(2, "secstore cmd failed\n");
575 exits("secstore cmd failed");