1 /*
2  * ufdblib.h - URLfilterDB
3  *
4  * ufdbGuard is copyrighted (C) 2005-2020 by URLfilterDB with all rights reserved.
5  *
6  * RCS $Id: ufdblib.h,v 1.112 2020/10/12 16:15:16 root Exp root $
7  */
8 
9 #ifndef UFDB_UFDBLIB_H_INCLUDED
10 #define UFDB_UFDBLIB_H_INCLUDED
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 #include "ufdb.h"
17 #include "ufdb_globals.h"
18 
19 #include <time.h>
20 #include <stdio.h>
21 #include <sys/times.h>
22 
23 #if !UFDB_BARE_METAL_SUPPORT
24 #include <sys/utsname.h>
25 #endif
26 
27 
28 void increment_UFDB_API_num_https( void );
29 
30 void UFDBdropPrivileges( const char * username );
31 void UFDBraisePrivileges( const char * username, const char * why );
32 
33 typedef unsigned long ufdb_memdb_hash_t;
34 
35 typedef struct UFDBmemDBkv
36 {
37    char *               key;
38    char *               value;
39    ufdb_memdb_hash_t    hash;
40    struct UFDBmemDBkv * next;
41 } UFDBmemDBkv;
42 
43 struct UFDBmemDB
44 {
45    unsigned int      tableSize;
46    unsigned int      nEntries;
47    unsigned int      optimalMaxEntries;
48    UFDBmemDBkv **    table;
49 };
50 
51 /* The following functions are not all threadsafe !
52  * A memDB is created by one thread while other threads have no access.
53  * After the creation and initialisation a memDB is static and
54  * multiple threads can search in the memDB.
55  */
56 struct UFDBmemDB * UFDBmemDBinit( void );
57 void UFDBmemDBinsert( struct UFDBmemDB * db, const char * key, const char * value );
58 int  UFDBmemDBfind( struct UFDBmemDB * db, const char * key, char ** value );
59 int  UFDBmemDBfindDomain( struct UFDBmemDB * db, const char * domain );
60 void UFDBmemDBdeleteDB( struct UFDBmemDB * db );
61 void UFDBmemDBprintUserDB( const char * prefix, struct UFDBmemDB * db );
62 
63 void UFDBappInit( void );
64 unsigned long UFDBappMemUsage( void );
65 
66 void UFDBtimerInit( struct tms * t );
67 void UFDBtimerStop( struct tms * t );
68 void UFDBtimerPrint( struct tms * t, const char * tag );
69 void UFDBtimerPrintString( char * line, struct tms * t, const char * tag );
70 void UFDBlogTimer( struct tms * t, char * tag );
71 
72 int    UFDBgettimeofday( struct timeval * tv, struct timezone * tz );
73 time_t UFDBtime( void );
74 int    UFDBhasRTC( void );
75 
76 int UFDBloadExpressions(
77    struct ufdbRegExp ** exprList,
78    char *               file     );
79 
80 struct ufdbRegExp * UFDBoptimizeExprList(
81    char *               reSource,
82    struct ufdbRegExp *  reList );
83 
84 void ufdbFreeRegExprList( struct ufdbRegExp * re );
85 
86 int    UFDBregexec( const void * preg, const char * string, size_t nmatch, regmatch_t pmatch[], int eflags );
87 
88 struct Source * UFDBgetSourceList( void );
89 void UFDBsetSourceList( struct Source * sl );
90 
91 int  ufdbSetCPU( char * CPUspec );
92 int  ufdbSetThreadCPUaffinity( int thread_num );
93 void ufdbResetCPUs( void );
94 
95 const char * ufdbAPIstatusString( int api_code );
96 const char * ufdbDBstat2string( int status );
97 const char * ufdbStatus2string( int status );
98 
99 #define URL_HIST_SIZE 180000
100 
101 int    ufdbVerifyURLallCats( UFDBrevURL * revURL, char * URL );
102 void   ufdbRegisterUnknownURL( char * webserver, int portnumber, char * referer );
103 char * ufdbGetUnknownURLs( void );
104 void   ufdbResetUnknownURLs( void );
105 
106 int UFDBopenSocket( const char * serverName, int port );
107 
108 void UFDBregisterCountedIP( const char * address );
109 void UFDBinitializeIPcounters( void );
110 unsigned long UFDBgetNumberOfRegisteredIPs( void );
111 
112 void UFDBregisterCountedUser( const char * username );
113 void UFDBinitializeUserCounters( void );
114 unsigned long UFDBgetNumberOfRegisteredUsers( void );
115 
116 struct ufdbSetting {
117   char *           name;
118   char *           value;
119   struct ufdbSetting * next;
120 };
121 
122 void   ufdbLogFile( int, int, char * );
123 void   ufdbGlobalSetLogging( int logging );
124 void   ufdbRotateLogfile( char * );
125 void   UFDBrotateLogfile( void );
126 void   ufdbSetGlobalErrorLogFile( char * logdir, char * basename, int mutex_is_used );
127 void   UFDBcloseFilesNoLog( void );
128 
129 void   ufdbLogMessage( const char * format, ... )
130 #ifdef __GNUC__
131 __attribute__ ((format(printf,1,2)))
132 #endif
133 ;
134 void   ufdbLogError( const char *, ... )
135 #ifdef __GNUC__
136 __attribute__ ((format(printf,1,2)))
137 #endif
138 ;
139 void   ufdbLogFatalError( const char *, ... )
140 #ifdef __GNUC__
141 __attribute__ ((format(printf,1,2)))
142 #endif
143 ;
144 
145 typedef void (*ufdb_crashfun_t)(void);
146 void ufdbRegisterFatalErrorCrashfun( ufdb_crashfun_t crfun );
147 
148 #if !UFDB_BARE_METAL_SUPPORT
149 void ufdbGetSysInfo( struct utsname * si );
150 long ufdbGetNumCPUs( void );
151 #endif
152 
153 void UFDBlogConfig( struct ufdbGV * gv );
154 int  UFDBchangeStatus( int status );
155 
156 char * UFDBparseIPv6address( char * url, char * domain );
157 void   UFDBupdateURLwithNormalisedDomain( char * url, char * newDomain );
158 void   UFDBnormaliseIPv4( char * domain );
159 
160 /*
161  * strip a URL;
162  * remove http:// prefix, remove www[0-9*]. prefix,
163  * remove port number, remove username and password
164  */
165 void UFDBstripURL(
166    char * URL,                  /* input URL string */
167    char * strippedUrl,          /* output char array (must be 1024 bytes) */
168    char * domain,               /* output char array (must be 1024 bytes) */
169    char * protocol,             /* output char array (must be 16 bytes) */
170    int  * portnumber );         /* output integer */
171 
172 void UFDBstripURL2(
173    char * URL, 			/* input URL string */
174    int    stripwwwprefix,	/* input flag for stripping "www." prefix from URL */
175    char * strippedUrl,  	/* output char array (must be UFDB_MAX_URL_LENGTH bytes) */
176    char * domain,       	/* output char array (must be 1024 bytes) */
177    char * protocol,		/* output char array (must be 16 bytes) */
178    int  * portnumber );		/* output integer */
179 
180 char * UFDBprintable( char * string );
181 
182 char * UFDBfgets(
183    char * requestBuffer,
184    int    bufferSize,
185    FILE * fp );
186 
187 char * UFDBfgetsNoNL( char * s, int size, FILE * stream );
188 
189 int UFDBcalcCksum( char * mem, long size );
190 
191 void ufdbExecutePstack( const char * reason );
192 
193 #ifdef __cplusplus
194 }
195 #endif
196 
197 #endif
198 
199