]> git.ipfire.org Git - thirdparty/squid.git/blob - helpers/basic_auth/SASL/sasl_auth.c
Cleanup: zap CVS Id tags
[thirdparty/squid.git] / helpers / basic_auth / SASL / sasl_auth.c
1 /*
2 * $Id$
3 *
4 * SASL authenticator module for Squid.
5 * Copyright (C) 2002 Ian Castle <ian.castle@coldcomfortfarm.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20 *
21 * Install instructions:
22 *
23 * This program authenticates users against using cyrus-sasl
24 *
25 * Compile this program with: gcc -Wall -o sasl_auth sasl_auth.c -lsasl
26 * or with SASL2: gcc -Wall -o sasl_auth sasl_auth.c -lsasl2
27 *
28 */
29 #include <stdio.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "util.h"
35
36 #ifdef HAVE_SASL_SASL_H
37 #include <sasl/sasl.h>
38 #else
39 #include <sasl.h>
40 #endif
41
42 #define APP_NAME_SASL "squid_sasl_auth"
43
44 int
45 main(int argc, char *argv[])
46 {
47 char line[8192];
48 char *username, *password;
49 #if SASL_VERSION_MAJOR < 2
50 const char *errstr;
51 #endif
52
53 int rc;
54 sasl_conn_t *conn = NULL;
55
56 /* make standard output line buffered */
57 setvbuf(stdout, NULL, _IOLBF, 0);
58
59 rc = sasl_server_init( NULL, APP_NAME_SASL );
60
61 if ( rc != SASL_OK ) {
62 fprintf( stderr, "error %d %s\n", rc, sasl_errstring(rc, NULL, NULL ));
63 fprintf( stdout, "ERR\n" );
64 return 1;
65 }
66
67 #if SASL_VERSION_MAJOR < 2
68 rc = sasl_server_new( APP_NAME_SASL, NULL, NULL, NULL, 0, &conn );
69 #else
70 rc = sasl_server_new( APP_NAME_SASL, NULL, NULL, NULL, NULL, NULL, 0, &conn );
71 #endif
72
73 if ( rc != SASL_OK ) {
74 fprintf( stderr, "error %d %s\n", rc, sasl_errstring(rc, NULL, NULL ));
75 fprintf( stdout, "ERR\n" );
76 return 1;
77 }
78
79 while ( fgets( line, sizeof( line ), stdin )) {
80 username = &line[0];
81 password = strchr( line, '\n' );
82 if ( !password) {
83 fprintf( stderr, "authenticator: Unexpected input '%s'\n", line );
84 fprintf( stdout, "ERR\n" );
85 continue;
86 }
87 *password = '\0';
88 password = strchr ( line, ' ' );
89 if ( !password) {
90 fprintf( stderr, "authenticator: Unexpected input '%s'\n", line );
91 fprintf( stdout, "ERR\n" );
92 continue;
93 }
94 *password++ = '\0';
95
96 rfc1738_unescape(username);
97 rfc1738_unescape(password);
98
99 #if SASL_VERSION_MAJOR < 2
100 rc = sasl_checkpass(conn, username, strlen(username), password, strlen(password), &errstr);
101 #else
102 rc = sasl_checkpass(conn, username, strlen(username), password, strlen(password));
103 #endif
104
105 if ( rc != SASL_OK ) {
106 #if SASL_VERSION_MAJOR < 2
107 if ( errstr ) {
108 fprintf( stderr, "errstr %s\n", errstr );
109 }
110 if ( rc != SASL_BADAUTH ) {
111 fprintf( stderr, "error %d %s\n", rc, sasl_errstring(rc, NULL, NULL ));
112 }
113 #endif
114 fprintf( stdout, "ERR\n" );
115 } else {
116 fprintf( stdout, "OK\n" );
117 }
118
119 }
120
121 sasl_dispose( &conn );
122 sasl_done();
123
124 return 0;
125 }