eap_server_methods.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * EAP server method registration
  3. * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "eap_i.h"
  17. #include "eap_methods.h"
  18. static struct eap_method *eap_methods;
  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. const struct eap_method * eap_server_get_eap_method(int vendor, EapType method)
  26. {
  27. struct eap_method *m;
  28. for (m = eap_methods; m; m = m->next) {
  29. if (m->vendor == vendor && m->method == method)
  30. return m;
  31. }
  32. return NULL;
  33. }
  34. /**
  35. * eap_server_get_type - Get EAP type for the given EAP method name
  36. * @name: EAP method name, e.g., TLS
  37. * @vendor: Buffer for returning EAP Vendor-Id
  38. * Returns: EAP method type or %EAP_TYPE_NONE if not found
  39. *
  40. * This function maps EAP type names into EAP type numbers based on the list of
  41. * EAP methods included in the build.
  42. */
  43. EapType eap_server_get_type(const char *name, int *vendor)
  44. {
  45. struct eap_method *m;
  46. for (m = eap_methods; m; m = m->next) {
  47. if (os_strcmp(m->name, name) == 0) {
  48. *vendor = m->vendor;
  49. return m->method;
  50. }
  51. }
  52. *vendor = EAP_VENDOR_IETF;
  53. return EAP_TYPE_NONE;
  54. }
  55. /**
  56. * eap_server_method_alloc - Allocate EAP server method structure
  57. * @version: Version of the EAP server method interface (set to
  58. * EAP_SERVER_METHOD_INTERFACE_VERSION)
  59. * @vendor: EAP Vendor-ID (EAP_VENDOR_*) (0 = IETF)
  60. * @method: EAP type number (EAP_TYPE_*)
  61. * @name: Name of the method (e.g., "TLS")
  62. * Returns: Allocated EAP method structure or %NULL on failure
  63. *
  64. * The returned structure should be freed with eap_server_method_free() when it
  65. * is not needed anymore.
  66. */
  67. struct eap_method * eap_server_method_alloc(int version, int vendor,
  68. EapType method, const char *name)
  69. {
  70. struct eap_method *eap;
  71. eap = os_zalloc(sizeof(*eap));
  72. if (eap == NULL)
  73. return NULL;
  74. eap->version = version;
  75. eap->vendor = vendor;
  76. eap->method = method;
  77. eap->name = name;
  78. return eap;
  79. }
  80. /**
  81. * eap_server_method_free - Free EAP server method structure
  82. * @method: Method structure allocated with eap_server_method_alloc()
  83. */
  84. void eap_server_method_free(struct eap_method *method)
  85. {
  86. os_free(method);
  87. }
  88. /**
  89. * eap_server_method_register - Register an EAP server method
  90. * @method: EAP method to register
  91. * Returns: 0 on success, -1 on invalid method, or -2 if a matching EAP method
  92. * has already been registered
  93. *
  94. * Each EAP server method needs to call this function to register itself as a
  95. * supported EAP method.
  96. */
  97. int eap_server_method_register(struct eap_method *method)
  98. {
  99. struct eap_method *m, *last = NULL;
  100. if (method == NULL || method->name == NULL ||
  101. method->version != EAP_SERVER_METHOD_INTERFACE_VERSION)
  102. return -1;
  103. for (m = eap_methods; m; m = m->next) {
  104. if ((m->vendor == method->vendor &&
  105. m->method == method->method) ||
  106. os_strcmp(m->name, method->name) == 0)
  107. return -2;
  108. last = m;
  109. }
  110. if (last)
  111. last->next = method;
  112. else
  113. eap_methods = method;
  114. return 0;
  115. }
  116. /**
  117. * eap_server_unregister_methods - Unregister EAP server methods
  118. *
  119. * This function is called at program termination to unregister all EAP server
  120. * methods.
  121. */
  122. void eap_server_unregister_methods(void)
  123. {
  124. struct eap_method *m;
  125. while (eap_methods) {
  126. m = eap_methods;
  127. eap_methods = eap_methods->next;
  128. if (m->free)
  129. m->free(m);
  130. else
  131. eap_server_method_free(m);
  132. }
  133. }
  134. /**
  135. * eap_server_get_name - Get EAP method name for the given EAP type
  136. * @vendor: EAP Vendor-Id (0 = IETF)
  137. * @type: EAP method type
  138. * Returns: EAP method name, e.g., TLS, or %NULL if not found
  139. *
  140. * This function maps EAP type numbers into EAP type names based on the list of
  141. * EAP methods included in the build.
  142. */
  143. const char * eap_server_get_name(int vendor, EapType type)
  144. {
  145. struct eap_method *m;
  146. for (m = eap_methods; m; m = m->next) {
  147. if (m->vendor == vendor && m->method == type)
  148. return m->name;
  149. }
  150. return NULL;
  151. }