mac2pwd.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 <ctype.h>
  47. #include <errno.h>
  48. #include <limits.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 * file, unsigned alpha, unsigned bunch, flag_t flags)
  89. *
  90. * read Ethernet hardware address strings from a file and print
  91. * address passwords pairs on stdout;
  92. *
  93. * parse an Ethernet hardware address string into vendor and device
  94. * ID substrings; print a specified number of consecutive addresses
  95. * and password strings having a defined letter count and grouping;
  96. *
  97. * Contributor(s):
  98. * Charles Maier
  99. *
  100. *--------------------------------------------------------------------*/
  101. static void function (const char * file, unsigned alpha, unsigned bunch, unsigned space, flag_t flags)
  102. {
  103. extern void (* generate)(unsigned, unsigned, unsigned, unsigned, unsigned, char, flag_t);
  104. unsigned line = 1;
  105. unsigned radix = 0x10;
  106. unsigned width;
  107. unsigned digit;
  108. signed c = getc (stdin);
  109. while (c != EOF)
  110. {
  111. uint32_t vendor = 0;
  112. uint32_t device = 0;
  113. while (isspace (c))
  114. {
  115. if (c == '\n')
  116. {
  117. line++;
  118. }
  119. c = getc (stdin);
  120. }
  121. if ((c == '#') || (c == ';'))
  122. {
  123. do
  124. {
  125. c = getc (stdin);
  126. }
  127. while ((c != '\n') && (c != EOF));
  128. continue;
  129. }
  130. for (width = 0; width < ETHER_ADDR_LEN; width++)
  131. {
  132. if ((digit = todigit (c)) < radix)
  133. {
  134. vendor *= radix;
  135. vendor += digit;
  136. c = getc (stdin);
  137. continue;
  138. }
  139. error (1, EINVAL, "%s: line %d: Illegal vendor", file, line);
  140. }
  141. if (!vendor)
  142. {
  143. error (1, EPERM, "%s: line %d: Vendor can't be zero", file, line);
  144. }
  145. for (width = 0; width < ETHER_ADDR_LEN; width++)
  146. {
  147. if ((digit = todigit (c)) < radix)
  148. {
  149. device *= radix;
  150. device += digit;
  151. c = getc (stdin);
  152. continue;
  153. }
  154. error (1, EINVAL, "%s: line %d: Illegal device", file, line);
  155. }
  156. if (!device)
  157. {
  158. error (1, EPERM, "%s: line %d: Device can't be zero", file, line);
  159. }
  160. while (isspace (c))
  161. {
  162. if (c == '\n')
  163. {
  164. line++;
  165. }
  166. c = getc (stdin);
  167. }
  168. generate (vendor, device, 1, alpha, bunch, space, flags);
  169. }
  170. return;
  171. }
  172. /*====================================================================*
  173. *
  174. * int main (int argc, const char * argv []);
  175. *
  176. * read one or more text files containing device address strings
  177. * and print a stream of address/password pairs; device addresses
  178. * must be separated by white space;
  179. *
  180. * Contributor(s):
  181. * Charles Maier
  182. *
  183. *--------------------------------------------------------------------*/
  184. #define DEFAULT_ALPHA 25
  185. #define DEFAULT_BUNCH 0
  186. int main (int argc, const char * argv [])
  187. {
  188. extern void (* generate)(unsigned, unsigned, unsigned, unsigned, unsigned, char, flag_t);
  189. static const char * optv [] =
  190. {
  191. "b:el:mqv",
  192. PUTOPTV_S_FUNNEL,
  193. "Atheros device password generator",
  194. "b n\tbunching factor [" LITERAL (DEFAULT_BUNCH) "]",
  195. "e\tbase passwords on host system entropy (more secure)",
  196. "l n\tpassword letters [" LITERAL (DEFAULT_ALPHA) "]",
  197. "m\tbase passwords on MAC addresses (non-secure)",
  198. "q\tomit device address on output",
  199. "v\tprepend PTS flag on output",
  200. (const char *)(0)
  201. };
  202. unsigned alpha = DEFAULT_ALPHA;
  203. unsigned bunch = DEFAULT_BUNCH;
  204. unsigned space = '-';
  205. flag_t flags = (flag_t)(0);
  206. signed c;
  207. optind = 1;
  208. while ((c = getoptv (argc, argv, optv)) != -1)
  209. {
  210. switch ((char)(c))
  211. {
  212. case 'b':
  213. bunch = uintspec (optarg, 0, UCHAR_MAX);
  214. break;
  215. case 'e':
  216. generate = RNDPasswords;
  217. break;
  218. case 'l':
  219. alpha = uintspec (optarg, 12, 64);
  220. break;
  221. case 'm':
  222. generate = MACPasswords;
  223. break;
  224. case 'q':
  225. _setbits (flags, PASSWORD_SILENCE);
  226. break;
  227. case 'v':
  228. _setbits (flags, PASSWORD_VERBOSE);
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. argc -= optind;
  235. argv += optind;
  236. if (!argc)
  237. {
  238. function ("stdin", alpha, bunch, space, flags);
  239. }
  240. while ((argv) && (* argv))
  241. {
  242. if (!freopen (* argv, "rb", stdin))
  243. {
  244. error (1, EINVAL, "Can't open %s", * argv);
  245. }
  246. function (* argv, alpha, bunch, space, flags);
  247. argc--;
  248. argv++;
  249. }
  250. return (0);
  251. }