1 /*
2     webalizer - a web server log analysis program
3 
4     Copyright (C) 1997-2013  Bradford L. Barrett
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version, and provided that the above
10     copyright and permission notice is included with all distributed
11     copies of this or derived software.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 
22 */
23 
24 /*********************************************/
25 /* STANDARD INCLUDES                         */
26 /*********************************************/
27 
28 #include <time.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>                           /* normal stuff             */
33 #include <ctype.h>
34 #include <sys/utsname.h>
35 
36 /* ensure sys/types */
37 #ifndef _SYS_TYPES_H
38 #include <sys/types.h>
39 #endif
40 
41 /* need socket header? */
42 #ifdef HAVE_SYS_SOCKET_H
43 #include <sys/socket.h>
44 #endif
45 
46 /* some systems need this */
47 #ifdef HAVE_MATH_H
48 #include <math.h>
49 #endif
50 
51 #include "webalizer.h"                         /* main header              */
52 #include "lang.h"
53 #include "linklist.h"
54 
55 /* internal function prototypes */
56 
57 NLISTPTR new_nlist(char *);                         /* new list node       */
58 void     del_nlist(NLISTPTR *);                     /* del list            */
59 
60 GLISTPTR new_glist(char *, char *);                 /* new group list node */
61 void     del_glist(GLISTPTR *);                     /* del group list      */
62 
63 int      isinstr(char *, char *);
64 
65 /* Linkded list pointers */
66 GLISTPTR group_sites   = NULL;                /* "group" lists            */
67 GLISTPTR group_urls    = NULL;
68 GLISTPTR group_refs    = NULL;
69 GLISTPTR group_agents  = NULL;
70 GLISTPTR group_users   = NULL;
71 NLISTPTR hidden_sites  = NULL;                /* "hidden" lists           */
72 NLISTPTR hidden_urls   = NULL;
73 NLISTPTR hidden_refs   = NULL;
74 NLISTPTR hidden_agents = NULL;
75 NLISTPTR hidden_users  = NULL;
76 NLISTPTR ignored_sites = NULL;                /* "Ignored" lists          */
77 NLISTPTR ignored_urls  = NULL;
78 NLISTPTR ignored_refs  = NULL;
79 NLISTPTR ignored_agents= NULL;
80 NLISTPTR ignored_users = NULL;
81 NLISTPTR include_sites = NULL;                /* "Include" lists          */
82 NLISTPTR include_urls  = NULL;
83 NLISTPTR include_refs  = NULL;
84 NLISTPTR include_agents= NULL;
85 NLISTPTR include_users = NULL;
86 NLISTPTR index_alias   = NULL;                /* index. aliases           */
87 NLISTPTR html_pre      = NULL;                /* before anything else :)  */
88 NLISTPTR html_head     = NULL;                /* top HTML code            */
89 NLISTPTR html_body     = NULL;                /* body HTML code           */
90 NLISTPTR html_post     = NULL;                /* middle HTML code         */
91 NLISTPTR html_tail     = NULL;                /* tail HTML code           */
92 NLISTPTR html_end      = NULL;                /* after everything else    */
93 NLISTPTR page_type     = NULL;                /* page view types          */
94 NLISTPTR omit_page     = NULL;                /* pages not counted        */
95 NLISTPTR page_prefix   = NULL;                /* page view prefixes       */
96 GLISTPTR search_list   = NULL;                /* Search engine list       */
97 
98 /*********************************************/
99 /* NEW_NLIST - create new linked list node   */
100 /*********************************************/
101 
new_nlist(char * str)102 NLISTPTR new_nlist(char *str)
103 {
104    NLISTPTR newptr;
105 
106    if (sizeof(newptr->string) < strlen(str))
107    {
108       if (verbose)
109     fprintf(stderr,"[new_nlist] %s\n",msg_big_one);
110    }
111    if (( newptr = malloc(sizeof(struct nlist))) != NULL)
112     {strncpy(newptr->string, str, sizeof(newptr->string));newptr->next=NULL;}
113    return newptr;
114 }
115 
116 /*********************************************/
117 /* ADD_NLIST - add item to FIFO linked list  */
118 /*********************************************/
119 
add_nlist(char * str,NLISTPTR * list)120 int add_nlist(char *str, NLISTPTR *list)
121 {
122    NLISTPTR newptr,cptr,pptr;
123 
124    if ( (newptr = new_nlist(str)) != NULL)
125    {
126       if (*list==NULL) *list=newptr;
127       else
128       {
129          cptr=pptr=*list;
130          while(cptr!=NULL) { pptr=cptr; cptr=cptr->next; };
131          pptr->next = newptr;
132       }
133    }
134    return newptr==NULL;
135 }
136 
137 /*********************************************/
138 /* DEL_NLIST - delete FIFO linked list       */
139 /*********************************************/
140 
del_nlist(NLISTPTR * list)141 void del_nlist(NLISTPTR *list)
142 {
143    NLISTPTR cptr,nptr;
144 
145    cptr=*list;
146    while (cptr!=NULL)
147    {
148       nptr=cptr->next;
149       free(cptr);
150       cptr=nptr;
151    }
152 }
153 
154 /*********************************************/
155 /* NEW_GLIST - create new linked list node   */
156 /*********************************************/
157 
new_glist(char * str,char * name)158 GLISTPTR new_glist(char *str, char *name)
159 {
160    GLISTPTR newptr;
161 
162    if (sizeof(newptr->string) < strlen(str) ||
163        sizeof(newptr->name) < strlen(name))
164    {
165       if (verbose)
166 	fprintf(stderr,"[new_glist] %s\n",msg_big_one);
167    }
168    if (( newptr = malloc(sizeof(struct glist))) != NULL)
169      {
170        strncpy(newptr->string, str, sizeof(newptr->string));
171        strncpy(newptr->name, name, sizeof(newptr->name));
172        newptr->next=NULL;
173      }
174    return newptr;
175 }
176 
177 /*********************************************/
178 /* ADD_GLIST - add item to FIFO linked list  */
179 /*********************************************/
180 
add_glist(char * str,GLISTPTR * list)181 int add_glist(char *str, GLISTPTR *list)
182 {
183    GLISTPTR newptr,cptr,pptr;
184    char temp_buf[MAXKVAL];
185    char *name=temp_buf;
186    char sep=0;
187 
188    /* make local copy of string */
189    if (*str=='"' || *str=='\'') sep=*str++;             /* Quote character?  */
190    strncpy(temp_buf,str,MAXKVAL-1);
191    temp_buf[MAXKVAL-1]=0;
192 
193    if (!sep)                                            /* Space separated   */
194       while (!isspace((unsigned char)*name) && *name!=0) name++;
195    else
196       while (*name!=sep && *name!=0) name++;            /* Quote separated   */
197 
198    if (*name==0) name=temp_buf;
199    else
200    {
201       *name++=0;
202       while (isspace((unsigned char)*name)&&*name!=0) name++;
203       if (*name==0) name=temp_buf;
204    }
205 
206    if ( (newptr = new_glist(temp_buf, name)) != NULL)
207    {
208       if (*list==NULL) *list=newptr;
209       else
210       {
211          cptr=pptr=*list;
212          while(cptr!=NULL) { pptr=cptr; cptr=cptr->next; };
213          pptr->next = newptr;
214       }
215    }
216    return newptr==NULL;
217 }
218 
219 /*********************************************/
220 /* DEL_GLIST - delete FIFO linked list       */
221 /*********************************************/
222 
del_glist(GLISTPTR * list)223 void del_glist(GLISTPTR *list)
224 {
225    GLISTPTR cptr,nptr;
226 
227    cptr=*list;
228    while (cptr!=NULL)
229    {
230       nptr=cptr->next;
231       free(cptr);
232       cptr=nptr;
233    }
234 }
235 
236 /*********************************************/
237 /* ISINLIST - Test if string is in list      */
238 /*********************************************/
239 
isinlist(NLISTPTR list,char * str)240 char *isinlist(NLISTPTR list, char *str)
241 {
242    NLISTPTR lptr;
243 
244    lptr=list;
245    while (lptr!=NULL)
246    {
247       if (isinstr(str,lptr->string)) return lptr->string;
248       lptr=lptr->next;
249    }
250    return NULL;
251 }
252 
253 /*********************************************/
254 /* ISINGLIST - Test if string is in list     */
255 /*********************************************/
256 
isinglist(GLISTPTR list,char * str)257 char *isinglist(GLISTPTR list, char *str)
258 {
259    GLISTPTR lptr;
260 
261    lptr=list;
262    while (lptr!=NULL)
263    {
264       if (isinstr(str,lptr->string)) return lptr->name;
265       lptr=lptr->next;
266    }
267    return NULL;
268 }
269 
270 /*********************************************/
271 /* ISINSTR - Scan for string in string       */
272 /*********************************************/
273 
isinstr(char * str,char * cp)274 int isinstr(char *str, char *cp)
275 {
276    char *cp1,*cp2;
277 
278    cp1=(cp+strlen(cp))-1;
279    if (*cp=='*')
280    {
281       /* if leading wildcard, start from end */
282       cp2=str+strlen(str)-1;
283       while ( (cp1!=cp) && (cp2!=str))
284       {
285          if (*cp1=='*') return 1;
286          if (*cp1--!=*cp2--) return 0;
287       }
288       if (cp1==cp) return 1;
289       else return 0;
290    }
291    else
292    {
293       /* if no leading/trailing wildcard, just strstr */
294       if (*cp1!='*') return(strstr(str,cp)!=NULL);
295       /* otherwise do normal forward scan */
296       cp1=cp; cp2=str;
297       while (*cp2!='\0')
298       {
299          if (*cp1=='*') return 1;
300          if (*cp1++!=*cp2++) return 0;
301       }
302       if (*cp1=='*') return 1;
303          else return 0;
304    }
305 }
306