]> git.ipfire.org Git - thirdparty/sarg.git/blob - sort.c
Protection against buffer overflows in getword and friends and report the origin...
[thirdparty/sarg.git] / sort.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
23 *
24 */
25
26 #include "include/conf.h"
27
28 void tmpsort()
29 {
30
31 DIR *dirp;
32 struct dirent *direntp;
33
34 char csort[MAXLEN];
35 char arqou[MAXLEN], arqin[MAXLEN], wnome[MAXLEN], wnome2[MAXLEN];
36 char field1[10]="2,2";
37 char field2[10]="1,1";
38 char field3[10]="3,3";
39 char order[4]="-r";
40
41 if(indexonly) return;
42 if(strstr(ReportType,"users_sites") == 0) return;
43
44 wnome[0]='\0';
45 wnome2[0]='\0';
46
47 dirp = opendir(tmp);
48 while ((direntp = readdir( dirp )) != NULL ){
49 if(strstr(direntp->d_name,".utmp") == 0)
50 continue;
51
52 strcpy(wentp,direntp->d_name);
53
54 wnome[0]='\0';
55 striptmp:
56 if (getword(wnome2,sizeof(wnome2),wentp,'.')<0) {
57 printf("SARG: Maybe you have a broken record or garbage in your %s directory.\n",tmp);
58 exit(1);
59 }
60 strcat(wnome,wnome2);
61
62 if(strcmp(wentp,"utmp") !=0) {
63 strcat(wnome,".");
64 goto striptmp;
65 }
66
67 strcpy(arqou,tmp);
68 strcat(arqou,"/");
69 strcpy(arqin,arqou);
70 strcat(arqou,wnome);
71 strcat(arqin,direntp->d_name);
72
73 if(debug) {
74 sprintf(msg,"%s: %s",text[54],arqou);
75 debuga(msg);
76 }
77
78 strup(UserSortField);
79 strlow(UserSortOrder);
80
81 if(strcmp(UserSortField,"CONNECT") == 0) {
82 strcpy(field1,"1,1");
83 strcpy(field2,"2,2");
84 strcpy(field3,"3,3");
85 }
86
87 if(strcmp(UserSortField,"SITE") == 0) {
88 strcpy(field1,"3,3");
89 strcpy(field2,"2,2");
90 strcpy(field3,"1,1");
91 }
92
93 if(strcmp(UserSortField,"TIME") == 0) {
94 strcpy(field1,"5,5");
95 strcpy(field2,"2,2");
96 strcpy(field3,"1,1");
97 }
98
99 if(strcmp(UserSortOrder,"normal") == 0)
100 order[0]='\0';
101
102 strcat(arqou,".txt");
103 sprintf(csort,"sort -n -T %s %s -k %s -k %s -k %s -o '%s' '%s'",TempDir,order,field1,field2,field3,arqou,arqin);
104 system(csort);
105 unlink(arqin);
106
107 }
108
109 (void)rewinddir( dirp );
110 (void)closedir( dirp );
111 return;
112 }
113
114 void sort_users_log(char *tmp, int debug)
115 {
116
117 DIR *dirp;
118 struct dirent *direntp;
119 char csort[MAXLEN];
120 char wtmp[MAXLEN];
121 char wname2[MAXLEN];
122 char wdname[MAXLEN];
123
124 if(debug) {
125 sprintf(msg,"%s",text[138]);
126 debuga(msg);
127 }
128
129 sprintf(wtmp,"%s/sarg",tmp);
130
131 dirp = opendir(wtmp);
132 while ( (direntp = readdir( dirp )) != NULL ){
133 if(strstr(direntp->d_name,".unsort") == 0)
134 continue;
135 if(strcmp(direntp->d_name,"denied.log") == 0 || strcmp(direntp->d_name,"authfail.log.unsort") == 0)
136 continue;
137
138 strcpy(wdname,direntp->d_name);
139
140 bzero(user, MAXLEN);
141 strip_unsort:
142 if (getword(wname2,sizeof(wname2),wdname,'.')<0) {
143 printf("SARG: Maybe you have a broken record or garbage in your %s directory.\n",wtmp);
144 exit(1);
145 }
146 strcat(user,wname2);
147
148 if(strcmp(wdname,"unsort") !=0) {
149 strcat(user,".");
150 goto strip_unsort;
151 }
152
153 if(strcmp(direntp->d_name,"download.unsort") == 0)
154 sprintf(csort,"sort -T %s -k 3,3 -k 1,1 -k 2,2 -k 5,5 -o '%s/%s.log' '%s/%s.unsort'",
155 tmp, wtmp, user, wtmp, user);
156 else
157 sprintf(csort,"sort -T %s -k 5,5 -k 1,1 -k 2,2 -o '%s/%s.log' '%s/%s.unsort'",
158 tmp, wtmp, user, wtmp, user);
159 system(csort);
160 sprintf(wdname,"%s/%s.unsort",wtmp,user);
161 unlink(wdname);
162 bzero(user, MAXLEN);
163
164 }
165 (void)rewinddir( dirp );
166 (void)closedir( dirp );
167
168 return;
169 }