]> git.sur5r.net Git - openldap/blob - servers/slapd/back-perl/SampleLDAP.pm
Finished compare implementation
[openldap] / servers / slapd / back-perl / SampleLDAP.pm
1
2 =head1 Introduction
3
4 This is a sample Perl module for the OpenLDAP server slapd.
5 It also contains the documentation that you will need to
6 get up and going.
7
8 WARNING: the interfaces of this backen to the perl module
9 MAY change.  Any suggestions would greatly be appreciated.
10
11
12 =head1 Overview
13
14 The Perl back end works by embedding a Perl interpreter into
15 the slapd backend. Then when the configuration file indicates
16 that we are going to be using a Perl backend it will get an
17 option that tells it what module to use.  It then creates a 
18 new Perl object that handles all the request for that particular
19 instance of the back end.
20
21
22 =head1 Interface
23
24 You will need to create a method for each one of the
25 following actions that you wish to handle.
26
27    * new        # Creates a new object.
28    * search     # Performs the ldap search
29    * compare    # does a compare
30    * modify     # modify's and entry
31    * add        # adds an entry to back end
32    * modrdn     # modifies a an entries rdn
33    * delete     # deletes an ldap entry
34    * config     # process unknow config file lines
35
36 =head2 new
37
38 This method is called when the config file encounters a 
39 B<perlmod> line. The module in that line is then effectively
40 used into the perl interpreter, then the new method is called
41 to create a new object.  Note that multiple instances of that
42 object may be instantiated, as with any perl object.
43
44 The new method doesn't receive any arguments other than the
45 class name.
46
47 RETURN: 
48
49 =head2 search
50
51 This method is called when a search request comes from a client.
52 It arguments are as follow.
53
54   * obj reference
55   * filter string
56   * size limit
57   * time limit
58   * attributes only flag ( 1 for yes )
59   * list of attributes that are to be returned. (could be empty)
60
61 RETURN:
62
63 =head2 compare
64
65 This method is called when a compare request comes from a client.
66 Its arguments are as follows.
67
68   * obj reference
69   * dn
70   * attribute assertion string
71
72 RETURN:
73
74 =head2 modify
75
76 This method is called when a modify request comes from a client.
77 Its arguments are as follows.
78
79   * obj reference
80   * dn
81   * lists formatted as follows
82    { ADD | DELETE | REPLACE }, key, value
83
84 RETURN:
85
86 =head2 add
87
88 This method is called when a add request comes from a client.
89 Its arguments are as follows.
90
91   * obj reference
92   * entry in string format.
93
94 RETURN:
95
96 =head2 modrdn
97
98 This method is called when a modrdn request comes from a client.
99 Its arguments are as follows.
100
101   * obj reference
102   * dn
103   * new rdn
104   * delete old dn flage ( 1 means yes )
105
106 RETURN:
107
108 =head2 delete
109
110 This method is called when a delete request comes from a client.
111 Its arguments are as follows.
112
113   * obj reference
114   * dn
115
116 RETURN:
117
118 =head2 config
119
120   * obj reference
121   * arrray of arguments on line
122
123 RETURN: non zero value if this is not a valid option.
124
125 =head1 Configuration
126
127 The perl section of the config file recognizes the following 
128 options.  It should also be noted that any option not recoginized
129 will be sent to the B<config> method of the perl module as noted
130 above.
131
132   database perl         # startn section for the perl database
133
134   suffix          "o=AnyOrg, c=US"
135
136   perlModulePath /path/to/libs  # addes the path to @INC variable same
137                              # as "use lib '/path/to/libs'"
138
139   perlModule ModName       # use the module name ModName from ModName.pm
140
141
142
143 =cut
144
145 package SampleLDAP;
146
147 use POSIX;
148
149 sub new
150 {
151         my $class = shift;
152
153         my $this = {};
154         bless $this, $class;
155         print STDERR "Here in new\n";
156         print STDERR "Posix Var " . BUFSIZ . " and " . FILENAME_MAX . "\n";
157         return $this;
158 }
159
160 sub search
161 {
162         my $this = shift;
163         my( $filterStr, $sizeLim, $timeLim, $attrOnly, @attrs ) = @_;
164         print STDERR "====$filterStr====\n";
165         $filterStr =~ s/\(|\)//g;
166         $filterStr =~ s/=/: /;
167
168         my @match_dn = ();
169         foreach my $dn ( keys %$this ) {
170                 if ( $this->{ $dn } =~ /$filterStr/im ) {
171                         push @match_dn, $dn;
172                         last if ( scalar @match_dn == $sizeLim );
173
174                 }
175         }
176
177         my @match_entries = ();
178         
179         foreach my $dn ( @match_dn )  {
180                 push @match_entries, $this->{ $dn };
181         }
182
183         return ( 0 , @match_entries );
184
185 }
186
187 sub compare
188 {
189         my $this = shift;
190         my ( $dn, $avaStr ) = @_;
191         my $rc = 0;
192
193         $avaStr =~ s/=/: /;
194
195         if ( $this->{ $dn } =~ /$avaStr/im ) {
196                 $rc = 1;
197         }
198
199         return $rc;
200 }
201
202 sub modify
203 {
204         my $this = shift;
205
206         my ( $dn, @list ) = @_;
207
208         while ( @list > 0 ) {
209                 my $action = shift @list;
210                 my $key    = shift @list;
211                 my $value  = shift @list;
212
213                 if( $action eq "ADD" ) {
214                         $this->{ $dn } .= "$key: $value\n";
215
216                 }
217                 elsif( $action eq "DELETE" ) {
218                         $this->{ $dn } =~ s/^$key:\s*$value\n//mi ;
219
220                 }
221                 elsif( $action eq "REPLACE" ) {
222                         $this->{ $dn } =~ s/$key: .*$/$key: $value/im ;
223                 }
224         }
225
226         return 0;
227 }
228
229 sub add
230 {
231         my $this = shift;
232
233         my ( $entryStr ) = @_;
234
235         my ( $dn ) = ( $entryStr =~ /dn:\s(.*)$/m );
236
237         #
238         # This needs to be here untill a normalize dn is
239         # passed to this routine.
240         #
241         $dn = uc( $dn );
242         $dn =~ s/\s*//g;
243
244
245         $this->{$dn} = $entryStr;
246
247         return 0;
248 }
249
250 sub modrdn
251 {
252         my $this = shift;
253
254         my ( $dn, $newdn, $delFlag ) = @_;
255
256         $this->{ $newdn } = $this->{ $dn };
257
258         if( $delFlag ) {
259                 delete $this->{ $dn };
260         }
261         return 0;
262
263 }
264
265 sub delete
266 {
267         my $this = shift;
268
269         my ( $dn ) = @_;
270         
271         print STDERR "XXXXXX $dn XXXXXXX\n";
272         delete $this->{$dn};
273 }
274
275 sub config
276 {
277         my $this = shift;
278
279         my ( @args ) = @_;
280         local $, = " - ";
281         print STDERR @args;
282         print STDERR "\n";
283         return 0;
284 }
285
286 1;
287
288