| 1 |
exec Module |
| 2 |
|
| 3 |
Jiri Kuthan |
| 4 |
|
| 5 |
FhG FOKUS |
| 6 |
|
| 7 |
Daniel-Constantin Mierla |
| 8 |
|
| 9 |
<[email protected]> |
| 10 |
|
| 11 |
Edited by |
| 12 |
|
| 13 |
Jan Janak |
| 14 |
|
| 15 |
Copyright © 2003 FhG FOKUS |
| 16 |
__________________________________________________________________ |
| 17 |
|
| 18 |
Table of Contents |
| 19 |
|
| 20 |
1. Admin Guide |
| 21 |
|
| 22 |
1. Overview |
| 23 |
2. Dependencies |
| 24 |
|
| 25 |
2.1. Kamailio Modules |
| 26 |
2.2. External Libraries or Applications |
| 27 |
|
| 28 |
3. Parameters |
| 29 |
|
| 30 |
3.1. setvars (integer) |
| 31 |
3.2. time_to_kill (integer) |
| 32 |
|
| 33 |
4. Functions |
| 34 |
|
| 35 |
4.1. exec_dset(command) |
| 36 |
4.2. exec_msg(command) |
| 37 |
4.3. exec_avp(command [, avplist]) |
| 38 |
4.4. exec_cmd(command) |
| 39 |
|
| 40 |
5. Known Issues |
| 41 |
|
| 42 |
List of Examples |
| 43 |
|
| 44 |
1.1. Set “setvars” parameter |
| 45 |
1.2. Set “time_to_kill” parameter |
| 46 |
1.3. exec_dset usage |
| 47 |
1.4. exec_msg usage |
| 48 |
1.5. exec_avp usage |
| 49 |
1.6. exec_cmd usage |
| 50 |
|
| 51 |
Chapter 1. Admin Guide |
| 52 |
|
| 53 |
Table of Contents |
| 54 |
|
| 55 |
1. Overview |
| 56 |
2. Dependencies |
| 57 |
|
| 58 |
2.1. Kamailio Modules |
| 59 |
2.2. External Libraries or Applications |
| 60 |
|
| 61 |
3. Parameters |
| 62 |
|
| 63 |
3.1. setvars (integer) |
| 64 |
3.2. time_to_kill (integer) |
| 65 |
|
| 66 |
4. Functions |
| 67 |
|
| 68 |
4.1. exec_dset(command) |
| 69 |
4.2. exec_msg(command) |
| 70 |
4.3. exec_avp(command [, avplist]) |
| 71 |
4.4. exec_cmd(command) |
| 72 |
|
| 73 |
5. Known Issues |
| 74 |
|
| 75 |
1. Overview |
| 76 |
|
| 77 |
The exec module allows external commands to be executed from a Kamailio |
| 78 |
script. The commands may be any valid shell commands--the command |
| 79 |
string is passed to the shell using “popen” command. Kamailio passes |
| 80 |
additional information about the request in environment variables: |
| 81 |
* SIP_HF_<hf_name> contains value of each header field in request. If |
| 82 |
a header field occurred multiple times, values are concatenated and |
| 83 |
comma-separated. <hf_name> is in capital letters. Ff a header-field |
| 84 |
name occurred in compact form, <hf_name> is canonical. |
| 85 |
* SIP_TID is transaction identifier. All request retransmissions or |
| 86 |
CANCELs/ACKs associated with a previous INVITE result in the same |
| 87 |
value. |
| 88 |
* SIP_DID is dialog identifier, which is the same as to-tag. |
| 89 |
Initially, it is empty. |
| 90 |
* SIP_SRCIP is source IP address from which request came. |
| 91 |
* SIP_ORURI is the original request URI. |
| 92 |
* SIP_RURI is current request URI (if unchanged, equal to original). |
| 93 |
* SIP_USER is userpart of current request URI. |
| 94 |
* SIP_OUSER is userpart of original request URI. |
| 95 |
|
| 96 |
NOTE: The environment variables must be specified with double $ (e.g., |
| 97 |
$$SIP_OUSER) in the parameters given to exec functions. Otherwise they |
| 98 |
will be evaluated as Kamailio pseudo-variables, throwing errors. |
| 99 |
|
| 100 |
WARNING: if the exec functions are passed variables that might include |
| 101 |
malicious input, then remote attackers may abuse the exec functions to |
| 102 |
execute arbitrary code. Specifically, this may result in OS command |
| 103 |
injection. In such cases, input validation is required to prevent the |
| 104 |
vulnerability. The following is an example of how input validation and |
| 105 |
exec module functions may be used together to prevent exploitation: |
| 106 |
... |
| 107 |
if !($rU =~ "^[0-9]{1,15}$") { |
| 108 |
xlog("Malformed R-URI username: '$rU'\n"); |
| 109 |
exit; |
| 110 |
} |
| 111 |
exec_msg("echo TEST >> /tmp/$(rU).txt"); |
| 112 |
... |
| 113 |
|
| 114 |
2. Dependencies |
| 115 |
|
| 116 |
2.1. Kamailio Modules |
| 117 |
2.2. External Libraries or Applications |
| 118 |
|
| 119 |
2.1. Kamailio Modules |
| 120 |
|
| 121 |
The following modules must be loaded before this module: |
| 122 |
* No dependencies on other Kamailio modules. |
| 123 |
|
| 124 |
2.2. External Libraries or Applications |
| 125 |
|
| 126 |
The following libraries or applications must be installed before |
| 127 |
running Kamailio with this module loaded: |
| 128 |
* None. |
| 129 |
|
| 130 |
3. Parameters |
| 131 |
|
| 132 |
3.1. setvars (integer) |
| 133 |
3.2. time_to_kill (integer) |
| 134 |
|
| 135 |
3.1. setvars (integer) |
| 136 |
|
| 137 |
Turn off to disable setting environment variables for executed |
| 138 |
commands. |
| 139 |
|
| 140 |
Default value is 1. |
| 141 |
|
| 142 |
Example 1.1. Set “setvars” parameter |
| 143 |
... |
| 144 |
modparam("exec", "setvars", 1) |
| 145 |
... |
| 146 |
|
| 147 |
3.2. time_to_kill (integer) |
| 148 |
|
| 149 |
Specifies the longest time a program is allowed to execute. If the time |
| 150 |
is exceeded, the program is killed. |
| 151 |
|
| 152 |
Default value is 0. |
| 153 |
|
| 154 |
Example 1.2. Set “time_to_kill” parameter |
| 155 |
... |
| 156 |
modparam("exec", "time_to_kill", 20) |
| 157 |
... |
| 158 |
|
| 159 |
4. Functions |
| 160 |
|
| 161 |
4.1. exec_dset(command) |
| 162 |
4.2. exec_msg(command) |
| 163 |
4.3. exec_avp(command [, avplist]) |
| 164 |
4.4. exec_cmd(command) |
| 165 |
|
| 166 |
4.1. exec_dset(command) |
| 167 |
|
| 168 |
Executes an external command. Current URI is passed to the command as |
| 169 |
parameter. Output of the command is considered URI set (separated by |
| 170 |
lines). |
| 171 |
|
| 172 |
Meaning of the parameters is as follows: |
| 173 |
* command - Command to be executed. It can include pseudo-variables; |
| 174 |
|
| 175 |
WARNING: if the var you are passing out has a bash special character in |
| 176 |
it, the var needs to be placed inside quotes, for example: |
| 177 |
exec_dset("print-contact.sh '$ct'"); |
| 178 |
|
| 179 |
This function can be used from REQUEST_ROUTE, FAILURE_ROUTE. |
| 180 |
|
| 181 |
Example 1.3. exec_dset usage |
| 182 |
... |
| 183 |
exec_dset("echo TEST > /tmp/test.txt"); |
| 184 |
exec_dset("echo TEST > /tmp/$(rU).txt"); |
| 185 |
... |
| 186 |
|
| 187 |
4.2. exec_msg(command) |
| 188 |
|
| 189 |
Executes an external command. The whole message is passed to it in |
| 190 |
input, no command-line parameters are added, output of the command is |
| 191 |
not processed. |
| 192 |
|
| 193 |
The “examples” directory in the source tarball contains several |
| 194 |
examples that shows how to use this function. |
| 195 |
|
| 196 |
Meaning of the parameters is as follows: |
| 197 |
* command - Command to be executed. It can include pseudo-variables. |
| 198 |
|
| 199 |
WARNING: if the var you are passing out has a bash special character in |
| 200 |
it, the var needs to be placed inside quotes, for example: |
| 201 |
exec_msg("print-contact.sh '$ct'"); |
| 202 |
|
| 203 |
This function can be used from REQUEST_ROUTE, FAILURE_ROUTE. |
| 204 |
|
| 205 |
Example 1.4. exec_msg usage |
| 206 |
... |
| 207 |
exec_msg("echo TEST > /tmp/test.txt"); |
| 208 |
exec_msg("echo TEST > /tmp/$(rU).txt"); |
| 209 |
... |
| 210 |
|
| 211 |
4.3. exec_avp(command [, avplist]) |
| 212 |
|
| 213 |
Executes an external command. Each line from output of the command is |
| 214 |
saved in an AVP from 'avplist'. If 'avplist' is missing, the AVPs are |
| 215 |
named 1, 2, 3, ... |
| 216 |
|
| 217 |
Meaning of the parameters is as follows: |
| 218 |
* command - Command to be executed. It can include pseudo-variables; |
| 219 |
* avplist - comma separated list with AVP names to store the result |
| 220 |
in; |
| 221 |
|
| 222 |
WARNING: if the var you are passing out has a bash special character in |
| 223 |
it, the var needs to be placed inside quotes, for example: |
| 224 |
exec_avp("print-contact.sh '$ct'"); |
| 225 |
|
| 226 |
This function can be used from REQUEST_ROUTE, FAILURE_ROUTE. |
| 227 |
|
| 228 |
Example 1.5. exec_avp usage |
| 229 |
... |
| 230 |
exec_avp("echo TEST"); |
| 231 |
exec_avp("echo TEST", "$avp(s:test)"); |
| 232 |
... |
| 233 |
|
| 234 |
4.4. exec_cmd(command) |
| 235 |
|
| 236 |
Executes an external command. It is a lightweight version, which does |
| 237 |
not pass the SIP message as parameter, does not set the environment |
| 238 |
variables and it does not use the output of the command. |
| 239 |
|
| 240 |
Meaning of the parameters is as follows: |
| 241 |
* command - Command to be executed. It can include pseudo-variables. |
| 242 |
|
| 243 |
WARNING: if the var you are passing out has a bash special character in |
| 244 |
it, the var needs to be placed inside quotes, for example: |
| 245 |
exec_msg("print-contact.sh '$ct'"); |
| 246 |
|
| 247 |
This function can be used from ANY_ROUTE. |
| 248 |
|
| 249 |
Example 1.6. exec_cmd usage |
| 250 |
... |
| 251 |
exec_cmd("echo TEST > /tmp/test.txt"); |
| 252 |
exec_cmd("echo TEST > /tmp/$(rU).txt"); |
| 253 |
... |
| 254 |
|
| 255 |
5. Known Issues |
| 256 |
|
| 257 |
There is currently no guarantee that scripts ever return and stop |
| 258 |
blocking the SIP server. (There is kill.c but it is not used along with |
| 259 |
the current mechanisms based on popen. Besides that kill.c is ugly). |