Annotation of embedaddon/axTLS/httpd/htpasswd.c, revision 1.1
1.1 ! misho 1: /*
! 2: * Copyright (c) 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:
! 31: #include <stdio.h>
! 32: #include <string.h>
! 33: #include <stdlib.h>
! 34: #include "os_port.h"
! 35: #include "ssl.h"
! 36:
! 37: int tfd;
! 38:
! 39: void base64_encode(const uint8_t *in, size_t inlen, char *out, size_t outlen)
! 40: {
! 41: static const char b64str[64] =
! 42: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
! 43:
! 44: while (inlen && outlen)
! 45: {
! 46: *out++ = b64str[(in[0] >> 2) & 0x3f];
! 47: if (!--outlen)
! 48: break;
! 49:
! 50: *out++ = b64str[((in[0] << 4)
! 51: + (--inlen ? in[1] >> 4 : 0)) & 0x3f];
! 52: if (!--outlen)
! 53: break;
! 54: *out++ = (inlen
! 55: ? b64str[((in[1] << 2)
! 56: + (--inlen ? in[2] >> 6 : 0))
! 57: & 0x3f]
! 58: : '=');
! 59: if (!--outlen)
! 60: break;
! 61: *out++ = inlen ? b64str[in[2] & 0x3f] : '=';
! 62: if (!--outlen)
! 63: break;
! 64: if (inlen)
! 65: inlen--;
! 66: if (inlen)
! 67: in += 3;
! 68: }
! 69:
! 70: if (outlen)
! 71: *out = '\0';
! 72: }
! 73:
! 74: static void usage(void)
! 75: {
! 76: fprintf(stderr,"Usage: htpasswd username\n");
! 77: exit(1);
! 78: }
! 79:
! 80: #ifdef WIN32
! 81: static char * getpass(const char *prompt)
! 82: {
! 83: static char buf[127];
! 84: FILE *fp = stdin;
! 85:
! 86: printf(prompt); TTY_FLUSH();
! 87: #if 0
! 88: fp = fopen("/dev/tty", "w");
! 89: if (fp == NULL)
! 90: {
! 91: printf("null\n"); TTY_FLUSH();
! 92: fp = stdin;
! 93: }
! 94: #endif
! 95:
! 96: fgets(buf, sizeof(buf), fp);
! 97: while (buf[strlen(buf)-1] < ' ')
! 98: buf[strlen(buf)-1] = '\0';
! 99:
! 100: //if (fp != stdin)
! 101: // fclose(fp);
! 102: return buf;
! 103: }
! 104: #endif
! 105:
! 106: int main(int argc, char *argv[])
! 107: {
! 108: char* pw;
! 109: uint8_t md5_salt[MD5_SIZE], md5_pass[MD5_SIZE];
! 110: char b64_salt[MD5_SIZE+10], b64_pass[MD5_SIZE+10];
! 111: MD5_CTX ctx;
! 112:
! 113: if (argc != 2)
! 114: usage();
! 115:
! 116: pw = strdup(getpass("New password:"));
! 117: if (strcmp(pw, getpass("Re-type new password:")) != 0)
! 118: {
! 119: fprintf(stderr, "They don't match, sorry.\n" );
! 120: exit(1);
! 121: }
! 122:
! 123: RNG_initialize();
! 124: get_random(MD5_SIZE, md5_salt);
! 125: RNG_terminate();
! 126: base64_encode(md5_salt, MD5_SIZE, b64_salt, sizeof(b64_salt));
! 127:
! 128: MD5_Init(&ctx);
! 129: MD5_Update(&ctx, md5_salt, MD5_SIZE);
! 130: MD5_Update(&ctx, (uint8_t *)pw, strlen(pw));
! 131: MD5_Final(md5_pass, &ctx);
! 132: base64_encode(md5_pass, MD5_SIZE, b64_pass, sizeof(b64_pass));
! 133:
! 134: printf("Add the following to your '.htpasswd' file\n");
! 135: printf("%s:%s$%s\n", argv[1], b64_salt, b64_pass);
! 136: return 0;
! 137: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>