leafnode  1.12.0
About: Leafnode is a store & forward NNTP proxy for small (dialup) sites.
  Fossies Dox: leafnode-1.12.0.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

leafnode.h
Go to the documentation of this file.
1/*
2 * This file is copyrighted material. See the file COPYING for licensing
3 * conditions.
4 */
5
6/* $Id: leafnode.h,v 1.95 2008/08/07 20:19:41 emma Exp $ */
7
8#ifndef LEAFNODE_H
9#define LEAFNODE_H
10
11#include "config.h"
12
13/* newer glibc needs this so it doesn't complain about _BSD_SOURCE */
14#ifndef _DEFAULT_SOURCE
15#define _DEFAULT_SOURCE
16#endif
17
18#ifndef _GNU_SOURCE
19#define _GNU_SOURCE
20#endif
21#ifndef _BSD_SOURCE
22#define _BSD_SOURCE
23#endif
24
25#include "critmem.h"
26
27/* I wish the world were a happy place */
28#ifndef TRUE
29#define TRUE (1)
30#endif
31#ifndef FALSE
32#define FALSE (0)
33#endif
34
35#define PLURAL(no) (((no) == 1) ? "" : "s")
36
37/* limits.h may contain PATH_MAX but shall not if the system has variable
38 * length limits here */
39#include <limits.h>
40#ifndef PATH_MAX
41/* just some random limit - we'll use this as a starting point
42 * for dynamically growing memory only */
43#define PATH_MAX 4096
44#endif
45
46#define PORTFILENAMECSET "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-"
47
48#define PCRE2_CODE_UNIT_WIDTH 0 /* let's fail unless explicitly using the _8() functions */
49#include <pcre2.h>
50#include <stdarg.h> /* va_list */
51#include <stdio.h> /* FILE * */
52#include <setjmp.h> /* sigjmp_buf */
53#include "system.h" /* time.h */
54
55#ifdef HAVE_AP_CONFIG_H
56#define AP_CONFIG_H
57#endif
58
59#include <errno.h>
60
61/* from strdup.c */
62#ifndef HAVE_STRDUP
63char *strdup(const char *);
64#endif
65
66/* from snprintf.c */
67#ifndef HAVE_WORKING_SNPRINTF
68int ln_snprintf(char *str, size_t n, const char *format, ...)
69#ifdef __GNUC__
70 __attribute__ ((format(printf, 3, 4)))
71#endif
72 ;
73int ln_vsnprintf(char *str, size_t n, const char *format, va_list ap);
74#define snprintf ln_snprintf
75#define vsnprintf ln_vsnprintf
76#endif
77
78int xsnprintf(/*@out@*/ char *str, size_t n, const char *format, ...)
79#ifdef __GNUC__
80 __attribute__ ((format(printf, 3, 4)))
81#endif
82 ;
83
84/* map LOG_NEWS onto LOG_DAEMON where the former doesn't exist */
85#include <syslog.h>
86#if !defined( LOG_NEWS )
87#define LOG_NEWS LOG_DAEMON
88#endif
89/* define LOG_CONS if missing */
90#if !defined( LOG_CONS )
91#define LOG_CONS 0 /* if it isn't supported, make do without */
92#endif
93
94#define SECONDS_PER_DAY ( 24L * 60 * 60 )
95
96/* Limit on the number of message bodies marked for download per group. */
97#define BODY_DOWNLOAD_LIMIT 2048
98
99/* initialize global variables */
100int initvars(char *progname);
101
102/* converts a message-id to a file name, the return value points into
103 a static array */
104#define LOOKUP_FREE ((const char *)-2)
105/*@dependent@*/ const char *lookup(const char *msgid);
106
107/*@dependent@*/
108char *getaline(FILE * f); /* reads one line, regardless of length */
109/*@dependent@*/
110char *mgetaline(FILE * f); /* dito, with timeout */
111void mgetaline_settimeout(unsigned int); /* set timeout for mgetaline */
112extern sigjmp_buf timeout;
113extern void timer(int sig);
114
115/* changes (and optionally creates) directory */
116int chdirgroup(const char *group, int creatdir);
117
118/*
119 * newsgroup management
120 */
121struct newsgroup {
122 unsigned long first;
123 unsigned long last;
124 char *name;
125 char *desc;
126 time_t age;
127};
128
129int isinteresting(const char *groupname);
130void insertgroup(const char *name, long unsigned first, long unsigned last,
131 time_t date);
132extern void changegroupdesc(const char *groupname, const char *description);
133extern void newgroupdesc(const char *groupname, const char *description);
134void mergegroups(void);
135/*@null@*//*@dependent@*/
136struct newsgroup *xfindgroup(struct newsgroup *active, const char *name,
137 unsigned long size);
138/*@null@*//*@dependent@*/
139struct newsgroup *findgroup(const char *name);
140void readactive(void);
141int writeactive(void);
142void fakeactive(void);
143void freeactive(/*@null@*/ struct newsgroup *act);
144extern char *activeread(void);
145extern int killactiveread(void);
146
147extern size_t activesize;
148/*@null@*/ extern struct newsgroup *active;
149extern size_t oldactivesize;
150/*@null@*/ extern struct newsgroup *oldactive;
151
152/* translation from message-id to article number, used in fetch and expire */
153
154void clearidtree(void);
155void insertmsgid( /*@unique@*/ const char *msgid);
156int findmsgid(const char *msgid);
157typedef int (*tmihook)(const char *);
159
160/* -----------here starts the new stuff-----------------*/
161
162/*
163 * a linear list of strings
164 */
167 char string[1];
168};
169
170void
171prependtolist(struct stringlist **list, /*@unique@*/ const char *newentry);
172
173/*@null@*//*@dependent@*/
174struct stringlist **lfindinlist(struct stringlist **haystack, char *needle, size_t len);
175 /* find a stringlist element by doing a linear search */
176char *findinlist(struct stringlist *haystack, char *needle);
177 /* find a string in a stringlist by doing a linear search */
178void freelist( /*@only@*/ struct stringlist *list);
179 /* free memory occupied by a stringlist */
180
181/*
182 * filterfile.c -- PCRE filtering of articles
183 */
184void readfilter(char *filterfile);
185void freefilter(void);
186int dofilter(unsigned char *h);
187
188/*
189 * artutil.c -- handling article files
190 */
191void store(const char *filename,
192 FILE * filehandle, const char *newsgroups, const char *msgid);
193
194/*
195 * find a certain header in an article and return it
196 */
197/*@null@*//*@only@*/ char *getheader(const char *filename, const char *header);
198/*@null@*//*@only@*/ char *fgetheader(FILE * f, const char *header);
199
200/*
201 * the strings in config.c
202 */
203extern const char *spooldir;
204extern const char *sysconfdir;
205extern const char *version;
206extern const char *lockfile;
207
208/*
209 * global variables from config file. These are defined in configutil.c
210 */
213 char *group;
214 time_t xtime;
215 int days;
216};
217
218struct server {
219 struct server *next;
220 char *name; /* Servername */
221 char *username;
222 char *password;
223 pcre2_code_8 *group_pcre;
224 unsigned int port;
225 int descriptions; /* download descriptions as well */
226 int timeout; /* timeout in seconds before we give up */
227 int nopost; /* if set, do not try to post to this server */
228 int noread; /* if set, do not try to fetch articles from
229 this server */
230 int updateactive; /* update the active file of this server, BITFIELD: 1 = from command line, 2 = automatically set */
231 int post_anygroup; /* if set, do not check if the group is on the
232 server, but just post */
233 int noxover; /* if set, use XHDR for overview */
234 int only_groups_match_all; /* if set, any unmatched group (refer to
235 group_pcre) prevents the post */
236};
237
238extern int date_is_evil; /* skip DATE check on servers known bad */
239extern int stat_is_evil; /* use HEAD instead of STAT to figure if a
240 posting is available upstream, workaround for broken NewsCache */
241extern time_t expire; /* articles not touched since this time get deleted */
242extern int expiredays;
243extern struct expire_entry *expire_base;
244 /* expire for certain groups */
245extern unsigned long artlimit; /* max # of articles to read per group in one go */
246extern unsigned long initiallimit;
247 /* max # of articles to read at first time */
248extern long crosspostlimit;
249 /* crossposting limit, to reduce spam */
250extern int delaybody; /* delay download of message body */
251extern int db_situ; /* delaybody: keep original article number */
252extern int debugmode; /* log lots of stuff via syslog */
253extern int create_all_links;
254 /* store articles even in uninteresting groups */
255extern int maxage; /* max age of articles */
256extern long maxlines; /* max length of articles in lines */
257extern long minlines; /* min length of articles in lines */
258extern unsigned long maxbytes; /* max length of articles in bytes */
259extern int timeout_short; /* don't fetch groups that have been
260 accidentally accessed after that many days */
261extern int timeout_long; /* don't fetch groups that have been accessed
262 that many days */
263extern int timeout_active; /* reread active file after that many days */
264extern int timeout_client; /* activity timeout for clients in seconds */
265extern int timeout_fetchnews; /* response deadline for upstream in seconds */
266extern int clamp_maxage; /* limit maxage to applicable group/global expire? */
267extern int article_despite_filter; /* request and discard body if filterfile is defined, for high-latency, high-throughput links */
268extern char *filterfile; /* filename where filter resides */
269extern struct server *servers; /* list of servers to use */
270extern int allowstrangers; /* if addresses not local to our links
271 are allowed to connect */
272extern int allow_8bit_headers; /* if 8bit junk in headers is allowed */
273extern char *newsadmin; /* address of news administrator, default: news@%s with %s = fqdn */
274extern unsigned long timeout_lock; /* lock file timeout */
275
276/*
277 * other global variables
278 */
279#define SIZE_lineout 1024 /* also used for PCRE2 error strings */
280extern char last_command[SIZE_lineout + 1];
281extern char lineout[SIZE_lineout + 1];
282
283/* defined in nntputil.c */
284extern /*@relnull@*/ /*@dependent@*/ FILE *nntpin;
285extern /*@relnull@*/ /*@dependent@*/ FILE *nntpout;
286
287#define SIZE_s (8192)
288#define FQDNLEN 255
289extern char fqdn[FQDNLEN + 1]; /* my name, and my naming myself */
290
291extern int verbose; /* verbosity level, for fetch and texpire */
292extern int debug; /* debug level */
293
294/*
295 * misc prototypes
296 */
297int try_lock(unsigned long);
298int handover_lock(pid_t);
299void putaline(void);
300void retry(void);
301void readexpire(void);
302int readconfig(int logtostderr);
303void freeexpire(void);
304void freeservers(void);
305void freeconfig(void);
306void lowercase(char *string);
307int ngmatch(const char *pattern, const char *string);
308void overrun(void);
309void replaceinlist(struct stringlist **haystack, char *needle, size_t len);
310time_t lookup_expire(char *group);
311int lookup_expiredays(char *group);
312int log_unlink(const char *f, int ignore_enoent);
313
314/* int rename(const char *old, const char *new); */
315 /* to avoid barfing of Digital Unix */
316
317/*
318 * stuff from nntputil.c
319 */
320int authenticate(const struct server*); /* authenticate ourselves at a server */
321/*@dependent@*//*@null@*/
322char *lastreply(void); /* last line frpm nntpreply */
323int nntpreply(const struct server*); /* decode an NNTP reply number */
324int nntpconnect(const struct server *upstream);
325 /* connect to upstream server */
326void nntpdisconnect(void); /* disconnect from upstream server */
327void nntpquit(void); /* send QUIT, then disconnect from upstream server */
328void freelastreply(void);
329
330/*@dependent@*/
331const char *rfctime(void); /* An rfc type date */
332
333int safe_mkstemp(char *); /* permission safe mkstemp wrapper */
334/* from syslog.c */
335void myopenlog(const char *ident);
336
337/* from mkstemp.c */
338#ifndef HAVE_MKSTEMP
339int mkstemp(char *);
340#endif
341
342/* from getline.c */
343#ifndef HAVE_GETLINE
344ssize_t getline(char **, size_t *, FILE *); /* fgets replacement */
345#endif
346void freegetaline(void);
347
348#ifndef HAVE_TIMEGM
349time_t timegm(struct tm *tm);
350#endif
351
352/* from wildmat.c */
353int wildmat(const char *, const char *);
354
355/* from getfoldedline.c */
356/*@null@*/ /*@only@*/
357char *getfoldedline(FILE * f, char *(*reader)(FILE *));
358/* reads one line, regardless of length, returns malloc()ed string! */
359
360/* from writes.c (ln-2) */
361ssize_t writes(int fd, const char *string);
362
363void fixxover(void);
364
365/* from grouplist.c */
366/*@null@*/ struct stringlist *get_grouplist(void);
367
368/* checkpeerlocal.c */
369int checkpeerlocal(int sock);
370
371/* pcre_extract.c */
372int ln_pcre_extract(const unsigned char *input, const unsigned char *pattern, /*@out@*/ char **output, size_t num);
373void ln_pcre_extract_free(char **vec, int count);
374size_t xstrlcpy(/*@out@*/ char *dst, const char *src, size_t size);
375
376/* xoverutil.c */
377struct xoverinfo {
378 char *text;
380};
381
382/*@null@*/ extern struct xoverinfo *xoverinfo;
383extern unsigned long xfirst;
384extern unsigned long xlast;
385
386int getxover(void); /* set xoverinfo, return 0 on error, nonzero else */
387
388int legalxoverline(const char *xover, /*@out@*/ const char **errmsg);
389void freexover(void);
390
391/* agetcwd.c */
392int agetcwd(/*@out@*/ char **buf, /*@out@*/ size_t *capa);
393
394#ifdef CHECKGROUPORDER
395void checkgrouporder(void);
396#endif /* CHECKGROUPORDER */
397
398#define SKIPLWS(p) while (*(p) && isspace((unsigned char) *(p))) { (p)++; }
399#endif /* #ifndef LEAFNODE_H */
char * mgetaline(FILE *f)
Definition: mgetaline.c:51
const char * sysconfdir
struct newsgroup * oldactive
Definition: activutil.c:42
void nntpdisconnect(void)
Definition: nntputil.c:547
int verbose
Definition: applyfilter.c:31
struct stringlist ** lfindinlist(struct stringlist **haystack, char *needle, size_t len)
Definition: miscutil.c:594
char * fgetheader(FILE *f, const char *header)
Definition: artutil.c:27
char * filterfile
Definition: configutil.c:88
struct newsgroup * xfindgroup(struct newsgroup *active, const char *name, unsigned long size)
Definition: activutil.c:242
void freexover(void)
Definition: xoverutil.c:396
char * getfoldedline(FILE *f, char *(*reader)(FILE *))
Definition: getfoldedline.c:13
int safe_mkstemp(char *)
Definition: lockfile.c:163
unsigned long maxbytes
Definition: configutil.c:72
char * strdup(const char *)
Definition: strdup.c:5
int stat_is_evil
Definition: nntputil.c:59
const char * spooldir
unsigned long xlast
Definition: miscutil.c:64
void lowercase(char *string)
char * activeread(void)
Definition: activutil.c:582
int log_unlink(const char *f, int ignore_enoent)
Definition: log_unlink.c:13
int(* tmihook)(const char *)
Definition: leafnode.h:157
void freeexpire(void)
Definition: configutil.c:708
int authenticate(const struct server *)
Definition: nntputil.c:96
void retry(void)
int allowstrangers
Definition: configutil.c:87
void freelist(struct stringlist *list)
Definition: miscutil.c:626
time_t expire
Definition: configutil.c:58
void myopenlog(const char *ident)
Definition: syslog.c:24
void ln_pcre_extract_free(char **vec, int count)
Definition: pcre_extract.c:108
int initvars(char *progname)
Definition: miscutil.c:143
void freelastreply(void)
Definition: nntputil.c:154
unsigned long xfirst
Definition: miscutil.c:64
int killactiveread(void)
Definition: activutil.c:595
char * getaline(FILE *f)
Definition: getaline.c:84
void overrun(void)
Definition: miscutil.c:296
unsigned long timeout_lock
Definition: configutil.c:92
FILE * nntpin
Definition: nntputil.c:56
void readexpire(void)
void readactive(void)
Definition: activutil.c:373
void putaline(void)
Definition: nntputil.c:75
void mgetaline_settimeout(unsigned int)
Definition: mgetaline.c:69
void fakeactive(void)
Definition: activutil.c:515
long crosspostlimit
Definition: configutil.c:63
int findmsgid(const char *msgid)
Definition: miscutil.c:695
const char * rfctime(void)
Definition: miscutil.c:752
time_t timegm(struct tm *tm)
Definition: timegm.c:50
int ln_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition: snprintf.c:71
int legalxoverline(const char *xover, const char **errmsg)
Definition: xoverutil.c:206
char fqdn[255+1]
Definition: miscutil.c:58
int mkstemp(char *)
Definition: mkstemp.c:27
void freeactive(struct newsgroup *act)
Definition: activutil.c:351
int traverseidtree(tmihook h)
Definition: miscutil.c:747
struct xoverinfo * xoverinfo
Definition: miscutil.c:63
void store(const char *filename, FILE *filehandle, const char *newsgroups, const char *msgid)
Definition: artutil.c:84
int nntpconnect(const struct server *upstream)
Definition: nntputil.c:318
ssize_t writes(int fd, const char *string)
Definition: writes.c:7
const char * lookup(const char *msgid)
Definition: miscutil.c:306
char * newsadmin
Definition: configutil.c:91
ssize_t getline(char **, size_t *, FILE *)
Definition: getline.c:54
void mergegroups(void)
Definition: activutil.c:162
sigjmp_buf timeout
Definition: mgetaline.c:39
int handover_lock(pid_t)
Definition: lockfile.c:327
void prependtolist(struct stringlist **list, const char *newentry)
Definition: miscutil.c:558
int clamp_maxage
Definition: configutil.c:81
int timeout_fetchnews
Definition: configutil.c:80
void freeservers(void)
Definition: configutil.c:695
struct newsgroup * findgroup(const char *name)
Definition: activutil.c:260
void replaceinlist(struct stringlist **haystack, char *needle, size_t len)
Definition: miscutil.c:607
void timer(int sig)
Definition: mgetaline.c:41
void changegroupdesc(const char *groupname, const char *description)
Definition: activutil.c:144
int checkpeerlocal(int sock)
void nntpquit(void)
Definition: nntputil.c:560
int date_is_evil
Definition: nntputil.c:60
int agetcwd(char **buf, size_t *capa)
Definition: agetcwd.c:10
int debugmode
Definition: configutil.c:67
time_t lookup_expire(char *group)
Definition: configutil.c:735
void clearidtree(void)
Definition: miscutil.c:729
int timeout_long
Definition: configutil.c:76
int ln_pcre_extract(const unsigned char *input, const unsigned char *pattern, char **output, size_t num)
Definition: pcre_extract.c:55
size_t activesize
Definition: activutil.c:39
int article_despite_filter
Definition: configutil.c:69
int lookup_expiredays(char *group)
Definition: configutil.c:743
unsigned long artlimit
Definition: configutil.c:61
void freegetaline(void)
Definition: getaline.c:89
const char * version
FILE * nntpout
Definition: nntputil.c:57
int timeout_short
Definition: configutil.c:77
int wildmat(const char *, const char *)
Definition: wildmat.c:126
int writeactive(void)
Definition: activutil.c:269
char * findinlist(struct stringlist *haystack, char *needle)
Definition: miscutil.c:576
size_t oldactivesize
Definition: activutil.c:41
const char * lockfile
int db_situ
Definition: configutil.c:66
int timeout_client
Definition: configutil.c:79
char last_command[1024+1]
Definition: nntputil.c:53
int maxage
Definition: configutil.c:68
int delaybody
Definition: configutil.c:65
#define SIZE_lineout
Definition: leafnode.h:279
char lineout[1024+1]
Definition: nntputil.c:54
struct server * servers
Definition: configutil.c:89
int debug
Definition: applyfilter.c:30
struct newsgroup * active
Definition: activutil.c:40
void insertmsgid(const char *msgid)
Definition: miscutil.c:662
int expiredays
Definition: configutil.c:59
void freefilter(void)
Definition: filterutil.c:113
int ngmatch(const char *pattern, const char *string)
Definition: miscutil.c:792
int readconfig(int logtostderr)
Definition: configutil.c:208
void fixxover(void)
Definition: xoverutil.c:687
void newgroupdesc(const char *groupname, const char *description)
Definition: activutil.c:124
int isinteresting(const char *groupname)
Definition: miscutil.c:271
int xsnprintf(char *str, size_t n, const char *format,...)
Definition: miscutil.c:798
void freeconfig(void)
Definition: configutil.c:722
void readfilter(char *filterfile)
Definition: filterutil.c:33
int timeout_active
Definition: configutil.c:78
int dofilter(unsigned char *h)
Definition: filterutil.c:87
struct stringlist * get_grouplist(void)
Definition: grouplist.c:113
#define FQDNLEN
Definition: leafnode.h:288
char * getheader(const char *filename, const char *header)
Definition: artutil.c:62
struct expire_entry * expire_base
Definition: configutil.c:60
void insertgroup(const char *name, long unsigned first, long unsigned last, time_t date)
Definition: activutil.c:77
unsigned long initiallimit
Definition: configutil.c:62
int nntpreply(const struct server *)
Definition: nntputil.c:249
long maxlines
Definition: configutil.c:70
int try_lock(unsigned long)
Definition: lockfile.c:200
int create_all_links
Definition: configutil.c:64
int chdirgroup(const char *group, int creatdir)
Definition: miscutil.c:446
char * lastreply(void)
Definition: nntputil.c:162
size_t xstrlcpy(char *dst, const char *src, size_t size)
Definition: miscutil.c:812
int allow_8bit_headers
Definition: configutil.c:90
int ln_snprintf(char *str, size_t n, const char *format,...)
Definition: snprintf.c:221
int getxover(void)
Definition: xoverutil.c:415
long minlines
Definition: configutil.c:71
#define len
Definition: mastring.c:31
const char * name
Definition: miscutil.c:126
static struct newsgroup * group
Definition: nntpd.c:73
static void list(struct newsgroup *g, const int what, const char *pattern)
Definition: nntpd.c:800
Definition: leafnode.h:211
time_t xtime
Definition: leafnode.h:214
int days
Definition: leafnode.h:215
char * group
Definition: leafnode.h:213
struct expire_entry * next
Definition: leafnode.h:212
unsigned long first
Definition: leafnode.h:122
char * desc
Definition: leafnode.h:125
char * name
Definition: leafnode.h:124
time_t age
Definition: leafnode.h:126
unsigned long last
Definition: leafnode.h:123
char * password
Definition: leafnode.h:222
char * username
Definition: leafnode.h:221
pcre2_code_8 * group_pcre
Definition: leafnode.h:223
int updateactive
Definition: leafnode.h:230
char * name
Definition: leafnode.h:220
unsigned int port
Definition: leafnode.h:224
int descriptions
Definition: leafnode.h:225
int only_groups_match_all
Definition: leafnode.h:234
int noread
Definition: leafnode.h:228
int timeout
Definition: leafnode.h:226
int noxover
Definition: leafnode.h:233
struct server * next
Definition: leafnode.h:219
int nopost
Definition: leafnode.h:227
int post_anygroup
Definition: leafnode.h:231
struct stringlist * next
Definition: leafnode.h:166
int exists
Definition: leafnode.h:379
char * text
Definition: leafnode.h:378