getent 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/bin/sh
  2. # $Header: /var/cvs/uClibc/extra/scripts/getent,v 1.2 2005/02/02 14:18:01 solar Exp $
  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. file="/etc/$1"
  13. string="dummy"
  14. #aliases|ethers|group|hosts|netgroup|networks|passwd|protocols|rpc|services|shadow)
  15. # dns based search is not supported for hosts|networks
  16. # ethers|netgroup (not done, needed)?
  17. # it returns only the first match
  18. case $1 in
  19. passwd)
  20. string="(^\<$2\>:|^.*:.*:\<$2\>:.*:.*:.*:.*)"
  21. ;;
  22. group)
  23. string="(^|:)\<$2\>:"
  24. ;;
  25. shadow)
  26. string="^\<$2\>:"
  27. ;;
  28. aliases)
  29. if [ -f /etc/postfix/aliases ] ; then
  30. file="/etc/postfix/aliases"
  31. elif [ -f /etc/mail/aliases ] ; then
  32. file="/etc/mail/aliases"
  33. fi
  34. string="^\<$2\>:"
  35. ;;
  36. networks)
  37. string="^\<$2\>"
  38. ;;
  39. hosts|protocols|rpc|services)
  40. string="\<$2\>"
  41. ;;
  42. *)
  43. echo "Unknown database: $1"
  44. exit 1
  45. ;;
  46. esac
  47. search_entry $file $2
  48. exit $retval