authradius.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2000-2016 by Vladimir Dubrovin <3proxy@3proxy.ru>
  4. please read License Agreement
  5. */
  6. #ifndef NORADIUS
  7. #include "proxy.h"
  8. #include "libs/md5.h"
  9. #define AUTH_VECTOR_LEN 16
  10. #define MAX_STRING_LEN 254
  11. #define PW_AUTH_UDP_PORT 1645
  12. #define PW_TYPE_STRING 0
  13. #define PW_TYPE_INTEGER 1
  14. #define PW_TYPE_IPADDR 2
  15. #define PW_TYPE_DATE 3
  16. #define PW_TYPE_ABINARY 4
  17. #define PW_TYPE_OCTETS 5
  18. #define PW_AUTHENTICATION_REQUEST 1
  19. #define PW_AUTHENTICATION_ACK 2
  20. #define PW_AUTHENTICATION_REJECT 3
  21. #define PW_ACCOUNTING_REQUEST 4
  22. #define PW_ACCOUNTING_RESPONSE 5
  23. #define PW_ACCOUNTING_STATUS 6
  24. #define PW_PASSWORD_REQUEST 7
  25. #define PW_USER_NAME 1
  26. #define PW_PASSWORD 2
  27. #define PW_CHAP_PASSWORD 3
  28. #define PW_NAS_IP_ADDRESS 4
  29. #define PW_NAS_PORT_ID 5
  30. #define PW_SERVICE_TYPE 6
  31. #define PW_FRAMED_PROTOCOL 7
  32. #define PW_FRAMED_IP_ADDRESS 8
  33. #define PW_FRAMED_IP_NETMASK 9
  34. #define PW_FRAMED_ROUTING 10
  35. #define PW_FILTER_ID 11
  36. #define PW_FRAMED_MTU 12
  37. #define PW_FRAMED_COMPRESSION 13
  38. #define PW_LOGIN_IP_HOST 14
  39. #define PW_LOGIN_SERVICE 15
  40. #define PW_LOGIN_TCP_PORT 16
  41. #define PW_OLD_PASSWORD 17
  42. #define PW_REPLY_MESSAGE 18
  43. #define PW_CALLBACK_NUMBER 19
  44. #define PW_CALLBACK_ID 20
  45. #define PW_FRAMED_ROUTE 22
  46. #define PW_FRAMED_IPXNET 23
  47. #define PW_STATE 24
  48. #define PW_CLASS 25
  49. #define PW_VENDOR_SPECIFIC 26
  50. #define PW_SESSION_TIMEOUT 27
  51. #define PW_IDLE_TIMEOUT 28
  52. #define PW_CALLED_STATION_ID 30
  53. #define PW_CALLING_STATION_ID 31
  54. #define PW_NAS_IDENTIFIER 32
  55. #define PW_PROXY_STATE 33
  56. #define PW_ACCT_STATUS_TYPE 40
  57. #define PW_ACCT_DELAY_TIME 41
  58. #define PW_ACCT_INPUT_OCTETS 42
  59. #define PW_ACCT_OUTPUT_OCTETS 43
  60. #define PW_ACCT_SESSION_ID 44
  61. #define PW_ACCT_AUTHENTIC 45
  62. #define PW_ACCT_SESSION_TIME 46
  63. #define PW_ACCT_INPUT_PACKETS 47
  64. #define PW_ACCT_OUTPUT_PACKETS 48
  65. #define PW_ACCT_TERMINATE_CAUSE 49
  66. #define PW_EVENT_TIMESTAMP 55
  67. #define PW_CHAP_CHALLENGE 60
  68. #define PW_NAS_PORT_TYPE 61
  69. #define PW_PORT_LIMIT 62
  70. #define PW_ARAP_PASSWORD 70
  71. #define PW_ARAP_FEATURES 71
  72. #define PW_ARAP_ZONE_ACCESS 72
  73. #define PW_ARAP_SECURITY 73
  74. #define PW_ARAP_SECURITY_DATA 74
  75. #define PW_PASSWORD_RETRY 75
  76. #define PW_PROMPT 76
  77. #define PW_CONNECT_INFO 77
  78. #define PW_CONFIGURATION_TOKEN 78
  79. #define PW_EAP_MESSAGE 79
  80. #define PW_MESSAGE_AUTHENTICATOR 80
  81. #define PW_ARAP_CHALLENGE_RESPONSE 84
  82. #define PW_NAS_PORT_ID_STRING 87
  83. #define PW_FRAMED_POOL 89
  84. #define PW_NAS_IPV6_ADDRESS 95
  85. #define PW_LOGIN_IPV6_HOST 98
  86. #define PW_FRAMED_IPV6_ADDRESS 168
  87. #define PW_FALL_THROUGH 500
  88. #define PW_ADD_PORT_TO_IP_ADDRESS 501
  89. #define PW_EXEC_PROGRAM 502
  90. #define PW_EXEC_PROGRAM_WAIT 503
  91. #define PW_AUTHTYPE 1000
  92. #define PW_PREFIX 1003
  93. #define PW_SUFFIX 1004
  94. #define PW_GROUP 1005
  95. #define PW_CRYPT_PASSWORD 1006
  96. #define PW_CONNECT_RATE 1007
  97. #define PW_ADD_PREFIX 1008
  98. #define PW_ADD_SUFFIX 1009
  99. #define PW_EXPIRATION 1010
  100. #define PW_USER_CATEGORY 1029
  101. #define PW_GROUP_NAME 1030
  102. #define PW_HUNTGROUP_NAME 1031
  103. #define PW_SIMULTANEOUS_USE 1034
  104. #define PW_STRIP_USER_NAME 1035
  105. #define PW_HINT 1040
  106. #define PAM_AUTH_ATTR 1041
  107. #define PW_LOGIN_TIME 1042
  108. #define PW_STRIPPED_USER_NAME 1043
  109. #define PW_CURRENT_TIME 1044
  110. #define PW_REALM 1045
  111. #define PW_NO_SUCH_ATTRIBUTE 1046
  112. #define PW_PACKET_TYPE 1047
  113. #define PW_PROXY_TO_REALM 1048
  114. #define PW_REPLICATE_TO_REALM 1049
  115. #define PW_ACCT_SESSION_START_TIME 1050
  116. #define PW_ACCT_UNIQUE_SESSION_ID 1051
  117. #define PW_CLIENT_IP_ADDRESS 1052
  118. #define LDAP_USERDN 1053
  119. #define PW_NS_MTA_MD5_PASSWORD 1054
  120. #define PW_SQL_USER_NAME 1055
  121. #define PW_LOGIN_USER 1
  122. #define PW_FRAMED_USER 2
  123. #define PW_CALLBACK_LOGIN_USER 3
  124. #define PW_CALLBACK_FRAMED_USER 4
  125. #define PW_OUTBOUND_USER 5
  126. #define PW_ADMINISTRATIVE_USER 6
  127. #define PW_NAS_PROMPT_USER 7
  128. #define PW_AUTHENTICATE_ONLY 8
  129. #define PW_CALLBACK_NAS_PROMPT 9
  130. #define PW_NAS_PORT_ASYNC 0
  131. #define PW_NAS_PORT_SYNC 1
  132. #define PW_NAS_PORT_ISDN 2
  133. #define PW_NAS_PORT_ISDN_V120 3
  134. #define PW_NAS_PORT_ISDN_V110 4
  135. #define PW_NAS_PORT_VIRTUAL 5
  136. #define PW_STATUS_START 1
  137. #define PW_STATUS_STOP 2
  138. #define PW_STATUS_ALIVE 3
  139. #define PW_STATUS_ACCOUNTING_ON 7
  140. #define PW_STATUS_ACCOUNTING_OFF 8
  141. struct radserver radiuslist[MAXRADIUS];
  142. static int ntry = 0;
  143. int nradservers = 0;
  144. char radiussecret[64]="";
  145. pthread_mutex_t rad_mutex;
  146. void md5_calc(unsigned char *output, unsigned char *input,
  147. unsigned int inputlen);
  148. char *strNcpy(char *dest, const char *src, int n)
  149. {
  150. if (n > 0)
  151. strncpy(dest, src, n);
  152. else
  153. n = 1;
  154. dest[n - 1] = 0;
  155. return dest;
  156. }
  157. void md5_calc(unsigned char *output, unsigned char *input,
  158. unsigned int inlen)
  159. {
  160. MD5_CTX context;
  161. MD5Init(&context);
  162. MD5Update(&context, input, inlen);
  163. MD5Final(output, &context);
  164. }
  165. static uint8_t random_vector_pool[AUTH_VECTOR_LEN*2];
  166. static int calc_replydigest(char *packet, char *original, const char *secret, int len)
  167. {
  168. int secretlen;
  169. uint8_t calc_digest[AUTH_VECTOR_LEN];
  170. uint8_t calc_vector[AUTH_VECTOR_LEN];
  171. memcpy(calc_vector, packet + 4, AUTH_VECTOR_LEN);
  172. memcpy(packet + 4, original, AUTH_VECTOR_LEN);
  173. secretlen = strlen(secret);
  174. memcpy(packet + len, secret, secretlen);
  175. md5_calc(calc_digest, (u_char *)packet, len + secretlen);
  176. /*
  177. * Return 0 if OK, 2 if not OK.
  178. */
  179. return memcmp(calc_vector, calc_digest, AUTH_VECTOR_LEN) ? 2 : 0;
  180. }
  181. #define AUTH_PASS_LEN (16)
  182. int rad_pwencode(char *passwd, int *pwlen, const char *secret, const char *vector)
  183. {
  184. uint8_t buffer[AUTH_VECTOR_LEN + MAX_STRING_LEN + 1];
  185. char digest[AUTH_VECTOR_LEN];
  186. int i, n, secretlen;
  187. int len;
  188. len = strlen(passwd);
  189. if (len > 128) len = 128;
  190. *pwlen = len;
  191. if (len % AUTH_PASS_LEN != 0) {
  192. n = AUTH_PASS_LEN - (len % AUTH_PASS_LEN);
  193. for (i = len; n > 0; n--, i++)
  194. passwd[i] = 0;
  195. len = *pwlen = i;
  196. }
  197. secretlen = strlen(secret);
  198. memcpy(buffer, secret, secretlen);
  199. memcpy(buffer + secretlen, vector, AUTH_VECTOR_LEN);
  200. md5_calc((u_char *)digest, buffer, secretlen + AUTH_VECTOR_LEN);
  201. for (i = 0; i < AUTH_PASS_LEN; i++)
  202. passwd[i] ^= digest[i];
  203. if (len <= AUTH_PASS_LEN) return 0;
  204. for (n = 0; n < 128 && n <= (len - AUTH_PASS_LEN); n += AUTH_PASS_LEN) {
  205. memcpy(buffer + secretlen, passwd + n, AUTH_PASS_LEN);
  206. md5_calc((u_char *)digest, buffer, secretlen + AUTH_PASS_LEN);
  207. for (i = 0; i < AUTH_PASS_LEN; i++)
  208. passwd[i + n + AUTH_PASS_LEN] ^= digest[i];
  209. }
  210. return 0;
  211. }
  212. void random_vector(uint8_t *vector, struct clientparam *param)
  213. {
  214. int i;
  215. static int did_random = 0;
  216. static int counter = 0;
  217. if (!did_random) {
  218. ntry = (int)basetime;
  219. for (i = 0; i < (int)sizeof(random_vector_pool); i++) {
  220. random_vector_pool[i] += myrand((void *) &param->msec_start, sizeof(param->msec_start)) & 0xff;
  221. }
  222. did_random = 1;
  223. }
  224. counter++;
  225. random_vector_pool[AUTH_VECTOR_LEN] += (counter & 0xff);
  226. md5_calc((u_char *) random_vector_pool,
  227. (u_char *) random_vector_pool,
  228. sizeof(random_vector_pool));
  229. md5_calc((u_char *) vector,
  230. (u_char *) random_vector_pool,
  231. sizeof(random_vector_pool));
  232. }
  233. static float timeout = 5;
  234. typedef struct radius_packet_t {
  235. uint8_t code;
  236. uint8_t id;
  237. uint16_t length;
  238. uint8_t vector[AUTH_VECTOR_LEN];
  239. uint8_t data[4096];
  240. } radius_packet_t;
  241. #define RETURN(xxx) { res = xxx; goto CLEANRET; }
  242. int radsend(struct clientparam * param, int auth){
  243. int loop;
  244. int id;
  245. int res = 4;
  246. SOCKET sockfd = -1;
  247. unsigned char *ptr;
  248. int total_length;
  249. int len;
  250. int op;
  251. #ifdef NOIPV6
  252. struct sockaddr_in saremote;
  253. #else
  254. struct sockaddr_in6 saremote;
  255. #endif
  256. struct pollfd fds[1];
  257. char vector[AUTH_VECTOR_LEN];
  258. radius_packet_t packet, rpacket;
  259. SASIZETYPE salen;
  260. int data_len;
  261. uint8_t *vendor_len;
  262. int count=0;
  263. uint8_t *attr;
  264. long vendor=0;
  265. int vendorlen=0;
  266. if(!radiussecret || !nradservers) {
  267. return 4;
  268. }
  269. memset(&packet, 0, sizeof(packet));
  270. pthread_mutex_lock(&rad_mutex);
  271. random_vector(packet.vector, param);
  272. id = ((ntry++) & 0xff);
  273. pthread_mutex_unlock(&rad_mutex);
  274. packet.code = PW_AUTHENTICATION_REQUEST;
  275. packet.id=id;
  276. ptr = packet.data;
  277. total_length = 0;
  278. md5_calc(packet.vector, packet.vector,
  279. sizeof(packet.vector));
  280. /* Service Type */
  281. *ptr++ = PW_SERVICE_TYPE;
  282. *ptr++ = 6;
  283. (*(uint32_t *)ptr)=htonl(PW_AUTHENTICATE_ONLY);
  284. ptr+=4;
  285. total_length+=6;
  286. /* NAS-Port-Type */
  287. *ptr++ = PW_NAS_PORT_TYPE;
  288. *ptr++ = 6;
  289. (*(uint32_t *)ptr)=htonl(PW_NAS_PORT_VIRTUAL);
  290. ptr+=4;
  291. total_length+=6;
  292. /* NAS-Port */
  293. *ptr++ = PW_NAS_PORT_ID;
  294. *ptr++ = 6;
  295. (*(uint32_t *)ptr)=htonl((uint32_t)ntohs((*SAPORT(&param->srv->intsa))));
  296. ptr+=4;
  297. total_length+=6;
  298. if(*SAFAMILY(&param->sincl) == AF_INET6){
  299. /* NAS-IPv6-Address */
  300. *ptr++ = PW_NAS_IPV6_ADDRESS;
  301. *ptr++ = 18;
  302. }
  303. else {
  304. /* NAS-IP-Address */
  305. *ptr++ = PW_NAS_IP_ADDRESS;
  306. *ptr++ = 6;
  307. }
  308. len = SAADDRLEN(&param->sincl);
  309. memcpy(ptr, SAADDR(&param->sincl), len);
  310. ptr += len;
  311. total_length += (2+len);
  312. /* NAS-Identifier */
  313. if(conf.stringtable){
  314. *ptr++ = PW_NAS_IDENTIFIER;
  315. len = strlen(conf.stringtable[SERVICES+param->service]);
  316. *ptr++ = (2 + len);
  317. memcpy(ptr, conf.stringtable[SERVICES+param->service], len);
  318. ptr += len;
  319. total_length+=(len+2);
  320. }
  321. if(*SAFAMILY(&param->sincr) == AF_INET6){
  322. /* Framed-IPv6-Address */
  323. *ptr++ = PW_FRAMED_IPV6_ADDRESS;
  324. *ptr++ = 18;
  325. }
  326. else {
  327. /* Framed-IP-Address */
  328. *ptr++ = PW_FRAMED_IP_ADDRESS;
  329. *ptr++ = 6;
  330. }
  331. len = SAADDRLEN(&param->sincr);
  332. memcpy(ptr, SAADDR(&param->sincr), len);
  333. ptr += len;
  334. total_length += (2+len);
  335. /* Called-Station-ID */
  336. if(param->hostname){
  337. *ptr++ = PW_CALLED_STATION_ID;
  338. len = strlen(param->hostname);
  339. *ptr++ = (2 + len);
  340. memcpy(ptr, param->hostname, len);
  341. ptr += len;
  342. total_length+=(len+2);
  343. }
  344. /* Login-Service */
  345. op = param->operation;
  346. for(len=0; op; len++)op>>=1;
  347. *ptr++ = PW_LOGIN_SERVICE;
  348. *ptr++ = 4;
  349. (*(uint16_t *)ptr)=htons((uint16_t)(len + 1000));
  350. ptr+=2;
  351. total_length+=4;
  352. /* Login-TCP-Port */
  353. *ptr++ = PW_LOGIN_TCP_PORT;
  354. *ptr++ = 4;
  355. (*(uint16_t *)ptr)=*SAPORT(&param->req);
  356. ptr+=2;
  357. total_length+=4;
  358. if(*SAFAMILY(&param->req) == AF_INET6){
  359. /* Login-IPv6-Host */
  360. *ptr++ = PW_LOGIN_IPV6_HOST;
  361. *ptr++ = 18;
  362. }
  363. else {
  364. /* Login-IP-Host */
  365. *ptr++ = PW_LOGIN_IP_HOST;
  366. *ptr++ = 6;
  367. }
  368. len = SAADDRLEN(&param->req);
  369. memcpy(ptr, SAADDR(&param->req), len);
  370. ptr += len;
  371. total_length += (2+len);
  372. /* Username */
  373. if(param->username){
  374. len = strlen(param->username);
  375. if(len>128)len=128;
  376. *ptr++ = PW_USER_NAME;
  377. *ptr++ = len + 2;
  378. memcpy(ptr, param->username, len);
  379. ptr+=len;
  380. total_length += (len+2);
  381. }
  382. if(param->password){
  383. len = strlen(param->password);
  384. if(len > 128) len = 128;
  385. *ptr++ = PW_PASSWORD;
  386. ptr++;
  387. memcpy(ptr, param->password, len);
  388. rad_pwencode(ptr,
  389. &len,
  390. radiussecret,
  391. (char *)packet.vector);
  392. *(ptr-1) = len + 2;
  393. ptr+=len;
  394. total_length+= (len+2);
  395. }
  396. total_length+=(4+AUTH_VECTOR_LEN);
  397. packet.length = htons(total_length);
  398. memcpy(vector, packet.vector, AUTH_VECTOR_LEN);
  399. for (loop = 0; loop < nradservers && loop < MAXRADIUS; loop++) {
  400. SOCKET remsock;
  401. saremote = auth?radiuslist[loop].authaddr : radiuslist[loop].logaddr;
  402. #ifdef NOIPV6
  403. if(*SAFAMILY(&saremote)!= AF_INET) {
  404. continue;
  405. }
  406. #else
  407. if(*SAFAMILY(&saremote)!= AF_INET && *SAFAMILY(&saremote)!= AF_INET6){
  408. continue;
  409. }
  410. #endif
  411. packet.id++;
  412. if(auth) {
  413. if(sockfd >= 0) so._closesocket(sockfd);
  414. if ((sockfd = so._socket(SASOCK(&saremote), SOCK_DGRAM, 0)) < 0) {
  415. return 4;
  416. }
  417. remsock = sockfd;
  418. }
  419. else remsock = radiuslist[loop].logsock;
  420. len = so._sendto(remsock, (char *)&packet, total_length, 0,
  421. (struct sockaddr *)&saremote, sizeof(saremote));
  422. if(len != ntohs(packet.length)){
  423. continue;
  424. }
  425. memset(fds, 0, sizeof(fds));
  426. fds[0].fd = remsock;
  427. fds[0].events = POLLIN;
  428. if(so._poll(fds, 1, conf.timeouts[SINGLEBYTE_L]*1000) <= 0) {
  429. continue;
  430. }
  431. salen = sizeof(saremote);
  432. data_len = so._recvfrom(remsock, (char *)&rpacket, sizeof(packet)-16,
  433. 0, (struct sockaddr *)&saremote, &salen);
  434. if (data_len < 20) {
  435. continue;
  436. }
  437. if( rpacket.code != PW_AUTHENTICATION_ACK &&
  438. rpacket.code != PW_AUTHENTICATION_REJECT ){
  439. continue;
  440. }
  441. if (calc_replydigest((char *)&rpacket, packet.vector, radiussecret,
  442. data_len) ){
  443. continue;
  444. }
  445. total_length = ntohs(rpacket.length);
  446. if (data_len != total_length) {
  447. continue;
  448. }
  449. attr = rpacket.data;
  450. count = total_length - 20;
  451. vendor_len = 0;
  452. while (count >= 2) {
  453. if (!vendor && attr[0] == 0) {
  454. break;
  455. }
  456. if (attr[1] < 2) {
  457. break;
  458. }
  459. if(!vendor && attr[0] == PW_VENDOR_SPECIFIC) {
  460. if (attr[1] < 6 || count < 6) RETURN(4);
  461. vendorlen = attr[1]-6;
  462. vendor = htonl(*((int*)(attr +2)));
  463. count -= 6;
  464. attr += 6;
  465. continue;
  466. }
  467. if (!vendor && attr[0] == PW_FRAMED_IP_ADDRESS && attr[1] == 6) {
  468. *SAFAMILY(&param->sinsl) = AF_INET;
  469. memcpy(SAADDR(&param->sinsl), attr+2, 4);
  470. }
  471. else if (!vendor && attr[0] == PW_FRAMED_IPV6_ADDRESS && attr[1] == 18) {
  472. *SAFAMILY(&param->sinsl) = AF_INET6;
  473. memcpy(SAADDR(&param->sinsl), attr+2, 16);
  474. }
  475. else if (!vendor && attr[0] == PW_REPLY_MESSAGE && attr[1] >= 3 && isdigit(attr[2])) {
  476. res = 0;
  477. for(len = 2; len < attr[1] && isdigit(attr[len]); len++) res = (res * 10) + (attr[len] - '0');
  478. }
  479. /*
  480. else if (vendor == SANDY && attr[0] == SANDY_MAIL_MAILBOX) {
  481. memcpy (p->drop_name, attr + 2, attr[1] - 2);
  482. }
  483. else if (vendor == SANDY && attr[0] == SANDY_MAIL_MBOXCONTROL) {
  484. if (ntohl(*(int *)(attr+2)) & 1) p->dodeletes = 1;
  485. }
  486. else if (vendor == SANDY && attr[0] == SANDY_MAIL_SERVICE) {
  487. mailservice = ntohl(*(int *)(attr+2)) ;
  488. }
  489. */
  490. count -= attr[1];
  491. if(vendorlen) {
  492. vendorlen -= attr[1];
  493. if (!vendorlen) vendor = 0;
  494. else if (vendorlen < 0) RETURN(4);
  495. }
  496. attr += attr[1];
  497. }
  498. if (count !=0 || vendorlen!=0) {
  499. continue;
  500. }
  501. if(rpacket.code == PW_AUTHENTICATION_REJECT) RETURN (res);
  502. if(rpacket.code == PW_AUTHENTICATION_ACK) RETURN(0);
  503. res = 4;
  504. }
  505. CLEANRET:
  506. if(sockfd >= 0) so._closesocket(sockfd);
  507. return res;
  508. }
  509. int radauth(struct clientparam * param){
  510. return radsend(param, 1);
  511. }
  512. #endif