/* Package Name : twhttpd * File Name : htpasswd.c * Author : Sam NG * All rights reserved. * * This package is an secure HTTP application proxy writen by Sam Ng. * The software is free for commercial and non-commercial use as long as * the following conditions are aheared to. * * Copyright remains Sam NG's, and as such any Copyright notices in * the code are not to be removed. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY SAM NG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence * [including the GNU Public Licence.] */ #include #include #include #include #include #include #define MAX_PASS 32 /* generate DES password */ char *encrypt_passwd(char *passwd) { int i; char salt[3] = {'0', '1', '\0' } ; char *ascii_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./" ; char *encrypt; srand(time(0)); // generate salt i = (int) (64.0*rand()/(RAND_MAX+1.0)); salt[0] = ascii_table[i]; i = (int) (64.0*rand()/(RAND_MAX+1.0)); salt[1] = ascii_table[i]; encrypt = (char *) crypt(passwd, salt); return (NULL == encrypt) ? "!" : encrypt; } /* * check if the string is in ASCII char only * range */ int non_ascii(char *fname) { char *ptr; ptr = fname; while ( '\0' != *ptr ) { // allowed char set is // between 0d32 (space) to 0d126 (~) if ( *ptr < ' ' || *ptr > '~' ) return 1; ptr++; } return 0; } int main(int argc, char *argv[]) { int i=0; char *user, *pass; char pass1[MAX_PASS], pass2[MAX_PASS]; char field1[MAX_PASS], field2[MAX_PASS]; char format_string[64]; char *fname; FILE *fp; if ( argc < 3 ) { fprintf(stderr, "HTTP password generator\n"); fprintf(stderr, "Usage: %s htpasswd_file user_name\n", argv[0]); return 0; } fname = argv[1]; user = argv[2]; /* check username */ for ( i=0; i= 3 ) { fprintf(stderr, "Too many errors\n"); return 0; } if ( 0 != i ) fprintf(stderr, "Passwords do not match\n"); i++; strcpy(pass1, getpass("Input Password: ")); if ( non_ascii(pass1) ) { fprintf(stderr, "Password can be ascii characters 32-127 only\n"); continue; } strcpy(pass2, getpass("Confirm Password: ")); } while ( strcmp(pass1, pass2) ); pass = encrypt_passwd(pass1); fprintf(fp, "%s:%s\n", user, pass); return 0; }