Harald Welte | a3c6223 | 2010-03-25 21:33:21 +0800 | [diff] [blame] | 1 | /* ipaccess nanoBTS proprietary telnet authentication, |
| 2 | * written by Dieter Spaar <spaar@mirider.augusta.de> */ |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <unistd.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <errno.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | #include <sys/types.h> |
| 11 | #include <sys/socket.h> |
| 12 | |
| 13 | #include <openssl/md5.h> |
| 14 | |
| 15 | #define CLI_USER "CLICLIENT\n" |
| 16 | #define KEY "Sh1n30nY0uCra2yD1am0nd" |
| 17 | |
| 18 | static void compute_response(unsigned char *ubChallenge, unsigned char *ubResponse) |
| 19 | { |
| 20 | MD5_CTX md5; |
| 21 | int i; |
| 22 | |
| 23 | MD5_Init(&md5); |
| 24 | |
| 25 | for(i = 0; i < 4; i++) { |
| 26 | MD5_Update(&md5, (unsigned char *)KEY, strlen(KEY)); |
| 27 | MD5_Update(&md5, ubChallenge, 16); |
| 28 | } |
| 29 | MD5_Final(ubResponse, &md5); |
| 30 | } |
| 31 | |
| 32 | // nanoBTS Challenge/Response |
| 33 | int ipaccess_telnet_auth(int sock) |
| 34 | { |
| 35 | char buffer[512]; |
| 36 | int rs; |
| 37 | unsigned char ubResponse[18]; |
| 38 | |
| 39 | // send client name |
| 40 | |
| 41 | if ((rs = send(sock, CLI_USER, strlen(CLI_USER), 0)) == -1) { |
| 42 | fprintf(stderr, "send() failed: %s\n", strerror(errno)); |
| 43 | exit(1); |
| 44 | } else if (rs == 0) { |
| 45 | fprintf(stderr, "send() unexpectedly returned 0\n"); |
| 46 | exit(1); |
| 47 | } |
| 48 | |
| 49 | // receive challenge |
| 50 | |
| 51 | if ((rs = recv(sock, buffer, sizeof(buffer), 0)) > 0) { |
| 52 | if(rs != 18 || buffer[0] != '<' || buffer[17] != '>') { |
| 53 | fprintf(stderr, "unexpected response\n"); |
| 54 | exit(1); |
| 55 | } |
| 56 | } else if (rs == 0) { |
| 57 | fprintf(stderr, "recv(client) unexpectedly returned 0\n"); |
| 58 | exit(1); |
| 59 | } else { |
| 60 | fprintf(stderr, "recv(client) failed: %s\n", |
| 61 | strerror(errno)); |
| 62 | exit(1); |
| 63 | } |
| 64 | |
| 65 | // calculate response |
| 66 | |
| 67 | memset(&ubResponse, 0, sizeof(ubResponse)); |
| 68 | compute_response(buffer + 1, ubResponse + 1); |
| 69 | ubResponse[0] = '<'; |
| 70 | ubResponse[17] = '>'; |
| 71 | |
| 72 | // send response |
| 73 | if ((rs = send(sock, ubResponse, sizeof(ubResponse), 0)) == -1) { |
| 74 | fprintf(stderr, "send() failed: %s\n", strerror(errno)); |
| 75 | exit(1); |
| 76 | } else if (rs == 0) { |
| 77 | fprintf(stderr, "send() unexpectedly returned 0\n"); |
| 78 | exit(1); |
| 79 | } |
| 80 | } |