krad.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
  2. /*
  3. * Copyright 2013 Red Hat, Inc. 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. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  17. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  18. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  19. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  20. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /*
  29. * This API is not considered as stable as the main krb5 API.
  30. *
  31. * - We may make arbitrary incompatible changes between feature releases
  32. * (e.g. from 1.12 to 1.13).
  33. * - We will make some effort to avoid making incompatible changes for
  34. * bugfix releases, but will make them if necessary.
  35. */
  36. #ifndef KRAD_H_
  37. #define KRAD_H_
  38. #include <krb5.h>
  39. #include <verto.h>
  40. #include <stddef.h>
  41. #include <stdio.h>
  42. #define KRAD_PACKET_SIZE_MAX 4096
  43. #define KRAD_SERVICE_TYPE_LOGIN 1
  44. #define KRAD_SERVICE_TYPE_FRAMED 2
  45. #define KRAD_SERVICE_TYPE_CALLBACK_LOGIN 3
  46. #define KRAD_SERVICE_TYPE_CALLBACK_FRAMED 4
  47. #define KRAD_SERVICE_TYPE_OUTBOUND 5
  48. #define KRAD_SERVICE_TYPE_ADMINISTRATIVE 6
  49. #define KRAD_SERVICE_TYPE_NAS_PROMPT 7
  50. #define KRAD_SERVICE_TYPE_AUTHENTICATE_ONLY 8
  51. #define KRAD_SERVICE_TYPE_CALLBACK_NAS_PROMPT 9
  52. #define KRAD_SERVICE_TYPE_CALL_CHECK 10
  53. #define KRAD_SERVICE_TYPE_CALLBACK_ADMINISTRATIVE 11
  54. typedef struct krad_attrset_st krad_attrset;
  55. typedef struct krad_packet_st krad_packet;
  56. typedef struct krad_client_st krad_client;
  57. typedef unsigned char krad_code;
  58. typedef unsigned char krad_attr;
  59. /* Called when a response is received or the request times out. */
  60. typedef void
  61. (*krad_cb)(krb5_error_code retval, const krad_packet *request,
  62. const krad_packet *response, void *data);
  63. /*
  64. * Called to iterate over a set of requests. Either the callback will be
  65. * called until it returns NULL, or it will be called with cancel = TRUE to
  66. * terminate in the middle of an iteration.
  67. */
  68. typedef const krad_packet *
  69. (*krad_packet_iter_cb)(void *data, krb5_boolean cancel);
  70. /*
  71. * Code
  72. */
  73. /* Convert a code name to its number. Only works for codes defined
  74. * by RFC 2875 or 2882. Returns 0 if the name was not found. */
  75. krad_code
  76. krad_code_name2num(const char *name);
  77. /* Convert a code number to its name. Only works for attributes defined
  78. * by RFC 2865 or 2882. Returns NULL if the name was not found. */
  79. const char *
  80. krad_code_num2name(krad_code code);
  81. /*
  82. * Attribute
  83. */
  84. /* Convert an attribute name to its number. Only works for attributes defined
  85. * by RFC 2865. Returns 0 if the name was not found. */
  86. krad_attr
  87. krad_attr_name2num(const char *name);
  88. /* Convert an attribute number to its name. Only works for attributes defined
  89. * by RFC 2865. Returns NULL if the name was not found. */
  90. const char *
  91. krad_attr_num2name(krad_attr type);
  92. /*
  93. * Attribute set
  94. */
  95. /* Create a new attribute set. */
  96. krb5_error_code
  97. krad_attrset_new(krb5_context ctx, krad_attrset **set);
  98. /* Create a deep copy of an attribute set. */
  99. krb5_error_code
  100. krad_attrset_copy(const krad_attrset *set, krad_attrset **copy);
  101. /* Free an attribute set. */
  102. void
  103. krad_attrset_free(krad_attrset *set);
  104. /* Add an attribute to a set. */
  105. krb5_error_code
  106. krad_attrset_add(krad_attrset *set, krad_attr type, const krb5_data *data);
  107. /* Add a four-octet unsigned number attribute to the given set. */
  108. krb5_error_code
  109. krad_attrset_add_number(krad_attrset *set, krad_attr type, krb5_ui_4 num);
  110. /* Delete the specified attribute. */
  111. void
  112. krad_attrset_del(krad_attrset *set, krad_attr type, size_t indx);
  113. /* Get the specified attribute. */
  114. const krb5_data *
  115. krad_attrset_get(const krad_attrset *set, krad_attr type, size_t indx);
  116. /*
  117. * Packet
  118. */
  119. /* Determine the bytes needed from the socket to get the whole packet. Don't
  120. * cache the return value as it can change! Returns -1 on EBADMSG. */
  121. ssize_t
  122. krad_packet_bytes_needed(const krb5_data *buffer);
  123. /* Free a packet. */
  124. void
  125. krad_packet_free(krad_packet *pkt);
  126. /*
  127. * Create a new request packet.
  128. *
  129. * This function takes the attributes specified in set and converts them into a
  130. * radius packet. The packet will have a randomized id. If cb is not NULL, it
  131. * will be called passing data as the argument to iterate over a set of
  132. * outstanding requests. In this case, the id will be both random and unique
  133. * across the set of requests.
  134. */
  135. krb5_error_code
  136. krad_packet_new_request(krb5_context ctx, const char *secret, krad_code code,
  137. const krad_attrset *set, krad_packet_iter_cb cb,
  138. void *data, krad_packet **request);
  139. /*
  140. * Create a new response packet.
  141. *
  142. * This function is similar to krad_packet_new_requst() except that it crafts a
  143. * packet in response to a request packet. This new packet will borrow values
  144. * from the request such as the id and the authenticator.
  145. */
  146. krb5_error_code
  147. krad_packet_new_response(krb5_context ctx, const char *secret, krad_code code,
  148. const krad_attrset *set, const krad_packet *request,
  149. krad_packet **response);
  150. /*
  151. * Decode a request radius packet from krb5_data.
  152. *
  153. * The resulting decoded packet will be a request packet stored in *reqpkt.
  154. *
  155. * If cb is NULL, *duppkt will always be NULL.
  156. *
  157. * If cb is not NULL, it will be called (with the data argument) to iterate
  158. * over a set of requests currently being processed. In this case, if the
  159. * packet is a duplicate of an already received request, the original request
  160. * will be set in *duppkt.
  161. */
  162. krb5_error_code
  163. krad_packet_decode_request(krb5_context ctx, const char *secret,
  164. const krb5_data *buffer, krad_packet_iter_cb cb,
  165. void *data, const krad_packet **duppkt,
  166. krad_packet **reqpkt);
  167. /*
  168. * Decode a response radius packet from krb5_data.
  169. *
  170. * The resulting decoded packet will be a response packet stored in *rsppkt.
  171. *
  172. * If cb is NULL, *reqpkt will always be NULL.
  173. *
  174. * If cb is not NULL, it will be called (with the data argument) to iterate
  175. * over a set of requests awaiting responses. In this case, if the response
  176. * packet matches one of these requests, the original request will be set in
  177. * *reqpkt.
  178. */
  179. krb5_error_code
  180. krad_packet_decode_response(krb5_context ctx, const char *secret,
  181. const krb5_data *buffer, krad_packet_iter_cb cb,
  182. void *data, const krad_packet **reqpkt,
  183. krad_packet **rsppkt);
  184. /* Encode packet. */
  185. const krb5_data *
  186. krad_packet_encode(const krad_packet *pkt);
  187. /* Get the code for the given packet. */
  188. krad_code
  189. krad_packet_get_code(const krad_packet *pkt);
  190. /* Get the specified attribute. */
  191. const krb5_data *
  192. krad_packet_get_attr(const krad_packet *pkt, krad_attr type, size_t indx);
  193. /*
  194. * Client
  195. */
  196. /* Create a new client. */
  197. krb5_error_code
  198. krad_client_new(krb5_context kctx, verto_ctx *vctx, krad_client **client);
  199. /* Free the client. */
  200. void
  201. krad_client_free(krad_client *client);
  202. /*
  203. * Send a request to a radius server.
  204. *
  205. * The remote host may be specified by one of the following formats:
  206. * - /path/to/unix.socket
  207. * - IPv4
  208. * - IPv4:port
  209. * - IPv4:service
  210. * - [IPv6]
  211. * - [IPv6]:port
  212. * - [IPv6]:service
  213. * - hostname
  214. * - hostname:port
  215. * - hostname:service
  216. *
  217. * The timeout parameter (milliseconds) is the total timeout across all remote
  218. * hosts (when DNS returns multiple entries) and all retries. For stream
  219. * sockets, the retries parameter is ignored and no retries are performed.
  220. *
  221. * The cb function will be called with the data argument when either a response
  222. * is received or the request times out on all possible remote hosts.
  223. */
  224. krb5_error_code
  225. krad_client_send(krad_client *rc, krad_code code, const krad_attrset *attrs,
  226. const char *remote, const char *secret, int timeout,
  227. size_t retries, krad_cb cb, void *data);
  228. #endif /* KRAD_H_ */