Drop gnits mode.
[gsasl.git] / tests / old-gssapi.c
blobc7e5c7d02acd11af26635c2c5cfecc3ac9bfbfe0
1 /* old-gssapi.c --- Test the GSSAPI mechanism, using old callback API.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2007 Simon Josefsson
4 * This file is part of GNU SASL.
6 * This program 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 3 of the License, or
9 * (at your option) any later version.
11 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "utils.h"
32 #define SERVICE "host"
33 #define HOST "latte.josefsson.org"
34 #define GSSAPI_USER "jas"
36 static const char *USERNAME[] = {
37 "foo", "BABABA", "jas", "hepp", "@"
39 size_t i;
41 static int
42 server_cb_gssapi (Gsasl_session_ctx * ctx,
43 const char *client_name, const char *authentication_id)
45 if (client_name)
46 printf ("GSSAPI user: %s\n", client_name);
48 if (authentication_id)
49 printf ("Authorization ID: %s\n", authentication_id);
51 if (strcmp (client_name, GSSAPI_USER) == 0 &&
52 strcmp (authentication_id, USERNAME[i]) == 0)
53 return GSASL_OK;
54 else
55 return GSASL_AUTHENTICATION_ERROR;
58 static int
59 server_cb_service (Gsasl_session_ctx * ctx,
60 char *srv, size_t * srvlen, char *host, size_t * hostlen)
62 size_t srvneedlen = strlen (SERVICE);
63 size_t hostneedlen = strlen (HOST);
65 if (srv && *srvlen < srvneedlen)
66 return GSASL_TOO_SMALL_BUFFER;
68 if (host && *hostlen < hostneedlen)
69 return GSASL_TOO_SMALL_BUFFER;
71 *srvlen = srvneedlen;
72 if (srv)
73 memcpy (srv, SERVICE, *srvlen);
75 *hostlen = hostneedlen;
76 if (host)
77 memcpy (host, HOST, *hostlen);
79 return GSASL_OK;
82 static int
83 client_cb_authentication_id (Gsasl_session_ctx * xctx,
84 char *out, size_t * outlen)
86 size_t needlen = strlen (USERNAME[i]);
88 if (out && *outlen < needlen)
89 return GSASL_TOO_SMALL_BUFFER;
91 *outlen = needlen;
92 if (out)
93 memcpy (out, USERNAME[i], *outlen);
95 return GSASL_OK;
98 static int
99 client_cb_service (Gsasl_session_ctx * ctx,
100 char *srv, size_t * srvlen,
101 char *host, size_t * hostlen,
102 char *srvname, size_t * srvnamelen)
104 size_t srvneedlen = strlen (SERVICE);
105 size_t hostneedlen = strlen (HOST);
107 if (srv && srvlen && *srvlen < srvneedlen)
108 return GSASL_TOO_SMALL_BUFFER;
110 if (host && hostlen && *hostlen < hostneedlen)
111 return GSASL_TOO_SMALL_BUFFER;
113 if (srvlen)
115 *srvlen = srvneedlen;
116 if (srv)
117 memcpy (srv, SERVICE, *srvlen);
120 if (hostlen)
122 *hostlen = hostneedlen;
123 if (host)
124 memcpy (host, HOST, hostneedlen);
127 if (srvnamelen)
128 *srvnamelen = 0;
130 return GSASL_OK;
133 void
134 doit (void)
136 Gsasl_ctx *ctx = NULL;
137 Gsasl_session_ctx *server = NULL, *client = NULL;
138 char *s1 = NULL, *s2 = NULL;
139 int rc, res1, res2;
141 rc = gsasl_init (&ctx);
142 if (rc != GSASL_OK)
144 fail ("gsasl_init() failed (%d):\n%s\n", rc, gsasl_strerror (rc));
145 return;
148 gsasl_client_callback_service_set (ctx, client_cb_service);
149 gsasl_client_callback_authentication_id_set (ctx,
150 client_cb_authentication_id);
152 gsasl_server_callback_gssapi_set (ctx, server_cb_gssapi);
154 for (i = 0; i < 5; i++)
156 rc = gsasl_server_start (ctx, "GSSAPI", &server);
157 if (rc != GSASL_OK)
159 fail ("gsasl_init() failed (%d):\n%s\n", rc, gsasl_strerror (rc));
160 return;
162 rc = gsasl_client_start (ctx, "GSSAPI", &client);
163 if (rc != GSASL_OK)
165 fail ("gsasl_init() failed (%d):\n%s\n", rc, gsasl_strerror (rc));
166 return;
171 res1 = gsasl_step64 (server, s1, &s2);
172 if (s1)
174 free (s1);
175 s1 = NULL;
177 if (res1 != GSASL_OK && res1 != GSASL_NEEDS_MORE)
179 fail ("gsasl_step64 (1) failed (%d):\n%s\n", res1,
180 gsasl_strerror (res1));
181 return;
184 if (debug)
185 printf ("S: %s\n", s2);
187 if (res1 == GSASL_OK && strcmp (s2, "") == 0)
188 break;
190 res2 = gsasl_step64 (client, s2, &s1);
191 free (s2);
192 if (res2 != GSASL_OK && res2 != GSASL_NEEDS_MORE)
194 fail ("gsasl_step64 (2) failed (%d):\n%s\n", res2,
195 gsasl_strerror (res2));
196 return;
199 if (debug)
200 printf ("C: %s\n", s1);
202 while (res1 != GSASL_OK || res2 != GSASL_OK);
204 if (s1)
206 free (s1);
207 s1 = NULL;
210 if (debug)
211 printf ("\n");
213 gsasl_client_finish (client);
214 gsasl_server_finish (server);
217 gsasl_done (ctx);