37#define GSSKRB_APPLE_DEPRECATED(x)
54#if HAVE_GSSAPI_GSSAPI_H
55#include <gssapi/gssapi.h>
64#if HAVE_GSSAPI_GSSAPI_H
65#include <gssapi/gssapi.h>
69#if HAVE_GSSAPI_GSSAPI_KRB5_H
70#include <gssapi/gssapi_krb5.h>
72#if HAVE_GSSAPI_GSSAPI_GENERIC_H
73#include <gssapi/gssapi_generic.h>
75#if HAVE_GSSAPI_GSSAPI_EXT_H
76#include <gssapi/gssapi_ext.h>
80#ifndef gss_nt_service_name
81#define gss_nt_service_name GSS_C_NT_HOSTBASED_SERVICE
84static const char *
LogTime(
void);
86int check_gss_err(OM_uint32 major_status, OM_uint32 minor_status,
87 const char *function);
89const char *squid_kerb_proxy_auth(
char *proxy);
91#define PROGRAM "negotiate_kerberos_auth_test"
98 static time_t last_t = 0;
101 gettimeofday(&
now,
nullptr);
102 if (
now.tv_sec != last_t) {
103 tm = localtime((
const time_t *) &
now.tv_sec);
104 strftime(buf, 127,
"%Y/%m/%d %H:%M:%S", tm);
110#ifndef gss_mech_spnego
111static gss_OID_desc _gss_mech_spnego = {6, (
void *)
"\x2b\x06\x01\x05\x05\x02"};
112gss_OID gss_mech_spnego = &_gss_mech_spnego;
117 const char *function)
119 if (GSS_ERROR(major_status)) {
120 OM_uint32 maj_stat, min_stat;
121 OM_uint32 msg_ctx = 0;
122 gss_buffer_desc status_string;
130 maj_stat = gss_display_status(&min_stat, major_status,
131 GSS_C_GSS_CODE, GSS_C_NULL_OID, &msg_ctx, &status_string);
132 if (maj_stat == GSS_S_COMPLETE && status_string.length > 0) {
133 if (
sizeof(buf) > len + status_string.length + 1) {
134 snprintf(buf + len, (
sizeof(buf) - len),
"%s", (
char *) status_string.value);
135 len += status_string.length;
139 gss_release_buffer(&min_stat, &status_string);
141 if (
sizeof(buf) > len + 2) {
142 snprintf(buf + len, (
sizeof(buf) - len),
"%s",
". ");
148 maj_stat = gss_display_status(&min_stat, minor_status,
149 GSS_C_MECH_CODE, GSS_C_NULL_OID, &msg_ctx, &status_string);
150 if (maj_stat == GSS_S_COMPLETE && status_string.length > 0) {
151 if (
sizeof(buf) > len + status_string.length) {
152 snprintf(buf + len, (
sizeof(buf) - len),
"%s", (
char *) status_string.value);
153 len += status_string.length;
157 gss_release_buffer(&min_stat, &status_string);
159 fprintf(stderr,
"%s| %s: %s failed: %s\n",
LogTime(),
PROGRAM, function,
167squid_kerb_proxy_auth(
char *proxy)
169 OM_uint32 major_status, minor_status;
170 gss_ctx_id_t gss_context = GSS_C_NO_CONTEXT;
171 gss_name_t server_name = GSS_C_NO_NAME;
172 gss_buffer_desc service = GSS_C_EMPTY_BUFFER;
173 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
174 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
175 char *token =
nullptr;
177 setbuf(stdout,
nullptr);
178 setbuf(stdin,
nullptr);
181 fprintf(stderr,
"%s| %s: Error: No proxy server name\n",
LogTime(),
185 service.value =
xmalloc(strlen(
"HTTP") + strlen(proxy) + 2);
186 snprintf((
char *) service.value, strlen(
"HTTP") + strlen(proxy) + 2,
"%s@%s",
"HTTP", proxy);
187 service.length = strlen((
char *) service.value);
189 major_status = gss_import_name(&minor_status, &service,
192 if (!
check_gss_err(major_status, minor_status,
"gss_import_name()")) {
194 major_status = gss_init_sec_context(&minor_status,
195 GSS_C_NO_CREDENTIAL, &gss_context, server_name,
199 GSS_C_NO_CHANNEL_BINDINGS,
200 &input_token,
nullptr, &output_token,
nullptr,
nullptr);
202 if (!
check_gss_err(major_status, minor_status,
"gss_init_sec_context()") && output_token.length) {
206 size_t blen =
base64_encode_update(&ctx, token, output_token.length,
reinterpret_cast<const uint8_t*
>(output_token.value));
211 gss_delete_sec_context(&minor_status, &gss_context,
nullptr);
212 gss_release_buffer(&minor_status, &service);
213 gss_release_buffer(&minor_status, &input_token);
214 gss_release_buffer(&minor_status, &output_token);
215 gss_release_name(&minor_status, &server_name);
221main(
int argc,
char *argv[])
227 fprintf(stderr,
"%s| %s: Error: No proxy server name given\n",
232 count = atoi(argv[2]);
234 Token = (
const char *) squid_kerb_proxy_auth(argv[1]);
235 fprintf(stdout,
"YR %s\n", Token ? Token :
"NULL");
238 fprintf(stdout,
"QQ\n");
240 Token = (
const char *) squid_kerb_proxy_auth(argv[1]);
241 fprintf(stdout,
"Token: %s\n", Token ? Token :
"NULL");
const char * LogTime(void)
void base64_encode_init(struct base64_encode_ctx *ctx)
size_t base64_encode_update(struct base64_encode_ctx *ctx, char *dst, size_t length, const uint8_t *src)
size_t base64_encode_final(struct base64_encode_ctx *ctx, char *dst)
#define base64_encode_len(length)
#define gss_nt_service_name
int check_gss_err(OM_uint32 major_status, OM_uint32 minor_status, const char *function, int log, int sout)
int main(int argc, char *argv[])
void * xcalloc(size_t n, size_t sz)