]> git.ipfire.org Git - thirdparty/squid.git/blob - helpers/negotiate_auth/kerberos/negotiate_kerberos_auth_test.cc
Merge from trunk
[thirdparty/squid.git] / helpers / negotiate_auth / kerberos / negotiate_kerberos_auth_test.cc
1 /*
2 * -----------------------------------------------------------------------------
3 *
4 * Author: Markus Moeller (markus_moeller at compuserve.com)
5 *
6 * Copyright (C) 2007 Markus Moeller. All rights reserved.
7 *
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.
12 *
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.
17 *
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.
21 *
22 * -----------------------------------------------------------------------------
23 */
24 /*
25 * Hosted at http://sourceforge.net/projects/squidkerbauth
26 */
27
28 #include "config.h"
29
30 #if HAVE_GSSAPI
31
32 #if HAVE_STRING_H
33 #include <string.h>
34 #endif
35 #if HAVE_STDIO_H
36 #include <stdio.h>
37 #endif
38 #if HAVE_STDLIB_H
39 #include <stdlib.h>
40 #endif
41 #if HAVE_NETDB_H
42 #include <netdb.h>
43 #endif
44 #if HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47 #if HAVE_TIME_H
48 #include <time.h>
49 #endif
50 #if HAVE_SYS_TIME_H
51 #include <sys/time.h>
52 #endif
53 #if HAVE_ERRNO_H
54 #include <errno.h>
55 #endif
56
57 #include "base64.h"
58 #include "util.h"
59
60 #if HAVE_HEIMDAL_KERBEROS
61 #if HAVE_GSSAPI_GSSAPI_H
62 #include <gssapi/gssapi.h>
63 #elif HAVE_GSSAPI_H
64 #include <gssapi.h>
65 #endif /* HAVE_GSSAPI_GSSAPI_H */
66 #define gss_nt_service_name GSS_C_NT_HOSTBASED_SERVICE
67 #else /* HAVE_HEIMDAL_KERBEROS */
68 #if HAVE_GSSAPI_GSSAPI_H
69 #include <gssapi/gssapi.h>
70 #elif HAVE_GSSAPI_H
71 #include <gssapi.h>
72 #endif /* HAVE_GSSAPI_GSSAPI_H */
73 #if HAVE_GSSAPI_GSSAPI_KRB5_H
74 #include <gssapi/gssapi_krb5.h>
75 #endif /* HAVE_GSSAPI_GSSAPI_KRB5_H */
76 #if HAVE_GSSAPI_GSSAPI_GENERIC_H
77 #include <gssapi/gssapi_generic.h>
78 #endif /* HAVE_GSSAPI_GSSAPI_GENERIC_H */
79 #endif /* HAVE_HEIMDAL_KERBEROS */
80
81 static const char *LogTime(void);
82
83 int check_gss_err(OM_uint32 major_status, OM_uint32 minor_status,
84 const char *function);
85
86 const char *squid_kerb_proxy_auth(char *proxy);
87
88 #define PROGRAM "negotiate_kerberos_auth_test"
89
90 static const char *
91 LogTime()
92 {
93 struct tm *tm;
94 struct timeval now;
95 static time_t last_t = 0;
96 static char buf[128];
97
98 gettimeofday(&now, NULL);
99 if (now.tv_sec != last_t) {
100 tm = localtime(&now.tv_sec);
101 strftime(buf, 127, "%Y/%m/%d %H:%M:%S", tm);
102 last_t = now.tv_sec;
103 }
104 return buf;
105 }
106
107 #ifdef HAVE_SPNEGO
108 #ifndef gss_mech_spnego
109 static gss_OID_desc _gss_mech_spnego =
110 { 6, (void *) "\x2b\x06\x01\x05\x05\x02" };
111 gss_OID gss_mech_spnego = &_gss_mech_spnego;
112 #endif
113 #endif
114
115 int
116 check_gss_err(OM_uint32 major_status, OM_uint32 minor_status,
117 const char *function)
118 {
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;
123 char buf[1024];
124 size_t len;
125
126 len = 0;
127 msg_ctx = 0;
128 while (!msg_ctx) {
129 /* convert major status code (GSS-API error) to text */
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) {
133 if (sizeof(buf) > len + status_string.length + 1) {
134 sprintf(buf + len, "%s", (char *) status_string.value);
135 len += status_string.length;
136 }
137 gss_release_buffer(&min_stat, &status_string);
138 break;
139 }
140 gss_release_buffer(&min_stat, &status_string);
141 }
142 if (sizeof(buf) > len + 2) {
143 sprintf(buf + len, "%s", ". ");
144 len += 2;
145 }
146 msg_ctx = 0;
147 while (!msg_ctx) {
148 /* convert minor status code (underlying routine error) to text */
149 maj_stat = gss_display_status(&min_stat, minor_status,
150 GSS_C_MECH_CODE, GSS_C_NULL_OID, &msg_ctx, &status_string);
151 if (maj_stat == GSS_S_COMPLETE) {
152 if (sizeof(buf) > len + status_string.length) {
153 sprintf(buf + len, "%s", (char *) status_string.value);
154 len += status_string.length;
155 }
156 gss_release_buffer(&min_stat, &status_string);
157 break;
158 }
159 gss_release_buffer(&min_stat, &status_string);
160 }
161 fprintf(stderr, "%s| %s: %s failed: %s\n", LogTime(), PROGRAM, function,
162 buf);
163 return (1);
164 }
165 return (0);
166 }
167
168 const char *
169 squid_kerb_proxy_auth(char *proxy)
170 {
171 OM_uint32 major_status, minor_status;
172 gss_ctx_id_t gss_context = GSS_C_NO_CONTEXT;
173 gss_name_t server_name = GSS_C_NO_NAME;
174 gss_buffer_desc service = GSS_C_EMPTY_BUFFER;
175 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
176 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
177 char *token = NULL;
178
179 setbuf(stdout, NULL);
180 setbuf(stdin, NULL);
181
182 if (!proxy) {
183 fprintf(stderr, "%s| %s: Error: No proxy server name\n", LogTime(),
184 PROGRAM);
185 return NULL;
186 }
187
188 service.value = xmalloc(strlen("HTTP") + strlen(proxy) + 2);
189 snprintf((char*)service.value, strlen("HTTP") + strlen(proxy) + 2, "%s@%s", "HTTP", proxy);
190 service.length = strlen((char *) service.value);
191
192 major_status = gss_import_name(&minor_status, &service,
193 gss_nt_service_name, &server_name);
194
195 if (check_gss_err(major_status, minor_status, "gss_import_name()"))
196 goto cleanup;
197
198 major_status = gss_init_sec_context(&minor_status,
199 GSS_C_NO_CREDENTIAL, &gss_context, server_name,
200 #ifdef HAVE_SPNEGO
201 gss_mech_spnego,
202 #else
203 0,
204 #endif
205 0,
206 0,
207 GSS_C_NO_CHANNEL_BINDINGS,
208 &input_token, NULL, &output_token, NULL, NULL);
209
210 if (check_gss_err(major_status, minor_status, "gss_init_sec_context()"))
211 goto cleanup;
212
213 if (output_token.length) {
214 token = (char*)xmalloc(ska_base64_encode_len(output_token.length));
215 ska_base64_encode(token, (const char *) output_token.value,
216 ska_base64_encode_len(output_token.length), output_token.length);
217 }
218
219
220 cleanup:
221 gss_delete_sec_context(&minor_status, &gss_context, NULL);
222 gss_release_buffer(&minor_status, &service);
223 gss_release_buffer(&minor_status, &input_token);
224 gss_release_buffer(&minor_status, &output_token);
225 gss_release_name(&minor_status, &server_name);
226
227 return token;
228 }
229
230 int
231 main(int argc, char *argv[])
232 {
233
234 const char *Token;
235 int count;
236
237 if (argc < 2) {
238 fprintf(stderr, "%s| %s: Error: No proxy server name given\n",
239 LogTime(), PROGRAM);
240 exit(99);
241 }
242 if (argc == 3) {
243 count = atoi(argv[2]);
244 while (count > 0) {
245 Token = (const char *) squid_kerb_proxy_auth(argv[1]);
246 fprintf(stdout, "YR %s\n", Token ? Token : "NULL");
247 count--;
248 }
249 fprintf(stdout, "QQ\n");
250 } else {
251 Token = (const char *) squid_kerb_proxy_auth(argv[1]);
252 fprintf(stdout, "Token: %s\n", Token ? Token : "NULL");
253 }
254
255 exit(0);
256 }
257 #else
258 #include <stdlib.h>
259 int
260 main(int argc, char *argv[])
261 {
262 exit(-1);
263 }
264 #endif /* HAVE_GSSAPI */