44 #include <sys/signal.h>
62 #define DEFAULT_SESSION_LIMIT 100
63 #define DEFAULT_SESSION_INACTIVITY 30000
65 #define DEFAULT_HTTP_PORT 8088
66 #define DEFAULT_HTTPS_PORT 8089
69 #if defined(HAVE_OPENSSL) && (defined(HAVE_FUNOPEN) || defined(HAVE_FOPENCOOKIE))
89 .name =
"http server",
99 .name =
"https server",
115 {
"png",
"image/png" },
116 {
"xml",
"text/xml" },
117 {
"jpg",
"image/jpeg" },
118 {
"js",
"application/x-javascript" },
119 {
"wav",
"audio/x-wav" },
120 {
"mp3",
"audio/mpeg" },
121 {
"svg",
"image/svg+xml" },
122 {
"svgz",
"image/svg+xml" },
123 {
"gif",
"image/gif" },
124 {
"html",
"text/html" },
125 {
"htm",
"text/html" },
126 {
"css",
"text/css" },
127 {
"cnf",
"text/plain" },
128 {
"cfg",
"text/plain" },
129 {
"bin",
"application/octet-stream" },
130 {
"sbn",
"application/octet-stream" },
131 {
"ld",
"application/octet-stream" },
186 for (v = cookies; v; v = v->
next) {
187 if (!strcasecmp(v->
name,
"mansession_id")) {
188 sscanf(v->
value,
"%30x", &mngid);
218 char timebuf[80], etag[23];
220 int not_modified = 0;
223 ast_http_error(ser, 501,
"Not Implemented",
"Attempt to use unimplemented / unsupported method");
234 if ((uri[0] < 33) || strchr(
"./|~@#$%^&*() \t", uri[0])) {
238 if (strstr(uri,
"/..")) {
242 if ((ftype = strrchr(uri,
'.'))) {
247 snprintf(wkspace,
sizeof(wkspace),
"text/%s",
S_OR(ftype,
"plain"));
258 if (stat(path, &st)) {
262 if (S_ISDIR(st.st_mode)) {
270 fd = open(path, O_RDONLY);
276 snprintf(etag,
sizeof(etag),
"\"%ld\"", (
long)st.st_mtime);
279 tv.tv_sec = st.st_mtime;
284 for (v = headers; v; v = v->
next) {
285 if (!strcasecmp(v->
name,
"If-None-Match")) {
286 if (!strcasecmp(v->
value, etag)) {
298 ast_str_set(&http_header, 0,
"Content-type: %s\r\n"
300 "Last-Modified: %s\r\n",
307 ast_http_send(ser, method, 304,
"Not Modified", http_header, NULL, 0, 1);
309 ast_http_send(ser, method, 200, NULL, http_header, NULL, fd, 1);
315 ast_http_error(ser, 404,
"Not Found",
"The requested URL was not found on this server.");
319 ast_http_error(ser, 403,
"Access Denied",
"You do not have permission to access the requested URL.");
332 ast_http_error(ser, 501,
"Not Implemented",
"Attempt to use unimplemented / unsupported method");
341 "<title>Asterisk HTTP Status</title>\r\n"
342 "<body bgcolor=\"#ffffff\">\r\n"
343 "<table bgcolor=\"#f1f1f1\" align=\"center\"><tr><td bgcolor=\"#e0e0ff\" colspan=\"2\" width=\"500\">\r\n"
344 "<h2> Asterisk™ HTTP Status</h2></td></tr>\r\n");
346 ast_str_append(&out, 0,
"<tr><td><i>Prefix</i></td><td><b>%s</b></td></tr>\r\n", prefix);
347 ast_str_append(&out, 0,
"<tr><td><i>Bind Address</i></td><td><b>%s</b></td></tr>\r\n",
349 ast_str_append(&out, 0,
"<tr><td><i>Bind Port</i></td><td><b>%s</b></td></tr>\r\n",
352 ast_str_append(&out, 0,
"<tr><td><i>SSL Bind Port</i></td><td><b>%s</b></td></tr>\r\n",
355 ast_str_append(&out, 0,
"<tr><td colspan=\"2\"><hr></td></tr>\r\n");
356 for (v = get_vars; v; v = v->
next) {
357 ast_str_append(&out, 0,
"<tr><td><i>Submitted GET Variable '%s'</i></td><td>%s</td></tr>\r\n", v->
name, v->
value);
359 ast_str_append(&out, 0,
"<tr><td colspan=\"2\"><hr></td></tr>\r\n");
362 for (v = cookies; v; v = v->
next) {
367 ast_str_append(&out, 0,
"</table><center><font size=\"-1\"><i>Asterisk and Digium are registered trademarks of Digium, Inc.</i></font></center></body>\r\n");
374 .description =
"Asterisk HTTP General Status",
383 .description =
"Asterisk HTTP Static Delivery",
394 enum ast_http_method method,
int status_code,
const char *status_title,
396 unsigned int static_content)
401 int content_length = 0;
403 if (!ser || 0 == ser->
f) {
415 content_length += lseek(fd, 0, SEEK_END);
416 lseek(fd, 0, SEEK_SET);
420 fprintf(ser->
f,
"HTTP/1.1 %d %s\r\n"
421 "Server: Asterisk/%s\r\n"
423 "Connection: close\r\n"
425 "Content-Length: %d\r\n"
428 status_code, status_title ? status_title :
"OK",
431 static_content ?
"" :
"Cache-Control: no-cache, no-store\r\n",
447 while ((len = read(fd, buf,
sizeof(buf))) > 0) {
448 if (fwrite(buf, len, 1, ser->
f) != 1) {
469 const unsigned long nonce,
const unsigned long opaque,
int stale,
475 if (!http_headers || !out) {
482 "WWW-authenticate: Digest algorithm=MD5, realm=\"%s\", nonce=\"%08lx\", qop=\"auth\", opaque=\"%08lx\"%s\r\n"
483 "Content-type: text/html\r\n",
484 realm ? realm :
"Asterisk",
487 stale ?
", stale=true" :
"");
490 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
492 "<title>401 Unauthorized</title>\r\n"
494 "<h1>401 Unauthorized</h1>\r\n"
497 "<address>Asterisk Server</address>\r\n"
498 "</body></html>\r\n",
511 if (!http_headers || !out) {
517 ast_str_set(&http_headers, 0,
"Content-type: text/html\r\n");
520 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
522 "<title>%d %s</title>\r\n"
527 "<address>Asterisk Server</address>\r\n"
528 "</body></html>\r\n",
529 status_code, status_title, status_title, text);
547 int len = strlen(urih->
uri);
586 if (!strcmp(urih->
key, key)) {
609 for (t = s; *t; t++) {
618 #define MAX_POST_CONTENT 1025
627 int content_length = 0;
632 for (v = headers; v; v = v->
next) {
633 if (!strcasecmp(v->
name,
"Content-Type")) {
634 if (strcasecmp(v->
value,
"application/x-www-form-urlencoded")) {
641 for (v = headers; v; v = v->
next) {
642 if (!strcasecmp(v->
name,
"Content-Length")) {
643 content_length = atoi(v->
value);
648 if (content_length <= 0) {
653 ast_log(
LOG_WARNING,
"Excessively long HTTP content. %d is greater than our max of %d\n",
664 res = fread(buf, 1, content_length, ser->
f);
665 if (res < content_length) {
670 buf[content_length] =
'\0';
672 while ((val =
strsep(&buf,
"&"))) {
706 ast_debug(2,
"HTTP Request URI is %s \n", uri);
713 while ((val =
strsep(¶ms,
"&"))) {
735 if (!strcasecmp(uri, redirect->
target)) {
738 ast_http_send(ser, method, 302,
"Moved Temporarily", http_header, NULL, 0, 0);
750 if (!strncasecmp(uri, prefix, l) && uri[l] ==
'/') {
755 ast_debug(2,
"match request [%s] with handler [%s] len %d\n", uri, urih->
uri, l);
756 l = strlen(urih->
uri);
758 if (strncasecmp(urih->
uri, uri, l)
759 || (*c && *c !=
'/')) {
773 res = urih->
callback(ser, urih, uri, method, get_vars, headers);
775 ast_http_error(ser, 404,
"Not Found",
"The requested URL was not found on this server.");
784 #if defined(HAVE_FUNOPEN)
788 #define HOOK_T ssize_t
834 while ((cur =
strsep(&parse,
";"))) {
851 ast_debug(1,
"HTTP Cookie, Name: '%s' Value: '%s'\n", name, val);
866 for (v = headers; v; v = v->
next) {
867 if (!strcasecmp(v->
name,
"Cookie")) {
876 #define MAX_HTTP_REQUEST_HEADERS 100
881 char header_line[4096];
887 int remaining_headers;
898 p = getprotobyname(
"tcp");
901 if( setsockopt(ser->
fd, p->p_proto, TCP_NODELAY, (
char *)&arg,
sizeof(arg) ) < 0 ) {
906 ast_log(
LOG_WARNING,
"Failed to set TCP_NODELAY on HTTP connection, getprotobyname(\"tcp\") failed\n");
911 flags = fcntl(ser->
fd, F_GETFL);
913 fcntl(ser->
fd, F_SETFL, flags);
920 if (!fgets(buf,
sizeof(buf), ser->
f) || feof(ser->
f)) {
931 if (!strcasecmp(method,
"GET")) {
933 }
else if (!strcasecmp(method,
"POST")) {
935 }
else if (!strcasecmp(method,
"HEAD")) {
937 }
else if (!strcasecmp(method,
"PUT")) {
960 if (!fgets(header_line,
sizeof(header_line), ser->
f) || feof(ser->
f)) {
973 name =
strsep(&value,
":");
985 if (!remaining_headers--) {
987 ast_http_error(ser, 413,
"Request Entity Too Large",
"Too many headers");
1035 unsigned int target_len;
1036 unsigned int total_len;
1040 target =
strsep(&dest,
" ");
1042 target =
strsep(&target,
" ");
1049 target_len = strlen(target) + 1;
1050 total_len =
sizeof(*redirect) + target_len + strlen(dest) + 1;
1052 if (!(redirect =
ast_calloc(1, total_len))) {
1055 redirect->
dest = redirect->
target + target_len;
1056 strcpy(redirect->
target, target);
1057 strcpy(redirect->
dest, dest);
1089 int newenablestatic=0;
1095 struct sockaddr_in tmp = {0,};
1096 struct sockaddr_in tmp2 = {0,};
1097 int http_tls_was_enabled = 0;
1106 tmp.sin_family = AF_INET;
1139 for (; v; v = v->
next) {
1142 if (strcasecmp(v->
name,
"tlscafile")
1143 && strcasecmp(v->
name,
"tlscapath")
1144 && strcasecmp(v->
name,
"tlscadir")
1145 && strcasecmp(v->
name,
"tlsverifyclient")
1146 && strcasecmp(v->
name,
"tlsdontverifyserver")
1147 && strcasecmp(v->
name,
"tlsclientmethod")
1148 && strcasecmp(v->
name,
"sslclientmethod")
1149 && strcasecmp(v->
name,
"tlscipher")
1150 && strcasecmp(v->
name,
"sslcipher")
1155 if (!strcasecmp(v->
name,
"enabled")) {
1157 }
else if (!strcasecmp(v->
name,
"enablestatic")) {
1159 }
else if (!strcasecmp(v->
name,
"bindport")) {
1162 }
else if (!strcasecmp(v->
name,
"bindaddr")) {
1166 memcpy(&tmp.sin_addr, hp->h_addr,
sizeof(tmp.sin_addr));
1172 }
else if (!strcasecmp(v->
name,
"prefix")) {
1177 newprefix[0] =
'\0';
1179 }
else if (!strcasecmp(v->
name,
"redirect")) {
1181 }
else if (!strcasecmp(v->
name,
"sessionlimit")) {
1187 }
else if (!strcasecmp(v->
name,
"session_inactivity")) {
1203 if (!tmp2.sin_addr.s_addr) {
1204 tmp2.sin_addr = tmp.sin_addr;
1206 if (!tmp2.sin_port) {
1214 if (strcmp(prefix, newprefix)) {
1217 enablestatic = newenablestatic;
1233 struct sockaddr_in tmp;
1237 e->
command =
"http show status";
1239 "Usage: http show status\n"
1240 " Lists status of internal HTTP engine\n";
1249 ast_cli(a->
fd,
"HTTP Server Status:\n");
1252 if (!tmp.sin_family) {
1255 ast_cli(a->
fd,
"Server Enabled and Bound to %s:%d\n\n",
1259 ast_cli(a->
fd,
"HTTPS Server Enabled and Bound to %s:%d\n\n",
1261 ntohs(tmp.sin_port));
1275 ast_cli(a->
fd,
"\nEnabled Redirects:\n");
static char * ast_sockaddr_stringify_addr(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return an address only.
#define DEFAULT_HTTP_PORT
struct http_uri_redirect::@275 entry
static struct ast_variable * parse_cookies(const char *cookies)
#define AST_CLI_DEFINE(fn, txt,...)
Asterisk main include file. File version handling, generic pbx functions.
char * strsep(char **str, const char *delims)
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized...
void ast_http_error(struct ast_tcptls_session_instance *ser, int status, const char *title, const char *text)
Send HTTP error message and close socket.
ast_http_callback callback
void ast_tcptls_server_start(struct ast_tcptls_session_args *desc)
This is a generic (re)start routine for a TCP server, which does the socket/bind/listen and starts a ...
String manipulation functions.
int ast_ssl_setup(struct ast_tls_config *cfg)
Set up an SSL server.
#define AST_RWLIST_INSERT_AFTER
#define DEFAULT_HTTPS_PORT
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Asterisk version information.
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
void ast_http_auth(struct ast_tcptls_session_instance *ser, const char *realm, const unsigned long nonce, const unsigned long opaque, int stale, const char *text)
Send http "401 Unauthorized" response and close socket.
const char * ast_get_version(void)
Retrieve the Asterisk version string.
void ast_uri_decode(char *s)
Decode URI, URN, URL (overwrite string)
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
descriptor for a cli entry.
static struct ast_cli_entry cli_http[]
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category)
Goes through variables.
int ast_http_uri_link(struct ast_http_uri *urihandler)
Register a URI handler.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
const char * ast_http_ftype2mtype(const char *ftype) attribute_pure
Return mime type based on extension.
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
#define DEFAULT_SESSION_LIMIT
Structure for variables, used for configurations and for channel variables.
static struct ast_cfhttp_methods_text ast_http_methods_text[]
static struct ast_http_uri staticuri
void ast_http_uri_unlink(struct ast_http_uri *urihandler)
Unregister a URI handler.
Configuration File Parser.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
struct ast_str * ast_str_create(size_t init_len)
Create a malloc'ed dynamic length string.
arguments for the accepting thread
uint32_t ast_http_manid_from_vars(struct ast_variable *headers) attribute_pure
Return manager id, if exist, from request headers.
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Generic support for tcp/tls servers in Asterisk.
void ast_cli(int fd, const char *fmt,...)
struct ast_config * ast_config_load2(const char *filename, const char *who_asked, struct ast_flags flags)
Load a config file.
void ast_config_destroy(struct ast_config *config)
Destroys a config.
char * ast_skip_nonblanks(const char *str)
Gets a pointer to first whitespace character in a string.
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return * the previous value of *p. This can be used to handle reference co...
String fields in structures.
static void ast_sockaddr_setnull(struct ast_sockaddr *addr)
Sets address addr to null.
static int httpstatus_callback(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_vars, struct ast_variable *headers)
#define CONFIG_STATUS_FILEMISSING
void ast_http_send(struct ast_tcptls_session_instance *ser, enum ast_http_method method, int status_code, const char *status_title, struct ast_str *http_header, struct ast_str *out, const int fd, unsigned int static_content)
Generic function for sending http/1.1 response.
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
static char * ast_sockaddr_stringify_port(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return a port only.
Support for Private Asterisk HTTP Servers.
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
#define AST_RWLIST_INSERT_HEAD
char * ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes)
Strip leading/trailing whitespace and quotes from a string.
#define ast_debug(level,...)
Log a DEBUG message.
int astman_is_authed(uint32_t ident)
Determinie if a manager session ident is authenticated.
void * ast_tcptls_server_root(void *)
int ast_http_reload(void)
int ast_parse_arg(const char *arg, enum ast_parse_flags flags, void *result,...)
The argument parsing routine.
Asterisk file paths, configured in asterisk.conf.
#define ast_sockaddr_from_sin(addr, sin)
Converts a struct sockaddr_in to a struct ast_sockaddr.
#define AST_PTHREADT_NULL
static force_inline int attribute_pure ast_strlen_zero(const char *s)
#define AST_RWLIST_TRAVERSE
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
#define ao2_ref(o, delta)
int ast_register_atexit(void(*func)(void))
Register a function to be executed before Asterisk exits.
static struct ast_tls_config http_tls_cfg
#define AST_RWLIST_REMOVE_CURRENT
void ast_tcptls_stream_set_exclusive_input(struct ast_tcptls_stream *stream, int exclusive_input)
Set the TCP/TLS stream I/O if it can exclusively depend upon the set timeouts.
struct ast_tcptls_stream * stream_cookie
static struct ast_tcptls_session_args http_desc
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Wrapper for network related headers, masking differences between various operating systems...
static void * httpd_helper_thread(void *arg)
const char * ast_config_AST_DATA_DIR
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
#define ast_strdupa(s)
duplicate a string in memory from the stack
static int __ast_http_load(int reload)
static void add_redirect(const char *value)
Add a new URI redirect The entries in the redirect list are sorted by length, just like the list of U...
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
#define ast_sockaddr_set_port(addr, port)
Sets the port number of a socket address.
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static struct @274 mimetypes[]
Limit the kinds of files we're willing to serve up.
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
char * ast_trim_blanks(char *str)
Trims trailing whitespace characters from a string.
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
void ast_tcptls_stream_set_timeout_inactivity(struct ast_tcptls_stream *stream, int timeout)
Set the TCP/TLS stream inactivity timeout timer.
static void http_shutdown(void)
static void parse(struct mgcp_request *req)
const char * ast_inet_ntoa(struct in_addr ia)
thread-safe replacement for inet_ntoa().
static struct ast_http_uri statusuri
static int session_inactivity
#define AST_RWLIST_REMOVE_HEAD
Prototypes for public functions only of internal interest,.
const char * ast_get_http_method(enum ast_http_method method) attribute_pure
Return http method name string.
struct ast_sockaddr old_address
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
#define DEFAULT_SESSION_INACTIVITY
static void * cleanup(void *unused)
Structure used to handle boolean flags.
static int handle_uri(struct ast_tcptls_session_instance *ser, char *uri, enum ast_http_method method, struct ast_variable *headers)
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
#define AST_RWLIST_INSERT_TAIL
struct hostent * ast_gethostbyname(const char *host, struct ast_hostent *hp)
Thread-safe gethostbyname function to use in Asterisk.
Standard Command Line Interface.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
void ast_tcptls_close_session_file(struct ast_tcptls_session_instance *tcptls_session)
Closes a tcptls session instance's file and/or file descriptor. The tcptls_session will be set to NUL...
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
Register multiple commands.
Definition of a URI handler.
static char * handle_show_http(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
void ast_http_prefix(char *buf, int len)
Return the current prefix.
static int static_callback(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_vars, struct ast_variable *headers)
struct ast_variable * ast_http_get_post_vars(struct ast_tcptls_session_instance *ser, struct ast_variable *headers)
Get post variables from client Request Entity-Body, if content type is application/x-www-form-urlenco...
int ast_tls_read_conf(struct ast_tls_config *tls_cfg, struct ast_tcptls_session_args *tls_desc, const char *varname, const char *value)
Used to parse conf files containing tls/ssl options.
void ast_http_uri_unlink_all_with_key(const char *key)
Unregister all handlers with matching key.
static void http_decode(char *s)
#define AST_RWLIST_REMOVE
#define ast_sockaddr_to_sin(addr, sin)
Converts a struct ast_sockaddr to a struct sockaddr_in.
#define MAX_HTTP_REQUEST_HEADERS
struct ast_variable * next
void ast_tcptls_server_stop(struct ast_tcptls_session_args *desc)
Shutdown a running server if there is one.
struct ast_tls_config * tls_cfg
struct ast_sockaddr local_address
#define CONFIG_STATUS_FILEINVALID
ast_http_method
HTTP Request methods known by Asterisk.
static struct hostent * hp
static struct ast_tcptls_session_args https_desc
#define AST_RWLIST_TRAVERSE_SAFE_END
struct ast_variable * ast_variable_new(const char *name, const char *value, const char *filename)
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
struct ast_variable * ast_http_get_cookies(struct ast_variable *headers)
Get cookie from Request headers.
#define CONFIG_STATUS_FILEUNCHANGED
static char prefix[MAX_PREFIX]