Fix.
[gsasl.git] / tests / gssapi.c
blobb3e5759e5791935a6e405bbddcc57c3828ab75d1
1 /* gssapi.c --- Test the GSSAPI mechanism.
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 callback (Gsasl * ctx, Gsasl_session * sctx, Gsasl_property prop)
45 int rc = GSASL_NO_CALLBACK;
47 printf ("Callback for property %d\n", prop);
49 switch (prop)
51 case GSASL_AUTHID:
52 gsasl_property_set (sctx, GSASL_AUTHID, USERNAME[i]);
53 rc = GSASL_OK;
54 break;
56 case GSASL_SERVICE:
57 gsasl_property_set (sctx, prop, SERVICE);
58 rc = GSASL_OK;
59 break;
61 case GSASL_HOSTNAME:
62 gsasl_property_set (sctx, prop, HOST);
63 rc = GSASL_OK;
64 break;
66 case GSASL_VALIDATE_GSSAPI:
68 const char *client_name =
69 gsasl_property_fast (sctx, GSASL_GSSAPI_DISPLAY_NAME);
70 const char *authzid = gsasl_property_fast (sctx, GSASL_AUTHZID);
72 printf ("GSSAPI user: %s\n", client_name);
73 printf ("Authorization ID: %s\n", authzid);
75 if (strcmp (client_name, GSSAPI_USER) == 0 &&
76 strcmp (authzid, USERNAME[i]) == 0)
77 rc = GSASL_OK;
78 else
79 rc = GSASL_AUTHENTICATION_ERROR;
81 break;
83 default:
84 fail ("Unknown callback property %d\n", prop);
85 break;
88 return rc;
91 void
92 doit (void)
94 Gsasl *ctx = NULL;
95 Gsasl_session *server = NULL, *client = NULL;
96 char *s1 = NULL, *s2 = NULL;
97 int rc, res1, res2;
99 rc = gsasl_init (&ctx);
100 if (rc != GSASL_OK)
102 fail ("gsasl_init() failed (%d):\n%s\n", rc, gsasl_strerror (rc));
103 return;
106 gsasl_callback_set (ctx, callback);
108 for (i = 0; i < 5; i++)
110 rc = gsasl_server_start (ctx, "GSSAPI", &server);
111 if (rc != GSASL_OK)
113 fail ("gsasl_init() failed (%d):\n%s\n", rc, gsasl_strerror (rc));
114 return;
116 rc = gsasl_client_start (ctx, "GSSAPI", &client);
117 if (rc != GSASL_OK)
119 fail ("gsasl_init() failed (%d):\n%s\n", rc, gsasl_strerror (rc));
120 return;
125 res1 = gsasl_step64 (server, s1, &s2);
126 if (s1)
128 free (s1);
129 s1 = NULL;
131 if (res1 != GSASL_OK && res1 != GSASL_NEEDS_MORE)
133 fail ("gsasl_step64 (1) failed (%d):\n%s\n", res1,
134 gsasl_strerror (res1));
135 return;
138 if (debug)
139 printf ("S: %s\n", s2);
141 if (res1 == GSASL_OK && strcmp (s2, "") == 0)
142 break;
144 res2 = gsasl_step64 (client, s2, &s1);
145 free (s2);
146 if (res2 != GSASL_OK && res2 != GSASL_NEEDS_MORE)
148 fail ("gsasl_step64 (2) failed (%d):\n%s\n", res2,
149 gsasl_strerror (res2));
150 return;
153 if (debug)
154 printf ("C: %s\n", s1);
156 while (res1 != GSASL_OK || res2 != GSASL_OK);
158 if (s1)
160 free (s1);
161 s1 = NULL;
164 if (debug)
165 printf ("\n");
167 gsasl_finish (client);
168 gsasl_finish (server);
171 gsasl_done (ctx);