gee
oldstatus
2005-09-19, Nathan Walp
* @file proxy.c Proxy API * Gaim is the legal property of its developers, whose names are too numerous * to list here. Please refer to the COPYRIGHT file distributed with this * 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 , 3rd draw women to it like flies to honey */ static GaimProxyInfo *global_proxy_info = NULL; static void try_connect(struct PHB *); const char* socks5errors[] = { "general SOCKS server failure\n", "connection not allowed by ruleset\n", "Command not supported\n", "Address type not supported\n" /************************************************************************** **************************************************************************/ gaim_proxy_info_new(void) return g_new0(GaimProxyInfo, 1); gaim_proxy_info_destroy(GaimProxyInfo *info) g_return_if_fail(info != NULL); if (info->host != NULL) g_free(info->host); if (info->username != NULL) g_free(info->username); if (info->password != NULL) g_free(info->password); gaim_proxy_info_set_type(GaimProxyInfo *info, GaimProxyType type) g_return_if_fail(info != NULL); gaim_proxy_info_set_host(GaimProxyInfo *info, const char *host) g_return_if_fail(info != NULL); info->host = (host == NULL ? NULL : g_strdup(host)); gaim_proxy_info_set_port(GaimProxyInfo *info, int port) g_return_if_fail(info != NULL); gaim_proxy_info_set_username(GaimProxyInfo *info, const char *username) g_return_if_fail(info != NULL); if (info->username != NULL) info->username = (username == NULL ? NULL : g_strdup(username)); gaim_proxy_info_set_password(GaimProxyInfo *info, const char *password) g_return_if_fail(info != NULL); if (info->password != NULL) info->password = (password == NULL ? NULL : g_strdup(password)); gaim_proxy_info_get_type(const GaimProxyInfo *info) g_return_val_if_fail(info != NULL, GAIM_PROXY_NONE); gaim_proxy_info_get_host(const GaimProxyInfo *info) g_return_val_if_fail(info != NULL, NULL); gaim_proxy_info_get_port(const GaimProxyInfo *info) g_return_val_if_fail(info != NULL, 0); gaim_proxy_info_get_username(const GaimProxyInfo *info) g_return_val_if_fail(info != NULL, NULL); gaim_proxy_info_get_password(const GaimProxyInfo *info) g_return_val_if_fail(info != NULL, NULL); /************************************************************************** **************************************************************************/ gaim_global_proxy_get_info(void) return global_proxy_info; /************************************************************************** **************************************************************************/ typedef void (*dns_callback_t)(GSList *hosts, gpointer data, const char *error_message); /* This structure represents both a pending DNS request and static GSList *free_dns_children = NULL; static GQueue *queued_requests = NULL; static int number_of_dns_children = 0; const int MAX_DNS_CHILDREN = 2; static void req_free(pending_dns_request_t *req) g_return_if_fail(req != NULL); number_of_dns_children--; static int send_dns_request_to_child(pending_dns_request_t *req, dns_params_t *dns_params) if(kill(req->dns_pid, 0) != 0) { gaim_debug(GAIM_DEBUG_WARNING, "dns", "DNS child %d no longer exists\n", req->dns_pid); /* Let's contact this lost child! */ rc = write(req->fd_in, dns_params, sizeof(*dns_params)); gaim_debug(GAIM_DEBUG_ERROR, "dns", "Unable to write to DNS child %d: %d\n", req->dns_pid, strerror(errno)); g_return_val_if_fail(rc == sizeof(*dns_params), -1); /* Did you hear me? (This avoids some race conditions) */ rc = read(req->fd_out, &ch, 1); gaim_debug(GAIM_DEBUG_WARNING, "dns", "DNS child %d not responding. Killing it!\n", kill(req->dns_pid, SIGKILL); gaim_debug(GAIM_DEBUG_INFO, "dns", "Successfully sent DNS request to child %d\n", req->dns_pid); static void host_resolved(gpointer data, gint source, GaimInputCondition cond); static void release_dns_child(pending_dns_request_t *req) if(queued_requests && !g_queue_is_empty(queued_requests)) { queued_dns_request_t *r = g_queue_pop_head(queued_requests); req->host = g_strdup(r->params.hostname); req->port = r->params.port; req->callback = r->callback; gaim_debug(GAIM_DEBUG_INFO, "dns", "Processing queued DNS query for '%s' with child %d\n", req->host, req->dns_pid); if(send_dns_request_to_child(req, &(r->params)) != 0) { gaim_debug(GAIM_DEBUG_WARNING, "dns", "Intent of process queued query of '%s' failed, " "requeueing...\n", r->params.hostname); g_queue_push_head(queued_requests, r); req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req); free_dns_children = g_slist_append(free_dns_children, req); static void host_resolved(gpointer data, gint source, GaimInputCondition cond) pending_dns_request_t *req = (pending_dns_request_t*)data; struct sockaddr *addr = NULL; gaim_debug(GAIM_DEBUG_INFO, "dns", "Host '%s' resolved\n", req->host); gaim_input_remove(req->inpa); rc=read(req->fd_out, &err, sizeof(err)); if((rc==4) && (err!=0)) { g_snprintf(message, sizeof(message), "DNS error: %s (pid=%d)", gai_strerror(err), req->dns_pid); g_snprintf(message, sizeof(message), "DNS error: %d (pid=%d)", gaim_debug(GAIM_DEBUG_ERROR, "dns", "%s\n", message); req->callback(NULL, req->data, message); rc=read(req->fd_out, &addrlen, sizeof(addrlen)); if(rc>0 && addrlen > 0) { rc=read(req->fd_out, addr, addrlen); hosts = g_slist_append(hosts, GINT_TO_POINTER(addrlen)); hosts = g_slist_append(hosts, addr); g_snprintf(message, sizeof(message), "Error reading from DNS child: %s",strerror(errno)); gaim_debug(GAIM_DEBUG_ERROR, "dns", "%s\n", message); req->callback(NULL, req->data, message); g_snprintf(message, sizeof(message), "EOF reading from DNS child"); gaim_debug(GAIM_DEBUG_ERROR, "dns", "%s\n", message); req->callback(NULL, req->data, message); /* wait4(req->dns_pid, NULL, WNOHANG, NULL); */ req->callback(hosts, req->data, NULL); static void trap_gdb_bug() "Gaim's DNS child got a SIGTRAP signal. \n" "This can be caused by trying to run gaim inside gdb.\n" "There is a known gdb bug which prevents this. Supposedly gaim\n" "should have detected you were using gdb and used an ugly hack,\n" "check cope_with_gdb_brokenness() in proxy.c.\n\n" "For more info about this bug, see http://sources.redhat.com/ml/gdb/2001-07/msg00349.html\n"; fputs("\n* * *\n",stderr); fputs("* * *\n\n",stderr); execlp("xmessage","xmessage","-center", message, NULL); static void cope_with_gdb_brokenness() static gboolean already_done = FALSE; snprintf(s, sizeof(s), "/proc/%d/exe", ppid); n = readlink(s, e, sizeof(e)); e[MIN(n,sizeof(e)-1)] = '\0'; gaim_debug(GAIM_DEBUG_INFO, "dns", "Debugger detected, performing useless query...\n"); gethostbyname("x.x.x.x.x"); gaim_dns_childthread(int child_out, int child_in, dns_params_t *dns_params, gboolean show_debug) struct addrinfo hints, *res, *tmp; const size_t addrlen = sizeof(sin); signal(SIGQUIT, SIG_DFL); signal(SIGCHLD, SIG_DFL); signal(SIGTERM, SIG_DFL); signal(SIGTRAP, trap_gdb_bug); if (dns_params->hostname[0] == '\0') { struct timeval tv = { .tv_sec = 40 , .tv_usec = 0 }; rc = select(child_in + 1, &fds, NULL, NULL, &tv); fprintf(stderr,"dns[%d]: nobody needs me... =(\n", getpid()); rc = read(child_in, dns_params, sizeof(dns_params_t)); fprintf(stderr,"dns[%d]: Oops, father has gone, wait for me, wait...!\n", getpid()); if (dns_params->hostname[0] == '\0') { fprintf(stderr, "dns[%d]: hostname = \"\" (port = %d)!!!\n", getpid(), dns_params->port); g_snprintf(servname, sizeof(servname), "%d", dns_params->port); memset(&hints,0,sizeof(hints)); /* This is only used to convert a service * name to a port number. As we know we are * passing a number already, we know this * value will not be really used by the C hints.ai_socktype = SOCK_STREAM; rc = getaddrinfo(dns_params->hostname, servname, &hints, &res); write(child_out, &rc, sizeof(rc)); fprintf(stderr,"dns[%d] Error: getaddrinfo returned %d\n", dns_params->hostname[0] = '\0'; size_t ai_addrlen = res->ai_addrlen; write(child_out, &ai_addrlen, sizeof(ai_addrlen)); write(child_out, res->ai_addr, res->ai_addrlen); write(child_out, &zero, sizeof(zero)); if (!inet_aton(dns_params->hostname, &sin.sin_addr)) { if (!(hp = gethostbyname(dns_params->hostname))) { write(child_out, &h_errno, sizeof(int)); fprintf(stderr,"DNS Error: %d\n", h_errno); memset(&sin, 0, sizeof(struct sockaddr_in)); memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); sin.sin_family = hp->h_addrtype; sin.sin_family = AF_INET; write(child_out, &rc, sizeof(rc)); sin.sin_port = htons(dns_params->port); write(child_out, &addrlen, sizeof(addrlen)); write(child_out, &sin, addrlen); write(child_out, &zero, sizeof(zero)); dns_params->hostname[0] = '\0'; int gaim_gethostbyname_async(const char *hostname, int port, dns_callback_t callback, gpointer data) pending_dns_request_t *req = NULL; host_temp = g_strstrip(g_strdup(hostname)); strncpy(dns_params.hostname, host_temp, sizeof(dns_params.hostname) - 1); dns_params.hostname[sizeof(dns_params.hostname) - 1] = '\0'; * If we have any children, attempt to have them perform the DNS * query. If we're able to send the query to a child, then req * will be set to the pending_dns_request_t. Otherwise, req will * be NULL and we'll need to create a new DNS request child. while (free_dns_children != NULL) { req = free_dns_children->data; free_dns_children = g_slist_remove(free_dns_children, req); if (send_dns_request_to_child(req, &dns_params) == 0) /* We found an acceptable child, yay */ /* We need to create a new DNS request child */ int child_out[2], child_in[2]; if (number_of_dns_children >= MAX_DNS_CHILDREN) { queued_dns_request_t *r = g_new(queued_dns_request_t, 1); memcpy(&(r->params), &dns_params, sizeof(dns_params)); queued_requests = g_queue_new(); g_queue_push_tail(queued_requests, r); gaim_debug(GAIM_DEBUG_INFO, "dns", "DNS query for '%s' queued\n", dns_params.hostname); /* Create pipes for communicating with the child process */ if (pipe(child_out) || pipe(child_in)) { gaim_debug(GAIM_DEBUG_ERROR, "dns", "Could not create pipes: %s\n", strerror(errno)); req = g_new(pending_dns_request_t, 1); cope_with_gdb_brokenness(); /* If we are the child process... */ /* We should not access the parent's side of the pipe, so close them... */ gaim_dns_childthread(child_out[1], child_in[0], &dns_params, opt_debug); /* The thread calls _exit() rather than returning, so we never get here */ /* We should not access the child's side of the pipe, so close them... */ if (req->dns_pid == -1) { gaim_debug(GAIM_DEBUG_ERROR, "dns", "Could not create child process for DNS: %s\n", req->fd_out = child_out[0]; req->fd_in = child_in[1]; number_of_dns_children++; gaim_debug(GAIM_DEBUG_INFO, "dns", "Created new DNS child %d, there are now %d children.\n", req->dns_pid, number_of_dns_children); req->host = g_strdup(hostname); req->callback = callback; req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req); #elif defined _WIN32 /* end __unix__ */ /* Note: The below win32 implementation is actually platform independent. Perhaps this can be used in place of the above platform dependent code. typedef struct _dns_tdata { static gboolean dns_main_thread_cb(gpointer data) { dns_tdata *td = (dns_tdata*)data; if (td->errmsg != NULL) { gaim_debug_info("dns", "%s\n", td->errmsg); td->callback(td->hosts, td->data, td->errmsg); static gpointer dns_thread(gpointer data) { dns_tdata *td = (dns_tdata*)data; if ((hp = gethostbyname(td->hostname))) { memset(&sin, 0, sizeof(struct sockaddr_in)); memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); sin.sin_family = hp->h_addrtype; sin.sin_port = htons(td->port); td->hosts = g_slist_append(td->hosts, GINT_TO_POINTER(sizeof(sin))); td->hosts = g_slist_append(td->hosts, g_memdup(&sin, sizeof(sin))); td->errmsg = g_strdup_printf("DNS error: %d", errno); /* back to main thread */ g_idle_add(dns_main_thread_cb, td); int gaim_gethostbyname_async(const char *hostname, int port, dns_callback_t callback, gpointer data) { if(inet_aton(hostname, &sin.sin_addr)) { sin.sin_family = AF_INET; sin.sin_port = htons(port); hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin))); hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin))); callback(hosts, data, NULL); gaim_debug_info("dns", "DNS Lookup for: %s\n", hostname); td = g_new0(dns_tdata, 1); td->hostname = g_strdup(hostname); if(!g_thread_create(dns_thread, td, FALSE, &err)) { gaim_debug_error("dns", "DNS thread create failure: %s\n", err?err->message:""); #else /* not __unix__ or _WIN32 */ static gboolean host_resolved(gpointer data) pending_dns_request_t *req = (pending_dns_request_t*)data; hosts = g_slist_append(hosts, GINT_TO_POINTER(req->addrlen)); hosts = g_slist_append(hosts, req->addr); req->callback(hosts, req->data, NULL); gaim_gethostbyname_async(const char *hostname, int port, dns_callback_t callback, gpointer data) pending_dns_request_t *req; if (!inet_aton(hostname, &sin.sin_addr)) { if(!(hp = gethostbyname(hostname))) { gaim_debug(GAIM_DEBUG_ERROR, "dns", "gaim_gethostbyname(\"%s\", %d) failed: %d\n", hostname, port, h_errno); memset(&sin, 0, sizeof(struct sockaddr_in)); memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); sin.sin_family = hp->h_addrtype; sin.sin_family = AF_INET; sin.sin_port = htons(port); req = g_new(pending_dns_request_t, 1); req->addr = (struct sockaddr*) g_memdup(&sin, sizeof(sin)); req->addrlen = sizeof(sin); req->callback = callback; gaim_timeout_add(10, host_resolved, req); #endif /* not __unix__ or _WIN32 */ no_one_calls(gpointer data, gint source, GaimInputCondition cond) gaim_debug(GAIM_DEBUG_INFO, "proxy", "Connected.\n"); * getsockopt after a non-blocking connect returns -1 if something is * really messed up (bad descriptor, usually). Otherwise, it returns 0 and * error holds what connect would have returned if it blocked until now. * Thus, error == 0 is success, error == EINPROGRESS means "try again", * and anything else is a real error. * (error == EINPROGRESS can happen after a select because the kernel can * be overly optimistic sometimes. select is just a hint that you might be ret = getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len); if (ret == 0 && error == EINPROGRESS) return; /* we'll be called again later */ if (ret < 0 || error != 0) { if(ret!=0) error = errno; gaim_input_remove(phb->inpa); gaim_debug(GAIM_DEBUG_ERROR, "proxy", "getsockopt SO_ERROR check: %s\n", strerror(error)); fcntl(source, F_SETFL, 0); gaim_input_remove(phb->inpa); if (phb->account == NULL || gaim_account_get_connection(phb->account) != NULL) { phb->func(phb->data, source, GAIM_INPUT_READ); static gboolean clean_connect(gpointer data) if (phb->account == NULL || gaim_account_get_connection(phb->account) != NULL) { phb->func(phb->data, phb->port, GAIM_INPUT_READ); proxy_connect_none(struct PHB *phb, struct sockaddr *addr, socklen_t addrlen) gaim_debug(GAIM_DEBUG_INFO, "proxy", "Connecting to %s:%d with no proxy\n", phb->host, phb->port); if ((fd = socket(addr->sa_family, SOCK_STREAM, 0)) < 0) { gaim_debug(GAIM_DEBUG_ERROR, "proxy", "Unable to create socket: %s\n", strerror(errno)); fcntl(fd, F_SETFL, O_NONBLOCK); fcntl(fd, F_SETFD, FD_CLOEXEC); if (connect(fd, (struct sockaddr *)addr, addrlen) < 0) { if ((errno == EINPROGRESS) || (errno == EINTR)) { gaim_debug(GAIM_DEBUG_WARNING, "proxy", "Connect would have blocked.\n"); phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, no_one_calls, phb); gaim_debug(GAIM_DEBUG_ERROR, "proxy", "Connect failed: %s\n", strerror(errno)); gaim_debug(GAIM_DEBUG_MISC, "proxy", "Connect didn't block.\n"); if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { gaim_debug(GAIM_DEBUG_ERROR, "proxy", "getsockopt failed.\n"); phb->port = fd; /* bleh */ gaim_timeout_add(50, clean_connect, phb); /* we do this because we never want to call our callback #define HTTP_GOODSTRING "HTTP/1.0 200" #define HTTP_GOODSTRING2 "HTTP/1.1 200" http_complete(struct PHB *phb, gint source) gaim_debug(GAIM_DEBUG_INFO, "http proxy", "proxy connection established\n"); } else if(!phb->account || phb->account->gc) { phb->func(phb->data, source, GAIM_INPUT_READ); /* read the response to the CONNECT request, if we are requesting a non-port-80 tunnel */ http_canread(gpointer data, gint source, GaimInputCondition cond) int minor, major, status, error=0; char inputline[8192], *p; gaim_input_remove(phb->inpa); while ((pos < sizeof(inputline)-1) && (nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) { if (inputline[pos - 1] == '\n') else if (inputline[pos - 1] != '\r') error = strncmp(inputline, "HTTP/", 5) != 0; major = strtol(p, &p, 10); error = (major==0) || (*p != '.'); minor = strtol(p, &p, 10); status = strtol(p, &p, 10); gaim_debug(GAIM_DEBUG_ERROR, "proxy", "Unable to parse proxy's response: %s\n", inputline); gaim_debug(GAIM_DEBUG_ERROR, "proxy", "Proxy server replied with:\n%s\n", p); /* XXX: why in the hell are we calling gaim_connection_error() here? */ if ( status == 403 /* Forbidden */ ) { gchar *msg = g_strdup_printf(_("Access denied: proxy server forbids port %d tunnelling."), phb->port); gaim_connection_error(phb->account->gc, msg); char *msg = g_strdup_printf(_("Proxy connection error %d"), status); gaim_connection_error(phb->account->gc, msg); http_complete(phb, source); http_canwrite(gpointer data, gint source, GaimInputCondition cond) gaim_debug(GAIM_DEBUG_INFO, "http proxy", "Connected.\n"); gaim_input_remove(phb->inpa); if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { gaim_debug(GAIM_DEBUG_INFO, "proxy", "using CONNECT tunnelling for %s:%d\n", phb->host, phb->port); request_len = g_snprintf(request, sizeof(request), "CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n", phb->host, phb->port, phb->host, phb->port); if (gaim_proxy_info_get_username(phb->gpi) != NULL) { t1 = g_strdup_printf("%s:%s", gaim_proxy_info_get_username(phb->gpi), gaim_proxy_info_get_password(phb->gpi) ? gaim_proxy_info_get_password(phb->gpi) : ""); t2 = gaim_base64_encode(t1, strlen(t1)); g_return_if_fail(request_len < sizeof(request)); request_len += g_snprintf(request + request_len, sizeof(request) - request_len, "Proxy-Authorization: Basic %s\r\n", t2); g_return_if_fail(request_len < sizeof(request)); strcpy(request + request_len, "\r\n"); if (write(source, request, request_len) < 0) { /* register the response handler for the CONNECT request */ phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, http_canread, phb); proxy_connect_http(struct PHB *phb, struct sockaddr *addr, socklen_t addrlen) gaim_debug(GAIM_DEBUG_INFO, "http proxy", "Connecting to %s:%d via %s:%d using HTTP\n", gaim_proxy_info_get_host(phb->gpi), gaim_proxy_info_get_port(phb->gpi)); if ((fd = socket(addr->sa_family, SOCK_STREAM, 0)) < 0) { fcntl(fd, F_SETFL, O_NONBLOCK); fcntl(fd, F_SETFD, FD_CLOEXEC); if (connect(fd, addr, addrlen) < 0) { if ((errno == EINPROGRESS) || (errno == EINTR)) { gaim_debug(GAIM_DEBUG_WARNING, "http proxy", "Connect would have blocked.\n"); /* we need to do CONNECT first */ phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, gaim_debug(GAIM_DEBUG_MISC, "http proxy", "Connect didn't block.\n"); if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { http_canwrite(phb, fd, GAIM_INPUT_WRITE); s4_canread(gpointer data, gint source, GaimInputCondition cond) unsigned char packet[12]; gaim_input_remove(phb->inpa); memset(packet, 0, sizeof(packet)); if (read(source, packet, 9) >= 4 && packet[1] == 90) { if (phb->account == NULL || gaim_account_get_connection(phb->account) != NULL) { phb->func(phb->data, source, GAIM_INPUT_READ); s4_canwrite(gpointer data, gint source, GaimInputCondition cond) unsigned char packet[12]; gaim_debug(GAIM_DEBUG_INFO, "s4 proxy", "Connected.\n"); gaim_input_remove(phb->inpa); if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { fcntl(source, F_SETFL, 0); * The socks4 spec doesn't include support for doing host name * lookups by the proxy. Some socks4 servers do this via * extensions to the protocol. Since we don't know if a * server supports this, it would need to be implemented * with an option, or some detection mechanism - in the * meantime, stick with plain old SOCKS4. if (!(hp = gethostbyname(phb->host))) { packet[2] = phb->port >> 8; packet[3] = phb->port & 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]; if (write(source, packet, 9) != 9) { phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s4_canread, phb); proxy_connect_socks4(struct PHB *phb, struct sockaddr *addr, socklen_t addrlen) gaim_debug(GAIM_DEBUG_INFO, "socks4 proxy", "Connecting to %s:%d via %s:%d using SOCKS4\n", gaim_proxy_info_get_host(phb->gpi), gaim_proxy_info_get_port(phb->gpi)); if ((fd = socket(addr->sa_family, SOCK_STREAM, 0)) < 0) fcntl(fd, F_SETFL, O_NONBLOCK); fcntl(fd, F_SETFD, FD_CLOEXEC); if (connect(fd, addr, addrlen) < 0) { if ((errno == EINPROGRESS) || (errno == EINTR)) { gaim_debug(GAIM_DEBUG_WARNING, "socks4 proxy", "Connect would have blocked.\n"); phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, s4_canwrite, phb); gaim_debug(GAIM_DEBUG_MISC, "socks4 proxy", "Connect didn't block.\n"); if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { s4_canwrite(phb, fd, GAIM_INPUT_WRITE); s5_canread_again(gpointer data, gint source, GaimInputCondition cond) gaim_input_remove(phb->inpa); gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy", "Able to read again.\n"); if (read(source, buf, 4) < 4) { gaim_debug(GAIM_DEBUG_WARNING, "socks5 proxy", "or not...\n"); if ((buf[0] != 0x05) || (buf[1] != 0x00)) { if ((buf[0] == 0x05) && (buf[1] < 0x09)) gaim_debug(GAIM_DEBUG_ERROR, "socks5 proxy", socks5errors[buf[1]]); gaim_debug(GAIM_DEBUG_ERROR, "socks5 proxy", "Bad data.\n"); case 0x01: /* the address is a version-4 IP address, with a length of 4 octets */ case 0x03: /* the address field contains a fully-qualified domain name. The first octet of the address field contains the number of octets of name that follow, there is no terminating NUL octet. */ read(source, buf, buf[0]); case 0x04: /* the address is a version-6 IP address, with a length of 16 octets */ if (phb->account == NULL || gaim_account_get_connection(phb->account) != NULL) { phb->func(phb->data, source, GAIM_INPUT_READ); s5_sendconnect(gpointer data, gint source) int hlen = strlen(phb->host); buf[1] = 0x01; /* CONNECT */ buf[2] = 0x00; /* reserved */ buf[3] = 0x03; /* address type -- host name */ memcpy(buf + 5, phb->host, hlen); buf[5 + hlen] = phb->port >> 8; buf[5 + hlen + 1] = phb->port & 0xff; if (write(source, buf, (5 + hlen + 2)) < (5 + hlen + 2)) { phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread_again, phb); s5_readauth(gpointer data, gint source, GaimInputCondition cond) gaim_input_remove(phb->inpa); gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy", "Got auth response.\n"); if (read(source, buf, 2) < 2) { if ((buf[0] != 0x01) || (buf[1] != 0x00)) { s5_sendconnect(phb, source); static void hmacmd5_chap(const unsigned char * challenge, int challen, const char * passwd, unsigned char * response) unsigned char Kxoripad[65]; unsigned char Kxoropad[65]; md5_append(&ctx, (unsigned char *)passwd, strlen(passwd)); md5_finish(&ctx, (unsigned char *)md5buf); memset(Kxoripad,0,sizeof(Kxoripad)); memset(Kxoropad,0,sizeof(Kxoropad)); memcpy(Kxoripad,pwinput,pwlen); memcpy(Kxoropad,pwinput,pwlen); md5_append(&ctx, Kxoripad, 64); md5_append(&ctx, challenge, challen); md5_finish(&ctx, (unsigned char *)Kxoripad); md5_append(&ctx, Kxoropad, 64); md5_append(&ctx, Kxoripad, 16); md5_finish(&ctx, response); s5_readchap(gpointer data, gint source, GaimInputCondition cond) unsigned char cmdbuf[20]; gaim_input_remove(phb->inpa); gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy", "Got CHAP response.\n"); if (read(source, cmdbuf, 2) < 2) { for (currentav = 0; currentav < navas; currentav++) { if (read(source, cmdbuf, 2) < 2) { if (read(source, buf, cmdbuf[1]) < cmdbuf[1]) { s5_sendconnect(phb, source); gaim_debug_warning("proxy", "socks5 CHAP authentication " "failed. Disconnecting..."); /* Server wants our credentials */ hmacmd5_chap(buf, cmdbuf[1], gaim_proxy_info_get_password(phb->gpi), if (write(source, buf, 20) < 20) { /* Server wants to select an algorithm */ /* Only currently support HMAC-MD5 */ gaim_debug_warning("proxy", "Server tried to select an " "algorithm that we did not advertise " "as supporting. This is a violation " "of the socks5 CHAP specification. " /* Fell through. We ran out of CHAP events to process, but haven't * succeeded or failed authentication - there may be more to come. * If this is the case, come straight back here. */ phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_readchap, phb); s5_canread(gpointer data, gint source, GaimInputCondition cond) gaim_input_remove(phb->inpa); gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy", "Able to read.\n"); if (read(source, buf, 2) < 2) { if ((buf[0] != 0x05) || (buf[1] == 0xff)) { u = gaim_proxy_info_get_username(phb->gpi); p = gaim_proxy_info_get_password(phb->gpi); i = (u == NULL) ? 0 : strlen(u); j = (p == NULL) ? 0 : strlen(p); buf[0] = 0x01; /* version 1 */ memcpy(buf + 2 + i + 1, p, j); if (write(source, buf, 3 + i + j) < 3 + i + j) { phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_readauth, phb); } else if (buf[1] == 0x03) { userlen = strlen(gaim_proxy_info_get_username(phb->gpi)); memcpy(buf + 7, gaim_proxy_info_get_username(phb->gpi), userlen); if (write(source, buf, 7 + userlen) < 7 + userlen) { phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_readchap, phb); s5_sendconnect(phb, source); s5_canwrite(gpointer data, gint source, GaimInputCondition cond) gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy", "Connected.\n"); gaim_input_remove(phb->inpa); if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { fcntl(source, F_SETFL, 0); buf[0] = 0x05; /* SOCKS version 5 */ if (gaim_proxy_info_get_username(phb->gpi) != NULL) { buf[1] = 0x03; /* three methods */ buf[2] = 0x00; /* no authentication */ buf[3] = 0x03; /* CHAP authentication */ buf[4] = 0x02; /* username/password authentication */ if (write(source, buf, i) < i) { gaim_debug(GAIM_DEBUG_ERROR, "socks5 proxy", "Unable to write\n"); phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread, phb); proxy_connect_socks5(struct PHB *phb, struct sockaddr *addr, socklen_t addrlen) gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy", "Connecting to %s:%d via %s:%d using SOCKS5\n", gaim_proxy_info_get_host(phb->gpi), gaim_proxy_info_get_port(phb->gpi)); if ((fd = socket(addr->sa_family, SOCK_STREAM, 0)) < 0) fcntl(fd, F_SETFL, O_NONBLOCK); fcntl(fd, F_SETFD, FD_CLOEXEC); if (connect(fd, addr, addrlen) < 0) { if ((errno == EINPROGRESS) || (errno == EINTR)) { gaim_debug(GAIM_DEBUG_WARNING, "socks5 proxy", "Connect would have blocked.\n"); phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, s5_canwrite, phb); gaim_debug(GAIM_DEBUG_MISC, "socks5 proxy", "Connect didn't block.\n"); if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { s5_canwrite(phb, fd, GAIM_INPUT_WRITE); static void try_connect(struct PHB *phb) addrlen = GPOINTER_TO_INT(phb->hosts->data); phb->hosts = g_slist_remove(phb->hosts, phb->hosts->data); phb->hosts = g_slist_remove(phb->hosts, phb->hosts->data); switch (gaim_proxy_info_get_type(phb->gpi)) { ret = proxy_connect_none(phb, addr, addrlen); ret = proxy_connect_http(phb, addr, addrlen); ret = proxy_connect_socks4(phb, addr, addrlen); ret = proxy_connect_socks5(phb, addr, addrlen); case GAIM_PROXY_USE_ENVVAR: ret = proxy_connect_http(phb, addr, addrlen); if (phb->account == NULL || gaim_account_get_connection(phb->account) != NULL) { phb->func(phb->data, -1, GAIM_INPUT_READ); connection_host_resolved(GSList *hosts, gpointer data, const char *error_message) struct PHB *phb = (struct PHB*)data; gaim_proxy_connect(GaimAccount *account, const char *host, int port, GaimInputFunction func, gpointer data) const char *connecthost = host; g_return_val_if_fail(host != NULL, -1); g_return_val_if_fail(port != 0 && port != -1, -1); g_return_val_if_fail(func != NULL, -1); phb = g_new0(struct PHB, 1); if (account == NULL || gaim_account_get_proxy_info(account) == NULL) phb->gpi = gaim_global_proxy_get_info(); phb->gpi = gaim_account_get_proxy_info(account); phb->host = g_strdup(host); if (gaim_proxy_info_get_type(phb->gpi) == GAIM_PROXY_USE_ENVVAR) { if ((tmp = g_getenv("HTTP_PROXY")) != NULL || (tmp = g_getenv("http_proxy")) != NULL || (tmp = g_getenv("HTTPPROXY")) != NULL) { char *proxyhost,*proxypath,*proxyuser,*proxypasswd; * export http_proxy="http://user:passwd@your.proxy.server:port/" if(gaim_url_parse(tmp, &proxyhost, &proxyport, &proxypath, &proxyuser, &proxypasswd)) { gaim_proxy_info_set_host(phb->gpi, proxyhost); gaim_proxy_info_set_username(phb->gpi, proxyuser); if (proxypasswd != NULL) { gaim_proxy_info_set_password(phb->gpi, proxypasswd); /* only for backward compatibility */ ((tmp = g_getenv("HTTP_PROXY_PORT")) != NULL || (tmp = g_getenv("http_proxy_port")) != NULL || (tmp = g_getenv("HTTPPROXYPORT")) != NULL)) gaim_proxy_info_set_port(phb->gpi, proxyport); /* no proxy environment variable found, don't use a proxy */ gaim_debug_info("proxy", "No environment settings found, not using a proxy\n"); gaim_proxy_info_set_type(phb->gpi, GAIM_PROXY_NONE); /* XXX: Do we want to skip this step if user/password were part of url? */ if ((tmp = g_getenv("HTTP_PROXY_USER")) != NULL || (tmp = g_getenv("http_proxy_user")) != NULL || (tmp = g_getenv("HTTPPROXYUSER")) != NULL) gaim_proxy_info_set_username(phb->gpi, tmp); if ((tmp = g_getenv("HTTP_PROXY_PASS")) != NULL || (tmp = g_getenv("http_proxy_pass")) != NULL || (tmp = g_getenv("HTTPPROXYPASS")) != NULL) gaim_proxy_info_set_password(phb->gpi, tmp); if ((gaim_proxy_info_get_type(phb->gpi) != GAIM_PROXY_NONE) && (gaim_proxy_info_get_host(phb->gpi) == NULL || gaim_proxy_info_get_port(phb->gpi) <= 0)) { gaim_notify_error(NULL, NULL, _("Invalid proxy settings"), _("Either the host name or port number specified for your given proxy type is invalid.")); switch (gaim_proxy_info_get_type(phb->gpi)) case GAIM_PROXY_USE_ENVVAR: connecthost = gaim_proxy_info_get_host(phb->gpi); connectport = gaim_proxy_info_get_port(phb->gpi); return gaim_gethostbyname_async(connecthost, connectport, connection_host_resolved, phb); gaim_proxy_connect_socks5(GaimProxyInfo *gpi, const char *host, int port, GaimInputFunction func, gpointer data) phb = g_new0(struct PHB, 1); phb->host = g_strdup(host); return gaim_gethostbyname_async(gaim_proxy_info_get_host(gpi), gaim_proxy_info_get_port(gpi), connection_host_resolved, phb); proxy_pref_cb(const char *name, GaimPrefType type, gpointer value, GaimProxyInfo *info = gaim_global_proxy_get_info(); if (!strcmp(name, "/core/proxy/type")) { if (!strcmp(type, "none")) proxytype = GAIM_PROXY_NONE; else if (!strcmp(type, "http")) proxytype = GAIM_PROXY_HTTP; else if (!strcmp(type, "socks4")) proxytype = GAIM_PROXY_SOCKS4; else if (!strcmp(type, "socks5")) proxytype = GAIM_PROXY_SOCKS5; else if (!strcmp(type, "envvar")) proxytype = GAIM_PROXY_USE_ENVVAR; gaim_proxy_info_set_type(info, proxytype); } else if (!strcmp(name, "/core/proxy/host")) gaim_proxy_info_set_host(info, value); else if (!strcmp(name, "/core/proxy/port")) gaim_proxy_info_set_port(info, GPOINTER_TO_INT(value)); else if (!strcmp(name, "/core/proxy/username")) gaim_proxy_info_set_username(info, value); else if (!strcmp(name, "/core/proxy/password")) gaim_proxy_info_set_password(info, value); /* Initialize a default proxy info struct. */ global_proxy_info = gaim_proxy_info_new(); gaim_prefs_add_none("/core/proxy"); gaim_prefs_add_string("/core/proxy/type", "none"); gaim_prefs_add_string("/core/proxy/host", ""); gaim_prefs_add_int("/core/proxy/port", 0); gaim_prefs_add_string("/core/proxy/username", ""); gaim_prefs_add_string("/core/proxy/password", ""); /* Setup callbacks for the preferences. */ gaim_prefs_connect_callback("/core/proxy/type", gaim_prefs_connect_callback("/core/proxy/host", gaim_prefs_connect_callback("/core/proxy/port", gaim_prefs_connect_callback("/core/proxy/username", gaim_prefs_connect_callback("/core/proxy/password", if(!g_thread_supported())