# &adminguide;

## Overview

This module provides an XCAP server functionally inside Kamailio and SER SIP servers.

Benefits brought by this integrated XCAP server:

- reuse of SIP router transport layer - XCAP documents can be sent via SIP (UDP, TCP, TLS and SCTP) and via HTTP (TCP or TLS (HTTPS)). For HTTP/S, you need to load XHTTP module to handle HTTP/S requests.

- the Presence server has access immediately to the latest version of XCAP documents. No more need to trigger refresh of XCAP documents via RPC command.

- can be used stand-alone, with a different Presence server. It is not specific for Kamailio or SER. Documents can be fetched via GET

- no exotic dependencies, it is written in C. It depends on libxml2, sl module and a database module (required to store the xcap documents).

- you can do digest authentication using database, radius, ldap, etc. Can reuse authorization mechanisms provided by SIP server.

- flexibility - the XCAP server is controlled from config file of SIP server, therefore you can blend the XCAP logic with features provided by core or other modules.

Important: be sure you have global parameter: 'tcp_accept_no_cl=yes'.

## Dependencies

### &kamailio; Modules

The following modules must be loaded before this module:

- sl - stateless reply module

- db - a database engine module

- xhttp - embedded HTTP server if you want to get XCAP documents via HTTP.

### External Libraries or Applications

The following libraries or applications must be installed before running &kamailio; with this module loaded:

- libxml2 - libxml2 library for runtime and libxml2-dev for compilation.

## Parameters

### `db_url` (string)

Database &url;.

Default value is &defaultdb;.

#### Set the db_url parameter

    ...
    modparam("xcap_server", "db_url", "mysql://user:passwd@host.com/dbname")
    ...

### `xcap_table` (string)

The name of table where to store the xcap documents.

Default value is xcap.

#### Set the xcap_table parameter

    ...
    modparam("xcap_server", "xcap_table", "xcapdocs")
    ...

### `xcap_root` (str)

XCAP root URL.

Default value is '/xcap-root/'.

#### Set `xcap_root` parameter

    ...
    modparam("xcap_server", "xcap_root", "/xcap-root/")
    ...

### `buf_size` (int)

Size of local buffer for handling XCAP documents.

Default value is 1024.

#### Set the buf_size parameter

    ...
    modparam("xcap_server", "buf_size", 2048)
    ...

### `xml_ns` (str)

Register extra XML namespaces to be used with XPath. You can set the parameter many times to add more namespaces. The format is 'prefix=href'.

Default value is 'null'.

#### Set `xml_ns` parameter

    ...
    modparam("xcap_server", "xml_ns",
        "rl=urn:ietf:params:xml:ns:resource-lists")
    modparam("xcap_server", "xml_ns",
        "my=urn:my:prefix")
    ...

### `directory_scheme` (int)

Allows the scheme used in org.openmobilealliance.xcap-directory listings to be set to a specific value.

The URLs in an org.oma.xcap-directory listing must be be paths a client can use to download the listed XCAP documents. In some cases (for example, when the XCAP server is accessed using HTTP from an HTTP proxy the client connected to using HTTPS) the XCAP server may not be able to automatically determine the correct scheme to use based on the HTTP connection.

- -1 (default) - determine scheme automatically. HTTP for TCP connections and HTTPS for TLS connections.

- 0 - use the HTTP scheme.

- 1 - use the HTTPS scheme.

#### Set `directory_scheme` parameter

    ...
    modparam("xcap_server", "directory_scheme", 0)
    ...

### `directory_hostname` (string)

Allows the hostname (and port) used in org.openmobilealliance.xcap-directory listings to be set to a specific value.

The URLs in an org.oma.xcap-directory listing must be be paths a client can use to download the listed XCAP documents. In some cases (for example, when the XCAP server is accessed through a proxy that re-writes headers, or a client is non-conforming and does not include a Host: header) the XCAP server may not be able to automatically determine the correct hostname to use.

