radius_example.c 3.7 KB

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