ldif2ldap.in 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/bin/sh
  2. source @datadir@/@package_name@/data/DSSharedLib
  3. libpath_add "@ldapsdk_libdir@"
  4. libpath_add "@libdir@"
  5. libpath_add "@nss_libdir@"
  6. libpath_add "@libdir@/@package_name@/"
  7. export LD_LIBRARY_PATH
  8. SHLIB_PATH=$LD_LIBRARY_PATH
  9. export SHLIB_PATH
  10. PATH=$PATH:@ldaptool_bindir@:@ldaptool_bindir@:/usr/bin:/usr/lib64/mozldap
  11. usage ()
  12. {
  13. echo "Usage: ldif2ldap [-Z serverID] [-D <rootdn>] -w <password> -f <file> [-P protocol] [-h]"
  14. echo "Options:"
  15. echo " -Z serverID - Server instance identifier"
  16. echo " -D rootdn - Directory Manager DN"
  17. echo " -w passwd - Directory Manager password"
  18. echo " -f file - File containing LDAP entries to add to the server"
  19. echo " -P protocol - STARTTLS, LDAPS, LDAPI, LDAP"
  20. echo " -h - Display usage"
  21. }
  22. while getopts "Z:D:w:f:hP:" flag
  23. do
  24. case $flag in
  25. Z) servid=$OPTARG;;
  26. P) protocol=$OPTARG;;
  27. D) rootdn=$OPTARG;;
  28. w) passwd=$OPTARG;;
  29. f) args=$args"-f $OPTARG"
  30. input_file=$OPTARG;;
  31. h) usage
  32. exit 0;;
  33. ?) usage
  34. exit 1;;
  35. esac
  36. done
  37. if [ "$input_file" == "" ]
  38. then
  39. usage
  40. exit 1
  41. fi
  42. initfile=$(get_init_file "@initconfigdir@" $servid)
  43. if [ $? == 1 ]
  44. then
  45. usage
  46. echo "You must supply a valid server instance identifier. Use -Z to specify instance name"
  47. echo "Available instances: $initfile"
  48. exit 1
  49. fi
  50. . $initfile
  51. process_dse $CONFIG_DIR $$
  52. file="/tmp/DSSharedLib.$$"
  53. port=$(grep -i 'nsslapd-port' $file | awk '{print $2}' )
  54. host=$(grep -i 'nsslapd-localhost' $file | awk '{print $2}' )
  55. security=$(grep -i 'nsslapd-security' $file | awk '{print $2}' )
  56. secure_port=$(grep -i 'nsslapd-secureport' $file | awk '{print $2}' )
  57. ldapi=$(grep -i 'nsslapd-ldapilisten' $file | awk '{print $2}' )
  58. ldapiURL=$(grep -i 'nsslapd-ldapifilepath' $file | awk '{print $2}' )
  59. certdir=$(grep -i 'nsslapd-certdir' $file | awk '{print $2}' )
  60. autobind=$(grep -i 'nsslapd-ldapiautobind' $file | awk '{print $2}' )
  61. if [ "$rootdn" == "" ]; then
  62. value=$(grep -i 'nsslapd-rootdn' $file)
  63. rootdn=`echo "$value" | sed -e 's/nsslapd-rootdn: //i'`
  64. fi
  65. rm $file
  66. if [ "$ldapiURL" != "" ]; then
  67. ldapiURL=`echo "$ldapiURL" | sed -e 's/\//%2f/g'`
  68. ldapiURL="ldapi://"$ldapiURL
  69. fi
  70. client_type=`ldapmodify -V 2>&1`;
  71. echo "$client_type" | grep -q "OpenLDAP"
  72. if [ $? -eq 0 ]
  73. then
  74. openldap="yes"
  75. export LDAPTLS_CACERTDIR=$certdir
  76. fi
  77. if [ -z $security ]; then
  78. security="off"
  79. fi
  80. revised_protocol=$(check_protocol $protocol $security $ldapi $openldap)
  81. if [ "$revised_protocol" != "$protocol" ]; then
  82. echo Protocol $protocol requested, but this protocol is not supported
  83. error="yes"
  84. fi
  85. protocol=$revised_protocol
  86. #
  87. # STARTTLS
  88. #
  89. if [ "$security" == "on" ]; then
  90. if [ "$protocol" == "STARTTLS" ] || [ "$protocol" == "" ]; then
  91. if [ "$error" == "yes" ]; then
  92. echo "Using the next most secure protocol(STARTTLS)"
  93. fi
  94. if [ "$openldap" == "yes" ]; then
  95. ldapmodify -x -ZZ -p $port -h $host -D $rootdn -w $passwd -a -f $input_file
  96. else
  97. ldapmodify -ZZZ -P $certdir -p $port -h $host -D $rootdn -w $passwd -a -f $input_file
  98. fi
  99. exit $?
  100. fi
  101. fi
  102. #
  103. # LDAPS
  104. #
  105. if [ "$security" == "on" ]; then
  106. if [ "$protocol" == "LDAPS" ] || [ "$protocol" == "" ]; then
  107. if [ "$error" == "yes" ]; then
  108. echo "Using the next most secure protocol(LDAPS)"
  109. fi
  110. if [ "$openldap" == "yes" ]; then
  111. ldapmodify -x -H "ldaps://$host:$secure_port" -D $rootdn -w $passwd -a -f $input_file
  112. else
  113. ldapmodify -Z -P $certdir -p $secure_port -h $host -D $rootdn -w $passwd -a -f $input_file
  114. fi
  115. exit $?
  116. fi
  117. fi
  118. #
  119. # LDAPI
  120. #
  121. if [ "$ldapi" == "on" ] && [ "$openldap" == "yes" ]; then
  122. if [ "$protocol" == "LDAPI" ] || [ "$protocol" == "" ]; then
  123. if [ "$(id -u)" == "0" ] && [ "$autobind" == "on" ]; then
  124. if [ "$error" == "yes" ]; then
  125. echo "Using the next most secure protocol(LDAPI/AUTOBIND)"
  126. fi
  127. ldapmodify -H $ldapiURL -Y EXTERNAL -a -f $input_file 2>/dev/null
  128. else
  129. if [ "$error" == "yes" ]; then
  130. echo "Using the next most secure protocol(LDAPI)"
  131. fi
  132. ldapmodify -x -H $ldapiURL -D $rootdn -w $passwd -a -f $input_file
  133. fi
  134. rc=$?
  135. if [ $rc != 0 ]
  136. then
  137. echo "Operation failed (error $rc)"
  138. fi
  139. exit $rc
  140. fi
  141. fi
  142. #
  143. # LDAP
  144. #
  145. if [ "$protocol" == "LDAP" ] || [ "$protocol" == "" ]; then
  146. if [ "$error" == "yes" ]; then
  147. echo "Using the next most secure protocol(LDAP)"
  148. fi
  149. if [ "$openldap" == "yes" ]; then
  150. ldapmodify -x -p $port -h $host -D $rootdn -w $passwd -a -f $input_file
  151. else
  152. ldapmodify -p $port -h $host -D $rootdn -w $passwd -a -f $input_file
  153. fi
  154. exit $?
  155. fi
  156. echo ERROR $protocol