getent 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/sh
  2. # Script to replicate the `getent` binary that comes with glibc
  3. search_entry() {
  4. if [ -e "$1" ] ; then
  5. /bin/egrep -v "^#" $1 | /bin/sed 's/#.*$//' | /bin/egrep "${string}" | /bin/sed -n 1p
  6. retval=$?
  7. [ "$retval" = 0 ] || retval=2
  8. else
  9. retval=2
  10. fi
  11. }
  12. if [ -z "$1" ] ; then
  13. echo "getent: wrong number of arguments" 1>&2
  14. exit 1
  15. fi
  16. file="/etc/$1"
  17. string="dummy"
  18. if [ ! -f "$file" ] ; then
  19. echo "Unknown database: $1" 1>&2
  20. exit 1
  21. fi
  22. #aliases|ethers|group|hosts|netgroup|networks|passwd|protocols|rpc|services|shadow)
  23. # dns based search is not supported for hosts|networks
  24. # ethers|netgroup (not done, needed)?
  25. # it returns only the first match
  26. case $1 in
  27. passwd)
  28. string="(^\<$2\>:|^.*:.*:\<$2\>:.*:.*:.*:.*)"
  29. ;;
  30. group)
  31. string="(^|:)\<$2\>:"
  32. ;;
  33. shadow)
  34. string="^\<$2\>:"
  35. ;;
  36. aliases)
  37. if [ -f /etc/postfix/aliases ] ; then
  38. file="/etc/postfix/aliases"
  39. elif [ -f /etc/mail/aliases ] ; then
  40. file="/etc/mail/aliases"
  41. fi
  42. string="^\<$2\>:"
  43. ;;
  44. networks)
  45. string="^\<$2\>"
  46. ;;
  47. hosts|protocols|rpc|services)
  48. string="\<$2\>"
  49. ;;
  50. *)
  51. echo "Unknown database: $1"
  52. exit 1
  53. ;;
  54. esac
  55. if [ -z "$2" ] ; then
  56. cat $file
  57. retval=$?
  58. else
  59. search_entry "$file" "$2"
  60. fi
  61. exit $retval