forked from Magentron/chkrootkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchkdirs.c
262 lines (227 loc) · 6.96 KB
/
chkdirs.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
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
/* Copyright (C) Hal Pomeranz <[email protected]> and Deer Run Assoc, 2002/11/24
All rights reserved. Permission granted to freely redistribute and update
as long as this Copyright notice is preserved. No warranty expressed or
implied.
$Id: chkdirs.c,v 1.3 2003/01/20 19:44:13 hal Exp $
Usage: chkdirs [-n] dir ...
Examples: chkdirs /
chkdirs -n /proc
Recursively traverses one or more directories looking for discrepancies
between the parent directory link count and the number of subdirectories
(parent directory link count should always equal the number of subdirs
plus two-- anything else indicates a "hidden" directory). "-n" option
means check directory but don't recursively descend into subdirectories.
Changelog :
2002/12/19 - Little port for *BSB and Solaris - Nelson Murilo
2003/01/09 - More fix for Solaris - Nelson Murilo
2003/01/14 - HP-UX patch - Gerard Breiner
2003/01/20 - NAME_MAX Fix by Hal Pomeranz
2003/09/01 - BSDI port by Nelson Murilo and Thomas Davidson
2005/22/05 - APPLE test for limits.h included by Aaron Harwood
2007/08/10 - strncpy used instead of strcpy - nm
2007/12/24 - change `c' variable type - NIDE, Naoyuki
*/
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun) || defined (hpux) || defined (__bsdi__) || defined (bsdi) || defined (__APPLE__)
#include <limits.h>
#elif defined(__APPLE__) && defined(__MACH__)
#include <sys/syslimits.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#ifndef NAME_MAX
#define NAME_MAX PATH_MAX
#endif
struct dirinfolist {
char dil_name[NAME_MAX+1];
int dil_lc;
struct dirinfolist *dil_next;
};
void usage ()
{
fprintf(stderr, "chkdirs [-n] dir ...\n");
exit(255);
}
char *make_pathname (char *path, char *dir, char **buffer)
{
int plen, pathname_len, bufsize, offs;
bufsize = 0;
plen = strlen(path);
pathname_len = plen + strlen(dir) + 2;
if (!(*buffer) || (sizeof(*buffer) < pathname_len)) {
if (buffer) free((void *)*buffer);
bufsize = (pathname_len > PATH_MAX) ? pathname_len : PATH_MAX;
if (!(*buffer = (char *)malloc(bufsize))) {
return((char *)NULL);
}
}
if (dir[0] == '/') { /* "dir" is absolute pathname, don't prepend "path" */
offs = 0;
}
else {
strncpy(*buffer, path, bufsize);
if ((*buffer)[plen-1] == '/') { /* "path" ends in "/", don't add extra */
offs = plen;
}
else {
(*buffer)[plen] = '/';
offs = plen + 1;
}
}
strncpy((*buffer)+offs, dir, bufsize - offs);
return((*buffer));
}
int check_dir (char *dir, char *path, int linkcount, int norecurse)
{
int diff = -1;
int plen, buflen, numdirs;
char *curpath, *fullpath;
DIR *dirhandle;
struct dirent *finfo;
struct dirinfolist *dl, *dptr;
struct stat statinfo;
/* When called recursively, "path" will be the full path of the cwd,
but when called from main() "path" is empty. We need the cwd path
so we can chdir() back at the end of this routine, as well as when
printing errors and other output.
*/
if (!path || !(plen = strlen(path))) {
buflen = PATH_MAX;
retry:
if (!(curpath = (char *)malloc(buflen))) {
fprintf(stderr, "malloc() failed: %s\n", strerror(errno));
return(-1);
}
if (!getcwd(curpath, buflen)) {
if (errno == ERANGE) {
free((void *)curpath);
buflen = buflen * 2;
goto retry;
}
else {
fprintf(stderr, "getcwd() failed: %s\n", strerror(errno));
return(-1);
}
}
}
else { /* "path" is set, so just copy it into "curpath" */
if (!(curpath = (char *)malloc(plen+1))) {
fprintf(stderr, "malloc() failed: %s\n", strerror(errno));
return(-1);
}
strncpy(curpath, path, plen+1);
}
/* Now set "fullpath" to be the absolute path name of the directory
we will be checking (prepend "curpath" if "dir" is not already an
absolute pathname).
*/
fullpath = (char *)NULL;
if (!make_pathname(curpath, dir, &fullpath)) {
fprintf(stderr, "make_pathname() failed: %s\n", strerror(errno));
free((void *)curpath);
return(-1);
}
if (chdir(dir)) {
fprintf(stderr, "chdir(%s): %s\n", fullpath, strerror(errno));
free((void *)curpath);
free((void *)fullpath);
return(-1);
}
/* Again, "linkcount" (the link count of the current directory) is set
only if check_dir() is called recursively. Otherwise, we need to
stat the directory ourselves.
*/
if (!linkcount) {
if (lstat(".", &statinfo)) {
fprintf(stderr, "lstat(%s): %s\n", fullpath, strerror(errno));
goto abort;
}
linkcount = statinfo.st_nlink;
}
if (!(dirhandle = opendir("."))) {
fprintf(stderr, "opendir(%s): %s\n", fullpath, strerror(errno));
goto abort;
}
numdirs = 0;
dl = (struct dirinfolist *)NULL;
while ((finfo = readdir(dirhandle))) {
if (!strcmp(finfo->d_name, ".") || !strcmp(finfo->d_name, ".."))
continue;
if (lstat(finfo->d_name, &statinfo)) {
fprintf(stderr, "lstat(%s/%s): %s\n",
fullpath, finfo->d_name, strerror(errno));
closedir(dirhandle);
goto abort;
}
if (S_ISDIR(statinfo.st_mode)) {
numdirs++;
if (norecurse) continue; /* just count subdirs if "-n" */
/* Otherwise, keep a list of all directories found that have link
count > 2 (indicating directory contains subdirectories). We'll
call check_dir() on each of these subdirectories in a moment...
*/
if (statinfo.st_nlink > 2) {
dptr = dl;
if (!(dl = (struct dirinfolist *)malloc(sizeof(struct dirinfolist)))) {
fprintf(stderr, "malloc() failed: %s\n", strerror(errno));
norecurse = 1;
while (dptr) {
dl = dptr->dil_next;
free((void *)dptr);
dptr = dl;
}
continue;
}
strncpy(dl->dil_name, finfo->d_name, sizeof(dl->dil_name));
dl->dil_lc = statinfo.st_nlink;
dl->dil_next = dptr;
}
}
}
closedir(dirhandle);
/* Parent directory link count had better equal #subdirs+2... */
diff = linkcount - numdirs - 2;
if (diff) printf("%d\t%s\n", diff, fullpath);
/* Now check all subdirectories in turn... */
while (dl) {
check_dir(dl->dil_name, fullpath, dl->dil_lc, norecurse);
dptr = dl->dil_next;
free((void *)dl);
dl = dptr;
}
abort:
if (chdir(curpath)) {
fprintf(stderr, "Final chdir(%s) failed (%s) -- EXIT!\n",
curpath, strerror(errno));
exit(255);
}
free((void *)fullpath);
free((void *)curpath);
return(diff);
}
int main (int argc, char **argv)
{
int norecurse = 0;
int i, retval;
int c;
opterr = 0;
while ((c = getopt(argc, argv, "n")) > 0) {
switch (c) {
case 'n':
norecurse = 1;
break;
default:
usage();
}
}
if (argc <= optind) usage();
for (i = optind; i < argc; i++) {
retval = check_dir(argv[i], (char *)NULL, 0, norecurse);
}
exit(retval);
}