summaryrefslogtreecommitdiff
path: root/build-validate-params
blob: 27027bb1b4318e369dfd7835fc8290176f8da9b1 (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
#
# parameter validation functions
#
################################################################
#
# Copyright (c) 2016 SUSE LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################

validate_param=()
validate_buildroot=()

validate_init() {
    local cf="$1"
    if ! test -f "$cf" ; then
	return
    fi
    local _key _flag _arg
    while read _key _flag _arg; do
	case $_key in
	\#* | '')
	    continue ;;
	ALLOW_PARAM:)
	    validate_param[${#validate_param[@]}]="${_flag#--} $_arg"
	    ;;
	ALLOW_BUILD_ROOT:)
	    validate_buildroot[${#validate_buildroot[@]}]="$_flag"
	    ;;
	*)
	    cleanup_and_exit 1 "unknown directive in $cf: $_key"
	    ;;
	  esac
    done < "$cf"
}

validate_param() {
    local param="$1"
    local arg="$2"
    local envvar="$3"

    if test ${#validate_param[@]} -eq 0 ; then
	return
    fi
    param=${param#-}
    param=${param#-}
    local t ok=
    for t in "${validate_param[@]}" ; do
	local targ="${t#$param }"
	if test "$t" != "$targ" ; then
	    if test -z "$targ" -o "$targ" == "$arg" ; then
		ok=true
		break
	    fi
	fi
    done
    if test -z "$ok" ; then
	if test -n "$envvar" ; then
	    cleanup_and_exit 1 "build: environment variable $envvar=$arg not allowed"
	else
	    cleanup_and_exit 1 "build: parameter --$param $arg not allowed"
	fi
    fi
}

validate_buildroot() {
    local br="$1"

    if test ${#validate_buildroot[@]} -eq 0 ; then
	return
    fi
    local user=${SUDO_USER:-$USER}
    local t ok= error="Build root $br not allowed for user $user"
    for t in "${validate_buildroot[@]}" ; do
	t=${t//\%user/$user}
	t=$(readlink -m "$t")
        if [[ "$br" == $t ]] ; then
	    local tp=$(readlink -m "${t%/*}")
	    if test -d "$tp" ; then
		ok=true
		break
	    fi
	    error="Directory $tp does not exist"
	fi
    done
    if test -z "$ok" ; then
	cleanup_and_exit 1 "$error"
    fi
}