1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
|
/*
* everybuddy
*
* Copyright (C) 1998-1999, Torrey Searle
* proxy featured by Seb C.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/* this is a little piece of code to handle proxy connection */
/* it is intended to : 1st handle http proxy, using the CONNECT command
, 2nd provide an easy way to add socks support */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <gtk/gtk.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "config.h"
#include "libproxy.h"
static int proxy_inited=0;
int proxy_type = PROXY_NONE;
char *proxy_host;
int proxy_port = 3128;
static char *proxy_realhost = NULL;
static char debug_buff[256];
/* Stores the encoded proxy username and password */
static char *proxy_auth = NULL;
/* Prototypes */
static char *encode_proxy_auth_str(const char *user, const char *passwd);
#define debug_print printf
#ifndef HAVE_GETHOSTBYNAME2
#define gethostbyname2(x, y) gethostbyname(x)
#endif
typedef struct _udp_connection
{
int fd;
struct sockaddr * host;
struct _udp_connection * next;
} udp_connection;
static udp_connection * server_list = NULL;
static struct sockaddr * get_server( int fd )
{
udp_connection * node = NULL;
printf("looking for connection matching %d", fd);
for ( node = server_list; node; node = node->next )
{
if(fd == node->fd )
{
printf("match found\n");
return node->host;
}
}
printf("no match found\n");
return NULL;
}
static int connect_address(unsigned int addy, unsigned short port)
{
int fd;
struct sockaddr_in sin;
sin.sin_addr.s_addr = addy;
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd > -1)
{
if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) > -1)
{
return fd;
}
else
{
perror("connect:" );
}
}
else
{
printf("unable to create socket\n");
}
return -1;
}
/* Call this function if you want to free memory when leaving */
void proxy_freemem() {
/* dummy function to avoid possibly memory leak */
if (proxy_host != NULL)
free(proxy_host);
if (proxy_realhost != NULL)
free(proxy_realhost);
if ( proxy_auth != NULL )
free( proxy_auth );
return;
}
/* this function is useless if you do use an external method to set
the proxy setting, but it does yet allow to test the code without
having to code the interface */
static void proxy_autoinit() {
if (getenv("HTTPPROXY") != NULL) {
proxy_host=(char *)strdup(getenv("HTTPPROXY"));
if (proxy_host != NULL) {
proxy_port=atoi(getenv("HTTPPROXYPORT"));
proxy_type=PROXY_HTTP;
if (!proxy_port)
proxy_port=3128;
if ( getenv( "HTTPPROXYUSER" ) ) {
char *proxy_user;
char *proxy_password;
proxy_user = getenv( "HTTPPROXYUSER" );
proxy_password = getenv( "HTTPPROXYPASSWORD" );
proxy_auth = encode_proxy_auth_str(proxy_user, proxy_password);
}
#ifdef HAVE_ATEXIT
/* if you do not have atexit, it means that if you don't call
proxy_freemem when leaving, you'll have mem leaks */
atexit(proxy_freemem);
#endif
}
}
else
{
proxy_type=PROXY_NONE;
}
proxy_inited=1;
return;
}
/* external function to use to set the proxy settings */
int proxy_set_proxy(int type,char *host,int port) {
proxy_type=type;
if (type != PROXY_NONE) {
proxy_port=0;
proxy_host=(char *)strdup(host);
if (proxy_host != NULL)
proxy_port=port;
if (proxy_port == 0)
proxy_port=3128;
#ifdef HAVE_ATEXIT
/* if you do not have atexit, it means that if you don't call
proxy_freemem when leaving, you'll have mem leaks */
atexit(proxy_freemem);
#endif
}
proxy_inited=1;
return(0);
}
/* external function to set the proxy user and proxy pasword */
int proxy_set_auth( const char *user, const char *passwd )
{
proxy_auth = encode_proxy_auth_str(user, passwd);
return 1;
}
/* this code is borrowed from cvs 1.10 */
static int
proxy_recv_line (int sock, char **resultp)
{
int c;
char *result;
size_t input_index = 0;
size_t result_size = 80;
result = (char *) malloc (result_size);
while (1)
{
char ch;
if (recv (sock, &ch, 1, 0) < 0)
fprintf (stderr, "recv() error from proxy server\n");
c = ch;
if (c == EOF)
{
free (result);
/* It's end of file. */
fprintf(stderr, "end of file from server\n");
}
if (c == '\012')
break;
result[input_index++] = c;
while (input_index + 1 >= result_size)
{
result_size *= 2;
result = (char *) realloc (result, result_size);
}
}
if (resultp)
*resultp = result;
/* Terminate it just for kicks, but we *can* deal with embedded NULs. */
result[input_index] = '\0';
if (resultp == NULL)
free (result);
return input_index;
}
int proxy_recv( int s, void * buff, int len, unsigned int flags )
{
int type;
int size;
getsockopt( s, SOL_SOCKET, SO_TYPE, &type, &size );
if( proxy_type != PROXY_SOCKS5 || type == SOCK_STREAM )
{
return recv(s, buff, len, flags );
}
else
{
char * tmpbuff = g_new0(char, len+10);
int mylen;
mylen = recv(s, tmpbuff, len+10, flags);
memcpy( buff, tmpbuff+10, len);
g_free(tmpbuff);
return mylen - 9;
}
return 0;
}
int proxy_send(int s, const void *msg, int len, unsigned int
flags)
{
int type;
int size;
printf("proxy_send\n");
getsockopt( s, SOL_SOCKET, SO_TYPE, &type, &size );
if( proxy_type != PROXY_SOCKS5 || type == SOCK_STREAM)
{
if( type != SOCK_DGRAM )
printf("type TCP\n");
else
printf("type UDP\n");
return send(s, msg, len, flags);
}
else
{
char * buff = g_new0(gchar, len+10);
struct sockaddr * serv_addr = get_server( s );
int ret;
printf("type UDP %s %d\n",
inet_ntoa((((struct sockaddr_in *)serv_addr)->sin_addr)),
ntohs(((struct sockaddr_in *)serv_addr)->sin_port) );
buff[3] = 0x01;
memcpy(buff+4, &(((struct sockaddr_in *)serv_addr)->sin_addr),4 );
memcpy((buff+8), &(((struct sockaddr_in *)serv_addr)->sin_port), 2);
memcpy((buff+10), msg, len );
ret = send(s, buff, len+10, flags);
g_free(buff);
return ret;
}
return 0;
}
struct hostent *proxy_gethostbyname(char *host)
{
if (!proxy_inited)
proxy_autoinit();
if (proxy_type == PROXY_NONE || proxy_type == PROXY_SOCKS5)
return (gethostbyname(host));
if (proxy_realhost != NULL)
g_free(proxy_realhost);
/* we keep the real host name for the Connect command */
proxy_realhost = (char *) strdup(host);
return (gethostbyname(proxy_host));
}
struct hostent *proxy_gethostbyname2(char *host,int type)
{
if (!proxy_inited)
proxy_autoinit();
if (proxy_type == PROXY_NONE || proxy_type == PROXY_SOCKS5)
return (gethostbyname2(host,type));
if (proxy_realhost != NULL)
g_free(proxy_realhost);
/* we keep the real host name for the Connect command */
proxy_realhost = (char *) strdup(host);
return (gethostbyname2(proxy_host,type));
}
int socks4_connect(int sock, struct sockaddr *serv_addr, int addrlen )
{
struct sockaddr_in sa;
unsigned char packet[12];
struct hostent * hp;
if (!(hp = gethostbyname(proxy_host)))
return -1;
/* Clear the structure and setup for SOCKS4 open */
bzero(&sa, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons (proxy_port);
bcopy(hp->h_addr, (char *) &sa.sin_addr, hp->h_length);
/* Connect to the SOCKS4 port and send a connect packet */
if (connect(sock, (struct sockaddr *) &sa, sizeof (sa))!=-1)
{
unsigned short realport;
if (!(hp = gethostbyname(proxy_realhost)))
{
return -1;
}
realport = ntohs(((struct sockaddr_in *)serv_addr)->sin_port);
packet[0] = 4; /* Version number */
packet[1] = 1;
packet[2] = (((unsigned short) realport) >> 8);
packet[3] = (((unsigned short) realport) & 0xff);
packet[4] = (unsigned char) (hp->h_addr_list[0])[0];
packet[5] = (unsigned char) (hp->h_addr_list[0])[1];
packet[6] = (unsigned char) (hp->h_addr_list[0])[2];
packet[7] = (unsigned char) (hp->h_addr_list[0])[3];
packet[8] = 0;
if (write(sock, packet, 9) == 9)
{
bzero(packet, sizeof(packet));
/* Check response - return as SOCKS4 if its valid */
if (read(sock, packet, 9)>=4 && packet[1] == 90)
{
return 0;
}
}
close(sock);
}
return -1;
}
int socks5_connect(int sockfd, struct sockaddr *serv_addr, int addrlen )
{
int i;
int s =0;
int type;
int size = sizeof(int);
int tcplink = 0;
struct sockaddr_in bind_addr;
struct sockaddr_in sin;
struct hostent * hostinfo;
char buff[255];
getsockopt( sockfd, SOL_SOCKET, SO_TYPE, &type, &size );
printf(" %d %d %d \n", SOCK_STREAM, SOCK_DGRAM, type );
bind_addr.sin_addr.s_addr = INADDR_ANY;
bind_addr.sin_family = PF_INET;
bind_addr.sin_port = 0;
if( type == SOCK_DGRAM )
{
size = sizeof(bind_addr);
if(bind(sockfd, (struct sockaddr*)&bind_addr, sizeof(bind_addr)) < 0 )
{
perror("bind");
exit(0);
}
getsockname( sockfd, (struct sockaddr*)&bind_addr, &size );
}
hostinfo = gethostbyname (proxy_host);
if (hostinfo == NULL)
{
fprintf (stderr, "Unknown host %s.\n", proxy_host);
return (-1);
}
/* connect to the proxy server */
bcopy(hostinfo->h_addr, (char *)&sin.sin_addr, hostinfo->h_length);
sin.sin_family = AF_INET;
sin.sin_port = htons(proxy_port);
if( type != SOCK_DGRAM )
{
s = connect(sockfd, (struct sockaddr *)&sin, sizeof(sin));
if( s < 0 )
return s;
}
else
{
printf("Connecting to %x on %u\n", inet_addr(proxy_host), proxy_port );
tcplink = connect_address( inet_addr(proxy_host), proxy_port );
printf("We got socket %d\n", tcplink);
}
buff[0] = 0x05; //use socks v5
buff[1] = 0x01; //we support only one method (no authenticaton)
buff[2] = 0x00; //we support the method type "no authentication"
if( type != SOCK_DGRAM )
{
write( sockfd, buff, 3 );
read( sockfd, buff, 2 );
}
else
{
write( tcplink, buff, 3 );
read( tcplink, buff, 2 );
fprintf(stderr, "We got a response back from the SOCKS server\n");
}
if( buff[1] != 0x00 )
{
fprintf(stderr, "No Acceptable Methods");
exit(0);
}
buff[0] = 0x05; //use socks5
buff[1] = ((type == SOCK_STREAM) ? 0x01 : 0x03); //connect
buff[2] = 0x00; //reserved
buff[3] = 0x01; //ipv4 address
if( type != SOCK_DGRAM )
{
memcpy(buff+4, &(((struct sockaddr_in *)serv_addr)->sin_addr),4 );
memcpy((buff+8), &(((struct sockaddr_in *)serv_addr)->sin_port), 2);
}
else
{
buff[4] = 0x00;
buff[5] = 0x00;
buff[6] = 0x00;
buff[7] = 0x00;
memcpy((buff+8), &(((struct sockaddr_in *)&bind_addr)->sin_port), 2);
printf("UDP port %s %d\n", inet_ntoa(((struct sockaddr_in *)serv_addr)->sin_addr),
ntohs(((struct sockaddr_in *)serv_addr)->sin_port));
for( i = 0; i < 8; i++ )
{
printf("%03d ", buff[i] );
}
printf("%d", ntohs(*(unsigned short *)&buff[8]));
printf("\n");
}
if( type != SOCK_DGRAM )
{
write( sockfd, buff, 10 );
read(sockfd, buff, 10);
}
else
{
write( tcplink, buff, 10 );
read( tcplink, buff, 10);
fprintf(stderr, "We got another response from the server!\n");
}
// buff[1] = 0;
if( buff[1] == 0x00 )
{
for( i = 0; i < 8; i++ )
{
printf("%03d ", buff[i] );
}
printf("%d", ntohs(*(unsigned short *)&buff[8]));
printf("\n");
if( type == SOCK_DGRAM )
{
udp_connection * conn = g_new0(udp_connection, 1);
struct sockaddr * host = g_new0(struct sockaddr, 1);
memcpy(host, serv_addr, sizeof(struct sockaddr_in));
conn->next = server_list;
conn->fd = sockfd;
conn->host = host;
server_list = conn;
printf("adding UDP connection %s %d on fd %d\n",
inet_ntoa((((struct sockaddr_in *)serv_addr)->sin_addr)),
ntohs(((struct sockaddr_in *)serv_addr)->sin_port),
sockfd );
memcpy( &sin.sin_addr, buff+4, 4);
memcpy( &sin.sin_port, buff+8, 2);
sin.sin_family = AF_INET;
printf( "trying to connect to %s on port %d \n",
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port) );
s = connect(sockfd, (struct sockaddr *)&sin, sizeof(sin));
}
printf("libproxy; SOCKS5 connection on %d\n", sockfd );
return s;
}
else
{
for( i = 0; i < 8; i++ )
{
printf("%03d ", buff[i] );
}
printf("%d", ntohs(*(unsigned short *)&buff[8]));
printf("\n");
fprintf(stderr, "SOCKS error number %d\n", buff[1] );
close(sockfd);
exit(0);
return -1;
}
}
int http_tunnel_init(int sockfd, struct sockaddr *serv_addr, int addrlen )
{
/* step two : do proxy tunneling init */
char cmd[200];
char *inputline;
unsigned short realport=ntohs(((struct sockaddr_in *)serv_addr)->sin_port);
sprintf(cmd,"CONNECT %s:%d HTTP/1.0\r\n",proxy_realhost,realport);
if ( proxy_auth != NULL ) {
strcat( cmd, "Proxy-Authorization: Basic " );
strcat( cmd, proxy_auth );
strcat( cmd, "\r\n" );
}
strcat( cmd, "\r\n" );
#ifndef DEBUG
sprintf(debug_buff,"<%s>\n",cmd);
debug_print(debug_buff);
#endif
if (send(sockfd,cmd,strlen(cmd),0)<0)
return(-1);
if (proxy_recv_line(sockfd,&inputline) < 0)
return(-1);
#ifndef DEBUG
sprintf(debug_buff,"<%s>\n",inputline);
debug_print(debug_buff);
#endif
if (!strstr(inputline, "200")) {
/* Check if proxy authorization needed */
if ( strstr( inputline, "407" ) ) {
return( -2 );
}
free(inputline);
return(-1);
}
while (strlen(inputline)>1) {
free(inputline);
if (proxy_recv_line(sockfd,&inputline) < 0) {
return(-1);
}
#ifndef DEBUG
sprintf(debug_buff,"<%s>\n",inputline);
debug_print(debug_buff);
#endif
}
free(inputline);
return 0;
}
int http_connect(int sockfd, struct sockaddr *serv_addr, int addrlen )
{
/* do the tunneling */
/* step one : connect to proxy */
struct sockaddr_in name;
int ret;
struct hostent *hostinfo;
unsigned short shortport = proxy_port;
memset (&name, 0, sizeof (name));
name.sin_family = AF_INET;
name.sin_port = htons (shortport);
hostinfo = gethostbyname (proxy_host);
if (hostinfo == NULL) {
fprintf (stderr, "Unknown host %s.\n", proxy_host);
return (-1);
}
name.sin_addr = *(struct in_addr *) hostinfo->h_addr;
#ifndef DEBUG
sprintf(debug_buff,"Trying to connect ...\n");
debug_print(debug_buff);
#endif
if ((ret = connect(sockfd,(struct sockaddr *)&name,sizeof(name)))<0)
return(ret);
return (http_tunnel_init(sockfd, serv_addr, addrlen));
}
int proxy_connect(int sockfd, struct sockaddr *serv_addr, int addrlen )
{
if (!proxy_inited)
proxy_autoinit();
switch (proxy_type) {
case PROXY_NONE: /* No proxy */
return (connect(sockfd,serv_addr,addrlen));
break;
case PROXY_HTTP: /* Http proxy */
return ( http_connect(sockfd, serv_addr, addrlen) < 0 ? -1 : 0 );
break;
case PROXY_SOCKS4: /* SOCKS4 proxy */
return socks4_connect(sockfd, serv_addr, addrlen);
break;
case PROXY_SOCKS5: /* SOCKS5 proxy */
return socks5_connect(sockfd, serv_addr, addrlen);
break;
default:
fprintf(stderr,"Unknown proxy type : %d.\n",proxy_type);
break;
}
return(-1);
}
/* Check if proxy needs authorization. Send a dummy CONNECT request and see the
* response. If return value is,
* >= 0 .Proxy does not need authorization
* = -2, need proxy authorization.
* = -1. Other error.
*/
int need_proxy_auth()
{
int res;
int servfd;
struct hostent *server;
struct sockaddr_in serv_addr;
printf( "Came in need_proxy_auth. Type %d %d\n", proxy_type, PROXY_HTTP );
if ( proxy_type != PROXY_HTTP )
return 0;
servfd = socket(AF_INET, SOCK_STREAM, 0);
server = proxy_gethostbyname(proxy_host);
if (!server)
{
printf("[YahooLib] failed to look up server (%s:%d)\n",
proxy_host, proxy_port);
return (0);
}
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy(server->h_addr, &serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(proxy_port);
res = http_connect( servfd, (struct sockaddr *)&serv_addr, sizeof( serv_addr ) );
close( servfd );
return ( res );
}
int ap_base64encode(char *encoded, const char *string, int len);
static char *
encode_proxy_auth_str( const char *user, const char *passwd )
{
char buff[200];
char buff1[200];
if ( user == NULL )
return NULL;
strcpy( buff, user );
strcat( buff, ":" );
strcat( buff, passwd );
ap_base64encode( buff1, buff, strlen( buff ) );
return strdup( buff1 );
}
/* ====================================================================
* Copyright (c) 1995-1999 The Apache Group. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* 4. The names "Apache Server" and "Apache Group" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Group and was originally based
* on public domain software written at the National Center for
* Supercomputing Applications, University of Illinois, Urbana-Champaign.
* For more information on the Apache Group and the Apache HTTP server
* project, please see <http://www.apache.org/>.
*
*/
/* base64 encoder/decoder. Originally part of main/util.c
* but moved here so that support/ab and ap_sha1.c could
* use it. This meant removing the ap_palloc()s and adding
* ugly 'len' functions, which is quite a nasty cost.
*/
#include <string.h>
int ap_base64decode_binary(unsigned char *bufplain, const char *bufcoded);
/* aaaack but it's fast and const should make it shared text page. */
static const unsigned char pr2six[256] =
{
#ifndef CHARSET_EBCDIC
/* ASCII table */
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
#else /*CHARSET_EBCDIC*/
/* EBCDIC table */
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 63, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 64, 64, 64, 64, 64, 64,
64, 35, 36, 37, 38, 39, 40, 41, 42, 43, 64, 64, 64, 64, 64, 64,
64, 64, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 64, 64, 64, 64, 64, 64,
64, 9, 10, 11, 12, 13, 14, 15, 16, 17, 64, 64, 64, 64, 64, 64,
64, 64, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64, 64,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64
#endif /*CHARSET_EBCDIC*/
};
int ap_base64decode_len(const char *bufcoded)
{
int nbytesdecoded;
register const unsigned char *bufin;
register int nprbytes;
bufin = (const unsigned char *) bufcoded;
while (pr2six[*(bufin++)] <= 63);
nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
nbytesdecoded = ((nprbytes + 3) / 4) * 3;
return nbytesdecoded + 1;
}
int ap_base64decode(char *bufplain, const char *bufcoded)
{
#ifdef CHARSET_EBCDIC
int i;
#endif /* CHARSET_EBCDIC */
int len;
len = ap_base64decode_binary((unsigned char *) bufplain, bufcoded);
#ifdef CHARSET_EBCDIC
for (i = 0; i < len; i++)
bufplain[i] = os_toebcdic[bufplain[i]];
#endif /* CHARSET_EBCDIC */
bufplain[len] = '\0';
return len;
}
/* This is the same as ap_base64udecode() except on EBCDIC machines, where
* the conversion of the output to ebcdic is left out.
*/
int ap_base64decode_binary(unsigned char *bufplain,
const char *bufcoded)
{
int nbytesdecoded;
register const unsigned char *bufin;
register unsigned char *bufout;
register int nprbytes;
bufin = (const unsigned char *) bufcoded;
while (pr2six[*(bufin++)] <= 63);
nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
nbytesdecoded = ((nprbytes + 3) / 4) * 3;
bufout = (unsigned char *) bufplain;
bufin = (const unsigned char *) bufcoded;
while (nprbytes > 4) {
*(bufout++) =
(unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
*(bufout++) =
(unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
*(bufout++) =
(unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
bufin += 4;
nprbytes -= 4;
}
/* Note: (nprbytes == 1) would be an error, so just ingore that case */
if (nprbytes > 1) {
*(bufout++) =
(unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
}
if (nprbytes > 2) {
*(bufout++) =
(unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
}
if (nprbytes > 3) {
*(bufout++) =
(unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
}
nbytesdecoded -= (4 - nprbytes) & 3;
return nbytesdecoded;
}
static const char basis_64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int ap_base64encode_len(int len)
{
return ((len + 2) / 3 * 4) + 1;
}
/* This is the same as ap_base64encode() except on EBCDIC machines, where
* the conversion of the input to ascii is left out.
*/
int ap_base64encode_binary(char *encoded,
const unsigned char *string, int len)
{
int i;
char *p;
p = encoded;
for (i = 0; i < len - 2; i += 3) {
*p++ = basis_64[(string[i] >> 2) & 0x3F];
*p++ = basis_64[((string[i] & 0x3) << 4) |
((int) (string[i + 1] & 0xF0) >> 4)];
*p++ = basis_64[((string[i + 1] & 0xF) << 2) |
((int) (string[i + 2] & 0xC0) >> 6)];
*p++ = basis_64[string[i + 2] & 0x3F];
}
if (i < len) {
*p++ = basis_64[(string[i] >> 2) & 0x3F];
if (i == (len - 1)) {
*p++ = basis_64[((string[i] & 0x3) << 4)];
*p++ = '=';
}
else {
*p++ = basis_64[((string[i] & 0x3) << 4) |
((int) (string[i + 1] & 0xF0) >> 4)];
*p++ = basis_64[((string[i + 1] & 0xF) << 2)];
}
*p++ = '=';
}
*p++ = '\0';
return p - encoded;
}
int ap_base64encode(char *encoded, const char *string, int len)
{
#ifndef CHARSET_EBCDIC
return ap_base64encode_binary(encoded, (const unsigned char *) string, len);
#else /* CHARSET_EBCDIC */
int i;
char *p;
p = encoded;
for (i = 0; i < len - 2; i += 3) {
*p++ = basis_64[(os_toascii[string[i]] >> 2) & 0x3F];
*p++ = basis_64[((os_toascii[string[i]] & 0x3) << 4) |
((int) (os_toascii[string[i + 1]] & 0xF0) >> 4)];
*p++ = basis_64[((os_toascii[string[i + 1]] & 0xF) << 2) |
((int) (os_toascii[string[i + 2]] & 0xC0) >> 6)];
*p++ = basis_64[os_toascii[string[i + 2]] & 0x3F];
}
if (i < len) {
*p++ = basis_64[(os_toascii[string[i]] >> 2) & 0x3F];
if (i == (len - 1)) {
*p++ = basis_64[((os_toascii[string[i]] & 0x3) << 4)];
*p++ = '=';
}
else {
*p++ = basis_64[((os_toascii[string[i]] & 0x3) << 4) |
((int) (os_toascii[string[i + 1]] & 0xF0) >> 4)];
*p++ = basis_64[((os_toascii[string[i + 1]] & 0xF) << 2)];
}
*p++ = '=';
}
*p++ = '\0';
return p - encoded;
#endif /* CHARSET_EBCDIC */
}
#if 0
int
main( )
{
char *str = "praveen:itisme";
char buff[1000];
char buff1[1000];
ap_base64encode( buff, str, strlen(str) );
printf( "Buff is %s\n", buff );
ap_base64decode( buff1, buff );
return 1;
}
#endif /* 0 */
|