Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/rpc_gss_utils.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2013, Oracle America, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of "Oracle America, Inc." nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <string.h>
30 #include <errno.h>
31 #include <reentrant.h>
32
33 #include <rpc/auth_gss.h>
34 #include <rpc/rpcsec_gss.h>
35
36 #include <gssapi/gssapi_krb5.h>
37
38 /* Internal only */
39 void rpc_gss_set_error(int);
40 void rpc_gss_clear_error(void);
41 bool_t rpc_gss_oid_to_mech(rpc_gss_OID, char **);
42
43 /*
44 * Return the static "_rpc_gss_error" if we are the main thread
45 * (including non-threaded programs), or if an allocation fails.
46 */
47 static rpc_gss_error_t *
48 __rpc_gss_error(void)
49 {
50 static rpc_gss_error_t _rpc_gss_error = { 0, 0 };
51 extern thread_key_t rg_key;
52 rpc_gss_error_t *result;
53
54 if (rg_key == KEY_INITIALIZER) {
55 static mutex_t _rpc_gss_error_lock = MUTEX_INITIALIZER;
56 int err = 0;
57
58 mutex_lock(&_rpc_gss_error_lock);
59 if (rg_key == KEY_INITIALIZER)
60 err = thr_keycreate(&rg_key, free);
61 mutex_unlock(&_rpc_gss_error_lock);
62
63 if (err != 0)
64 return &_rpc_gss_error;
65 }
66
67 result = thr_getspecific(rg_key);
68 if (result == NULL) {
69 result = calloc(1, sizeof(*result));
70 if (result == NULL)
71 return &_rpc_gss_error;
72
73 if (thr_setspecific(rg_key, result) != 0) {
74 free(result);
75 return &_rpc_gss_error;
76 }
77 }
78
79 return result;
80 }
81
82 /*
83 * External API: Retrieve thread-specific error values
84 *
85 * error: address of rpc_gss_error_t structure to fill in
86 */
87 void
88 rpc_gss_get_error(rpc_gss_error_t *result)
89 {
90 rpc_gss_error_t *error = __rpc_gss_error();
91
92 result->rpc_gss_error = error->rpc_gss_error;
93 result->system_error = error->system_error;
94 }
95
96 /*
97 * Internal only: Set thread-specific error value
98 *
99 * system_error: new value for thread's .system_error field.
100 * .rpc_gss_error is always set to RPC_GSS_ER_SYSTEMERROR.
101 */
102 void
103 rpc_gss_set_error(int system_error)
104 {
105 rpc_gss_error_t *error = __rpc_gss_error();
106
107 error->rpc_gss_error = RPC_GSS_ER_SYSTEMERROR;
108 error->system_error = system_error;
109 }
110
111 /*
112 * Internal only: Clear thread-specific error values
113 */
114 void
115 rpc_gss_clear_error(void)
116 {
117 rpc_gss_error_t *error = __rpc_gss_error();
118
119 error->rpc_gss_error = RPC_GSS_ER_SUCCESS;
120 error->system_error = 0;
121 }
122
123 /*
124 * On Solaris, the GSS-API implementation consults the files
125 * /etc/gss/mech and /etc/gss/qop for a mapping of mechanism
126 * names to OIDs, and QOP names to numbers.
127 *
128 * GNU's GSS-API has no such database. We emulate it here
129 * with a built-in table of supported mechanisms and qops,
130 * since the set of supported mechanisms is unlikely to
131 * change often.
132 */
133
134 struct _rpc_gss_qop {
135 char *qi_name;
136 unsigned int qi_num;
137 };
138
139 struct _rpc_gss_mechanism {
140 char *mi_name;
141 rpc_gss_OID_desc mi_oid;
142 char **mi_qop_names;
143 struct _rpc_gss_qop **mi_qops;
144 };
145
146
147 static struct _rpc_gss_qop _rpc_gss_qop_default = {
148 .qi_name = "GSS_C_QOP_DEFAULT",
149 .qi_num = GSS_C_QOP_DEFAULT,
150 };
151
152 static struct _rpc_gss_qop *_rpc_gss_krb5_qops[] = {
153 &_rpc_gss_qop_default,
154 NULL,
155 };
156
157 static char *_rpc_gss_krb5_qop_names[] = {
158 "GSS_C_QOP_DEFAULT",
159 NULL,
160 };
161
162 /* GSS_MECH_KRB5_OID: Defined by RFC 1964 */
163 static struct _rpc_gss_mechanism _rpc_gss_mech_kerberos_v5 = {
164 .mi_name = "kerberos_v5",
165 .mi_oid = { 9, "\052\206\110\206\367\022\001\002\002" },
166 .mi_qop_names = _rpc_gss_krb5_qop_names,
167 .mi_qops = _rpc_gss_krb5_qops,
168 };
169
170 /* GSS_KRB5_NT_PRINCIPAL_NAME: Defined by RFC 1964 */
171 static struct _rpc_gss_mechanism _rpc_gss_mech_kerberos_v5_princname = {
172 .mi_name = "kerberos_v5",
173 .mi_oid = { 10, "\052\206\110\206\367\022\001\002\002\001" },
174 .mi_qop_names = _rpc_gss_krb5_qop_names,
175 .mi_qops = _rpc_gss_krb5_qops,
176 };
177
178 static struct _rpc_gss_mechanism *_rpc_gss_mechanisms[] = {
179 &_rpc_gss_mech_kerberos_v5,
180 &_rpc_gss_mech_kerberos_v5_princname,
181 NULL,
182 };
183
184 static char *_rpc_gss_mechanism_names[] = {
185 "kerberos_v5",
186 NULL,
187 };
188
189 static struct _rpc_gss_mechanism *
190 _rpc_gss_find_mechanism(char *mechanism)
191 {
192 unsigned int i;
193
194 for (i = 0; _rpc_gss_mechanisms[i] != NULL; i++)
195 if (strcmp(mechanism, _rpc_gss_mechanisms[i]->mi_name) == 0)
196 return _rpc_gss_mechanisms[i];
197 return NULL;
198 }
199
200 static bool_t
201 _rpc_gss_OID_equal(rpc_gss_OID o1, rpc_gss_OID o2)
202 {
203 return (o1->length == o2->length) &&
204 (memcmp(o1->elements, o2->elements, o1->length) == 0);
205 }
206
207 static struct _rpc_gss_mechanism *
208 _rpc_gss_find_oid(rpc_gss_OID oid)
209 {
210 unsigned int i;
211
212 for (i = 0; _rpc_gss_mechanisms[i] != NULL; i++)
213 if (_rpc_gss_OID_equal(oid, &_rpc_gss_mechanisms[i]->mi_oid))
214 return _rpc_gss_mechanisms[i];
215 return NULL;
216 }
217
218 static struct _rpc_gss_qop *
219 _rpc_gss_find_qop_by_name(struct _rpc_gss_mechanism *m, char *qop)
220 {
221 unsigned int i;
222
223 for (i = 0; m->mi_qops[i] != NULL; i++)
224 if (strcmp(qop, m->mi_qops[i]->qi_name) == 0)
225 return m->mi_qops[i];
226 return NULL;
227 }
228
229 static struct _rpc_gss_qop *
230 _rpc_gss_find_qop_by_num(struct _rpc_gss_mechanism *m, u_int num)
231 {
232 unsigned int i;
233
234 for (i = 0; m->mi_qops[i] != NULL; i++)
235 if (num == m->mi_qops[i]->qi_num)
236 return m->mi_qops[i];
237 return NULL;
238 }
239
240 /*
241 * External API: Return array of security mechanism names
242 *
243 * Returns NULL-terminated array of pointers to NUL-terminated C
244 * strings, each containing a mechanism name. This is statically
245 * allocated memory. Caller must not free this array.
246 */
247 char **
248 rpc_gss_get_mechanisms(void)
249 {
250 rpc_gss_clear_error();
251 return (char **)_rpc_gss_mechanism_names;
252 }
253
254 /*
255 * External API: Return array of qop names for a security mechanism
256 *
257 * mechanism: NUL-terminated C string containing mechanism name
258 * dummy: address of a service enum
259 *
260 * Returns NULL-terminated array of pointers to NUL-terminated C
261 * strings, each containing a qop name. This is statically
262 * allocated memory. Caller must not free this array.
263 */
264 char **
265 rpc_gss_get_mech_info(char *mechanism, rpc_gss_service_t *dummy)
266 {
267 struct _rpc_gss_mechanism *m;
268
269 if (mechanism == NULL || dummy == NULL) {
270 rpc_gss_set_error(EINVAL);
271 return NULL;
272 }
273
274 m = _rpc_gss_find_mechanism(mechanism);
275 if (m == NULL) {
276 rpc_gss_set_error(ENOENT);
277 return NULL;
278 }
279
280 rpc_gss_clear_error();
281 *dummy = rpcsec_gss_svc_privacy;
282 return (char **)m->mi_qop_names;
283 }
284
285 /*
286 * External API: Return range of supported RPCSEC_GSS versions
287 *
288 * high: address of highest supported version to fill in
289 * low: address of lowest supported version to fill in
290 *
291 * Returns TRUE if successful, or FALSE if an error occurs.
292 */
293 bool_t
294 rpc_gss_get_versions(u_int *high, u_int *low)
295 {
296 if (high == NULL || low == NULL) {
297 rpc_gss_set_error(EINVAL);
298 return FALSE;
299 }
300
301 rpc_gss_clear_error();
302 *high = *low = RPCSEC_GSS_VERSION;
303 return TRUE;
304 }
305
306 /*
307 * External API: Check if a security mechanism is supported
308 *
309 * mechanism: NUL-terminated C string containing mechanism name
310 *
311 * Returns TRUE if the mechanism name is recognized and supported,
312 * otherwise FALSE is returned.
313 */
314 bool_t
315 rpc_gss_is_installed(char *mechanism)
316 {
317 struct _rpc_gss_mechanism *m;
318
319 if (mechanism == NULL) {
320 rpc_gss_set_error(EINVAL);
321 return FALSE;
322 }
323
324 rpc_gss_clear_error();
325 m = _rpc_gss_find_mechanism(mechanism);
326 if (m == NULL)
327 return FALSE;
328 return TRUE;
329 }
330
331 /*
332 * External API: Return the OID for a given security mechanism
333 *
334 * mechanism: NUL-terminated C string containing mechanism name
335 * oid: address of OID buffer to fill in
336 *
337 * Returns TRUE if the mechanism name is recognized and supported,
338 * otherwise FALSE is returned.
339 */
340 bool_t
341 rpc_gss_mech_to_oid(char *mechanism, rpc_gss_OID *result)
342 {
343 struct _rpc_gss_mechanism *m;
344
345 if (mechanism == NULL || result == NULL) {
346 rpc_gss_set_error(EINVAL);
347 return FALSE;
348 }
349
350 m = _rpc_gss_find_mechanism(mechanism);
351 if (m == NULL) {
352 rpc_gss_set_error(ENOENT);
353 return FALSE;
354 }
355
356 *result = &m->mi_oid;
357 rpc_gss_clear_error();
358 return TRUE;
359 }
360
361 /*
362 * Internal only: Return the mechanism name for a given OID
363 *
364 * oid: GSS mechanism OID
365 * mechanism: address of a char * to fill in. This is statically
366 * allocated memory. Caller must not free this memory.
367 *
368 * Returns TRUE if the OID is a recognized and supported mechanism,
369 * otherwise FALSE is returned.
370 */
371 bool_t
372 rpc_gss_oid_to_mech(rpc_gss_OID oid, char **mechanism)
373 {
374 struct _rpc_gss_mechanism *m;
375
376 if (oid == NULL || mechanism == NULL) {
377 rpc_gss_set_error(EINVAL);
378 return FALSE;
379 }
380
381 m = _rpc_gss_find_oid(oid);
382 if (m == NULL) {
383 rpc_gss_set_error(ENOENT);
384 return FALSE;
385 }
386
387 *mechanism = m->mi_name;
388 rpc_gss_clear_error();
389 return TRUE;
390 }
391
392 /*
393 * External API: Return the QOP number for a given QOP and mechanism name
394 *
395 * qop: NUL-terminated C string containing qop name
396 * mechanism: NUL-terminated C string containing mechanism name
397 * num: address of QOP buffer to fill in
398 *
399 * Returns TRUE if the qop and mechanism name are recognized and
400 * supported, otherwise FALSE is returned.
401 */
402 bool_t
403 rpc_gss_qop_to_num(char *qop, char *mechanism, u_int *num)
404 {
405 struct _rpc_gss_mechanism *m;
406 struct _rpc_gss_qop *q;
407
408 if (qop == NULL || mechanism == NULL || num == NULL) {
409 rpc_gss_set_error(EINVAL);
410 return FALSE;
411 }
412
413 m = _rpc_gss_find_mechanism(mechanism);
414 if (m == NULL)
415 goto out_err;
416
417 q = _rpc_gss_find_qop_by_name(m, qop);
418 if (q == NULL)
419 goto out_err;
420
421 *num = q->qi_num;
422 rpc_gss_clear_error();
423 return TRUE;
424
425 out_err:
426 rpc_gss_set_error(ENOENT);
427 return FALSE;
428 }
429
430 /*
431 * Internal only: Return the QOP name for a given mechanism and QOP number
432 *
433 * mechanism: NUL-terminated C string containing security mechanism name
434 * num: QOP number
435 * qop: address of a char * to fill in. This is statically
436 * allocated memory. Caller must not free this memory.
437 *
438 * Returns TRUE if the QOP and mechanism are recognized and supported,
439 * otherwise FALSE is returned.
440 */
441 bool_t
442 rpc_gss_num_to_qop(char *mechanism, u_int num, char **qop)
443 {
444 struct _rpc_gss_mechanism *m;
445 struct _rpc_gss_qop *q;
446
447 if (mechanism == NULL || qop == NULL) {
448 rpc_gss_set_error(EINVAL);
449 return FALSE;
450 }
451
452 m = _rpc_gss_find_mechanism(mechanism);
453 if (m == NULL)
454 goto out_err;
455
456 q = _rpc_gss_find_qop_by_num(m, num);
457 if (q == NULL)
458 goto out_err;
459
460 *qop = q->qi_name;
461 rpc_gss_clear_error();
462 return TRUE;
463
464 out_err:
465 rpc_gss_set_error(ENOENT);
466 return FALSE;
467 }