File: openserctl.base

package info (click to toggle)
openser 1.1.0-9etch1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 9,828 kB
  • ctags: 11,809
  • sloc: ansic: 120,528; sh: 5,249; yacc: 1,716; makefile: 1,261; php: 656; perl: 205; sql: 190
file content (250 lines) | stat: -rw-r--r-- 5,027 bytes parent folder | download
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/bin/sh 
#
# $Id: openserctl.base,v 1.1 2006/07/04 17:49:58 miconda Exp $
#
# sc: openser control; tool for maintaining openser
#
#===================================================================

##### ----------------------------------------------- #####
### path to useful tools
if [ -z "$EGREP" ] ; then
	EGREP="egrep"
fi
if [ -z "$AWK" ] ; then
	AWK="awk"
fi
if [ -z "$MD5" ]; then
	MD5="md5sum"
fi
if [ -z "$LAST_LINE" ] ; then
	LAST_LINE='tail -1'
fi

##### ------------------------------------------------ #####
### configuration for starting/stopping openser
if [ -z "$PID_FILE" ] ; then
	PID_FILE=/var/run/openser.pid
fi
if [ -z "$SYSLOG" ] ; then
	SYSLOG=1 # 0=output to console, 1=output to syslog
fi
if [ -z "$STARTOPTIONS" ] ; then
	STARTOPTIONS= # for example -dddd
fi
if [ -z "$DIR" ] ; then
	DIR=`dirname $0`
fi
if [ -z "$OSERBIN" ] ; then
	OSERBIN=$DIR/openser
fi

##### ------------------------------------------------ #####
### aliases configuration
#
ENABLE_ALIASES=0
if [ "$ALIASES_TYPE" = "UL" ] ; then
	ENABLE_ALIASES=1
else
	if [ "$ALIASES_TYPE" = "DB" ] ; then
		ENABLE_ALIASES=2
	fi
fi


##### ----------------------------------------------- #####
#### Defined values
ALL_METHODS=4294967295

##### ----------------------------------------------- #####
#### Common functions

usage_base() {
	echo
	mecho " -- command 'start|stop|restart'"
	echo
cat <<EOF
 restart ............................ restart OpenSER
 start .............................. start OpenSER
 stop ............................... stop OpenSER
EOF
}
USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_base"

mdbg() {
	if [ "0$VERBOSE" -ne 0 ] ; then
		if [ -t 1 -a -z "$NOHLPRINT" ] ; then
			echo -e "\033[1m$1\033[0m"
		else
			echo "$1"
		fi
	fi
}

mwarn() {
	if [ -t 1 -a -z "$NOHLPRINT" ] ; then
		echo -e '\E[37;32m'"\033[1mWARINIG: $1\033[0m"
	else
		echo "** WARINIG: $1"
	fi
}

minfo() {
	if [ -t 1 -a -z "$NOHLPRINT" ] ; then
		echo -e '\E[37;33m'"\033[1mINFO: $1\033[0m"
	else
		echo "** INFO: $1"
	fi
}

mecho() {
	if [ -t 1 -a -z "$NOHLPRINT" ] ; then
		echo -e "\033[1m$1\033[0m"
	else
		echo "$1"
	fi
}

merr() {
	if [ -t 1 -a -z "$NOHLPRINT" ] ; then
		echo -e '\E[37;31m'"\033[1mERROR: $1\033[0m"
	else
		echo "** ERROR: $1"
	fi
}


# determine host name, typically for use in printing UAC
# messages; we use today a simplistic but portable uname -n way --
# no domain name is displayed ; fifo_uac expands !! to host
# address only for optional header fields; uname output without
# domain is sufficient for informational header fields such as
# From
#
get_my_host() {
	if [ -z "$SIP_DOMAIN" ]; then
		uname -n
	else
		echo "$SIP_DOMAIN"
	fi
}

# calculate name and domain of current user
set_user() {

	OSERUSER=`echo $1|$AWK -F @ '{print $1}'`
	OSERDOMAIN=`echo $1|$AWK -F @ '{print $2}'`

	if [ -z "$OSERDOMAIN" ] ; then
		OSERDOMAIN="$SIP_DOMAIN"
	fi

	if [ -z "$OSERDOMAIN" ] ; then
		merr "domain unknown: use usernames with domain or set default domain \
in SIP_DOMAIN"
		exit 1
	fi
}


# check the parameter if it is a valid address of record (user@domain)
check_aor() {
	echo "$1" | $EGREP "^[a-zA-Z0-9_\.]+@.*\..*" >/dev/null
	if [ $? -ne 0 ] ; then 
		echo "error: invalid AoR: $1" > /dev/stderr
		exit 1
	fi
}

# check the parameter if it is a valid address of record (user@domain)
is_aor() {
	echo "$1" | $EGREP "^[a-zA-Z0-9_\.]+@.*\..*" >/dev/null
	if [ $? -ne 0 ] ; then 
		false
	else
		true
	fi
}

# check the parameter if it is a valid SIP address of record (sip:user@domain)
check_sipaor() {
	echo "$1" | $EGREP "^sip(s)?:[a-zA-Z0-9_\.]+@.*\..*" >/dev/null
	if [ $? -ne 0 ] ; then 
		echo "error: invalid SIP AoR: $1" > /dev/stderr
		exit 1
	fi
}

# check the parameter if it is a valid SIP URI
# quite simplified now -- it captures just very basic
# errors
check_uri() {
	echo "$1" | $EGREP "^sip(s)?:([a-zA-Z0-9_]+@)?.*\..*"  > /dev/null
	if [ $? -ne 0 ] ; then 
		echo "error: invalid SIP URI: $1" > /dev/stderr
		exit 1
	fi
}

print_status() {
	echo $1 | grep "^[1-6][0-9][0-9]" > /dev/null
	if [ "$?" -eq 0 ] ; then 
		echo $1
	else
		echo "200 OK"
	fi
}

# process output from FIFO/Unixsock server; if everything is ok
# skip the first "ok" line and proceed to returned 
# parameters
filter_fl()
{
#	tail +2
	
	$AWK 'BEGIN {line=0;IGNORECASE=1;}
		{line++}
		line==1 && /^200 ok/ { next }
		{ print }'
}

# params: user, realm, password
# output: HA1
_gen_ha1()
{
	HA1=`echo -n "$1:$2:$3" | $MD5 | $AWK '{ print $1 }'`
	if [ $? -ne 0 ] ; then
		echo "HA1 calculation failed"
		exit 1
	fi
}

# params: user, realm, password
# output: HA1B
_gen_ha1b()
{
	HA1B=`echo -n "$1@$2:$2:$3" | $MD5 | $AWK '{ print $1 }'`
	if [ $? -ne 0 ] ; then
		echo "HA1B calculation failed"
		exit 1
	fi
}

# params: user, realm, password
# output: PHPLIB_ID
_gen_phplib_id()
{
	NOW=`date`;
	PHPLIB_ID=`echo -n "$1$2:$3:$NOW" | $MD5 | $AWK '{ print $1 }'`
}

# params: user, password
# output: HA1, HA1B
credentials()
{
	set_user $1
	_gen_ha1 "$OSERUSER" "$OSERDOMAIN" "$2"
	_gen_ha1b "$OSERUSER" "$OSERDOMAIN" "$2"
}