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
|
/* $Xorg: Prefs.c,v 1.5 2001/02/09 02:05:58 xorgcvs Exp $ */
/*
Copyright 1996, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABIL-
ITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization from
The Open Group.
*/
/* $XFree86: xc/programs/xrx/rx/Prefs.c,v 1.6 2001/12/14 20:02:19 dawes Exp $ */
#include "Prefs.h"
#include <ctype.h>
#include "RxI.h" /* for Malloc & Free */
#include <X11/StringDefs.h>
#ifndef Lynx
#include <sys/socket.h>
#else
#include <socket.h>
#endif
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
typedef struct {
Boolean has_fwp;
String internal_webservers; /* servers for which we won't use a fwp */
String trusted_webservers;
String fast_webservers; /* servers for which we won't use lbx */
} Prefs;
#define offset(field) XtOffsetOf(Prefs, field)
static XtResource resources[] = {
{ "XrxHasFirewallProxy", "xrxHasFirewallProxy",
XtRBoolean, sizeof(Boolean),
offset(has_fwp), XtRImmediate, (caddr_t) False },
{ "XrxInternalWebServers", "xrxInternalWebServers",
XtRString, sizeof(String),
offset(internal_webservers), XtRImmediate, (caddr_t) NULL },
{ "XrxTrustedWebServers", "xrxTrustedWebServers",
XtRString, sizeof(String),
offset(trusted_webservers), XtRImmediate, (caddr_t) NULL },
{ "XrxFastWebServers", "xrxFastWebServers", XtRString, sizeof(String),
offset(fast_webservers), XtRImmediate, (caddr_t) NULL }
};
/*
* Return the next available element in a list of AddressFilters, making the
* list bigger when necessary.
*/
/* how much bigger we make the list every time we reach the limit */
#define SIZEINC 8
static AddressFilter *
NextAFListElem(AddressFilter **list, int *count)
{
AddressFilter *l;
int n;
l = *list;
n = *count;
/* first make sure the list is big enough */
if (n == 0) {
l = (AddressFilter *)Malloc(sizeof(AddressFilter) * SIZEINC);
if (l == NULL)
return NULL;
*list = l;
} else if (n % SIZEINC == 0) { /* we need to enlarge the list */
l = (AddressFilter *)Realloc(l, sizeof(AddressFilter) * n,
sizeof(AddressFilter) * (n + SIZEINC));
if (l == NULL)
return NULL;
*list = l;
}
/* then just return the first available element */
*count = n + 1;
return (*list) + n;
}
/*
* find the end of next element of a comma separated string,
* returning the pointer to the trailing string or NULL if there is none
*/
static char *
NextListElem(char *ptr, char **end_ret)
{
char *end = strchr(ptr, ',');
if (end != NULL) {
/* skip comma and possible following space */
ptr = end + 1;
while (*ptr && isspace(*ptr))
ptr++;
} else {
end = ptr + strlen(ptr);
ptr = NULL;
}
*end_ret = end;
return ptr;
}
/*
* retreive the two parts of mask/value from the given string, specified by
* its beginning and end, and copy them into the given buffers
*/
static int
ParseListElem(char *bos, char *eos,
char *buf1, int len1, char *buf2, int len2)
{
char *sep = strchr(bos, '/');
if (sep != NULL) {
int len = sep - bos;
if (len < len1) {
strncpy(buf1, bos, len);
buf1[len] = '\0';
/* now deal with the second part */
bos = sep + 1;
len = eos - bos;
if (len < len2) {
strncpy(buf2, bos, len);
buf2[len] = '\0';
return 1;
}
}
}
return 0; /* failed */
}
/*
* parse a comma separated list of AddressFilters: mask/value
*/
static void
ParseList(String string, AddressFilter **list_return, int *count_return)
{
char *ptr, *boe, *eoe;
#define BUFLEN 32
char mask[BUFLEN], value[BUFLEN];
AddressFilter *elem;
*list_return = NULL;
*count_return = 0;
if (string == NULL || *string == '\0')
return;
ptr = string;
do {
boe = ptr;
ptr = NextListElem(ptr, &eoe);
if (boe && eoe) {
elem = NULL;
if (ParseListElem(boe, eoe, mask, BUFLEN, value, BUFLEN) != 0) {
unsigned long imask = inet_addr(mask);
unsigned long ivalue = inet_addr(value);
if (((long) imask) != -1 && ((long) ivalue) != -1) {
elem = NextAFListElem(list_return, count_return);
elem->mask = imask;
elem->value = ivalue;
}
}
if (elem == NULL) {
/* copy whathever we can in one of our bufs to print it out */
#define MYMIN(a,b) ((a) > (b) ? (b) : (a))
int len = MYMIN(eoe - boe, BUFLEN - 1);
strncpy(mask, boe, len);
mask[len] = '\0';
fprintf(stderr,
"Could not convert \"%s\" into a pair mask/value\n",
mask);
}
}
} while (ptr && *ptr);
}
void
GetPreferences(Widget widget, Preferences *preferences)
{
Prefs prefs;
XtGetApplicationResources(widget, &prefs,
resources, XtNumber(resources),
NULL, 0);
preferences->has_fwp = prefs.has_fwp;
ParseList(prefs.internal_webservers,
&preferences->internal_webservers,
&preferences->internal_webservers_count);
ParseList(prefs.trusted_webservers,
&preferences->trusted_webservers,
&preferences->trusted_webservers_count);
ParseList(prefs.fast_webservers,
&preferences->fast_webservers,
&preferences->fast_webservers_count);
}
void
FreePreferences(Preferences *preferences)
{
if (preferences->internal_webservers)
Free(preferences->internal_webservers);
if (preferences->trusted_webservers)
Free(preferences->trusted_webservers);
if (preferences->fast_webservers)
Free(preferences->fast_webservers);
}
static Boolean
FilterHost(char *hostname, AddressFilter *filters, int count)
{
struct hostent *host;
unsigned int addr;
int i;
if (count == 0 || filters == NULL)
return False;
/* first find the host address number */
host = gethostbyname(hostname);
if (host == NULL || host->h_addrtype != AF_INET)
/* host or address type unknown */
return False;
addr = ((struct in_addr*) host->h_addr_list[0])->s_addr;
for (i = 0; i < count; i++, filters++)
if ((addr & filters->mask) == (filters->value & filters->mask))
return True;
return False;
}
void
ComputePreferences(Preferences *prefs, char *webserver,
Boolean *trusted_ret, Boolean *use_fwp_ret, Boolean *use_lbx_ret)
{
if (webserver != NULL) {
if (prefs->has_fwp == True) {
*use_fwp_ret =
FilterHost(webserver,
prefs->internal_webservers,
prefs->internal_webservers_count) ? False : True;
} else
*use_fwp_ret = False;
*trusted_ret = FilterHost(webserver,
prefs->trusted_webservers,
prefs->trusted_webservers_count);
*use_lbx_ret = FilterHost(webserver,
prefs->fast_webservers,
prefs->fast_webservers_count) ? False : True;
} else {
/* can't do much without webserver name */
*use_fwp_ret = prefs->has_fwp;
*trusted_ret = False;
*use_lbx_ret = True;
}
}
|