Update.
[gsasl.git] / tests / old-gssapi.c
blob5b86f13e0d303e1287ef9a9f468fbe526cda15ae
1 /* old-gssapi.c --- Test the GSSAPI mechanism, using old callback API.
2 * Copyright (C) 2002, 2003, 2004, 2005 Simon Josefsson
4 * This file is part of GNU SASL.
6 * GNU SASL is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GNU SASL is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU SASL; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "utils.h"
33 #define SERVICE "host"
34 #define HOST "latte.josefsson.org"
35 #define GSSAPI_USER "jas"
37 static const char *USERNAME[] = {
38 "foo", "BABABA", "jas", "hepp", "@"
40 size_t i;
42 static int
43 server_cb_gssapi (Gsasl_session_ctx * ctx,
44 const char *client_name, const char *authentication_id)
46 if (client_name)
47 printf ("GSSAPI user: %s\n", client_name);
49 if (authentication_id)
50 printf ("Authorization ID: %s\n", authentication_id);
52 if (strcmp (client_name, GSSAPI_USER) == 0 &&
53 strcmp (authentication_id, USERNAME[i]) == 0)
54 return GSASL_OK;
55 else
56 return GSASL_AUTHENTICATION_ERROR;
59 static int
60 server_cb_service (Gsasl_session_ctx * ctx,
61 char *srv, size_t * srvlen, char *host, size_t * hostlen)
63 size_t srvneedlen = strlen (SERVICE);
64 size_t hostneedlen = strlen (HOST);
66 if (srv && *srvlen < srvneedlen)
67 return GSASL_TOO_SMALL_BUFFER;
69 if (host && *hostlen < hostneedlen)
70 return GSASL_TOO_SMALL_BUFFER;
72 *srvlen = srvneedlen;
73 if (srv)
74 memcpy (srv, SERVICE, *srvlen);
76 *hostlen = hostneedlen;
77 if (host)
78 memcpy (host, HOST, *hostlen);
80 return GSASL_OK;
83 static int
84 client_cb_authentication_id (Gsasl_session_ctx * xctx,
85 char *out, size_t * outlen)
87 size_t needlen = strlen (USERNAME[i]);
89 if (out && *outlen < needlen)
90 return GSASL_TOO_SMALL_BUFFER;
92 *outlen = needlen;
93 if (out)
94 memcpy (out, USERNAME[i], *outlen);
96 return GSASL_OK;
99 static int
100 client_cb_service (Gsasl_session_ctx * ctx,
101 char *srv, size_t * srvlen,
102 char *host, size_t * hostlen,
103 char *srvname, size_t * srvnamelen)
105 size_t srvneedlen = strlen (SERVICE);
106 size_t hostneedlen = strlen (HOST);
108 if (srv && srvlen && *srvlen < srvneedlen)
109 return GSASL_TOO_SMALL_BUFFER;
111 if (host && hostlen && *hostlen < hostneedlen)
112 return GSASL_TOO_SMALL_BUFFER;
114 if (srvlen)
116 *srvlen = srvneedlen;
117 if (srv)
118 memcpy (srv, SERVICE, *srvlen);
121 if (hostlen)
123 *hostlen = hostneedlen;
124 if (host)
125 memcpy (host, HOST, hostneedlen);
128 if (srvnamelen)
129 *srvnamelen = 0;
131 return GSASL_OK;
134 void
135 doit (void)
137 Gsasl_ctx *ctx = NULL;
138 Gsasl_session_ctx *server = NULL, *client = NULL;
139 char *s1 = NULL, *s2 = NULL;
140 int rc, res1, res2;
142 rc = gsasl_init (&ctx);
143 if (rc != GSASL_OK)
145 fail ("gsasl_init() failed (%d):\n%s\n", rc, gsasl_strerror (rc));
146 return;
149 gsasl_client_callback_service_set (ctx, client_cb_service);
150 gsasl_client_callback_authentication_id_set (ctx,
151 client_cb_authentication_id);
153 gsasl_server_callback_gssapi_set (ctx, server_cb_gssapi);
155 for (i = 0; i < 5; i++)
157 rc = gsasl_server_start (ctx, "GSSAPI", &server);
158 if (rc != GSASL_OK)
160 fail ("gsasl_init() failed (%d):\n%s\n", rc, gsasl_strerror (rc));
161 return;
163 rc = gsasl_client_start (ctx, "GSSAPI", &client);
164 if (rc != GSASL_OK)
166 fail ("gsasl_init() failed (%d):\n%s\n", rc, gsasl_strerror (rc));
167 return;
172 res1 = gsasl_step64 (server, s1, &s2);
173 if (s1)
175 free (s1);
176 s1 = NULL;
178 if (res1 != GSASL_OK && res1 != GSASL_NEEDS_MORE)
180 fail ("gsasl_step64 (1) failed (%d):\n%s\n", res1,
181 gsasl_strerror (res1));
182 return;
185 if (debug)
186 printf ("S: %s\n", s2);
188 if (res1 == GSASL_OK && strcmp (s2, "") == 0)
189 break;
191 res2 = gsasl_step64 (client, s2, &s1);
192 free (s2);
193 if (res2 != GSASL_OK && res2 != GSASL_NEEDS_MORE)
195 fail ("gsasl_step64 (2) failed (%d):\n%s\n", res2,
196 gsasl_strerror (res2));
197 return;
200 if (debug)
201 printf ("C: %s\n", s1);
203 while (res1 != GSASL_OK || res2 != GSASL_OK);
205 if (s1)
207 free (s1);
208 s1 = NULL;
211 if (debug)
212 printf ("\n");
214 gsasl_client_finish (client);
215 gsasl_server_finish (server);
218 gsasl_done (ctx);