]>
git.ipfire.org Git - thirdparty/squid.git/blob - helpers/basic_auth/NCSA/basic_ncsa_auth.cc
4 * AUTHOR: Arjan de Vet <Arjan.deVet@adv.iae.nl>
6 * Example authentication program for Squid, based on the original
7 * proxy_auth code from client_side.c, written by
8 * Jon Thackray <jrmt@uk.gdscorp.com>.
10 * Uses a NCSA httpd style password file for authentication with the
11 * following improvements suggested by various people:
13 * - comment lines are possible and should start with a '#';
14 * - empty or blank lines are possible;
15 * - extra fields in the password file are ignored; this makes it
16 * possible to use a Unix password file but I do not recommend that.
36 #include <sys/types.h>
47 #include "crypt_md5.h"
49 static hash_table
* hash
= NULL
;
50 static HASHFREE my_free
;
52 typedef struct _user_data
{
53 /* first two items must be same as hash_link */
55 struct _user_data
* next
;
62 user_data
* u
= static_cast < user_data
*>( p
);
69 read_passwd_file ( const char * passwdfile
)
77 hashFreeItems ( hash
, my_free
);
81 hash
= hash_create (( HASHCMP
*) strcmp
, 7921 , hash_string
);
83 fprintf ( stderr
, "ncsa_auth: cannot create hash table \n " );
86 f
= fopen ( passwdfile
, "r" );
88 fprintf ( stderr
, "%s: %s \n " , passwdfile
, xstrerror ());
91 while ( fgets ( buf
, 8192 , f
) != NULL
) {
92 if (( buf
[ 0 ] == '#' ) || ( buf
[ 0 ] == ' ' ) || ( buf
[ 0 ] == ' \t ' ) ||
95 user
= strtok ( buf
, ": \n\r " );
96 passwd
= strtok ( NULL
, ": \n\r " );
97 if (( strlen ( user
) > 0 ) && passwd
) {
98 u
= static_cast < user_data
*>( xmalloc ( sizeof (* u
)));
99 u
-> user
= xstrdup ( user
);
100 u
-> passwd
= xstrdup ( passwd
);
101 hash_join ( hash
, ( hash_link
*) u
);
108 main ( int argc
, char ** argv
)
111 time_t change_time
= - 1 ;
113 char * user
, * passwd
, * p
;
115 setbuf ( stdout
, NULL
);
117 fprintf ( stderr
, "Usage: ncsa_auth <passwordfile> \n " );
120 if ( stat ( argv
[ 1 ], & sb
) != 0 ) {
121 fprintf ( stderr
, "cannot stat %s \n " , argv
[ 1 ]);
124 while ( fgets ( buf
, 256 , stdin
) != NULL
) {
125 if (( p
= strchr ( buf
, ' \n ' )) != NULL
)
126 * p
= '\0' ; /* strip \n */
127 if ( stat ( argv
[ 1 ], & sb
) == 0 ) {
128 if ( sb
. st_mtime
!= change_time
) {
129 read_passwd_file ( argv
[ 1 ]);
130 change_time
= sb
. st_mtime
;
133 if (( user
= strtok ( buf
, " " )) == NULL
) {
137 if (( passwd
= strtok ( NULL
, "" )) == NULL
) {
141 rfc1738_unescape ( user
);
142 rfc1738_unescape ( passwd
);
143 u
= ( user_data
*) hash_lookup ( hash
, user
);
145 printf ( "ERR No such user \n " );
147 } else if ( strcmp ( u
-> passwd
, ( char *) crypt ( passwd
, u
-> passwd
)) == 0 ) {
150 } else if ( strcmp ( u
-> passwd
, ( char *) crypt_md5 ( passwd
, u
-> passwd
)) == 0 ) {
152 } else if ( strcmp ( u
-> passwd
, ( char *) md5sum ( passwd
)) == 0 ) { /* md5 without salt and magic strings - Added by Ramon de Carvalho and Rodrigo Rubira Branco */
155 printf ( "ERR Wrong password \n " );
159 hashFreeItems ( hash
, my_free
);
160 hashFreeMemory ( hash
);