radiusclient.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2004 Maxim Sobolev <sobomax@FreeBSD.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * $Id: radiusclient.c,v 1.8 2010/02/04 10:30:26 aland Exp $
  27. */
  28. #include <ctype.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <freeradius-client.h>
  34. #define BUF_LEN 4096
  35. int process(void *, VALUE_PAIR *, int, int);
  36. static void
  37. usage(void)
  38. {
  39. fprintf(stderr, "usage: radiusclient [-f config_file] [-p nas_port] [-s | [-a] a1=v1 [a2=v2[...[aN=vN]...]]]\n");
  40. exit(1);
  41. }
  42. int
  43. main(int argc, char **argv)
  44. {
  45. int i, nas_port, ch, acct, server, ecount, firstline, theend;
  46. void *rh;
  47. size_t len;
  48. VALUE_PAIR *send, **vp;
  49. char *rc_conf, *cp;
  50. char lbuf[4096];
  51. rc_conf = RC_CONFIG_FILE;
  52. nas_port = 5060;
  53. acct = 0;
  54. server = 0;
  55. while ((ch = getopt(argc, argv, "af:p:s")) != -1) {
  56. switch (ch) {
  57. case 'f':
  58. rc_conf = optarg;
  59. break;
  60. case 'p':
  61. nas_port = atoi(optarg);
  62. break;
  63. case 'a':
  64. acct = 1;
  65. break;
  66. case 's':
  67. server = 1;
  68. break;
  69. default:
  70. usage();
  71. }
  72. }
  73. argc -= optind;
  74. argv += optind;
  75. if ((argc == 0 && server == 0) || (argc != 0 && server != 0))
  76. usage();
  77. if ((rh = rc_read_config(rc_conf)) == NULL) {
  78. fprintf(stderr, "error opening radius configuration file\n");
  79. exit(1);
  80. }
  81. if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary")) != 0) {
  82. fprintf(stderr, "error reading radius dictionary\n");
  83. exit(2);
  84. }
  85. if (server == 0) {
  86. send = NULL;
  87. vp = &send;
  88. for (i = 0; i < argc; i++) {
  89. if (rc_avpair_parse(rh, argv[i], vp) < 0) {
  90. fprintf(stderr, "%s: can't parse AV pair\n", argv[i]);
  91. exit(3);
  92. }
  93. vp = &send->next;
  94. }
  95. exit(process(rh, send, acct, nas_port));
  96. }
  97. while (1 == 1) {
  98. send = NULL;
  99. vp = &send;
  100. ecount = 0;
  101. firstline = 1;
  102. acct = 0;
  103. do {
  104. len = 0;
  105. cp = rc_fgetln(stdin, &len);
  106. theend = 1;
  107. if (cp != NULL && len > 0) {
  108. if (firstline != 0) {
  109. if (len >= 4 && memcmp(cp, "ACCT", 4) == 0)
  110. acct = 1;
  111. firstline = 0;
  112. theend = 0;
  113. continue;
  114. }
  115. for (i = 0; i < len; i++) {
  116. if (!isspace(cp[i])) {
  117. theend = 0;
  118. break;
  119. }
  120. }
  121. if (theend == 0) {
  122. memcpy(lbuf, cp, len);
  123. lbuf[len] = '\0';
  124. if (rc_avpair_parse(rh, lbuf, vp) < 0) {
  125. fprintf(stderr, "%s: can't parse AV pair\n", lbuf);
  126. ecount++;
  127. } else {
  128. vp = &send->next;
  129. }
  130. }
  131. }
  132. } while (theend == 0);
  133. if (send != NULL && ecount == 0)
  134. printf("%d\n\n", process(rh, send, acct, nas_port));
  135. else
  136. printf("%d\n\n", -1);
  137. fflush(stdout);
  138. if (send != NULL)
  139. rc_avpair_free(send);
  140. if (cp == NULL || len == 0)
  141. break;
  142. }
  143. exit(0);
  144. }
  145. int
  146. process(void *rh, VALUE_PAIR *send, int acct, int nas_port)
  147. {
  148. VALUE_PAIR *received;
  149. char msg[PW_MAX_MSG_SIZE];
  150. char buf[BUF_LEN];
  151. int i;
  152. received = NULL;
  153. if (acct == 0) {
  154. i = rc_auth(rh, nas_port, send, &received, msg);
  155. if (received != NULL) {
  156. printf("%s", rc_avpair_log(rh, received, buf, BUF_LEN));
  157. rc_avpair_free(received);
  158. }
  159. } else {
  160. i = rc_acct(rh, nas_port, send);
  161. }
  162. return (i == OK_RC) ? 0 : 1;
  163. }