radius_example.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Example application using RADIUS client as a library
  3. * Copyright (c) 2007, 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 "eloop.h"
  11. #include "radius/radius.h"
  12. #include "radius/radius_client.h"
  13. extern int wpa_debug_level;
  14. struct radius_ctx {
  15. struct radius_client_data *radius;
  16. struct hostapd_radius_servers conf;
  17. u8 radius_identifier;
  18. struct in_addr own_ip_addr;
  19. };
  20. static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
  21. int level, const char *txt, size_t len)
  22. {
  23. printf("%s\n", txt);
  24. }
  25. /* Process the RADIUS frames from Authentication Server */
  26. static RadiusRxResult receive_auth(struct radius_msg *msg,
  27. struct radius_msg *req,
  28. const u8 *shared_secret,
  29. size_t shared_secret_len,
  30. void *data)
  31. {
  32. /* struct radius_ctx *ctx = data; */
  33. printf("Received RADIUS Authentication message; code=%d\n",
  34. radius_msg_get_hdr(msg)->code);
  35. /* We're done for this example, so request eloop to terminate. */
  36. eloop_terminate();
  37. return RADIUS_RX_PROCESSED;
  38. }
  39. static void start_example(void *eloop_ctx, void *timeout_ctx)
  40. {
  41. struct radius_ctx *ctx = eloop_ctx;
  42. struct radius_msg *msg;
  43. printf("Sending a RADIUS authentication message\n");
  44. ctx->radius_identifier = radius_client_get_id(ctx->radius);
  45. msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
  46. ctx->radius_identifier);
  47. if (msg == NULL) {
  48. printf("Could not create net RADIUS packet\n");
  49. return;
  50. }
  51. radius_msg_make_authenticator(msg, (u8 *) ctx, sizeof(*ctx));
  52. if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
  53. (u8 *) "user", 4)) {
  54. printf("Could not add User-Name\n");
  55. radius_msg_free(msg);
  56. return;
  57. }
  58. if (!radius_msg_add_attr_user_password(
  59. msg, (u8 *) "password", 8,
  60. ctx->conf.auth_server->shared_secret,
  61. ctx->conf.auth_server->shared_secret_len)) {
  62. printf("Could not add User-Password\n");
  63. radius_msg_free(msg);
  64. return;
  65. }
  66. if (!radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
  67. (u8 *) &ctx->own_ip_addr, 4)) {
  68. printf("Could not add NAS-IP-Address\n");
  69. radius_msg_free(msg);
  70. return;
  71. }
  72. if (radius_client_send(ctx->radius, msg, RADIUS_AUTH, NULL) < 0)
  73. radius_msg_free(msg);
  74. }
  75. int main(int argc, char *argv[])
  76. {
  77. struct radius_ctx ctx;
  78. struct hostapd_radius_server *srv;
  79. if (os_program_init())
  80. return -1;
  81. hostapd_logger_register_cb(hostapd_logger_cb);
  82. os_memset(&ctx, 0, sizeof(ctx));
  83. inet_aton("127.0.0.1", &ctx.own_ip_addr);
  84. if (eloop_init()) {
  85. printf("Failed to initialize event loop\n");
  86. return -1;
  87. }
  88. srv = os_zalloc(sizeof(*srv));
  89. if (srv == NULL)
  90. return -1;
  91. srv->addr.af = AF_INET;
  92. srv->port = 1812;
  93. if (hostapd_parse_ip_addr("127.0.0.1", &srv->addr) < 0) {
  94. printf("Failed to parse IP address\n");
  95. return -1;
  96. }
  97. srv->shared_secret = (u8 *) os_strdup("radius");
  98. srv->shared_secret_len = 6;
  99. ctx.conf.auth_server = ctx.conf.auth_servers = srv;
  100. ctx.conf.num_auth_servers = 1;
  101. ctx.conf.msg_dumps = 1;
  102. ctx.radius = radius_client_init(&ctx, &ctx.conf);
  103. if (ctx.radius == NULL) {
  104. printf("Failed to initialize RADIUS client\n");
  105. return -1;
  106. }
  107. if (radius_client_register(ctx.radius, RADIUS_AUTH, receive_auth,
  108. &ctx) < 0) {
  109. printf("Failed to register RADIUS authentication handler\n");
  110. return -1;
  111. }
  112. eloop_register_timeout(0, 0, start_example, &ctx, NULL);
  113. eloop_run();
  114. radius_client_deinit(ctx.radius);
  115. os_free(srv->shared_secret);
  116. os_free(srv);
  117. eloop_destroy();
  118. os_program_deinit();
  119. return 0;
  120. }