1
0

htpasswd.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2007, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include "ssl.h"
  34. int tfd;
  35. void base64_encode(const uint8_t *in, size_t inlen, char *out, size_t outlen)
  36. {
  37. static const char b64str[64] =
  38. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  39. while (inlen && outlen)
  40. {
  41. *out++ = b64str[(in[0] >> 2) & 0x3f];
  42. if (!--outlen)
  43. break;
  44. *out++ = b64str[((in[0] << 4)
  45. + (--inlen ? in[1] >> 4 : 0)) & 0x3f];
  46. if (!--outlen)
  47. break;
  48. *out++ = (inlen
  49. ? b64str[((in[1] << 2)
  50. + (--inlen ? in[2] >> 6 : 0))
  51. & 0x3f]
  52. : '=');
  53. if (!--outlen)
  54. break;
  55. *out++ = inlen ? b64str[in[2] & 0x3f] : '=';
  56. if (!--outlen)
  57. break;
  58. if (inlen)
  59. inlen--;
  60. if (inlen)
  61. in += 3;
  62. }
  63. if (outlen)
  64. *out = '\0';
  65. }
  66. static void usage(void)
  67. {
  68. fprintf(stderr,"Usage: htpasswd username\n");
  69. exit(1);
  70. }
  71. #ifdef WIN32
  72. static char * getpass(const char *prompt)
  73. {
  74. static char buf[127];
  75. FILE *fp = stdin;
  76. printf(prompt); TTY_FLUSH();
  77. #if 0
  78. fp = fopen("/dev/tty", "w");
  79. if (fp == NULL)
  80. {
  81. printf("null\n"); TTY_FLUSH();
  82. fp = stdin;
  83. }
  84. #endif
  85. fgets(buf, sizeof(buf), fp);
  86. while (buf[strlen(buf)-1] < ' ')
  87. buf[strlen(buf)-1] = '\0';
  88. //if (fp != stdin)
  89. // fclose(fp);
  90. return buf;
  91. }
  92. #endif
  93. int main(int argc, char *argv[])
  94. {
  95. char* pw;
  96. uint8_t md5_salt[MD5_SIZE], md5_pass[MD5_SIZE];
  97. char b64_salt[MD5_SIZE+10], b64_pass[MD5_SIZE+10];
  98. MD5_CTX ctx;
  99. if (argc != 2)
  100. usage();
  101. pw = strdup(getpass("New password:"));
  102. if (strcmp(pw, getpass("Re-type new password:")) != 0)
  103. {
  104. fprintf(stderr, "They don't match, sorry.\n" );
  105. exit(1);
  106. }
  107. RNG_initialize((uint8_t *)pw, sizeof(pw));
  108. get_random(MD5_SIZE, md5_salt);
  109. RNG_terminate();
  110. base64_encode(md5_salt, MD5_SIZE, b64_salt, sizeof(b64_salt));
  111. MD5_Init(&ctx);
  112. MD5_Update(&ctx, md5_salt, MD5_SIZE);
  113. MD5_Update(&ctx, (uint8_t *)pw, strlen(pw));
  114. MD5_Final(md5_pass, &ctx);
  115. base64_encode(md5_pass, MD5_SIZE, b64_pass, sizeof(b64_pass));
  116. printf("Add the following to your '.htpasswd' file\n");
  117. printf("%s:%s$%s\n", argv[1], b64_salt, b64_pass);
  118. return 0;
  119. }