forked from Cisco-Talos/clamav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.c
144 lines (125 loc) · 3.61 KB
/
actions.c
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
/*
* Copyright (C) 2015 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
* Copyright (C) 2009 Sourcefire, Inc.
*
* Author: aCaB
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <fcntl.h>
#include <errno.h>
#include <libgen.h>
#include "libclamav/clamav.h"
#include "shared/optparser.h"
#include "shared/output.h"
#include "shared/misc.h"
#include "shared/actions.h"
void (*action)(const char *) = NULL;
unsigned int notmoved = 0, notremoved = 0;
static char *actarget;
static int targlen;
static int getdest(const char *fullpath, char **newname) {
char *tmps, *filename;
int fd, i;
tmps = strdup(fullpath);
if(!tmps) {
*newname=NULL;
return -1;
}
filename = basename(tmps);
if(!(*newname = (char *)malloc(targlen + strlen(filename) + 6))) {
free(tmps);
return -1;
}
sprintf(*newname, "%s"PATHSEP"%s", actarget, filename);
for(i=1; i<1000; i++) {
fd = open(*newname, O_WRONLY | O_CREAT | O_EXCL, 0600);
if(fd >= 0) {
free(tmps);
return fd;
}
if(errno != EEXIST) break;
sprintf(*newname, "%s"PATHSEP"%s.%03u", actarget, filename, i);
}
free(tmps);
free(*newname);
*newname = NULL;
return -1;
}
static void action_move(const char *filename) {
char *nuname;
int fd = getdest(filename, &nuname), copied = 0;
if(fd<0 || (rename(filename, nuname) && (copied=1) && filecopy(filename, nuname))) {
logg("!Can't move file %s\n", filename);
notmoved++;
if(nuname) unlink(nuname);
} else {
if(copied && unlink(filename))
logg("!Can't unlink '%s': %s\n", filename, strerror(errno));
else
logg("~%s: moved to '%s'\n", filename, nuname);
}
if(fd>=0) close(fd);
if(nuname) free(nuname);
}
static void action_copy(const char *filename) {
char *nuname;
int fd = getdest(filename, &nuname);
if(fd < 0 || filecopy(filename, nuname)) {
logg("!Can't copy file '%s'\n", filename);
notmoved++;
if(nuname) unlink(nuname);
} else
logg("~%s: copied to '%s'\n", filename, nuname);
if(fd>=0) close(fd);
if(nuname) free(nuname);
}
static void action_remove(const char *filename) {
if(unlink(filename)) {
logg("!Can't remove file '%s'.\n", filename);
notremoved++;
} else {
logg("~%s: Removed.\n", filename);
}
}
static int isdir(void) {
STATBUF sb;
if(CLAMSTAT(actarget, &sb) || !S_ISDIR(sb.st_mode)) {
logg("!'%s' doesn't exist or is not a directory\n", actarget);
return 0;
}
return 1;
}
int actsetup(const struct optstruct *opts) {
int move = optget(opts, "move")->enabled;
if(move || optget(opts, "copy")->enabled) {
actarget = optget(opts, move ? "move" : "copy")->strarg;
if(!isdir()) return 1;
action = move ? action_move : action_copy;
targlen = strlen(actarget);
} else if(optget(opts, "remove")->enabled)
action = action_remove;
return 0;
}