BASH   70

migrate_common.ph

Guest on 28th July 2022 02:04:23 AM

  1. #
  2. # $Id: migrate_common.ph,v 1.22  lukeh Exp $
  3. #
  4. # Copyright (c)  Luke Howard.
  5. # All rights reserved.
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions
  9. # are met:
  10. # 1. Redistributions of source code must retain the above copyright
  11. #    notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. #    notice, this list of conditions and the following disclaimer in the
  14. #    documentation and/or other materials provided with the distribution.
  15. # 3. All advertising materials mentioning features or use of this software
  16. #    must display the following acknowledgement:
  17. #        This product includes software developed by Luke Howard.
  18. # 4. The name of the other may not be used to endorse or promote products
  19. #    derived from this software without specific prior written permission.
  20. #
  21. # THIS SOFTWARE IS PROVIDED BY THE LUKE HOWARD ``AS IS'' AND
  22. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. # ARE DISCLAIMED.  IN NO EVENT SHALL LUKE HOWARD BE LIABLE
  25. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. # SUCH DAMAGE.
  32. #
  33.  
  34. #
  35. # Common defines for MigrationTools
  36. #
  37.  
  38. # Naming contexts. Key is $PROGRAM with migrate_ and .pl
  39. # stripped off.
  40. $NETINFOBRIDGE = (-x "/usr/sbin/mkslapdconf");
  41.  
  42. if ($NETINFOBRIDGE) {
  43.         $NAMINGCONTEXT{'aliases'}           = "cn=aliases";
  44.         $NAMINGCONTEXT{'fstab'}             = "cn=mounts";
  45.         $NAMINGCONTEXT{'passwd'}            = "cn=users";
  46.         $NAMINGCONTEXT{'netgroup_byuser'}   = "cn=netgroup.byuser";
  47.         $NAMINGCONTEXT{'netgroup_byhost'}   = "cn=netgroup.byhost";
  48.         $NAMINGCONTEXT{'group'}             = "cn=groups";
  49.         $NAMINGCONTEXT{'netgroup'}          = "cn=netgroup";
  50.         $NAMINGCONTEXT{'hosts'}             = "cn=machines";
  51.         $NAMINGCONTEXT{'networks'}          = "cn=networks";
  52.         $NAMINGCONTEXT{'protocols'}         = "cn=protocols";
  53.         $NAMINGCONTEXT{'rpc'}               = "cn=rpcs";
  54.         $NAMINGCONTEXT{'services'}          = "cn=services";
  55. } else {
  56.         $NAMINGCONTEXT{'aliases'}           = "ou=Aliases";
  57.         $NAMINGCONTEXT{'fstab'}             = "ou=Mounts";
  58.         $NAMINGCONTEXT{'passwd'}            = "ou=People";
  59.         $NAMINGCONTEXT{'netgroup_byuser'}   = "nisMapName=netgroup.byuser";
  60.         $NAMINGCONTEXT{'netgroup_byhost'}   = "nisMapName=netgroup.byhost";
  61.         $NAMINGCONTEXT{'group'}             = "ou=Group";
  62.         $NAMINGCONTEXT{'netgroup'}          = "ou=Netgroup";
  63.         $NAMINGCONTEXT{'hosts'}             = "ou=Hosts";
  64.         $NAMINGCONTEXT{'networks'}          = "ou=Networks";
  65.         $NAMINGCONTEXT{'protocols'}         = "ou=Protocols";
  66.         $NAMINGCONTEXT{'rpc'}               = "ou=Rpc";
  67.         $NAMINGCONTEXT{'services'}          = "ou=Services";
  68. }
  69.  
  70. # Default DNS domain
  71. $DEFAULT_MAIL_DOMAIN = "mail.com";
  72.  
  73. # Default base
  74. $DEFAULT_BASE = "dc=club,dc=cc,dc=cmu,dc=edu";
  75.  
  76. # Turn this on for inetLocalMailReceipient
  77. # sendmail support; add the following to
  78. # sendmail.mc (thanks to Petr@Kristof.CZ):
  79. ##### CUT HERE #####
  80. #define(`confLDAP_DEFAULT_SPEC',`-h "ldap.padl.ldap"')dnl
  81. #LDAPROUTE_DOMAIN_FILE(`/etc/mail/ldapdomains')dnl
  82. #FEATURE(ldap_routing)dnl
  83. ##### CUT HERE #####
  84. # where /etc/mail/ldapdomains contains names of ldap_routed
  85. # domains (similiar to MASQUERADE_DOMAIN_FILE).
  86. #$DEFAULT_MAIL_HOST = "mail.mail.com";
  87.  
  88. # turn this on to support more general object clases
  89. # such as person.
  90. $EXTENDED_SCHEMA = 0;
  91.  
  92. # Comment this out if your ldap server does not support UTF8 encoding
  93. $USE_UTF8 = 1;
  94.  
  95. # Uncomment these to exclude system users and groups
  96. #$IGNORE_UID_BELOW = 1000;
  97. #$IGNORE_GID_BELOW = 100;
  98.  
  99. # And here's the opposite for completeness
  100. #$IGNORE_UID_ABOVE = 9999;
  101. #$IGNORE_GID_ABOVE = 9999;
  102.  
  103. #
  104. # allow environment variables to override predefines
  105. #
  106. if (defined($ENV{'LDAP_BASEDN'})) {
  107.         $DEFAULT_BASE = $ENV{'LDAP_BASEDN'};
  108. }
  109.  
  110. if (defined($ENV{'LDAP_DEFAULT_MAIL_DOMAIN'})) {
  111.         $DEFAULT_MAIL_DOMAIN = $ENV{'LDAP_DEFAULT_MAIL_DOMAIN'};
  112. }
  113.  
  114. if (defined($ENV{'LDAP_DEFAULT_MAIL_HOST'})) {
  115.         $DEFAULT_MAIL_HOST = $ENV{'LDAP_DEFAULT_MAIL_HOST'};
  116. }
  117.  
  118. # binddn used for alias owner (otherwise uid=root,...)
  119. if (defined($ENV{'LDAP_BINDDN'})) {
  120.         $DEFAULT_OWNER = $ENV{'LDAP_BINDDN'};
  121. }
  122.  
  123. if (defined($ENV{'LDAP_EXTENDED_SCHEMA'})) {
  124.         $EXTENDED_SCHEMA = $ENV{'LDAP_EXTENDED_SCHEMA'};
  125. }
  126.  
  127. # If we haven't set the default base, guess it automagically.
  128. if (!defined($DEFAULT_BASE)) {
  129.         $DEFAULT_BASE = &domain_expand($DEFAULT_MAIL_DOMAIN);
  130.         $DEFAULT_BASE =~ s/,$//o;
  131. }
  132.  
  133. # Default Kerberos realm
  134. if ($EXTENDED_SCHEMA) {
  135.         $DEFAULT_REALM = $DEFAULT_MAIL_DOMAIN;
  136.         $DEFAULT_REALM =~ tr/a-z/A-Z/;
  137. }
  138.  
  139. if (-x "/usr/sbin/revnetgroup") {
  140.         $REVNETGROUP = "/usr/sbin/revnetgroup";
  141. } elsif (-x "/usr/lib/yp/revnetgroup") {
  142.         $REVNETGROUP = "/usr/lib/yp/revnetgroup";
  143. }
  144.  
  145. $classmap{'o'} = 'organization';
  146. $classmap{'dc'} = 'domain';
  147. $classmap{'l'} = 'locality';
  148. $classmap{'ou'} = 'organizationalUnit';
  149. $classmap{'c'} = 'country';
  150. $classmap{'nismapname'} = 'nisMap';
  151. $classmap{'cn'} = 'container';
  152.  
  153. sub parse_args
  154. {
  155.         if ($#ARGV < 0) {
  156.                 print STDERR "Usage: $PROGRAM infile [outfile]\n";
  157.                 exit 1;
  158.         }
  159.        
  160.         $INFILE = $ARGV[0];
  161.        
  162.         if ($#ARGV > 0) {
  163.                 $OUTFILE = $ARGV[1];
  164.         }
  165. }
  166.  
  167. sub open_files
  168. {
  169.         open(INFILE);
  170.         if ($OUTFILE) {
  171.                 open(OUTFILE,">$OUTFILE");
  172.                 $use_stdout = 0;
  173.         } else {
  174.                 $use_stdout = 1;
  175.         }
  176. }
  177.  
  178. # moved from migrate_hosts.pl
  179. # lukeh
  180. sub domain_expand
  181. {
  182.         local($first) = 1;
  183.         local($dn);
  184.         local(@namecomponents) = split(/\./, $_[0]);
  185.         foreach $_ (@namecomponents) {
  186.                 $first = 0;
  187.                 $dn .= "dc=$_,";
  188.         }
  189.         $dn .= $DEFAULT_BASE;
  190.         return $dn;
  191. }
  192.  
  193. # case insensitive unique
  194. sub uniq
  195. {
  196.         local($name) = shift(@_);
  197.         local(@vec) = sort {uc($a) cmp uc($b)} @_;
  198.         local(@ret);
  199.         local($next, $last);
  200.         foreach $next (@vec) {
  201.                 if ((uc($next) ne uc($last)) &&
  202.                         (uc($next) ne uc($name))) {
  203.                         push (@ret, $next);
  204.                 }
  205.                 $last = $next;
  206.         }
  207.         return @ret;
  208. }
  209.  
  210. # concatenate naming context and
  211. # organizational base
  212. sub getsuffix
  213. {
  214.         local($program) = shift(@_);
  215.         local($nc);
  216.         $program =~ s/^migrate_(.*)\.pl$/$1/;
  217.         $nc = $NAMINGCONTEXT{$program};
  218.         if ($nc eq "") {
  219.                 return $DEFAULT_BASE;
  220.         } else {
  221.                 return $nc . ',' . $DEFAULT_BASE;
  222.         }
  223. }
  224.  
  225. sub ldif_entry
  226. {
  227. # remove leading, trailing whitespace
  228.         local ($HANDLE, $lhs, $rhs) = @_;
  229.         local ($type, $val) = split(/\=/, $lhs);
  230.         local ($dn);
  231.  
  232.         if ($rhs ne "") {
  233.                 $dn = $lhs . ',' . $rhs;
  234.         } else {
  235.                 $dn = $lhs;
  236.         }
  237.  
  238.         $type =~ s/\s*$//o;
  239.         $type =~ s/^\s*//o;
  240.         $type =~ tr/A-Z/a-z/;
  241.         $val =~ s/\s*$//o;
  242.         $val =~ s/^\s*//o;
  243.  
  244.         print $HANDLE "dn: $dn\n";
  245.         print $HANDLE "$type: $val\n";
  246.         print $HANDLE "objectClass: top\n";
  247.         print $HANDLE "objectClass: $classmap{$type}\n";
  248.         if ($EXTENDED_SCHEMA) {
  249.                 if ($DEFAULT_MAIL_DOMAIN) {
  250.                         print $HANDLE "objectClass: domainRelatedObject\n";
  251.                         print $HANDLE "associatedDomain: $DEFAULT_MAIL_DOMAIN\n";
  252.                 }
  253.         }
  254.  
  255.         print $HANDLE "\n";
  256. }
  257.  
  258. # Added Thu Jun 20 16:40:28 CDT  by Bob Apthorpe
  259. # <apthorpe@cynistar.net> to solve problems with embedded plusses in
  260. # protocols and mail aliases.
  261. sub escape_metacharacters
  262. {
  263.         local($name) = @_;
  264.  
  265.         # From Table 3.1 "Characters Requiring Quoting When Contained
  266.         # in Distinguished Names", p87 "Understanding and Deploying LDAP
  267.         # Directory Services", Howes, Smith, & Good.
  268.  
  269.         # 1) Quote backslash
  270.         # Note: none of these are very elegant or robust and may cause
  271.         # more trouble than they're worth. That's why they're disabled.
  272.         # 1.a) naive (escape all backslashes)
  273.         # $name =~ s#\\#\\\\#og;
  274.         #
  275.         # 1.b) mostly naive (escape all backslashes not followed by
  276.         # a backslash)
  277.         # $name =~ s#\\(?!\\)#\\\\#og;
  278.         #
  279.         # 1.c) less naive and utterly gruesome (replace solitary
  280.         # backslashes)
  281.         # $name =~ s{           # Replace
  282.         #               (?<!\\) # negative lookbehind (no preceding backslash)
  283.         #               \\      # a single backslash
  284.         #               (?!\\)  # negative lookahead (no following backslash)
  285.         #       }
  286.         #       {               # With
  287.         #               \\\\    # a pair of backslashes
  288.         #       }gx;
  289.         # Ugh. Note that s#(?:[^\\])\\(?:[^\\])#////#g fails if $name
  290.         # starts or ends with a backslash. This expression won't work
  291.         # under perl4 because the /x flag and negative lookahead and
  292.         # lookbehind operations aren't supported. Sorry. Also note that
  293.         # s#(?:[^\\]*)\\(?:[^\\]*)#////#g won't work either.  Of course,
  294.         # this is all broken if $name is already escaped before we get
  295.         # to it. Best to throw a warning and make the user import these
  296.         # records by hand.
  297.  
  298.         # 2) Quote leading and trailing spaces
  299.         local($leader, $body, $trailer) = ();
  300.         if (($leader, $body, $trailer) = ($name =~ m#^( *)(.*\S)( *)$#o)) {
  301.                 $leader =~ s# #\\ #og;
  302.                 $trailer =~ s# #\\ #og;
  303.                 $name = $leader . $body . $trailer;
  304.         }
  305.  
  306.         # 3) Quote leading octothorpe (#)
  307.         $name =~ s/^#/\\#/o;
  308.  
  309.         # 4) Quote comma, plus, double-quote, less-than, greater-than,
  310.         # and semicolon
  311.         $name =~ s#([,+"<>;])#\\$1#g;
  312.  
  313.         return $name;
  314. }
  315.  
  316. 1;

Raw Paste


Login or Register to edit or fork this paste. It's free.