strnpwd.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2015 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. /*====================================================================*
  42. *
  43. * char * strnpwd (char buffer [], unsigned length, unsigned count, unsigned group, char space);
  44. *
  45. * keys.h
  46. *
  47. * encode a buffer with a password containing the specified number
  48. * of random letters and digits; optionally, group the letters and
  49. * digits and separate groups with a space character; return the
  50. * address of the byte following the encoded password;
  51. *
  52. * alphabet is an array of 32 printable password characters;
  53. * count is the number of characters to be selected from alphabet;
  54. * group is the grouping factor;
  55. * space is the group separator;
  56. *
  57. * grouping is suppressed when group is zero or greater than or
  58. * equal to count;
  59. *
  60. * Contributors:
  61. * Charles Maier
  62. *
  63. *--------------------------------------------------------------------*/
  64. #ifndef STRNPWD_SOURCE
  65. #define STRNPWD_SOURCE
  66. #include <unistd.h>
  67. #include <fcntl.h>
  68. #include <errno.h>
  69. #include "../tools/types.h"
  70. #include "../tools/error.h"
  71. #include "../key/keys.h"
  72. char * strnpwd (char buffer [], unsigned length, unsigned count, unsigned group, char space)
  73. {
  74. signed fd;
  75. if ((fd = open ("/dev/urandom", O_RDONLY)) == -1)
  76. {
  77. error (1, errno, "can't open /dev/urandom");
  78. }
  79. while (count--)
  80. {
  81. static const char alphabet [] =
  82. {
  83. '2',
  84. '3',
  85. '4',
  86. '5',
  87. '6',
  88. '7',
  89. '8',
  90. '9',
  91. 'A',
  92. 'B',
  93. 'C',
  94. 'D',
  95. 'E',
  96. 'F',
  97. 'G',
  98. 'H',
  99. 'J',
  100. 'K',
  101. 'L',
  102. 'M',
  103. 'N',
  104. 'P',
  105. 'Q',
  106. 'R',
  107. 'S',
  108. 'T',
  109. 'U',
  110. 'V',
  111. 'W',
  112. 'X',
  113. 'Y',
  114. 'Z'
  115. };
  116. unsigned member;
  117. if (read (fd, & member, sizeof (member)) != sizeof (member))
  118. {
  119. error (1, errno, "can't read /dev/urandom");
  120. }
  121. if (length)
  122. {
  123. *buffer = alphabet [member % sizeof (alphabet)];
  124. buffer++;
  125. length--;
  126. }
  127. if ((count) && (group) && ! (count % group))
  128. {
  129. if (length)
  130. {
  131. *buffer = space;
  132. buffer++;
  133. length--;
  134. }
  135. }
  136. }
  137. close (fd);
  138. return (buffer);
  139. }
  140. #endif