Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # convert OpenLDAP schema file to LDIF file
  4. #
  5. # Copyright 2012 NDE Netzdesign und -entwicklung AG, Hamburg
  6. # Written by Jens-U. Mozdzen <jmozdzen@nde.ag>
  7. # Copyright 2014 jaseg <github@jaseg.net>
  8. #
  9. # Permission is granted to use, modify and redistribute this file as long as
  10. # - this copyright notice is left unmodified and included in the final code
  11. # - the original author is notified via email if this code is re-distributed as part of a paid-for deliverable
  12. # - the original author is not held liable for any damage, loss of profit, efforts or inconvenience of any sorts
  13. # that may result from using, modifying or redistributing this software.
  14. #
  15. # Use at your own risk - this code may not be suitable for your needs or even cause damage when used.
  16. # If you find any problems with this code, please let the author know so that it can be fixed or at least others
  17. # can be warned.
  18. #
  19. # Usage: schema2ldif.sh <fully-qualified schema file name>
  20. #
  21. # This program will try to convert the source file to an LDIF-style file, placing the resulting .ldif file
  22. # in the current directory.
  23.  
  24. rc=0
  25.  
  26. if [ $# -gt 0 ] ; then
  27. slaptest=$(which slaptest 2>/dev/null ||ls /usr/sbin/slaptest||echo "")
  28. if [ -x "$slaptest" ] ; then
  29. schemaFile=$(readlink -f "$1")
  30. shift
  31. localdir=$(pwd)
  32.  
  33. if [ -r "$schemaFile" ] ; then
  34. targetFile=$(basename "$schemaFile" .schema).ldif
  35. if [ ! -e "$localdir/$targetFile" ] ; then
  36. echo "$0: converting $schemaFile to LDIF $localdir/$targetFile"
  37.  
  38. # create temp dir and config file
  39. tmpDir=$(mktemp -d)
  40. cd "$tmpDir"
  41. touch tmp.conf
  42. for dependency in "$@"
  43. do echo "include $dependency" >> tmp.conf
  44. done
  45. echo "include $schemaFile" >> tmp.conf
  46.  
  47. # convert
  48. "$slaptest" -f tmp.conf -F "$tmpDir"
  49.  
  50. # 3. rename and sanitize
  51. cd cn\=config/cn\=schema
  52. filenametmp=$(echo cn\=*"$targetFile")
  53. sed -r -e 's/^dn: cn=\{0\}(.*)$/dn: cn=\1,cn=schema,cn=config/' \
  54. -e 's/cn: \{0\}(.*)$/cn: \1/' \
  55. -e '/^structuralObjectClass: /d' \
  56. -e '/^entryUUID: /d' \
  57. -e '/^creatorsName: /d' \
  58. -e '/^createTimestamp: /d' \
  59. -e '/^entryCSN: /d' \
  60. -e '/^modifiersName: /d' \
  61. -e '/^modifyTimestamp: /d' \
  62. -e '/^# AUTO-GENERATED FILE - DO NOT EDIT!! Use ldapmodify./d' \
  63. -e '/^# CRC32 [0-9a-f]+/d' \
  64. -e 's/^cn: \{[0-9]*\}(.*)$/cn: \1/' \
  65. -e 's/^dn: cn=\{[0-9]*\}(.*)$/dn: cn=\1,cn=schema,cn=config/' < "$filenametmp" > "$localdir/$targetFile"
  66.  
  67. # clean up
  68. echo "$0: LDIF file successfully created as $localdir/$targetFile"
  69. rc=0
  70. rm -rf "$tmpDir"
  71. else
  72. echo "$0: target file $localdir/$targetFile already exists, aborting." >&2
  73. rc=3
  74. fi
  75. else
  76. echo "$0: source file $schemaFile could not be read, aborting." >&2
  77. rc=2
  78. fi
  79. else
  80. echo "$0: could not locate slaptest binary, exiting." >&2
  81. rc=1
  82. fi
  83. else
  84. echo "$0: usage: $0 <schemafile>" >&2
  85. rc=99
  86. fi
  87.  
  88. exit $rc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement