wep.c 2.5 KB

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