]>
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.
35 #include <sys/types.h>
46 #include "crypt_md5.h"
48 static hash_table
* hash
= NULL
;
49 static HASHFREE my_free
;
51 typedef struct _user_data
{
52 /* first two items must be same as hash_link */
54 struct _user_data
* next
;
61 user_data
* u
= static_cast < user_data
*>( p
);
68 read_passwd_file ( const char * passwdfile
)
76 hashFreeItems ( hash
, my_free
);
80 hash
= hash_create (( HASHCMP
*) strcmp
, 7921 , hash_string
);
82 fprintf ( stderr
, "ncsa_auth: cannot create hash table \n " );
85 f
= fopen ( passwdfile
, "r" );
87 fprintf ( stderr
, "%s: %s \n " , passwdfile
, xstrerror ());
90 while ( fgets ( buf
, 8192 , f
) != NULL
) {
91 if (( buf
[ 0 ] == '#' ) || ( buf
[ 0 ] == ' ' ) || ( buf
[ 0 ] == ' \t ' ) ||
94 user
= strtok ( buf
, ": \n\r " );
95 passwd
= strtok ( NULL
, ": \n\r " );
96 if (( strlen ( user
) > 0 ) && passwd
) {
97 u
= static_cast < user_data
*>( xmalloc ( sizeof (* u
)));
98 u
-> user
= xstrdup ( user
);
99 u
-> passwd
= xstrdup ( passwd
);
100 hash_join ( hash
, ( hash_link
*) u
);
107 main ( int argc
, char ** argv
)
110 time_t change_time
= - 1 ;
112 char * user
, * passwd
, * p
;
114 setbuf ( stdout
, NULL
);
116 fprintf ( stderr
, "Usage: ncsa_auth <passwordfile> \n " );
119 if ( stat ( argv
[ 1 ], & sb
) != 0 ) {
120 fprintf ( stderr
, "cannot stat %s \n " , argv
[ 1 ]);
123 while ( fgets ( buf
, 256 , stdin
) != NULL
) {
124 if (( p
= strchr ( buf
, ' \n ' )) != NULL
)
125 * p
= '\0' ; /* strip \n */
126 if ( stat ( argv
[ 1 ], & sb
) == 0 ) {
127 if ( sb
. st_mtime
!= change_time
) {
128 read_passwd_file ( argv
[ 1 ]);
129 change_time
= sb
. st_mtime
;
132 if (( user
= strtok ( buf
, " " )) == NULL
) {
136 if (( passwd
= strtok ( NULL
, "" )) == NULL
) {
140 rfc1738_unescape ( user
);
141 rfc1738_unescape ( passwd
);
142 u
= ( user_data
*) hash_lookup ( hash
, user
);
144 printf ( "ERR No such user \n " );
146 } else if ( strcmp ( u
-> passwd
, ( char *) crypt ( passwd
, u
-> passwd
)) == 0 ) {
149 } else if ( strcmp ( u
-> passwd
, ( char *) crypt_md5 ( passwd
, u
-> passwd
)) == 0 ) {
151 } else if ( strcmp ( u
-> passwd
, ( char *) md5sum ( passwd
)) == 0 ) { /* md5 without salt and magic strings - Added by Ramon de Carvalho and Rodrigo Rubira Branco */
154 printf ( "ERR Wrong password \n " );
158 hashFreeItems ( hash
, my_free
);
159 hashFreeMemory ( hash
);