mac2pw.c.html 9.2 KB

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