1 /*
2   configfile.c
3 
4   The following macros must be set, when compiling this file:
5     CONFIGFILE
6     SPOOLDIR
7     VERSION
8 
9   $Id: configfile.c,v 1.22 2003/05/23 09:33:10 bears Exp $
10 */
11 
12 #if HAVE_CONFIG_H
13 #include <config.h>
14 #endif
15 
16 #include <ctype.h>
17 #include <limits.h>
18 #include <sys/types.h>
19 #include <regex.h>
20 #include "configfile.h"
21 #include "filter.h"
22 #include "itemlist.h"
23 #include "log.h"
24 #include "util.h"
25 #include "portable.h"
26 #include "wildmat.h"
27 
28 typedef struct
29 {
30     int numGroup;
31     int maxGroup;
32     char **groups;
33 }
34 GroupEntry;
35 
36 struct GroupEnum
37 {
38     GroupEntry *groupEntry;
39     int groupIdx;
40 };
41 
42 typedef struct
43 {
44     char *name;
45     char *user;
46     char *pass;
47     GroupEntry getgroups;
48     GroupEntry omitgroups;
49 }
50 ServEntry;
51 
52 typedef struct
53 {
54     char *pattern;
55     int days;
56 }
57 ExpireEntry;
58 
59 typedef struct
60 {
61     char *pattern;
62     char *mode;
63 }
64 AutoSubscribeModeEntry;
65 
66 struct
67 {
68     /* Compile time options */
69     const char *spoolDir;
70     const char *version;
71     /* Options from the config file */
72     int maxFetch;
73     int autoUnsubscribeDays;
74     int threadFollowTime;
75     int connectTimeout;
76     Bool autoSubscribe;
77     Bool autoUnsubscribe;
78     Bool infoAlways;
79     Bool appendReplyTo;
80     Bool replaceMsgId;
81     Str hostnameMsgId;
82     Bool postLocal;
83     Bool clientAuth;
84     Str defaultAutoSubscribeMode;
85     Str mailTo;
86     int defaultExpire;
87     int numServ;
88     int maxServ;
89     ServEntry *serv;
90     int servIdx; /* for server enumeration */
91     int numExpire;
92     int maxExpire;
93     ExpireEntry *expire;
94     int numAutoSubscribeMode;
95     int maxAutoSubscribeMode;
96     AutoSubscribeModeEntry *autoSubscribeMode;
97     Str pathHeader;
98     Str fromDomain;
99     Str organization;
100     Str noffleUser;
101     Str noffleGroup;
102 } config =
103 {
104     SPOOLDIR, /* spoolDir */
105     VERSION,  /* version */
106     300,      /* maxFetch */
107     30,       /* autoUnsubscribeDays */
108     7,        /* threadFollowTime */
109     30,       /* connectTimeout */
110     FALSE,    /* autoSubscribe */
111     FALSE,    /* autoUnsubscribe */
112     TRUE,     /* infoAlways */
113     TRUE,     /* appendReplyTo */
114     FALSE,    /* replaceMsgId */
115     "",       /* hostnameMsgId */
116     FALSE,    /* postLocal */
117     FALSE,    /* clientAuth */
118     "over",   /* defaultAutoSubscribeMode */
119     "",       /* mailTo */
120     14,       /* defaultExpire */
121     0,        /* numServ */
122     0,        /* maxServ */
123     NULL,     /* serv */
124     0,	      /* servIdx */
125     0,        /* numExpire */
126     0,        /* maxExpire */
127     NULL,     /* expire */
128     0,        /* numAutoSubscribeMode */
129     0,        /* maxAutoSubscribeMode */
130     NULL,     /* autoSubscribeMode */
131     "",       /* pathHeader */
132     "",       /* fromDomain */
133     "",       /* organization */
134     "news",   /* user Noffle runs as */
135     "news"    /* group Noffle runs as */
136 };
137 
Cfg_spoolDir(void)138 const char * Cfg_spoolDir( void ) { return config.spoolDir; }
Cfg_version(void)139 const char * Cfg_version( void ) { return config.version; }
140 
Cfg_maxFetch(void)141 int Cfg_maxFetch( void ) { return config.maxFetch; }
Cfg_autoUnsubscribeDays(void)142 int Cfg_autoUnsubscribeDays( void ) { return config.autoUnsubscribeDays; }
Cfg_threadFollowTime(void)143 int Cfg_threadFollowTime( void ) { return config.threadFollowTime; }
Cfg_connectTimeout(void)144 int Cfg_connectTimeout( void ) { return config.connectTimeout; }
Cfg_autoUnsubscribe(void)145 Bool Cfg_autoUnsubscribe( void ) { return config.autoUnsubscribe; }
Cfg_autoSubscribe(void)146 Bool Cfg_autoSubscribe( void )  { return config.autoSubscribe; }
Cfg_infoAlways(void)147 Bool Cfg_infoAlways( void )  { return config.infoAlways; }
Cfg_appendReplyTo(void)148 Bool Cfg_appendReplyTo ( void ) { return config.appendReplyTo; }
Cfg_replaceMsgId(void)149 Bool Cfg_replaceMsgId( void ) { return config.replaceMsgId; }
Cfg_hostnameMsgId(void)150 const char * Cfg_hostnameMsgId( void ) { return config.hostnameMsgId; }
Cfg_postLocal(void)151 Bool Cfg_postLocal( void ) { return config.postLocal; }
Cfg_needClientAuth(void)152 Bool Cfg_needClientAuth( void ) { return config.clientAuth; }
Cfg_defaultAutoSubscribeMode(void)153 const char * Cfg_defaultAutoSubscribeMode( void ) {
154     return config.defaultAutoSubscribeMode; }
Cfg_mailTo(void)155 const char * Cfg_mailTo( void ) { return config.mailTo; }
Cfg_defaultExpire(void)156 int Cfg_defaultExpire( void ) { return config.defaultExpire; }
Cfg_pathHeader(void)157 const char * Cfg_pathHeader( void ) { return config.pathHeader; }
Cfg_fromDomain(void)158 const char * Cfg_fromDomain( void ) { return config.fromDomain; }
Cfg_organization(void)159 const char * Cfg_organization( void ) { return config.organization; }
Cfg_noffleUser(void)160 const char * Cfg_noffleUser( void ) { return config.noffleUser; }
Cfg_noffleGroup(void)161 const char * Cfg_noffleGroup( void ) { return config.noffleGroup; }
162 
Cfg_setClientAuth(Bool needsAuth)163 void Cfg_setClientAuth( Bool needsAuth )
164 {
165     config.clientAuth = needsAuth;
166 }
167 
168 void
Cfg_beginServEnum(void)169 Cfg_beginServEnum( void )
170 {
171     config.servIdx = 0;
172 }
173 
174 Bool
Cfg_nextServ(Str name)175 Cfg_nextServ( Str name )
176 {
177     if ( config.servIdx >= config.numServ )
178         return FALSE;
179     Utl_cpyStr( name, config.serv[ config.servIdx ].name );
180     ++config.servIdx;
181     return TRUE;
182 }
183 
184 static Bool
searchServ(const char * name,int * idx)185 searchServ( const char *name, int *idx )
186 {
187     int i;
188 
189     for ( i = 0; i < config.numServ; ++i )
190         if ( strcmp( name, config.serv[ i ].name ) == 0 )
191         {
192             *idx = i;
193             return TRUE;
194         }
195     return FALSE;
196 }
197 
198 Bool
Cfg_servListContains(const char * name)199 Cfg_servListContains( const char *name )
200 {
201     int idx;
202 
203     return searchServ( name, &idx );
204 }
205 
206 Bool
Cfg_servIsPreferential(const char * name1,const char * name2)207 Cfg_servIsPreferential( const char *name1, const char *name2 )
208 {
209     Bool exists1, exists2;
210     int idx1, idx2;
211 
212     exists1 = searchServ( name1, &idx1 );
213     exists2 = searchServ( name2, &idx2 );
214     if ( exists1 && exists2 )
215         return ( idx1 < idx2 );
216     if ( exists1 && ! exists2 )
217         return TRUE;
218     /* ( ! exists1 && exists2 ) || ( ! exists1 && ! exists2 ) */
219     return FALSE;
220 }
221 
222 void
Cfg_authInfo(const char * name,Str user,Str pass)223 Cfg_authInfo( const char *name, Str user, Str pass )
224 {
225     int idx;
226 
227     if ( searchServ( name, &idx ) )
228     {
229         Utl_cpyStr( user, config.serv[ idx ].user );
230         Utl_cpyStr( pass, config.serv[ idx ].pass );
231     }
232     else
233     {
234         user[ 0 ] = '\0';
235         pass[ 0 ] = '\0';
236     }
237 }
238 
239 int
Cfg_expire(const char * grp)240 Cfg_expire( const char *grp )
241 {
242     int i, res;
243 
244     for ( i = 0; i < config.numExpire; i++ )
245 	if ( Wld_match( grp, config.expire[ i ].pattern ) )
246 	{
247 	    res = config.expire[ i ].days;
248 	    Log_dbg( LOG_DBG_CONFIG,
249 		     "Custom expire period %d for group %s",
250 		     res, grp );
251 	    return res;
252 	}
253 
254     return Cfg_defaultExpire();
255 }
256 
257 const char *
Cfg_autoSubscribeMode(const char * grp)258 Cfg_autoSubscribeMode( const char *grp )
259 {
260     int i;
261     const char *res;
262 
263     for ( i = 0; i < config.numAutoSubscribeMode; i++ )
264 	if ( Wld_match( grp, config.autoSubscribeMode[ i ].pattern ) )
265 	{
266 	    res = config.autoSubscribeMode[ i ].mode;
267 	    Log_dbg( LOG_DBG_CONFIG,
268 		     "Custom auto subscribe mode %s for group %s",
269 		     res, grp );
270 	    return res;
271 	}
272 
273     return Cfg_defaultAutoSubscribeMode();
274 }
275 
276 GroupEnum *
new_GetGrEn(const char * name)277 new_GetGrEn( const char *name )
278 {
279     GroupEnum *res;
280     int servIdx;
281 
282     res = (GroupEnum *) malloc( sizeof( GroupEnum ) );
283     if ( res == NULL )
284 	Log_fatal( "Malloc of GroupEnum failed." );
285     if ( ! searchServ( name, &servIdx ) )
286 	res->groupEntry = NULL;
287     else
288 	res->groupEntry = &config.serv[ servIdx ].getgroups;
289     GrEn_first( res );
290     return res;
291 }
292 
293 GroupEnum *
new_OmitGrEn(const char * name)294 new_OmitGrEn( const char *name )
295 {
296     GroupEnum *res;
297     int servIdx;
298 
299     res = (GroupEnum *) malloc( sizeof( GroupEnum ) );
300     if ( res == NULL )
301 	Log_fatal( "Malloc of GroupEnum failed." );
302     if ( ! searchServ( name, &servIdx ) )
303 	res->groupEntry = NULL;
304     else
305 	res->groupEntry = &config.serv[ servIdx ].omitgroups;
306     GrEn_first( res );
307     return res;
308 }
309 
310 void
del_GrEn(GroupEnum * ge)311 del_GrEn( GroupEnum *ge )
312 {
313     free(ge);
314 }
315 
316 void
GrEn_first(GroupEnum * ge)317 GrEn_first( GroupEnum *ge )
318 {
319     ge->groupIdx = 0;
320 }
321 
322 const char *
GrEn_next(GroupEnum * ge)323 GrEn_next( GroupEnum *ge )
324 {
325     if ( ge->groupEntry == NULL ||
326 	 ge->groupIdx >= ge->groupEntry->numGroup )
327 	return NULL;
328     return ge->groupEntry->groups[ ge->groupIdx++ ];
329 }
330 
331 static void
logSyntaxErr(const char * line)332 logSyntaxErr( const char *line )
333 {
334     Log_err( "Syntax error in config file: %s", line );
335 }
336 
337 static void
getBool(Bool * variable,const char * line)338 getBool( Bool *variable, const char *line )
339 {
340     Str value, name, lowerLn;
341 
342     Utl_cpyStr( lowerLn, line );
343     Utl_toLower( lowerLn );
344     if ( sscanf( lowerLn, MAXCHAR_FMT " " MAXCHAR_FMT, name, value ) != 2 )
345     {
346         logSyntaxErr( line );
347         return;
348     }
349 
350     if ( strcmp( value, "yes" ) == 0 )
351         *variable = TRUE;
352     else if ( strcmp( value, "no" ) == 0 )
353         *variable = FALSE;
354     else
355         Log_err( "Error in config file %s must be yes or no", name );
356 }
357 
358 static void
getInt(int * variable,int min,int max,const char * line)359 getInt( int *variable, int min, int max, const char *line )
360 {
361     int value;
362     Str name;
363 
364     if ( sscanf( line, MAXCHAR_FMT " %d", name, &value ) != 2 )
365     {
366         logSyntaxErr( line );
367         return;
368     }
369     if ( value < min || value > max )
370     {
371         Log_err( "Range error in config file %s [%d,%d]", name, min, max );
372         return;
373     }
374     *variable = value;
375 }
376 
377 static void
getStr(char * variable,const char * line)378 getStr( char *variable, const char *line )
379 {
380     Str dummy;
381 
382     if ( sscanf( line, MAXCHAR_FMT " " MAXCHAR_FMT, dummy, variable ) != 2 )
383     {
384         logSyntaxErr( line );
385         return;
386     }
387 }
388 
389 static void
getText(Str variable,const char * line)390 getText( Str variable, const char *line )
391 {
392     const char *l;
393 
394     /* Skip command */
395     l = Utl_restOfLn( line, 1 );
396     Utl_cpyStr( variable, l );
397 }
398 
399 static void
getServ(const char * line)400 getServ( const char *line )
401 {
402     Str dummy, name, user, pass;
403     int r, len;
404     ServEntry entry;
405 
406     memset( &entry, 0, sizeof( entry ) );
407     user[ 0 ] = pass[ 0 ] = '\0';
408     r = sscanf( line,
409 		MAXCHAR_FMT " " MAXCHAR_FMT " " MAXCHAR_FMT " " MAXCHAR_FMT,
410                 dummy, name, user, pass );
411     if ( r < 2 )
412     {
413         logSyntaxErr( line );
414         return;
415     }
416     len = strlen( name );
417     /* To make server name more definit, it is made lowercase and
418        port is removed, if it is the default port */
419     if ( len > 4 && strcmp( name + len - 4, ":119" ) == 0 )
420         name[ len - 4 ] = '\0';
421     Utl_toLower( name );
422 
423     Utl_allocAndCpy( &entry.name, name );
424     Utl_allocAndCpy( &entry.user, user );
425     Utl_allocAndCpy( &entry.pass, pass );
426 
427     if ( config.maxServ < config.numServ + 1 )
428     {
429         if ( ! ( config.serv = realloc( config.serv,
430                                         ( config.maxServ + 5 )
431                                         * sizeof( ServEntry ) ) ) )
432             Log_fatal( "Could not realloc server list" );
433         config.maxServ += 5;
434     }
435     config.serv[ config.numServ++ ] = entry;
436 }
437 
438 static void
getExpire(const char * line)439 getExpire( const char *line )
440 {
441     Str dummy, pattern;
442     ExpireEntry entry;
443     int days;
444 
445     if ( sscanf( line, MAXCHAR_FMT " " MAXCHAR_FMT " %d",
446 		 dummy, pattern, &days ) != 3 )
447     {
448 	logSyntaxErr( line );
449 	return;
450     }
451     else
452     {
453 	if ( days < 0 )
454 	{
455 	    Log_err( "Expire days error in '%s': must be integer > 0",
456 		     line, days );
457 	    return;
458 	}
459 
460 	Utl_toLower( pattern );
461 	Utl_allocAndCpy( &entry.pattern, pattern );
462 	entry.days = days;
463 
464 	if ( config.maxExpire < config.numExpire + 1 )
465 	{
466 	    if ( ! ( config.expire = realloc( config.expire,
467 					      ( config.maxExpire + 5 )
468 					      * sizeof( ExpireEntry ) ) ) )
469 		Log_fatal( "Could not realloc expire list" );
470 	    config.maxExpire += 5;
471 	}
472 	config.expire[ config.numExpire++ ] = entry;
473     }
474 }
475 
476 static void
getGroups(char * line,Bool isGet)477 getGroups( char *line, Bool isGet )
478 {
479     const char *name;
480     ItemList *patterns;
481     const char *pattern;
482 
483     if ( config.numServ == 0 )
484     {
485 	Log_err( "No current server in %s", line );
486 	return;
487     }
488 
489     name = line;
490     /* Skip over name and terminate it */
491     while ( line[ 0 ] != '\0' && ! isspace( line[ 0 ] ) )
492 	line++;
493     if ( line[ 0 ] == '\0' )
494     {
495 	logSyntaxErr( name );
496 	return;
497     }
498     line[ 0 ] = '\0';
499     line++;
500 
501     patterns = new_Itl( line, " ," );
502     for( pattern = Itl_first( patterns );
503 	 pattern != NULL;
504 	 pattern = Itl_next( patterns ) )
505     {
506 	GroupEntry *g;
507 
508 	if ( isGet )
509 	    g = &config.serv[ config.numServ - 1 ].getgroups;
510 	else
511 	    g = &config.serv[ config.numServ - 1 ].omitgroups;
512 	if ( g->maxGroup < g->numGroup + 1 )
513 	{
514 	    if ( ! ( g->groups = realloc( g->groups,
515 					  ( g->maxGroup + 5 )
516 					  * sizeof( char * ) ) ) )
517 		Log_fatal( "Could not realloc group list" );
518 	    g->maxGroup += 5;
519 	}
520 	Utl_allocAndCpy( &g->groups[ g->numGroup++ ], pattern );
521     }
522     del_Itl( patterns) ;
523 }
524 
525 static void
getDebugMask(char * line)526 getDebugMask( char *line )
527 {
528     const char *name;
529     ItemList *maskNames;
530     const char *maskName;
531     unsigned mask;
532 
533     name = line;
534     /* Skip over name and terminate it */
535     while ( line[ 0 ] != '\0' && ! isspace( line[ 0 ] ) )
536 	line++;
537     if ( line[ 0 ] == '\0' )
538     {
539 	logSyntaxErr( name );
540 	return;
541     }
542     line[ 0 ] = '\0';
543     line++;
544 
545     mask = LOG_DBG_NONE;
546     maskNames = new_Itl( line, " ," );
547     for( maskName = Itl_first( maskNames );
548 	 maskName != NULL;
549 	 maskName = Itl_next( maskNames ) )
550     {
551 	if ( strcmp( maskName, "all" ) == 0 )
552 	    mask = LOG_DBG_ALL;
553 	else if ( strcmp( maskName, "none" ) == 0 )
554 	    mask = LOG_DBG_NONE;
555 	else if ( strcmp( maskName, "config" ) == 0 )
556 	    mask |= LOG_DBG_CONFIG;
557 	else if ( strcmp( maskName, "control" ) == 0 )
558 	    mask |= LOG_DBG_CONTROL;
559 	else if ( strcmp( maskName, "expire" ) == 0 )
560 	    mask |= LOG_DBG_EXPIRE;
561 	else if ( strcmp( maskName, "fetch" ) == 0 )
562 	    mask |= LOG_DBG_FETCH;
563 	else if ( strcmp( maskName, "filter" ) == 0 )
564 	    mask |= LOG_DBG_FILTER;
565 	else if ( strcmp( maskName, "newsbase" ) == 0 )
566 	    mask |= LOG_DBG_NEWSBASE;
567 	else if ( strcmp( maskName, "noffle" ) == 0 )
568 	    mask |= LOG_DBG_NOFFLE;
569 	else if ( strcmp( maskName, "post" ) == 0 )
570 	    mask |= LOG_DBG_POST;
571 	else if ( strcmp( maskName, "protocol" ) == 0 )
572 	    mask |= LOG_DBG_PROTOCOL;
573 	else if ( strcmp( maskName, "requests" ) == 0 )
574 	    mask |= LOG_DBG_REQUESTS;
575 	else if ( strcmp( maskName, "server" ) == 0 )
576 	    mask |= LOG_DBG_SERVER;
577 	else if ( strcmp( maskName, "auth" ) == 0 )
578 	    mask |= LOG_DBG_AUTH;
579 	else
580 	    logSyntaxErr( line );
581     }
582     del_Itl( maskNames) ;
583     Log_setDbgMask( mask );
584 }
585 
586 static Bool
isValidAutoSubscribeMode(const char * mode)587 isValidAutoSubscribeMode( const char *mode )
588 {
589     return strcmp( mode, "full" ) == 0
590 	|| strcmp( mode, "thread" ) == 0
591 	|| strcmp( mode, "over" ) == 0
592 	|| strcmp( mode, "off" ) == 0;
593 }
594 
595 static void
getAutoSubscribeMode(const char * line)596 getAutoSubscribeMode( const char *line )
597 {
598     Str dummy, pattern, mode;
599     AutoSubscribeModeEntry entry;
600     int items;
601 
602     items = sscanf( line, MAXCHAR_FMT " " MAXCHAR_FMT " " MAXCHAR_FMT,
603 		    dummy, pattern, mode );
604     if ( items == 2 )
605     {
606 	/* Backwards compat. default-auto-subscribe-mode */
607 	Utl_cpyStr( mode, pattern );
608 	Utl_toLower( mode );
609 	if ( ! isValidAutoSubscribeMode( mode ) )
610 	{
611 	    logSyntaxErr( line );
612 	    return;
613 	}
614 	Utl_cpyStr( config.defaultAutoSubscribeMode, mode );
615 	return;
616     }
617     else if ( items != 3 )
618     {
619 	logSyntaxErr( line );
620 	return;
621     }
622 
623     Utl_toLower( mode );
624     if ( ! isValidAutoSubscribeMode( mode ) )
625     {
626 	logSyntaxErr( line );
627 	return;
628     }
629 
630     Utl_toLower( pattern );
631     Utl_allocAndCpy( &entry.pattern, pattern );
632     Utl_allocAndCpy( &entry.mode, mode );
633 
634     if ( config.maxAutoSubscribeMode < config.numAutoSubscribeMode + 1 )
635     {
636 	if ( ! ( config.autoSubscribeMode =
637 		 realloc( config.autoSubscribeMode,
638 			  ( config.maxAutoSubscribeMode + 5 )
639 			  * sizeof( AutoSubscribeModeEntry ) ) ) )
640 	    Log_fatal( "Could not realloc auto subscribe mode list" );
641 	config.maxAutoSubscribeMode += 5;
642     }
643     config.autoSubscribeMode[ config.numAutoSubscribeMode++ ] = entry;
644 }
645 
646 static const char *
getToken(const char * line,Str value)647 getToken( const char *line, Str value )
648 {
649     Bool isQuoted;
650     char quoteChar;
651     Bool seenEscape;
652     char *maxVal;
653 
654     while ( *line != '\0' && isspace( *line ) )
655 	line++;
656     if ( *line == '\0' )
657 	return NULL;
658 
659     maxVal = &value[ MAXCHAR ];
660     isQuoted = ( *line == '\'' || *line == '"' );
661     if ( isQuoted )
662     {
663 	quoteChar = *line;
664 	line++;
665 
666 	seenEscape = FALSE;
667 	while ( *line != '\0'
668 		&& ( *line != quoteChar || seenEscape )
669 		&& value < maxVal )
670 	{
671 	    if ( seenEscape )
672 	    {
673 		*value++ = *line;
674 		seenEscape = FALSE;
675 	    }
676 	    else
677 	    {
678 		if ( *line == '\\' )
679 		    seenEscape = TRUE;
680 		else
681 		    *value++ = *line;
682 	    }
683 	    line++;
684 	}
685 
686 	if ( *line == quoteChar )
687 	    line++;
688     }
689     else
690     {
691 	while ( *line != '\0' && ! isspace( *line ) && value < maxVal )
692 	    *value++ = *line++;
693     }
694     *value = '\0';
695     return line;
696 }
697 
698 /* very simple date parser.
699  * examples:
700  *      now+
701  */
702 static Bool
get_simpledate(time_t * timeoffsetp,FilterRuleDateEnumType * vartimep,const char * val)703 get_simpledate( time_t *timeoffsetp, FilterRuleDateEnumType *vartimep, const char *val)
704 {
705     float timef;
706 
707     if ( ! strncasecmp( val, "invalid", 7 ) )
708     {
709         *vartimep = INVALID;
710         return TRUE;
711     }
712     else if ( ! strncasecmp( val, "now", 3 ) )
713     {
714         val += 3;
715         *vartimep = NOW;
716     }
717     else if ( ! strncasecmp( val, "lastupdate", 10 ) )
718     {
719         val += 10;
720         *vartimep = LASTUPDATE;
721     }
722     else
723     {
724         *vartimep = FIXED;
725         *timeoffsetp = Utl_parseNewsDate( val );
726         if ( *timeoffsetp == (time_t) -1 )
727             return FALSE;
728         else
729             return TRUE;
730     }
731     /* NOW, LASTUPDATE +/- number of days. */
732     timef = atof( val ) * 86400.0 ; /* 24 * 60 * 60 == 86400 */
733 
734     /* let's assume more than 10 years of timeoffset are a mistake. */
735     if ( timef > 31536000.0 || timef < -31536000.0 )
736         return FALSE;
737     *timeoffsetp = (time_t) timef;
738     /* Todo: check if any garbage follows. */
739     return TRUE;
740 }
741 
742 static void
getFilter(const char * line)743 getFilter( const char *line )
744 {
745     Str ruleBuf, value;
746     const char *l;
747     char *p, *ruleName;
748     Filter *f;
749     FilterRule rule;
750     Bool seenAction;
751 
752     f = new_Filter();
753 
754     /* Skip "filter" */
755     l = Utl_restOfLn( line, 1 );
756     seenAction = FALSE;
757 
758     for(;;)
759     {
760 	while ( *l != '\0' && isspace( *l ) )
761 	    l++;
762 
763 	if ( *l == '\0' )
764 	    break;
765 
766 	/* Get the rule title */
767 	p = ruleBuf;
768 	while ( *l != '\0' && *l != '=' && *l != '<' && *l != '>' )
769 	    *p++ = *l++;
770 	*p = '\0';
771 	ruleName = Utl_stripWhiteSpace( ruleBuf );
772 	Utl_toLower( ruleName );
773 
774 	if ( *ruleName == '\0' )
775 	    goto synErr;
776 
777 	/* Do we know this rule? */
778 	if ( strcmp( ruleName, "group" ) == 0 )
779 	    rule.type = RULE_NEWSGROUP;
780 	else if ( strcmp( ruleName, "subject" ) == 0 )
781 	    rule.type = RULE_SUBJECT;
782 	else if ( strcmp( ruleName, "reference" ) == 0 )
783 	    rule.type = RULE_REFERENCE;
784 	else if ( strcmp( ruleName, "from" ) == 0 )
785 	    rule.type = RULE_FROM;
786 	else if ( strcmp( ruleName, "msgid" ) == 0 )
787 	    rule.type = RULE_MSGID;
788 	else if ( strcmp( ruleName, "bytes" ) == 0 )
789 	    rule.type = RULE_BYTES_EQ;
790 	else if ( strcmp( ruleName, "lines" ) == 0 )
791 	    rule.type = RULE_LINES_EQ;
792 	else if ( strcmp( ruleName, "refs" ) == 0 )
793 	    rule.type = RULE_NOREFS_EQ;
794 	else if ( strcmp( ruleName, "xposts" ) == 0 )
795 	    rule.type = RULE_XPOSTS_EQ;
796 	else if ( strcmp( ruleName, "post-status" ) == 0 )
797 	    rule.type = RULE_POST_STATUS;
798 	else if ( strcmp( ruleName, "date" ) == 0 )
799 	    rule.type = RULE_DATE_EQ;
800         /* date<lastupdate-12 equals older=lastupdate-12
801          * date>now+1.5       equals newer=now+1.5
802          * date=now           equals older=now+1 AND newer=now-1
803          * Stupid people like Mirko keep making mistakes
804          * if they're forced using date< or date>.
805          */
806 	else if ( strcmp( ruleName, "older" ) == 0 )
807 	    rule.type = RULE_DATE_LT;
808 	else if ( strcmp( ruleName, "newer" ) == 0 )
809 	    rule.type = RULE_DATE_GT;
810 
811 	else if ( strcmp( ruleName, "action" ) != 0 )
812 	    goto synErr;
813 
814 	if ( rule.type == RULE_BYTES_EQ ||
815 	     rule.type == RULE_LINES_EQ ||
816 	     rule.type == RULE_NOREFS_EQ ||
817 	     rule.type == RULE_XPOSTS_EQ ||
818 	     rule.type == RULE_DATE_EQ )
819 	{
820 	    if ( *l == '<' )
821 		rule.type--;
822 	    else if ( *l == '>' )
823 		rule.type++;
824 	    else if ( *l != '=' )
825 		goto synErr;
826 	}
827 	else if ( *l != '=' )
828 	    goto synErr;
829 
830 	/* Skip past '=' (or '>' or '<') */
831 	l++;
832 
833 	/* OK, we now have a valid rule. What value? */
834 	l = getToken( l, value );
835 	if ( l == NULL )
836 	    goto synErr;
837 
838 	if ( strcmp( ruleName, "action" ) == 0 )
839 	{
840 	    if ( seenAction )
841 		goto synErr;
842 
843 	    Utl_toLower( value );
844 	    if ( strcmp( value, "full" ) == 0 )
845 		f->action = FILTER_FULL;
846 	    else if ( strcmp( value, "over" ) == 0 )
847 		f->action = FILTER_XOVER;
848 	    else if ( strcmp( value, "thread" ) == 0 )
849 		f->action = FILTER_THREAD;
850 	    else if ( strcmp( value, "discard" ) == 0 )
851 		f->action = FILTER_DISCARD;
852 	    else if ( strcmp( value, "default" ) == 0 )
853 		f->action = FILTER_DEFAULT;
854 	    seenAction = TRUE;
855 	}
856 	else if ( rule.type == RULE_NEWSGROUP )
857 	    Utl_allocAndCpy( &rule.data.grp, value );
858 	else if ( rule.type >= RULE_SUBJECT && rule.type <= RULE_MSGID )
859 	{
860 	    if ( regcomp( &rule.data.regex, value, REG_EXTENDED ) != 0 )
861 		goto synErr;
862 	}
863 	else if (rule.type == RULE_POST_STATUS )
864         {
865 	    if ( ( strcmp( value, "yes" ) == 0 ) || \
866 	         ( strcmp( value, "no" ) == 0 ) || \
867 	         ( strncmp( value, "mod", 3 ) == 0 ) )
868 				/* no need to type out "moderated" */
869                 rule.data.postAllow = value[0]; /* 'y','n' or 'm' */
870 	    else
871 		goto synErr;
872         }
873         else if ( rule.type == RULE_DATE_LT ||
874                   rule.type == RULE_DATE_EQ ||
875                   rule.type == RULE_DATE_GT )
876         {
877              if ( !get_simpledate( &rule.data.reftime.timeoffset, &rule.data.reftime.vartime, value ) )
878                 goto synErr;
879              if ( rule.type != RULE_DATE_EQ &&
880                   rule.data.reftime.vartime == INVALID )
881                 goto synErr;
882         }
883 	else
884 	{
885 	    char * endVal;
886 	    int suffix;
887 
888 	    rule.data.amount = strtoul( value, &endVal, 0 );
889 	    suffix = tolower( *endVal );
890 	    if ( suffix == 'k' || suffix == 'm' )
891 	    {
892 		rule.data.amount *= 1024;
893 		if ( suffix == 'm' )
894 		    rule.data.amount *= 1024;
895 		endVal++;
896 	    }
897 	    if ( *endVal != '\0' && ! isspace( *endVal ) )
898 		goto synErr;
899 	}
900 
901 	if ( strcmp( ruleName, "action" ) != 0 )
902 	{
903 	    Log_dbg( LOG_DBG_CONFIG,
904 		     "Adding rule type %d value %s",
905 		     rule.type, value );
906 	    Flt_addRule( f, rule );
907 	}
908     }
909 
910     Log_dbg( LOG_DBG_CONFIG, "Adding filter, action %d", f->action );
911     Flt_addFilter( f );
912     return;
913 
914 synErr:
915     logSyntaxErr( line );
916     return;
917 }
918 
919 void
Cfg_read(void)920 Cfg_read( void )
921 {
922     char *p;
923     FILE *f;
924     Str file, line, lowerLine, name, s;
925 
926     Utl_cpyStr( file, CONFIGFILE );
927     if ( ! ( f = fopen( file, "r" ) ) )
928     {
929         Log_err( "Cannot read %s", file );
930         return;
931     }
932     while ( fgets( line, MAXCHAR, f ) )
933     {
934         p = Utl_stripWhiteSpace( line );
935 	Utl_stripComment( p );
936         Utl_cpyStr( lowerLine, p );
937         Utl_toLower( lowerLine );
938 	p = lowerLine;
939         if ( *p == '\0' )
940             continue;
941         if ( sscanf( p, MAXCHAR_FMT, name ) != 1 )
942             Log_err( "Syntax error in %s: %s", file, line );
943         else if ( strcmp( "max-fetch", name ) == 0 )
944             getInt( &config.maxFetch, 0, INT_MAX, p );
945         else if ( strcmp( "auto-unsubscribe-days", name ) == 0 )
946             getInt( &config.autoUnsubscribe, -1, INT_MAX, p );
947         else if ( strcmp( "thread-follow-time", name ) == 0 )
948             getInt( &config.threadFollowTime, 0, INT_MAX, p );
949         else if ( strcmp( "connect-timeout", name ) == 0 )
950             getInt( &config.connectTimeout, 0, INT_MAX, p );
951         else if ( strcmp( "default-expire", name ) == 0 )
952             getInt( &config.defaultExpire, 0, INT_MAX, p );
953         else if ( strcmp( "auto-subscribe", name ) == 0 )
954             getBool( &config.autoSubscribe, p );
955         else if ( strcmp( "auto-unsubscribe", name ) == 0 )
956             getBool( &config.autoUnsubscribe, p );
957         else if ( strcmp( "info-always-unread", name ) == 0 )
958             getBool( &config.infoAlways, p );
959 	else if ( strcmp( "append-reply-to", name ) == 0 )
960 	    getBool( &config.appendReplyTo, p);
961         else if ( strcmp( "replace-messageid", name ) == 0 )
962             getBool( &config.replaceMsgId, p );
963         else if ( strcmp( "hostname", name ) == 0 )
964         /* use line, do not change to lowercase */
965             getStr( config.hostnameMsgId, line );
966         else if ( strcmp( "post-locally", name ) == 0 )
967             getBool( &config.postLocal, p );
968 #if USE_AUTH
969 	/*
970 	 * Don't recognise this unless we have some sort of auth
971 	 * built in. A small sanity check on the config.
972 	 */
973         else if ( strcmp( "authenticate-client", name ) == 0 )
974             getBool( &config.clientAuth, p );
975 #endif
976         else if ( strcmp( "default-auto-subscribe-mode", name ) == 0 )
977         {
978             getStr( s, p );
979 	    if ( ! isValidAutoSubscribeMode( s ) )
980 	    {
981 		logSyntaxErr( line );
982 		return;
983 	    }
984             else
985                 Utl_cpyStr( config.defaultAutoSubscribeMode, s );
986         }
987         else if ( strcmp( "mail-to", name ) == 0 )
988             getStr( config.mailTo, p );
989         else if ( strcmp( "expire", name ) == 0 )
990             getExpire( p );
991         else if ( strcmp( "auto-subscribe-mode", name ) == 0 )
992             getAutoSubscribeMode( p );
993         else if ( strcmp( "log-debug", name ) == 0 )
994             getDebugMask( p );
995         else if ( strcmp( "getgroups", name ) == 0 )
996             getGroups( p, TRUE );
997         else if ( strcmp( "omitgroups", name ) == 0 )
998             getGroups( p, FALSE );
999         else if ( strcmp( "path-header", name ) == 0 )
1000             getStr( config.pathHeader, p );
1001         else if ( strcmp( "from-domain", name ) == 0 )
1002             getStr( config.fromDomain, p );
1003 	/* The following need line because they may have uppercase data */
1004         else if ( strcmp( "organization", name ) == 0 )
1005             getText( config.organization, line );
1006         else if ( strcmp( "noffle-user", name ) == 0 )
1007             getText( config.noffleUser, line );
1008         else if ( strcmp( "noffle-group", name ) == 0 )
1009             getText( config.noffleUser, line );
1010         else if ( strcmp( "server", name ) == 0 )
1011             getServ( line );
1012 	else if ( strcmp( "filter", name ) == 0 )
1013 	    getFilter( line );
1014         else
1015             Log_err( "Unknown config option: %s", name );
1016     }
1017     fclose( f );
1018     if ( ! config.numServ )
1019         Log_fatal( "Config file contains no server" );
1020 }
1021