radexample.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * $Id: radexample.c,v 1.8 2007/07/11 17:29:30 cparker Exp $
  3. *
  4. * Copyright (C) 1995,1996,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. static char rcsid[] =
  12. "$Id: radexample.c,v 1.8 2007/07/11 17:29:30 cparker Exp $";
  13. #include <config.h>
  14. #include <includes.h>
  15. #include <freeradius-client.h>
  16. #include <pathnames.h>
  17. static char *pname = NULL;
  18. int
  19. main (int argc, char **argv)
  20. {
  21. int result;
  22. char username[128];
  23. char passwd[AUTH_PASS_LEN + 1];
  24. VALUE_PAIR *send, *received;
  25. uint32_t service;
  26. char msg[PW_MAX_MSG_SIZE], username_realm[256];
  27. char *default_realm;
  28. rc_handle *rh;
  29. pname = (pname = strrchr(argv[0],'/'))?pname+1:argv[0];
  30. rc_openlog(pname);
  31. if ((rh = rc_read_config(RC_CONFIG_FILE)) == NULL)
  32. return ERROR_RC;
  33. if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary")) != 0)
  34. return ERROR_RC;
  35. default_realm = rc_conf_str(rh, "default_realm");
  36. strncpy(username, rc_getstr (rh, "login: ",1), sizeof(username));
  37. strncpy (passwd, rc_getstr(rh, "Password: ",0), sizeof (passwd));
  38. send = NULL;
  39. /*
  40. * Fill in User-Name
  41. */
  42. strncpy(username_realm, username, sizeof(username_realm));
  43. /* Append default realm */
  44. if ((strchr(username_realm, '@') == NULL) && default_realm &&
  45. (*default_realm != '\0'))
  46. {
  47. strncat(username_realm, "@", sizeof(username_realm)-strlen(username_realm)-1);
  48. strncat(username_realm, default_realm, sizeof(username_realm)-strlen(username_realm)-1);
  49. }
  50. if (rc_avpair_add(rh, &send, PW_USER_NAME, username_realm, -1, 0) == NULL)
  51. return ERROR_RC;
  52. /*
  53. * Fill in User-Password
  54. */
  55. if (rc_avpair_add(rh, &send, PW_USER_PASSWORD, passwd, -1, 0) == NULL)
  56. return ERROR_RC;
  57. /*
  58. * Fill in Service-Type
  59. */
  60. service = PW_AUTHENTICATE_ONLY;
  61. if (rc_avpair_add(rh, &send, PW_SERVICE_TYPE, &service, -1, 0) == NULL)
  62. return ERROR_RC;
  63. result = rc_auth(rh, 0, send, &received, msg);
  64. if (result == OK_RC)
  65. {
  66. fprintf(stderr, "\"%s\" RADIUS Authentication OK\n", username);
  67. }
  68. else
  69. {
  70. fprintf(stderr, "\"%s\" RADIUS Authentication failure (RC=%i)\n", username, result);
  71. }
  72. return result;
  73. }