Blob


1 /*
2 * This is just a repository for a password.
3 * We don't want to encourage this, there's
4 * no server side.
5 */
7 #include "dat.h"
9 typedef struct State State;
10 struct State
11 {
12 Key *key;
13 };
15 enum
16 {
17 HavePass,
18 Maxphase,
19 };
21 static char *phasenames[Maxphase] =
22 {
23 [HavePass] "HavePass",
24 };
26 static int
27 passinit(Proto *p, Fsstate *fss)
28 {
29 int ask;
30 Key *k;
31 State *s;
33 k = findkey(fss, Kuser, &ask, 0, fss->attr, "%s", p->keyprompt);
34 if(k == nil){
35 if(ask)
36 return RpcNeedkey;
37 return failure(fss, nil);
38 }
39 setattrs(fss->attr, k->attr);
40 s = emalloc(sizeof(*s));
41 s->key = k;
42 fss->ps = s;
43 return RpcOk;
44 }
46 static void
47 passclose(Fsstate *fss)
48 {
49 State *s;
51 s = fss->ps;
52 if(s->key)
53 closekey(s->key);
54 free(s);
55 }
57 static int
58 passread(Fsstate *fss, void *va, uint *n)
59 {
60 int m;
61 char buf[500];
62 char *pass, *user;
63 State *s;
65 s = fss->ps;
66 switch(fss->phase){
67 default:
68 return phaseerror(fss, "read");
70 case HavePass:
71 user = strfindattr(s->key->attr, "user");
72 pass = strfindattr(s->key->privattr, "!password");
73 if(user==nil || pass==nil)
74 return failure(fss, "passread cannot happen");
75 snprint(buf, sizeof buf, "%q %q", user, pass);
76 m = strlen(buf);
77 if(m > *n)
78 return toosmall(fss, m);
79 *n = m;
80 memmove(va, buf, m);
81 return RpcOk;
82 }
83 }
85 static int
86 passwrite(Fsstate *fss, void*, uint)
87 {
88 return phaseerror(fss, "write");
89 }
91 Proto pass =
92 {
93 .name= "pass",
94 .init= passinit,
95 .write= passwrite,
96 .read= passread,
97 .close= passclose,
98 .addkey= replacekey,
99 .keyprompt= "user? !password?",
100 };