sendserver.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*
  2. * $Id: sendserver.c,v 1.30 2010/06/15 09:22:52 aland Exp $
  3. *
  4. * Copyright (C) 1995,1996,1997 Lars Fenneberg
  5. *
  6. * Copyright 1992 Livingston Enterprises, Inc.
  7. *
  8. * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
  9. * and Merit Network, Inc. All Rights Reserved
  10. *
  11. * See the file COPYRIGHT for the respective terms and conditions.
  12. * If the file is missing contact me at lf@elemental.net
  13. * and I'll send you a copy.
  14. *
  15. */
  16. //#include <poll.h>
  17. #include <radius_config.h>
  18. #include <includes.h>
  19. #include <freeradius-client.h>
  20. #include <pathnames.h>
  21. #include "util.h"
  22. #include "radius_user.h"
  23. #include "lwip/sockets.h"
  24. #define SA(p) ((struct sockaddr *)(p))
  25. static void rc_random_vector (unsigned char *);
  26. static int rc_check_reply (AUTH_HDR *, int, char const *, unsigned char const *, unsigned char);
  27. /** Packs an attribute value pair list into a buffer
  28. *
  29. * @param vp a pointer to a #VALUE_PAIR.
  30. * @param secret the secret used by the server.
  31. * @param auth a pointer to #AUTH_HDR.
  32. * @return The number of octets packed.
  33. */
  34. static int rc_pack_list (VALUE_PAIR *vp, char *secret, AUTH_HDR *auth)
  35. {
  36. int length, i, pc, padded_length;
  37. int total_length = 0;
  38. size_t secretlen;
  39. uint32_t lvalue, vendor;
  40. unsigned char passbuf[MAX(AUTH_PASS_LEN, CHAP_VALUE_LENGTH)];
  41. unsigned char md5buf[256];
  42. unsigned char *buf, *vector, *vsa_length_ptr;
  43. buf = auth->data;
  44. while (vp != NULL)
  45. {
  46. vsa_length_ptr = NULL;
  47. if (VENDOR(vp->attribute) != 0) {
  48. *buf++ = PW_VENDOR_SPECIFIC;
  49. vsa_length_ptr = buf;
  50. *buf++ = 6;
  51. vendor = htonl(VENDOR(vp->attribute));
  52. memcpy(buf, &vendor, sizeof(uint32_t));
  53. buf += 4;
  54. total_length += 6;
  55. }
  56. *buf++ = (vp->attribute & 0xff);
  57. switch (vp->attribute)
  58. {
  59. case PW_USER_PASSWORD:
  60. /* Encrypt the password */
  61. /* Chop off password at AUTH_PASS_LEN */
  62. length = vp->lvalue;
  63. if (length > AUTH_PASS_LEN)
  64. length = AUTH_PASS_LEN;
  65. /* Calculate the padded length */
  66. padded_length = (length+(AUTH_VECTOR_LEN-1)) & ~(AUTH_VECTOR_LEN-1);
  67. /* Record the attribute length */
  68. *buf++ = padded_length + 2;
  69. if (vsa_length_ptr != NULL) *vsa_length_ptr += padded_length + 2;
  70. /* Pad the password with zeros */
  71. memset ((char *) passbuf, '\0', AUTH_PASS_LEN);
  72. memcpy ((char *) passbuf, vp->strvalue, (size_t) length);
  73. secretlen = strlen (secret);
  74. vector = (unsigned char *)auth->vector;
  75. for(i = 0; i < padded_length; i += AUTH_VECTOR_LEN)
  76. {
  77. /* Calculate the MD5 digest*/
  78. strcpy ((char *) md5buf, secret);
  79. memcpy ((char *) md5buf + secretlen, vector,
  80. AUTH_VECTOR_LEN);
  81. rc_md5_calc (buf, md5buf, secretlen + AUTH_VECTOR_LEN);
  82. /* Remeber the start of the digest */
  83. vector = buf;
  84. /* Xor the password into the MD5 digest */
  85. for (pc = i; pc < (i + AUTH_VECTOR_LEN); pc++)
  86. {
  87. *buf++ ^= passbuf[pc];
  88. }
  89. }
  90. total_length += padded_length + 2;
  91. break;
  92. default:
  93. switch (vp->type)
  94. {
  95. case PW_TYPE_STRING:
  96. length = vp->lvalue;
  97. *buf++ = length + 2;
  98. if (vsa_length_ptr != NULL) *vsa_length_ptr += length + 2;
  99. memcpy (buf, vp->strvalue, (size_t) length);
  100. buf += length;
  101. total_length += length + 2;
  102. break;
  103. case PW_TYPE_IPV6ADDR:
  104. length = 16;
  105. *buf++ = length + 2;
  106. if (vsa_length_ptr != NULL) *vsa_length_ptr += length + 2;
  107. memcpy (buf, vp->strvalue, (size_t) length);
  108. buf += length;
  109. total_length += length + 2;
  110. break;
  111. case PW_TYPE_IPV6PREFIX:
  112. length = vp->lvalue;
  113. *buf++ = length + 2;
  114. if (vsa_length_ptr != NULL) *vsa_length_ptr += length + 2;
  115. memcpy (buf, vp->strvalue, (size_t) length);
  116. buf += length;
  117. total_length += length + 2;
  118. break;
  119. case PW_TYPE_INTEGER:
  120. case PW_TYPE_IPADDR:
  121. case PW_TYPE_DATE:
  122. *buf++ = sizeof (uint32_t) + 2;
  123. if (vsa_length_ptr != NULL) *vsa_length_ptr += sizeof(uint32_t) + 2;
  124. lvalue = htonl (vp->lvalue);
  125. memcpy (buf, (char *) &lvalue, sizeof (uint32_t));
  126. buf += sizeof (uint32_t);
  127. total_length += sizeof (uint32_t) + 2;
  128. break;
  129. default:
  130. break;
  131. }
  132. break;
  133. }
  134. vp = vp->next;
  135. }
  136. return total_length;
  137. }
  138. /** Appends a string to the provided buffer
  139. *
  140. * @param dest the destination buffer.
  141. * @param max_size the maximum size available in the destination buffer.
  142. * @param pos the current position in the dest buffer; initially must be zero.
  143. * @param src the source buffer to append.
  144. */
  145. static void strappend(char *dest, unsigned max_size, int *pos, const char *src)
  146. {
  147. unsigned len = strlen(src) + 1;
  148. if (*pos == -1)
  149. return;
  150. if (len + *pos > max_size) {
  151. *pos = -1;
  152. return;
  153. }
  154. memcpy(&dest[*pos], src, len);
  155. *pos += len-1;
  156. return;
  157. }
  158. /** Sends a request to a RADIUS server and waits for the reply
  159. *
  160. * @param rh a handle to parsed configuration
  161. * @param data a pointer to a #SEND_DATA structure
  162. * @param msg must be an array of %PW_MAX_MSG_SIZE or %NULL; will contain the concatenation of
  163. * any %PW_REPLY_MESSAGE received.
  164. * @param flags must be %AUTH or %ACCT
  165. * @return %OK_RC (0) on success, %TIMEOUT_RC on timeout %REJECT_RC on acess reject, or negative
  166. * on failure as return value.
  167. */
  168. #define RS_PORT_NUM 1812
  169. //#define RS_IP_ADDR "192.168.1.2"
  170. #define RS_IP_ADDR "192.168.14.234"
  171. #define DEVICE_PORT_NUM 1812
  172. //#define DEVICE_IP_ADDR "192.168.1.6"
  173. #define DEVICE_IP_ADDR "192.168.14.37"
  174. #define BUF_LEN 300
  175. static uint8_t send_buffer[BUF_LEN];
  176. static uint8_t recv_buffer[BUF_LEN];
  177. int rc_send_server (rc_handle *rh, SEND_DATA *data, char *msg, unsigned flags)
  178. {
  179. #if 1
  180. struct sockaddr_in sa,ra;
  181. int socket;
  182. fdsets sets;
  183. AUTH_HDR* auth;
  184. AUTH_HDR* recv_auth;
  185. unsigned char vector[AUTH_VECTOR_LEN];
  186. char secret[MAX_SECRET_LENGTH + 1];
  187. int total_length;
  188. int sendLen, recvLen;
  189. int length;
  190. int pos;
  191. uint8_t* attr;
  192. int result = 0;
  193. VALUE_PAIR* vp;
  194. initFdsets(&sets);
  195. if(data->secret != NULL) {
  196. strlcpy(secret, data->secret, MAX_SECRET_LENGTH);
  197. }
  198. memset(&ra, 0, sizeof(struct sockaddr_in));
  199. ra.sin_family = AF_INET;
  200. ra.sin_addr.s_addr = inet_addr(RS_IP_ADDR);
  201. ra.sin_port = htons(RS_PORT_NUM);
  202. socket = socket(PF_INET, SOCK_DGRAM, 0);
  203. if ( socket < 0 )
  204. {
  205. printf("socket call failed");
  206. return -1;
  207. }
  208. // TODO bind?
  209. // Build a request (PW_ACCESS_REQUEST)
  210. auth = (AUTH_HDR *) send_buffer;
  211. auth->code = data->code;
  212. auth->id = data->seq_nbr;
  213. rc_random_vector(vector);
  214. memcpy ((char *) auth->vector, (char *) vector, AUTH_VECTOR_LEN);
  215. total_length = rc_pack_list(data->send_pairs, secret, auth) + AUTH_HDR_LEN;
  216. auth->length = htons ((unsigned short) total_length);
  217. // Bind socket
  218. memset(&sa, 0, sizeof(struct sockaddr_in));
  219. sa.sin_family = AF_INET;
  220. sa.sin_addr.s_addr = inet_addr(DEVICE_IP_ADDR);
  221. sa.sin_port = htons(DEVICE_PORT_NUM);
  222. if (bind(socket, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) == -1)
  223. {
  224. printf("Bind to Port Number %d ,IP address %s failed\n", DEVICE_PORT_NUM, DEVICE_IP_ADDR);
  225. close(socket);
  226. return 0;
  227. }
  228. sendLen = sendto(socket, (char*)auth, total_length, 0, (struct sockaddr*)&ra, sizeof(ra));
  229. if(sendLen < 0)
  230. {
  231. printf("send failed\n");
  232. close(socket);
  233. return 0;
  234. }
  235. // Получение ответа, select
  236. if (!recvSelect(&sets, &socket, 2000)) {
  237. //timeCount = HAL_GetTick() - timeCount;
  238. printf("SOCK recv timeout!\r\n");
  239. close(socket);
  240. return 0;
  241. }
  242. // Данные можно принимать
  243. socklen_t sl = sizeof(sa);
  244. recvLen = recvfrom(socket, recv_buffer, BUF_LEN, 0, (struct sockaddr*)&ra, &sl);
  245. recv_auth = (AUTH_HDR*)recv_buffer;
  246. // Проверки размера входящего сообщения
  247. if (recvLen < AUTH_HDR_LEN || recvLen < ntohs(recv_auth->length)) {
  248. printf("radius_server: reply is too short\r\n");
  249. close(socket);
  250. return 0;
  251. }
  252. if (recvLen > ntohs(recv_auth->length))
  253. {
  254. recvLen = ntohs(recv_auth->length);
  255. }
  256. // Verify that it's a valid RADIUS packet before doing ANYTHING with it.
  257. attr = recv_buffer + AUTH_HDR_LEN;
  258. while (attr < (recv_buffer + recvLen)) {
  259. if (attr[0] == 0) {
  260. printf("radius_server: attribute zero is invalid\r\n");
  261. close(socket);
  262. return 0;
  263. }
  264. if (attr[1] < 2) {
  265. printf("radius_server: attribute length is too small\r\n");
  266. close(socket);
  267. return 0;
  268. }
  269. if ((attr + attr[1]) > (recv_buffer + recvLen)) {
  270. printf("radius_server: attribute overflows the packet\r\n");
  271. close(socket);
  272. return 0;
  273. }
  274. attr += attr[1];
  275. }
  276. result = rc_check_reply (recv_auth, BUF_LEN, secret, vector, data->seq_nbr);
  277. length = ntohs(recv_auth->length) - AUTH_HDR_LEN;
  278. if (length > 0) {
  279. data->receive_pairs = rc_avpair_gen(rh, NULL, recv_auth->data, length, 0);
  280. } else {
  281. data->receive_pairs = NULL;
  282. }
  283. if (result != OK_RC) {
  284. return result;
  285. }
  286. if (msg) {
  287. *msg = '\0';
  288. pos = 0;
  289. vp = data->receive_pairs;
  290. while (vp)
  291. {
  292. if ((vp = rc_avpair_get(vp, PW_REPLY_MESSAGE, 0)))
  293. {
  294. strappend(msg, PW_MAX_MSG_SIZE, &pos, vp->strvalue);
  295. strappend(msg, PW_MAX_MSG_SIZE, &pos, "\n");
  296. vp = vp->next;
  297. }
  298. }
  299. }
  300. if ((recv_auth->code == PW_ACCESS_ACCEPT) ||
  301. (recv_auth->code == PW_PASSWORD_ACK) ||
  302. (recv_auth->code == PW_ACCOUNTING_RESPONSE))
  303. {
  304. result = OK_RC;
  305. }
  306. else if ((recv_auth->code == PW_ACCESS_REJECT) ||
  307. (recv_auth->code == PW_PASSWORD_REJECT))
  308. {
  309. result = REJECT_RC;
  310. }
  311. else
  312. {
  313. rc_log(LOG_ERR, "rc_send_server: received RADIUS server response neither ACCEPT nor REJECT, invalid");
  314. result = BADRESP_RC;
  315. }
  316. printf("Radius server end communication\r\n");
  317. close(socket);
  318. return result;
  319. //getnameinfo(SA(&our_sockaddr), SS_LEN(&our_sockaddr), NULL, 0, our_addr_txt, sizeof(our_addr_txt), NI_NUMERICHOST);
  320. //getnameinfo(auth_addr->ai_addr, auth_addr->ai_addrlen, NULL, 0, auth_addr_txt, sizeof(auth_addr_txt), NI_NUMERICHOST);
  321. #endif
  322. #if 0
  323. int sockfd;
  324. AUTH_HDR *auth, *recv_auth;
  325. char *server_name; /* Name of server to query */
  326. struct sockaddr_storage our_sockaddr;
  327. struct addrinfo *auth_addr = NULL;
  328. socklen_t salen;
  329. int result = 0;
  330. int total_length;
  331. int length, pos;
  332. int retry_max;
  333. unsigned discover_local_ip;
  334. size_t secretlen;
  335. char secret[MAX_SECRET_LENGTH + 1];
  336. unsigned char vector[AUTH_VECTOR_LEN];
  337. uint8_t recv_buffer[BUFFER_LEN];
  338. uint8_t send_buffer[BUFFER_LEN];
  339. char our_addr_txt[50]; /* hold a text IP */
  340. char auth_addr_txt[50]; /* hold a text IP */
  341. uint8_t *attr;
  342. int retries;
  343. VALUE_PAIR *vp;
  344. //struct pollfd pfd;
  345. double start_time, timeout;
  346. server_name = data->server;
  347. if (server_name == NULL || server_name[0] == '\0')
  348. return ERROR_RC;
  349. if(data->secret != NULL)
  350. {
  351. //strlcpy(secret, data->secret, MAX_SECRET_LENGTH);
  352. memcpy(secret, data->secret, MAX_SECRET_LENGTH);
  353. }
  354. if (rc_find_server_addr (rh, server_name, &auth_addr, secret, flags) != 0)
  355. {
  356. rc_log(LOG_ERR, "rc_send_server: unable to find server: %s", server_name);
  357. return ERROR_RC;
  358. }
  359. rc_own_bind_addr(rh, &our_sockaddr);
  360. discover_local_ip = 0;
  361. if (our_sockaddr.ss_family == AF_INET) {
  362. if (((struct sockaddr_in*)(&our_sockaddr))->sin_addr.s_addr == INADDR_ANY) {
  363. discover_local_ip = 1;
  364. }
  365. }
  366. DEBUG(LOG_ERR, "DEBUG: rc_send_server: creating socket to: %s", server_name);
  367. if (discover_local_ip) {
  368. result = rc_get_srcaddr(SA(&our_sockaddr), auth_addr->ai_addr);
  369. if (result != 0) {
  370. memset (secret, '\0', sizeof (secret));
  371. rc_log(LOG_ERR, "rc_send_server: cannot figure our own address");
  372. result = ERROR_RC;
  373. goto cleanup;
  374. }
  375. }
  376. sockfd = socket (our_sockaddr.ss_family, SOCK_DGRAM, 0);
  377. if (sockfd < 0)
  378. {
  379. memset (secret, '\0', sizeof (secret));
  380. rc_log(LOG_ERR, "rc_send_server: socket: %s", strerror(errno));
  381. result = ERROR_RC;
  382. goto cleanup;
  383. }
  384. if (our_sockaddr.ss_family == AF_INET)
  385. ((struct sockaddr_in*)&our_sockaddr)->sin_port = 0;
  386. else
  387. ((struct sockaddr_in6*)&our_sockaddr)->sin6_port = 0;
  388. if (bind(sockfd, SA(&our_sockaddr), SS_LEN(&our_sockaddr)) < 0)
  389. {
  390. close (sockfd);
  391. memset (secret, '\0', sizeof (secret));
  392. rc_log(LOG_ERR, "rc_send_server: bind: %s: %s", server_name, strerror(errno));
  393. result = ERROR_RC;
  394. goto cleanup;
  395. }
  396. retry_max = data->retries; /* Max. numbers to try for reply */
  397. retries = 0; /* Init retry cnt for blocking call */
  398. if (data->svc_port) {
  399. if (our_sockaddr.ss_family == AF_INET)
  400. ((struct sockaddr_in*)auth_addr->ai_addr)->sin_port = htons ((unsigned short) data->svc_port);
  401. else
  402. ((struct sockaddr_in6*)auth_addr->ai_addr)->sin6_port = htons ((unsigned short) data->svc_port);
  403. }
  404. /*
  405. * Fill in NAS-IP-Address (if needed)
  406. */
  407. if (rc_avpair_get(data->send_pairs, PW_NAS_IP_ADDRESS, 0) == NULL &&
  408. rc_avpair_get(data->send_pairs, PW_NAS_IPV6_ADDRESS, 0) == NULL) {
  409. if (our_sockaddr.ss_family == AF_INET) {
  410. uint32_t ip;
  411. ip = *((uint32_t*)(&((struct sockaddr_in*)&our_sockaddr)->sin_addr));
  412. ip = ntohl(ip);
  413. rc_avpair_add(rh, &(data->send_pairs), PW_NAS_IP_ADDRESS,
  414. &ip, 0, 0);
  415. } else {
  416. void *p;
  417. p = &((struct sockaddr_in6*)&our_sockaddr)->sin6_addr;
  418. rc_avpair_add(rh, &(data->send_pairs), PW_NAS_IPV6_ADDRESS,
  419. p, 0, 0);
  420. }
  421. }
  422. /* Build a request */
  423. auth = (AUTH_HDR *) send_buffer;
  424. auth->code = data->code;
  425. auth->id = data->seq_nbr;
  426. if (data->code == PW_ACCOUNTING_REQUEST)
  427. {
  428. total_length = rc_pack_list(data->send_pairs, secret, auth) + AUTH_HDR_LEN;
  429. auth->length = htons ((unsigned short) total_length);
  430. memset((char *) auth->vector, 0, AUTH_VECTOR_LEN);
  431. secretlen = strlen (secret);
  432. memcpy ((char *) auth + total_length, secret, secretlen);
  433. rc_md5_calc (vector, (unsigned char *) auth, total_length + secretlen);
  434. memcpy ((char *) auth->vector, (char *) vector, AUTH_VECTOR_LEN);
  435. }
  436. else
  437. {
  438. rc_random_vector (vector);
  439. memcpy ((char *) auth->vector, (char *) vector, AUTH_VECTOR_LEN);
  440. total_length = rc_pack_list(data->send_pairs, secret, auth) + AUTH_HDR_LEN;
  441. auth->length = htons ((unsigned short) total_length);
  442. }
  443. getnameinfo(SA(&our_sockaddr), SS_LEN(&our_sockaddr), NULL, 0, our_addr_txt, sizeof(our_addr_txt), NI_NUMERICHOST);
  444. getnameinfo(auth_addr->ai_addr, auth_addr->ai_addrlen, NULL, 0, auth_addr_txt, sizeof(auth_addr_txt), NI_NUMERICHOST);
  445. DEBUG(LOG_ERR, "DEBUG: local %s : 0, remote %s : %u\n",
  446. our_addr_txt, auth_addr_txt, data->svc_port);
  447. for (;;)
  448. {
  449. do {
  450. result = sendto (sockfd, (char *) auth, (unsigned int)total_length,
  451. (int) 0, SA(auth_addr->ai_addr), auth_addr->ai_addrlen);
  452. } while (result == -1 && errno == EINTR);
  453. if (result == -1) {
  454. rc_log(LOG_ERR, "%s: socket: %s", __FUNCTION__, strerror(errno));
  455. }
  456. pfd.fd = sockfd;
  457. pfd.events = POLLIN;
  458. pfd.revents = 0;
  459. start_time = rc_getctime();
  460. for (timeout = data->timeout; timeout > 0;
  461. timeout -= rc_getctime() - start_time) {
  462. result = poll(&pfd, 1, timeout * 1000);
  463. if (result != -1 || errno != EINTR)
  464. break;
  465. }
  466. if (result == -1)
  467. {
  468. rc_log(LOG_ERR, "rc_send_server: poll: %s", strerror(errno));
  469. memset (secret, '\0', sizeof (secret));
  470. close (sockfd);
  471. result = ERROR_RC;
  472. goto cleanup;
  473. }
  474. if (result == 1 && (pfd.revents & POLLIN) != 0)
  475. break;
  476. /*
  477. * Timed out waiting for response. Retry "retry_max" times
  478. * before giving up. If retry_max = 0, don't retry at all.
  479. */
  480. if (retries++ >= retry_max)
  481. {
  482. rc_log(LOG_ERR,
  483. "rc_send_server: no reply from RADIUS server %s:%u",
  484. auth_addr_txt, data->svc_port);
  485. close (sockfd);
  486. memset (secret, '\0', sizeof (secret));
  487. result = TIMEOUT_RC;
  488. goto cleanup;
  489. }
  490. }
  491. salen = auth_addr->ai_addrlen;
  492. do {
  493. length = recvfrom (sockfd, (char *) recv_buffer,
  494. (int) sizeof (recv_buffer),
  495. (int) 0, SA(auth_addr->ai_addr), &salen);
  496. } while(length == -1 && errno == EINTR);
  497. if (length <= 0)
  498. {
  499. rc_log(LOG_ERR, "rc_send_server: recvfrom: %s:%d: %s", server_name,\
  500. data->svc_port, strerror(errno));
  501. close (sockfd);
  502. memset (secret, '\0', sizeof (secret));
  503. result = ERROR_RC;
  504. goto cleanup;
  505. }
  506. recv_auth = (AUTH_HDR *)recv_buffer;
  507. if (length < AUTH_HDR_LEN || length < ntohs(recv_auth->length)) {
  508. rc_log(LOG_ERR, "rc_send_server: recvfrom: %s:%d: reply is too short",
  509. server_name, data->svc_port);
  510. close(sockfd);
  511. memset(secret, '\0', sizeof(secret));
  512. result = ERROR_RC;
  513. goto cleanup;
  514. }
  515. /*
  516. * If UDP is larger than RADIUS, shorten it to RADIUS.
  517. */
  518. if (length > ntohs(recv_auth->length)) length = ntohs(recv_auth->length);
  519. /*
  520. * Verify that it's a valid RADIUS packet before doing ANYTHING with it.
  521. */
  522. attr = recv_buffer + AUTH_HDR_LEN;
  523. while (attr < (recv_buffer + length)) {
  524. if (attr[0] == 0) {
  525. rc_log(LOG_ERR, "rc_send_server: recvfrom: %s:%d: attribute zero is invalid",
  526. server_name, data->svc_port);
  527. close(sockfd);
  528. memset(secret, '\0', sizeof(secret));
  529. return ERROR_RC;
  530. }
  531. if (attr[1] < 2) {
  532. rc_log(LOG_ERR, "rc_send_server: recvfrom: %s:%d: attribute length is too small",
  533. server_name, data->svc_port);
  534. close(sockfd);
  535. memset(secret, '\0', sizeof(secret));
  536. return ERROR_RC;
  537. }
  538. if ((attr + attr[1]) > (recv_buffer + length)) {
  539. rc_log(LOG_ERR, "rc_send_server: recvfrom: %s:%d: attribute overflows the packet",
  540. server_name, data->svc_port);
  541. close(sockfd);
  542. memset(secret, '\0', sizeof(secret));
  543. return ERROR_RC;
  544. }
  545. attr += attr[1];
  546. }
  547. result = rc_check_reply (recv_auth, BUFFER_LEN, secret, vector, data->seq_nbr);
  548. length = ntohs(recv_auth->length) - AUTH_HDR_LEN;
  549. if (length > 0) {
  550. data->receive_pairs = rc_avpair_gen(rh, NULL, recv_auth->data,
  551. length, 0);
  552. } else {
  553. data->receive_pairs = NULL;
  554. }
  555. close (sockfd);
  556. memset (secret, '\0', sizeof (secret));
  557. if (result != OK_RC) {
  558. goto cleanup;
  559. }
  560. if (msg) {
  561. *msg = '\0';
  562. pos = 0;
  563. vp = data->receive_pairs;
  564. while (vp)
  565. {
  566. if ((vp = rc_avpair_get(vp, PW_REPLY_MESSAGE, 0)))
  567. {
  568. strappend(msg, PW_MAX_MSG_SIZE, &pos, vp->strvalue);
  569. strappend(msg, PW_MAX_MSG_SIZE, &pos, "\n");
  570. vp = vp->next;
  571. }
  572. }
  573. }
  574. if ((recv_auth->code == PW_ACCESS_ACCEPT) ||
  575. (recv_auth->code == PW_PASSWORD_ACK) ||
  576. (recv_auth->code == PW_ACCOUNTING_RESPONSE))
  577. {
  578. result = OK_RC;
  579. }
  580. else if ((recv_auth->code == PW_ACCESS_REJECT) ||
  581. (recv_auth->code == PW_PASSWORD_REJECT))
  582. {
  583. result = REJECT_RC;
  584. }
  585. else
  586. {
  587. rc_log(LOG_ERR, "rc_send_server: received RADIUS server response neither ACCEPT nor REJECT, invalid");
  588. result = BADRESP_RC;
  589. }
  590. cleanup:
  591. if (auth_addr)
  592. freeaddrinfo(auth_addr);
  593. return result;
  594. #endif
  595. }
  596. /** Verify items in returned packet
  597. *
  598. * @param auth a pointer to #AUTH_HDR.
  599. * @param bufferlen the available buffer length.
  600. * @param secret the secret used by the server.
  601. * @param vector a random vector of %AUTH_VECTOR_LEN.
  602. * @param seq_nbr a unique sequence number.
  603. * @return %OK_RC upon success, %BADRESP_RC if anything looks funny.
  604. */
  605. static int rc_check_reply (AUTH_HDR *auth, int bufferlen, char const *secret, unsigned char const *vector, uint8_t seq_nbr)
  606. {
  607. int secretlen;
  608. int totallen;
  609. unsigned char calc_digest[AUTH_VECTOR_LEN];
  610. unsigned char reply_digest[AUTH_VECTOR_LEN];
  611. #ifdef DIGEST_DEBUG
  612. uint8_t *ptr;
  613. #endif
  614. totallen = ntohs (auth->length);
  615. secretlen = (int)strlen (secret);
  616. /* Do sanity checks on packet length */
  617. if ((totallen < 20) || (totallen > 4096))
  618. {
  619. rc_log(LOG_ERR, "rc_check_reply: received RADIUS server response with invalid length");
  620. return BADRESP_RC;
  621. }
  622. /* Verify buffer space, should never trigger with current buffer size and check above */
  623. if ((totallen + secretlen) > bufferlen)
  624. {
  625. rc_log(LOG_ERR, "rc_check_reply: not enough buffer space to verify RADIUS server response");
  626. return BADRESP_RC;
  627. }
  628. /* Verify that id (seq. number) matches what we sent */
  629. if (auth->id != seq_nbr)
  630. {
  631. rc_log(LOG_ERR, "rc_check_reply: received non-matching id in RADIUS server response");
  632. return BADRESP_RC;
  633. }
  634. /* Verify the reply digest */
  635. memcpy ((char *) reply_digest, (char *) auth->vector, AUTH_VECTOR_LEN);
  636. memcpy ((char *) auth->vector, (char *) vector, AUTH_VECTOR_LEN);
  637. memcpy ((char *) auth + totallen, secret, secretlen);
  638. #ifdef DIGEST_DEBUG
  639. rc_log(LOG_ERR, "Calculating digest on:");
  640. for (ptr = (u_char *)auth; ptr < ((u_char *)auth) + totallen + secretlen; ptr += 32) {
  641. char buf[65];
  642. int i;
  643. buf[0] = '\0';
  644. for (i = 0; i < 32; i++) {
  645. if (ptr + i >= ((u_char *)auth) + totallen + secretlen)
  646. break;
  647. sprintf(buf + i * 2, "%.2X", ptr[i]);
  648. }
  649. rc_log(LOG_ERR, " %s", buf);
  650. }
  651. #endif
  652. rc_md5_calc (calc_digest, (unsigned char *) auth, totallen + secretlen);
  653. #ifdef DIGEST_DEBUG
  654. rc_log(LOG_ERR, "Calculated digest is:");
  655. for (ptr = (u_char *)calc_digest; ptr < ((u_char *)calc_digest) + 16; ptr += 32) {
  656. char buf[65];
  657. int i;
  658. buf[0] = '\0';
  659. for (i = 0; i < 32; i++) {
  660. if (ptr + i >= ((u_char *)calc_digest) + 16)
  661. break;
  662. sprintf(buf + i * 2, "%.2X", ptr[i]);
  663. }
  664. rc_log(LOG_ERR, " %s", buf);
  665. }
  666. rc_log(LOG_ERR, "Reply digest is:");
  667. for (ptr = (u_char *)reply_digest; ptr < ((u_char *)reply_digest) + 16; ptr += 32) {
  668. char buf[65];
  669. int i;
  670. buf[0] = '\0';
  671. for (i = 0; i < 32; i++) {
  672. if (ptr + i >= ((u_char *)reply_digest) + 16)
  673. break;
  674. sprintf(buf + i * 2, "%.2X", ptr[i]);
  675. }
  676. rc_log(LOG_ERR, " %s", buf);
  677. }
  678. #endif
  679. if (memcmp ((char *) reply_digest, (char *) calc_digest,
  680. AUTH_VECTOR_LEN) != 0)
  681. {
  682. rc_log(LOG_ERR, "rc_check_reply: received invalid reply digest from RADIUS server");
  683. return BADRESP_RC;
  684. }
  685. return OK_RC;
  686. }
  687. /** Generates a random vector of AUTH_VECTOR_LEN octets
  688. *
  689. * @param vector a buffer with at least %AUTH_VECTOR_LEN bytes.
  690. */
  691. static void rc_random_vector (unsigned char *vector)
  692. {
  693. int randno;
  694. int i;
  695. #if defined(HAVE_GETENTROPY)
  696. if (getentropy(vector, AUTH_VECTOR_LEN) >= 0) {
  697. return;
  698. } /* else fall through */
  699. #elif defined(HAVE_DEV_URANDOM)
  700. int fd;
  701. /* well, I added this to increase the security for user passwords.
  702. we use /dev/urandom here, as /dev/random might block and we don't
  703. need that much randomness. BTW, great idea, Ted! -lf, 03/18/95 */
  704. if ((fd = open(_PATH_DEV_URANDOM, O_RDONLY)) >= 0)
  705. {
  706. unsigned char *pos;
  707. int readcount;
  708. i = AUTH_VECTOR_LEN;
  709. pos = vector;
  710. while (i > 0)
  711. {
  712. readcount = read(fd, (char *)pos, i);
  713. if (readcount >= 0) {
  714. pos += readcount;
  715. i -= readcount;
  716. } else {
  717. if (errno != EINTR && errno != EAGAIN)
  718. goto fallback;
  719. }
  720. }
  721. close(fd);
  722. return;
  723. } /* else fall through */
  724. #endif
  725. fallback:
  726. for (i = 0; i < AUTH_VECTOR_LEN;)
  727. {
  728. randno = random ();
  729. memcpy ((char *) vector, (char *) &randno, sizeof (int));
  730. vector += sizeof (int);
  731. i += sizeof (int);
  732. }
  733. return;
  734. }