3 # adssearch.pl - query an Active Directory server and
4 # display objects in a human readable format
6 # Copyright (C) Guenther Deschner <gd@samba.org> 2003-2007
8 # TODO: add range retrieval
9 # write sddl-converter, decode userParameters
10 # apparently only win2k3 allows simple-binds with machine-accounts.
11 # make sasl support independent from Authen::SASL::Cyrus v >0.11
15 use Net::LDAP::Control;
16 use Net::LDAP::Constant qw(LDAP_REFERRAL);
19 use POSIX qw(strftime);
26 my $class = 'Authen::SASL';
27 $pref_version = "0.32";
28 if ( eval "require $class;" ) {
31 if ( eval "Net::LDAP->VERSION($pref_version);" ) {
36 # users may set defaults here
44 my $tdbdump = "/usr/bin/tdbdump";
45 my $testparm = "/usr/bin/testparm";
46 my $net = "/usr/bin/net";
47 my $dig = "/usr/bin/dig";
48 my $nmblookup = "/usr/bin/nmblookup";
49 my $secrets_tdb = "/etc/samba/secrets.tdb";
50 my $klist = "/usr/bin/klist";
51 my $kinit = "/usr/bin/kinit";
62 $opt_display_extendeddn,
63 $opt_display_metadata,
90 'base|b=s' => \$opt_base,
91 'D|DN=s' => \$opt_binddn,
92 'debug=i' => \$opt_debug,
93 'domain_scope' => \$opt_domain_scope,
94 'extendeddn|e:i' => \$opt_display_extendeddn,
95 'fastbind' => \$opt_fastbind,
97 'host|h=s' => \$opt_host,
98 'machine|P' => \$opt_machine,
99 'metadata|m' => \$opt_display_metadata,
100 'nodiffs' => \$opt_notify_nodiffs,
101 'notify|n' => \$opt_notify,
102 'paging:i' => \$opt_paging,
103 'password|w=s' => \$opt_password,
104 'port=i' => \$opt_port,
105 'rawdisplay' => \$opt_display_raw,
106 'realm|R=s' => \$opt_realm,
107 'rootDSE' => \$opt_dump_rootdse,
108 'saslmech|Y=s' => \$opt_saslmech,
109 'schema|c' => \$opt_dump_schema,
110 'scope|s=s' => \$opt_scope,
111 'simpleauth|x' => \$opt_simpleauth,
112 'tls|Z' => \$opt_starttls,
113 'user|U=s' => \$opt_user,
114 'verbose|v' => \$opt_verbose,
115 'wknguid' => \$opt_dump_wknguid,
116 'workgroup|k=s' => \$opt_workgroup,
120 if (!@ARGV && !$opt_dump_schema && !$opt_dump_rootdse && !$opt_notify || $opt_help) {
125 if ($opt_fastbind && !$opt_simpleauth) {
126 printf("LDAP fast bind can only be performed with simple binds\n");
142 my (@ctrls, @ctrls_s);
143 my ($ctl_paged, $cookie);
144 my ($page_count, $total_entry_count);
145 my ($sasl_hd, $async_ldap_hd, $sync_ldap_hd);
150 # fixed values and vars
153 my $tabsize = "\t\t\t";
156 my $scope = $opt_scope || "sub";
157 my $port = $opt_port;
160 "LDAP_SERVER_NOTIFICATION_OID" => "1.2.840.113556.1.4.528",
161 "LDAP_SERVER_EXTENDED_DN_OID" => "1.2.840.113556.1.4.529",
162 "LDAP_PAGED_RESULT_OID_STRING" => "1.2.840.113556.1.4.319",
163 "LDAP_SERVER_SD_FLAGS_OID" => "1.2.840.113556.1.4.801",
164 "LDAP_SERVER_SORT_OID" => "1.2.840.113556.1.4.473",
165 "LDAP_SERVER_RESP_SORT_OID" => "1.2.840.113556.1.4.474",
166 "LDAP_CONTROL_VLVREQUEST" => "2.16.840.1.113730.3.4.9",
167 "LDAP_CONTROL_VLVRESPONSE" => "2.16.840.1.113730.3.4.10",
168 "LDAP_SERVER_RANGE_RETRIEVAL" => "1.2.840.113556.1.4.802", #unsure
169 "LDAP_SERVER_SHOW_DELETED_OID" => "1.2.840.113556.1.4.417",
170 "LDAP_SERVER_CROSSDOM_MOVE_TARGET_OID" => "1.2.840.113556.1.4.521",
171 "LDAP_SERVER_LAZY_COMMIT_OID" => "1.2.840.113556.1.4.619",
172 "LDAP_SERVER_TREE_DELETE_OID" => "1.2.840.113556.1.4.805",
173 "LDAP_SERVER_DIRSYNC_OID" => "1.2.840.113556.1.4.841",
174 "LDAP_SERVER_VERIFY_NAME_OID" => "1.2.840.113556.1.4.1338",
175 "LDAP_SERVER_DOMAIN_SCOPE_OID" => "1.2.840.113556.1.4.1339",
176 "LDAP_SERVER_SEARCH_OPTIONS_OID" => "1.2.840.113556.1.4.1340",
177 "LDAP_SERVER_PERMISSIVE_MODIFY_OID" => "1.2.840.113556.1.4.1413",
178 "LDAP_SERVER_ASQ_OID" => "1.2.840.113556.1.4.1504",
179 "NONE (Get stats control)" => "1.2.840.113556.1.4.970",
180 "LDAP_SERVER_QUOTA_CONTROL_OID" => "1.2.840.113556.1.4.1852",
181 "LDAP_SERVER_SHUTDOWN_NOTIFY_OID" => "1.2.840.113556.1.4.1907",
184 my %ads_capabilities = (
185 "LDAP_CAP_ACTIVE_DIRECTORY_OID" => "1.2.840.113556.1.4.800",
186 "LDAP_CAP_ACTIVE_DIRECTORY_V51_OID" => "1.2.840.113556.1.4.1670",
187 "LDAP_CAP_ACTIVE_DIRECTORY_LDAP_INTEG_OID" => "1.2.840.113556.1.4.1791",
190 my %ads_extensions = (
191 "LDAP_START_TLS_OID" => "1.3.6.1.4.1.1466.20037",
192 "LDAP_TTL_EXTENDED_OP_OID" => "1.3.6.1.4.1.1466.101.119.1",
193 "LDAP_SERVER_FAST_BIND_OID" => "1.2.840.113556.1.4.1781",
194 "NONE (TTL refresh extended op)" => "1.3.6.1.4.1.1466.101.119.1",
197 my %ads_matching_rules = (
198 "LDAP_MATCHING_RULE_BIT_AND" => "1.2.840.113556.1.4.803",
199 "LDAP_MATCHING_RULE_BIT_OR" => "1.2.840.113556.1.4.804",
203 "WELL_KNOWN_GUID_COMPUTERS" => "AA312825768811D1ADED00C04FD8D5CD",
204 "WELL_KNOWN_GUID_DOMAIN_CONTROLLERS" => "A361B2FFFFD211D1AA4B00C04FD7D83A",
205 "WELL_KNOWN_GUID_SYSTEM" => "AB1D30F3768811D1ADED00C04FD8D5CD",
206 "WELL_KNOWN_GUID_USERS" => "A9D1CA15768811D1ADED00C04FD8D5CD",
209 my %ads_systemflags = (
210 "FLAG_DONT_REPLICATE" => 0x00000001, # 1
211 "FLAG_REPLICATE_TO_GC" => 0x00000002, # 2
212 "FLAG_ATTRIBUTE_CONSTRUCT" => 0x00000004, # 4
213 "FLAG_CATEGORY_1_OBJECT" => 0x00000010, # 16
214 "FLAG_DELETE_WITHOUT_TOMBSTONE" => 0x02000000, # 33554432
215 "FLAG_DOMAIN_DISALLOW_MOVE" => 0x04000000, # 67108864
216 "FLAG_DOMAIN_DISALLOW_RENAME" => 0x08000000, # 134217728
217 #"FLAG_CONFIG_CAN_MOVE_RESTRICTED" => 0x10000000, # 268435456 # only setable on creation
218 #"FLAG_CONFIG_CAN_MOVE" => 0x20000000, # 536870912 # only setable on creation
219 #"FLAG_CONFIG_CAN_RENAME" => 0x40000000, # 1073741824 # only setable on creation
220 "FLAG_DISALLOW_DELETE" => 0x80000000, # 2147483648
223 my %ads_mixed_domain = (
224 "NATIVE_LEVEL_DOMAIN" => 0,
225 "MIXED_LEVEL_DOMAIN" => 1,
229 "DS_BEHAVIOR_WIN2000" => 0, # untested
230 "DS_BEHAVIOR_WIN2003" => 2,
233 my %ads_instance_type = (
234 "DS_INSTANCETYPE_IS_NC_HEAD" => 0x1,
236 "IT_NC_ABOVE" => 0x8,
240 "ACCOUNT_NEVER_EXPIRES" => 0x000000, # 0
241 "ACCOUNT_OK" => 0x800000, # 8388608
242 "ACCOUNT_LOCKED_OUT" => 0x800010, # 8388624
245 my %ads_gpoptions = (
246 "GPOPTIONS_INHERIT" => 0,
247 "GPOPTIONS_BLOCK_INHERITANCE" => 1,
250 my %ads_gplink_opts = (
251 "GPLINK_OPT_NONE" => 0,
252 "GPLINK_OPT_DISABLED" => 1,
253 "GPLINK_OPT_ENFORCED" => 2,
257 "GPFLAGS_ALL_ENABLED" => 0,
258 "GPFLAGS_USER_SETTINGS_DISABLED" => 1,
259 "GPFLAGS_MACHINE_SETTINGS_DISABLED" => 2,
260 "GPFLAGS_ALL_DISABLED" => 3,
263 my %ads_serverstate = (
264 "SERVER_ENABLED" => 1,
265 "SERVER_DISABLED" => 2,
268 my %ads_sdeffective = (
269 "OWNER_SECURITY_INFORMATION" => 0x01,
270 "DACL_SECURITY_INFORMATION" => 0x04,
271 "SACL_SECURITY_INFORMATION" => 0x10,
274 my %ads_trustattrs = (
275 "TRUST_ATTRIBUTE_NON_TRANSITIVE" => 1,
276 "TRUST_ATTRIBUTE_TREE_PARENT" => 2,
277 "TRUST_ATTRIBUTE_TREE_ROOT" => 3,
278 "TRUST_ATTRIBUTE_UPLEVEL_ONLY" => 4,
281 my %ads_trustdirection = (
282 "TRUST_DIRECTION_INBOUND" => 1,
283 "TRUST_DIRECTION_OUTBOUND" => 2,
284 "TRUST_DIRECTION_BIDIRECTIONAL" => 3,
287 my %ads_trusttype = (
288 "TRUST_TYPE_DOWNLEVEL" => 1,
289 "TRUST_TYPE_UPLEVEL" => 2,
290 "TRUST_TYPE_KERBEROS" => 3,
291 "TRUST_TYPE_DCE" => 4,
294 my %ads_pwdproperties = (
295 "DOMAIN_PASSWORD_COMPLEX" => 1,
296 "DOMAIN_PASSWORD_NO_ANON_CHANGE" => 2,
297 "DOMAIN_PASSWORD_NO_CLEAR_CHANGE" => 4,
298 "DOMAIN_LOCKOUT_ADMINS" => 8,
299 "DOMAIN_PASSWORD_STORE_CLEARTEXT" => 16,
300 "DOMAIN_REFUSE_PASSWORD_CHANGE" => 32,
303 my %ads_uascompat = (
304 "LANMAN_USER_ACCOUNT_SYSTEM_NOLIMITS" => 0,
305 "LANMAN_USER_ACCOUNT_SYSTEM_COMPAT" => 1,
309 "REPLICA_SET_SYSVOL" => 2, # unsure
310 "REPLICA_SET_DFS" => 1, # unsure
311 "REPLICA_SET_OTHER" => 0, # unsure
314 my %ads_gp_cse_extensions = (
315 "Administrative Templates Extension" => "35378EAC-683F-11D2-A89A-00C04FBBCFA2",
316 "Disk Quotas" => "3610EDA5-77EF-11D2-8DC5-00C04FA31A66",
317 "EFS Recovery" => "B1BE8D72-6EAC-11D2-A4EA-00C04F79F83A",
318 "Folder Redirection" => "25537BA6-77A8-11D2-9B6C-0000F8080861",
319 "IP Security" => "E437BC1C-AA7D-11D2-A382-00C04F991E27",
320 "Internet Explorer Maintenance" => "A2E30F80-D7DE-11d2-BBDE-00C04F86AE3B",
321 "QoS Packet Scheduler" => "426031c0-0b47-4852-b0ca-ac3d37bfcb39",
322 "Scripts" => "42B5FAAE-6536-11D2-AE5A-0000F87571E3",
323 "Security" => "827D319E-6EAC-11D2-A4EA-00C04F79F83A",
324 "Software Installation" => "C6DC5466-785A-11D2-84D0-00C04FB169F7",
328 my %ads_gpcextensions = (
329 "Administrative Templates" => "0F6B957D-509E-11D1-A7CC-0000F87571E3",
330 "Certificates" => "53D6AB1D-2488-11D1-A28C-00C04FB94F17",
331 "EFS recovery policy processing" => "B1BE8D72-6EAC-11D2-A4EA-00C04F79F83A",
332 "Folder Redirection policy processing" => "25537BA6-77A8-11D2-9B6C-0000F8080861",
333 "Folder Redirection" => "88E729D6-BDC1-11D1-BD2A-00C04FB9603F",
334 "Registry policy processing" => "35378EAC-683F-11D2-A89A-00C04FBBCFA2",
335 "Remote Installation Services" => "3060E8CE-7020-11D2-842D-00C04FA372D4",
336 "Security Settings" => "803E14A0-B4FB-11D0-A0D0-00A0C90F574B",
337 "Security policy processing" => "827D319E-6EAC-11D2-A4EA-00C04F79F83A",
338 "unknown" => "3060E8D0-7020-11D2-842D-00C04FA372D4",
339 "unknown2" => "53D6AB1B-2488-11D1-A28C-00C04FB94F17",
342 my %ads_gpo_default_guids = (
343 "Default Domain Policy" => "31B2F340-016D-11D2-945F-00C04FB984F9",
344 "Default Domain Controllers Policy" => "6AC1786C-016F-11D2-945F-00C04fB984F9",
345 "mist" => "61718096-3D3F-4398-8318-203A48976F9E",
349 "UF_SCRIPT" => 0x00000001,
350 "UF_ACCOUNTDISABLE" => 0x00000002,
351 # "UF_UNUSED_1" => 0x00000004,
352 "UF_HOMEDIR_REQUIRED" => 0x00000008,
353 "UF_LOCKOUT" => 0x00000010,
354 "UF_PASSWD_NOTREQD" => 0x00000020,
355 "UF_PASSWD_CANT_CHANGE" => 0x00000040,
356 "UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED" => 0x00000080,
357 "UF_TEMP_DUPLICATE_ACCOUNT" => 0x00000100,
358 "UF_NORMAL_ACCOUNT" => 0x00000200,
359 # "UF_UNUSED_2" => 0x00000400,
360 "UF_INTERDOMAIN_TRUST_ACCOUNT" => 0x00000800,
361 "UF_WORKSTATION_TRUST_ACCOUNT" => 0x00001000,
362 "UF_SERVER_TRUST_ACCOUNT" => 0x00002000,
363 # "UF_UNUSED_3" => 0x00004000,
364 # "UF_UNUSED_4" => 0x00008000,
365 "UF_DONT_EXPIRE_PASSWD" => 0x00010000,
366 "UF_MNS_LOGON_ACCOUNT" => 0x00020000,
367 "UF_SMARTCARD_REQUIRED" => 0x00040000,
368 "UF_TRUSTED_FOR_DELEGATION" => 0x00080000,
369 "UF_NOT_DELEGATED" => 0x00100000,
370 "UF_USE_DES_KEY_ONLY" => 0x00200000,
371 "UF_DONT_REQUIRE_PREAUTH" => 0x00400000,
372 "UF_PASSWORD_EXPIRED" => 0x00800000,
373 "UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION" => 0x01000000,
374 "UF_NO_AUTH_DATA_REQUIRED" => 0x02000000,
375 # "UF_UNUSED_8" => 0x04000000,
376 # "UF_UNUSED_9" => 0x08000000,
377 # "UF_UNUSED_10" => 0x10000000,
378 # "UF_UNUSED_11" => 0x20000000,
379 # "UF_UNUSED_12" => 0x40000000,
380 # "UF_UNUSED_13" => 0x80000000,
383 my %ads_grouptype = (
384 "GROUP_TYPE_BUILTIN_LOCAL_GROUP" => 0x00000001,
385 "GROUP_TYPE_ACCOUNT_GROUP" => 0x00000002,
386 "GROUP_TYPE_RESOURCE_GROUP" => 0x00000004,
387 "GROUP_TYPE_UNIVERSAL_GROUP" => 0x00000008,
388 "GROUP_TYPE_APP_BASIC_GROUP" => 0x00000010,
389 "GROUP_TYPE_APP_QUERY_GROUP" => 0x00000020,
390 "GROUP_TYPE_SECURITY_ENABLED" => 0x80000000,
394 "ATYPE_NORMAL_ACCOUNT" => 0x30000000,
395 "ATYPE_WORKSTATION_TRUST" => 0x30000001,
396 "ATYPE_INTERDOMAIN_TRUST" => 0x30000002,
397 "ATYPE_SECURITY_GLOBAL_GROUP" => 0x10000000,
398 "ATYPE_DISTRIBUTION_GLOBAL_GROUP" => 0x10000001,
399 "ATYPE_DISTRIBUTION_UNIVERSAL_GROUP" => 0x10000001, # ATYPE_DISTRIBUTION_GLOBAL_GROUP
400 "ATYPE_SECURITY_LOCAL_GROUP" => 0x20000000,
401 "ATYPE_DISTRIBUTION_LOCAL_GROUP" => 0x20000001,
402 "ATYPE_ACCOUNT" => 0x30000000, # ATYPE_NORMAL_ACCOUNT
403 "ATYPE_GLOBAL_GROUP" => 0x10000000, # ATYPE_SECURITY_GLOBAL_GROUP
404 "ATYPE_LOCAL_GROUP" => 0x20000000, # ATYPE_SECURITY_LOCAL_GROUP
408 "GTYPE_SECURITY_BUILTIN_LOCAL_GROUP" => 0x80000005,
409 "GTYPE_SECURITY_DOMAIN_LOCAL_GROUP" => 0x80000004,
410 "GTYPE_SECURITY_GLOBAL_GROUP" => 0x80000002,
411 "GTYPE_SECURITY_UNIVERSAL_GROUP" => 0x80000008,
412 "GTYPE_DISTRIBUTION_GLOBAL_GROUP" => 0x00000002,
413 "GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP" => 0x00000004,
414 "GTYPE_DISTRIBUTION_UNIVERSAL_GROUP" => 0x00000008,
418 "CtxCfgPresent" => \&dump_int,
419 "CtxCfgFlags1" => \&dump_int,
420 "CtxCallback" => \&dump_string,
421 "CtxShadow" => \&dump_string,
422 "CtxMaxConnectionTime" => \&dump_int,
423 "CtxMaxDisconnectionTime"=> \&dump_int,
424 "CtxMaxIdleTime" => \&dump_int,
425 "CtxKeyboardLayout" => \&dump_int,
426 "CtxMinEncryptionLevel" => \&dump_int,
427 "CtxWorkDirectory" => \&dump_string,
428 "CtxNWLogonServer" => \&dump_string,
429 "CtxWFHomeDir" => \&dump_string,
430 "CtxWFHomeDirDrive" => \&dump_string,
431 "CtxWFProfilePath" => \&dump_string,
432 "CtxInitialProgram" => \&dump_string,
433 "CtxCallbackNumber" => \&dump_string,
436 $SIG{__WARN__} = sub {
441 # if there is data missing, we try to autodetect with samba-tools (if installed)
442 # this might fill up workgroup, machine, realm
446 $workgroup = $workgroup || $opt_workgroup || "";
449 $server = process_servername($opt_host) ||
450 detect_server($workgroup,$opt_realm) ||
451 die "please define server to query with -h host\n";
455 $base = defined($opt_base)? $opt_base : "" ||
456 get_base_from_rootdse($server,$dse);
459 $realm = $opt_realm ||
460 get_realm_from_rootdse($server,$dse);
463 my @sasl_mechs = get_sasl_mechs_from_rootdse($server,$dse);
464 my $sasl_mech = "GSSAPI";
466 $sasl_mech = sprintf("%s", (check_sasl_mech($opt_saslmech) == 0)?uc($opt_saslmech):"");
470 my $sasl_bind = 1 if (!$opt_simpleauth);
473 my $user = check_user($opt_user) || $ENV{'USER'} || "";
476 my $upn = sprintf("%s", gen_upn($opt_machine ? "$machine\$" : $user, $realm));
479 $binddn = $opt_binddn || $upn;
482 $password = $password || $opt_password;
484 $password = $opt_machine ? get_machine_password($workgroup) : get_password();
488 "Token-Groups-No-GC-Acceptable" => \&dump_sid, #wrong name
489 "accountExpires" => \&dump_nttime,
490 "badPasswordTime" => \&dump_nttime,
491 "creationTime" => \&dump_nttime,
492 "currentTime" => \&dump_timestr,
493 "domainControllerFunctionality" => \&dump_ds_func,
494 "domainFunctionality" => \&dump_ds_func,
495 "fRSReplicaSetGUID" => \&dump_guid,
496 "fRSReplicaSetType" => \&dump_frstype,
497 "fRSVersionGUID" => \&dump_guid,
498 "flags" => \&dump_gpflags, # fixme: possibly only on gpos!
499 "forceLogoff" => \&dump_nttime_abs,
500 "forestFunctionality" => \&dump_ds_func,
501 # "gPCMachineExtensionNames" => \&dump_gpcextensions,
502 # "gPCUserExtensionNames" => \&dump_gpcextensions,
503 "gPLink" => \&dump_gplink,
504 "gPOptions" => \&dump_gpoptions,
505 "groupType" => \&dump_gtype,
506 "instanceType" => \&dump_instance_type,
507 "lastLogon" => \&dump_nttime,
508 "lastLogonTimestamp" => \&dump_nttime,
509 "lockOutObservationWindow" => \&dump_nttime_abs,
510 "lockoutDuration" => \&dump_nttime_abs,
511 "lockoutTime" => \&dump_nttime,
512 # "logonHours" => \&dump_logonhours,
513 "maxPwdAge" => \&dump_nttime_abs,
514 "minPwdAge" => \&dump_nttime_abs,
515 "modifyTimeStamp" => \&dump_timestr,
516 "msDS-Behavior-Version" => \&dump_ds_func, #unsure
517 "msDS-User-Account-Control-Computed" => \&dump_uacc,
518 "mS-DS-CreatorSID" => \&dump_sid,
519 # "msRADIUSFramedIPAddress" => \&dump_ipaddr,
520 # "msRASSavedFramedIPAddress" => \&dump_ipaddr,
521 "netbootGUID" => \&dump_guid,
522 "nTMixedDomain" => \&dump_mixed_domain,
523 "nTSecurityDescriptor" => \&dump_secdesc,
524 "objectGUID" => \&dump_guid,
525 "objectSid" => \&dump_sid,
527 "pKTGuid" => \&dump_guid,
528 "pwdLastSet" => \&dump_nttime,
529 "pwdProperties" => \&dump_pwdproperties,
530 "sAMAccountType" => \&dump_atype,
531 "sDRightsEffective" => \&dump_sdeffective,
532 "securityIdentifier" => \&dump_sid,
533 "serverState" => \&dump_serverstate,
534 "supportedCapabilities", => \&dump_capabilities,
535 "supportedControl", => \&dump_controls,
536 "supportedExtension", => \&dump_extension,
537 "systemFlags" => \&dump_systemflags,
538 "tokenGroups", => \&dump_sid,
539 "tokenGroupsGlobalAndUniversal" => \&dump_sid,
540 "tokenGroupsNoGCAcceptable" => \&dump_sid,
541 "trustAttributes" => \&dump_trustattr,
542 "trustDirection" => \&dump_trustdirection,
543 "trustType" => \&dump_trusttype,
544 "uASCompat" => \&dump_uascompat,
545 "userAccountControl" => \&dump_uac,
546 "userCertificate" => \&dump_cert,
547 "userFlags" => \&dump_uf,
548 "userParameters" => \&dump_munged_dial,
549 "whenChanged" => \&dump_timestr,
550 "whenCreated" => \&dump_timestr,
551 # "dSCorePropagationData" => \&dump_timestr,
561 print "usage: $0 [--asq] [--base|-b base] [--debug level] [--debug level] [--DN|-D binddn] [--extendeddn|-e] [--help] [--host|-h host] [--machine|-P] [--metadata|-m] [--nodiffs] [--notify|-n] [--password|-w password] [--port port] [--rawdisplay] [--realm|-R realm] [--rootdse] [--saslmech|-Y saslmech] [--schema|-c] [--scope|-s scope] [--simpleauth|-x] [--starttls|-Z] [--user|-U user] [--wknguid] [--workgroup|-k workgroup] filter [attrs]\n";
562 print "\t--asq [attribute]\n\t\tAttribute to use for a attribute scoped query (LDAP_SERVER_ASQ_OID)\n";
563 print "\t--base|-b [base]\n\t\tUse base [base]\n";
564 print "\t--debug [level]\n\t\tUse debuglevel (for Net::LDAP)\n";
565 print "\t--domain_scope\n\t\tLimit LDAP search to local domain (LDAP_SERVER_DOMAIN_SCOPE_OID)\n";
566 print "\t--DN|-D [binddn]\n\t\tUse binddn or principal\n";
567 print "\t--extendeddn|-e [value]\n\t\tDisplay extended dn (LDAP_SERVER_EXTENDED_DN_OID)\n";
568 print "\t--fastbind\n\t\tDo LDAP fast bind using LDAP_SERVER_FAST_BIND_OID extension\n";
569 print "\t--help\n\t\tDisplay help page\n";
570 print "\t--host|-h [host]\n\t\tQuery Host [host] (either a hostname or an LDAP uri)\n";
571 print "\t--machine|-P\n\t\tUse samba3 machine account stored in $secrets_tdb (needs root access)\n";
572 print "\t--metdata|-m\n\t\tDisplay replication metadata\n";
573 print "\t--nodiffs\n\t\tDisplay no diffs but full entry dump when running in notify mode\n";
574 print "\t--notify|-n\n\t\tActivate asynchronous change notification (LDAP_SERVER_NOTIFICATION_OID)\n";
575 print "\t--paging [pagesize]\n\t\tUse paged results when searching\n";
576 print "\t--password|-w [password]\n\t\tUse [password] for binddn\n";
577 print "\t--port [port]\n\t\tUse [port] when connecting ADS\n";
578 print "\t--rawdisplay\n\t\tDo not interpret values\n";
579 print "\t--realm|-R [realm]\n\t\tUse [realm] when trying to construct bind-principal\n";
580 print "\t--rootdse\n\t\tDisplay RootDSE (anonymously)\n";
581 print "\t--saslmech|-Y [saslmech]\n\t\tUse SASL Mechanism [saslmech] when binding\n";
582 print "\t--schema|-c\n\t\tDisplay DSE-Schema\n";
583 print "\t--scope|-s [scope]\n\t\tUse scope [scope] (sub, base, one)\n";
584 print "\t--simpleauth|-x\n\t\tUse simple bind (otherwise SASL binds are performed)\n";
585 print "\t--starttls|-Z\n\t\tUse Start TLS extended operation to secure LDAP traffic\n";
586 print "\t--user|-U [user]\n\t\tUse [user]\n";
587 print "\t--wknguid\n\t\tDisplay well known guids\n";
588 print "\t--workgroup|-k [workgroup]\n\t\tWhen LDAP-Server is not known try to find a Domain-Controller for [workgroup]\n";
592 my ($mod,$attr,$value) = @_;
593 my $ofh = select(STDOUT);
599 @<<<< @>>>>>>>>>>>>>>>>>>>>>>>: @*
605 my ($mod,$attr,$value) = @_;
606 my $ofh = select(STDOUT);
612 @<<<< @>>>>>>>>>>>>>>>>>>>>>>>: ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
614 ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
621 my $workgroup = shift;
626 # try net cache (nbt records)
627 if ( -x $net && $workgroup ) {
628 my $key = sprintf("NBT/%s#1C", uc($workgroup));
629 chomp($result = `$net cache search $key 2>&1 /dev/null`);
630 $result =~ s/^.*Value: //;
632 return $result if $result;
633 printf("%10s query failed for [%s]\n", "net cache", $key);
636 # try dns SRV entries
637 if ( -x $dig && $realm ) {
638 my $key = sprintf("_ldap._tcp.%s", lc($realm));
639 chomp($result = `$dig $key SRV +short +search | egrep "^[0-9]" | head -1`);
642 return $result if $result;
643 printf("%10s query failed for [%s]\n", "dns", $key);
646 # try netbios broadcast query
647 if ( -x $nmblookup && $workgroup ) {
648 my $key = sprintf("%s#1C", uc($workgroup));
649 my $pattern = sprintf("%s<1c>", uc($workgroup));
650 chomp($result = `$nmblookup $key -d 0 | grep '$pattern'`);
652 return $result if $result;
653 printf("%10s query failed for [%s]\n", "nmblookup", $key);
661 if (! -x $testparm) { return -1; }
664 open(TESTPARM, "$testparm -s -v 2> /dev/null |");
665 while (my $line = <TESTPARM>) {
667 if ($line =~ /netbios name/) {
668 ($tmp, $machine) = split(/=/, $line);
669 $machine =~ s/\s+|\t+//g;
671 if ($line =~ /realm/) {
672 ($tmp, $realm) = split(/=/, $line);
673 $realm =~ s/\s+|\t+//g;
675 if ($line =~ /workgroup/) {
676 ($tmp, $workgroup) = split(/=/, $line);
677 $workgroup =~ s/\s+|\t+//g;
687 if ($machine && $realm) {
688 return sprintf("%s\@%s", lc($machine), uc($realm));
694 if (!$password && $opt_simpleauth && check_ticket($user)) {
695 return prompt_password($user);
701 my $user = shift || prompt_user();
705 sub get_machine_password {
707 my $workgroup = shift || "";
708 $workgroup = uc($workgroup);
711 -x $tdbdump || die "tdbdump is not installed. cannot proceed autodetection\n";
712 -r $secrets_tdb || die "cannot read $secrets_tdb. cannot proceed autodetection\n";
714 # get machine-password
715 my $key = sprintf("SECRETS/MACHINE_PASSWORD/%s", $workgroup);
716 open(SECRETS,"$tdbdump $secrets_tdb |");
717 while(my $line = <SECRETS>) {
721 ($line,$password) = split(/"/, $line);
724 if ($line =~ /$key/) {
731 print "Successfully autodetected machine password for workgroup: $workgroup\n";
734 warn "No machine password available for $workgroup\n";
739 sub prompt_password {
741 my $acct = shift || "";
743 ($acct, $password) = split(/\%/, $acct);
747 print "Enter password for $acct:";
748 my $password = <STDIN>;
757 print "Enter Username:";
766 # works only for heimdal
767 return system("$klist -t");
772 my $KRB5_CONFIG = "/tmp/.krb5.conf.telads-$<";
774 open(KRB5CONF, "> $KRB5_CONFIG") || die "cannot write $KRB5_CONFIG";
775 printf KRB5CONF "# autogenerated by $0\n";
776 printf KRB5CONF "[libdefaults]\n\tdefault_realm = %s\n\tclockskew = %d\n", uc($realm), 60*60;
777 printf KRB5CONF "[realms]\n\t%s = {\n\t\tkdc = %s\n\t}\n", uc($realm), $server;
780 if ( system("KRB5_CONFIG=$KRB5_CONFIG $kinit $user") != 0) {
788 my $acct = shift || "";
790 ($acct, $password) = split(/\%/, $acct);
795 sub check_root_dse($$$@) {
798 my $server = shift || "";
799 $dse = shift || get_dse($server) || return -1;
800 my $attr = shift || die "unknown query";
803 my $dse_list = $dse->get_value($attr, asref => '1');
804 my @dse_array = @$dse_list;
806 foreach my $i (@array) {
807 # we could use -> supported_control but this
808 # is only available in newer versions of perl-ldap
809 # if ( ! $dse->supported_control( $i ) ) {
810 if ( grep(/$i->type()/, @dse_array) ) {
811 printf("required \"$attr\": %s is not supported by ADS-server.\n", $i->type());
818 sub check_ctrls ($$@) {
822 return check_root_dse($server, $dse, "supportedControl", @array);
825 sub check_exts ($$@) {
829 return check_root_dse($server, $dse, "supportedExtension", @array);
832 sub get_base_from_rootdse {
834 my $server = shift || "";
835 $dse = shift || get_dse($server,$async_ldap_hd) || return -1;
836 return $dse->get_value('defaultNamingContext');
839 sub get_realm_from_rootdse {
841 my $server = shift || "";
842 $dse = shift || get_dse($server,$async_ldap_hd) || return -1;
843 my $service = $dse->get_value('ldapServiceName') || "";
845 my ($t,$realm) = split(/\@/, $service);
848 die "very odd: could not get realm";
852 sub get_sasl_mechs_from_rootdse {
854 my $server = shift || "";
855 $dse = shift || get_dse($server,$async_ldap_hd) || return -1;
856 my $mechs = $dse->get_value('supportedSASLMechanisms', asref => 1);
862 my $server = shift || return undef;
863 $async_ldap_hd = shift || get_ldap_hd($server,1);
864 if (!$async_ldap_hd) {
865 print "oh, no connection\n";
868 my $mesg = $async_ldap_hd->bind() || die "cannot bind\n";
869 if ($mesg->code) { die "failed to bind\n"; };
870 my $dse = $async_ldap_hd->root_dse( attrs => ['*', "supportedExtension", "supportedFeatures" ] );
875 sub process_servername {
877 my $name = shift || return "";
878 if ($name =~ /^ldaps:\/\//i ) {
879 $name =~ s#^ldaps://##i;
880 $uri = sprintf("%s://%s", "ldaps", $name);
882 $name =~ s#^ldap://##i;
883 $uri = sprintf("%s://%s", "ldap", $name);
890 my $server = shift || return undef;
891 my $async = shift || "0";
893 die "uri unavailable" if (!$uri);
894 if ($uri =~ /^ldaps:\/\//i ) {
895 $port = $port || 636;
897 $hd = Net::LDAPS->new( $server, async => $async, port => $port ) ||
898 die "host $server not available: $!";
900 $port = $port || 389;
901 $hd = Net::LDAP->new( $server, async => $async, port => $port ) ||
902 die "host $server not available: $!";
904 $hd->debug($opt_debug);
906 $hd->start_tls( verify => 'none' );
915 print "no sasl support\n";
920 if ($sasl_mech && $sasl_mech eq "GSSAPI") {
921 my $user = sprintf("%s\@%s", $user, uc($realm));
922 if (check_ticket($user) != 0 && get_ticket($user) != 0) {
923 print "Could not get Kerberos ticket for user [$user]\n";
927 $hd = Authen::SASL->new( mechanism => 'GSSAPI' ) || die "nope";
928 my $conn = $hd->client_new("ldap", $server);
930 if ($conn->code == -1) {
931 printf "%s\n", $conn->error();
935 } elsif ($sasl_mech) {
937 # here comes generic sasl code
938 $hd = Authen::SASL->new( mechanism => $sasl_mech,
941 pass => \&get_password
946 print "no supported SASL mechanism found (@sasl_mechs).\n";
947 print "falling back to simple bind.\n";
954 sub check_sasl_mech {
955 my $mech_check = shift;
957 foreach my $mech (@sasl_mechs) {
958 $have_mech = 1 if ($mech eq uc($mech_check));
966 sub store_result ($) {
970 $entry_store{$entry->dn} = $entry;
973 sub display_result_diff ($) {
975 my $entry_new = shift;
976 return if ( !$entry_new);
978 if ( !exists $entry_store{$entry_new->dn}) {
979 print "can't display any differences yet. full dump...\n";
980 display_entry_generic($entry_new);
984 my $entry_old = $entry_store{$entry_new->dn};
986 foreach my $attr (sort $entry_new->attributes) {
987 if ( $entry_new->exists($attr) && ! $entry_old->exists($attr)) {
988 display_attr_generic("add:\t", $entry_new, $attr);
993 foreach my $attr (sort $entry_old->attributes) {
994 if (! $entry_new->exists($attr) && $entry_old->exists($attr)) {
995 display_attr_generic("del:\t", $entry_old, $attr);
999 # now check for all values if they have changed, display changes
1000 my ($old_vals, $new_vals, @old_vals, @new_vals, %old, %new);
1002 $old_vals = $entry_old->get_value($attr, asref => 1);
1003 @old_vals = @$old_vals;
1004 $new_vals = $entry_new->get_value($attr, asref => 1);
1005 @new_vals = @$new_vals;
1007 if (scalar(@old_vals) == 1 && scalar(@new_vals) == 1) {
1008 if ($old_vals[0] ne $new_vals[0]) {
1009 display_attr_generic("old:\t", $entry_old, $attr);
1010 display_attr_generic("new:\t", $entry_new, $attr);
1015 # handle multivalued diffs
1016 foreach my $val (@old_vals) { $old{$val} = "dummy"; };
1017 foreach my $val (@new_vals) { $new{$val} = "dummy"; };
1018 foreach my $val (sort keys %new) {
1019 if (!exists $old{$val}) {
1020 display_value_generic("add:\t", $attr, $val);
1023 foreach my $val (sort keys %old) {
1024 if (!exists $new{$val}) {
1025 display_value_generic("del:\t", $attr, $val);
1033 sub sid2string ($) {
1035 # Fix from Michael James <michael@james.st>
1036 my $binary_sid = shift;
1037 my($sid_rev, $num_auths, $id1, $id2, @ids) = unpack("H2 H2 n N V*", $binary_sid);
1038 my $sid_string = join("-", "S", hex($sid_rev), ($id1<<32)+$id2, @ids);
1043 sub string_to_guid {
1045 return undef unless $string =~ /([0-9,a-z]{8})-([0-9,a-z]{4})-([0-9,a-z]{4})-([0-9,a-z]{2})([0-9,a-z]{2})-([0-9,a-z]{2})([0-9,a-z]{2})([0-9,a-z]{2})([0-9,a-z]{2})([0-9,a-z]{2})([0-9,a-z]{2})/i;
1047 return pack("I", hex $1) .
1056 pack("C", hex $10) .
1059 # print "$1\n$2\n$3\n$4\n$5\n$6\n$7\n$8\n$9\n$10\n$11\n";
1062 sub bindstring_to_guid {
1064 return pack("H2 H2 H2 H2 H2 H2 H2 H2 H2 H2 H2 H2 H2 H2 H2 H2",
1083 sub guid_to_string {
1085 my $string = sprintf "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
1087 unpack("S", substr($guid, 4, 2)),
1088 unpack("S", substr($guid, 6, 2)),
1089 unpack("C", substr($guid, 8, 1)),
1090 unpack("C", substr($guid, 9, 1)),
1091 unpack("C", substr($guid, 10, 1)),
1092 unpack("C", substr($guid, 11, 1)),
1093 unpack("C", substr($guid, 12, 1)),
1094 unpack("C", substr($guid, 13, 1)),
1095 unpack("C", substr($guid, 14, 1)),
1096 unpack("C", substr($guid, 15, 1));
1100 sub guid_to_bindstring {
1102 return unpack("H" . 2 * length($guid), $guid),
1106 print "Dumping Well known GUIDs:\n";
1107 foreach my $wknguid (keys %wknguids) {
1109 my $guid = bindstring_to_guid($wknguids{$wknguid});
1110 my $str = guid_to_string($guid);
1111 my $back = guid_to_bindstring($guid);
1113 printf "wkguid: %s\n", $wknguid;
1114 printf "bind_string format: %s\n", $wknguids{$wknguid};
1115 printf "string format: %s\n", guid_to_string($guid);
1116 printf "back to bind_string:%s\n", guid_to_bindstring($guid);
1118 printf "use base: \"<WKGUID=%s,%s>\"\n", guid_to_bindstring($guid), $base;
1122 sub gen_bitmask_string_format($%) {
1126 foreach my $key (sort keys %tmp) {
1127 push(@list, sprintf("%s %s", $tmp{$key}, $key));
1129 return join("\n$mod$tabsize",@list);
1133 sub nt_to_unixtime ($) {
1134 # the number of 100 nanosecond intervals since jan. 1. 1601 (utc)
1136 $t64 =~ s/(.+).{7,7}/$1/;
1137 $t64 -= 11644473600;
1143 my $mod = shift || die "no mod";
1147 foreach my $key (keys %header) {
1148 if ($header{$key} eq $val) {
1149 $tmp{"($val)"} = $key;
1154 if (!$found) { $tmp{$val} = ""; };
1155 return gen_bitmask_string_format($mod,%tmp);
1159 my $op = shift || die "no op";
1161 my $mod = shift || die "no mod";
1164 $tmp{""} = sprintf("%s (0x%08x)", $val, $val);
1165 foreach my $key (sort keys %header) { # sort by val !
1166 my $val_hex = sprintf("0x%08x", $header{$key});
1168 $tmp{"$key ($val_hex)"} = ( $val & $header{$key} ) ? $set:$unset;
1169 } elsif ($op eq "==") {
1170 $tmp{"$key ($val_hex)"} = ( $val == $header{$key} ) ? $set:$unset;
1172 print "unknown operator: $op\n";
1176 return gen_bitmask_string_format($mod,%tmp);
1179 sub dump_bitmask_and {
1180 return dump_bitmask("&",@_);
1183 sub dump_bitmask_equal {
1184 return dump_bitmask("==",@_);
1188 return dump_bitmask_and(@_,%ads_uf); # ads_uf ?
1191 sub dump_uascompat {
1192 return dump_bitmask_equal(@_,%ads_uascompat);
1195 sub dump_gpoptions {
1196 return dump_bitmask_equal(@_,%ads_gpoptions);
1200 return dump_bitmask_equal(@_,%ads_gpflags);
1204 return dump_bitmask_equal(@_,%ads_uacc);
1208 return dump_bitmask_and(@_,%ads_uf);
1212 my $ret = dump_bitmask_and(@_,%ads_grouptype);
1213 $ret .= "\n$tabsize\t";
1214 $ret .= dump_bitmask_equal(@_,%ads_gtype);
1219 return dump_bitmask_equal(@_,%ads_atype);
1223 return dump_equal(@_,%ads_controls);
1226 sub dump_capabilities {
1227 return dump_equal(@_,%ads_capabilities);
1230 sub dump_extension {
1231 return dump_equal(@_,%ads_extensions);
1234 sub dump_systemflags {
1235 return dump_bitmask_and(@_,%ads_systemflags);
1238 sub dump_instance_type {
1239 return dump_bitmask_and(@_,%ads_instance_type);
1243 return dump_bitmask_equal(@_,%ads_ds_func);
1246 sub dump_serverstate {
1247 return dump_bitmask_equal(@_,%ads_serverstate);
1250 sub dump_sdeffective {
1251 return dump_bitmask_and(@_,%ads_sdeffective);
1254 sub dump_trustattr {
1255 return dump_bitmask_equal(@_,%ads_trustattrs);
1258 sub dump_trusttype {
1259 return dump_bitmask_equal(@_,%ads_trusttype);
1262 sub dump_trustdirection {
1263 return dump_bitmask_equal(@_,%ads_trustdirection);
1266 sub dump_pwdproperties {
1267 return dump_bitmask_and(@_,%ads_pwdproperties);
1271 return dump_bitmask_equal(@_,%ads_frstypes)
1274 sub dump_mixed_domain {
1275 return dump_bitmask_equal(@_,%ads_mixed_domain);
1279 my $bin_sid = shift;
1280 return sid2string($bin_sid);
1285 return guid_to_string($guid);
1290 return "FIXME: write sddl-converter!";
1296 return sprintf("%s (%s)", "never", $nttime);
1298 my $localtime = localtime(nt_to_unixtime($nttime));
1299 return sprintf("%s (%s)", $localtime, $nttime);
1302 sub dump_nttime_abs {
1303 if ($_[0] == 9223372036854775807) {
1304 return sprintf("%s (%s)", "now", $_[0]);
1306 if ($_[0] == -9223372036854775808 || $_[0] == 0) { # 0x7FFFFFFFFFFFFFFF
1307 return sprintf("%s (%s)", "never", $_[0]);
1309 # FIXME: actually *do* abs time !
1310 return dump_nttime($_[0]);
1315 if ($time eq "16010101000010.0Z") {
1316 return sprintf("%s (%s)", "never", $time);
1318 my ($year,$mon,$mday,$hour,$min,$sec,$zone) =
1319 unpack('a4 a2 a2 a2 a2 a2 a4', $time);
1321 my $localtime = localtime(timegm($sec,$min,$hour,$mday,$mon,$year));
1322 return sprintf("%s (%s)", $localtime, $time);
1330 return sprintf("%d", $_[0]);
1333 sub dump_munged_dial {
1335 return "FIXME! decode this";
1341 open(OPENSSL, "| /usr/bin/openssl x509 -text -inform der");
1342 print OPENSSL $cert;
1350 printf("%s: ", $pkt);
1351 printf("%02X", $pkt);
1358 my $gplink_mod = $gplink;
1359 my @links = split("\\]\\[", $gplink_mod);
1360 foreach my $link (@links) {
1361 $link =~ s/^\[|\]$//g;
1362 my ($ldap_link, $opt) = split(";", $link);
1363 my @array = ( "$opt", "\t" );
1364 printf("%slink: %s, opt: %s\n", $tabsize, $ldap_link, dump_bitmask_and(@array, %ads_gplink_opts));
1369 sub construct_filter {
1373 if (!$tmp || $opt_notify) {
1374 return "(objectclass=*)";
1377 if ($tmp && $tmp !~ /^.*\=/) {
1378 return "(ANR=$tmp)";
1382 # (&(objectCategory=person)
1383 # (userAccountControl:$ads_matching_rules{LDAP_MATCHING_RULE_BIT_AND}:=2))
1386 sub construct_attrs {
1395 push(@attrs,"uSNChanged");
1398 if ($opt_display_metadata) {
1399 push(@attrs,"msDS-ReplAttributeMetaData");
1400 push(@attrs,"replPropertyMetaData");
1404 push(@attrs,"nTSecurityDescriptor");
1405 push(@attrs,"msDS-KeyVersionNumber");
1406 push(@attrs,"msDS-User-Account-Control-Computed");
1407 push(@attrs,"modifyTimeStamp");
1416 printf "%10s: %s\n", "uri", $uri;
1417 printf "%10s: %s\n", "port", $port;
1418 printf "%10s: %s\n", "base", $base;
1419 printf "%10s: %s\n", $sasl_bind ? "principal" : "binddn", $sasl_bind ? $upn : $binddn;
1420 printf "%10s: %s\n", "password", $password;
1421 printf "%10s: %s\n", "bind-type", $sasl_bind ? "SASL" : "simple";
1422 printf "%10s: %s\n", "sasl-mech", $sasl_mech if ($sasl_mech);
1423 printf "%10s: %s\n", "filter", $filter;
1424 printf "%10s: %s\n", "scope", $scope;
1425 printf "%10s: %s\n", "attrs", join(", ", @attrs);
1426 printf "%10s: %s\n", "controls", join(", ", @ctrls_s);
1427 printf "%10s: %s\n", "page_size", $opt_paging if ($opt_paging);
1428 printf "%10s: %s\n", "start_tls", $opt_starttls ? "yes" : "no";
1434 # setup attribute-scoped query control
1435 my $asq_asn = Convert::ASN1->new;
1437 q< asq ::= SEQUENCE {
1438 sourceAttribute OCTET_STRING
1442 my $ctl_asq_val = $asq_asn->encode( sourceAttribute => $opt_asq);
1443 my $ctl_asq = Net::LDAP::Control->new(
1444 type => $ads_controls{'LDAP_SERVER_ASQ_OID'},
1446 value => $ctl_asq_val);
1449 # setup extended dn control
1450 my $asn_extended_dn = Convert::ASN1->new;
1451 $asn_extended_dn->prepare(
1452 q< ExtendedDn ::= SEQUENCE {
1458 # only w2k3 accepts '1' and needs the ctl_val, w2k does not accept a ctl_val
1459 my $ctl_extended_dn_val = $asn_extended_dn->encode( mode => $opt_display_extendeddn);
1460 my $ctl_extended_dn = Net::LDAP::Control->new(
1461 type => $ads_controls{'LDAP_SERVER_EXTENDED_DN_OID'},
1463 value => $opt_display_extendeddn ? $ctl_extended_dn_val : "");
1465 # setup notify control
1466 my $ctl_notification = Net::LDAP::Control->new(
1467 type => $ads_controls{'LDAP_SERVER_NOTIFICATION_OID'},
1468 critical => 'true');
1471 # setup paging control
1472 $ctl_paged = Net::LDAP::Control->new(
1473 type => $ads_controls{'LDAP_PAGED_RESULT_OID_STRING'},
1475 size => $opt_paging ? $opt_paging : 1000);
1477 # setup domscope control
1478 my $ctl_domscope = Net::LDAP::Control->new(
1479 type => $ads_controls{'LDAP_SERVER_DOMAIN_SCOPE_OID'},
1483 if (defined($opt_paging)) {
1484 push(@ctrls, $ctl_paged);
1485 push(@ctrls_s, "LDAP_PAGED_RESULT_OID_STRING" );
1488 if (defined($opt_display_extendeddn)) {
1489 push(@ctrls, $ctl_extended_dn);
1490 push(@ctrls_s, "LDAP_SERVER_EXTENDED_DN_OID");
1493 push(@ctrls, $ctl_notification);
1494 push(@ctrls_s, "LDAP_SERVER_NOTIFICATION_OID");
1498 push(@ctrls, $ctl_asq);
1499 push(@ctrls_s, "LDAP_SERVER_ASQ_OID");
1502 if ($opt_domain_scope) {
1503 push(@ctrls, $ctl_domscope);
1504 push(@ctrls_s, "LDAP_SERVER_DOMAIN_SCOPE_OID");
1510 sub display_value_generic ($$$) {
1512 my ($mod,$attr,$value) = @_;
1513 return unless (defined($value) and defined($attr));
1515 if ( ! $opt_display_raw && exists $attr_handler{$attr} ) {
1516 $value = $attr_handler{$attr}($value,$mod);
1517 write_ads_list($mod,$attr,$value);
1520 write_ads($mod,$attr,$value);
1523 sub display_attr_generic ($$$) {
1525 my ($mod,$entry,$attr) = @_;
1526 return if (!$attr || !$entry);
1528 my $ref = $entry->get_value($attr, asref => 1);
1531 foreach my $value ( sort @values) {
1532 display_value_generic($mod,$attr,$value);
1536 sub display_entry_generic ($) {
1539 return if (!$entry);
1541 foreach my $attr ( sort $entry->attributes) {
1542 display_attr_generic("\t",$entry,$attr);
1546 sub display_ldap_err ($) {
1550 my ($package, $filename, $line, $subroutine) = caller(0);
1552 print "got error from ADS:\n";
1553 printf("%s(%d): ERROR (%s): %s\n",
1554 $subroutine, $line, $msg->code, $msg->error);
1558 sub process_result {
1560 my ($msg,$entry) = @_;
1562 if (!defined($msg)) {
1567 display_ldap_err($msg);
1571 if (!defined($entry)) {
1575 if ($entry->isa('Net::LDAP::Reference')) {
1576 foreach my $ref ($entry->references) {
1577 print "\ngot Reference: [$ref]\n";
1582 print "\nfound entry: ".$entry->dn."\n".("-" x 80)."\n";
1584 display_entry_generic($entry);
1587 sub default_callback {
1589 my ($res,$obj) = @_;
1591 if (!$opt_notify && $res->code == LDAP_REFERRAL) {
1596 return process_result($res,$obj);
1600 sub error_callback {
1602 my ($msg,$entry) = @_;
1609 display_ldap_err($msg);
1615 sub notify_callback {
1617 my ($msg, $obj) = @_;
1619 if (! defined($obj)) {
1623 if ($obj->isa('Net::LDAP::Reference')) {
1624 foreach my $ref ($obj->references) {
1625 print "\ngot Reference: [$ref]\n";
1632 my $async_entry = $async_search->pop_entry;
1634 if (!$async_entry->dn) {
1635 print "very weird. entry has no dn\n";
1639 printf("\ngot changenotify for dn: [%s]\n%s\n", $async_entry->dn, ("-" x 80));
1641 my $new_usn = $async_entry->get_value('uSNChanged');
1643 die "odd, no usnChanged attribute???";
1645 if (!$usn || $new_usn > $usn) {
1647 if ($opt_notify_nodiffs) {
1648 display_entry_generic($async_entry);
1650 display_result_diff($async_entry);
1653 store_result($async_entry);
1659 my $async_ldap_hd = shift || return undef;
1660 my $sasl_bind = shift;
1664 print "this version of Net::LDAP does not have proper SASL support.\n";
1665 print "Need at least perl-ldap V. $pref_version\n";
1667 $sasl_hd = get_sasl_hd();
1669 print "failed to create SASL handle\n";
1672 $mesg = $async_ldap_hd->bind(
1674 callback => \&error_callback
1675 ) || die "doesnt work";
1678 $mesg = $async_ldap_hd->bind(
1680 password => $password,
1681 callback => $opt_fastbind ? undef : \&error_callback
1682 ) || die "doesnt work";
1685 display_ldap_err($mesg) if (!$opt_fastbind);
1691 sub do_fast_bind() {
1695 my $hd = get_ldap_hd($server,1);
1697 my $mesg = $hd->extension(
1698 name => $ads_extensions{'LDAP_SERVER_FAST_BIND_OID'});
1701 display_ldap_err($mesg);
1705 my $ret = do_bind($hd, undef);
1708 printf("bind using 'LDAP_SERVER_FAST_BIND_OID' %s\n",
1709 $ret == 0 ? "succeeded" : "failed");
1715 if ($async_ldap_hd) {
1716 $async_ldap_hd->unbind;
1718 if ($sync_ldap_hd) {
1719 $sync_ldap_hd->unbind;
1723 ###############################################################################
1727 if ($opt_fastbind) {
1729 if (check_exts($server,$dse,"LDAP_SERVER_FAST_BIND_OID") == -1) {
1730 print "LDAP_SERVER_FAST_BIND_OID not supported by this server\n";
1734 # fast bind can only bind, not search
1735 exit do_fast_bind();
1738 $filter = construct_filter($query);
1739 @attrs = construct_attrs(@attrs);
1740 @ctrls = gen_controls();
1742 if (check_ctrls($server,$dse,@ctrls) == -1) {
1743 print "not all required LDAP Controls are supported by this server\n";
1747 if ($opt_dump_rootdse) {
1748 print "Dumping Root-DSE:\n";
1749 display_entry_generic($dse);
1753 if ($opt_dump_wknguid) {
1758 if (do_bind($async_ldap_hd, $sasl_bind) == -1) {
1764 if ($opt_dump_schema) {
1765 print "Dumping Schema:\n";
1766 my $ads_schema = $async_ldap_hd->schema;
1773 $async_search = $async_ldap_hd->search(
1776 attrs => [ @attrs ],
1777 control => [ @ctrls ],
1778 callback => \&default_callback,
1780 ) || die "cannot search";
1782 if (!$opt_notify && ($async_search->code == LDAP_REFERRAL)) {
1783 foreach my $ref ($async_search->referrals) {
1784 print "\ngot Referral: [$ref]\n";
1785 my ($prot, $host, $base) = split(/\/+/, $ref);
1786 $async_ldap_hd->unbind();
1787 $async_ldap_hd = get_ldap_hd($host, 1);
1788 if (do_bind($async_ldap_hd, $sasl_bind) == -1) {
1789 $async_ldap_hd->unbind();
1792 print "\nsuccessful rebind to: [$ref]\n";
1795 next; # repeat the query
1800 print "Base [$base] is registered now for change-notify\n";
1801 print "\nWaiting for change-notify...\n";
1803 my $sync_ldap_hd = get_ldap_hd($server,0);
1804 if (do_bind($sync_ldap_hd, $sasl_bind) == -1) {
1808 my $sync_search = $sync_ldap_hd->search(
1811 attrs => [ @attrs ],
1812 callback => \¬ify_callback,
1814 ) || die "cannot search";
1818 $total_entry_count += $async_search->count;
1820 print "-" x 80 . "\n";
1821 printf("Got %d Entries in Page %d \n\n",
1822 $async_search->count, $page_count);
1823 my ($resp) = $async_search->control(
1824 $ads_controls{'LDAP_PAGED_RESULT_OID_STRING'} ) or last;
1825 last unless ref $resp && $ctl_paged->cookie($resp->cookie);
1828 if ($async_search->entries == 0) {
1829 print "No entries found with filter $filter\n";
1831 print "Got $total_entry_count Entries in $page_count Pages\n" if ($opt_paging);