buildreq.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * $Id: buildreq.c,v 1.17 2010/02/04 10:27:09 aland Exp $
  3. *
  4. * Copyright (C) 1995,1997 Lars Fenneberg
  5. *
  6. * See the file COPYRIGHT for the respective terms and conditions.
  7. * If the file is missing contact me at lf@elemental.net
  8. * and I'll send you a copy.
  9. *
  10. */
  11. #include <radius_config.h>
  12. #include <includes.h>
  13. #include <freeradius-client.h>
  14. #include "util.h"
  15. /** Build a skeleton RADIUS request using information from the config file
  16. *
  17. * @param rh a handle to parsed configuration.
  18. * @param data a pointer to a #SEND_DATA structure.
  19. * @param code one of standard RADIUS codes (e.g., %PW_ACCESS_REQUEST).
  20. * @param server the name of the server.
  21. * @param port the server's port number.
  22. * @param secret the secret used by the server.
  23. * @param timeout the timeout in seconds of a message.
  24. * @param retries the number of retries.
  25. */
  26. void rc_buildreq(rc_handle const *rh, SEND_DATA *data, int code, char *server, unsigned short port,
  27. char *secret, int timeout, int retries)
  28. {
  29. data->server = server;
  30. data->secret = secret;
  31. data->svc_port = port;
  32. data->seq_nbr = rc_get_id();
  33. data->timeout = timeout;
  34. data->retries = retries;
  35. data->code = code;
  36. }
  37. /** Generates a random ID
  38. *
  39. * @return the random ID.
  40. */
  41. unsigned char rc_get_id()
  42. {
  43. return (unsigned char)(random() & UCHAR_MAX);
  44. }
  45. /** Builds an authentication/accounting request for port id client_port with the value_pairs send and submits it to a server
  46. *
  47. * @param rh a handle to parsed configuration.
  48. * @param client_port the client port number to use (may be zero to use any available).
  49. * @param send a #VALUE_PAIR array of values (e.g., %PW_USER_NAME).
  50. * @param received an allocated array of received values.
  51. * @param msg must be an array of %PW_MAX_MSG_SIZE or %NULL; will contain the concatenation of any
  52. * %PW_REPLY_MESSAGE received.
  53. * @param add_nas_port if non-zero it will include %PW_NAS_PORT in sent pairs.
  54. * @param request_type one of standard RADIUS codes (e.g., %PW_ACCESS_REQUEST).
  55. * @return received value_pairs in received, messages from the server in msg and %OK_RC (0) on success, negative
  56. * on failure as return value.
  57. */
  58. int rc_aaa(rc_handle *rh, uint32_t client_port, VALUE_PAIR *send, VALUE_PAIR **received,
  59. char *msg, int add_nas_port, int request_type)
  60. {
  61. SEND_DATA data;
  62. VALUE_PAIR* myVp;
  63. SERVER *aaaserver;
  64. int timeout = rc_conf_int(rh, "radius_timeout");
  65. int retries = rc_conf_int(rh, "radius_retries");
  66. int radius_deadtime = rc_conf_int(rh, "radius_deadtime");
  67. unsigned type;
  68. int result;
  69. SERVER myServer;
  70. myVp = rc_avpair_get(send, PW_USER_PASSWORD, 0);
  71. myServer.secret[0] = myVp->name;
  72. aaaserver = &myServer;
  73. type = AUTH;
  74. if (aaaserver == NULL)
  75. return ERROR_RC;
  76. data.send_pairs = send;
  77. data.receive_pairs = NULL;
  78. if (add_nas_port != 0) {
  79. // Fill in NAS-Port
  80. if (rc_avpair_add(rh, &(data.send_pairs), PW_NAS_PORT,
  81. &client_port, 0, 0) == NULL)
  82. return ERROR_RC;
  83. }
  84. if (data.receive_pairs != NULL) {
  85. rc_avpair_free(data.receive_pairs);
  86. data.receive_pairs = NULL;
  87. }
  88. rc_buildreq(rh, &data, request_type, aaaserver->name[0],
  89. aaaserver->port[0], aaaserver->secret[0], timeout, retries);
  90. // Делаем 3 попытки если есть какие-либо проблемы с обменом
  91. for (uint8_t i = 0; i < 3; i++)
  92. {
  93. //printf ("Radius trying\r\n");
  94. result = rc_send_server(rh, &data, msg, type);
  95. if (result != NET_ERR_RC)
  96. break;
  97. }
  98. return result;
  99. }
  100. /** Builds an authentication request for port id client_port with the value_pairs send and submits it to a server
  101. *
  102. * @param rh a handle to parsed configuration.
  103. * @param client_port the client port number to use (may be zero to use any available).
  104. * @param send a #VALUE_PAIR array of values (e.g., %PW_USER_NAME).
  105. * @param received an allocated array of received values.
  106. * @param msg must be an array of %PW_MAX_MSG_SIZE or %NULL; will contain the concatenation of any
  107. * %PW_REPLY_MESSAGE received.
  108. * @return received value_pairs in @received, messages from the server in msg (if non-NULL),
  109. * and %OK_RC (0) on success, negative on failure as return value.
  110. */
  111. int rc_auth(rc_handle *rh, uint32_t client_port, VALUE_PAIR *send, VALUE_PAIR **received,
  112. char *msg)
  113. {
  114. //return rc_aaa(rh, client_port, send, received, msg, 1, PW_ACCESS_REQUEST);
  115. return rc_aaa(rh, client_port, send, received, msg, 0, PW_ACCESS_REQUEST);
  116. }
  117. /** Builds an authentication request for proxying
  118. *
  119. * Builds an authentication request with the value_pairs send and submits it to a server.
  120. * Works for a proxy; does not add IP address, and does does not rely on config file.
  121. *
  122. * @param rh a handle to parsed configuration.
  123. * @param client_port the client port number to use (may be zero to use any available).
  124. * @param send a #VALUE_PAIR array of values (e.g., %PW_USER_NAME).
  125. * @param received an allocated array of received values.
  126. * @param msg must be an array of %PW_MAX_MSG_SIZE or %NULL; will contain the concatenation of
  127. * any %PW_REPLY_MESSAGE received.
  128. * @return received value_pairs in @received, messages from the server in msg (if non-NULL)
  129. * and %OK_RC (0) on success, negative on failure as return value.
  130. */
  131. int rc_auth_proxy(rc_handle *rh, VALUE_PAIR *send, VALUE_PAIR **received, char *msg)
  132. {
  133. return rc_aaa(rh, 0, send, received, msg, 0, PW_ACCESS_REQUEST);
  134. }
  135. /** Builds an accounting request for port id client_port with the value_pairs at send
  136. *
  137. * @note NAS-IP-Address, NAS-Port and Acct-Delay-Time get filled in by this function, the rest has to be supplied.
  138. *
  139. * @param rh a handle to parsed configuration.
  140. * @param client_port the client port number to use (may be zero to use any available).
  141. * @param send a #VALUE_PAIR array of values (e.g., %PW_USER_NAME).
  142. * @return received value_pairs in @received, and %OK_RC (0) on success, negative on failure as return value.
  143. */
  144. int rc_acct(rc_handle *rh, uint32_t client_port, VALUE_PAIR *send)
  145. {
  146. return rc_aaa(rh, client_port, send, NULL, NULL, 1, PW_ACCOUNTING_REQUEST);
  147. }
  148. /** Builds an accounting request with the value_pairs at send
  149. *
  150. * @param rh a handle to parsed configuration.
  151. * @param send a #VALUE_PAIR array of values (e.g., %PW_USER_NAME).
  152. * @return %OK_RC (0) on success, negative on failure as return value.
  153. */
  154. int rc_acct_proxy(rc_handle *rh, VALUE_PAIR *send)
  155. {
  156. return rc_aaa(rh, 0, send, NULL, NULL, 0, PW_ACCOUNTING_REQUEST);
  157. }