Branch data Line data Source code
1 : : /* gs2-krb5.c --- Test the GS2-KRB5 mechanism.
2 : : * Copyright (C) 2002-2012 Simon Josefsson
3 : : *
4 : : * This file is part of GNU SASL.
5 : : *
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.
10 : : *
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.
15 : : *
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/>.
18 : : *
19 : : */
20 : :
21 : : #ifdef HAVE_CONFIG_H
22 : : #include "config.h"
23 : : #endif
24 : :
25 : : #include <stdio.h>
26 : : #include <stdarg.h>
27 : : #include <stdlib.h>
28 : : #include <string.h>
29 : : #include <stdbool.h>
30 : :
31 : : #include "utils.h"
32 : :
33 : : #define SERVICE "host"
34 : : #define HOST "latte.josefsson.org"
35 : : #define GSSAPI_USER "jas"
36 : :
37 : : static const char *AUTHZID[] = {
38 : : "foo", "BAB,ABA", ",=,=", "=", ""
39 : : };
40 : :
41 : : size_t i;
42 : :
43 : : static int
44 : 0 : callback (Gsasl * ctx, Gsasl_session * sctx, Gsasl_property prop)
45 : : {
46 : 0 : int rc = GSASL_NO_CALLBACK;
47 : :
48 [ # # # # : 0 : switch (prop)
# ]
49 : : {
50 : : case GSASL_AUTHZID:
51 [ # # ]: 0 : if (*AUTHZID[i])
52 : : {
53 : 0 : gsasl_property_set (sctx, GSASL_AUTHZID, AUTHZID[i]);
54 : 0 : rc = GSASL_OK;
55 : : }
56 : 0 : break;
57 : :
58 : : case GSASL_SERVICE:
59 : 0 : gsasl_property_set (sctx, prop, SERVICE);
60 : 0 : rc = GSASL_OK;
61 : 0 : break;
62 : :
63 : : case GSASL_HOSTNAME:
64 : 0 : gsasl_property_set (sctx, prop, HOST);
65 : 0 : rc = GSASL_OK;
66 : 0 : break;
67 : :
68 : : case GSASL_VALIDATE_GSSAPI:
69 : : {
70 : 0 : const char *client_name =
71 : : gsasl_property_fast (sctx, GSASL_GSSAPI_DISPLAY_NAME);
72 : 0 : const char *authzid = gsasl_property_fast (sctx, GSASL_AUTHZID);
73 : :
74 [ # # ]: 0 : if (client_name)
75 : 0 : printf ("GSSAPI user: %s\n", client_name);
76 : : else
77 : 0 : fail ("no client name\n");
78 [ # # ]: 0 : if (authzid)
79 : 0 : printf ("Authorization ID: %s\n", authzid);
80 : :
81 [ # # ][ # # ]: 0 : if (client_name && strcmp (client_name, GSSAPI_USER) == 0 &&
[ # # ]
82 [ # # ]: 0 : ((authzid == NULL && *AUTHZID[i] == '\0')
83 [ # # ]: 0 : || strcmp (authzid, AUTHZID[i]) == 0))
84 : 0 : rc = GSASL_OK;
85 : : else
86 : 0 : rc = GSASL_AUTHENTICATION_ERROR;
87 : : }
88 : 0 : break;
89 : :
90 : : default:
91 : 0 : fail ("Unknown callback property %d\n", prop);
92 : 0 : break;
93 : : }
94 : :
95 : 0 : return rc;
96 : : }
97 : :
98 : : static char
99 : 0 : ret_char (int rc)
100 : : {
101 [ # # ]: 0 : if (rc == GSASL_OK)
102 : 0 : return 'O';
103 [ # # ]: 0 : else if (rc == GSASL_NEEDS_MORE)
104 : 0 : return 'N';
105 : : else
106 : 0 : return '?';
107 : : }
108 : :
109 : : void
110 : 1 : doit (void)
111 : : {
112 : 1 : Gsasl *ctx = NULL;
113 : 1 : Gsasl_session *server = NULL, *client = NULL;
114 : 1 : char *s1 = NULL, *s2 = NULL;
115 : : int rc, res1, res2;
116 : :
117 [ + - ][ + - ]: 1 : if (getenv ("GNUGSS") && strcmp (getenv ("GNUGSS"), "no") == 0)
118 : : {
119 : 1 : fail ("Not using GNU GSS, skipping self-test.\n");
120 : 1 : exit (77);
121 : : }
122 : :
123 : 0 : rc = gsasl_init (&ctx);
124 [ # # ]: 0 : if (rc != GSASL_OK)
125 : : {
126 : 0 : fail ("gsasl_init() failed (%d):\n%s\n", rc, gsasl_strerror (rc));
127 : 0 : return;
128 : : }
129 : :
130 [ # # ]: 0 : if (!gsasl_client_support_p (ctx, "GS2-KRB5")
131 [ # # ]: 0 : || !gsasl_server_support_p (ctx, "GS2-KRB5"))
132 : : {
133 : 0 : gsasl_done (ctx);
134 : 0 : fail ("No support for GS2-KRB5.\n");
135 : 0 : exit (77);
136 : : }
137 : :
138 : 0 : gsasl_callback_set (ctx, callback);
139 : :
140 [ # # ]: 0 : for (i = 0; i < 5; i++)
141 : : {
142 : 0 : bool client_first = (i % 2) == 0;
143 : :
144 : 0 : rc = gsasl_server_start (ctx, "GS2-KRB5", &server);
145 [ # # ]: 0 : if (rc != GSASL_OK)
146 : : {
147 : 0 : fail ("gsasl_server_start() failed (%d):\n%s\n",
148 : : rc, gsasl_strerror (rc));
149 : 0 : return;
150 : : }
151 : 0 : rc = gsasl_client_start (ctx, "GS2-KRB5", &client);
152 [ # # ]: 0 : if (rc != GSASL_OK)
153 : : {
154 : 0 : fail ("gsasl_client_start() failed (%d):\n%s\n",
155 : : rc, gsasl_strerror (rc));
156 : 0 : return;
157 : : }
158 : :
159 [ # # ]: 0 : if (client_first)
160 : : {
161 : 0 : rc = gsasl_step64 (client, NULL, &s1);
162 [ # # ][ # # ]: 0 : if (rc != GSASL_OK && rc != GSASL_NEEDS_MORE)
163 : : {
164 : 0 : fail ("gsasl_step64 failed (%d):\n%s\n", rc,
165 : : gsasl_strerror (rc));
166 : 0 : return;
167 : : }
168 : :
169 [ # # ]: 0 : if (debug)
170 : 0 : printf ("C: %s [%c]\n", s1, ret_char (rc));
171 : : }
172 : :
173 : : do
174 : : {
175 : 0 : res1 = gsasl_step64 (server, s1, &s2);
176 [ # # ][ # # ]: 0 : if (s1 == NULL && res1 == GSASL_OK)
177 : 0 : fail ("gsasl_step64 direct success?\n");
178 [ # # ]: 0 : if (s1)
179 : : {
180 : 0 : gsasl_free (s1);
181 : 0 : s1 = NULL;
182 : : }
183 [ # # ][ # # ]: 0 : if (res1 != GSASL_OK && res1 != GSASL_NEEDS_MORE)
184 : : {
185 : 0 : fail ("gsasl_step64 (1) failed (%d):\n%s\n", res1,
186 : : gsasl_strerror (res1));
187 : 0 : return;
188 : : }
189 : :
190 [ # # ]: 0 : if (debug)
191 : 0 : printf ("S: %s [%c]\n", s2, ret_char (res1));
192 : :
193 : 0 : res2 = gsasl_step64 (client, s2, &s1);
194 : 0 : gsasl_free (s2);
195 [ # # ][ # # ]: 0 : if (res2 != GSASL_OK && res2 != GSASL_NEEDS_MORE)
196 : : {
197 : 0 : fail ("gsasl_step64 (2) failed (%d):\n%s\n", res2,
198 : : gsasl_strerror (res2));
199 : 0 : return;
200 : : }
201 : :
202 [ # # ]: 0 : if (debug)
203 : 0 : printf ("C: %s [%c]\n", s1, ret_char (res2));
204 : : }
205 [ # # ][ # # ]: 0 : while (res1 != GSASL_OK || res2 != GSASL_OK);
206 : :
207 [ # # ]: 0 : if (s1)
208 : : {
209 : 0 : gsasl_free (s1);
210 : 0 : s1 = NULL;
211 : : }
212 : :
213 [ # # ]: 0 : if (debug)
214 : 0 : printf ("\n");
215 : :
216 : 0 : gsasl_finish (client);
217 : 0 : gsasl_finish (server);
218 : : }
219 : :
220 : 0 : gsasl_done (ctx);
221 : : }
|