summaryrefslogtreecommitdiff
blob: b7de424354b635e5598d7b74409a84938ea9fa04 (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
#!/bin/sh
# This files is part of uClibc. 
# Distributed under the terms of the Lesser GNU General Public License v2
#
# Closely (not perfectly) emulate the behavior of glibc's getent utility
#
#passwd|shadow|group|aliases|hosts|networks|ethers|netgroup|protocols|services|rpc
# only returns the first match (by design)
# dns based search is not supported (hosts,networks)
# case-insensitive matches not supported (ethers; others?)
# may return false-positives (hosts,protocols,rpc,services,ethers)

[ -z "$PATH" ] && PATH="/bin:/usr/bin" || PATH="${PATH}:/bin:/usr/bin"
export PATH

file="/etc/$1"
case $1 in
	passwd|group)
		match="^$2:\|^[^:]*:[^:]*:$2:" ;;
	shadow)
		match="^$2:" ;;
	networks|netgroup)
		match="^[[:space:]]*$2\>" ;;
	hosts|protocols|rpc|services|ethers)
		match="\<$2\>" ;;
	aliases)
		match="^[[:space:]]*$2[[:space:]]*:" ;;
	""|-h|--help)
		echo "USAGE: $0 database [key]"
		exit 0 ;;
	*)
		echo "$0: Unknown database: $1" 1>&2
		exit 1 ;;
esac

if [ ! -f "$file" ] ; then
	echo "$0: Could not find database file for $1" 1>&2
	exit 1
fi

if [ $# -eq 1 ] ; then
	exec cat "$file"
else
	sed "s/#.*//; /$match/q; d" "$file" | grep . || exit 2
fi