]> git.decadent.org.uk Git - nfs-utils.git/blob - support/gssapi/g_mechname.c
Merge commit 'debian/1.0.7-2'
[nfs-utils.git] / support / gssapi / g_mechname.c
1 /*
2  * g_mechname.c --- registry of mechanism-specific name types
3  *
4  * This file contains a registry of mechanism-specific name types.  It
5  * is used to determine which name types not should be lazy evaluated,
6  * but rather evaluated on the spot.
7  */
8
9 #include "mglueP.h"
10 #ifdef HAVE_STDLIB_H
11 #include <stdlib.h>
12 #endif
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <errno.h>
17
18 #define g_OID_equal(o1,o2) \
19    (((o1)->length == (o2)->length) && \
20     (memcmp((o1)->elements,(o2)->elements,(int) (o1)->length) == 0))
21
22 static gss_mech_spec_name name_list = NULL;
23
24 /*
25  * generic searching helper function.
26  */
27 static gss_mech_spec_name search_mech_spec(name_type)
28     gss_OID name_type;
29 {
30     gss_mech_spec_name p;
31
32     for (p = name_list; p; p = p->next) {
33         if (g_OID_equal(name_type, p->name_type))
34             return p;
35     }
36     return NULL;
37 }
38
39 /*
40  * Given a name_type, if it is specific to a mechanism, return the
41  * mechanism OID.  Otherwise, return NULL.
42  */
43 gss_OID gss_find_mechanism_from_name_type(name_type)
44     gss_OID name_type;
45 {
46     gss_mech_spec_name p;
47
48     p = search_mech_spec(name_type);
49     if (!p)
50         return NULL;
51     return p->mech;
52 }
53
54 /*
55  * This function adds a (name_type, mechanism) pair to the
56  * mechanism-specific name type registry.  If an entry for the
57  * name_type already exists, then zero out the mechanism entry.
58  * Otherwise, enter the pair into the registry.
59  */
60 OM_uint32
61 gss_add_mech_name_type(minor_status, name_type, mech)
62     OM_uint32   *minor_status;
63     gss_OID     name_type;
64     gss_OID     mech;
65 {
66     OM_uint32   major_status, tmp;
67     gss_mech_spec_name p;
68
69     p = search_mech_spec(name_type);
70     if (p) {
71         /*
72          * We found an entry for this name type; mark it as not being
73          * a mechanism-specific name type.
74          */
75         if (p->mech) {
76             if (!g_OID_equal(mech, p->mech)) {
77                 generic_gss_release_oid(minor_status, &p->mech);
78                 p->mech = 0;
79             }
80         }
81         return GSS_S_COMPLETE;
82     }
83     p = malloc(sizeof(gss_mech_spec_name_desc));
84     if (!p) {
85         *minor_status = ENOMEM;
86         goto allocation_failure;
87     }
88     p->name_type = 0;
89     p->mech = 0;
90
91     major_status = generic_gss_copy_oid(minor_status, name_type,
92                                         &p->name_type);
93     if (major_status)
94         goto allocation_failure;
95     major_status = generic_gss_copy_oid(minor_status, mech,
96                                         &p->mech);
97     if (major_status)
98         goto allocation_failure;
99
100     p->next = name_list;
101     p->prev = 0;
102     name_list = p;
103
104     return GSS_S_COMPLETE;
105
106 allocation_failure:
107     if (p) {
108         if (p->mech)
109             generic_gss_release_oid(&tmp, &p->mech);
110         if (p->name_type)
111             generic_gss_release_oid(&tmp, &p->name_type);
112         free(p);
113     }
114     return GSS_S_FAILURE;
115 }
116