diff options
| author | npmccallum <npmccallum@c587cffe-e639-0410-9787-d7902ae8ed56> | 2007-11-03 16:43:52 +0000 |
|---|---|---|
| committer | npmccallum <npmccallum@c587cffe-e639-0410-9787-d7902ae8ed56> | 2007-11-03 16:43:52 +0000 |
| commit | 081aae6fa631a42b4bde929f7936420153266ff3 (patch) | |
| tree | c8a848f1a7528f9635b9ae124314ed58394e6075 /src/lib | |
| parent | 8163eece5b4f6bd96db9d72e609cd38b2650950a (diff) | |
| download | libproxy-git-081aae6fa631a42b4bde929f7936420153266ff3.tar.gz | |
implement configuration file for proxy lockdown
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/misc.c | 29 | ||||
| -rw-r--r-- | src/lib/misc.h | 8 | ||||
| -rw-r--r-- | src/lib/proxy_factory.c | 89 | ||||
| -rw-r--r-- | src/lib/url.c | 12 |
4 files changed, 106 insertions, 32 deletions
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 |
