summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornpmccallum <npmccallum@c587cffe-e639-0410-9787-d7902ae8ed56>2007-11-03 16:43:52 +0000
committernpmccallum <npmccallum@c587cffe-e639-0410-9787-d7902ae8ed56>2007-11-03 16:43:52 +0000
commit850d0f2d0de5dc7eff4f19826c3ecf39b1539c67 (patch)
treec8a848f1a7528f9635b9ae124314ed58394e6075
parentfd81f6ea8b04347657b1cea2aa7a07277b2cc6ce (diff)
downloadlibproxy-850d0f2d0de5dc7eff4f19826c3ecf39b1539c67.tar.gz
implement configuration file for proxy lockdown
git-svn-id: http://libproxy.googlecode.com/svn/trunk@33 c587cffe-e639-0410-9787-d7902ae8ed56
-rw-r--r--configure.ac8
-rw-r--r--src/lib/misc.c29
-rw-r--r--src/lib/misc.h8
-rw-r--r--src/lib/proxy_factory.c89
-rw-r--r--src/lib/url.c12
5 files changed, 110 insertions, 36 deletions
diff --git a/configure.ac b/configure.ac
index 3f12b9a..fbea624 100644
--- a/configure.ac
+++ b/configure.ac
@@ -15,7 +15,7 @@ AC_PROG_MAKE_SET
# Mozilla Javascript
AC_ARG_WITH([mozjs],
[AS_HELP_STRING([--with-mozjs],
- [build mozilla javascript PAC runner plugin @<:@default=auto@:>@])],
+ [build Mozilla JavaScript PAC runner plugin @<:@automatic@:>@])],
[],
[PKG_CHECK_EXISTS(firefox-js, with_mozjs=yes, with_mozjs=no)])
if test x$with_mozjs = xyes; then
@@ -28,7 +28,7 @@ AM_CONDITIONAL([WITH_MOZJS], [test x$with_mozjs = xyes])
# GConf
AC_ARG_WITH([gnome],
[AS_HELP_STRING([--with-gnome],
- [build GConf configuration plugin @<:@default=auto@:>@])],
+ [build GConf configuration plugin @<:@automatic@:>@])],
[],
[PKG_CHECK_EXISTS(gconf-2.0, have_gconf=yes, with_gnome=no) ;
PKG_CHECK_EXISTS(gdk-2.0, have_gdk=yes, with_gnome=no)])
@@ -47,7 +47,7 @@ AM_CONDITIONAL([WITH_GNOME], [test x$with_gnome = xyes])
# Python
AC_ARG_WITH([python],
[AS_HELP_STRING([--with-python],
- [build Python bindings @<:@default=auto@:>@])],
+ [build Python bindings @<:@automatic@:>@])],
[AM_PATH_PYTHON([2.5], with_python=yes)],
[AM_PATH_PYTHON([2.5], with_python=yes, with_python=no)])
AM_CONDITIONAL([WITH_PYTHON], [test x$with_python = xyes])
@@ -70,7 +70,7 @@ AC_C_CONST
AC_TYPE_SIZE_T
PLUGINDIR=$libdir/$PACKAGE_NAME/$PACKAGE_VERSION/plugins
AC_SUBST(PLUGINDIR)
-CFLAGS="-std=c99 $CFLAGS -DPLUGINDIR=\\\"$PLUGINDIR\\\""
+CFLAGS="-std=c99 $CFLAGS -DPLUGINDIR=\\\"$PLUGINDIR\\\" -DSYSCONFDIR=\\\"$SYSCONFDIR\\\" -D_POSIX_C_SOURCE=1"
### Checks for library functions.
AC_FUNC_MALLOC
diff --git a/src/lib/misc.c b/src/lib/misc.c
index c70d33b..02ff678 100644
--- a/src/lib/misc.c
+++ b/src/lib/misc.c
@@ -23,6 +23,7 @@
#include <string.h>
#include <unistd.h>
#include <ctype.h>
+#include <stdarg.h>
#include "misc.h"
@@ -79,6 +80,34 @@ px_strdup(const char *s)
}
/**
+ * Concatenates two or more strings into a newly allocated string
+ * @s The first string to concatenate.
+ * @... Subsequent strings. The last argument must be NULL.
+ * @return Newly allocated string
+ */
+char *
+px_strcat(const char *s, ...)
+{
+ va_list args;
+
+ // Count the number of characters to concatentate
+ va_start(args, s);
+ int count = strlen(s);
+ for (char *tmp = NULL ; (tmp = va_arg(args, char *)) ; count += strlen(tmp));
+ va_end(args);
+
+ // Build our output string
+ char *output = px_malloc0(count + 1);
+ strcat(output, s);
+ va_start(args, s);
+ for (char *tmp = NULL ; (tmp = va_arg(args, char *)) ; )
+ strcat(output, tmp);
+ va_end(args);
+
+ return output;
+}
+
+/**
* Joins NULL terminated array of strings into one string separated by delimiter
* @strv NULL terminated array of string to join
* @delimiter The string to use in between each string in the array
diff --git a/src/lib/misc.h b/src/lib/misc.h
index 9a12fbe..cab466c 100644
--- a/src/lib/misc.h
+++ b/src/lib/misc.h
@@ -51,6 +51,14 @@ char *px_strndup(const char *s, size_t n);
char *px_strdup(const char *s);
/**
+ * Concatenates two or more strings into a newly allocated string
+ * @s The first string to concatenate.
+ * @... Subsequent strings. The last argument must be NULL.
+ * @return Newly allocated string
+ */
+char *px_strcat(const char *s, ...);
+
+/**
* Joins NULL terminated array of strings into one string separated by delimiter
* @strv NULL terminated array of string to join
* @delimiter The string to use in between each string in the array
diff --git a/src/lib/proxy_factory.c b/src/lib/proxy_factory.c
index 081d171..c3877e7 100644
--- a/src/lib/proxy_factory.c
+++ b/src/lib/proxy_factory.c
@@ -65,14 +65,12 @@ _format_pac_response(char *response)
if (!strncmp(tmp, "PROXY", 5) || !strncmp(tmp, "SOCKS", 5))
{
- char *tmpa = px_strstrip(tmp + 5);
- chain[i] = px_malloc0(strlen(tmpa) + 9);
+ char *hostport = px_strstrip(tmp + 5);
if (!strncmp(tmp, "PROXY", 5))
- strcpy(chain[i], "http://");
+ chain[i] = px_strcat("http://", hostport, NULL);
else
- strcpy(chain[i], "socks://");
- strcat(chain[i], tmpa);
- px_free(tmpa);
+ chain[i] = px_strcat("socks://", hostport, NULL);
+ px_free(hostport);
}
else
chain[i] = px_strdup("direct://");
@@ -110,8 +108,7 @@ px_proxy_factory_new ()
for (i=0 ; (ent = readdir(plugindir)) ; i++)
{
// Load the plugin
- char *tmp = px_malloc0(strlen(PLUGINDIR) + strlen(ent->d_name) + 2);
- sprintf(tmp, PLUGINDIR "/%s", ent->d_name);
+ char *tmp = px_strcat(PLUGINDIR, "/", ent->d_name);
self->plugins[i] = dlopen(tmp, RTLD_LOCAL);
px_free(tmp);
if (!(self->plugins[i]))
@@ -221,6 +218,8 @@ px_proxy_factory_get_proxy (pxProxyFactory *self, char *url)
pxURL *realurl = px_url_new(url);
pxConfig *config = NULL;
char **response = px_strsplit("direct://", ";");
+ char *tmp = NULL, *order = NULL, **orderv = NULL;
+ FILE *file = NULL;
// Verify some basic stuff
if (!self) goto do_return;
@@ -231,30 +230,74 @@ px_proxy_factory_get_proxy (pxProxyFactory *self, char *url)
for (int i=0 ; self->on_get_proxy && self->on_get_proxy[i] ; i++)
self->on_get_proxy[i](self);
- // Get the configuration order
- char *tmp = NULL, **order = NULL;
+ // Open the config file
+ tmp = px_strcat(SYSCONFDIR, "/", "proxy.conf");
+ file = fopen(tmp, "r");
+ px_free(tmp);
+
+ // If we have a config file, read its info
+ if (file)
+ {
+ for (char *line = NULL ; (line = px_readline(fileno(file))) ; px_free(line))
+ {
+ // Strip whitespace
+ tmp = px_strstrip(line);
+ px_free(line);
+ line = tmp;
+
+ // Check for comment
+ if (*line == '#') continue;
+
+ // Build the string
+ if (order)
+ {
+ tmp = px_strcat(order, ",", line, NULL);
+ px_free(order);
+ order = tmp;
+ }
+ else
+ order = px_strdup(line);
+ }
+ fclose(file);
+ }
+
+ // Read the environmental info
if (getenv("PX_CONFIG_ORDER"))
{
- tmp = px_malloc0(strlen(getenv("PX_CONFIG_ORDER")) + strlen(DEFAULT_CONFIG_ORDER) + 2);
- strcpy(tmp, getenv("PX_CONFIG_ORDER"));
- strcat(tmp, ",");
+ if (order)
+ {
+ tmp = px_strcat(order, ",", getenv("PX_CONFIG_ORDER"), NULL);
+ px_free(order);
+ order = tmp;
+ }
+ else
+ order = px_strdup(getenv("PX_CONFIG_ORDER"));
+ }
+
+ // Finally add the default config
+ if (order)
+ {
+ tmp = px_strcat(order, ",", DEFAULT_CONFIG_ORDER, NULL);
+ px_free(order);
+ order = tmp;
}
else
- tmp = px_malloc0(strlen(DEFAULT_CONFIG_ORDER) + 1);
- strcat(tmp, DEFAULT_CONFIG_ORDER);
- order = px_strsplit(tmp, ",");
- px_free(tmp);
+ order = px_strdup(DEFAULT_CONFIG_ORDER);
+
+ // Create the config plugin order vector
+ orderv = px_strsplit(order, ",");
+ px_free(order);
// Get the config by searching the config order
- for (int i=0 ; order[i] && !config ; i++)
+ for (int i=0 ; orderv[i] && !config ; i++)
{
// Get the category (if applicable)
pxConfigCategory category;
- if (!strcmp(order[i], "USER"))
+ if (!strcmp(orderv[i], "USER"))
category = PX_CONFIG_CATEGORY_USER;
- else if (!strcmp(order[i], "SESSION"))
+ else if (!strcmp(orderv[i], "SESSION"))
category = PX_CONFIG_CATEGORY_SESSION;
- else if (!strcmp(order[i], "SYSTEM"))
+ else if (!strcmp(orderv[i], "SYSTEM"))
category = PX_CONFIG_CATEGORY_SYSTEM;
else
category = PX_CONFIG_CATEGORY_NONE;
@@ -263,11 +306,11 @@ px_proxy_factory_get_proxy (pxProxyFactory *self, char *url)
{
if (category != PX_CONFIG_CATEGORY_NONE && self->configs[j]->category == category)
config = self->configs[j]->callback(self);
- else if (category == PX_CONFIG_CATEGORY_NONE && !strcmp(self->configs[j]->name, order[i]))
+ else if (category == PX_CONFIG_CATEGORY_NONE && !strcmp(self->configs[j]->name, orderv[i]))
config = self->configs[j]->callback(self);
}
}
- px_strfreev(order);
+ px_strfreev(orderv);
// No config was found via search order, call all plugins
for (int i=0 ; self->configs && self->configs[i] && !config ; i++)
diff --git a/src/lib/url.c b/src/lib/url.c
index dcafba3..de397ad 100644
--- a/src/lib/url.c
+++ b/src/lib/url.c
@@ -80,7 +80,6 @@ px_url_is_valid(const char *url)
int
px_url_open(pxURL *self, const char **headers)
{
- char *request_template = "GET %s HTTP/1.1\r\nHost: %s\r\n%s\r\n\r\n";
char *request = NULL;
char *joined_headers = NULL;
int sock = -1;
@@ -104,14 +103,9 @@ px_url_open(pxURL *self, const char **headers)
joined_headers = px_strdup("");
// Create request header
- request = px_malloc0(strlen(px_url_get_path(self)) +
- strlen(px_url_get_host(self)) +
- strlen(joined_headers) +
- strlen(request_template));
- sprintf(request, request_template,
- px_url_get_path(self),
- px_url_get_host(self),
- joined_headers);
+ request = px_strcat("GET ", px_url_get_path(self),
+ " HTTP/1.1\r\nHost: ", px_url_get_host(self),
+ "\r\n", joined_headers, "\r\n\r\n");
px_free(joined_headers);
// Send HTTP request