File: userinfo.c

package info (click to toggle)
sarg 2.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,456 kB
  • sloc: ansic: 17,692; sh: 4,581; xml: 371; javascript: 352; php: 205; makefile: 183; sed: 16; pascal: 2
file content (468 lines) | stat: -rw-r--r-- 11,745 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
 * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
 *                                                            1998, 2015
 *
 * SARG donations:
 *      please look at http://sarg.sourceforge.net/donations.php
 * Support:
 *     http://sourceforge.net/projects/sarg/forums/forum/363374
 * ---------------------------------------------------------------------
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 */

#include "include/conf.h"
#include "include/defs.h"
#include "include/stringbuffer.h"
#include "include/alias.h"

//! The number of users to group in one unit.
#define USERS_PER_GROUP 50

/*! \brief Group the users in one allocation unit.
Structure to store a group of users and reduce the number of memory
allocations.
*/
struct usergroupstruct
{
	//! The next group of users.
	struct usergroupstruct *next;
	//! A group of users.
	struct userinfostruct list[USERS_PER_GROUP];
	//! The number of users stored in the list.
	int nusers;
};

/*! \brief Hold pointer to scan through the user list.
*/
struct userscanstruct
{
	//! The group containing the user.
	struct usergroupstruct *group;
	//! The index of the user in the group.
	int index;
};

//! The first group of users.
static struct usergroupstruct *first_user_group=NULL;
//! The counter to generate unique user number when ::AnonymousOutputFiles is set.
static int AnonymousCounter=0;
//! String buffer to store the user's related constants.
static StringBufferObject UserStrings=NULL;
//! User aliases.
static AliasObject UserAliases=NULL;

extern struct ReadLogDataStruct ReadFilter;
extern char StripUserSuffix[MAX_USER_LEN];
extern int StripSuffixLen;
extern char *userfile;

struct userinfostruct *userinfo_create(const char *userid,const char *ip)
{
	struct usergroupstruct *group, *last;
	struct userinfostruct *user;
	int i, j, lastuser;
	int skip;
	int flen;
	int count, clen;
	char cstr[9];
	char filename[MAX_USER_FNAME_LEN];

	if (!UserStrings) {
		UserStrings=StringBuffer_Create();
		if (!UserStrings) {
			debuga(__FILE__,__LINE__,_("Not enough memory to store the user's strings\n"));
			exit(EXIT_FAILURE);
		}
	}

	last=NULL;
	for (group=first_user_group ; group ; group=group->next) {
		if (group->nusers<USERS_PER_GROUP) break;
		last=group;
	}

	if (!group) {
		group=malloc(sizeof(*group));
		if (!group) {
			debuga(__FILE__,__LINE__,_("Not enough memory to store user \"%s\"\n"),userid);
			exit(EXIT_FAILURE);
		}
		memset(group,0,sizeof(*group));
		if (last)
			last->next=group;
		else
			first_user_group=group;
	}
	user=group->list+group->nusers++;

	user->id=StringBuffer_Store(UserStrings,userid);
	if (!user->id) {
		debuga(__FILE__,__LINE__,_("Not enough memory to store user ID \"%s\"\n"),userid);
		exit(EXIT_FAILURE);
	}
	user->label=user->id; //assign a label to avoid a NULL pointer in case none is provided
	if (ip) {
		/*
		 * IP address is not the same as the user's ID. A separate buffer
		 * must be allocated.
		 */
		user->id_is_ip=false;
		user->ip=StringBuffer_Store(UserStrings,ip);
	} else {
		/*
		 * User's IP address share the same buffer as the user's ID.
		 */
		user->id_is_ip=true;
		user->ip=user->id;
	}

	if (AnonymousOutputFiles) {
		snprintf(filename,sizeof(filename),"%d",AnonymousCounter++);
	} else {
		skip=0;
		j=0;
		for (i=0 ; userid[i] && j<MAX_USER_FNAME_LEN-1 ; i++) {
			if (isalnum(userid[i]) || userid[i]=='-' || userid[i]=='_') {
				filename[j++]=userid[i];
				skip=0;
			} else {
				if (!skip) {
					filename[j++]='_';
					skip=1;
				}
			}
		}
		if (j==0) filename[j++]='_'; //don't leave a file name empty
		flen=j;
		filename[j]='\0';

		count=0;
		for (group=first_user_group ; group ; group=group->next) {
			lastuser=(group->next) ? group->nusers : group->nusers-1;
			for (i=0 ; i<lastuser ; i++) {
				if (strcasecmp(filename,group->list[i].filename)==0) {
					clen=sprintf(cstr,"+%X",count++);
					if (flen+clen<MAX_USER_FNAME_LEN)
						strcpy(filename+flen,cstr);
					else
						strcpy(filename+MAX_USER_FNAME_LEN-clen,cstr);
				}
			}
		}
	}
	user->filename=StringBuffer_Store(UserStrings,filename);
	if (!user->filename)
	{
		debuga(__FILE__,__LINE__,_("Not enough memory to store the file name for user \"%s\"\n"),user->id);
		exit(EXIT_FAILURE);
	}

