radembedded.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * radembedded.c - a sample c program showing how to embed the configuration of a radius
  3. * client, using the FreeRADIUS Client Library without an external configuration file.
  4. */
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <syslog.h>
  8. #include <freeradius-client.h>
  9. int
  10. main (int argc, char **argv)
  11. {
  12. int retval = 0;
  13. rc_handle *rh = NULL;
  14. uint32_t client_port = 0;
  15. uint32_t status_type = PW_STATUS_STOP;
  16. VALUE_PAIR *send = NULL;
  17. char username[255] = "bob@somedomain.here";
  18. char callfrom[255] = "8475551212";
  19. char callto[255] = "8479630116";
  20. char myuuid[255] = "981743-asdf-90834klj234";
  21. /* Initialize the 'rh' structure */
  22. rh = rc_new();
  23. if (rh == NULL)
  24. {
  25. printf("ERROR: Failed to allocate initial structure\n");
  26. exit(1);
  27. }
  28. /* Initialize the config structure */
  29. rh = rc_config_init(rh);
  30. if (rh == NULL)
  31. {
  32. printf("ERROR: Failed to initialze configuration\n");
  33. exit(1);
  34. }
  35. /*
  36. * Set the required options for configuration
  37. */
  38. /* set auth_order to be radius only */
  39. if (rc_add_config(rh, "auth_order", "radius", "config", 0) != 0)
  40. {
  41. printf("ERROR: Unable to set auth_order.\n");
  42. rc_destroy(rh);
  43. exit(1);
  44. }
  45. if (rc_add_config(rh, "login_tries", "4", "config", 0) != 0)
  46. {
  47. printf("ERROR: Unable to set login_tries.\n");
  48. rc_destroy(rh);
  49. exit(1);
  50. }
  51. if (rc_add_config(rh, "dictionary", "/usr/local/radius/dictionary", "config", 0) != 0)
  52. {
  53. printf("ERROR: Unable to set dictionary.\n");
  54. rc_destroy(rh);
  55. exit(1);
  56. }
  57. if (rc_add_config(rh, "radius_retries", "3", "config", 0) != 0)
  58. {
  59. printf("ERROR: Unable to set radius_retries.\n");
  60. rc_destroy(rh);
  61. exit(1);
  62. }
  63. if (rc_add_config(rh, "radius_timeout", "5", "config", 0) != 0)
  64. {
  65. printf("ERROR: Unable to set radius_timeout.\n");
  66. rc_destroy(rh);
  67. exit(1);
  68. }
  69. /* auth/acct servers are added in the form: host[:port[:secret]]
  70. * If you don't set the secret via the add_config option, you must set a 'servers'
  71. * entry to specify the location of the 'servers' file which stores the secrets to
  72. * be used.
  73. */
  74. if (rc_add_config(rh, "authserver", "localhost::testing123", "config", 0) != 0)
  75. {
  76. printf("ERROR: Unable to set authserver.\n");
  77. rc_destroy(rh);
  78. exit(1);
  79. }
  80. if (rc_add_config(rh, "acctserver", "localhost:1813:testing123", "config", 0) != 0)
  81. {
  82. printf("ERROR: Unable to set acctserver.\n");
  83. rc_destroy(rh);
  84. exit(1);
  85. }
  86. /* Done setting configuration items */
  87. /* Read in the dictionary file(s) */
  88. if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary")) != 0)
  89. {
  90. printf("ERROR: Failed to initialize radius dictionary\n");
  91. exit(1);
  92. }
  93. if (rc_avpair_add(rh, &send, PW_ACCT_STATUS_TYPE, &status_type, -1, 0) == NULL)
  94. {
  95. printf("ERROR: Failed adding Acct-Status-Type: to %d\n", status_type);
  96. exit(1);
  97. }
  98. if (rc_avpair_add(rh, &send, PW_ACCT_SESSION_ID, myuuid, -1, 0) == NULL)
  99. {
  100. printf("ERROR: Failed adding Acct-Session-ID: to %s\n", myuuid);
  101. exit(1);
  102. }
  103. if (rc_avpair_add(rh, &send, PW_USER_NAME, username, -1, 0) == NULL)
  104. {
  105. printf("ERROR: Failed adding User-Name: to %s\n", username);
  106. exit(1);
  107. }
  108. if (rc_avpair_add(rh, &send, PW_CALLED_STATION_ID, callto, -1, 0) == NULL)
  109. {
  110. printf("ERROR: Failed adding Called-Station-ID: to %s\n", callto);
  111. exit(1);
  112. }
  113. if (rc_avpair_add(rh, &send, PW_CALLING_STATION_ID, callfrom, -1, 0) == NULL)
  114. {
  115. printf("ERROR: Failed adding Calling-Station-ID: to %s\n", callfrom);
  116. exit(1);
  117. }
  118. if(rc_acct(rh, client_port, send) == OK_RC)
  119. {
  120. printf("INFO: Accounting OK: %s\n", username);
  121. retval = 0;
  122. }
  123. else
  124. {
  125. printf("INFO: Accounting Failed: %s\n", username);
  126. retval = -1;
  127. }
  128. rc_destroy(rh);
  129. rc_avpair_free(send);
  130. exit(retval);
  131. }