When this parameter is not set the XCAP server will attempt to use the contents of the (mandatory) Host: header. If the Host: header is not present the XCAP server will use the IP address and port the XCAP request was received on.

#### Set `directory_hostname` parameter

    ...
    modparam("xcap_server", "directory_hostname", "xcap.example.com")
    ...

## Functions

### `xcaps_put(uri, path, doc)`

Handle XCAP PUT command.

#### `xcaps_put` usage

    ...
    event_route[xhttp:request] {
    	if($hu=~"^/xcap-root/")
    	{
    		# xcap ops
    		switch($rm) {
    			case "PUT":
    				xcaps_put("sip:101@$Ri", "$hu", "$rb");
    				exit;
    				break;
    		}
    	}
    }
    ...

### `xcaps_get(uri, path)`

Handle XCAP GET command.

#### `xcaps_get` usage

    ...
    event_route[xhttp:request] {
    	if($hu=~"^/xcap-root/")
    	{
    		# xcap ops
    		switch($rm) {
    			case "GETT":
    				xcaps_get("sip:101@$Ri", "$hu");
    				exit;
    				break;
    		}
    	}
    }
    ...

### `xcaps_del(uri, path)`

Handle XCAP DELETE command.

#### `xcaps_del` usage

    ...
    event_route[xhttp:request] {
    	if($hu=~"^/xcap-root/")
    	{
    		# xcap ops
    		switch($rm) {
    			case "DELETE":
    				xcaps_del("sip:101@$Ri", "$hu");
    				exit;
    				break;
    		}
    	}
    }
    ...

## Pseudo Variables

- $xcapuri(name=>key) - name can be any to idenitfy the XCAP uri; key can be: data, uri, root, auid, type, tree, xuid, file, node, target, domain, uri_adoc.

Exported pseudo-variables are documented at &kamwikilink;.

### $xcapuri(...) PV

    ...
        $xcapuri(u=>data) = $hu;
        xdbg("SCRIPT: xcap service $xcapuri(u=>auid) for $xcapuri(u=>xuid)\n");
    ...

## Simple XCAP Server Config

