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