getent 1.4 KB

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