-
Notifications
You must be signed in to change notification settings - Fork 5
/
CheckUserFolders.cpp
194 lines (186 loc) · 6.2 KB
/
CheckUserFolders.cpp
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string>
#include <vector>
#include "adreports_version.h"
#include "ldapconnection.h"
#include "dataoutput.h"
#include "adformats.h"
#ifdef __WIN32
#define PATH_SEPARATOR '\\'
#else
#define PATH_SEPARATOR '/'
#endif
void show_help()
{
printf(
"CheckUserFolders v" ADREPORTS_VERSION_STRING " - generate Active Directory home folder reports\n" \
"Credits: " ADREPORTS_CREDITS "\n" \
"Usage: CheckUserFolders " LDAP_COMMAND_LINE_PARAMETERS " [-f format] [-o file] [-q ldapfilter] path ...\n" \
"Parameters:\n" \
LDAP_COMMAND_LINE_HELP \
" -f format \tOutput format (" DATAOUTPUT_FORMAT_HELP_LIST ")\n" \
" -o file \tOutput file (default is standard output)\n" \
" -q ldapfilter \tLDAP filter\n" \
" path \tOne or more directories to scan\n" \
"\n"
);
}
int main (int argc, char *argv[])
{
char* dstformat = NULL;
char* dstfilename = NULL;
std::vector<std::string> paths;
std::string ldapfilter;
LDAPConnection* ldap = new LDAPConnection;
LDAPResponse* result;
//parse command line parameters
{
int i = 0;
const char* param;
while (++i < argc) {
if (!argv[i][0] || (argv[i][0] != '/' && argv[i][0] != '-')) {
paths.push_back(argv[i]);
} else if (!ldap->ProcessCommandLineParameter(argc, argv, i)) {
param = NULL;
switch (tolower(argv[i][1])) {
case '?' :
show_help();
return 0;
case 'f' :
if (argv[i][2])
param = argv[i] + 2;
else if (i + 1 < argc && argv[i + 1])
param = argv[++i];
if (!param)
return false;
dstformat = strdup(param);
break;
case 'o' :
if (argv[i][2])
param = argv[i] + 2;
else if (i + 1 < argc && argv[i + 1])
param = argv[++i];
if (!param)
return false;
dstfilename = strdup(param);
break;
case 'q' :
if (argv[i][2])
param = argv[i] + 2;
else if (i + 1 < argc && argv[i + 1])
param = argv[++i];
if (!param)
return false;
ldapfilter = param;
break;
default :
paths.push_back(argv[i]);
break;
}
}
}
}
if (paths.size() == 0) {
show_help();
return 0;
}
//initialize
time_t now = time(NULL);
DataOutputBase* dst = CreateDataOutput(dstformat, dstfilename);
if (dstfilename)
free(dstfilename);
if (dstformat) {
free(dstformat);
if (!dst) {
fprintf(stderr, "Invalid output format\n");
return 1;
}
}
//do LDAP stuff
const char* ldaperrmsg;
if ((ldaperrmsg = ldap->Open()) != NULL) {
fprintf(stderr, "Error opening LDAP connection: %s\n", ldaperrmsg);
return 2;
}
std::string searchfilter;
std::string searchfilterbase = "(&(objectCategory=person)(objectClass=user)";
if (!ldapfilter.empty()) {
searchfilterbase += "(";
searchfilterbase += ldapfilter;
searchfilterbase += ")";
}
//searchfilter += "(sAMAccountName=)";
//searchfilter += ")";
std::vector<std::string>::iterator path;
for (path = paths.begin(); path != paths.end(); path++) {
DIR* dir;
struct dirent* entry;
struct stat status;
std::string fullname;
//int pathlen = path->length();
dst->AddColumn("Folder", 40);
dst->AddColumn("Login", 18);
dst->AddColumn("Name", 18);
dst->AddColumn("Active", 23);
dst->AddColumn("Expires", 19);
dst->AddColumn("LastLogon", 19);
dst->AddColumn("Logons", 8);
dst->AddColumn("HomeDirectory", 48);
dst->AddColumn("ADName", 128);
if ((dir = opendir(path->c_str())) != NULL) {
while ((entry = readdir(dir)) != NULL) {
if (memcmp(entry->d_name, ".", 2) != 0 && memcmp(entry->d_name, "..", 3) != 0) {
fullname = *path + PATH_SEPARATOR + entry->d_name;
if (stat(fullname.c_str(), &status) == 0) {
if (S_ISDIR(status.st_mode)) {
searchfilter = searchfilterbase + "(sAMAccountName=" + entry->d_name + "))";
//searchfilter = searchfilterbase + ")";
dst->AddRow();
dst->AddData(fullname.c_str());
if ((result = ldap->Search(searchfilter.c_str())) != NULL && result->Rewind()) {
char* s;
do {
time_t expiration = timevalue2time(result->GetAttributeInt("accountExpires"));
long long accountctrl = result->GetAttributeInt("userAccountControl");
char activedata[32];
strcpy(activedata, (accountctrl & UF_ACCOUNTDISABLE ? "Disabled" : "Enabled"));
if (expiration && expiration <= now)
strcat(activedata, "+Expired");
if (accountctrl & UF_LOCKOUT /*|| result->GetAttributeInt("lockoutTime") != 0*/)
strcat(activedata, "+Locked");
dst->AddData(s = result->GetAttribute("sAMAccountName"));
free(s);
dst->AddData(s = result->GetAttribute("cn"));
free(s);
dst->AddData(activedata);
dst->AddData(format_time(expiration));
dst->AddData(format_timevalue(result->GetAttributeInt("lastLogonTimestamp")));
dst->AddData((int64_t)result->GetAttributeInt("logonCount"));
dst->AddData(s = result->GetAttribute("homeDirectory"));
free(s);
dst->AddData((s = result->GetDN()));
free(s);
} while (result->Next());
} else {
int i;
for (i = 0; i < 8; i++)
dst->AddData("");
}
}
}
}
}
closedir(dir);
}
}
//clean up
ldap->Close();
delete ldap;
delete dst;
return 0;
}