1 /*
2 * The caller supplies the device, we do the flavoring. There
3 * are no phases, everything happens in the init routine.
4 */
5
6 #include "dat.h"
7
8 typedef struct State State;
9 struct State
10 {
11 Key *key;
12 };
13
14 enum
15 {
16 HavePass,
17 };
18
19 static int
wepinit(Proto *,Fsstate * fss)20 wepinit(Proto*, Fsstate *fss)
21 {
22 int ret;
23 Key *k;
24 Keyinfo ki;
25 State *s;
26
27 /* find a key with at least one password */
28 mkkeyinfo(&ki, fss, nil);
29 ret = findkey(&k, &ki, "!key1?");
30 if(ret != RpcOk)
31 ret = findkey(&k, &ki, "!key2?");
32 if(ret != RpcOk)
33 ret = findkey(&k, &ki, "!key3?");
34 if(ret != RpcOk)
35 return ret;
36
37 setattrs(fss->attr, k->attr);
38 s = emalloc(sizeof(*s));
39 s->key = k;
40 fss->ps = s;
41 fss->phase = HavePass;
42
43 return RpcOk;
44 }
45
46 static void
wepclose(Fsstate * fss)47 wepclose(Fsstate *fss)
48 {
49 State *s;
50
51 s = fss->ps;
52 if(s->key)
53 closekey(s->key);
54 free(s);
55 }
56
57 static int
wepread(Fsstate * fss,void *,uint *)58 wepread(Fsstate *fss, void*, uint*)
59 {
60 return phaseerror(fss, "read");
61 }
62
63 static int
wepwrite(Fsstate * fss,void * va,uint n)64 wepwrite(Fsstate *fss, void *va, uint n)
65 {
66 char *data = va;
67 State *s;
68 char dev[64];
69 int fd, cfd;
70 int rv;
71 char *p;
72
73 /* get the device */
74 if(n > sizeof(dev)-5){
75 werrstr("device too long");
76 return RpcErrstr;
77 }
78 memmove(dev, data, n);
79 dev[n] = 0;
80 s = fss->ps;
81
82 /* legal? */
83 if(dev[0] != '#' || dev[1] != 'l'){
84 werrstr("%s not an ether device", dev);
85 return RpcErrstr;
86 }
87 strcat(dev, "!0");
88 fd = dial(dev, 0, 0, &cfd);
89 if(fd < 0)
90 return RpcErrstr;
91
92 /* flavor it with passwords, essid, and turn on wep */
93 rv = RpcErrstr;
94 p = _strfindattr(s->key->privattr, "!key1");
95 if(p != nil)
96 if(fprint(cfd, "key1 %s", p) < 0)
97 goto out;
98 p = _strfindattr(s->key->privattr, "!key2");
99 if(p != nil)
100 if(fprint(cfd, "key2 %s", p) < 0)
101 goto out;
102 p = _strfindattr(s->key->privattr, "!key3");
103 if(p != nil)
104 if(fprint(cfd, "key3 %s", p) < 0)
105 goto out;
106 p = _strfindattr(fss->attr, "essid");
107 if(p != nil)
108 if(fprint(cfd, "essid %s", p) < 0)
109 goto out;
110 if(fprint(cfd, "crypt on") < 0)
111 goto out;
112 rv = RpcOk;
113 out:
114 close(fd);
115 close(cfd);
116 return rv;
117 }
118
119 Proto wep =
120 {
121 .name= "wep",
122 .init= wepinit,
123 .write= wepwrite,
124 .read= wepread,
125 .close= wepclose,
126 .addkey= replacekey,
127 .keyprompt= "!key1? !key2? !key3? essid?",
128 };
129