2 * -----------------------------------------------------------------------------
4 * Author: Markus Moeller (markus_moeller at compuserve.com)
6 * Copyright (C) 2007 Markus Moeller. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 * -----------------------------------------------------------------------------
25 * Hosted at http://sourceforge.net/projects/squidkerbauth
54 #if HAVE_GSSAPI_GSSAPI_H
55 #include <gssapi/gssapi.h>
60 #if !HAVE_HEIMDAL_KERBEROS
61 #if HAVE_GSSAPI_GSSAPI_KRB5_H
62 #include <gssapi/gssapi_krb5.h>
64 #if HAVE_GSSAPI_GSSAPI_GENERIC_H
65 #include <gssapi/gssapi_generic.h>
67 #if HAVE_GSSAPI_GSSAPI_EXT_H
68 #include <gssapi/gssapi_ext.h>
72 #ifndef gss_nt_service_name
73 #define gss_nt_service_name GSS_C_NT_HOSTBASED_SERVICE
76 static const char *LogTime(void);
78 int check_gss_err(OM_uint32 major_status
, OM_uint32 minor_status
,
79 const char *function
);
81 const char *squid_kerb_proxy_auth(char *proxy
);
83 #define PROGRAM "negotiate_kerberos_auth_test"
90 static time_t last_t
= 0;
93 gettimeofday(&now
, NULL
);
94 if (now
.tv_sec
!= last_t
) {
95 tm
= localtime((const time_t *) &now
.tv_sec
);
96 strftime(buf
, 127, "%Y/%m/%d %H:%M:%S", tm
);
102 #ifndef gss_mech_spnego
103 static gss_OID_desc _gss_mech_spnego
= {6, (void *) "\x2b\x06\x01\x05\x05\x02"};
104 gss_OID gss_mech_spnego
= &_gss_mech_spnego
;
108 check_gss_err(OM_uint32 major_status
, OM_uint32 minor_status
,
109 const char *function
)
111 if (GSS_ERROR(major_status
)) {
112 OM_uint32 maj_stat
, min_stat
;
113 OM_uint32 msg_ctx
= 0;
114 gss_buffer_desc status_string
;
121 /* convert major status code (GSS-API error) to text */
122 maj_stat
= gss_display_status(&min_stat
, major_status
,
123 GSS_C_GSS_CODE
, GSS_C_NULL_OID
, &msg_ctx
, &status_string
);
124 if (maj_stat
== GSS_S_COMPLETE
&& status_string
.length
> 0) {
125 if (sizeof(buf
) > len
+ status_string
.length
+ 1) {
126 snprintf(buf
+ len
, (sizeof(buf
) - len
), "%s", (char *) status_string
.value
);
127 len
+= status_string
.length
;
131 gss_release_buffer(&min_stat
, &status_string
);
133 if (sizeof(buf
) > len
+ 2) {
134 snprintf(buf
+ len
, (sizeof(buf
) - len
), "%s", ". ");
139 /* convert minor status code (underlying routine error) to text */
140 maj_stat
= gss_display_status(&min_stat
, minor_status
,
141 GSS_C_MECH_CODE
, GSS_C_NULL_OID
, &msg_ctx
, &status_string
);
142 if (maj_stat
== GSS_S_COMPLETE
&& status_string
.length
> 0) {
143 if (sizeof(buf
) > len
+ status_string
.length
) {
144 snprintf(buf
+ len
, (sizeof(buf
) - len
), "%s", (char *) status_string
.value
);
145 len
+= status_string
.length
;
149 gss_release_buffer(&min_stat
, &status_string
);
151 fprintf(stderr
, "%s| %s: %s failed: %s\n", LogTime(), PROGRAM
, function
,
159 squid_kerb_proxy_auth(char *proxy
)
161 OM_uint32 major_status
, minor_status
;
162 gss_ctx_id_t gss_context
= GSS_C_NO_CONTEXT
;
163 gss_name_t server_name
= GSS_C_NO_NAME
;
164 gss_buffer_desc service
= GSS_C_EMPTY_BUFFER
;
165 gss_buffer_desc input_token
= GSS_C_EMPTY_BUFFER
;
166 gss_buffer_desc output_token
= GSS_C_EMPTY_BUFFER
;
169 setbuf(stdout
, NULL
);
173 fprintf(stderr
, "%s| %s: Error: No proxy server name\n", LogTime(),
177 service
.value
= xmalloc(strlen("HTTP") + strlen(proxy
) + 2);
178 snprintf((char *) service
.value
, strlen("HTTP") + strlen(proxy
) + 2, "%s@%s", "HTTP", proxy
);
179 service
.length
= strlen((char *) service
.value
);
181 major_status
= gss_import_name(&minor_status
, &service
,
182 gss_nt_service_name
, &server_name
);
184 if (check_gss_err(major_status
, minor_status
, "gss_import_name()"))
187 major_status
= gss_init_sec_context(&minor_status
,
188 GSS_C_NO_CREDENTIAL
, &gss_context
, server_name
,
192 GSS_C_NO_CHANNEL_BINDINGS
,
193 &input_token
, NULL
, &output_token
, NULL
, NULL
);
195 if (check_gss_err(major_status
, minor_status
, "gss_init_sec_context()"))
198 if (output_token
.length
) {
199 token
= (char *) xmalloc(base64_encode_len(output_token
.length
));
200 base64_encode_str(token
, base64_encode_len(output_token
.length
),
201 (const char *) output_token
.value
, output_token
.length
);
204 gss_delete_sec_context(&minor_status
, &gss_context
, NULL
);
205 gss_release_buffer(&minor_status
, &service
);
206 gss_release_buffer(&minor_status
, &input_token
);
207 gss_release_buffer(&minor_status
, &output_token
);
208 gss_release_name(&minor_status
, &server_name
);
214 main(int argc
, char *argv
[])
221 fprintf(stderr
, "%s| %s: Error: No proxy server name given\n",
226 count
= atoi(argv
[2]);
228 Token
= (const char *) squid_kerb_proxy_auth(argv
[1]);
229 fprintf(stdout
, "YR %s\n", Token
? Token
: "NULL");
232 fprintf(stdout
, "QQ\n");
234 Token
= (const char *) squid_kerb_proxy_auth(argv
[1]);
235 fprintf(stdout
, "Token: %s\n", Token
? Token
: "NULL");
244 main(int argc
, char *argv
[])
249 #endif /* HAVE_GSSAPI */