plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

wep.c (1487B)


      1 /*
      2  * Copy WEP key to ethernet device.
      3  */
      4 
      5 #include "std.h"
      6 #include "dat.h"
      7 
      8 static int
      9 wepclient(Conv *c)
     10 {
     11 	char *dev, buf[128], *p, *kp;
     12 	Key *k;
     13 	int ret, fd, cfd;
     14 
     15 	fd = cfd = -1;
     16 	ret = -1;
     17 	dev = nil;
     18 
     19 	if((k = keylookup("%A !key1?", c->attr)) == nil
     20 	&& (k = keylookup("%A !key2?", c->attr)) == nil
     21 	&& (k = keylookup("%A !key3?", c->attr)) == nil){
     22 		werrstr("cannot find wep keys");
     23 		goto out;
     24 	}
     25 	if(convreadm(c, &dev) < 0)
     26 		return -1;
     27 	if(dev[0] != '#' || dev[1] != 'l'){
     28 		werrstr("not an ethernet device: %s", dev);
     29 		goto out;
     30 	}
     31 	snprint(buf, sizeof buf, "%s!0", dev);
     32 	if((fd = dial(buf, 0, 0, &cfd)) < 0)
     33 		goto out;
     34 	if(!(p = strfindattr(k->privattr, kp="!key1"))
     35 	&& !(p = strfindattr(k->privattr, kp="key2"))
     36 	&& !(p = strfindattr(k->privattr, kp="key3"))){
     37 		werrstr("lost key");
     38 		goto out;
     39 	}
     40 	if(fprint(cfd, "%s %q", kp+1, p) < 0)
     41 		goto out;
     42 	if((p = strfindattr(k->attr, "essid")) != nil
     43 	&& fprint(cfd, "essid %q", p) < 0)
     44 		goto out;
     45 	if(fprint(cfd, "crypt on") < 0)
     46 		goto out;
     47 	ret = 0;
     48 
     49 out:
     50 	free(dev);
     51 	if(cfd >= 0)
     52 		close(cfd);
     53 	if(fd >= 0)
     54 		close(fd);
     55 	keyclose(k);
     56 	return ret;
     57 }
     58 
     59 static int
     60 wepcheck(Key *k)
     61 {
     62 	if(strfindattr(k->privattr, "!key1") == nil
     63 	&& strfindattr(k->privattr, "!key2") == nil
     64 	&& strfindattr(k->privattr, "!key3") == nil){
     65 		werrstr("need !key1, !key2, or !key3 attribute");
     66 		return -1;
     67 	}
     68 	return 0;
     69 }
     70 
     71 static Role weproles[] = {
     72 	"client",	wepclient,
     73 	0
     74 };
     75 
     76 Proto wep =
     77 {
     78 	"wep",
     79 	weproles,
     80 	nil,
     81 	wepcheck,
     82 	nil
     83 };