summaryrefslogtreecommitdiff
path: root/security/pf/pkg-install
blob: a22e2c88c1bb15cb476349e610d3640bf285debf (plain) (blame)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/sh
# an installation script for pf_freebsd copied from Wnn6

check_pw()
{
    if which -s pw; then
	:
    else
	cat <<EOF

This system looks like a pre-2.2 version of FreeBSD.  We see that it
is missing the "pw" utility.  We need this utility.  Please get and
install it, and try again.  You can get the source from:

  ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/src/usr.sbin/pw.tar.gz

EOF
	exit 1
    fi
}

ask() {
    local question default answer

    question=$1
    default=$2
    if [ -z "${PACKAGE_BUILDING}" ]; then
	read -p "${question} (y/n) [${default}]? " answer
    fi
    if [ x${answer} = x ]; then
	answer=${default}
    fi
    echo ${answer}
}

yesno() {
    local dflt question answer

    question=$1
    dflt=$2
    while :; do
	answer=$(ask "${question}" "${dflt}")
	case "${answer}" in
	[Yy]*)		return 0;;
	[Nn]*)		return 1;;
	esac
	echo "Please answer yes or no."
    done
}

check_service() {
    local name number type comment
    
    name=$1
    number=$2
    type=$3
    comment=$4
    
    FILE="/etc/services"
    # check
    OK=no
    HAS_SERVICE=no
    COUNT=1
    for i in `grep $name $FILE `; do
	if [ $COUNT = 1 ] && [ X"$i" = X"$name" ]; then
	    HAS_SERVICE=yes
	elif [ $COUNT = 2 ] && [ $HAS_SERVICE = yes ] && \
		[ X"$i" = X"$number/$type" ]; then
	    OK=yes
	    break
	fi
	COUNT=`expr ${COUNT} + 1`
    done
    # add an entry for SERVICE to /etc/services
    if [ $OK = no ]; then
	echo "This system has no entry for $name in ${FILE}"
	if yesno "Would you like to add it automatically?" y; then
	    mv ${FILE} ${FILE}.bak
	    (grep -v $name ${FILE}.bak ; \
		echo "$name	$number/$type	# $comment") \
		>> ${FILE}
	    rm ${FILE}.bak
	else
	    echo "Please add '$name $number/$type' into ${FILE}, and try again."
	    return 1
	fi
    fi
    return 0
}

check_group() {
    local name id

    name=$1
    id=$2
    #check
    # We need a command 'pw(8)'
    check_pw
    if pw groupshow $name -q ; then
	return 0
    fi
    if pw groupadd -g $id -n $name -N -q ; then 
    	echo ""
    	echo "You need a group '$name' whose ID number is $id"
    	if yesno "Would you like to create it automatically?" y; then
	    pw groupadd -g $id -n $name
	    return 0
	fi
    fi
    echo ""
    echo "I was not able to add group 'proxy:*:62:' as pw reported:"
    pw groupadd -g $id -n $name -N
    echo "Please correct this and try again!"
    echo ""
    return 1
}

check_user() {
    local name id group

    name=$1
    id=$2
    group=$3
    # check
    id_id=`id -u $id 2> /dev/null`
    id_name=`id -u $name 2> /dev/null`
    if [ X"$id_name" = X$id ];then
	return 0
    elif [ X"$id_id" != X ]; then
cat <<EOF

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
This system already has an account whose name is not '$name' and ID
number is $id.

   '`id $id`'

For ftp-proxy in this port or package, ID number of '$name' has to be $id.
Please try again after you delete the account.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
EOF
	exit 1
    elif [ X"$id_name" != X ]; then
	cat <<EOF

There is a user '$name' with ID '$id_name'. I'll try to use this account.

EOF
	return 0
    fi

    # add an account '$name' to this system
    echo ""
    echo "You need an account '$name' whose ID number is $id"
    if yesno "Would you like to create it automatically?" y; then
	# We need a command 'pw(8)'
	check_pw
	pw useradd $name -u $id -g $group -h - -d /nonexistent \
		-s /nonexistent -c "Packet Filter pseudo-user" || exit
    else
	echo "Please create it, and try again."
	return 1
    fi
    return 0
}

case $2 in
PRE-INSTALL)

    if ! check_service ftp-proxy 8021 tcp "# ftp-proxy service port"; then
	exit 1
    fi
    if [ "`grep ftp-proxy /etc/inetd.conf`" ]; then
 	echo "Found ftp-proxy entry in inetd.conf ..."
    else
	echo "Adding sample entry for ftp-proxy to /etc/inetd.conf"
	echo "#ftp-proxy stream tcp nowait root ${PKG_PREFIX}/libexec/ftp-proxy ftp-proxy" >> /etc/inetd.conf
    fi
    if ! check_group proxy 62 ; then 
	exit 1
    fi
    groupid=`pw groupshow proxy | awk \
		'{ split ($1,var,":"); print var[3] }' `
    if ! check_user proxy 62 $groupid; then
	exit 1
    fi
    ;;

esac