Add.
[gsasl.git] / tests / gssapi.c
blobf0af2891c94d7807a4cc8181b160acc4c47bb0df
1 /* gssapi.c --- Test the GSSAPI mechanism.
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 callback (Gsasl * ctx, Gsasl_session * sctx, Gsasl_property prop)
44 int rc = GSASL_NO_CALLBACK;
46 printf ("Callback for property %d\n", prop);
48 switch (prop)
50 case GSASL_AUTHID:
51 gsasl_property_set (sctx, GSASL_AUTHID, USERNAME[i]);
52 rc = GSASL_OK;
53 break;
55 case GSASL_SERVICE:
56 gsasl_property_set (sctx, prop, SERVICE);
57 rc = GSASL_OK;
58 break;
60 case GSASL_HOSTNAME:
61 gsasl_property_set (sctx, prop, HOST);
62 rc = GSASL_OK;
63 break;
65 case GSASL_VALIDATE_GSSAPI:
67 const char *client_name =
68 gsasl_property_fast (sctx, GSASL_GSSAPI_DISPLAY_NAME);
69 const char *authzid = gsasl_property_fast (sctx, GSASL_AUTHZID);
71 printf ("GSSAPI user: %s\n", client_name);
72 printf ("Authorization ID: %s\n", authzid);
74 if (strcmp (client_name, GSSAPI_USER) == 0 &&
75 strcmp (authzid, USERNAME[i]) == 0)
76 rc = GSASL_OK;
77 else
78 rc = GSASL_AUTHENTICATION_ERROR;
80 break;
82 default:
83 fail ("Unknown callback property %d\n", prop);
84 break;
87 return rc;
90 void
91 doit (void)
93 Gsasl *ctx = NULL;
94 Gsasl_session *server = NULL, *client = NULL;
95 char *s1 = NULL, *s2 = NULL;
96 int rc, res1, res2;
98 rc = gsasl_init (&ctx);
99 if (rc != GSASL_OK)
101 fail ("gsasl_init() failed (%d):\n%s\n", rc, gsasl_strerror (rc));
102 return;
105 gsasl_callback_set (ctx, callback);
107 for (i = 0; i < 5; i++)
109 rc = gsasl_server_start (ctx, "GSSAPI", &server);
110 if (rc != GSASL_OK)
112 fail ("gsasl_init() failed (%d):\n%s\n", rc, gsasl_strerror (rc));
113 return;
115 rc = gsasl_client_start (ctx, "GSSAPI", &client);
116 if (rc != GSASL_OK)
118 fail ("gsasl_init() failed (%d):\n%s\n", rc, gsasl_strerror (rc));
119 return;
124 res1 = gsasl_step64 (server, s1, &s2);
125 if (s1)
127 free (s1);
128 s1 = NULL;
130 if (res1 != GSASL_OK && res1 != GSASL_NEEDS_MORE)
132 fail ("gsasl_step64 (1) failed (%d):\n%s\n", res1,
133 gsasl_strerror (res1));
134 return;
137 if (debug)
138 printf ("S: %s\n", s2);
140 if (res1 == GSASL_OK && strcmp (s2, "") == 0)
141 break;
143 res2 = gsasl_step64 (client, s2, &s1);
144 free (s2);
145 if (res2 != GSASL_OK && res2 != GSASL_NEEDS_MORE)
147 fail ("gsasl_step64 (2) failed (%d):\n%s\n", res2,
148 gsasl_strerror (res2));
149 return;
152 if (debug)
153 printf ("C: %s\n", s1);
155 while (res1 != GSASL_OK || res2 != GSASL_OK);
157 if (s1)
159 free (s1);
160 s1 = NULL;
163 if (debug)
164 printf ("\n");
166 gsasl_finish (client);
167 gsasl_finish (server);
170 gsasl_done (ctx);