	return(user);
}

void userinfo_free(void)
{
	struct usergroupstruct *group, *next;

	for (group=first_user_group ; group ; group=next) {
		next=group->next;
		free(group);
	}
	first_user_group=NULL;
	StringBuffer_Destroy(&UserStrings);
}

/*!
 * Store the user's label.
 * \param uinfo The user info structure created by userinfo_create().
 * \param label The string label to store.
 */
void userinfo_label(struct userinfostruct *uinfo,const char *label)
{
	if (!uinfo) return;
	if (!UserStrings) return;
	uinfo->label=StringBuffer_Store(UserStrings,label);
	if (!uinfo->label) {
		debuga(__FILE__,__LINE__,_("Not enough memory to store label \"%s\" of user \"%s\"\n"),label,uinfo->id);
		exit(EXIT_FAILURE);
	}
}

struct userinfostruct *userinfo_find_from_file(const char *filename)
{
	struct usergroupstruct *group;
	int i;

	for (group=first_user_group ; group ; group=group->next) {
		for (i=0 ; i<group->nusers ; i++)
			if (strcmp(filename,group->list[i].filename)==0)
				return(group->list+i);
	}
	return(NULL);
}

struct userinfostruct *userinfo_find_from_id(const char *id)
{
	struct usergroupstruct *group;
	int i;

	for (group=first_user_group ; group ; group=group->next) {
		for (i=0 ; i<group->nusers ; i++)
			if (strcmp(id,group->list[i].id)==0)
				return(group->list+i);
	}
	return(NULL);
}

struct userinfostruct *userinfo_find_from_ip(const char *ip)
{
	struct usergroupstruct *group;
	int i;

	for (group=first_user_group ; group ; group=group->next) {
		for (i=0 ; i<group->nusers ; i++)
			if (strcmp(ip,group->list[i].ip)==0)
				return(group->list+i);
	}
	return(NULL);
}

/*!
Start the scanning of the user list.

\return The object to pass to subsequent scanning functions or NULL
if it failed. The object must be freed with a call to userinfo_stop().
*/
userscan userinfo_startscan(void)
{
	userscan uscan;

	uscan=malloc(sizeof(*uscan));
	if (!uscan) return(NULL);
	uscan->group=first_user_group;
	uscan->index=0;
	return(uscan);
}

/*!
Free the memory allocated by userinfo_start().

\param uscan The object created by userinfo_start().
*/
void userinfo_stopscan(userscan uscan)
{
	free(uscan);
}

/*!
Get the user pointed to by the object and advance the object
to the next user.

\param uscan The object created by userinfo_start().

\return The user in the list or NULL if the end of the list
is reached.
*/
struct userinfostruct *userinfo_advancescan(userscan uscan)
{
	struct userinfostruct *uinfo;

	if (!uscan) return(NULL);
	if (!uscan->group) return(NULL);
	if (uscan->index<0 || uscan->index>=uscan->group->nusers) return(NULL);

	uinfo=uscan->group->list+uscan->index;

	++uscan->index;
	if (uscan->index>=uscan->group->nusers) {
		uscan->group=uscan->group->next;
		uscan->index=0;
	}
	return(uinfo);
}

/*!
Clear the general purpose flag from all the user's info.
*/
void userinfo_clearflag(void)
{
	struct usergroupstruct *group;
	int i;

	for (group=first_user_group ; group ; group=group->next) {
		for (i=0 ; i<group->nusers ; i++)
			group->list[i].flag=0;
	}
}

