-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_hba_rules.sh
executable file
·144 lines (119 loc) · 4.46 KB
/
custom_hba_rules.sh
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
#!/bin/sh
pgm="${0##*/}" # Program basename
progdir="${0%/*}" # Program directory
: ${REALPATH_CMD=$( which realpath )}
: ${SQLITE3_CMD=$( which sqlite3 )}
: ${RM_CMD=$( which rm )}
: ${MKDIR_CMD=$( which mkdir )}
: ${FORM_PATH="/opt/forms"}
: ${distdir="/usr/local/cbsd"}
MY_PATH="$( ${REALPATH_CMD} ${progdir} )"
HELPER="postgresql"
# MAIN
if [ -z "${workdir}" ]; then
[ -z "${cbsd_workdir}" ] && . /etc/rc.conf
[ -z "${cbsd_workdir}" ] && exit 0
workdir="${cbsd_workdir}"
fi
set -e
. ${distdir}/cbsd.conf
. ${subrdir}/tools.subr
. ${subr}
. ${subrdir}/forms.subr
set +e
FORM_PATH="${workdir}/formfile"
[ ! -d "${FORM_PATH}" ] && err 1 "No such ${FORM_PATH}"
###
groupname="hba_rules"
err() {
exitval=$1
shift
echo "$*"
exit $exitval
}
add()
{
local _custom_id=
_custom_id=$( get_custom_id "hba_rules_type" )
if [ -r "${formfile}" ]; then
/usr/local/bin/cbsd ${miscdir}/updatesql ${formfile} ${distsharedir}/forms_yesno.schema purge_truefalse${index}
${SQLITE3_CMD} ${formfile} <<EOF
BEGIN TRANSACTION;
INSERT INTO forms ( mytable,group_id,order_id,param,desc,def,cur,new,mandatory,attr,xattr,type,link,groupname ) VALUES ( 'forms', ${index},${order_id},'hba_rules_type${_custom_id}','type of connection, e.g: "host"','host','','',1, 'maxlen=60', 'dynamic', 'inputbox', '', '${groupname}' );
INSERT INTO forms ( mytable,group_id,order_id,param,desc,def,cur,new,mandatory,attr,xattr,type,link,groupname ) VALUES ( 'forms', ${index},${order_id},'hba_rules_address${_custom_id}','address for host type','0.0.0.0/0','0.0.0.0/0','',1, 'maxlen=60', 'dynamic', 'inputbox', '', '${groupname}' );
INSERT INTO forms ( mytable,group_id,order_id,param,desc,def,cur,new,mandatory,attr,xattr,type,link,groupname ) VALUES ( 'forms', ${index},${order_id},'hba_rules_database${_custom_id}','list of database name(s) to which this rule applies','','','',1, 'maxlen=60', 'dynamic', 'inputbox', '', '${groupname}' );
INSERT INTO forms ( mytable,group_id,order_id,param,desc,def,cur,new,mandatory,attr,xattr,type,link,groupname ) VALUES ( 'forms', ${index},${order_id},'hba_rules_user${_custom_id}','list of user and group name(s) to which this rule applies','','','',1, 'maxlen=60', 'dynamic', 'inputbox', '', '${groupname}' );
INSERT INTO forms ( mytable,group_id,order_id,param,desc,def,cur,new,mandatory,attr,xattr,type,link,groupname ) VALUES ( 'forms', ${index},${order_id},'hba_rules_auth_method${_custom_id}','authentication method, e.g: "password","trust","md5"..','password','password','',1, 'maxlen=60', 'dynamic', 'inputbox', '', '${groupname}' );
INSERT INTO forms ( mytable,group_id,order_id,param,desc,def,cur,new,mandatory,attr,xattr,type,link,groupname ) VALUES ( 'forms', ${index},${order_id},'hba_rules_order${_custom_id}','rule order, integer, eg: 00${_custom_id}','00${_custom_id}','00${_custom_id}','',1, 'maxlen=60', 'dynamic', 'inputbox', '', '${groupname}' );
COMMIT;
EOF
else
/bin/cat <<EOF
BEGIN TRANSACTION;
INSERT INTO forms ( mytable,group_id,order_id,param,desc,def,cur,new,mandatory,attr,type,link,groupname ) VALUES ( 'forms', ${index},${order_id},'hba_rule${_custom_id}','hba_rule part ${_custom_id}','','','',1, 'maxlen=60', 'inputbox', '', '${groupname}' );
COMMIT;
EOF
fi
}
del()
{
if [ -r '${formfile}' ]; then
${SQLITE3_CMD} ${formfile} <<EOF
BEGIN TRANSACTION;
DELETE FROM forms WHERE group_id = '${index}' AND groupname = '${groupname}';
COMMIT;
EOF
else
/bin/cat <<EOF
BEGIN TRANSACTION;
DELETE FROM forms WHERE group_id = '${index}' AND groupname = '${groupname}';
COMMIT;
EOF
fi
}
usage()
{
echo "$0 -a add/remove -i index"
}
get_index()
{
local new_index
[ ! -r "${formfile}" ] && err 1 "formfile not readable: ${formfile}"
new_index=$( ${SQLITE3_CMD} ${formfile} "SELECT group_id FROM forms WHERE groupname = '${groupname}' ORDER BY group_id DESC LIMIT 1" )
case "${action}" in
add|create)
index=$(( new_index + 1 ))
;;
del*|remove)
index=$new_index
;;
esac
[ "${index}" = "0" ] && index=1 # protect ADD custom button
}
while getopts "a:i:f:o:" opt; do
case "$opt" in
a) action="${OPTARG}" ;;
i) index="${OPTARG}" ;;
f) formfile="${OPTARG}" ;;
o) order_id="${OPTARG}" ;;
esac
shift $(($OPTIND - 1))
done
[ -z "${action}" ] && usage
[ -z "${index}" ] && err 1 "${pgm}: empty index"
[ -z "${order_id}" ] && err 1 "${pgm}: empty order_id"
if [ ${index} -eq 1 ]; then
err 1 "${pgm} error: index=0"
fi
case "${action}" in
add|create)
add
;;
del*|remove)
del
;;
*)
echo "Unknown action: must be 'add' or 'del'"
;;
esac
exit 0