### sample xcap server

    ...
    tcp_accept_no_cl=yes
    ...
    loadmodule "xhttp.so"
    loadmodule "xcap_server.so"

    ...

    # ----- xcap_server params -----
    modparam("xcap_server", "db_url",
    	"&defaultdb;")

    ...

    event_route[xhttp:request] {
        if (!www_authorize("xcap", "subscriber"))
        {
            www_challenge("xcap", "0");
            exit;
        }
        if($hu=~"^/xcap-root/")
        {
            set_reply_close();
            set_reply_no_connect();
            # xcap ops - break down http uri to get xcap user id
            $xcapuri(u=>data) = $hu;
            if($xcapuri(u=>xuid)=~"^sip:.+@.+")
                $var(uri) = $xcapuri(u=>xuid);
            else
                $var(uri) = "sip:"+ $xcapuri(u=>xuid) + "@" + $Ri;

            # handle XCAP capability request
            if($xcapuri(u=>auid)=="xcap-caps")
            {
                if ($rm == "GET")
                {
                    $var(xbody) =
    "<?xml version='1.0' encoding='UTF-8'?>
    <xcap-caps xmlns='urn:ietf:params:xml:ns:xcap-caps'>
      <auids>
        <auid>rls-services</auid>
        <auid>pidf-manipulation</auid>
        <auid>xcap-caps</auid>
        <auid>resource-lists</auid>
        <auid>pres-rules</auid>
        <auid>org.openmobilealliance.pres-rules</auid>
        <auid>org.openmobilealliance.user-profile</auid>
        <auid>org.openmobilealliance.pres-content</auid>
        <auid>org.openmobilealliance.search</auid>
        <auid>org.openmobilealliance.xcap-directory</auid>
      </auids>
      <extensions>
      </extensions>
      <namespaces>
        <namespace>urn:ietf:params:xml:ns:rls-services</namespace>
        <namespace>urn:ietf:params:xml:ns:pidf</namespace>
        <namespace>urn:ietf:params:xml:ns:xcap-caps</namespace>
        <namespace>urn:ietf:params:xml:ns:resource-lists</namespace>
        <namespace>urn:ietf:params:xml:ns:pres-rules</namespace>
        <namespace>urn:oma:xml:xdm:user-profile</namespace>
        <namespace>urn:oma:xml:prs:pres-content</namespace>
        <namespace>urn:oma:xml:xdm:search</namespace>
        <namespace>urn:oma:xml:xdm:xcap-directory</namespace>
      </namespaces>
    </xcap-caps>";
                    xhttp_reply("200", "ok", "application/xcap-caps+xml",
                                "$var(xbody)");
                }
                else
                {
                    append_to_reply("Allow: GET\r\n");
                    xhttp_reply("405", "Method Not Allowed", "", "");
                }
                exit;
            }
            # be sure auth user access only its documents
            if ($au!=$(var(uri){uri.user})) {
                xhttp_reply("403", "Forbidden", "text/html",
                        "operation not allowed");
                exit;
            }

            xdbg("SCRIPT: xcap service $xcapuri(u=>auid) for $xcapuri(u=>xuid)\n");
            switch($rm) {
                case "PUT":
                    xcaps_put("$var(uri)", "$hu", "$rb");
                    if($xcapuri(u=>auid)=~"pres-rules")
                    {
                        pres_update_watchers("$var(uri)", "presence");
                        pres_refresh_watchers("$var(uri)", "presence", 1);
                    }
                    else if($xcapuri(u=>auid)=~"rls-services"
                            || $xcapuri(u=>auid)=~"resource-lists")
                    {
                        rls_update_subs("$var(uri)", "presence");
                    }
                    else if($xcapuri(u=>auid)=~"pidf-manipulation")
                    {
                        pres_refresh_watchers("$var(uri)", "presence", 2,
                                                "$xcapuri(u=>uri_adoc)",
                                                "$xcapuri(u=>file)");
                    }
                    exit;
                break;
                case "GET":
                    xcaps_get("$var(uri)", "$hu");
                    exit;
                break;
                case "DELETE":
                    xcaps_del("$var(uri)", "$hu");
                    if($xcapuri(u=>auid)=~"pres-rules")
                    {
                        pres_update_watchers("$var(uri)", "presence");
                        pres_refresh_watchers("$var(uri)", "presence", 1);
                    }
                    else if($xcapuri(u=>auid)=~"rls-services"
                            || $xcapuri(u=>auid)=~"resource-lists")
                    {
                        rls_update_subs("$var(uri)", "presence");
                    }
                    else if($xcapuri(u=>auid)=~"pidf-manipulation")
                    {
                        pres_refresh_watchers("$var(uri)", "presence", 2,
                                                "$xcapuri(u=>uri_adoc)",
                                                "$xcapuri(u=>file)");
                    }
                    exit;
                break;
                case "POST":
                    if($xcapuri(u=>auid)=~"search")
                    {
                        xhttp_reply("501", "Not Implemented", "", "");
                    }
                    else
                    {
                        if($xcapuri(u=>auid)=~"xcap-directory")
                        {
                            append_to_reply("Allow: GET\r\n");
                        }
                        else
                        {
                            append_to_reply("Allow: DELETE, GET, PUT\r\n");
                        }
                        xhttp_reply("405", "Method Not Allowed", "", "");
                    }
                    exit;
                break;
            }
        }

        # other http requests
        xhttp_reply("404", "Not Found", "", "");
        exit;
    }
    ...

The URL for XCAP has to be:

http://_your_sip_server_ip_:_your_sip_server_port_/xcap-root/...

For example, if your SIP server IP is 10.1.1.10 and it is listening on TCP port 5060 and TLS port 5061, following XCAP URLs can be used:

http://10.1.1.10:5060/xcap-root/...

https://10.1.1.10:5061/xcap-root/...