mac2pw.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. #define _GETOPT_H
  42. /*====================================================================*
  43. * system header files;
  44. *--------------------------------------------------------------------*/
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include <limits.h>
  48. #include <errno.h>
  49. /*====================================================================*
  50. * custom header files;
  51. *--------------------------------------------------------------------*/
  52. #include "../tools/getoptv.h"
  53. #include "../tools/putoptv.h"
  54. #include "../tools/memory.h"
  55. #include "../tools/number.h"
  56. #include "../tools/types.h"
  57. #include "../tools/flags.h"
  58. #include "../tools/error.h"
  59. #include "../key/keys.h"
  60. /*====================================================================*
  61. * custom source files;
  62. *--------------------------------------------------------------------*/
  63. #ifndef MAKEFILE
  64. #include "../tools/getoptv.c"
  65. #include "../tools/putoptv.c"
  66. #include "../tools/version.c"
  67. #include "../tools/todigit.c"
  68. #include "../tools/uintspec.c"
  69. #include "../tools/error.c"
  70. #endif
  71. #ifndef MAKEFILE
  72. #include "../key/MACPasswords.c"
  73. #include "../key/RNDPasswords.c"
  74. #include "../key/putpwd.c"
  75. #endif
  76. /*====================================================================*
  77. * program constants;
  78. *--------------------------------------------------------------------*/
  79. #ifndef ETHER_ADDR_LEN
  80. #define ETHER_ADDR_LEN 6
  81. #endif
  82. /*====================================================================*
  83. * program functions;
  84. *--------------------------------------------------------------------*/
  85. void (* generate)(unsigned, unsigned, unsigned, unsigned, unsigned, char, flag_t) = RNDPasswords;
  86. /*====================================================================*
  87. *
  88. * void function (const char * string, unsigned range, unsigned alpha, unsigned bunch, unsigned space, flag_t flags)
  89. *
  90. * parse an Ethernet hardware address string into vendor and device
  91. * ID substrings; print a specified number of consecutive addresses
  92. * and password strings having a defined letter count and grouping;
  93. *
  94. * Contributor(s):
  95. * Charles Maier
  96. *
  97. *--------------------------------------------------------------------*/
  98. static void function (const char * string, unsigned range, unsigned alpha, unsigned bunch, unsigned space, flag_t flags)
  99. {
  100. extern void (* generate)(unsigned, unsigned, unsigned, unsigned, unsigned, char, flag_t);
  101. const char * offset = string;
  102. unsigned vendor = 0;
  103. unsigned device = 0;
  104. unsigned radix = 0x10;
  105. unsigned width;
  106. unsigned digit;
  107. for (width = 0; width < ETHER_ADDR_LEN; width++)
  108. {
  109. if ((digit = todigit (*offset)) < radix)
  110. {
  111. vendor *= radix;
  112. vendor += digit;
  113. offset++;
  114. continue;
  115. }
  116. error (1, EINVAL, "Bad MAC Address: %s", string);
  117. }
  118. if (!vendor)
  119. {
  120. error (1, EPERM, "Vendor ID can't be zero");
  121. }
  122. for (width = 0; width < ETHER_ADDR_LEN; width++)
  123. {
  124. if ((digit = todigit (*offset)) < radix)
  125. {
  126. device *= radix;
  127. device += digit;
  128. offset++;
  129. continue;
  130. }
  131. error (1, EINVAL, "Bad MAC Address: %s", string);
  132. }
  133. if (!device)
  134. {
  135. error (1, EPERM, "Device ID can't be zero");
  136. }
  137. if (*offset)
  138. {
  139. error (1, EINVAL, "Bad MAC address: %s", string);
  140. }
  141. if (range > (0x00FFFFFF - device))
  142. {
  143. error (1, ERANGE, "Want %d passwords but only %d left in range", range, (0x00FFFFFF - device));
  144. }
  145. generate (vendor, device, range, alpha, bunch, space, flags);
  146. return;
  147. }
  148. /*====================================================================*
  149. *
  150. * int main (int argc, const char * argv []);
  151. *
  152. * generate unique password strings for a range of device hardware
  153. * addresses; print paired addresses and passwords on stdout;
  154. *
  155. * Many Atheros programs expect the user to enter a password to
  156. * access a device; the password is encoded to produce the 16-bit
  157. * Device Access Key (DAK) stored in the PIB;
  158. *
  159. * Vendors must publish the device password so that end users can
  160. * reproduce the same 16-byte hexadecimal value later; a password
  161. * is more user-friendly than a 16-byte hexadecimal value;
  162. *
  163. * given a range of MAC address, this program will produce unique
  164. * passwords so vendors can program devices and print labels that
  165. * ship devices;
  166. *
  167. *
  168. * Contributor(s):
  169. * Charles Maier
  170. *
  171. *--------------------------------------------------------------------*/
  172. #define DEFAULT_RANGE 1
  173. #define DEFAULT_ALPHA 25
  174. #define DEFAULT_BUNCH 0
  175. int main (int argc, const char * argv [])
  176. {
  177. extern void (* generate)(unsigned, unsigned, unsigned, unsigned, unsigned, char, flag_t);
  178. static const char * optv [] =
  179. {
  180. "b:el:mn:qv",
  181. "address [address] [...]",
  182. "Atheros device password generator",
  183. "b n\tbunching factor [" LITERAL (DEFAULT_BUNCH) "]",
  184. "e\tbase passwords on host system entropy (more secure)",
  185. "l n\tpassword letters [" LITERAL (DEFAULT_ALPHA) "]",
  186. "m\tbase passwords on MAC addresses (non-secure)",
  187. "n n\tgenerate n consecutive passwords [" LITERAL (DEFAULT_RANGE) "]",
  188. "q\tomit device address on output",
  189. "v\tprepend PTS flag on output",
  190. (const char *)(0)
  191. };
  192. unsigned range = DEFAULT_RANGE;
  193. unsigned alpha = DEFAULT_ALPHA;
  194. unsigned bunch = DEFAULT_BUNCH;
  195. unsigned space = '-';
  196. flag_t flags = (flag_t)(0);
  197. signed c;
  198. optind = 1;
  199. while ((c = getoptv (argc, argv, optv)) != -1)
  200. {
  201. switch ((char)(c))
  202. {
  203. case 'b':
  204. bunch = uintspec (optarg, 0, UCHAR_MAX);
  205. break;
  206. case 'e':
  207. generate = RNDPasswords;
  208. break;
  209. case 'l':
  210. alpha = uintspec (optarg, 12, 64);
  211. break;
  212. case 'm':
  213. generate = MACPasswords;
  214. break;
  215. case 'n':
  216. range = uintspec (optarg, 0, 0x00FFFFFF);
  217. break;
  218. case 'q':
  219. _setbits (flags, PASSWORD_SILENCE);
  220. break;
  221. case 'v':
  222. _setbits (flags, PASSWORD_VERBOSE);
  223. break;
  224. default:
  225. break;
  226. }
  227. }
  228. argc -= optind;
  229. argv += optind;
  230. while ((argv) && (* argv))
  231. {
  232. function (* argv, range, alpha, bunch, space, flags);
  233. argc--;
  234. argv++;
  235. }
  236. return (0);
  237. }