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