Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
6.0 KiB

  1. # Copyright (c) 1998 Graham Barr <[email protected]>. All rights reserved.
  2. # This program is free software; you can redistribute it and/or
  3. # modify it under the same terms as Perl itself.
  4. package URI::ldap;
  5. use strict;
  6. use vars qw(@ISA $VERSION);
  7. $VERSION = "1.10";
  8. require URI::_server;
  9. @ISA=qw(URI::_server);
  10. use URI::Escape qw(uri_unescape);
  11. sub default_port { 389 }
  12. sub _ldap_elem {
  13. my $self = shift;
  14. my $elem = shift;
  15. my $query = $self->query;
  16. my @bits = (split(/\?/,defined($query) ? $query : ""),("")x4);
  17. my $old = $bits[$elem];
  18. if (@_) {
  19. my $new = shift;
  20. $new =~ s/\?/%3F/g;
  21. $bits[$elem] = $new;
  22. $query = join("?",@bits);
  23. $query =~ s/\?+$//;
  24. $query = undef unless length($query);
  25. $self->query($query);
  26. }
  27. $old;
  28. }
  29. sub dn {
  30. my $old = shift->path(@_);
  31. $old =~ s:^/::;
  32. uri_unescape($old);
  33. }
  34. sub attributes {
  35. my $self = shift;
  36. my $old = _ldap_elem($self,0, @_ ? join(",", map { my $tmp = $_; $tmp =~ s/,/%2C/g; $tmp } @_) : ());
  37. return $old unless wantarray;
  38. map { uri_unescape($_) } split(/,/,$old);
  39. }
  40. sub _scope {
  41. my $self = shift;
  42. my $old = _ldap_elem($self,1, @_);
  43. return unless defined wantarray && defined $old;
  44. uri_unescape($old);
  45. }
  46. sub scope {
  47. my $old = &_scope;
  48. $old = "base" unless length $old;
  49. $old;
  50. }
  51. sub _filter {
  52. my $self = shift;
  53. my $old = _ldap_elem($self,2, @_);
  54. return unless defined wantarray && defined $old;
  55. uri_unescape($old); # || "(objectClass=*)";
  56. }
  57. sub filter {
  58. my $old = &_filter;
  59. $old = "(objectClass=*)" unless length $old;
  60. $old;
  61. }
  62. sub extensions {
  63. my $self = shift;
  64. my @ext;
  65. while (@_) {
  66. my $key = shift;
  67. my $value = shift;
  68. push(@ext, join("=", map { $_="" unless defined; s/,/%2C/g; $_ } $key, $value));
  69. }
  70. @ext = join(",", @ext) if @ext;
  71. my $old = _ldap_elem($self,3, @ext);
  72. return $old unless wantarray;
  73. map { uri_unescape($_) } map { /^([^=]+)=(.*)$/ } split(/,/,$old);
  74. }
  75. sub canonical
  76. {
  77. my $self = shift;
  78. my $other = $self->SUPER::canonical;
  79. # The stuff below is not as efficient as one might hope...
  80. $other = $other->clone if $other == $self;
  81. $other->dn(_normalize_dn($other->dn));
  82. # Should really know about mixed case "postalAddress", etc...
  83. $other->attributes(map lc, $other->attributes);
  84. # Lowecase scope, remove default
  85. my $old_scope = $other->scope;
  86. my $new_scope = lc($old_scope);
  87. $new_scope = "" if $new_scope eq "base";
  88. $other->scope($new_scope) if $new_scope ne $old_scope;
  89. # Remove filter if default
  90. my $old_filter = $other->filter;
  91. $other->filter("") if lc($old_filter) eq "(objectclass=*)" ||
  92. lc($old_filter) eq "objectclass=*";
  93. # Lowercase extensions types and deal with known extension values
  94. my @ext = $other->extensions;
  95. for (my $i = 0; $i < @ext; $i += 2) {
  96. my $etype = $ext[$i] = lc($ext[$i]);
  97. if ($etype =~ /^!?bindname$/) {
  98. $ext[$i+1] = _normalize_dn($ext[$i+1]);
  99. }
  100. }
  101. $other->extensions(@ext) if @ext;
  102. $other;
  103. }
  104. sub _normalize_dn # RFC 2253
  105. {
  106. my $dn = shift;
  107. return $dn;
  108. # The code below will fail if the "+" or "," is embedding in a quoted
  109. # string or simply escaped...
  110. my @dn = split(/([+,])/, $dn);
  111. for (@dn) {
  112. s/^([a-zA-Z]+=)/lc($1)/e;
  113. }
  114. join("", @dn);
  115. }
  116. 1;
  117. __END__
  118. =head1 NAME
  119. URI::ldap - LDAP Uniform Resource Locators
  120. =head1 SYNOPSIS
  121. use URI;
  122. $uri = URI->new("ldap:$uri_string");
  123. $dn = $uri->dn;
  124. $filter = $uri->filter;
  125. @attr = $uri->attributes;
  126. $scope = $uri->scope;
  127. %extn = $uri->extensions;
  128. $uri = URI->new("ldap:"); # start empty
  129. $uri->host("ldap.itd.umich.edu");
  130. $uri->dn("o=University of Michigan,c=US");
  131. $uri->attributes(qw(postalAddress));
  132. $uri->scope('sub');
  133. $uri->filter('(cn=Babs Jensen)');
  134. print $uri->as_string,"\n";
  135. =head1 DESCRIPTION
  136. C<URI::ldap> provides an interface to parse an LDAP URI in its
  137. constituent parts and also build a URI as described in
  138. RFC 2255.
  139. =head1 METHODS
  140. C<URI::ldap> support all the generic and server methods defined by
  141. L<URI>, plus the following.
  142. Each of the following methods can be used to set or get the value in
  143. the URI. The values are passed in unescaped form. None of these will
  144. return undefined values, but elements without a default can be empty.
  145. If arguments are given then a new value will be set for the given part
  146. of the URI.
  147. =over 4
  148. =item $uri->dn( [$new_dn] )
  149. Set or get the I<Distinguised Name> part of the URI. The DN
  150. identifies the base object of the LDAP search.
  151. =item $uri->attributes( [@new_attrs] )
  152. Set or get the list of attribute names which will be
  153. returned by the search.
  154. =item $uri->scope( [$new_scope] )
  155. Set or get the scope that the search will use. The value can be one of
  156. C<"base">, C<"one"> or C<"sub">. If none is given in the URI then the
  157. return value will default to C<"base">.
  158. =item $uri->_scope( [$new_scope] )
  159. Same as scope(), but does not default to anything.
  160. =item $uri->filter( [$new_filter] )
  161. Set or get the filter that the search will use. If none is given in
  162. the URI then the return value will default to C<"(objectClass=*)">.
  163. =item $uri->_filter( [$new_filter] )
  164. Same as filter(), but does not default to anything.
  165. =item $uri->extensions( [$etype => $evalue,...] )
  166. Set or get the extensions used for the search. The list passed should
  167. be in the form etype1 => evalue1, etype2 => evalue2,... This is also
  168. the form of list that will be returned.
  169. =back
  170. =head1 SEE ALSO
  171. L<RFC-2255|http://www.cis.ohio-state.edu/htbin/rfc/rfc2255.html>
  172. =head1 AUTHOR
  173. Graham Barr E<lt>F<[email protected]>E<gt>
  174. Slightly modified by Gisle Aas to fit into the URI distribution.
  175. =head1 COPYRIGHT
  176. Copyright (c) 1998 Graham Barr. All rights reserved. This program is
  177. free software; you can redistribute it and/or modify it under the same
  178. terms as Perl itself.
  179. =cut