2023-06-26 23:12:20 +00:00
|
|
|
/* $Id: upnpsoap.c,v 1.165 2023/06/26 23:15:56 nanard Exp $ */
|
2018-01-16 01:06:46 +00:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
* MiniUPnP project
|
|
|
|
* http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/
|
2024-01-04 01:36:38 +00:00
|
|
|
* (c) 2006-2024 Thomas Bernard
|
2011-09-28 19:13:20 +00:00
|
|
|
* This software is subject to the conditions detailed
|
|
|
|
* in the LICENCE file provided within the distribution */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-05-16 10:43:11 +00:00
|
|
|
#include <limits.h>
|
2011-09-28 19:13:20 +00:00
|
|
|
#include <string.h>
|
2012-04-20 22:09:52 +00:00
|
|
|
#include <errno.h>
|
2011-09-28 19:13:20 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <netinet/in.h>
|
2012-01-20 21:55:43 +00:00
|
|
|
#include <arpa/inet.h>
|
2011-09-28 19:13:20 +00:00
|
|
|
#include <netdb.h>
|
2017-03-13 10:30:44 +00:00
|
|
|
#include <ctype.h>
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2012-05-01 09:47:37 +00:00
|
|
|
#include "macros.h"
|
2011-09-28 19:13:20 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "upnpglobalvars.h"
|
|
|
|
#include "upnphttp.h"
|
|
|
|
#include "upnpsoap.h"
|
|
|
|
#include "upnpreplyparse.h"
|
|
|
|
#include "upnpredirect.h"
|
2020-10-17 22:20:24 +00:00
|
|
|
#include "upnppermissions.h"
|
2012-05-01 20:25:30 +00:00
|
|
|
#include "upnppinhole.h"
|
2011-09-28 19:13:20 +00:00
|
|
|
#include "getifaddr.h"
|
|
|
|
#include "getifstats.h"
|
|
|
|
#include "getconnstatus.h"
|
|
|
|
#include "upnpurns.h"
|
2018-03-13 10:43:07 +00:00
|
|
|
#include "upnputils.h"
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2016-02-16 11:07:22 +00:00
|
|
|
/* utility function */
|
|
|
|
static int is_numeric(const char * s)
|
|
|
|
{
|
|
|
|
while(*s) {
|
|
|
|
if(*s < '0' || *s > '9') return 0;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
static void
|
|
|
|
BuildSendAndCloseSoapResp(struct upnphttp * h,
|
|
|
|
const char * body, int bodylen)
|
|
|
|
{
|
|
|
|
static const char beforebody[] =
|
|
|
|
"<?xml version=\"1.0\"?>\r\n"
|
|
|
|
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
|
|
|
|
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
|
|
|
|
"<s:Body>";
|
|
|
|
|
|
|
|
static const char afterbody[] =
|
|
|
|
"</s:Body>"
|
|
|
|
"</s:Envelope>\r\n";
|
|
|
|
|
2014-12-09 10:04:15 +00:00
|
|
|
int r = BuildHeader_upnphttp(h, 200, "OK", sizeof(beforebody) - 1
|
|
|
|
+ sizeof(afterbody) - 1 + bodylen );
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2014-12-09 10:04:15 +00:00
|
|
|
if(r >= 0) {
|
|
|
|
memcpy(h->res_buf + h->res_buflen, beforebody, sizeof(beforebody) - 1);
|
|
|
|
h->res_buflen += sizeof(beforebody) - 1;
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2014-12-09 10:04:15 +00:00
|
|
|
memcpy(h->res_buf + h->res_buflen, body, bodylen);
|
|
|
|
h->res_buflen += bodylen;
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2014-12-09 10:04:15 +00:00
|
|
|
memcpy(h->res_buf + h->res_buflen, afterbody, sizeof(afterbody) - 1);
|
|
|
|
h->res_buflen += sizeof(afterbody) - 1;
|
|
|
|
} else {
|
|
|
|
BuildResp2_upnphttp(h, 500, "Internal Server Error", NULL, 0);
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2011-11-18 11:23:16 +00:00
|
|
|
SendRespAndClose_upnphttp(h);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
GetConnectionTypeInfo(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2015-09-14 22:02:56 +00:00
|
|
|
#if 0
|
2011-09-28 19:13:20 +00:00
|
|
|
static const char resp[] =
|
|
|
|
"<u:GetConnectionTypeInfoResponse "
|
|
|
|
"xmlns:u=\"" SERVICE_TYPE_WANIPC "\">"
|
|
|
|
"<NewConnectionType>IP_Routed</NewConnectionType>"
|
|
|
|
"<NewPossibleConnectionTypes>IP_Routed</NewPossibleConnectionTypes>"
|
|
|
|
"</u:GetConnectionTypeInfoResponse>";
|
2015-09-14 22:02:56 +00:00
|
|
|
#endif
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"<NewConnectionType>IP_Routed</NewConnectionType>"
|
|
|
|
"<NewPossibleConnectionTypes>IP_Routed</NewPossibleConnectionTypes>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
2012-05-01 09:47:37 +00:00
|
|
|
|
2015-09-14 22:02:56 +00:00
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
|
|
|
action, ns, action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
2016-02-19 10:59:52 +00:00
|
|
|
/* maximum value for a UPNP ui4 type variable */
|
|
|
|
#define UPNP_UI4_MAX (4294967295ul)
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
GetTotalBytesSent(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"<NewTotalBytesSent>%lu</NewTotalBytesSent>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
|
|
|
struct ifdata data;
|
|
|
|
|
|
|
|
r = getifstats(ext_if_name, &data);
|
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-09-21 15:10:15 +00:00
|
|
|
action, ns, /* was "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1" */
|
2016-02-19 10:59:52 +00:00
|
|
|
#ifdef UPNP_STRICT
|
|
|
|
r<0?0:(data.obytes & UPNP_UI4_MAX), action);
|
|
|
|
#else /* UPNP_STRICT */
|
2011-09-28 19:13:20 +00:00
|
|
|
r<0?0:data.obytes, action);
|
2016-02-19 10:59:52 +00:00
|
|
|
#endif /* UPNP_STRICT */
|
2011-09-28 19:13:20 +00:00
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
GetTotalBytesReceived(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"<NewTotalBytesReceived>%lu</NewTotalBytesReceived>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
|
|
|
struct ifdata data;
|
|
|
|
|
|
|
|
r = getifstats(ext_if_name, &data);
|
2016-02-19 10:59:52 +00:00
|
|
|
/* TotalBytesReceived
|
|
|
|
* This variable represents the cumulative counter for total number of
|
|
|
|
* bytes received downstream across all connection service instances on
|
|
|
|
* WANDevice. The count rolls over to 0 after it reaching the maximum
|
|
|
|
* value (2^32)-1. */
|
2011-09-28 19:13:20 +00:00
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-09-21 15:10:15 +00:00
|
|
|
action, ns, /* was "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1" */
|
2016-02-19 10:59:52 +00:00
|
|
|
#ifdef UPNP_STRICT
|
|
|
|
r<0?0:(data.ibytes & UPNP_UI4_MAX), action);
|
|
|
|
#else /* UPNP_STRICT */
|
2011-09-28 19:13:20 +00:00
|
|
|
r<0?0:data.ibytes, action);
|
2016-02-19 10:59:52 +00:00
|
|
|
#endif /* UPNP_STRICT */
|
2011-09-28 19:13:20 +00:00
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
GetTotalPacketsSent(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"<NewTotalPacketsSent>%lu</NewTotalPacketsSent>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
|
|
|
struct ifdata data;
|
|
|
|
|
|
|
|
r = getifstats(ext_if_name, &data);
|
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-09-14 22:02:56 +00:00
|
|
|
action, ns,/*"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",*/
|
2016-02-19 10:59:52 +00:00
|
|
|
#ifdef UPNP_STRICT
|
|
|
|
r<0?0:(data.opackets & UPNP_UI4_MAX), action);
|
|
|
|
#else /* UPNP_STRICT */
|
2011-09-28 19:13:20 +00:00
|
|
|
r<0?0:data.opackets, action);
|
2016-02-19 10:59:52 +00:00
|
|
|
#endif /* UPNP_STRICT */
|
2011-09-28 19:13:20 +00:00
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
GetTotalPacketsReceived(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"<NewTotalPacketsReceived>%lu</NewTotalPacketsReceived>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
|
|
|
struct ifdata data;
|
|
|
|
|
|
|
|
r = getifstats(ext_if_name, &data);
|
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-09-21 15:10:15 +00:00
|
|
|
action, ns, /* was "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1" */
|
2016-02-19 10:59:52 +00:00
|
|
|
#ifdef UPNP_STRICT
|
|
|
|
r<0?0:(data.ipackets & UPNP_UI4_MAX), action);
|
|
|
|
#else /* UPNP_STRICT */
|
2011-09-28 19:13:20 +00:00
|
|
|
r<0?0:data.ipackets, action);
|
2016-02-19 10:59:52 +00:00
|
|
|
#endif /* UPNP_STRICT */
|
2011-09-28 19:13:20 +00:00
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
GetCommonLinkProperties(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
/* WANAccessType : set depending on the hardware :
|
|
|
|
* DSL, POTS (plain old Telephone service), Cable, Ethernet */
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
2014-11-27 12:25:45 +00:00
|
|
|
"<NewWANAccessType>%s</NewWANAccessType>"
|
2011-09-28 19:13:20 +00:00
|
|
|
"<NewLayer1UpstreamMaxBitRate>%lu</NewLayer1UpstreamMaxBitRate>"
|
|
|
|
"<NewLayer1DownstreamMaxBitRate>%lu</NewLayer1DownstreamMaxBitRate>"
|
|
|
|
"<NewPhysicalLinkStatus>%s</NewPhysicalLinkStatus>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
|
|
|
|
char body[2048];
|
|
|
|
int bodylen;
|
|
|
|
struct ifdata data;
|
|
|
|
const char * status = "Up"; /* Up, Down (Required),
|
|
|
|
* Initializing, Unavailable (Optional) */
|
2014-11-27 12:25:45 +00:00
|
|
|
const char * wan_access_type = "Cable"; /* DSL, POTS, Cable, Ethernet */
|
2011-09-28 19:13:20 +00:00
|
|
|
char ext_ip_addr[INET_ADDRSTRLEN];
|
|
|
|
|
|
|
|
if((downstream_bitrate == 0) || (upstream_bitrate == 0))
|
|
|
|
{
|
|
|
|
if(getifstats(ext_if_name, &data) >= 0)
|
|
|
|
{
|
|
|
|
if(downstream_bitrate == 0) downstream_bitrate = data.baudrate;
|
|
|
|
if(upstream_bitrate == 0) upstream_bitrate = data.baudrate;
|
|
|
|
}
|
|
|
|
}
|
2013-03-23 10:50:57 +00:00
|
|
|
if(getifaddr(ext_if_name, ext_ip_addr, INET_ADDRSTRLEN, NULL, NULL) < 0) {
|
2011-09-28 19:13:20 +00:00
|
|
|
status = "Down";
|
|
|
|
}
|
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-09-21 15:10:15 +00:00
|
|
|
action, ns, /* was "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1" */
|
2014-11-27 12:25:45 +00:00
|
|
|
wan_access_type,
|
|
|
|
upstream_bitrate, downstream_bitrate,
|
2011-09-28 19:13:20 +00:00
|
|
|
status, action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
GetStatusInfo(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"<NewConnectionStatus>%s</NewConnectionStatus>"
|
|
|
|
"<NewLastConnectionError>ERROR_NONE</NewLastConnectionError>"
|
|
|
|
"<NewUptime>%ld</NewUptime>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
|
|
|
time_t uptime;
|
|
|
|
const char * status;
|
|
|
|
/* ConnectionStatus possible values :
|
|
|
|
* Unconfigured, Connecting, Connected, PendingDisconnect,
|
|
|
|
* Disconnecting, Disconnected */
|
|
|
|
|
|
|
|
status = get_wan_connection_status_str(ext_if_name);
|
2018-03-13 10:43:07 +00:00
|
|
|
uptime = upnp_get_uptime();
|
2011-09-28 19:13:20 +00:00
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-09-14 22:02:56 +00:00
|
|
|
action, ns, /*SERVICE_TYPE_WANIPC,*/
|
2012-03-01 01:44:38 +00:00
|
|
|
status, (long)uptime, action);
|
2011-09-28 19:13:20 +00:00
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
GetNATRSIPStatus(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2015-09-15 07:39:50 +00:00
|
|
|
#if 0
|
2011-09-28 19:13:20 +00:00
|
|
|
static const char resp[] =
|
|
|
|
"<u:GetNATRSIPStatusResponse "
|
|
|
|
"xmlns:u=\"" SERVICE_TYPE_WANIPC "\">"
|
|
|
|
"<NewRSIPAvailable>0</NewRSIPAvailable>"
|
|
|
|
"<NewNATEnabled>1</NewNATEnabled>"
|
|
|
|
"</u:GetNATRSIPStatusResponse>";
|
2012-05-01 09:47:37 +00:00
|
|
|
UNUSED(action);
|
2015-09-15 07:39:50 +00:00
|
|
|
#endif
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"<NewRSIPAvailable>0</NewRSIPAvailable>"
|
|
|
|
"<NewNATEnabled>1</NewNATEnabled>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
2011-09-28 19:13:20 +00:00
|
|
|
/* 2.2.9. RSIPAvailable
|
|
|
|
* This variable indicates if Realm-specific IP (RSIP) is available
|
|
|
|
* as a feature on the InternetGatewayDevice. RSIP is being defined
|
|
|
|
* in the NAT working group in the IETF to allow host-NATing using
|
|
|
|
* a standard set of message exchanges. It also allows end-to-end
|
|
|
|
* applications that otherwise break if NAT is introduced
|
|
|
|
* (e.g. IPsec-based VPNs).
|
|
|
|
* A gateway that does not support RSIP should set this variable to 0. */
|
2015-09-15 07:39:50 +00:00
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
|
|
|
action, ns, /*SERVICE_TYPE_WANIPC,*/
|
|
|
|
action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
GetExternalIPAddress(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"<NewExternalIPAddress>%s</NewExternalIPAddress>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
|
|
|
char ext_ip_addr[INET_ADDRSTRLEN];
|
|
|
|
/* Does that method need to work with IPv6 ?
|
|
|
|
* There is usually no NAT with IPv6 */
|
|
|
|
|
|
|
|
#ifndef MULTIPLE_EXTERNAL_IP
|
|
|
|
if(use_ext_ip_addr)
|
|
|
|
{
|
|
|
|
strncpy(ext_ip_addr, use_ext_ip_addr, INET_ADDRSTRLEN);
|
2015-06-24 05:30:41 +00:00
|
|
|
ext_ip_addr[INET_ADDRSTRLEN - 1] = '\0';
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2020-05-29 06:55:44 +00:00
|
|
|
else
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2020-05-29 06:55:44 +00:00
|
|
|
struct in_addr addr;
|
|
|
|
if(getifaddr(ext_if_name, ext_ip_addr, INET_ADDRSTRLEN, &addr, NULL) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "Failed to get ip address for interface %s",
|
|
|
|
ext_if_name);
|
2021-03-31 07:43:28 +00:00
|
|
|
ext_ip_addr[0] = '\0';
|
2020-05-29 06:55:44 +00:00
|
|
|
} else if (addr_is_reserved(&addr)) {
|
|
|
|
syslog(LOG_NOTICE, "private/reserved address %s is not suitable for external IP", ext_ip_addr);
|
2021-03-31 07:43:28 +00:00
|
|
|
ext_ip_addr[0] = '\0';
|
2020-05-29 06:55:44 +00:00
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
struct lan_addr_s * lan_addr;
|
2021-03-31 07:43:28 +00:00
|
|
|
ext_ip_addr[0] = '\0';
|
2011-09-28 19:13:20 +00:00
|
|
|
for(lan_addr = lan_addrs.lh_first; lan_addr != NULL; lan_addr = lan_addr->list.le_next)
|
|
|
|
{
|
|
|
|
if( (h->clientaddr.s_addr & lan_addr->mask.s_addr)
|
|
|
|
== (lan_addr->addr.s_addr & lan_addr->mask.s_addr))
|
|
|
|
{
|
|
|
|
strncpy(ext_ip_addr, lan_addr->ext_ip_str, INET_ADDRSTRLEN);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2021-03-31 07:43:28 +00:00
|
|
|
/* WANIPConnection:2 Service 2.3.13 :
|
|
|
|
* When the external IP address could not be retrieved by the gateway
|
|
|
|
* (for example, because the interface is down or because there was a
|
|
|
|
* failure in the last connection setup attempt),
|
|
|
|
* then the ExternalIPAddress MUST be equal to the empty string.
|
|
|
|
*
|
|
|
|
* There is no precise requirement on how theses cases must be handled
|
|
|
|
* in IGDv1 specifications, but ExternalIPAddress default value is empty
|
|
|
|
* string. */
|
2018-05-19 15:28:43 +00:00
|
|
|
if (strcmp(ext_ip_addr, "0.0.0.0") == 0)
|
2021-03-28 15:20:34 +00:00
|
|
|
ext_ip_addr[0] = '\0';
|
2011-09-28 19:13:20 +00:00
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-09-14 22:02:56 +00:00
|
|
|
action, ns, /*SERVICE_TYPE_WANIPC,*/
|
2011-09-28 19:13:20 +00:00
|
|
|
ext_ip_addr, action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* AddPortMapping method of WANIPConnection Service
|
|
|
|
* Ignored argument : NewEnabled */
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
AddPortMapping(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
2015-09-15 07:39:50 +00:00
|
|
|
/*static const char resp[] =
|
2011-09-28 19:13:20 +00:00
|
|
|
"<u:AddPortMappingResponse "
|
2015-09-15 07:39:50 +00:00
|
|
|
"xmlns:u=\"" SERVICE_TYPE_WANIPC "\"/>";*/
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\"/>";
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2015-09-15 07:39:50 +00:00
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
2011-09-28 19:13:20 +00:00
|
|
|
struct NameValueParserData data;
|
|
|
|
char * int_ip, * int_port, * ext_port, * protocol, * desc;
|
|
|
|
char * leaseduration_str;
|
|
|
|
unsigned int leaseduration;
|
|
|
|
char * r_host;
|
|
|
|
unsigned short iport, eport;
|
|
|
|
|
|
|
|
struct hostent *hp; /* getbyhostname() */
|
|
|
|
char ** ptr; /* getbyhostname() */
|
|
|
|
struct in_addr result_ip;/*unsigned char result_ip[16];*/ /* inet_pton() */
|
|
|
|
|
|
|
|
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data);
|
|
|
|
int_ip = GetValueFromNameValueList(&data, "NewInternalClient");
|
2017-03-13 10:03:58 +00:00
|
|
|
if (int_ip) {
|
|
|
|
/* trim */
|
|
|
|
while(int_ip[0] == ' ')
|
|
|
|
int_ip++;
|
|
|
|
}
|
|
|
|
#ifdef UPNP_STRICT
|
|
|
|
if (!int_ip || int_ip[0] == '\0')
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
|
|
|
}
|
2017-03-13 10:03:58 +00:00
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
/* IGD 2 MUST support both wildcard and specific IP address values
|
|
|
|
* for RemoteHost (only the wildcard value was REQUIRED in release 1.0) */
|
|
|
|
r_host = GetValueFromNameValueList(&data, "NewRemoteHost");
|
|
|
|
#ifndef SUPPORT_REMOTEHOST
|
|
|
|
#ifdef UPNP_STRICT
|
2018-06-01 08:55:45 +00:00
|
|
|
if (r_host && (r_host[0] != '\0') && (0 != strcmp(r_host, "*")))
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 726, "RemoteHostOnlySupportsWildcard");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2017-03-13 10:03:58 +00:00
|
|
|
#ifndef UPNP_STRICT
|
|
|
|
/* if <NewInternalClient> arg is empty, use client address
|
|
|
|
* see https://github.com/miniupnp/miniupnp/issues/236 */
|
|
|
|
if (!int_ip || int_ip[0] == '\0')
|
|
|
|
{
|
|
|
|
int_ip = h->clientaddr_str;
|
|
|
|
memcpy(&result_ip, &(h->clientaddr), sizeof(struct in_addr));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
/* if ip not valid assume hostname and convert */
|
2012-03-01 01:44:38 +00:00
|
|
|
if (inet_pton(AF_INET, int_ip, &result_ip) <= 0)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
hp = gethostbyname(int_ip);
|
2012-03-01 01:44:38 +00:00
|
|
|
if(hp && hp->h_addrtype == AF_INET)
|
|
|
|
{
|
2011-09-28 19:13:20 +00:00
|
|
|
for(ptr = hp->h_addr_list; ptr && *ptr; ptr++)
|
|
|
|
{
|
|
|
|
int_ip = inet_ntoa(*((struct in_addr *) *ptr));
|
|
|
|
result_ip = *((struct in_addr *) *ptr);
|
|
|
|
/* TODO : deal with more than one ip per hostname */
|
|
|
|
break;
|
|
|
|
}
|
2012-03-01 01:44:38 +00:00
|
|
|
}
|
|
|
|
else
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-03-01 01:44:38 +00:00
|
|
|
syslog(LOG_ERR, "Failed to convert hostname '%s' to ip address", int_ip);
|
2011-09-28 19:13:20 +00:00
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
2012-03-01 01:44:38 +00:00
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check if NewInternalAddress is the client address */
|
|
|
|
if(GETFLAG(SECUREMODEMASK))
|
|
|
|
{
|
|
|
|
if(h->clientaddr.s_addr != result_ip.s_addr)
|
|
|
|
{
|
|
|
|
syslog(LOG_INFO, "Client %s tried to redirect port to %s",
|
|
|
|
inet_ntoa(h->clientaddr), int_ip);
|
|
|
|
ClearNameValueList(&data);
|
2024-01-04 01:36:38 +00:00
|
|
|
#ifdef IGD_V2
|
|
|
|
SoapError(h, 606, "Action not authorized");
|
|
|
|
#else
|
2011-09-28 19:13:20 +00:00
|
|
|
SoapError(h, 718, "ConflictInMappingEntry");
|
2024-01-04 01:36:38 +00:00
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int_port = GetValueFromNameValueList(&data, "NewInternalPort");
|
|
|
|
ext_port = GetValueFromNameValueList(&data, "NewExternalPort");
|
|
|
|
protocol = GetValueFromNameValueList(&data, "NewProtocol");
|
|
|
|
desc = GetValueFromNameValueList(&data, "NewPortMappingDescription");
|
|
|
|
leaseduration_str = GetValueFromNameValueList(&data, "NewLeaseDuration");
|
|
|
|
|
|
|
|
if (!int_port || !ext_port || !protocol)
|
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
eport = (unsigned short)atoi(ext_port);
|
|
|
|
iport = (unsigned short)atoi(int_port);
|
|
|
|
|
2016-02-12 14:02:06 +00:00
|
|
|
if (strcmp(ext_port, "*") == 0 || eport == 0)
|
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
2024-01-01 20:36:18 +00:00
|
|
|
SoapError(h, 716, "WildCardNotPermittedInExtPort");
|
2016-02-12 14:02:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
leaseduration = leaseduration_str ? atoi(leaseduration_str) : 0;
|
|
|
|
#ifdef IGD_V2
|
|
|
|
/* PortMappingLeaseDuration can be either a value between 1 and
|
|
|
|
* 604800 seconds or the zero value (for infinite lease time).
|
|
|
|
* Note that an infinite lease time can be only set by out-of-band
|
|
|
|
* mechanisms like WWW-administration, remote management or local
|
|
|
|
* management.
|
|
|
|
* If a control point uses the value 0 to indicate an infinite lease
|
|
|
|
* time mapping, it is REQUIRED that gateway uses the maximum value
|
|
|
|
* instead (e.g. 604800 seconds) */
|
|
|
|
if(leaseduration == 0 || leaseduration > 604800)
|
|
|
|
leaseduration = 604800;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
syslog(LOG_INFO, "%s: ext port %hu to %s:%hu protocol %s for: %s leaseduration=%u rhost=%s",
|
2012-05-24 16:52:27 +00:00
|
|
|
action, eport, int_ip, iport, protocol, desc, leaseduration,
|
|
|
|
r_host ? r_host : "NULL");
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
r = upnp_redirect(r_host, eport, int_ip, iport, protocol, desc, leaseduration);
|
|
|
|
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
|
|
|
|
/* possible error codes for AddPortMapping :
|
|
|
|
* 402 - Invalid Args
|
|
|
|
* 501 - Action Failed
|
2019-04-07 21:01:51 +00:00
|
|
|
* 606 - Action not authorized (added in IGD v2)
|
2024-01-01 20:36:18 +00:00
|
|
|
* 715 - WildCardNotPermittedInSrcIP
|
|
|
|
* 716 - WildCardNotPermittedInExtPort
|
2011-09-28 19:13:20 +00:00
|
|
|
* 718 - ConflictInMappingEntry
|
|
|
|
* 724 - SamePortValuesRequired (deprecated in IGD v2)
|
|
|
|
* 725 - OnlyPermanentLeasesSupported
|
|
|
|
The NAT implementation only supports permanent lease times on
|
|
|
|
port mappings (deprecated in IGD v2)
|
|
|
|
* 726 - RemoteHostOnlySupportsWildcard
|
|
|
|
RemoteHost must be a wildcard and cannot be a specific IP
|
|
|
|
address or DNS name (deprecated in IGD v2)
|
|
|
|
* 727 - ExternalPortOnlySupportsWildcard
|
|
|
|
ExternalPort must be a wildcard and cannot be a specific port
|
|
|
|
value (deprecated in IGD v2)
|
|
|
|
* 728 - NoPortMapsAvailable
|
2016-02-12 13:54:13 +00:00
|
|
|
There are not enough free ports available to complete the mapping
|
2011-09-28 19:13:20 +00:00
|
|
|
(added in IGD v2)
|
2019-04-07 21:01:51 +00:00
|
|
|
* 729 - ConflictWithOtherMechanisms (added in IGD v2)
|
|
|
|
* 732 - WildCardNotPermittedInIntPort (added in IGD v2) */
|
2011-09-28 19:13:20 +00:00
|
|
|
switch(r)
|
|
|
|
{
|
|
|
|
case 0: /* success */
|
2015-09-15 07:39:50 +00:00
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
|
|
|
action, ns/*SERVICE_TYPE_WANIPC*/);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
2011-09-28 19:13:20 +00:00
|
|
|
break;
|
2016-02-12 13:54:13 +00:00
|
|
|
case -4:
|
|
|
|
#ifdef IGD_V2
|
|
|
|
SoapError(h, 729, "ConflictWithOtherMechanisms");
|
|
|
|
break;
|
|
|
|
#endif /* IGD_V2 */
|
2011-09-28 19:13:20 +00:00
|
|
|
case -3: /* not permitted */
|
2019-04-07 21:01:51 +00:00
|
|
|
#ifdef IGD_V2
|
|
|
|
SoapError(h, 606, "Action not authorized");
|
|
|
|
break;
|
|
|
|
#endif /* IGD_V2 */
|
|
|
|
case -2: /* already redirected */
|
2011-09-28 19:13:20 +00:00
|
|
|
SoapError(h, 718, "ConflictInMappingEntry");
|
|
|
|
break;
|
|
|
|
default:
|
2024-01-01 20:36:18 +00:00
|
|
|
SoapError(h, 501, "Action Failed");
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* AddAnyPortMapping was added in WANIPConnection v2 */
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
AddAnyPortMapping(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"<NewReservedPort>%hu</NewReservedPort>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
|
|
|
|
|
|
|
struct NameValueParserData data;
|
|
|
|
const char * int_ip, * int_port, * ext_port, * protocol, * desc;
|
|
|
|
const char * r_host;
|
2020-10-17 22:20:24 +00:00
|
|
|
unsigned short iport, eport;
|
2011-09-28 19:13:20 +00:00
|
|
|
const char * leaseduration_str;
|
|
|
|
unsigned int leaseduration;
|
|
|
|
|
|
|
|
struct hostent *hp; /* getbyhostname() */
|
|
|
|
char ** ptr; /* getbyhostname() */
|
|
|
|
struct in_addr result_ip;/*unsigned char result_ip[16];*/ /* inet_pton() */
|
|
|
|
|
|
|
|
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data);
|
|
|
|
r_host = GetValueFromNameValueList(&data, "NewRemoteHost");
|
|
|
|
ext_port = GetValueFromNameValueList(&data, "NewExternalPort");
|
|
|
|
protocol = GetValueFromNameValueList(&data, "NewProtocol");
|
|
|
|
int_port = GetValueFromNameValueList(&data, "NewInternalPort");
|
|
|
|
int_ip = GetValueFromNameValueList(&data, "NewInternalClient");
|
|
|
|
/* NewEnabled */
|
|
|
|
desc = GetValueFromNameValueList(&data, "NewPortMappingDescription");
|
|
|
|
leaseduration_str = GetValueFromNameValueList(&data, "NewLeaseDuration");
|
|
|
|
|
|
|
|
leaseduration = leaseduration_str ? atoi(leaseduration_str) : 0;
|
|
|
|
if(leaseduration == 0)
|
|
|
|
leaseduration = 604800;
|
|
|
|
|
2019-03-09 09:20:47 +00:00
|
|
|
if (!int_ip || !ext_port || !int_port || !protocol)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
|
|
|
}
|
2012-10-04 22:19:26 +00:00
|
|
|
|
2020-06-20 14:35:31 +00:00
|
|
|
eport = (0 == strcmp(ext_port, "*")) ? 0 : (unsigned short)atoi(ext_port);
|
|
|
|
if (eport == 0) {
|
|
|
|
eport = 1024 + ((random() & 0x7ffffffL) % (65536-1024));
|
|
|
|
}
|
2012-10-04 22:19:26 +00:00
|
|
|
iport = (unsigned short)atoi(int_port);
|
2020-06-20 14:35:31 +00:00
|
|
|
if(iport == 0 || (!is_numeric(ext_port) && 0 != strcmp(ext_port, "*"))) {
|
2016-02-16 11:07:22 +00:00
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
#ifndef SUPPORT_REMOTEHOST
|
|
|
|
#ifdef UPNP_STRICT
|
2018-06-01 08:55:45 +00:00
|
|
|
if (r_host && (r_host[0] != '\0') && (0 != strcmp(r_host, "*")))
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 726, "RemoteHostOnlySupportsWildcard");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* if ip not valid assume hostname and convert */
|
2012-03-01 01:44:38 +00:00
|
|
|
if (inet_pton(AF_INET, int_ip, &result_ip) <= 0)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
hp = gethostbyname(int_ip);
|
2012-03-01 01:44:38 +00:00
|
|
|
if(hp && hp->h_addrtype == AF_INET)
|
|
|
|
{
|
2011-09-28 19:13:20 +00:00
|
|
|
for(ptr = hp->h_addr_list; ptr && *ptr; ptr++)
|
|
|
|
{
|
|
|
|
int_ip = inet_ntoa(*((struct in_addr *) *ptr));
|
|
|
|
result_ip = *((struct in_addr *) *ptr);
|
|
|
|
/* TODO : deal with more than one ip per hostname */
|
|
|
|
break;
|
|
|
|
}
|
2012-03-01 01:44:38 +00:00
|
|
|
}
|
|
|
|
else
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-03-01 01:44:38 +00:00
|
|
|
syslog(LOG_ERR, "Failed to convert hostname '%s' to ip address", int_ip);
|
2011-09-28 19:13:20 +00:00
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
2012-03-01 01:44:38 +00:00
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check if NewInternalAddress is the client address */
|
|
|
|
if(GETFLAG(SECUREMODEMASK))
|
|
|
|
{
|
|
|
|
if(h->clientaddr.s_addr != result_ip.s_addr)
|
|
|
|
{
|
|
|
|
syslog(LOG_INFO, "Client %s tried to redirect port to %s",
|
|
|
|
inet_ntoa(h->clientaddr), int_ip);
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 606, "Action not authorized");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 14:30:38 +00:00
|
|
|
/* first try the port asked in request, then
|
|
|
|
* try +1, -1, +2, -2, etc. */
|
2020-10-17 22:20:24 +00:00
|
|
|
r = upnp_redirect(r_host, eport, int_ip, iport, protocol, desc, leaseduration);
|
|
|
|
if (r != 0 && r != -1) {
|
|
|
|
unsigned short eport_below, eport_above;
|
|
|
|
struct in_addr address;
|
|
|
|
uint32_t allowed_eports[65536 / 32];
|
|
|
|
|
|
|
|
if(inet_aton(int_ip, &address) <= 0) {
|
|
|
|
syslog(LOG_ERR, "inet_aton(%s) FAILED", int_ip);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2020-10-17 22:20:24 +00:00
|
|
|
get_permitted_ext_ports(allowed_eports, upnppermlist, num_upnpperm,
|
2020-10-22 21:20:50 +00:00
|
|
|
address.s_addr, iport);
|
2020-10-17 22:20:24 +00:00
|
|
|
eport_above = eport_below = eport;
|
|
|
|
for(;;) {
|
|
|
|
/* loop invariant
|
|
|
|
* eport is equal to either eport_below or eport_above (or both) */
|
|
|
|
if (eport_below <= 1 && eport_above == 65535) {
|
|
|
|
/* all possible ports tried */
|
|
|
|
r = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (eport_above == 65535 || (eport > eport_below && eport_below > 1)) {
|
|
|
|
eport = --eport_below;
|
|
|
|
} else {
|
|
|
|
eport = ++eport_above;
|
|
|
|
}
|
2020-10-26 07:46:37 +00:00
|
|
|
if (!(allowed_eports[eport / 32] & ((uint32_t)1U << (eport % 32))))
|
2020-10-17 22:20:24 +00:00
|
|
|
continue; /* not allowed */
|
|
|
|
r = upnp_redirect(r_host, eport, int_ip, iport, protocol, desc, leaseduration);
|
|
|
|
if (r == 0 || r == -1) {
|
|
|
|
/* OK or failure : Stop */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* r : -2 / -4 already redirected or -3 permission check failed :
|
|
|
|
* continue */
|
2020-06-20 14:30:38 +00:00
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
|
|
|
|
switch(r)
|
|
|
|
{
|
2020-06-20 14:30:38 +00:00
|
|
|
case 1: /* exhausted possible mappings */
|
|
|
|
SoapError(h, 728, "NoPortMapsAvailable");
|
|
|
|
break;
|
2011-09-28 19:13:20 +00:00
|
|
|
case 0: /* success */
|
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-09-14 22:02:56 +00:00
|
|
|
action, ns, /*SERVICE_TYPE_WANIPC,*/
|
2011-09-28 19:13:20 +00:00
|
|
|
eport, action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
break;
|
|
|
|
case -2: /* already redirected */
|
|
|
|
SoapError(h, 718, "ConflictInMappingEntry");
|
|
|
|
break;
|
|
|
|
case -3: /* not permitted */
|
|
|
|
SoapError(h, 606, "Action not authorized");
|
|
|
|
break;
|
|
|
|
default:
|
2024-01-01 20:36:18 +00:00
|
|
|
SoapError(h, 501, "Action Failed");
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
GetSpecificPortMappingEntry(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"<NewInternalPort>%u</NewInternalPort>"
|
|
|
|
"<NewInternalClient>%s</NewInternalClient>"
|
|
|
|
"<NewEnabled>1</NewEnabled>"
|
|
|
|
"<NewPortMappingDescription>%s</NewPortMappingDescription>"
|
|
|
|
"<NewLeaseDuration>%u</NewLeaseDuration>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
|
|
|
|
char body[1024];
|
|
|
|
int bodylen;
|
|
|
|
struct NameValueParserData data;
|
|
|
|
const char * r_host, * ext_port, * protocol;
|
|
|
|
unsigned short eport, iport;
|
|
|
|
char int_ip[32];
|
|
|
|
char desc[64];
|
|
|
|
unsigned int leaseduration = 0;
|
|
|
|
|
|
|
|
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data);
|
|
|
|
r_host = GetValueFromNameValueList(&data, "NewRemoteHost");
|
|
|
|
ext_port = GetValueFromNameValueList(&data, "NewExternalPort");
|
|
|
|
protocol = GetValueFromNameValueList(&data, "NewProtocol");
|
|
|
|
|
2012-10-04 22:31:08 +00:00
|
|
|
#ifdef UPNP_STRICT
|
|
|
|
if(!ext_port || !protocol || !r_host)
|
|
|
|
#else
|
2011-09-28 19:13:20 +00:00
|
|
|
if(!ext_port || !protocol)
|
2012-10-04 22:31:08 +00:00
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifndef SUPPORT_REMOTEHOST
|
|
|
|
#ifdef UPNP_STRICT
|
2018-06-01 08:55:45 +00:00
|
|
|
if (r_host && (r_host[0] != '\0') && (0 != strcmp(r_host, "*")))
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 726, "RemoteHostOnlySupportsWildcard");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
eport = (unsigned short)atoi(ext_port);
|
2016-02-16 09:58:11 +00:00
|
|
|
if(eport == 0)
|
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
/* TODO : add r_host as an input parameter ...
|
|
|
|
* We prevent several Port Mapping with same external port
|
|
|
|
* but different remoteHost to be set up, so that is not
|
|
|
|
* a priority. */
|
|
|
|
r = upnp_get_redirection_infos(eport, protocol, &iport,
|
|
|
|
int_ip, sizeof(int_ip),
|
|
|
|
desc, sizeof(desc),
|
|
|
|
NULL, 0,
|
|
|
|
&leaseduration);
|
|
|
|
|
|
|
|
if(r < 0)
|
2012-03-01 01:44:38 +00:00
|
|
|
{
|
2011-09-28 19:13:20 +00:00
|
|
|
SoapError(h, 714, "NoSuchEntryInArray");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-30 08:09:22 +00:00
|
|
|
syslog(LOG_INFO, "%s: rhost='%s' %s %s found => %s:%u desc='%s' duration=%u",
|
2011-09-28 19:13:20 +00:00
|
|
|
action,
|
2012-05-24 16:52:27 +00:00
|
|
|
r_host ? r_host : "NULL", ext_port, protocol, int_ip,
|
2020-05-30 08:09:22 +00:00
|
|
|
(unsigned int)iport, desc, leaseduration);
|
2011-09-28 19:13:20 +00:00
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-09-15 07:39:50 +00:00
|
|
|
action, ns/*SERVICE_TYPE_WANIPC*/,
|
2011-09-28 19:13:20 +00:00
|
|
|
(unsigned int)iport, int_ip, desc, leaseduration,
|
|
|
|
action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
DeletePortMapping(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
2015-09-15 07:39:50 +00:00
|
|
|
/*static const char resp[] =
|
2011-09-28 19:13:20 +00:00
|
|
|
"<u:DeletePortMappingResponse "
|
|
|
|
"xmlns:u=\"" SERVICE_TYPE_WANIPC "\">"
|
2015-09-15 07:39:50 +00:00
|
|
|
"</u:DeletePortMappingResponse>";*/
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"</u:%sResponse>";
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2015-09-15 07:39:50 +00:00
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
2011-09-28 19:13:20 +00:00
|
|
|
struct NameValueParserData data;
|
2015-02-16 21:41:40 +00:00
|
|
|
const char * ext_port, * protocol;
|
2011-09-28 19:13:20 +00:00
|
|
|
unsigned short eport;
|
2015-02-16 21:41:40 +00:00
|
|
|
#ifdef UPNP_STRICT
|
|
|
|
const char * r_host;
|
|
|
|
#endif /* UPNP_STRICT */
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data);
|
|
|
|
ext_port = GetValueFromNameValueList(&data, "NewExternalPort");
|
|
|
|
protocol = GetValueFromNameValueList(&data, "NewProtocol");
|
2015-02-16 21:41:40 +00:00
|
|
|
#ifdef UPNP_STRICT
|
|
|
|
r_host = GetValueFromNameValueList(&data, "NewRemoteHost");
|
|
|
|
#endif /* UPNP_STRICT */
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2012-10-04 22:31:08 +00:00
|
|
|
#ifdef UPNP_STRICT
|
|
|
|
if(!ext_port || !protocol || !r_host)
|
|
|
|
#else
|
2011-09-28 19:13:20 +00:00
|
|
|
if(!ext_port || !protocol)
|
2015-02-16 21:41:40 +00:00
|
|
|
#endif /* UPNP_STRICT */
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifndef SUPPORT_REMOTEHOST
|
|
|
|
#ifdef UPNP_STRICT
|
2018-06-01 08:55:45 +00:00
|
|
|
if (r_host && (r_host[0] != '\0') && (0 != strcmp(r_host, "*")))
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 726, "RemoteHostOnlySupportsWildcard");
|
|
|
|
return;
|
|
|
|
}
|
2015-02-16 21:41:40 +00:00
|
|
|
#endif /* UPNP_STRICT */
|
|
|
|
#endif /* SUPPORT_REMOTEHOST */
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
eport = (unsigned short)atoi(ext_port);
|
2016-02-16 09:58:11 +00:00
|
|
|
if(eport == 0)
|
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2014-09-25 09:47:16 +00:00
|
|
|
syslog(LOG_INFO, "%s: external port: %hu, protocol: %s",
|
|
|
|
action, eport, protocol);
|
|
|
|
|
|
|
|
/* if in secure mode, check the IP
|
2011-09-28 19:13:20 +00:00
|
|
|
* Removing a redirection is not a security threat,
|
|
|
|
* just an annoyance for the user using it. So this is not
|
|
|
|
* a priority. */
|
2014-09-25 09:47:16 +00:00
|
|
|
if(GETFLAG(SECUREMODEMASK))
|
|
|
|
{
|
|
|
|
char int_ip[32];
|
|
|
|
struct in_addr int_ip_addr;
|
|
|
|
unsigned short iport;
|
|
|
|
unsigned int leaseduration = 0;
|
|
|
|
r = upnp_get_redirection_infos(eport, protocol, &iport,
|
|
|
|
int_ip, sizeof(int_ip),
|
|
|
|
NULL, 0, NULL, 0,
|
|
|
|
&leaseduration);
|
|
|
|
if(r >= 0)
|
|
|
|
{
|
|
|
|
if(inet_pton(AF_INET, int_ip, &int_ip_addr) > 0)
|
|
|
|
{
|
|
|
|
if(h->clientaddr.s_addr != int_ip_addr.s_addr)
|
|
|
|
{
|
2024-01-04 01:36:38 +00:00
|
|
|
#ifdef IGD_V2
|
2014-09-25 09:47:16 +00:00
|
|
|
SoapError(h, 606, "Action not authorized");
|
2024-01-04 01:36:38 +00:00
|
|
|
#else
|
|
|
|
SoapError(h, 714, "NoSuchEntryInArray");
|
|
|
|
#endif
|
2014-09-25 09:47:16 +00:00
|
|
|
ClearNameValueList(&data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
r = upnp_delete_redirection(eport, protocol);
|
|
|
|
|
|
|
|
if(r < 0)
|
2012-03-01 01:44:38 +00:00
|
|
|
{
|
2011-09-28 19:13:20 +00:00
|
|
|
SoapError(h, 714, "NoSuchEntryInArray");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-15 07:39:50 +00:00
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
|
|
|
action, ns, action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DeletePortMappingRange was added in IGD spec v2 */
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
DeletePortMappingRange(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
int r = -1;
|
2015-09-15 07:39:50 +00:00
|
|
|
/*static const char resp[] =
|
2011-09-28 19:13:20 +00:00
|
|
|
"<u:DeletePortMappingRangeResponse "
|
|
|
|
"xmlns:u=\"" SERVICE_TYPE_WANIPC "\">"
|
2015-09-15 07:39:50 +00:00
|
|
|
"</u:DeletePortMappingRangeResponse>";*/
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
2011-09-28 19:13:20 +00:00
|
|
|
struct NameValueParserData data;
|
|
|
|
const char * protocol;
|
2012-10-04 22:19:26 +00:00
|
|
|
const char * startport_s, * endport_s;
|
2011-09-28 19:13:20 +00:00
|
|
|
unsigned short startport, endport;
|
2012-10-04 22:19:26 +00:00
|
|
|
/*int manage;*/
|
2011-09-28 19:13:20 +00:00
|
|
|
unsigned short * port_list;
|
|
|
|
unsigned int i, number = 0;
|
|
|
|
|
|
|
|
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data);
|
2012-10-04 22:19:26 +00:00
|
|
|
startport_s = GetValueFromNameValueList(&data, "NewStartPort");
|
|
|
|
endport_s = GetValueFromNameValueList(&data, "NewEndPort");
|
2011-09-28 19:13:20 +00:00
|
|
|
protocol = GetValueFromNameValueList(&data, "NewProtocol");
|
2012-10-04 22:19:26 +00:00
|
|
|
/*manage = atoi(GetValueFromNameValueList(&data, "NewManage"));*/
|
2016-02-16 11:07:22 +00:00
|
|
|
if(startport_s == NULL || endport_s == NULL || protocol == NULL ||
|
|
|
|
!is_numeric(startport_s) || !is_numeric(endport_s)) {
|
2012-10-04 22:19:26 +00:00
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
startport = (unsigned short)atoi(startport_s);
|
|
|
|
endport = (unsigned short)atoi(endport_s);
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
/* possible errors :
|
|
|
|
606 - Action not authorized
|
|
|
|
730 - PortMappingNotFound
|
|
|
|
733 - InconsistentParameter
|
|
|
|
*/
|
|
|
|
if(startport > endport)
|
|
|
|
{
|
|
|
|
SoapError(h, 733, "InconsistentParameter");
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-13 20:29:49 +00:00
|
|
|
syslog(LOG_INFO, "%s: deleting external ports: %hu-%hu, protocol: %s",
|
|
|
|
action, startport, endport, protocol);
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
port_list = upnp_get_portmappings_in_range(startport, endport,
|
|
|
|
protocol, &number);
|
2014-05-13 20:29:49 +00:00
|
|
|
if(number == 0)
|
|
|
|
{
|
|
|
|
SoapError(h, 730, "PortMappingNotFound");
|
|
|
|
ClearNameValueList(&data);
|
2015-06-24 05:14:14 +00:00
|
|
|
free(port_list);
|
2014-05-13 20:29:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
for(i = 0; i < number; i++)
|
|
|
|
{
|
|
|
|
r = upnp_delete_redirection(port_list[i], protocol);
|
2014-05-13 20:29:49 +00:00
|
|
|
syslog(LOG_INFO, "%s: deleting external port: %hu, protocol: %s: %s",
|
|
|
|
action, port_list[i], protocol, r < 0 ? "failed" : "ok");
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
free(port_list);
|
2015-09-15 07:39:50 +00:00
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
|
|
|
action, ns, action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
GetGenericPortMappingEntry(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
int r;
|
2012-03-01 01:44:38 +00:00
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"<NewRemoteHost>%s</NewRemoteHost>"
|
|
|
|
"<NewExternalPort>%u</NewExternalPort>"
|
|
|
|
"<NewProtocol>%s</NewProtocol>"
|
|
|
|
"<NewInternalPort>%u</NewInternalPort>"
|
|
|
|
"<NewInternalClient>%s</NewInternalClient>"
|
|
|
|
"<NewEnabled>1</NewEnabled>"
|
|
|
|
"<NewPortMappingDescription>%s</NewPortMappingDescription>"
|
|
|
|
"<NewLeaseDuration>%u</NewLeaseDuration>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
|
2013-05-16 10:43:11 +00:00
|
|
|
long int index = 0;
|
2011-09-28 19:13:20 +00:00
|
|
|
unsigned short eport, iport;
|
|
|
|
const char * m_index;
|
2013-05-16 10:43:11 +00:00
|
|
|
char * endptr;
|
2016-02-15 12:36:30 +00:00
|
|
|
char protocol[8], iaddr[32];
|
2011-09-28 19:13:20 +00:00
|
|
|
char desc[64];
|
|
|
|
char rhost[40];
|
|
|
|
unsigned int leaseduration = 0;
|
|
|
|
struct NameValueParserData data;
|
|
|
|
|
|
|
|
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data);
|
|
|
|
m_index = GetValueFromNameValueList(&data, "NewPortMappingIndex");
|
|
|
|
|
|
|
|
if(!m_index)
|
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
2012-03-01 01:44:38 +00:00
|
|
|
}
|
2013-05-16 10:43:11 +00:00
|
|
|
errno = 0; /* To distinguish success/failure after call */
|
|
|
|
index = strtol(m_index, &endptr, 10);
|
|
|
|
if((errno == ERANGE && (index == LONG_MAX || index == LONG_MIN))
|
|
|
|
|| (errno != 0 && index == 0) || (m_index == endptr))
|
|
|
|
{
|
|
|
|
/* should condition (*endptr != '\0') be also an error ? */
|
|
|
|
if(m_index == endptr)
|
|
|
|
syslog(LOG_WARNING, "%s: no digits were found in <%s>",
|
|
|
|
"GetGenericPortMappingEntry", "NewPortMappingIndex");
|
|
|
|
else
|
|
|
|
syslog(LOG_WARNING, "%s: strtol('%s'): %m",
|
|
|
|
"GetGenericPortMappingEntry", m_index);
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2013-05-16 10:43:11 +00:00
|
|
|
syslog(LOG_INFO, "%s: index=%d", action, (int)index);
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
rhost[0] = '\0';
|
2013-05-16 10:43:11 +00:00
|
|
|
r = upnp_get_redirection_infos_by_index((int)index, &eport, protocol, &iport,
|
2011-09-28 19:13:20 +00:00
|
|
|
iaddr, sizeof(iaddr),
|
|
|
|
desc, sizeof(desc),
|
|
|
|
rhost, sizeof(rhost),
|
|
|
|
&leaseduration);
|
|
|
|
|
|
|
|
if(r < 0)
|
|
|
|
{
|
|
|
|
SoapError(h, 713, "SpecifiedArrayIndexInvalid");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int bodylen;
|
|
|
|
char body[2048];
|
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-09-14 22:02:56 +00:00
|
|
|
action, ns, /*SERVICE_TYPE_WANIPC,*/ rhost,
|
2011-09-28 19:13:20 +00:00
|
|
|
(unsigned int)eport, protocol, (unsigned int)iport, iaddr, desc,
|
|
|
|
leaseduration, action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* GetListOfPortMappings was added in the IGD v2 specification */
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
GetListOfPortMappings(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
static const char resp_start[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"<NewPortListing><![CDATA[";
|
|
|
|
static const char resp_end[] =
|
|
|
|
"]]></NewPortListing>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
|
|
|
|
static const char list_start[] =
|
|
|
|
"<p:PortMappingList xmlns:p=\"urn:schemas-upnp-org:gw:WANIPConnection\""
|
|
|
|
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
|
|
|
|
" xsi:schemaLocation=\"urn:schemas-upnp-org:gw:WANIPConnection"
|
|
|
|
" http://www.upnp.org/schemas/gw/WANIPConnection-v2.xsd\">";
|
|
|
|
static const char list_end[] =
|
|
|
|
"</p:PortMappingList>";
|
|
|
|
|
|
|
|
static const char entry[] =
|
|
|
|
"<p:PortMappingEntry>"
|
|
|
|
"<p:NewRemoteHost>%s</p:NewRemoteHost>"
|
|
|
|
"<p:NewExternalPort>%hu</p:NewExternalPort>"
|
|
|
|
"<p:NewProtocol>%s</p:NewProtocol>"
|
|
|
|
"<p:NewInternalPort>%hu</p:NewInternalPort>"
|
|
|
|
"<p:NewInternalClient>%s</p:NewInternalClient>"
|
|
|
|
"<p:NewEnabled>1</p:NewEnabled>"
|
|
|
|
"<p:NewDescription>%s</p:NewDescription>"
|
|
|
|
"<p:NewLeaseTime>%u</p:NewLeaseTime>"
|
|
|
|
"</p:PortMappingEntry>";
|
|
|
|
|
|
|
|
char * body;
|
|
|
|
size_t bodyalloc;
|
|
|
|
int bodylen;
|
|
|
|
|
|
|
|
int r = -1;
|
|
|
|
unsigned short iport;
|
|
|
|
char int_ip[32];
|
|
|
|
char desc[64];
|
|
|
|
char rhost[64];
|
|
|
|
unsigned int leaseduration = 0;
|
|
|
|
|
|
|
|
struct NameValueParserData data;
|
2012-10-04 22:19:26 +00:00
|
|
|
const char * startport_s, * endport_s;
|
2011-09-28 19:13:20 +00:00
|
|
|
unsigned short startport, endport;
|
|
|
|
const char * protocol;
|
2012-10-04 22:19:26 +00:00
|
|
|
/*int manage;*/
|
|
|
|
const char * number_s;
|
2011-09-28 19:13:20 +00:00
|
|
|
int number;
|
|
|
|
unsigned short * port_list;
|
|
|
|
unsigned int i, list_size = 0;
|
|
|
|
|
|
|
|
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data);
|
2012-10-04 22:19:26 +00:00
|
|
|
startport_s = GetValueFromNameValueList(&data, "NewStartPort");
|
|
|
|
endport_s = GetValueFromNameValueList(&data, "NewEndPort");
|
2011-09-28 19:13:20 +00:00
|
|
|
protocol = GetValueFromNameValueList(&data, "NewProtocol");
|
2012-10-04 22:19:26 +00:00
|
|
|
/*manage_s = GetValueFromNameValueList(&data, "NewManage");*/
|
|
|
|
number_s = GetValueFromNameValueList(&data, "NewNumberOfPorts");
|
|
|
|
if(startport_s == NULL || endport_s == NULL || protocol == NULL ||
|
2016-02-16 11:07:22 +00:00
|
|
|
number_s == NULL || !is_numeric(number_s) ||
|
|
|
|
!is_numeric(startport_s) || !is_numeric(endport_s)) {
|
2012-10-04 22:19:26 +00:00
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
startport = (unsigned short)atoi(startport_s);
|
|
|
|
endport = (unsigned short)atoi(endport_s);
|
|
|
|
/*manage = atoi(manage_s);*/
|
|
|
|
number = atoi(number_s);
|
2011-09-28 19:13:20 +00:00
|
|
|
if(number == 0) number = 1000; /* return up to 1000 mappings by default */
|
|
|
|
|
|
|
|
if(startport > endport)
|
|
|
|
{
|
|
|
|
SoapError(h, 733, "InconsistentParameter");
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
build the PortMappingList xml document :
|
|
|
|
|
|
|
|
<p:PortMappingList xmlns:p="urn:schemas-upnp-org:gw:WANIPConnection"
|
|
|
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
|
|
xsi:schemaLocation="urn:schemas-upnp-org:gw:WANIPConnection
|
|
|
|
http://www.upnp.org/schemas/gw/WANIPConnection-v2.xsd">
|
|
|
|
<p:PortMappingEntry>
|
|
|
|
<p:NewRemoteHost>202.233.2.1</p:NewRemoteHost>
|
|
|
|
<p:NewExternalPort>2345</p:NewExternalPort>
|
|
|
|
<p:NewProtocol>TCP</p:NewProtocol>
|
|
|
|
<p:NewInternalPort>2345</p:NewInternalPort>
|
|
|
|
<p:NewInternalClient>192.168.1.137</p:NewInternalClient>
|
|
|
|
<p:NewEnabled>1</p:NewEnabled>
|
|
|
|
<p:NewDescription>dooom</p:NewDescription>
|
|
|
|
<p:NewLeaseTime>345</p:NewLeaseTime>
|
|
|
|
</p:PortMappingEntry>
|
|
|
|
</p:PortMappingList>
|
|
|
|
*/
|
|
|
|
bodyalloc = 4096;
|
|
|
|
body = malloc(bodyalloc);
|
|
|
|
if(!body)
|
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
2024-01-01 20:36:18 +00:00
|
|
|
SoapError(h, 501, "Action Failed");
|
2011-09-28 19:13:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
bodylen = snprintf(body, bodyalloc, resp_start,
|
2015-09-14 22:02:56 +00:00
|
|
|
action, ns/*SERVICE_TYPE_WANIPC*/);
|
2012-05-01 09:52:21 +00:00
|
|
|
if(bodylen < 0)
|
|
|
|
{
|
2024-01-01 20:36:18 +00:00
|
|
|
SoapError(h, 501, "Action Failed");
|
2013-02-06 14:03:25 +00:00
|
|
|
free(body);
|
2012-05-01 09:52:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
memcpy(body+bodylen, list_start, sizeof(list_start));
|
|
|
|
bodylen += (sizeof(list_start) - 1);
|
|
|
|
|
|
|
|
port_list = upnp_get_portmappings_in_range(startport, endport,
|
|
|
|
protocol, &list_size);
|
|
|
|
/* loop through port mappings */
|
|
|
|
for(i = 0; number > 0 && i < list_size; i++)
|
|
|
|
{
|
|
|
|
/* have a margin of 1024 bytes to store the new entry */
|
2012-05-01 09:52:21 +00:00
|
|
|
if((unsigned int)bodylen + 1024 > bodyalloc)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2013-02-06 14:03:25 +00:00
|
|
|
char * body_sav = body;
|
2011-09-28 19:13:20 +00:00
|
|
|
bodyalloc += 4096;
|
|
|
|
body = realloc(body, bodyalloc);
|
|
|
|
if(!body)
|
|
|
|
{
|
2014-12-09 10:01:37 +00:00
|
|
|
syslog(LOG_CRIT, "realloc(%p, %u) FAILED", body_sav, (unsigned)bodyalloc);
|
2011-09-28 19:13:20 +00:00
|
|
|
ClearNameValueList(&data);
|
2024-01-01 20:36:18 +00:00
|
|
|
SoapError(h, 501, "Action Failed");
|
2013-02-06 14:03:25 +00:00
|
|
|
free(body_sav);
|
2011-09-28 19:13:20 +00:00
|
|
|
free(port_list);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rhost[0] = '\0';
|
|
|
|
r = upnp_get_redirection_infos(port_list[i], protocol, &iport,
|
|
|
|
int_ip, sizeof(int_ip),
|
|
|
|
desc, sizeof(desc),
|
|
|
|
rhost, sizeof(rhost),
|
|
|
|
&leaseduration);
|
|
|
|
if(r == 0)
|
|
|
|
{
|
|
|
|
bodylen += snprintf(body+bodylen, bodyalloc-bodylen, entry,
|
|
|
|
rhost, port_list[i], protocol,
|
|
|
|
iport, int_ip, desc, leaseduration);
|
|
|
|
number--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(port_list);
|
|
|
|
port_list = NULL;
|
|
|
|
|
2014-12-09 10:01:37 +00:00
|
|
|
if((bodylen + sizeof(list_end) + 1024) > bodyalloc)
|
|
|
|
{
|
|
|
|
char * body_sav = body;
|
|
|
|
bodyalloc += (sizeof(list_end) + 1024);
|
|
|
|
body = realloc(body, bodyalloc);
|
|
|
|
if(!body)
|
|
|
|
{
|
|
|
|
syslog(LOG_CRIT, "realloc(%p, %u) FAILED", body_sav, (unsigned)bodyalloc);
|
|
|
|
ClearNameValueList(&data);
|
2024-01-01 20:36:18 +00:00
|
|
|
SoapError(h, 501, "Action Failed");
|
2014-12-09 10:01:37 +00:00
|
|
|
free(body_sav);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
memcpy(body+bodylen, list_end, sizeof(list_end));
|
|
|
|
bodylen += (sizeof(list_end) - 1);
|
|
|
|
bodylen += snprintf(body+bodylen, bodyalloc-bodylen, resp_end,
|
|
|
|
action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
free(body);
|
|
|
|
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ENABLE_L3F_SERVICE
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
SetDefaultConnectionService(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2015-09-15 07:39:50 +00:00
|
|
|
/*static const char resp[] =
|
2011-09-28 19:13:20 +00:00
|
|
|
"<u:SetDefaultConnectionServiceResponse "
|
|
|
|
"xmlns:u=\"urn:schemas-upnp-org:service:Layer3Forwarding:1\">"
|
2015-09-15 07:39:50 +00:00
|
|
|
"</u:SetDefaultConnectionServiceResponse>";*/
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
2011-09-28 19:13:20 +00:00
|
|
|
struct NameValueParserData data;
|
|
|
|
char * p;
|
|
|
|
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data);
|
|
|
|
p = GetValueFromNameValueList(&data, "NewDefaultConnectionService");
|
|
|
|
if(p) {
|
2012-10-04 22:31:08 +00:00
|
|
|
/* 720 InvalidDeviceUUID
|
|
|
|
* 721 InvalidServiceID
|
|
|
|
* 723 InvalidConnServiceSelection */
|
2012-09-28 09:07:12 +00:00
|
|
|
#ifdef UPNP_STRICT
|
2013-06-05 09:11:53 +00:00
|
|
|
char * service;
|
|
|
|
service = strchr(p, ',');
|
2013-06-13 14:03:36 +00:00
|
|
|
if(0 != memcmp(uuidvalue_wcd, p, sizeof("uuid:00000000-0000-0000-0000-000000000000") - 1)) {
|
2012-10-04 22:31:08 +00:00
|
|
|
SoapError(h, 720, "InvalidDeviceUUID");
|
2013-06-05 09:11:53 +00:00
|
|
|
} else if(service == NULL || 0 != strcmp(service+1, SERVICE_ID_WANIPC)) {
|
|
|
|
SoapError(h, 721, "InvalidServiceID");
|
2012-10-04 22:31:08 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
syslog(LOG_INFO, "%s(%s) : Ignored", action, p);
|
2015-09-15 07:39:50 +00:00
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
|
|
|
action, ns, action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
2012-10-04 22:31:08 +00:00
|
|
|
}
|
2012-09-28 09:07:12 +00:00
|
|
|
} else {
|
|
|
|
/* missing argument */
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
GetDefaultConnectionService(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
2015-09-14 22:02:56 +00:00
|
|
|
"xmlns:u=\"%s\">"
|
2015-12-12 02:08:18 +00:00
|
|
|
#ifdef IGD_V2
|
|
|
|
"<NewDefaultConnectionService>%s:WANConnectionDevice:2,"
|
|
|
|
#else
|
2011-09-28 19:13:20 +00:00
|
|
|
"<NewDefaultConnectionService>%s:WANConnectionDevice:1,"
|
2015-12-12 02:08:18 +00:00
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
SERVICE_ID_WANIPC "</NewDefaultConnectionService>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
/* example from UPnP_IGD_Layer3Forwarding 1.0.pdf :
|
|
|
|
* uuid:44f5824f-c57d-418c-a131-f22b34e14111:WANConnectionDevice:1,
|
|
|
|
* urn:upnp-org:serviceId:WANPPPConn1 */
|
|
|
|
char body[1024];
|
|
|
|
int bodylen;
|
|
|
|
|
2015-09-21 15:10:15 +00:00
|
|
|
/* namespace : urn:schemas-upnp-org:service:Layer3Forwarding:1 */
|
2011-09-28 19:13:20 +00:00
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-09-14 22:02:56 +00:00
|
|
|
action, ns, uuidvalue_wcd, action);
|
2011-09-28 19:13:20 +00:00
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Added for compliance with WANIPConnection v2 */
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
SetConnectionType(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2015-02-16 21:41:40 +00:00
|
|
|
#ifdef UPNP_STRICT
|
2011-09-28 19:13:20 +00:00
|
|
|
const char * connection_type;
|
2015-02-16 21:41:40 +00:00
|
|
|
#endif /* UPNP_STRICT */
|
2011-09-28 19:13:20 +00:00
|
|
|
struct NameValueParserData data;
|
2012-05-01 09:47:37 +00:00
|
|
|
UNUSED(action);
|
2015-09-14 22:02:56 +00:00
|
|
|
UNUSED(ns);
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data);
|
2012-10-04 22:31:08 +00:00
|
|
|
#ifdef UPNP_STRICT
|
2015-02-16 21:41:40 +00:00
|
|
|
connection_type = GetValueFromNameValueList(&data, "NewConnectionType");
|
2012-10-04 22:31:08 +00:00
|
|
|
if(!connection_type) {
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
|
|
|
}
|
2015-02-16 21:41:40 +00:00
|
|
|
#endif /* UPNP_STRICT */
|
2011-09-28 19:13:20 +00:00
|
|
|
/* Unconfigured, IP_Routed, IP_Bridged */
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
/* always return a ReadOnly error */
|
|
|
|
SoapError(h, 731, "ReadOnly");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Added for compliance with WANIPConnection v2 */
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
RequestConnection(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-05-01 09:47:37 +00:00
|
|
|
UNUSED(action);
|
2015-09-14 22:02:56 +00:00
|
|
|
UNUSED(ns);
|
2011-09-28 19:13:20 +00:00
|
|
|
SoapError(h, 606, "Action not authorized");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Added for compliance with WANIPConnection v2 */
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
ForceTermination(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-05-01 09:47:37 +00:00
|
|
|
UNUSED(action);
|
2015-09-14 22:02:56 +00:00
|
|
|
UNUSED(ns);
|
2011-09-28 19:13:20 +00:00
|
|
|
SoapError(h, 606, "Action not authorized");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
If a control point calls QueryStateVariable on a state variable that is not
|
|
|
|
buffered in memory within (or otherwise available from) the service,
|
|
|
|
the service must return a SOAP fault with an errorCode of 404 Invalid Var.
|
|
|
|
|
|
|
|
QueryStateVariable remains useful as a limited test tool but may not be
|
|
|
|
part of some future versions of UPnP.
|
|
|
|
*/
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
QueryStateVariable(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"<return>%s</return>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
|
|
|
struct NameValueParserData data;
|
|
|
|
const char * var_name;
|
|
|
|
|
|
|
|
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data);
|
|
|
|
/*var_name = GetValueFromNameValueList(&data, "QueryStateVariable"); */
|
|
|
|
/*var_name = GetValueFromNameValueListIgnoreNS(&data, "varName");*/
|
|
|
|
var_name = GetValueFromNameValueList(&data, "varName");
|
|
|
|
|
|
|
|
/*syslog(LOG_INFO, "QueryStateVariable(%.40s)", var_name); */
|
|
|
|
|
|
|
|
if(!var_name)
|
|
|
|
{
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
}
|
|
|
|
else if(strcmp(var_name, "ConnectionStatus") == 0)
|
2012-03-01 01:44:38 +00:00
|
|
|
{
|
2011-09-28 19:13:20 +00:00
|
|
|
const char * status;
|
|
|
|
|
|
|
|
status = get_wan_connection_status_str(ext_if_name);
|
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-09-14 22:02:56 +00:00
|
|
|
action, ns,/*"urn:schemas-upnp-org:control-1-0",*/
|
2011-09-28 19:13:20 +00:00
|
|
|
status, action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
|
|
|
#if 0
|
2018-01-09 01:32:39 +00:00
|
|
|
/* not useful */
|
2011-09-28 19:13:20 +00:00
|
|
|
else if(strcmp(var_name, "ConnectionType") == 0)
|
2012-03-01 01:44:38 +00:00
|
|
|
{
|
2011-09-28 19:13:20 +00:00
|
|
|
bodylen = snprintf(body, sizeof(body), resp, "IP_Routed");
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
|
|
|
else if(strcmp(var_name, "LastConnectionError") == 0)
|
2012-03-01 01:44:38 +00:00
|
|
|
{
|
2011-09-28 19:13:20 +00:00
|
|
|
bodylen = snprintf(body, sizeof(body), resp, "ERROR_NONE");
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else if(strcmp(var_name, "PortMappingNumberOfEntries") == 0)
|
|
|
|
{
|
|
|
|
char strn[10];
|
|
|
|
snprintf(strn, sizeof(strn), "%i",
|
|
|
|
upnp_get_portmapping_number_of_entries());
|
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-09-14 22:02:56 +00:00
|
|
|
action, ns,/*"urn:schemas-upnp-org:control-1-0",*/
|
2011-09-28 19:13:20 +00:00
|
|
|
strn, action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
syslog(LOG_NOTICE, "%s: Unknown: %s", action, var_name?var_name:"");
|
|
|
|
SoapError(h, 404, "Invalid Var");
|
|
|
|
}
|
|
|
|
|
2012-03-01 01:44:38 +00:00
|
|
|
ClearNameValueList(&data);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ENABLE_6FC_SERVICE
|
|
|
|
#ifndef ENABLE_IPV6
|
|
|
|
#error "ENABLE_6FC_SERVICE needs ENABLE_IPV6"
|
|
|
|
#endif
|
|
|
|
/* WANIPv6FirewallControl actions */
|
|
|
|
static void
|
2015-09-14 22:02:56 +00:00
|
|
|
GetFirewallStatus(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"<FirewallEnabled>%d</FirewallEnabled>"
|
|
|
|
"<InboundPinholeAllowed>%d</InboundPinholeAllowed>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
|
|
|
|
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-09-14 22:02:56 +00:00
|
|
|
action, ns, /*"urn:schemas-upnp-org:service:WANIPv6FirewallControl:1",*/
|
2014-03-13 10:26:53 +00:00
|
|
|
GETFLAG(IPV6FCFWDISABLEDMASK) ? 0 : 1,
|
|
|
|
GETFLAG(IPV6FCINBOUNDDISALLOWEDMASK) ? 0 : 1,
|
|
|
|
action);
|
2011-09-28 19:13:20 +00:00
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
CheckStatus(struct upnphttp * h)
|
|
|
|
{
|
2014-03-13 10:26:53 +00:00
|
|
|
if (GETFLAG(IPV6FCFWDISABLEDMASK))
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-04-23 22:51:48 +00:00
|
|
|
SoapError(h, 702, "FirewallDisabled");
|
2011-09-28 19:13:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-03-13 10:26:53 +00:00
|
|
|
else if(GETFLAG(IPV6FCINBOUNDDISALLOWEDMASK))
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
SoapError(h, 703, "InboundPinholeNotAllowed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static int connecthostport(const char * host, unsigned short port, char * result)
|
|
|
|
{
|
|
|
|
int s, n;
|
|
|
|
char hostname[INET6_ADDRSTRLEN];
|
|
|
|
char port_str[8], ifname[8], tmp[4];
|
|
|
|
struct addrinfo *ai, *p;
|
|
|
|
struct addrinfo hints;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
/* hints.ai_flags = AI_ADDRCONFIG; */
|
|
|
|
#ifdef AI_NUMERICSERV
|
|
|
|
hints.ai_flags = AI_NUMERICSERV;
|
|
|
|
#endif
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_family = AF_UNSPEC; /* AF_INET, AF_INET6 or AF_UNSPEC */
|
|
|
|
/* hints.ai_protocol = IPPROTO_TCP; */
|
|
|
|
snprintf(port_str, sizeof(port_str), "%hu", port);
|
|
|
|
strcpy(hostname, host);
|
|
|
|
if(!strncmp(host, "fe80", 4))
|
|
|
|
{
|
|
|
|
printf("Using an linklocal address\n");
|
|
|
|
strcpy(ifname, "%");
|
|
|
|
snprintf(tmp, sizeof(tmp), "%d", linklocal_index);
|
|
|
|
strcat(ifname, tmp);
|
|
|
|
strcat(hostname, ifname);
|
|
|
|
printf("host: %s\n", hostname);
|
|
|
|
}
|
|
|
|
n = getaddrinfo(hostname, port_str, &hints, &ai);
|
|
|
|
if(n != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "getaddrinfo() error : %s\n", gai_strerror(n));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
s = -1;
|
|
|
|
for(p = ai; p; p = p->ai_next)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
char tmp_host[256];
|
|
|
|
char tmp_service[256];
|
|
|
|
printf("ai_family=%d ai_socktype=%d ai_protocol=%d ai_addrlen=%d\n ",
|
|
|
|
p->ai_family, p->ai_socktype, p->ai_protocol, p->ai_addrlen);
|
|
|
|
getnameinfo(p->ai_addr, p->ai_addrlen, tmp_host, sizeof(tmp_host),
|
|
|
|
tmp_service, sizeof(tmp_service),
|
|
|
|
NI_NUMERICHOST | NI_NUMERICSERV);
|
|
|
|
printf(" host=%s service=%s\n", tmp_host, tmp_service);
|
|
|
|
#endif
|
|
|
|
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)p->ai_addr)->sin6_addr), result, INET6_ADDRSTRLEN);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-02-04 10:32:29 +00:00
|
|
|
/* Check the security policy rights
|
|
|
|
* Pinhole InternalClient address must correspond to the action sender
|
|
|
|
* returns 1 if it passes.
|
|
|
|
* call SoapError() and returns 0 if it fails
|
|
|
|
* Side effect : if int_ip is a hostname, convert it to litteral ipv6 */
|
2011-09-28 19:13:20 +00:00
|
|
|
static int
|
2012-04-20 22:09:52 +00:00
|
|
|
PinholeVerification(struct upnphttp * h, char * int_ip, unsigned short int_port)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2023-02-04 10:32:29 +00:00
|
|
|
char clientaddr_str[INET6_ADDRSTRLEN];
|
2012-04-20 22:09:52 +00:00
|
|
|
struct in6_addr result_ip;
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
/* if ip not valid assume hostname and convert */
|
2012-04-20 22:09:52 +00:00
|
|
|
if (inet_pton(AF_INET6, int_ip, &result_ip) <= 0)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2023-02-04 10:32:29 +00:00
|
|
|
int r;
|
|
|
|
struct addrinfo hints, *ai, *p;
|
|
|
|
|
|
|
|
syslog(LOG_INFO, "%s: InternalClient %s is not an IPv6, assume hostname and convert",
|
|
|
|
"PinholeVerification", int_ip);
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = 0; /* we may indicate which protocol IPPROTO_UDP / IPPROTO_TCP / IPPROTO_SCTP / IPPROTO_UDPLITE */
|
|
|
|
|
|
|
|
r = getaddrinfo(int_ip, NULL, &hints, &ai);
|
|
|
|
if (r == 0)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2020-06-05 08:13:13 +00:00
|
|
|
int found = 0;
|
2012-04-20 22:09:52 +00:00
|
|
|
for(p = ai; p; p = p->ai_next)
|
|
|
|
{
|
2020-06-05 08:13:13 +00:00
|
|
|
if(p->ai_family == AF_INET6)
|
|
|
|
{
|
2023-02-04 10:32:29 +00:00
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
result_ip = ((struct sockaddr_in6 *)p->ai_addr)->sin6_addr;
|
|
|
|
if (inet_ntop(AF_INET6, &result_ip, int_ip, sizeof(struct in6_addr)) == NULL)
|
|
|
|
syslog(LOG_WARNING, "%s: inet_ntop(): %m", "PinholeVerification");
|
|
|
|
syslog(LOG_INFO, "%s: InternalClient resolved as %s",
|
|
|
|
"PinholeVerification", int_ip);
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char tmp[48];
|
|
|
|
sockaddr_to_string(p->ai_addr, tmp, sizeof(tmp));
|
|
|
|
syslog(LOG_INFO, "%s: additionnal IPv6: %s",
|
|
|
|
"PinholeVerification", tmp);
|
|
|
|
}
|
2020-06-05 08:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
if (!found)
|
|
|
|
{
|
2023-02-04 10:32:29 +00:00
|
|
|
syslog(LOG_NOTICE, "%s: No IPv6 address for hostname '%s'",
|
|
|
|
"PinholeVerification", int_ip);
|
2020-06-05 08:13:13 +00:00
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return -1;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-02-04 10:32:29 +00:00
|
|
|
syslog(LOG_WARNING, "%s: Failed to convert hostname '%s' to IP address : %s",
|
|
|
|
"PinholeVerification", int_ip, gai_strerror(r));
|
2011-09-28 19:13:20 +00:00
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-04 10:32:29 +00:00
|
|
|
if(inet_ntop(AF_INET6, &(h->clientaddr_v6), clientaddr_str, INET6_ADDRSTRLEN) == NULL)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "inet_ntop: %m");
|
2023-02-04 10:32:29 +00:00
|
|
|
strncpy(clientaddr_str, "*ERROR*", sizeof(clientaddr_str));
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2023-02-04 10:32:29 +00:00
|
|
|
|
2023-02-11 22:36:56 +00:00
|
|
|
if(memcmp(&h->clientaddr_v6, &result_ip, sizeof(struct in6_addr)) != 0)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2023-02-04 10:32:29 +00:00
|
|
|
syslog(LOG_INFO, "%s: Client %s tried to access pinhole for internal %s and is not authorized",
|
|
|
|
"PinholeVerification", clientaddr_str, int_ip);
|
2011-09-28 19:13:20 +00:00
|
|
|
SoapError(h, 606, "Action not authorized");
|
|
|
|
return 0;
|
|
|
|
}
|
2023-02-04 10:32:29 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
else
|
|
|
|
{
|
|
|
|
syslog(LOG_DEBUG, "%s: sender %s == InternalClient %s",
|
|
|
|
"PinholeVerification", clientaddr_str, int_ip);
|
|
|
|
}
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
/* Pinhole InternalPort must be greater than or equal to 1024 */
|
2012-04-20 22:09:52 +00:00
|
|
|
if (int_port < 1024)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
syslog(LOG_INFO, "Client %s tried to access pinhole with port < 1024 and is not authorized to do it",
|
2023-02-04 10:32:29 +00:00
|
|
|
clientaddr_str);
|
2011-09-28 19:13:20 +00:00
|
|
|
SoapError(h, 606, "Action not authorized");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-10-30 18:53:08 +00:00
|
|
|
AddPinhole(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"<UniqueID>%d</UniqueID>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
|
|
|
struct NameValueParserData data;
|
|
|
|
char * rem_host, * rem_port, * int_ip, * int_port, * protocol, * leaseTime;
|
|
|
|
int uid = 0;
|
|
|
|
unsigned short iport, rport;
|
2012-04-20 14:53:34 +00:00
|
|
|
int ltime;
|
2012-04-20 22:09:52 +00:00
|
|
|
long proto;
|
2012-04-21 23:55:35 +00:00
|
|
|
char rem_ip[INET6_ADDRSTRLEN];
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
if(CheckStatus(h)==0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data);
|
|
|
|
rem_host = GetValueFromNameValueList(&data, "RemoteHost");
|
|
|
|
rem_port = GetValueFromNameValueList(&data, "RemotePort");
|
|
|
|
int_ip = GetValueFromNameValueList(&data, "InternalClient");
|
|
|
|
int_port = GetValueFromNameValueList(&data, "InternalPort");
|
|
|
|
protocol = GetValueFromNameValueList(&data, "Protocol");
|
|
|
|
leaseTime = GetValueFromNameValueList(&data, "LeaseTime");
|
|
|
|
|
2023-01-28 14:49:53 +00:00
|
|
|
#ifdef UPNP_STRICT
|
|
|
|
if (rem_port == NULL || rem_port[0] == '\0' || int_port == NULL || int_port[0] == '\0' )
|
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2012-04-20 14:53:34 +00:00
|
|
|
rport = (unsigned short)(rem_port ? atoi(rem_port) : 0);
|
|
|
|
iport = (unsigned short)(int_port ? atoi(int_port) : 0);
|
2012-04-20 22:09:52 +00:00
|
|
|
ltime = leaseTime ? atoi(leaseTime) : -1;
|
|
|
|
errno = 0;
|
|
|
|
proto = protocol ? strtol(protocol, NULL, 0) : -1;
|
|
|
|
if(errno != 0 || proto > 65535 || proto < 0)
|
|
|
|
{
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
goto clear_and_exit;
|
|
|
|
}
|
|
|
|
if(iport == 0)
|
|
|
|
{
|
|
|
|
SoapError(h, 706, "InternalPortWilcardingNotAllowed");
|
|
|
|
goto clear_and_exit;
|
|
|
|
}
|
2012-04-20 14:53:34 +00:00
|
|
|
|
|
|
|
/* In particular, [IGD2] RECOMMENDS that unauthenticated and
|
|
|
|
* unauthorized control points are only allowed to invoke
|
|
|
|
* this action with:
|
|
|
|
* - InternalPort value greater than or equal to 1024,
|
|
|
|
* - InternalClient value equals to the control point's IP address.
|
|
|
|
* It is REQUIRED that InternalClient cannot be one of IPv6
|
|
|
|
* addresses used by the gateway. */
|
2018-06-01 08:55:45 +00:00
|
|
|
if(!int_ip || int_ip[0] == '\0' || 0 == strcmp(int_ip, "*"))
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-04-20 22:09:52 +00:00
|
|
|
SoapError(h, 708, "WildCardNotPermittedInSrcIP");
|
|
|
|
goto clear_and_exit;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2012-04-21 23:55:35 +00:00
|
|
|
/* I guess it is useless to convert int_ip to literal ipv6 address */
|
2017-03-13 10:30:44 +00:00
|
|
|
if(rem_host)
|
|
|
|
{
|
|
|
|
/* trim */
|
|
|
|
while(isspace(rem_host[0]))
|
|
|
|
rem_host++;
|
|
|
|
}
|
2012-04-21 23:55:35 +00:00
|
|
|
/* rem_host should be converted to literal ipv6 : */
|
2017-03-13 10:30:44 +00:00
|
|
|
if(rem_host && (rem_host[0] != '\0') && (rem_host[0] != '*'))
|
2012-04-21 23:55:35 +00:00
|
|
|
{
|
|
|
|
struct addrinfo *ai, *p;
|
|
|
|
struct addrinfo hints;
|
|
|
|
int err;
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_family = AF_INET6;
|
|
|
|
/*hints.ai_flags = */
|
|
|
|
/* hints.ai_protocol = proto; */
|
|
|
|
err = getaddrinfo(rem_host, rem_port, &hints, &ai);
|
|
|
|
if(err == 0)
|
|
|
|
{
|
|
|
|
/* take the 1st IPv6 address */
|
|
|
|
for(p = ai; p; p = p->ai_next)
|
|
|
|
{
|
|
|
|
if(p->ai_family == AF_INET6)
|
|
|
|
{
|
|
|
|
inet_ntop(AF_INET6,
|
|
|
|
&(((struct sockaddr_in6 *)p->ai_addr)->sin6_addr),
|
|
|
|
rem_ip, sizeof(rem_ip));
|
|
|
|
syslog(LOG_INFO, "resolved '%s' to '%s'", rem_host, rem_ip);
|
|
|
|
rem_host = rem_ip;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, "AddPinhole : getaddrinfo(%s) : %s",
|
|
|
|
rem_host, gai_strerror(err));
|
|
|
|
#if 0
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
goto clear_and_exit;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2012-04-20 22:09:52 +00:00
|
|
|
if(proto == 65535)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-04-20 22:09:52 +00:00
|
|
|
SoapError(h, 707, "ProtocolWilcardingNotAllowed");
|
|
|
|
goto clear_and_exit;
|
|
|
|
}
|
|
|
|
if(proto != IPPROTO_UDP && proto != IPPROTO_TCP
|
|
|
|
#ifdef IPPROTO_UDPITE
|
|
|
|
&& atoi(protocol) != IPPROTO_UDPLITE
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
SoapError(h, 705, "ProtocolNotSupported");
|
|
|
|
goto clear_and_exit;
|
|
|
|
}
|
|
|
|
if(ltime < 1 || ltime > 86400)
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, "%s: LeaseTime=%d not supported, (ip=%s)",
|
|
|
|
action, ltime, int_ip);
|
2011-09-28 19:13:20 +00:00
|
|
|
SoapError(h, 402, "Invalid Args");
|
2012-04-20 22:09:52 +00:00
|
|
|
goto clear_and_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(PinholeVerification(h, int_ip, iport) <= 0)
|
|
|
|
goto clear_and_exit;
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2012-04-20 22:09:52 +00:00
|
|
|
syslog(LOG_INFO, "%s: (inbound) from [%s]:%hu to [%s]:%hu with proto %ld during %d sec",
|
|
|
|
action, rem_host?rem_host:"any",
|
|
|
|
rport, int_ip, iport,
|
|
|
|
proto, ltime);
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2012-04-20 14:53:34 +00:00
|
|
|
/* In cases where the RemoteHost, RemotePort, InternalPort,
|
|
|
|
* InternalClient and Protocol are the same than an existing pinhole,
|
|
|
|
* but LeaseTime is different, the device MUST extend the existing
|
|
|
|
* pinhole's lease time and return the UniqueID of the existing pinhole. */
|
2014-05-15 10:11:42 +00:00
|
|
|
r = upnp_add_inboundpinhole(rem_host, rport, int_ip, iport, proto, "IGD2 pinhole", ltime, &uid);
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
switch(r)
|
|
|
|
{
|
|
|
|
case 1: /* success */
|
2012-04-20 22:09:52 +00:00
|
|
|
bodylen = snprintf(body, sizeof(body),
|
|
|
|
resp, action,
|
2015-10-30 18:53:08 +00:00
|
|
|
ns/*"urn:schemas-upnp-org:service:WANIPv6FirewallControl:1"*/,
|
2012-04-20 22:09:52 +00:00
|
|
|
uid, action);
|
2011-09-28 19:13:20 +00:00
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
break;
|
|
|
|
case -1: /* not permitted */
|
|
|
|
SoapError(h, 701, "PinholeSpaceExhausted");
|
|
|
|
break;
|
|
|
|
default:
|
2024-01-01 20:36:18 +00:00
|
|
|
SoapError(h, 501, "Action Failed");
|
2011-09-28 19:13:20 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-04-20 22:09:52 +00:00
|
|
|
/* 606 Action not authorized
|
|
|
|
* 701 PinholeSpaceExhausted
|
|
|
|
* 702 FirewallDisabled
|
|
|
|
* 703 InboundPinholeNotAllowed
|
|
|
|
* 705 ProtocolNotSupported
|
|
|
|
* 706 InternalPortWildcardingNotAllowed
|
|
|
|
* 707 ProtocolWildcardingNotAllowed
|
|
|
|
* 708 WildCardNotPermittedInSrcIP */
|
|
|
|
clear_and_exit:
|
2011-09-28 19:13:20 +00:00
|
|
|
ClearNameValueList(&data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-10-30 18:53:08 +00:00
|
|
|
UpdatePinhole(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2015-10-30 18:53:08 +00:00
|
|
|
#if 0
|
2011-09-28 19:13:20 +00:00
|
|
|
static const char resp[] =
|
|
|
|
"<u:UpdatePinholeResponse "
|
|
|
|
"xmlns:u=\"urn:schemas-upnp-org:service:WANIPv6FirewallControl:1\">"
|
|
|
|
"</u:UpdatePinholeResponse>";
|
2015-10-30 18:53:08 +00:00
|
|
|
#endif
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
2011-09-28 19:13:20 +00:00
|
|
|
struct NameValueParserData data;
|
2012-04-23 22:50:20 +00:00
|
|
|
const char * uid_str, * leaseTime;
|
|
|
|
char iaddr[INET6_ADDRSTRLEN];
|
2011-09-28 19:13:20 +00:00
|
|
|
unsigned short iport;
|
2012-04-23 22:50:20 +00:00
|
|
|
int ltime;
|
|
|
|
int uid;
|
|
|
|
int n;
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
if(CheckStatus(h)==0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data);
|
2012-04-23 22:50:20 +00:00
|
|
|
uid_str = GetValueFromNameValueList(&data, "UniqueID");
|
2011-09-28 19:13:20 +00:00
|
|
|
leaseTime = GetValueFromNameValueList(&data, "NewLeaseTime");
|
2012-04-23 22:50:20 +00:00
|
|
|
uid = uid_str ? atoi(uid_str) : -1;
|
|
|
|
ltime = leaseTime ? atoi(leaseTime) : -1;
|
|
|
|
ClearNameValueList(&data);
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2012-04-23 22:50:20 +00:00
|
|
|
if(uid < 0 || uid > 65535 || ltime <= 0 || ltime > 86400)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-23 22:50:20 +00:00
|
|
|
/* Check that client is not updating an pinhole
|
2012-02-04 23:40:50 +00:00
|
|
|
* it doesn't have access to, because of its public access */
|
2012-04-23 22:50:20 +00:00
|
|
|
n = upnp_get_pinhole_info(uid, NULL, 0, NULL,
|
|
|
|
iaddr, sizeof(iaddr), &iport,
|
2014-05-15 10:11:42 +00:00
|
|
|
NULL, /* proto */
|
|
|
|
NULL, 0, /* desc, desclen */
|
|
|
|
NULL, NULL);
|
2012-04-23 22:50:20 +00:00
|
|
|
if (n >= 0)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-04-23 22:50:20 +00:00
|
|
|
if(PinholeVerification(h, iaddr, iport) <= 0)
|
|
|
|
return;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2012-04-23 22:50:20 +00:00
|
|
|
else if(n == -2)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-04-23 22:50:20 +00:00
|
|
|
SoapError(h, 704, "NoSuchEntry");
|
|
|
|
return;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-01 20:36:18 +00:00
|
|
|
SoapError(h, 501, "Action Failed");
|
2012-04-23 22:50:20 +00:00
|
|
|
return;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2012-04-23 22:50:20 +00:00
|
|
|
|
|
|
|
syslog(LOG_INFO, "%s: (inbound) updating lease duration to %d for pinhole with ID: %d",
|
|
|
|
action, ltime, uid);
|
|
|
|
|
|
|
|
n = upnp_update_inboundpinhole(uid, ltime);
|
|
|
|
if(n == -1)
|
|
|
|
SoapError(h, 704, "NoSuchEntry");
|
|
|
|
else if(n < 0)
|
2024-01-01 20:36:18 +00:00
|
|
|
SoapError(h, 501, "Action Failed");
|
2015-10-30 18:53:08 +00:00
|
|
|
else {
|
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
|
|
|
action, ns, action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-10-30 18:53:08 +00:00
|
|
|
GetOutboundPinholeTimeout(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"<OutboundPinholeTimeout>%d</OutboundPinholeTimeout>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
|
|
|
struct NameValueParserData data;
|
|
|
|
char * int_ip, * int_port, * rem_host, * rem_port, * protocol;
|
2015-10-30 18:53:08 +00:00
|
|
|
int opt=0;
|
|
|
|
/*int proto=0;*/
|
2011-09-28 19:13:20 +00:00
|
|
|
unsigned short iport, rport;
|
|
|
|
|
2014-03-13 10:26:53 +00:00
|
|
|
if (GETFLAG(IPV6FCFWDISABLEDMASK))
|
2012-04-20 14:53:34 +00:00
|
|
|
{
|
2012-04-23 22:51:48 +00:00
|
|
|
SoapError(h, 702, "FirewallDisabled");
|
2012-04-20 14:53:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data);
|
|
|
|
int_ip = GetValueFromNameValueList(&data, "InternalClient");
|
|
|
|
int_port = GetValueFromNameValueList(&data, "InternalPort");
|
|
|
|
rem_host = GetValueFromNameValueList(&data, "RemoteHost");
|
|
|
|
rem_port = GetValueFromNameValueList(&data, "RemotePort");
|
|
|
|
protocol = GetValueFromNameValueList(&data, "Protocol");
|
|
|
|
|
2018-12-18 22:47:54 +00:00
|
|
|
if (!int_port || !rem_port || !protocol)
|
2018-12-18 21:54:51 +00:00
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
rport = (unsigned short)atoi(rem_port);
|
|
|
|
iport = (unsigned short)atoi(int_port);
|
2015-10-30 18:53:08 +00:00
|
|
|
/*proto = atoi(protocol);*/
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
syslog(LOG_INFO, "%s: retrieving timeout for outbound pinhole from [%s]:%hu to [%s]:%hu protocol %s", action, int_ip, iport,rem_host, rport, protocol);
|
|
|
|
|
2012-04-23 22:50:20 +00:00
|
|
|
/* TODO */
|
|
|
|
r = -1;/*upnp_check_outbound_pinhole(proto, &opt);*/
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
switch(r)
|
|
|
|
{
|
|
|
|
case 1: /* success */
|
2012-04-23 22:50:20 +00:00
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-10-30 18:53:08 +00:00
|
|
|
action, ns/*"urn:schemas-upnp-org:service:WANIPv6FirewallControl:1"*/,
|
2012-04-23 22:50:20 +00:00
|
|
|
opt, action);
|
2011-09-28 19:13:20 +00:00
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
break;
|
|
|
|
case -5: /* Protocol not supported */
|
|
|
|
SoapError(h, 705, "ProtocolNotSupported");
|
|
|
|
break;
|
|
|
|
default:
|
2024-01-01 20:36:18 +00:00
|
|
|
SoapError(h, 501, "Action Failed");
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-10-30 18:53:08 +00:00
|
|
|
DeletePinhole(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-04-22 23:47:37 +00:00
|
|
|
int n;
|
2015-10-30 18:53:08 +00:00
|
|
|
#if 0
|
2011-09-28 19:13:20 +00:00
|
|
|
static const char resp[] =
|
|
|
|
"<u:DeletePinholeResponse "
|
|
|
|
"xmlns:u=\"urn:schemas-upnp-org:service:WANIPv6FirewallControl:1\">"
|
|
|
|
"</u:DeletePinholeResponse>";
|
2015-10-30 18:53:08 +00:00
|
|
|
#endif
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
struct NameValueParserData data;
|
2012-04-22 23:47:37 +00:00
|
|
|
const char * uid_str;
|
|
|
|
char iaddr[INET6_ADDRSTRLEN];
|
|
|
|
int proto;
|
2011-09-28 19:13:20 +00:00
|
|
|
unsigned short iport;
|
2012-04-22 23:47:37 +00:00
|
|
|
unsigned int leasetime;
|
|
|
|
int uid;
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2012-04-20 14:53:34 +00:00
|
|
|
if(CheckStatus(h)==0)
|
|
|
|
return;
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data);
|
2012-04-22 23:47:37 +00:00
|
|
|
uid_str = GetValueFromNameValueList(&data, "UniqueID");
|
|
|
|
uid = uid_str ? atoi(uid_str) : -1;
|
|
|
|
ClearNameValueList(&data);
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2012-04-22 23:47:37 +00:00
|
|
|
if(uid < 0 || uid > 65535)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-04 23:40:50 +00:00
|
|
|
/* Check that client is not deleting an pinhole
|
|
|
|
* it doesn't have access to, because of its public access */
|
2012-04-22 23:47:37 +00:00
|
|
|
n = upnp_get_pinhole_info(uid, NULL, 0, NULL,
|
|
|
|
iaddr, sizeof(iaddr), &iport,
|
2014-05-15 10:11:42 +00:00
|
|
|
&proto,
|
|
|
|
NULL, 0, /* desc, desclen */
|
|
|
|
&leasetime, NULL);
|
2012-04-22 23:47:37 +00:00
|
|
|
if (n >= 0)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-04-23 22:50:20 +00:00
|
|
|
if(PinholeVerification(h, iaddr, iport) <= 0)
|
2012-04-22 23:47:37 +00:00
|
|
|
return;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2012-04-22 23:47:37 +00:00
|
|
|
else if(n == -2)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-04-22 23:47:37 +00:00
|
|
|
SoapError(h, 704, "NoSuchEntry");
|
|
|
|
return;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-01 20:36:18 +00:00
|
|
|
SoapError(h, 501, "Action Failed");
|
2012-04-22 23:47:37 +00:00
|
|
|
return;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2012-04-22 23:47:37 +00:00
|
|
|
|
|
|
|
n = upnp_delete_inboundpinhole(uid);
|
|
|
|
if(n < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_INFO, "%s: (inbound) failed to remove pinhole with ID: %d",
|
|
|
|
action, uid);
|
2024-01-01 20:36:18 +00:00
|
|
|
SoapError(h, 501, "Action Failed");
|
2012-04-22 23:47:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
syslog(LOG_INFO, "%s: (inbound) pinhole with ID %d successfully removed",
|
|
|
|
action, uid);
|
2015-10-30 18:53:08 +00:00
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
|
|
|
action, ns, action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-10-30 18:53:08 +00:00
|
|
|
CheckPinholeWorking(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
|
|
|
"<IsWorking>%d</IsWorking>"
|
|
|
|
"</u:%sResponse>";
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
2012-04-23 22:50:20 +00:00
|
|
|
int r;
|
2011-09-28 19:13:20 +00:00
|
|
|
struct NameValueParserData data;
|
2012-04-23 22:50:20 +00:00
|
|
|
const char * uid_str;
|
|
|
|
int uid;
|
|
|
|
char iaddr[INET6_ADDRSTRLEN];
|
|
|
|
unsigned short iport;
|
|
|
|
unsigned int packets;
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2012-04-20 14:53:34 +00:00
|
|
|
if(CheckStatus(h)==0)
|
|
|
|
return;
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data);
|
2012-04-23 22:50:20 +00:00
|
|
|
uid_str = GetValueFromNameValueList(&data, "UniqueID");
|
|
|
|
uid = uid_str ? atoi(uid_str) : -1;
|
|
|
|
ClearNameValueList(&data);
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2012-04-23 22:50:20 +00:00
|
|
|
if(uid < 0 || uid > 65535)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-04 23:40:50 +00:00
|
|
|
/* Check that client is not checking a pinhole
|
|
|
|
* it doesn't have access to, because of its public access */
|
2012-04-23 22:50:20 +00:00
|
|
|
r = upnp_get_pinhole_info(uid,
|
|
|
|
NULL, 0, NULL,
|
|
|
|
iaddr, sizeof(iaddr), &iport,
|
2014-05-15 10:11:42 +00:00
|
|
|
NULL, /* proto */
|
|
|
|
NULL, 0, /* desc, desclen */
|
|
|
|
NULL, &packets);
|
2012-04-23 22:50:20 +00:00
|
|
|
if (r >= 0)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-04-23 22:50:20 +00:00
|
|
|
if(PinholeVerification(h, iaddr, iport) <= 0)
|
2011-09-28 19:13:20 +00:00
|
|
|
return ;
|
2012-04-23 22:50:20 +00:00
|
|
|
if(packets == 0)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2023-06-26 23:12:20 +00:00
|
|
|
SoapError(h, 709, "NoTrafficReceived");
|
2012-04-23 22:50:20 +00:00
|
|
|
return;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2012-04-23 22:50:20 +00:00
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-10-30 18:53:08 +00:00
|
|
|
action, ns/*"urn:schemas-upnp-org:service:WANIPv6FirewallControl:1"*/,
|
2012-04-23 22:50:20 +00:00
|
|
|
1, action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2012-04-23 22:50:20 +00:00
|
|
|
else if(r == -2)
|
2011-09-28 19:13:20 +00:00
|
|
|
SoapError(h, 704, "NoSuchEntry");
|
|
|
|
else
|
2024-01-01 20:36:18 +00:00
|
|
|
SoapError(h, 501, "Action Failed");
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-10-30 18:53:08 +00:00
|
|
|
GetPinholePackets(struct upnphttp * h, const char * action, const char * ns)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
2012-04-22 23:47:37 +00:00
|
|
|
"<PinholePackets>%u</PinholePackets>"
|
2011-09-28 19:13:20 +00:00
|
|
|
"</u:%sResponse>";
|
|
|
|
char body[512];
|
|
|
|
int bodylen;
|
|
|
|
struct NameValueParserData data;
|
2012-04-22 23:47:37 +00:00
|
|
|
const char * uid_str;
|
|
|
|
int n;
|
|
|
|
char iaddr[INET6_ADDRSTRLEN];
|
2011-09-28 19:13:20 +00:00
|
|
|
unsigned short iport;
|
2012-04-22 23:47:37 +00:00
|
|
|
unsigned int packets = 0;
|
|
|
|
int uid;
|
|
|
|
int proto;
|
|
|
|
unsigned int leasetime;
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2012-04-20 14:53:34 +00:00
|
|
|
if(CheckStatus(h)==0)
|
|
|
|
return;
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data);
|
2012-04-22 23:47:37 +00:00
|
|
|
uid_str = GetValueFromNameValueList(&data, "UniqueID");
|
|
|
|
uid = uid_str ? atoi(uid_str) : -1;
|
|
|
|
ClearNameValueList(&data);
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2012-04-22 23:47:37 +00:00
|
|
|
if(uid < 0 || uid > 65535)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-04 23:40:50 +00:00
|
|
|
/* Check that client is not getting infos of a pinhole
|
|
|
|
* it doesn't have access to, because of its public access */
|
2012-04-22 23:47:37 +00:00
|
|
|
n = upnp_get_pinhole_info(uid, NULL, 0, NULL,
|
|
|
|
iaddr, sizeof(iaddr), &iport,
|
2014-05-15 10:11:42 +00:00
|
|
|
&proto,
|
|
|
|
NULL, 0, /* desc, desclen */
|
|
|
|
&leasetime, &packets);
|
2012-04-22 23:47:37 +00:00
|
|
|
if (n >= 0)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-04-23 22:50:20 +00:00
|
|
|
if(PinholeVerification(h, iaddr, iport)<=0)
|
2011-09-28 19:13:20 +00:00
|
|
|
return ;
|
|
|
|
}
|
2012-04-22 23:47:37 +00:00
|
|
|
#if 0
|
2011-09-28 19:13:20 +00:00
|
|
|
else if(r == -4 || r == -1)
|
|
|
|
{
|
|
|
|
SoapError(h, 704, "NoSuchEntry");
|
|
|
|
}
|
2012-04-22 23:47:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-10-30 18:53:08 +00:00
|
|
|
action, ns/*"urn:schemas-upnp-org:service:WANIPv6FirewallControl:1"*/,
|
2012-04-22 23:47:37 +00:00
|
|
|
packets, action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-04-09 13:35:55 +00:00
|
|
|
#ifdef ENABLE_DP_SERVICE
|
|
|
|
static void
|
2015-10-30 18:53:08 +00:00
|
|
|
SendSetupMessage(struct upnphttp * h, const char * action, const char * ns)
|
2014-04-09 13:35:55 +00:00
|
|
|
{
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
2015-12-12 07:36:19 +00:00
|
|
|
"<OutMessage>%s</OutMessage>"
|
2014-04-09 13:35:55 +00:00
|
|
|
"</u:%sResponse>";
|
|
|
|
char body[1024];
|
|
|
|
int bodylen;
|
|
|
|
struct NameValueParserData data;
|
|
|
|
const char * ProtocolType; /* string */
|
|
|
|
const char * InMessage; /* base64 */
|
|
|
|
const char * OutMessage = ""; /* base64 */
|
|
|
|
|
|
|
|
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data);
|
2015-12-12 10:33:30 +00:00
|
|
|
ProtocolType = GetValueFromNameValueList(&data, "ProtocolType"); /* string */
|
|
|
|
InMessage = GetValueFromNameValueList(&data, "InMessage"); /* base64 */
|
2014-04-09 13:35:55 +00:00
|
|
|
|
|
|
|
if(ProtocolType == NULL || InMessage == NULL)
|
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 402, "Invalid Args");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*if(strcmp(ProtocolType, "DeviceProtection:1") != 0)*/
|
|
|
|
if(strcmp(ProtocolType, "WPS") != 0)
|
|
|
|
{
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
SoapError(h, 600, "Argument Value Invalid"); /* 703 ? */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* TODO : put here code for WPS */
|
|
|
|
|
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-10-30 18:53:08 +00:00
|
|
|
action, ns/*"urn:schemas-upnp-org:service:DeviceProtection:1"*/,
|
2014-04-09 13:35:55 +00:00
|
|
|
OutMessage, action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
ClearNameValueList(&data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-10-30 18:53:08 +00:00
|
|
|
GetSupportedProtocols(struct upnphttp * h, const char * action, const char * ns)
|
2014-04-09 13:35:55 +00:00
|
|
|
{
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
2015-12-12 23:24:05 +00:00
|
|
|
"<ProtocolList><![CDATA[%s]]></ProtocolList>"
|
2014-04-09 13:35:55 +00:00
|
|
|
"</u:%sResponse>";
|
|
|
|
char body[1024];
|
|
|
|
int bodylen;
|
|
|
|
const char * ProtocolList =
|
|
|
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
|
|
|
"<SupportedProtocols xmlns=\"urn:schemas-upnp-org:gw:DeviceProtection\""
|
|
|
|
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
|
|
|
|
" xsi:schemaLocation=\"urn:schemas-upnp-org:gw:DeviceProtection"
|
|
|
|
" http://www.upnp.org/schemas/gw/DeviceProtection-v1.xsd\">"
|
|
|
|
"<Introduction><Name>WPS</Name></Introduction>"
|
|
|
|
"<Login><Name>PKCS5</Name></Login>"
|
|
|
|
"</SupportedProtocols>";
|
|
|
|
|
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-10-30 18:53:08 +00:00
|
|
|
action, ns/*"urn:schemas-upnp-org:service:DeviceProtection:1"*/,
|
2014-04-09 13:35:55 +00:00
|
|
|
ProtocolList, action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-10-30 18:53:08 +00:00
|
|
|
GetAssignedRoles(struct upnphttp * h, const char * action, const char * ns)
|
2014-04-09 13:35:55 +00:00
|
|
|
{
|
|
|
|
static const char resp[] =
|
|
|
|
"<u:%sResponse "
|
|
|
|
"xmlns:u=\"%s\">"
|
2015-12-12 07:36:19 +00:00
|
|
|
"<RoleList>%s</RoleList>"
|
2014-04-09 13:35:55 +00:00
|
|
|
"</u:%sResponse>";
|
|
|
|
char body[1024];
|
|
|
|
int bodylen;
|
2014-04-15 13:40:03 +00:00
|
|
|
const char * RoleList = "Public"; /* list of roles separated by spaces */
|
2014-04-09 13:35:55 +00:00
|
|
|
|
2014-04-09 13:43:39 +00:00
|
|
|
#ifdef ENABLE_HTTPS
|
2014-04-15 13:40:03 +00:00
|
|
|
if(h->ssl != NULL) {
|
|
|
|
/* we should get the Roles of the session (based on client certificate) */
|
|
|
|
X509 * peercert;
|
|
|
|
peercert = SSL_get_peer_certificate(h->ssl);
|
|
|
|
if(peercert != NULL) {
|
|
|
|
RoleList = "Admin Basic";
|
|
|
|
X509_free(peercert);
|
|
|
|
}
|
|
|
|
}
|
2014-04-09 13:43:39 +00:00
|
|
|
#endif
|
2014-04-09 13:35:55 +00:00
|
|
|
|
|
|
|
bodylen = snprintf(body, sizeof(body), resp,
|
2015-10-30 18:53:08 +00:00
|
|
|
action, ns/*"urn:schemas-upnp-org:service:DeviceProtection:1"*/,
|
2014-04-09 13:35:55 +00:00
|
|
|
RoleList, action);
|
|
|
|
BuildSendAndCloseSoapResp(h, body, bodylen);
|
|
|
|
}
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
/* Windows XP as client send the following requests :
|
|
|
|
* GetConnectionTypeInfo
|
|
|
|
* GetNATRSIPStatus
|
|
|
|
* ? GetTotalBytesSent - WANCommonInterfaceConfig
|
|
|
|
* ? GetTotalBytesReceived - idem
|
|
|
|
* ? GetTotalPacketsSent - idem
|
|
|
|
* ? GetTotalPacketsReceived - idem
|
|
|
|
* GetCommonLinkProperties - idem
|
|
|
|
* GetStatusInfo - WANIPConnection
|
|
|
|
* GetExternalIPAddress
|
|
|
|
* QueryStateVariable / ConnectionStatus!
|
|
|
|
*/
|
2012-03-01 01:44:38 +00:00
|
|
|
static const struct
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-03-01 01:44:38 +00:00
|
|
|
const char * methodName;
|
2015-09-14 22:02:56 +00:00
|
|
|
void (*methodImpl)(struct upnphttp *, const char *, const char *);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
soapMethods[] =
|
|
|
|
{
|
|
|
|
/* WANCommonInterfaceConfig */
|
|
|
|
{ "QueryStateVariable", QueryStateVariable},
|
|
|
|
{ "GetTotalBytesSent", GetTotalBytesSent},
|
|
|
|
{ "GetTotalBytesReceived", GetTotalBytesReceived},
|
|
|
|
{ "GetTotalPacketsSent", GetTotalPacketsSent},
|
|
|
|
{ "GetTotalPacketsReceived", GetTotalPacketsReceived},
|
|
|
|
{ "GetCommonLinkProperties", GetCommonLinkProperties},
|
|
|
|
{ "GetStatusInfo", GetStatusInfo},
|
|
|
|
/* WANIPConnection */
|
|
|
|
{ "GetConnectionTypeInfo", GetConnectionTypeInfo },
|
|
|
|
{ "GetNATRSIPStatus", GetNATRSIPStatus},
|
|
|
|
{ "GetExternalIPAddress", GetExternalIPAddress},
|
|
|
|
{ "AddPortMapping", AddPortMapping},
|
|
|
|
{ "DeletePortMapping", DeletePortMapping},
|
|
|
|
{ "GetGenericPortMappingEntry", GetGenericPortMappingEntry},
|
|
|
|
{ "GetSpecificPortMappingEntry", GetSpecificPortMappingEntry},
|
|
|
|
/* Required in WANIPConnection:2 */
|
|
|
|
{ "SetConnectionType", SetConnectionType},
|
|
|
|
{ "RequestConnection", RequestConnection},
|
|
|
|
{ "ForceTermination", ForceTermination},
|
|
|
|
{ "AddAnyPortMapping", AddAnyPortMapping},
|
|
|
|
{ "DeletePortMappingRange", DeletePortMappingRange},
|
|
|
|
{ "GetListOfPortMappings", GetListOfPortMappings},
|
|
|
|
#ifdef ENABLE_L3F_SERVICE
|
|
|
|
/* Layer3Forwarding */
|
|
|
|
{ "SetDefaultConnectionService", SetDefaultConnectionService},
|
|
|
|
{ "GetDefaultConnectionService", GetDefaultConnectionService},
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_6FC_SERVICE
|
|
|
|
/* WANIPv6FirewallControl */
|
2012-04-19 22:06:24 +00:00
|
|
|
{ "GetFirewallStatus", GetFirewallStatus}, /* Required */
|
|
|
|
{ "AddPinhole", AddPinhole}, /* Required */
|
|
|
|
{ "UpdatePinhole", UpdatePinhole}, /* Required */
|
|
|
|
{ "GetOutboundPinholeTimeout", GetOutboundPinholeTimeout}, /* Optional */
|
|
|
|
{ "DeletePinhole", DeletePinhole}, /* Required */
|
|
|
|
{ "CheckPinholeWorking", CheckPinholeWorking}, /* Optional */
|
|
|
|
{ "GetPinholePackets", GetPinholePackets}, /* Required */
|
2014-04-09 13:35:55 +00:00
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_DP_SERVICE
|
|
|
|
/* DeviceProtection */
|
|
|
|
{ "SendSetupMessage", SendSetupMessage}, /* Required */
|
|
|
|
{ "GetSupportedProtocols", GetSupportedProtocols}, /* Required */
|
|
|
|
{ "GetAssignedRoles", GetAssignedRoles}, /* Required */
|
2011-09-28 19:13:20 +00:00
|
|
|
#endif
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
ExecuteSoapAction(struct upnphttp * h, const char * action, int n)
|
|
|
|
{
|
|
|
|
char * p;
|
|
|
|
int i, len, methodlen;
|
2015-09-14 22:02:56 +00:00
|
|
|
char namespace[256];
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2014-11-28 13:29:24 +00:00
|
|
|
/* SoapAction example :
|
|
|
|
* urn:schemas-upnp-org:service:WANIPConnection:1#GetStatusInfo */
|
2023-12-29 17:22:38 +00:00
|
|
|
p = memchr(action, '#', n);
|
|
|
|
if(p) {
|
2015-09-14 22:02:56 +00:00
|
|
|
for(i = 0; i < ((int)sizeof(namespace) - 1) && (action + i) < p; i++)
|
|
|
|
namespace[i] = action[i];
|
|
|
|
namespace[i] = '\0';
|
2011-09-28 19:13:20 +00:00
|
|
|
p++;
|
2023-12-29 17:24:50 +00:00
|
|
|
methodlen = n - (int)(p - action);
|
|
|
|
if(p[methodlen-1] == '"') {
|
|
|
|
methodlen--; /* remove the ending " */
|
|
|
|
}
|
2014-11-28 13:29:24 +00:00
|
|
|
/*syslog(LOG_DEBUG, "SoapMethod: %.*s %d %d %p %p %d",
|
|
|
|
methodlen, p, methodlen, n, action, p, (int)(p - action));*/
|
|
|
|
for(i = 0; soapMethods[i].methodName; i++) {
|
2011-09-28 19:13:20 +00:00
|
|
|
len = strlen(soapMethods[i].methodName);
|
2014-11-28 13:29:24 +00:00
|
|
|
if((len == methodlen) && memcmp(p, soapMethods[i].methodName, len) == 0) {
|
2014-02-28 15:40:20 +00:00
|
|
|
#ifdef DEBUG
|
2015-12-15 11:14:05 +00:00
|
|
|
syslog(LOG_DEBUG, "Remote Call of SoapMethod '%s' %s",
|
|
|
|
soapMethods[i].methodName, namespace);
|
2014-11-28 13:29:24 +00:00
|
|
|
#endif /* DEBUG */
|
2015-09-14 22:02:56 +00:00
|
|
|
soapMethods[i].methodImpl(h, soapMethods[i].methodName, namespace);
|
2011-09-28 19:13:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 11:14:05 +00:00
|
|
|
syslog(LOG_NOTICE, "SoapMethod: Unknown: %.*s %s", methodlen, p, namespace);
|
2014-11-28 13:29:24 +00:00
|
|
|
} else {
|
|
|
|
syslog(LOG_NOTICE, "cannot parse SoapAction");
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SoapError(h, 401, "Invalid Action");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Standard Errors:
|
|
|
|
*
|
|
|
|
* errorCode errorDescription Description
|
|
|
|
* -------- ---------------- -----------
|
|
|
|
* 401 Invalid Action No action by that name at this service.
|
|
|
|
* 402 Invalid Args Could be any of the following: not enough in args,
|
2012-03-01 01:44:38 +00:00
|
|
|
* too many in args, no in arg by that name,
|
2011-09-28 19:13:20 +00:00
|
|
|
* one or more in args are of the wrong data type.
|
|
|
|
* 403 Out of Sync Out of synchronization.
|
|
|
|
* 501 Action Failed May be returned in current state of service
|
|
|
|
* prevents invoking that action.
|
|
|
|
* 600-699 TBD Common action errors. Defined by UPnP Forum
|
|
|
|
* Technical Committee.
|
|
|
|
* 700-799 TBD Action-specific errors for standard actions.
|
|
|
|
* Defined by UPnP Forum working committee.
|
2012-03-01 01:44:38 +00:00
|
|
|
* 800-899 TBD Action-specific errors for non-standard actions.
|
2011-09-28 19:13:20 +00:00
|
|
|
* Defined by UPnP vendor.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
SoapError(struct upnphttp * h, int errCode, const char * errDesc)
|
|
|
|
{
|
2012-03-01 01:44:38 +00:00
|
|
|
static const char resp[] =
|
2011-09-28 19:13:20 +00:00
|
|
|
"<s:Envelope "
|
|
|
|
"xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
|
|
|
|
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
|
|
|
|
"<s:Body>"
|
|
|
|
"<s:Fault>"
|
|
|
|
"<faultcode>s:Client</faultcode>"
|
|
|
|
"<faultstring>UPnPError</faultstring>"
|
|
|
|
"<detail>"
|
|
|
|
"<UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\">"
|
|
|
|
"<errorCode>%d</errorCode>"
|
|
|
|
"<errorDescription>%s</errorDescription>"
|
|
|
|
"</UPnPError>"
|
|
|
|
"</detail>"
|
|
|
|
"</s:Fault>"
|
|
|
|
"</s:Body>"
|
|
|
|
"</s:Envelope>";
|
|
|
|
|
|
|
|
char body[2048];
|
|
|
|
int bodylen;
|
|
|
|
|
|
|
|
syslog(LOG_INFO, "Returning UPnPError %d: %s", errCode, errDesc);
|
|
|
|
bodylen = snprintf(body, sizeof(body), resp, errCode, errDesc);
|
|
|
|
BuildResp2_upnphttp(h, 500, "Internal Server Error", body, bodylen);
|
2011-11-18 11:23:16 +00:00
|
|
|
SendRespAndClose_upnphttp(h);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|