Line data Source code
1 : /*
2 : * EAP server method registration
3 : * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
4 : *
5 : * This software may be distributed under the terms of the BSD license.
6 : * See README for more details.
7 : */
8 :
9 : #include "includes.h"
10 :
11 : #include "common.h"
12 : #include "eap_i.h"
13 : #include "eap_methods.h"
14 :
15 :
16 : static struct eap_method *eap_methods;
17 :
18 :
19 : /**
20 : * eap_server_get_eap_method - Get EAP method based on type number
21 : * @vendor: EAP Vendor-Id (0 = IETF)
22 : * @method: EAP type number
23 : * Returns: Pointer to EAP method or %NULL if not found
24 : */
25 7962 : const struct eap_method * eap_server_get_eap_method(int vendor, EapType method)
26 : {
27 : struct eap_method *m;
28 48299 : for (m = eap_methods; m; m = m->next) {
29 48134 : if (m->vendor == vendor && m->method == method)
30 7797 : return m;
31 : }
32 165 : return NULL;
33 : }
34 :
35 :
36 : /**
37 : * eap_server_get_type - Get EAP type for the given EAP method name
38 : * @name: EAP method name, e.g., TLS
39 : * @vendor: Buffer for returning EAP Vendor-Id
40 : * Returns: EAP method type or %EAP_TYPE_NONE if not found
41 : *
42 : * This function maps EAP type names into EAP type numbers based on the list of
43 : * EAP methods included in the build.
44 : */
45 14951 : EapType eap_server_get_type(const char *name, int *vendor)
46 : {
47 : struct eap_method *m;
48 197030 : for (m = eap_methods; m; m = m->next) {
49 194696 : if (os_strcmp(m->name, name) == 0) {
50 12617 : *vendor = m->vendor;
51 12617 : return m->method;
52 : }
53 : }
54 2334 : *vendor = EAP_VENDOR_IETF;
55 2334 : return EAP_TYPE_NONE;
56 : }
57 :
58 :
59 : /**
60 : * eap_server_method_alloc - Allocate EAP server method structure
61 : * @version: Version of the EAP server method interface (set to
62 : * EAP_SERVER_METHOD_INTERFACE_VERSION)
63 : * @vendor: EAP Vendor-ID (EAP_VENDOR_*) (0 = IETF)
64 : * @method: EAP type number (EAP_TYPE_*)
65 : * @name: Name of the method (e.g., "TLS")
66 : * Returns: Allocated EAP method structure or %NULL on failure
67 : *
68 : * The returned structure should be freed with eap_server_method_free() when it
69 : * is not needed anymore.
70 : */
71 673 : struct eap_method * eap_server_method_alloc(int version, int vendor,
72 : EapType method, const char *name)
73 : {
74 : struct eap_method *eap;
75 673 : eap = os_zalloc(sizeof(*eap));
76 673 : if (eap == NULL)
77 0 : return NULL;
78 673 : eap->version = version;
79 673 : eap->vendor = vendor;
80 673 : eap->method = method;
81 673 : eap->name = name;
82 673 : return eap;
83 : }
84 :
85 :
86 : /**
87 : * eap_server_method_free - Free EAP server method structure
88 : * @method: Method structure allocated with eap_server_method_alloc()
89 : */
90 673 : static void eap_server_method_free(struct eap_method *method)
91 : {
92 673 : os_free(method);
93 673 : }
94 :
95 :
96 : /**
97 : * eap_server_method_register - Register an EAP server method
98 : * @method: EAP method to register from eap_server_method_alloc()
99 : * Returns: 0 on success, -1 on invalid method, or -2 if a matching EAP method
100 : * has already been registered
101 : *
102 : * Each EAP server method needs to call this function to register itself as a
103 : * supported EAP method. The caller must not free the allocated method data
104 : * regardless of the return value.
105 : */
106 673 : int eap_server_method_register(struct eap_method *method)
107 : {
108 673 : struct eap_method *m, *last = NULL;
109 :
110 1346 : if (method == NULL || method->name == NULL ||
111 673 : method->version != EAP_SERVER_METHOD_INTERFACE_VERSION) {
112 0 : eap_server_method_free(method);
113 0 : return -1;
114 : }
115 :
116 7047 : for (m = eap_methods; m; m = m->next) {
117 10674 : if ((m->vendor == method->vendor &&
118 10674 : m->method == method->method) ||
119 6374 : os_strcmp(m->name, method->name) == 0) {
120 0 : eap_server_method_free(method);
121 0 : return -2;
122 : }
123 6374 : last = m;
124 : }
125 :
126 673 : if (last)
127 599 : last->next = method;
128 : else
129 74 : eap_methods = method;
130 :
131 673 : return 0;
132 : }
133 :
134 :
135 : /**
136 : * eap_server_unregister_methods - Unregister EAP server methods
137 : *
138 : * This function is called at program termination to unregister all EAP server
139 : * methods.
140 : */
141 74 : void eap_server_unregister_methods(void)
142 : {
143 : struct eap_method *m;
144 :
145 821 : while (eap_methods) {
146 673 : m = eap_methods;
147 673 : eap_methods = eap_methods->next;
148 :
149 673 : if (m->free)
150 0 : m->free(m);
151 : else
152 673 : eap_server_method_free(m);
153 : }
154 74 : }
155 :
156 :
157 : /**
158 : * eap_server_get_name - Get EAP method name for the given EAP type
159 : * @vendor: EAP Vendor-Id (0 = IETF)
160 : * @type: EAP method type
161 : * Returns: EAP method name, e.g., TLS, or "unknown" if not found
162 : *
163 : * This function maps EAP type numbers into EAP type names based on the list of
164 : * EAP methods included in the build.
165 : */
166 20126 : const char * eap_server_get_name(int vendor, EapType type)
167 : {
168 : struct eap_method *m;
169 20126 : if (vendor == EAP_VENDOR_IETF && type == EAP_TYPE_EXPANDED)
170 2666 : return "expanded";
171 213087 : for (m = eap_methods; m; m = m->next) {
172 211877 : if (m->vendor == vendor && m->method == type)
173 16250 : return m->name;
174 : }
175 1210 : return "unknown";
176 : }
|