Initial commit
[clearscm.git] / lib / Clearquest / LDAP.pm
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         $RCSfile: LDAP.pm,v $
5 # Revision:     $Revision: 1.3 $
6 # Description:  The Clearquest LDAP Perl Module.
7 # Author:       Andrew@ClearSCM.com
8 # Created:      Fri Sep 22 09:21:18 CDT 2006
9 # Modified:     $Date: 2011/01/09 01:04:33 $
10 # Language:     perl
11 #
12 # (c) Copyright 2006, ClearSCM, Inc. all rights reserved
13 #
14 ################################################################################
15 use strict;
16 use warnings;
17
18 package LDAP;
19   use base "Exporter";
20
21   use Carp;
22   use OSDep;
23
24   my @MapFields = (
25     "CQ_EMAIL",
26     "CQ_FULLNAME",
27     "CQ_LOGIN_NAME",
28     "CQ_MISC_INFO",
29     "CQ_PHONE",
30   );
31
32   my @ScopeFields = (
33     "sub",
34     "one",
35     "base",
36   );
37
38   my @EXPORT = qw (
39     MapFields
40     ScopeFields
41     Validate
42     GetSettings
43   );
44
45   sub MapFields {
46     return @MapFields;
47   } # MAPFields
48
49   sub ScopeFields {
50     return @ScopeFields;
51   } # ScopeFields
52
53   sub Validate {
54     my (
55       $server,
56       $port,
57       $base,
58       $search_filter,
59       $account_attribute,
60       $search_for,
61     ) = @_;
62
63     eval { require Net::LDAP };
64
65     if ($@) {
66       return $FALSE, "Unable to load Net::LDAP. LDAP validation not possible.";
67     } # if
68
69     my $ldap = Net::LDAP->new ($server,
70       timeout   => 2,
71       port      => $port
72     );
73
74     return $FALSE, "Unable to connect to $server:$port" if !$ldap;
75
76     if (!$ldap->bind (version => 3)) {
77       return $FALSE, "Unable to bind to $server:$port";
78     } # if
79
80     my @attribute       = ($account_attribute);
81     my $key             = $search_filter;
82     $key =~ s/\%login\%/$search_for/;
83
84     my $result = $ldap->search (base    => $base,
85                                 scope   => "sub",
86                                 filter  => $key,
87                                 attrs   => @attribute,
88                                );
89
90     $ldap->unbind;
91
92     my $entry = $result->entry;
93
94     if ($entry) {
95       my $value =  $entry->get_value ($account_attribute);
96       return $TRUE, "Matched $key to LDAP";
97     } else {
98       return $FALSE, "Unable to find entry ($key)";
99     } # if
100   } # Validate
101
102   sub GetSettings {
103     my $dbset           = shift;
104     my $admin_username  = shift;
105     my $admin_passwords = shift;
106
107     my %LDAPSettings;
108
109     my $cmd = "installutil getldapinit $dbset $admin_username $admin_passwords";
110
111     my @output = `$cmd`;
112
113     carp "Unable to execute $cmd" if $?;
114
115     foreach (@output) {
116       chomp; chop if /\r/;
117
118       next if /^\*|^$/;
119
120       if (/Exit code (\d*)/) {
121         $? = $1;
122         next;
123       } # if
124
125       $LDAPSettings {ldapinit} .= "$_\n";
126     } # foreach
127
128     $cmd = "installutil getldapsearch $dbset $admin_username $admin_passwords";
129
130     @output = `$cmd`;
131
132     croak "Unable to execute $cmd" if $?;
133
134     foreach (@output) {
135       chomp; chop if /\r/;
136
137       next if /^\*|^$/;
138
139       if (/Exit code (\d*)/) {
140         $? = $1;
141         next;
142       } # if
143
144       $LDAPSettings {ldapsearch} .= "$_\n";
145     } # foreach
146
147     $cmd = "installutil getcqldapmap $dbset $admin_username $admin_passwords";
148
149     @output = `$cmd`;
150
151     croak "Unable to execute $cmd" if $?;
152
153     foreach (@output) {
154       chomp; chop if /\r/;
155
156       next if /^\*|^$/;
157
158       if (/Exit code (\d*)/) {
159         $? = $1;
160         next;
161       } # if
162
163       $LDAPSettings {cqldapmap} .= "$_\n";
164     } # foreach
165
166     return %LDAPSettings;
167   } # GetSettings
168 1;