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/>.
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", "@"
42 server_cb_gssapi (Gsasl_session_ctx
* ctx
,
43 const char *client_name
, const char *authentication_id
)
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)
55 return GSASL_AUTHENTICATION_ERROR
;
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
;
73 memcpy (srv
, SERVICE
, *srvlen
);
75 *hostlen
= hostneedlen
;
77 memcpy (host
, HOST
, *hostlen
);
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
;
93 memcpy (out
, USERNAME
[i
], *outlen
);
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
;
115 *srvlen
= srvneedlen
;
117 memcpy (srv
, SERVICE
, *srvlen
);
122 *hostlen
= hostneedlen
;
124 memcpy (host
, HOST
, hostneedlen
);
136 Gsasl_ctx
*ctx
= NULL
;
137 Gsasl_session_ctx
*server
= NULL
, *client
= NULL
;
138 char *s1
= NULL
, *s2
= NULL
;
141 rc
= gsasl_init (&ctx
);
144 fail ("gsasl_init() failed (%d):\n%s\n", rc
, gsasl_strerror (rc
));
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
);
159 fail ("gsasl_init() failed (%d):\n%s\n", rc
, gsasl_strerror (rc
));
162 rc
= gsasl_client_start (ctx
, "GSSAPI", &client
);
165 fail ("gsasl_init() failed (%d):\n%s\n", rc
, gsasl_strerror (rc
));
171 res1
= gsasl_step64 (server
, s1
, &s2
);
177 if (res1
!= GSASL_OK
&& res1
!= GSASL_NEEDS_MORE
)
179 fail ("gsasl_step64 (1) failed (%d):\n%s\n", res1
,
180 gsasl_strerror (res1
));
185 printf ("S: %s\n", s2
);
187 if (res1
== GSASL_OK
&& strcmp (s2
, "") == 0)
190 res2
= gsasl_step64 (client
, s2
, &s1
);
192 if (res2
!= GSASL_OK
&& res2
!= GSASL_NEEDS_MORE
)
194 fail ("gsasl_step64 (2) failed (%d):\n%s\n", res2
,
195 gsasl_strerror (res2
));
200 printf ("C: %s\n", s1
);
202 while (res1
!= GSASL_OK
|| res2
!= GSASL_OK
);
213 gsasl_client_finish (client
);
214 gsasl_server_finish (server
);