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.
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", "@"
43 server_cb_gssapi (Gsasl_session_ctx
* ctx
,
44 const char *client_name
, const char *authentication_id
)
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)
56 return GSASL_AUTHENTICATION_ERROR
;
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
;
74 memcpy (srv
, SERVICE
, *srvlen
);
76 *hostlen
= hostneedlen
;
78 memcpy (host
, HOST
, *hostlen
);
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
;
94 memcpy (out
, USERNAME
[i
], *outlen
);
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
;
116 *srvlen
= srvneedlen
;
118 memcpy (srv
, SERVICE
, *srvlen
);
123 *hostlen
= hostneedlen
;
125 memcpy (host
, HOST
, hostneedlen
);
137 Gsasl_ctx
*ctx
= NULL
;
138 Gsasl_session_ctx
*server
= NULL
, *client
= NULL
;
139 char *s1
= NULL
, *s2
= NULL
;
142 rc
= gsasl_init (&ctx
);
145 fail ("gsasl_init() failed (%d):\n%s\n", rc
, gsasl_strerror (rc
));
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
);
160 fail ("gsasl_init() failed (%d):\n%s\n", rc
, gsasl_strerror (rc
));
163 rc
= gsasl_client_start (ctx
, "GSSAPI", &client
);
166 fail ("gsasl_init() failed (%d):\n%s\n", rc
, gsasl_strerror (rc
));
172 res1
= gsasl_step64 (server
, s1
, &s2
);
178 if (res1
!= GSASL_OK
&& res1
!= GSASL_NEEDS_MORE
)
180 fail ("gsasl_step64 (1) failed (%d):\n%s\n", res1
,
181 gsasl_strerror (res1
));
186 printf ("S: %s\n", s2
);
188 if (res1
== GSASL_OK
&& strcmp (s2
, "") == 0)
191 res2
= gsasl_step64 (client
, s2
, &s1
);
193 if (res2
!= GSASL_OK
&& res2
!= GSASL_NEEDS_MORE
)
195 fail ("gsasl_step64 (2) failed (%d):\n%s\n", res2
,
196 gsasl_strerror (res2
));
201 printf ("C: %s\n", s1
);
203 while (res1
!= GSASL_OK
|| res2
!= GSASL_OK
);
214 gsasl_client_finish (client
);
215 gsasl_server_finish (server
);