e3aaff8e |
/* |
68a6f51f |
* Copyright (C) 1999 - 2004 Tomasz Kojm <tkojm@clamav.net> |
e3aaff8e |
*
* This program is free software; you can redistribute it and/or modify |
bb34cb31 |
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. |
e3aaff8e |
*
* 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 |
48b7b4a7 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA. |
e3aaff8e |
*
*/
|
6d6e8271 |
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
|
e3aaff8e |
#include <stdio.h>
#include <stdlib.h> |
afb48b28 |
#include <string.h> |
e3aaff8e |
#include <ctype.h> |
34f71e0e |
#ifdef HAVE_UNISTD_H |
e3aaff8e |
#include <unistd.h> |
34f71e0e |
#endif |
f2beff4a |
#include <errno.h> |
34f71e0e |
#ifdef HAVE_PWD_H |
afb48b28 |
#include <pwd.h> |
34f71e0e |
#endif |
e3aaff8e |
#include <sys/types.h>
#include <sys/stat.h> |
34f71e0e |
#ifndef C_WINDOWS |
e3aaff8e |
#include <sys/wait.h>
#include <sys/time.h> |
34f71e0e |
#endif |
e3aaff8e |
#include <time.h>
#include <fcntl.h>
#include <signal.h>
#include <target.h>
|
8e65f09c |
#ifdef HAVE_REGEX_H
#include <regex.h>
#endif
|
7a2997f1 |
#include "shared/output.h" |
21cf4aeb |
#include "others.h" |
e3aaff8e |
int fileinfo(const char *filename, short i)
{
struct stat infostruct;
if(stat(filename, &infostruct) == -1)
return(-1);
switch(i) {
case 1: /* size */
return infostruct.st_size;
case 2: /* permissions */
return (mode_t)infostruct.st_mode;
case 3: /* modification time */
return infostruct.st_mtime;
case 4: /* UID */
return infostruct.st_uid;
case 5: /* GID */
return infostruct.st_gid;
default: |
0ae41a2d |
logg("!fileinfo(): Unknown option.\n"); |
e3aaff8e |
exit(1);
}
}
|
34f71e0e |
#ifdef C_WINDOWS
/* FIXME: Handle users correctly */
int checkaccess(const char *path, const char *username, int mode)
{
return _access(path, mode);
}
#else |
56bfccb2 |
int checkaccess(const char *path, const char *username, int mode) |
e3aaff8e |
{
struct passwd *user; |
56bfccb2 |
int ret = 0, status; |
e3aaff8e |
|
11f30313 |
if(!geteuid()) { |
e3aaff8e |
if((user = getpwnam(username)) == NULL) {
return -1;
}
|
56bfccb2 |
switch(fork()) {
case -1:
return -2;
case 0: |
9f51cb51 |
if(setgid(user->pw_gid)) {
fprintf(stderr, "ERROR: setgid(%d) failed.\n", (int) user->pw_gid);
exit(0);
}
if(setuid(user->pw_uid)) {
fprintf(stderr, "ERROR: setuid(%d) failed.\n", (int) user->pw_uid);
exit(0);
}
|
56bfccb2 |
if(access(path, mode))
exit(0);
else
exit(1);
default:
wait(&status);
if(WIFEXITED(status) && WEXITSTATUS(status) == 1)
ret = 1; |
e3aaff8e |
}
|
56bfccb2 |
} else {
if(!access(path, mode))
ret = 1; |
e3aaff8e |
}
|
56bfccb2 |
return ret; |
e3aaff8e |
} |
34f71e0e |
#endif |
e3aaff8e |
|
8e65f09c |
int match_regex(const char *filename, const char *pattern)
{
#ifdef HAVE_REGEX_H
regex_t reg;
int match, flags;
#if !defined(C_CYGWIN) && !defined(C_OS2) |
cc297984 |
flags = REG_EXTENDED; |
8e65f09c |
#else |
cc297984 |
flags = REG_EXTENDED | REG_ICASE; /* case insensitive on Windows */ |
8e65f09c |
#endif
if(regcomp(®, pattern, flags) != 0) {
logg("!%s: Could not parse regular expression %s.\n", filename, pattern);
return 2;
}
match = (regexec(®, filename, 0, NULL, 0) == REG_NOMATCH) ? 0 : 1;
regfree(®);
return match;
#else /* HAVE_REGEX_H */
return strstr(filename, pattern) ? 1 : 0;
#endif
} |