eap_server_methods.c 4.1 KB

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