This is config.c in view mode; [Download] [Up]
/*
Webster Access, a program to use NeXT online Webster dictionary.
Copyright (C) 1994 Benoit Grange, ben@fizz.fdn.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <syslog.h>
#include "config.h"
#include "daemon.h"
#include "utils.h"
#ifndef CONFIGFILE
#define CONFIGFILE "websterd.conf"
#endif
const char* dictionaryFileName = NULL;
const char* thesaurusFileName = NULL;
const char* codebookFileName = NULL;
const char* greetingText = NULL;
int logFacility = LOG_DAEMON;
int tcpPort = 0, timeout = 300, debug = 0;
enum requirement requirements = none;
allow* allowedList = NULL;
static void processAllow(char *name)
{
unsigned long addr;
unsigned long mask;
allow *a;
char *c = index(name, '/');
if (c) *c++ = 0;
if ((addr = inet_addr(name)) == 0xFFFFFFFF) {
struct hostent *host;
struct netent *net;
if (net = getnetbyname(name)) {
addr = net->n_net;
} else if (host = gethostbyname(name)) {
addr = *((unsigned long *)host->h_addr);
} else {
fprintf(stderr, "%s: no host or net with name '%s'\n", pgmname, name);
return;
}
}
if (c) {
if (sscanf(c, "%lx", &mask) != 1) {
fprintf(stderr, "%s: mask '%s' is incorrect\n", pgmname, c);
return;
}
} else {
/* Gess mask */
unsigned long u = addr;
int class = 1;
while (u & 0x80000000) {
u <<= 1;
class++;
}
switch(class) {
case 1:
mask = 0xFF000000;
break;
case 2:
mask = 0xFFFF0000;
break;
case 3:
mask = 0xFFFFFF00;
break;
default:
mask = 0;
}
if (addr & ~mask) mask = 0xFFFFFFFF;
}
if (!(a = malloc(sizeof(*a)))) {
fprintf(stderr, "%s: out of memory\n", pgmname);
exit(1);
}
a->next = allowedList;
allowedList = a;
a->addr = addr;
a->mask = mask;
a->domain = NULL;
}
static void processDomain(char *name)
{
allow *a;
if (!(a = malloc(sizeof(*a)))) {
fprintf(stderr, "%s: out of memory\n", pgmname);
exit(1);
}
a->next = allowedList;
allowedList = a;
a->addr = 0;
a->mask = 0;
a->domain = strrealloc(name);
}
void readConfig(const char* filename)
{
FILE *f = fopen(CONFIGFILE, "r");
char buffer[256];
struct servent* serv;
if (!f) perror(CONFIGFILE), exit(1);
if (serv = getservbyname("webster", "tcp"))
tcpPort = serv->s_port;
else
tcpPort = 0;
#ifdef DEBUG
printf("%s: Reading config file %s\n", pgmname, CONFIGFILE);
#endif
while (fgets(buffer, sizeof(buffer), f)) {
char *c = index(buffer, '\n');
if (c) *c = 0;
if (*buffer == '#') continue;
if (!*buffer) continue;
c = index(buffer, ' ');
if (c) {
*c++ = 0;
// printf("%s %s\n", buffer, c);
if (!strcmp(buffer, "TEXT")) {
greetingText = strrealloc(c);
} else if (!strcmp(buffer, "ALLOW")) {
processAllow(c);
} else if (!strcmp(buffer, "DOMAIN")) {
processDomain(c);
} else if (!strcmp(buffer, "SYSLOG")) {
if (!strncmp(c, "local", 5) && (strlen(c) == 6)
&& (c[5] >= '0') && (c[5]<= '7')) {
// Kludge !!!
logFacility = LOG_LOCAL0 + (LOG_LOCAL1 - LOG_LOCAL0) * (c[5]-'0');
} else if (!strcmp(c, "user")) logFacility = LOG_USER;
else if (!strcmp(c, "daemon")) logFacility = LOG_DAEMON;
else fprintf(stderr, "%s: bad syslog level '%s'\n", pgmname, c);
} else if (!strcmp(buffer, "DEBUG")) {
debug = 1;
} else if (!strcmp(buffer, "PORT")) {
if (atoi(c)) tcpPort = atoi(c);
else if (serv = getservbyname(c, NULL)) {
tcpPort = serv->s_port;
} else {
fprintf(stderr, "%s: bad PORT '%s' command in config file\n", pgmname, c);
tcpPort = 0;
}
} else if (!strcmp(buffer, "REQUIRE")) {
if (!strcmp(c, "none")) requirements = 0;
else if (!strcmp(c, "hostname")) requirements |= hostname;
else fprintf(stderr, "%s: bad REQUIRE '%s' command in config file\n",
pgmname, c);
} else if (!strcmp(buffer, "TIMEOUT")) {
if (atoi(c)) timeout = atoi(c);
else fprintf(stderr, "%s: bad TIMEOUT '%s' command in config file\n",
pgmname, c);
} else fprintf(stderr, "%s: command '%s' is not recognised in config file\n",
pgmname, buffer);
} else {
fprintf(stderr, "%s: bad configuration line '%s'\n",
pgmname, buffer);
}
}
if (fclose(f)<0) perror("fclose"), exit(1);
#ifdef DEBUG
printf("%s: End of config file %s\n", pgmname, CONFIGFILE);
#endif
if (tcpPort == 0) {
fprintf(stderr, "%s: the configuration file fails to define the tcp port\n",
pgmname);
exit(1);
}
if (debug) setlogmask(LOG_UPTO(LOG_DEBUG));
else setlogmask(LOG_UPTO(LOG_INFO));
}
char* strrealloc(const char* s)
{
return strcpy(malloc(strlen(s)+1), s);
}
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.