summaryrefslogtreecommitdiff
path: root/extra/scripts/getent
blob: 9ffffa1717fad674a20e057882189bd7312ec058 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/sh
#
# Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
#
# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
#

# Script to replicate the `getent` binary that comes with glibc

search_entry() {
  if [ -e "$1" ] ; then
    /bin/egrep -v "^#" $1 | /bin/sed 's/#.*$//' | /bin/egrep "${string}" | /bin/sed -n 1p
    retval=$?
    [ "$retval" = 0 ] || retval=2
  else
    retval=2
  fi
}

if [ -z "$1" ] ; then
  echo "getent: wrong number of arguments" 1>&2
  exit 1
fi

file="/etc/$1"
string="dummy"

if [ ! -f "$file" ] ; then
  echo "Unknown database: $1" 1>&2
  exit 1
fi

#aliases|ethers|group|hosts|netgroup|networks|passwd|protocols|rpc|services|shadow)
# dns based search is not supported for hosts|networks
# ethers|netgroup (not done, needed)?
# it returns only the first match
case $1 in
  passwd)
    string="(^\<$2\>:|^.*:.*:\<$2\>:.*:.*:.*:.*)"
    ;;
  group)
    string="(^|:)\<$2\>:"
    ;;
  shadow)
    string="^\<$2\>:"
    ;;
  aliases)
    if [ -f /etc/postfix/aliases ] ; then
      file="/etc/postfix/aliases"
    elif [ -f /etc/mail/aliases ] ; then
      file="/etc/mail/aliases"
    fi
    string="^\<$2\>:"
    ;;
  networks)
    string="^\<$2\>"
    ;;
  hosts|protocols|rpc|services)
    string="\<$2\>"
    ;;
  *)
    echo "Unknown database: $1"
    exit 1
    ;;
esac

if [ -z "$2" ] ; then
  cat $file
  retval=$?
else
  search_entry "$file" "$2"
fi

exit $retval