radius_example.c 3.5 KB

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