/*!
Read the file containing the user names to alias in the report.

\param Filename The name of the file.
*/
void read_useralias(const char *Filename)
{
	FileObject *fi;
	longline line;
	char *buf;

	if (debug) debuga(__FILE__,__LINE__,_("Reading user alias file \"%s\"\n"),Filename);

	UserAliases=Alias_Create();
	if (!UserAliases) {
		debuga(__FILE__,__LINE__,_("Cannot store user's aliases\n"));
		exit(EXIT_FAILURE);
	}

	fi=FileObject_Open(Filename);
	if (!fi) {
		debuga(__FILE__,__LINE__,_("Cannot read user name alias file \"%s\": %s\n"),Filename,FileObject_GetLastOpenError());
		exit(EXIT_FAILURE);
	}

	if ((line=longline_create())==NULL) {
		debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),Filename);
		exit(EXIT_FAILURE);
	}

	while ((buf=longline_read(fi,line)) != NULL) {
		if (Alias_Store(UserAliases,buf)<0) {
			debuga(__FILE__,__LINE__,_("While reading \"%s\"\n"),Filename);
			exit(EXIT_FAILURE);
		}
	}

	longline_destroy(&line);
	if (FileObject_Close(fi)) {
		debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),Filename,FileObject_GetLastCloseError());
		exit(EXIT_FAILURE);
	}

	if (debug) {
		debuga(__FILE__,__LINE__,_("List of user names to alias:\n"));
		Alias_PrintList(UserAliases);
	}
}

/*!
Free the memory allocated by read_useralias().
*/
void free_useralias(void)
{
	Alias_Destroy(&UserAliases);
}

/*!
Replace the user's name or ID by an alias if one is defined.

\param user The user's name or ID as extracted from the report.

\retval USERERR_NoError No error.
\retval USERERR_NameTooLong User name too long.
*/
enum UserProcessError process_user(const char **UserPtr,const char *IpAddress,bool *IsIp)
{
	const char *user=*UserPtr;
	static char UserBuffer[MAX_USER_LEN];
	const char *auser;

	if (UserIp) {
		user=IpAddress;
		*IsIp=true;
	} else {
		*IsIp=false;

		if (StripSuffixLen>0)
		{
			int x=strlen(user);
			if (x>StripSuffixLen && strcasecmp(user+(x-StripSuffixLen),StripUserSuffix)==0)
			{
				if (x-StripSuffixLen>=sizeof(UserBuffer))
					return(USERERR_NameTooLong);
				safe_strcpy(UserBuffer,user,x-StripSuffixLen+1);
				user=UserBuffer;
			}
		}
		if (strlen(user)>MAX_USER_LEN)
			return(USERERR_NameTooLong);

		if (testvaliduserchar(user))
			return(USERERR_InvalidChar);

		if ((user[0]=='\0') || (user[1]=='\0' && (user[0]=='-' || user[0]==' '))) {
			if (RecordsWithoutUser == RECORDWITHOUTUSER_IP) {
				user=IpAddress;
				*IsIp=true;
			}
			if (RecordsWithoutUser == RECORDWITHOUTUSER_IGNORE)
				return(USERERR_EmptyUser);
			if (RecordsWithoutUser == RECORDWITHOUTUSER_EVERYBODY)
				user="everybody";
		} else {
			if (NtlmUserFormat == NTLMUSERFORMAT_USER) {
				const char *str;
				if ((str=strchr(user,'+'))!=NULL || (str=strchr(user,'\\'))!=NULL || (str=strchr(user,'_'))!=NULL) {
					user=str+1;
				}
			}
		}
	}

	if (us[0]!='\0' && strcmp(user,us)!=0)
		return(USERERR_Untracked);

	if (ReadFilter.SysUsers) {
		char wuser[MAX_USER_LEN+2]=":";

		strcat(wuser,user);
		strcat(wuser,":");
		if (strstr(userfile, wuser) == 0)
			return(USERERR_SysUser);
	}

	if (ReadFilter.UserFilter) {
		if (!vuexclude(user)) {
			if (debugz>=LogLevel_Process) debuga(__FILE__,__LINE__,_("Excluded user: %s\n"),user);
			return(USERERR_Ignored);
		}
	}

	auser=Alias_Replace(UserAliases,user);
	if (auser!=user) {
		if (*auser==ALIAS_PREFIX) auser++;//no need for that indicator for a user name
		user=auser;
		*IsIp=false;
	}

	// include_users
	if (IncludeUsers[0] != '\0') {
		char wuser[MAX_USER_LEN+2]=":";
		char *str;

		strcat(wuser,user);
		strcat(wuser,":");
		str=strstr(IncludeUsers,wuser);
		if (!str)
			return(USERERR_Excluded);
	}

	if (user[0]=='\0' || (user[1]=='\0' && (user[0]=='-' || user[0]==' ' || user[0]==':')))
		return(USERERR_EmptyUser);

	*UserPtr=user;
	return(USERERR_NoError);
}