Difference between revisions of "Zms-inetd"

From ZoneMinder Wiki
Jump to navigationJump to search
(Script-Version: 2007-09-27)
Line 59: Line 59:
# Inetd-wrapper for ZMS (Zoneminder Streaming Server)
# Inetd-wrapper for ZMS (Zoneminder Streaming Server)
#
#
# Version:      2007-09-27
# Author:      Kurt Zankl <kz@xon.uni.cc>
# Author:      Kurt Zankl <kz@xon.uni.cc>
# Inspiration:  http://www.debian-administration.org/articles/371
# Inspiration:  http://www.debian-administration.org/articles/371
Line 80: Line 81:
# error handler
# error handler
function errormsg {
function errormsg {
   echo "HTTP/1.1 200 OK"
   echo "HTTP/1.0 500 Internal Server Error"
   echo "Content-Type: text/html"
   echo "Content-Type: text/html"
   echo
   echo
   echo "<h1>ERROR</h1>"
   echo "<title>ERROR</title><h1>ERROR</h1>"
   echo -e "<pre>$1</pre>"
   echo -e "<pre>$1</pre>"
  echo
   exit 1
   exit 1
}
}
# get request
read REQUEST
# skip headers
HEADER="nothing"; while [ "$HEADER" != $'\r' -a -n "$HEADER" ]; do read HEADER; done


# read ZM configuration
# read ZM configuration
[ -r $ZMCONF ] || errormsg "Error reading Zoneminder configuration \"$ZMCONF\"";
[ -r $ZMCONF ] || errormsg "Error reading Zoneminder configuration \"$ZMCONF\""
. $ZMCONF
. $ZMCONF
ZMS="$ZM_PATH_CGI/$CGIBIN"
ZMS="$ZM_PATH_CGI/$CGIBIN"
[ -x $ZMS ] || errormsg "Error finding ZMS executable \"$ZMS\"";
[ -x $ZMS ] || errormsg "Error finding ZMS executable \"$ZMS\""


# get request
read REQUEST
# skip headers
HEADER="nothing"; while [ "$HEADER" != $'\r' -a -n "$HEADER" ]; do read HEADER; done
# check request
# check request
[ -z "$REQUEST" ] && errormsg "Request is empty"
[ -z "$REQUEST" ] && errormsg "Request is empty"
Line 112: Line 113:


# execute ZMS
# execute ZMS
# (STDERR output is discarded as this confuses MPEG streaming clients)
export QUERY_STRING="$QUERY"
export QUERY_STRING="$QUERY"
echo "HTTP/1.1 200 OK"
echo "HTTP/1.0 200 OK"
"$ZMS"
"$ZMS" 2>/dev/null
echo
echo
exit 0</nowiki></pre>
exit 0</nowiki></pre>

Revision as of 09:57, 27 September 2007

zms-inetd is an inetd-wrapper for the ZMS (Zoneminder Streaming Server), so ZMS can work "standalone".


Motivation

I'm using Zoneminder with Nginx Httpd, which doesnt support simple CGI (just FastCGI). So I needed a leightweight solution to make ZMS work without any extra software. This script depends only on inetd and bash.

Installation and Configuration

Installation

Just copy and paste the zms-inetd-script from below (Section "Script") into a text file and save it, for example, as /usr/local/bin/zms-inetd. Don't forget to make it executable (chmod 755 /usr/local/bin/zms-inetd).

/etc/services

As for every inetd-service you have to define a port number in /etc/services:

zms-inetd       85/tcp   # Zoneminder ZMS inetd-wrapper

/etc/inetd.conf

Of course also an entry in the inetd configuration is needed. Please adapt this to your needs (user [= www], path):

zms-inetd       stream tcp nowait    www   /usr/local/bin/zms-inetd  zms-inetd

Path to zm.conf

Set the ZMCONF variable in the shell-script appropriate.

Zoneminder-Configuration

The last step is to adjust the web path (URL) to ZMS. This is done through the ZM-Webinterface > Options > Paths > Web path to zms streaming server (ZM_PATH_ZMS). According to the configuration above this would be:

http://<server>:85/<anypath>

It doesn't matter which path you specifiy as zms-inetd will always proxy to the ZMS cgi-binary for security reasons.

Further thoughts

Make it transparent

In combination with Nginx its propably useful not to point ZM directly to the zms-inetd port, but mask it with a reverse proxy configuration in Nginx. That should avoid some problems if you want to reverse proxy the whole ZM installation (eg: from intranet to the internet).

Example:

 location /zm/zms-inetd {
   proxy_pass  http://127.0.0.1:85;
}

Of course you have to adjust the cgi-bin path (ZM_PATH_ZMS) appropriate (eg: /zm/zms-inetd).

Other use cases for zms-inetd

Another scenario where zms-inetd perhaps could also be used is a leightweight or embedded system, just running ZMS. For example the busybox multibinary offers an inetd and a shell. Due the fact that zms-inetd is not heavily integrated with bash it should not be too hard to adapt it to ash (default shell in busybox).

Script

<nowiki>#!/bin/bash
#
# Inetd-wrapper for ZMS (Zoneminder Streaming Server)
#
# Version:      2007-09-27
# Author:       Kurt Zankl <kz@xon.uni.cc>
# Inspiration:  http://www.debian-administration.org/articles/371
# Requirements: bash, inetd
# License:      GNU General Public License, Version 2
#
# /etc/services:
#   zms-inetd       85/tcp   # Zoneminder ZMS inetd-wrapper
#
# /etc/inetd.conf:
#   zms-inetd       stream tcp nowait    www-data   /usr/local/bin/zms-inetd  zms-inetd
#
# ZM Options / Paths / Web path to zms streaming server:
#   ZM_PATH_ZMS = http://<server>:85/<anypath>
#

# configuration
ZMCONF="/usr/local/etc/zm.conf"
CGIBIN="zms"

# error handler
function errormsg {
  echo "HTTP/1.0 500 Internal Server Error"
  echo "Content-Type: text/html"
  echo
  echo "<title>ERROR</title><h1>ERROR</h1>"
  echo -e "<pre>$1

"

 exit 1

}

  1. get request

read REQUEST

  1. skip headers

HEADER="nothing"; while [ "$HEADER" != $'\r' -a -n "$HEADER" ]; do read HEADER; done

  1. read ZM configuration

[ -r $ZMCONF ] || errormsg "Error reading Zoneminder configuration \"$ZMCONF\"" . $ZMCONF ZMS="$ZM_PATH_CGI/$CGIBIN" [ -x $ZMS ] || errormsg "Error finding ZMS executable \"$ZMS\""

  1. check request

[ -z "$REQUEST" ] && errormsg "Request is empty"

  1. split request

URL="${REQUEST#GET }" URL="${URL% HTTP/*}" QUERY="${URL#*\?}" URL="${URL%%\?*}"

  1. check query

[ "$QUERY" == "$URL" ] && errormsg "Invalid query"

  1. execute ZMS
  2. (STDERR output is discarded as this confuses MPEG streaming clients)

export QUERY_STRING="$QUERY" echo "HTTP/1.0 200 OK" "$ZMS" 2>/dev/null echo

exit 0</nowiki>