-
Notifications
You must be signed in to change notification settings - Fork 14
/
gluster-lic-sign.in
executable file
·188 lines (135 loc) · 3.83 KB
/
gluster-lic-sign.in
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
#!/bin/bash -e
ME=$(basename $0);
function validate_uuid()
{
left=$(echo $1 | sed -r 's/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}//g');
if [ ! -z $left ]; then
echo "FATAL: invalid UUID $1 (line $lineno)";
exit 1
fi
}
function validate_secs()
{
if [ ! -z $(echo $1 | sed 's/[0-9]*//g') ]; then
echo "FATAL: invalid number $1 (line $lineno)";
exit 1;
fi
}
function validate_license_line()
{
set $@;
validate_uuid "$1";
validate_secs "$2";
}
function validate_license_request()
{
filename="$1";
if [ "x$filename" != "x${filename%.lsr}" ]; then
echo "FATAL: invalid filename $filename";
exit 1;
fi
if [ "x$filename" != "x${filename%.asc}" ]; then
echo "FATAL: invalid filename $filename";
exit 1;
fi
echo -n "Validating license request $filename ... ";
if [ ! -f $filename ]; then
echo "FATAL: file does not exist";
exit 1;
fi
if [ $(wc -l $filename | cut -f1 -d' ') -lt 2 ]; then
echo "FATAL: file has fewer than 2 lines";
exit 1;
fi
grep -E '^ *[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}' $filename | while read line; do
validate_license_line "$line";
done;
lineno=$(grep -E '^ *[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}' $filename | wc -l);
echo "done."
}
function validate_num_days()
{
numdays=$1
while [ -z "$numdays" ]; do
echo -n "Please enter the number days to generate the license for: "
read numdays;
done
if [ ! -z $(echo "$numdays" | sed 's/[0-9]*//g') ]; then
echo "FATAL: invalid number of days $1";
exit 1;
fi
if [ $numdays -gt 366 ]; then
cat >&2 <<EOF
================================================================================
WARNING: Generating a license for an unusually high ($numdays) number of days!!
================================================================================
[Hit ENTER to continue]
EOF
read
fi
}
function generate_license()
{
expire=$(($now + ($numdays * 86400)));
lsrfile=${filename%.*}.lsr;
grep -E '^\ *[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}' $filename > $lsrfile;
sed -i -r -e "s/^([^ ]+[ ]+)[^ ]+(.*)\$/\1$expire\2/g" $lsrfile;
expirestr=$(date -u --date="1970-01-01 00:00:00 $expire seconds");
}
function sign_license()
{
ascfile=${lsrfile%.lsr}.asc;
cat $lsrfile | gpg --output $ascfile --clearsign;
cat <<EOF
Generated license file for $lineno hosts expiring on $expirestr -
$ascfile
The above signed license file may be handed back to the requestor.
EOF
}
function show_help()
{
cat <<EOF
Gluster Licensing Tools, Version @PACKAGE_VERSION@.
Usage: $ME [-h] LICENSE-REQ-FILE [NUM-DAYS]
Sign license of Gluster servers. LICENSE-REQ-FILE is a file created
with gluster-lic-request tool on the Gluster cluster. NUM-DAYS is
number of days for which the generated license should be valid from
the time of signing (ie from now).
Miscellaneous:
-h display this help and exit
Example:
$ME license.req
$ME license.req 365
EOF
}
function main()
{
# Parse command line arguments.
while getopts :h OPT; do
case "$OPT" in
h)
show_help
exit 0
;;
\?)
# getopts issues an error message
echo "Invalid option: -$OPTARG"
show_help
exit 1
;;
esac
done
# Remove the switches we parsed above.
shift `expr $OPTIND - 1`
# We want only one or two non-option arguments.
if [ $# -lt 1 -o $# -gt 2 ]; then
show_help
exit 1
fi
now=$(date '+%s');
validate_license_request "$1";
validate_num_days "$2";
generate_license;
sign_license;
}
main "$@";