-
Notifications
You must be signed in to change notification settings - Fork 5
/
ADReportGroups.cpp
266 lines (260 loc) · 9 KB
/
ADReportGroups.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
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
#include <stdio.h>
#include <ctype.h>
#include <string>
#include <vector>
#include "adreports_version.h"
#include "ldapconnection.h"
#include "dataoutput.h"
#include "adformats.h"
void show_help()
{
printf(
"ADReportGroups v" ADREPORTS_VERSION_STRING " - generate Active Directory group reports\n" \
"Credits: " ADREPORTS_CREDITS "\n" \
"Usage: ADReportGroups " LDAP_COMMAND_LINE_PARAMETERS " [-f format] [-o file] [-l user] [-g] [-d] [-c days] [-q ldapfilter]\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" \
" -l user \tInclude all groups this user is a member of (may be\n" \
" \tspecified multiple times, default is all groups)\n" \
" -g \tSecurity groups only\n" \
" -d \tDistribution groups only\n" \
" -n \tGet count of members and enabled members in each group (can be slow)\n" \
" -c days \tShow only groups created in the last number of days\n" \
" -q ldapfilter \tLDAP filter\n" \
"\n"
);
}
int main (int argc, char *argv[])
{
char* dstformat = NULL;
char* dstfilename = NULL;
std::vector<std::string> users;
bool securitygroupsonly = false;
bool distributiongroupsonly = false;
bool addcounter = false;
int createdlastdays = -1;
std::string ldapfilter;
LDAPConnection* ldap = new LDAPConnection;
LDAPResponse* result;
//parse command line parameters
{
int i = 0;
const char* param;
bool paramerror = false;
while (!paramerror && ++i < argc) {
if (!argv[i][0] || (argv[i][0] != '/' && argv[i][0] != '-')) {
paramerror = true;
break;
}
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 'l' :
if (argv[i][2])
param = argv[i] + 2;
else if (i + 1 < argc && argv[i + 1])
param = argv[++i];
if (!param)
return false;
users.push_back(param);
break;
case 'g' :
securitygroupsonly = true;
break;
case 'd' :
distributiongroupsonly = true;
break;
case 'n' :
addcounter = true;
break;
case 'c' :
if (argv[i][2])
param = argv[i] + 2;
else if (i + 1 < argc && argv[i + 1])
param = argv[++i];
if (!param)
return false;
createdlastdays = atoi(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 :
paramerror = true;
break;
}
}
}
if (paramerror) {
fprintf(stderr, "Invalid command line parameters\n");
return 1;
}
}
//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 = "(&(objectCategory=group)(objectClass=group)";
if (securitygroupsonly) {
searchfilter += "(groupType:1.2.840.113556.1.4.803:=2147483648)"; //LDAP_MATCHING_RULE_BIT_AND, ADS_GROUP_TYPE_SECURITY_ENABLED (0x80000000)
}
if (distributiongroupsonly) {
searchfilter += "(!(groupType:1.2.840.113556.1.4.803:=2147483648))"; //LDAP_MATCHING_RULE_BIT_AND, ADS_GROUP_TYPE_SECURITY_ENABLED (0x80000000)
}
//TO DO: when not selecting either only security groups or only distribution groups only security groups are listed
//if (!securitygroupsonly && !distributiongroupsonly) {
// searchfilter += "(|((groupType:1.2.840.113556.1.4.803:=2147483648))(!(groupType:1.2.840.113556.1.4.803:=2147483648)))"; //LDAP_MATCHING_RULE_BIT_AND, ADS_GROUP_TYPE_SECURITY_ENABLED (0x80000000)
//}
if (users.size() > 0) {
std::vector<std::string>::iterator user;
searchfilter += "(|";
for (user = users.begin(); user != users.end(); user++) {
searchfilter += "(member:1.2.840.113556.1.4.1941:="; //LDAP_MATCHING_RULE_IN_CHAIN
searchfilter += *user;
searchfilter += ")";
}
searchfilter += ")";
}
if (createdlastdays > 0) {
searchfilter += "(whenCreated>=";
searchfilter += time2timestamp(now - createdlastdays * 24 * 60 * 60);
searchfilter += ")";
}
if (!ldapfilter.empty()) {
searchfilter += "(";
searchfilter += ldapfilter;
searchfilter += ")";
}
searchfilter += ")";
//printf("%s\n", searchfilter.c_str());
if ((result = ldap->Search(searchfilter.c_str())) == NULL) {
fprintf(stderr, "LDAP search failed\n");
} else {
dst->AddColumn("Name", 40);
dst->AddColumn("Created", 19);
dst->AddColumn("LastChanged", 19);
dst->AddColumn("GroupScope", 12);
dst->AddColumn("GroupType", 12);
dst->AddColumn("ADName", 128);
dst->AddColumn("Description", 48);
dst->AddColumn("PrimaryEmail", 48);
if (addcounter) {
dst->AddColumn("MemberCount", 5);
dst->AddColumn("EnabledMemberCount", 5);
}
if (result->Rewind()) {
char* s;
std::string type;
do {
dst->AddRow();
dst->AddData(s = result->GetAttribute("cn"));
free(s);
dst->AddData(format_timestamp(s = result->GetAttribute("whenCreated")));
free(s);
dst->AddData(format_timestamp(s = result->GetAttribute("whenChanged")));
free(s);
type = "";
if (result->GetAttributeInt("groupType") & 0x00000002)
type += " global";
if (result->GetAttributeInt("groupType") & 0x00000004)
type += " domain local";
if (result->GetAttributeInt("groupType") & 0x00000008)
type += " universal";
if (type.length() > 0)
type.erase(0, 1);
dst->AddData(type.c_str());
type = (result->GetAttributeInt("groupType") & 0x80000000 ? "security" : "distribution");
if (result->GetAttributeInt("groupType") & 0x00000001)
type += " (system)";
if (result->GetAttributeInt("groupType") & 0x00000010)
type += " APP_BASIC";
if (result->GetAttributeInt("groupType") & 0x00000020)
type += " APP_QUERY";
dst->AddData(type.c_str());
dst->AddData((s = result->GetDN()));
free(s);
dst->AddData(replace_line_breaks_with_spaces(s = result->GetAttribute("description")));
free(s);
dst->AddData(get_primary_smtp_address(s = result->GetAttribute("proxyAddresses")));
free(s);
if (addcounter) {
LDAPResponse* subresult;
std::string subsearchfilter = "(&";
s = result->GetDN();
subsearchfilter += "(memberOf:1.2.840.113556.1.4.1941:=";
subsearchfilter += s;
subsearchfilter += ")";
free(s);
subsearchfilter += ")";
if ((subresult = ldap->Search(subsearchfilter.c_str())) == NULL) {
dst->AddData("ERROR");
} else {
long long accountctrl;
int64_t countmembers = 0;
int64_t countenabledmembers = 0;
if (subresult->Rewind()) {
do {
countmembers++;
accountctrl = subresult->GetAttributeInt("userAccountControl");
if ((accountctrl & UF_ACCOUNTDISABLE) == 0)
countenabledmembers++;
} while (subresult->Next());
delete subresult;
}
dst->AddData(countmembers);
dst->AddData(countenabledmembers);
}
}
} while (result->Next());
}
delete result;
}
//clean up
ldap->Close();
delete ldap;
delete dst;
return 0;
}