summaryrefslogtreecommitdiff
path: root/mk/install/usergroupfuncs.FreeBSD
blob: 1da92949eec8376d6fa4b752338a78e82878a45f (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# $NetBSD: usergroupfuncs.FreeBSD,v 1.4 2006/04/24 23:44:18 jlam Exp $
#
# Platform-specific adduser and addgroup functionality
# on top of pw(8).

# group_exists group [groupid]
#	Returns 0 if $group exists and has gid $groupid
#	Returns 1 if neither $group nor $groupid exist
#	Returns 2 if $group or $groupid exist but don't match
#	Returns 3 for all errors
#	
group_exists()
{
	_group="$1"; _groupid="$2"
	${TEST} -n "$_group" || return 3

	# Check using chgrp to work properly in an NSS/NIS environment.
	_testfile="./grouptest.tmp.$$"
	${ECHO} > $_testfile
	if ${CHGRP} $_group $_testfile >/dev/null 2>&1; then
		# $_group exists
		_id=`${LS} -ln $_testfile 2>/dev/null | ${AWK} '{ print $4 }'`
		${TEST} -n "$_groupid" || _groupid=$_id
		if ${TEST} "$_groupid" = "$_id"; then
			${RM} -f $_testfile; return 0
		fi
		${RM} -f $_testfile; return 2
	elif ${TEST} -z "$_groupid"; then
		# $_group doesn't exist and $_groupid is not set
		return 1
	elif ${CHGRP} $_groupid $_testfile >/dev/null 2>&1; then
		_name=`${LS} -l $_testfile 2>/dev/null | ${AWK} '{ print $4 }'`
		if ${TEST} "$_name" != "$_groupid"; then
			# $_group doesn't exist, but $_groupid exists
			${RM} -f $_testfile; return 2
		fi
		# neither $_group nor $_groupid exist
		${RM} -f $_testfile; return 1
	fi
	${RM} -f $_testfile; return 3
}

# user_exists user [userid]
#	Returns 0 if $user exists and has uid $userid
#	Returns 1 if neither $user nor $userid exist
#	Returns 2 if $user or $userid exist but don't match
#	Returns 3 for all errors
#	
user_exists()
{
	_user="$1"; _userid="$2"
	${TEST} -n "$_user" || return 3

	# Check using chown to work properly in an NSS/NIS environment.
	_testfile="./usertest.tmp.$$"
	${ECHO} > $_testfile
	if ${CHOWN} $_user $_testfile >/dev/null 2>&1; then
		# $_user exists
		_id=`${LS} -ln $_testfile 2>/dev/null | ${AWK} '{ print $3 }'`
		${TEST} -n "$_userid" || _userid=$_id
		if ${TEST} "$_userid" = "$_id"; then
			${RM} -f $_testfile; return 0
		fi
		${RM} -f $_testfile; return 2
	elif ${TEST} -z "$_userid"; then
		# $_user doesn't exist and $_user is not set
		return 1
	elif ${CHOWN} $_userid $_testfile >/dev/null 2>&1; then
		_name=`${LS} -l $_testfile 2>/dev/null | ${AWK} '{ print $3 }'`
		if ${TEST} "$_name" != "$_userid"; then
			# $_user doesn't exist, but $_userid exists
			${RM} -f $_testfile; return 2
		fi
		# neither $_user nor $_userid exist
		${RM} -f $_testfile; return 1
	fi
	${RM} -f $_testfile; return 3
}

# adduser user group [userid] [descr] [home] [shell]
adduser()
{
	user="$1"; group="$2"; userid="$3"
	descr="$4"; home="$5" shell="$6"
	${TEST} $# -eq 6 || return 1
	${TEST} -n "$user" || return 2
	${TEST} -n "$group" || return 2

	PW="@PW@"

	: ${descr:="${PKGNAME%-[0-9]*} $user user"}
	: ${home:="@PKG_USER_HOME@"}
	: ${shell:="@PKG_USER_SHELL@"}

	if ${TEST} -n "${PW}" -a -x "${PW}"; then
		${ECHO} "Creating user: $user"
		case $userid in
		"")
			${PW} useradd					\
				$user					\
				-c "$descr" -d "$home" -s "$shell"	\
				-g $group
			;;
		*)
			${PW} useradd					\
				$user					\
				-c "$descr" -d "$home" -s "$shell"	\
				-g $group -u $userid
			;;
		esac
	fi
	return 0
}

# adduser group [groupid]
addgroup()
{
	group="$1"; groupid="$2"
	${TEST} $# -eq 2 || return 1
	${TEST} -n "$group" || return 2

	PW="@PW@"

	if ${TEST} -n "${PW}" -a -x "${PW}"; then
		${ECHO} "Creating group: $group";
		case $groupid in
		"")	${PW} groupadd $group ;;
		*)	${PW} groupadd $group -g $groupid ;;
		esac
	fi
	return 0
}