139beb93cSSam Leffler /*
239beb93cSSam Leffler * UPnP WPS Device - Web connections
339beb93cSSam Leffler * Copyright (c) 2000-2003 Intel Corporation
439beb93cSSam Leffler * Copyright (c) 2006-2007 Sony Corporation
539beb93cSSam Leffler * Copyright (c) 2008-2009 Atheros Communications
639beb93cSSam Leffler * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
739beb93cSSam Leffler *
839beb93cSSam Leffler * See wps_upnp.c for more details on licensing and code history.
939beb93cSSam Leffler */
1039beb93cSSam Leffler
1139beb93cSSam Leffler #include "includes.h"
1239beb93cSSam Leffler
1339beb93cSSam Leffler #include "common.h"
1439beb93cSSam Leffler #include "base64.h"
1539beb93cSSam Leffler #include "uuid.h"
1639beb93cSSam Leffler #include "httpread.h"
17e28a4053SRui Paulo #include "http_server.h"
1839beb93cSSam Leffler #include "wps_i.h"
1939beb93cSSam Leffler #include "wps_upnp.h"
2039beb93cSSam Leffler #include "wps_upnp_i.h"
21e28a4053SRui Paulo #include "upnp_xml.h"
2239beb93cSSam Leffler
2339beb93cSSam Leffler /***************************************************************************
2439beb93cSSam Leffler * Web connections (we serve pages of info about ourselves, handle
2539beb93cSSam Leffler * requests, etc. etc.).
2639beb93cSSam Leffler **************************************************************************/
2739beb93cSSam Leffler
2839beb93cSSam Leffler #define WEB_CONNECTION_TIMEOUT_SEC 30 /* Drop web connection after t.o. */
2939beb93cSSam Leffler #define WEB_CONNECTION_MAX_READ 8000 /* Max we'll read for TCP request */
3039beb93cSSam Leffler #define MAX_WEB_CONNECTIONS 10 /* max simultaneous web connects */
3139beb93cSSam Leffler
3239beb93cSSam Leffler
3339beb93cSSam Leffler static const char *urn_wfawlanconfig =
3439beb93cSSam Leffler "urn:schemas-wifialliance-org:service:WFAWLANConfig:1";
3539beb93cSSam Leffler static const char *http_server_hdr =
3639beb93cSSam Leffler "Server: unspecified, UPnP/1.0, unspecified\r\n";
3739beb93cSSam Leffler static const char *http_connection_close =
3839beb93cSSam Leffler "Connection: close\r\n";
3939beb93cSSam Leffler
4039beb93cSSam Leffler /*
4139beb93cSSam Leffler * "Files" that we serve via HTTP. The format of these files is given by
4239beb93cSSam Leffler * WFA WPS specifications. Extra white space has been removed to save space.
4339beb93cSSam Leffler */
4439beb93cSSam Leffler
4539beb93cSSam Leffler static const char wps_scpd_xml[] =
4639beb93cSSam Leffler "<?xml version=\"1.0\"?>\n"
4739beb93cSSam Leffler "<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\">\n"
4839beb93cSSam Leffler "<specVersion><major>1</major><minor>0</minor></specVersion>\n"
4939beb93cSSam Leffler "<actionList>\n"
5039beb93cSSam Leffler "<action>\n"
5139beb93cSSam Leffler "<name>GetDeviceInfo</name>\n"
5239beb93cSSam Leffler "<argumentList>\n"
5339beb93cSSam Leffler "<argument>\n"
5439beb93cSSam Leffler "<name>NewDeviceInfo</name>\n"
5539beb93cSSam Leffler "<direction>out</direction>\n"
5639beb93cSSam Leffler "<relatedStateVariable>DeviceInfo</relatedStateVariable>\n"
5739beb93cSSam Leffler "</argument>\n"
5839beb93cSSam Leffler "</argumentList>\n"
5939beb93cSSam Leffler "</action>\n"
6039beb93cSSam Leffler "<action>\n"
6139beb93cSSam Leffler "<name>PutMessage</name>\n"
6239beb93cSSam Leffler "<argumentList>\n"
6339beb93cSSam Leffler "<argument>\n"
6439beb93cSSam Leffler "<name>NewInMessage</name>\n"
6539beb93cSSam Leffler "<direction>in</direction>\n"
6639beb93cSSam Leffler "<relatedStateVariable>InMessage</relatedStateVariable>\n"
6739beb93cSSam Leffler "</argument>\n"
6839beb93cSSam Leffler "<argument>\n"
6939beb93cSSam Leffler "<name>NewOutMessage</name>\n"
7039beb93cSSam Leffler "<direction>out</direction>\n"
7139beb93cSSam Leffler "<relatedStateVariable>OutMessage</relatedStateVariable>\n"
7239beb93cSSam Leffler "</argument>\n"
7339beb93cSSam Leffler "</argumentList>\n"
7439beb93cSSam Leffler "</action>\n"
7539beb93cSSam Leffler "<action>\n"
7639beb93cSSam Leffler "<name>PutWLANResponse</name>\n"
7739beb93cSSam Leffler "<argumentList>\n"
7839beb93cSSam Leffler "<argument>\n"
7939beb93cSSam Leffler "<name>NewMessage</name>\n"
8039beb93cSSam Leffler "<direction>in</direction>\n"
8139beb93cSSam Leffler "<relatedStateVariable>Message</relatedStateVariable>\n"
8239beb93cSSam Leffler "</argument>\n"
8339beb93cSSam Leffler "<argument>\n"
8439beb93cSSam Leffler "<name>NewWLANEventType</name>\n"
8539beb93cSSam Leffler "<direction>in</direction>\n"
8639beb93cSSam Leffler "<relatedStateVariable>WLANEventType</relatedStateVariable>\n"
8739beb93cSSam Leffler "</argument>\n"
8839beb93cSSam Leffler "<argument>\n"
8939beb93cSSam Leffler "<name>NewWLANEventMAC</name>\n"
9039beb93cSSam Leffler "<direction>in</direction>\n"
9139beb93cSSam Leffler "<relatedStateVariable>WLANEventMAC</relatedStateVariable>\n"
9239beb93cSSam Leffler "</argument>\n"
9339beb93cSSam Leffler "</argumentList>\n"
9439beb93cSSam Leffler "</action>\n"
9539beb93cSSam Leffler "<action>\n"
9639beb93cSSam Leffler "<name>SetSelectedRegistrar</name>\n"
9739beb93cSSam Leffler "<argumentList>\n"
9839beb93cSSam Leffler "<argument>\n"
9939beb93cSSam Leffler "<name>NewMessage</name>\n"
10039beb93cSSam Leffler "<direction>in</direction>\n"
10139beb93cSSam Leffler "<relatedStateVariable>Message</relatedStateVariable>\n"
10239beb93cSSam Leffler "</argument>\n"
10339beb93cSSam Leffler "</argumentList>\n"
10439beb93cSSam Leffler "</action>\n"
10539beb93cSSam Leffler "</actionList>\n"
10639beb93cSSam Leffler "<serviceStateTable>\n"
10739beb93cSSam Leffler "<stateVariable sendEvents=\"no\">\n"
10839beb93cSSam Leffler "<name>Message</name>\n"
10939beb93cSSam Leffler "<dataType>bin.base64</dataType>\n"
11039beb93cSSam Leffler "</stateVariable>\n"
11139beb93cSSam Leffler "<stateVariable sendEvents=\"no\">\n"
11239beb93cSSam Leffler "<name>InMessage</name>\n"
11339beb93cSSam Leffler "<dataType>bin.base64</dataType>\n"
11439beb93cSSam Leffler "</stateVariable>\n"
11539beb93cSSam Leffler "<stateVariable sendEvents=\"no\">\n"
11639beb93cSSam Leffler "<name>OutMessage</name>\n"
11739beb93cSSam Leffler "<dataType>bin.base64</dataType>\n"
11839beb93cSSam Leffler "</stateVariable>\n"
11939beb93cSSam Leffler "<stateVariable sendEvents=\"no\">\n"
12039beb93cSSam Leffler "<name>DeviceInfo</name>\n"
12139beb93cSSam Leffler "<dataType>bin.base64</dataType>\n"
12239beb93cSSam Leffler "</stateVariable>\n"
12339beb93cSSam Leffler "<stateVariable sendEvents=\"yes\">\n"
12439beb93cSSam Leffler "<name>APStatus</name>\n"
12539beb93cSSam Leffler "<dataType>ui1</dataType>\n"
12639beb93cSSam Leffler "</stateVariable>\n"
12739beb93cSSam Leffler "<stateVariable sendEvents=\"yes\">\n"
12839beb93cSSam Leffler "<name>STAStatus</name>\n"
12939beb93cSSam Leffler "<dataType>ui1</dataType>\n"
13039beb93cSSam Leffler "</stateVariable>\n"
13139beb93cSSam Leffler "<stateVariable sendEvents=\"yes\">\n"
13239beb93cSSam Leffler "<name>WLANEvent</name>\n"
13339beb93cSSam Leffler "<dataType>bin.base64</dataType>\n"
13439beb93cSSam Leffler "</stateVariable>\n"
13539beb93cSSam Leffler "<stateVariable sendEvents=\"no\">\n"
13639beb93cSSam Leffler "<name>WLANEventType</name>\n"
13739beb93cSSam Leffler "<dataType>ui1</dataType>\n"
13839beb93cSSam Leffler "</stateVariable>\n"
13939beb93cSSam Leffler "<stateVariable sendEvents=\"no\">\n"
14039beb93cSSam Leffler "<name>WLANEventMAC</name>\n"
14139beb93cSSam Leffler "<dataType>string</dataType>\n"
14239beb93cSSam Leffler "</stateVariable>\n"
14339beb93cSSam Leffler "<stateVariable sendEvents=\"no\">\n"
14439beb93cSSam Leffler "<name>WLANResponse</name>\n"
14539beb93cSSam Leffler "<dataType>bin.base64</dataType>\n"
14639beb93cSSam Leffler "</stateVariable>\n"
14739beb93cSSam Leffler "</serviceStateTable>\n"
14839beb93cSSam Leffler "</scpd>\n"
14939beb93cSSam Leffler ;
15039beb93cSSam Leffler
15139beb93cSSam Leffler
15239beb93cSSam Leffler static const char *wps_device_xml_prefix =
15339beb93cSSam Leffler "<?xml version=\"1.0\"?>\n"
15439beb93cSSam Leffler "<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\n"
15539beb93cSSam Leffler "<specVersion>\n"
15639beb93cSSam Leffler "<major>1</major>\n"
15739beb93cSSam Leffler "<minor>0</minor>\n"
15839beb93cSSam Leffler "</specVersion>\n"
15939beb93cSSam Leffler "<device>\n"
16039beb93cSSam Leffler "<deviceType>urn:schemas-wifialliance-org:device:WFADevice:1"
16139beb93cSSam Leffler "</deviceType>\n";
16239beb93cSSam Leffler
16339beb93cSSam Leffler static const char *wps_device_xml_postfix =
16439beb93cSSam Leffler "<serviceList>\n"
16539beb93cSSam Leffler "<service>\n"
16639beb93cSSam Leffler "<serviceType>urn:schemas-wifialliance-org:service:WFAWLANConfig:1"
16739beb93cSSam Leffler "</serviceType>\n"
16839beb93cSSam Leffler "<serviceId>urn:wifialliance-org:serviceId:WFAWLANConfig1</serviceId>"
16939beb93cSSam Leffler "\n"
17039beb93cSSam Leffler "<SCPDURL>" UPNP_WPS_SCPD_XML_FILE "</SCPDURL>\n"
17139beb93cSSam Leffler "<controlURL>" UPNP_WPS_DEVICE_CONTROL_FILE "</controlURL>\n"
17239beb93cSSam Leffler "<eventSubURL>" UPNP_WPS_DEVICE_EVENT_FILE "</eventSubURL>\n"
17339beb93cSSam Leffler "</service>\n"
17439beb93cSSam Leffler "</serviceList>\n"
17539beb93cSSam Leffler "</device>\n"
17639beb93cSSam Leffler "</root>\n";
17739beb93cSSam Leffler
17839beb93cSSam Leffler
17939beb93cSSam Leffler /* format_wps_device_xml -- produce content of "file" wps_device.xml
18039beb93cSSam Leffler * (UPNP_WPS_DEVICE_XML_FILE)
18139beb93cSSam Leffler */
format_wps_device_xml(struct upnp_wps_device_interface * iface,struct upnp_wps_device_sm * sm,struct wpabuf * buf)1825b9c547cSRui Paulo static void format_wps_device_xml(struct upnp_wps_device_interface *iface,
1835b9c547cSRui Paulo struct upnp_wps_device_sm *sm,
18439beb93cSSam Leffler struct wpabuf *buf)
18539beb93cSSam Leffler {
18639beb93cSSam Leffler const char *s;
18739beb93cSSam Leffler char uuid_string[80];
18839beb93cSSam Leffler
18939beb93cSSam Leffler wpabuf_put_str(buf, wps_device_xml_prefix);
19039beb93cSSam Leffler
19139beb93cSSam Leffler /*
19239beb93cSSam Leffler * Add required fields with default values if not configured. Add
19339beb93cSSam Leffler * optional and recommended fields only if configured.
19439beb93cSSam Leffler */
195f05cddf9SRui Paulo s = iface->wps->friendly_name;
19639beb93cSSam Leffler s = ((s && *s) ? s : "WPS Access Point");
19739beb93cSSam Leffler xml_add_tagged_data(buf, "friendlyName", s);
19839beb93cSSam Leffler
199f05cddf9SRui Paulo s = iface->wps->dev.manufacturer;
20039beb93cSSam Leffler s = ((s && *s) ? s : "");
20139beb93cSSam Leffler xml_add_tagged_data(buf, "manufacturer", s);
20239beb93cSSam Leffler
203f05cddf9SRui Paulo if (iface->wps->manufacturer_url)
20439beb93cSSam Leffler xml_add_tagged_data(buf, "manufacturerURL",
205f05cddf9SRui Paulo iface->wps->manufacturer_url);
20639beb93cSSam Leffler
207f05cddf9SRui Paulo if (iface->wps->model_description)
20839beb93cSSam Leffler xml_add_tagged_data(buf, "modelDescription",
209f05cddf9SRui Paulo iface->wps->model_description);
21039beb93cSSam Leffler
211f05cddf9SRui Paulo s = iface->wps->dev.model_name;
21239beb93cSSam Leffler s = ((s && *s) ? s : "");
21339beb93cSSam Leffler xml_add_tagged_data(buf, "modelName", s);
21439beb93cSSam Leffler
215f05cddf9SRui Paulo if (iface->wps->dev.model_number)
21639beb93cSSam Leffler xml_add_tagged_data(buf, "modelNumber",
217f05cddf9SRui Paulo iface->wps->dev.model_number);
21839beb93cSSam Leffler
219f05cddf9SRui Paulo if (iface->wps->model_url)
220f05cddf9SRui Paulo xml_add_tagged_data(buf, "modelURL", iface->wps->model_url);
22139beb93cSSam Leffler
222f05cddf9SRui Paulo if (iface->wps->dev.serial_number)
22339beb93cSSam Leffler xml_add_tagged_data(buf, "serialNumber",
224f05cddf9SRui Paulo iface->wps->dev.serial_number);
22539beb93cSSam Leffler
226f05cddf9SRui Paulo uuid_bin2str(iface->wps->uuid, uuid_string, sizeof(uuid_string));
22739beb93cSSam Leffler s = uuid_string;
22839beb93cSSam Leffler /* Need "uuid:" prefix, thus we can't use xml_add_tagged_data()
22939beb93cSSam Leffler * easily...
23039beb93cSSam Leffler */
23139beb93cSSam Leffler wpabuf_put_str(buf, "<UDN>uuid:");
23239beb93cSSam Leffler xml_data_encode(buf, s, os_strlen(s));
23339beb93cSSam Leffler wpabuf_put_str(buf, "</UDN>\n");
23439beb93cSSam Leffler
235f05cddf9SRui Paulo if (iface->wps->upc)
236f05cddf9SRui Paulo xml_add_tagged_data(buf, "UPC", iface->wps->upc);
23739beb93cSSam Leffler
23839beb93cSSam Leffler wpabuf_put_str(buf, wps_device_xml_postfix);
23939beb93cSSam Leffler }
24039beb93cSSam Leffler
24139beb93cSSam Leffler
http_put_reply_code(struct wpabuf * buf,enum http_reply_code code)24239beb93cSSam Leffler static void http_put_reply_code(struct wpabuf *buf, enum http_reply_code code)
24339beb93cSSam Leffler {
24439beb93cSSam Leffler wpabuf_put_str(buf, "HTTP/1.1 ");
24539beb93cSSam Leffler switch (code) {
24639beb93cSSam Leffler case HTTP_OK:
24739beb93cSSam Leffler wpabuf_put_str(buf, "200 OK\r\n");
24839beb93cSSam Leffler break;
24939beb93cSSam Leffler case HTTP_BAD_REQUEST:
25039beb93cSSam Leffler wpabuf_put_str(buf, "400 Bad request\r\n");
25139beb93cSSam Leffler break;
25239beb93cSSam Leffler case HTTP_PRECONDITION_FAILED:
25339beb93cSSam Leffler wpabuf_put_str(buf, "412 Precondition failed\r\n");
25439beb93cSSam Leffler break;
25539beb93cSSam Leffler case HTTP_UNIMPLEMENTED:
25639beb93cSSam Leffler wpabuf_put_str(buf, "501 Unimplemented\r\n");
25739beb93cSSam Leffler break;
25839beb93cSSam Leffler case HTTP_INTERNAL_SERVER_ERROR:
25939beb93cSSam Leffler default:
26039beb93cSSam Leffler wpabuf_put_str(buf, "500 Internal server error\r\n");
26139beb93cSSam Leffler break;
26239beb93cSSam Leffler }
26339beb93cSSam Leffler }
26439beb93cSSam Leffler
26539beb93cSSam Leffler
http_put_date(struct wpabuf * buf)26639beb93cSSam Leffler static void http_put_date(struct wpabuf *buf)
26739beb93cSSam Leffler {
26839beb93cSSam Leffler wpabuf_put_str(buf, "Date: ");
26939beb93cSSam Leffler format_date(buf);
27039beb93cSSam Leffler wpabuf_put_str(buf, "\r\n");
27139beb93cSSam Leffler }
27239beb93cSSam Leffler
27339beb93cSSam Leffler
http_put_empty(struct wpabuf * buf,enum http_reply_code code)27439beb93cSSam Leffler static void http_put_empty(struct wpabuf *buf, enum http_reply_code code)
27539beb93cSSam Leffler {
27639beb93cSSam Leffler http_put_reply_code(buf, code);
27739beb93cSSam Leffler wpabuf_put_str(buf, http_server_hdr);
27839beb93cSSam Leffler wpabuf_put_str(buf, http_connection_close);
27939beb93cSSam Leffler wpabuf_put_str(buf, "Content-Length: 0\r\n"
28039beb93cSSam Leffler "\r\n");
28139beb93cSSam Leffler }
28239beb93cSSam Leffler
28339beb93cSSam Leffler
28439beb93cSSam Leffler /* Given that we have received a header w/ GET, act upon it
28539beb93cSSam Leffler *
28639beb93cSSam Leffler * Format of GET (case-insensitive):
28739beb93cSSam Leffler *
28839beb93cSSam Leffler * First line must be:
28939beb93cSSam Leffler * GET /<file> HTTP/1.1
29039beb93cSSam Leffler * Since we don't do anything fancy we just ignore other lines.
29139beb93cSSam Leffler *
29239beb93cSSam Leffler * Our response (if no error) which includes only required lines is:
29339beb93cSSam Leffler * HTTP/1.1 200 OK
29439beb93cSSam Leffler * Connection: close
29539beb93cSSam Leffler * Content-Type: text/xml
29639beb93cSSam Leffler * Date: <rfc1123-date>
29739beb93cSSam Leffler *
29839beb93cSSam Leffler * Header lines must end with \r\n
29939beb93cSSam Leffler * Per RFC 2616, content-length: is not required but connection:close
30039beb93cSSam Leffler * would appear to be required (given that we will be closing it!).
30139beb93cSSam Leffler */
web_connection_parse_get(struct upnp_wps_device_sm * sm,struct http_request * hreq,const char * filename)302e28a4053SRui Paulo static void web_connection_parse_get(struct upnp_wps_device_sm *sm,
303780fb4a2SCy Schubert struct http_request *hreq,
304780fb4a2SCy Schubert const char *filename)
30539beb93cSSam Leffler {
30639beb93cSSam Leffler struct wpabuf *buf; /* output buffer, allocated */
30739beb93cSSam Leffler char *put_length_here;
30839beb93cSSam Leffler char *body_start;
30939beb93cSSam Leffler enum {
31039beb93cSSam Leffler GET_DEVICE_XML_FILE,
31139beb93cSSam Leffler GET_SCPD_XML_FILE
31239beb93cSSam Leffler } req;
31339beb93cSSam Leffler size_t extra_len = 0;
31439beb93cSSam Leffler int body_length;
31539beb93cSSam Leffler char len_buf[10];
316f05cddf9SRui Paulo struct upnp_wps_device_interface *iface;
317f05cddf9SRui Paulo
318f05cddf9SRui Paulo iface = dl_list_first(&sm->interfaces,
319f05cddf9SRui Paulo struct upnp_wps_device_interface, list);
3205b9c547cSRui Paulo if (iface == NULL) {
3215b9c547cSRui Paulo http_request_deinit(hreq);
3225b9c547cSRui Paulo return;
3235b9c547cSRui Paulo }
32439beb93cSSam Leffler
32539beb93cSSam Leffler /*
32639beb93cSSam Leffler * It is not required that filenames be case insensitive but it is
32739beb93cSSam Leffler * allowed and cannot hurt here.
32839beb93cSSam Leffler */
32939beb93cSSam Leffler if (os_strcasecmp(filename, UPNP_WPS_DEVICE_XML_FILE) == 0) {
33039beb93cSSam Leffler wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP GET for device XML");
33139beb93cSSam Leffler req = GET_DEVICE_XML_FILE;
33239beb93cSSam Leffler extra_len = 3000;
333f05cddf9SRui Paulo if (iface->wps->friendly_name)
334f05cddf9SRui Paulo extra_len += os_strlen(iface->wps->friendly_name);
335f05cddf9SRui Paulo if (iface->wps->manufacturer_url)
336f05cddf9SRui Paulo extra_len += os_strlen(iface->wps->manufacturer_url);
337f05cddf9SRui Paulo if (iface->wps->model_description)
338f05cddf9SRui Paulo extra_len += os_strlen(iface->wps->model_description);
339f05cddf9SRui Paulo if (iface->wps->model_url)
340f05cddf9SRui Paulo extra_len += os_strlen(iface->wps->model_url);
341f05cddf9SRui Paulo if (iface->wps->upc)
342f05cddf9SRui Paulo extra_len += os_strlen(iface->wps->upc);
34339beb93cSSam Leffler } else if (!os_strcasecmp(filename, UPNP_WPS_SCPD_XML_FILE)) {
34439beb93cSSam Leffler wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP GET for SCPD XML");
34539beb93cSSam Leffler req = GET_SCPD_XML_FILE;
34639beb93cSSam Leffler extra_len = os_strlen(wps_scpd_xml);
34739beb93cSSam Leffler } else {
34839beb93cSSam Leffler /* File not found */
34939beb93cSSam Leffler wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP GET file not found: %s",
35039beb93cSSam Leffler filename);
35139beb93cSSam Leffler buf = wpabuf_alloc(200);
352e28a4053SRui Paulo if (buf == NULL) {
353e28a4053SRui Paulo http_request_deinit(hreq);
35439beb93cSSam Leffler return;
355e28a4053SRui Paulo }
35639beb93cSSam Leffler wpabuf_put_str(buf,
35739beb93cSSam Leffler "HTTP/1.1 404 Not Found\r\n"
35839beb93cSSam Leffler "Connection: close\r\n");
35939beb93cSSam Leffler
36039beb93cSSam Leffler http_put_date(buf);
36139beb93cSSam Leffler
36239beb93cSSam Leffler /* terminating empty line */
36339beb93cSSam Leffler wpabuf_put_str(buf, "\r\n");
36439beb93cSSam Leffler
36539beb93cSSam Leffler goto send_buf;
36639beb93cSSam Leffler }
36739beb93cSSam Leffler
36839beb93cSSam Leffler buf = wpabuf_alloc(1000 + extra_len);
369e28a4053SRui Paulo if (buf == NULL) {
370e28a4053SRui Paulo http_request_deinit(hreq);
37139beb93cSSam Leffler return;
372e28a4053SRui Paulo }
37339beb93cSSam Leffler
37439beb93cSSam Leffler wpabuf_put_str(buf,
37539beb93cSSam Leffler "HTTP/1.1 200 OK\r\n"
37639beb93cSSam Leffler "Content-Type: text/xml; charset=\"utf-8\"\r\n");
37739beb93cSSam Leffler wpabuf_put_str(buf, "Server: Unspecified, UPnP/1.0, Unspecified\r\n");
37839beb93cSSam Leffler wpabuf_put_str(buf, "Connection: close\r\n");
37939beb93cSSam Leffler wpabuf_put_str(buf, "Content-Length: ");
38039beb93cSSam Leffler /*
38139beb93cSSam Leffler * We will paste the length in later, leaving some extra whitespace.
38239beb93cSSam Leffler * HTTP code is supposed to be tolerant of extra whitespace.
38339beb93cSSam Leffler */
38439beb93cSSam Leffler put_length_here = wpabuf_put(buf, 0);
38539beb93cSSam Leffler wpabuf_put_str(buf, " \r\n");
38639beb93cSSam Leffler
38739beb93cSSam Leffler http_put_date(buf);
38839beb93cSSam Leffler
38939beb93cSSam Leffler /* terminating empty line */
39039beb93cSSam Leffler wpabuf_put_str(buf, "\r\n");
39139beb93cSSam Leffler
39239beb93cSSam Leffler body_start = wpabuf_put(buf, 0);
39339beb93cSSam Leffler
39439beb93cSSam Leffler switch (req) {
39539beb93cSSam Leffler case GET_DEVICE_XML_FILE:
3965b9c547cSRui Paulo format_wps_device_xml(iface, sm, buf);
39739beb93cSSam Leffler break;
39839beb93cSSam Leffler case GET_SCPD_XML_FILE:
39939beb93cSSam Leffler wpabuf_put_str(buf, wps_scpd_xml);
40039beb93cSSam Leffler break;
40139beb93cSSam Leffler }
40239beb93cSSam Leffler
40339beb93cSSam Leffler /* Now patch in the content length at the end */
40439beb93cSSam Leffler body_length = (char *) wpabuf_put(buf, 0) - body_start;
40539beb93cSSam Leffler os_snprintf(len_buf, 10, "%d", body_length);
40639beb93cSSam Leffler os_memcpy(put_length_here, len_buf, os_strlen(len_buf));
40739beb93cSSam Leffler
40839beb93cSSam Leffler send_buf:
409e28a4053SRui Paulo http_request_send_and_deinit(hreq, buf);
41039beb93cSSam Leffler }
41139beb93cSSam Leffler
41239beb93cSSam Leffler
wps_upnp_peer_del(struct upnp_wps_peer * peer)413780fb4a2SCy Schubert static void wps_upnp_peer_del(struct upnp_wps_peer *peer)
414780fb4a2SCy Schubert {
415780fb4a2SCy Schubert dl_list_del(&peer->list);
416780fb4a2SCy Schubert if (peer->wps)
417780fb4a2SCy Schubert wps_deinit(peer->wps);
418780fb4a2SCy Schubert os_free(peer);
419780fb4a2SCy Schubert }
420780fb4a2SCy Schubert
421780fb4a2SCy Schubert
42239beb93cSSam Leffler static enum http_reply_code
web_process_get_device_info(struct upnp_wps_device_sm * sm,struct wpabuf ** reply,const char ** replyname)42339beb93cSSam Leffler web_process_get_device_info(struct upnp_wps_device_sm *sm,
42439beb93cSSam Leffler struct wpabuf **reply, const char **replyname)
42539beb93cSSam Leffler {
42639beb93cSSam Leffler static const char *name = "NewDeviceInfo";
427e28a4053SRui Paulo struct wps_config cfg;
428f05cddf9SRui Paulo struct upnp_wps_device_interface *iface;
429f05cddf9SRui Paulo struct upnp_wps_peer *peer;
430f05cddf9SRui Paulo
431f05cddf9SRui Paulo iface = dl_list_first(&sm->interfaces,
432f05cddf9SRui Paulo struct upnp_wps_device_interface, list);
43339beb93cSSam Leffler
43439beb93cSSam Leffler wpa_printf(MSG_DEBUG, "WPS UPnP: GetDeviceInfo");
435e28a4053SRui Paulo
4365b9c547cSRui Paulo if (!iface || iface->ctx->ap_pin == NULL)
43739beb93cSSam Leffler return HTTP_INTERNAL_SERVER_ERROR;
438e28a4053SRui Paulo
439780fb4a2SCy Schubert peer = os_zalloc(sizeof(*peer));
440780fb4a2SCy Schubert if (!peer)
441780fb4a2SCy Schubert return HTTP_INTERNAL_SERVER_ERROR;
4425b9c547cSRui Paulo
443e28a4053SRui Paulo /*
444e28a4053SRui Paulo * Request for DeviceInfo, i.e., M1 TLVs. This is a start of WPS
445e28a4053SRui Paulo * registration over UPnP with the AP acting as an Enrollee. It should
446e28a4053SRui Paulo * be noted that this is frequently used just to get the device data,
447e28a4053SRui Paulo * i.e., there may not be any intent to actually complete the
448e28a4053SRui Paulo * registration.
449e28a4053SRui Paulo */
450e28a4053SRui Paulo
451e28a4053SRui Paulo os_memset(&cfg, 0, sizeof(cfg));
452f05cddf9SRui Paulo cfg.wps = iface->wps;
453f05cddf9SRui Paulo cfg.pin = (u8 *) iface->ctx->ap_pin;
454f05cddf9SRui Paulo cfg.pin_len = os_strlen(iface->ctx->ap_pin);
455e28a4053SRui Paulo peer->wps = wps_init(&cfg);
456e28a4053SRui Paulo if (peer->wps) {
457e28a4053SRui Paulo enum wsc_op_code op_code;
458e28a4053SRui Paulo *reply = wps_get_msg(peer->wps, &op_code);
459e28a4053SRui Paulo if (*reply == NULL) {
460e28a4053SRui Paulo wps_deinit(peer->wps);
461e28a4053SRui Paulo peer->wps = NULL;
462e28a4053SRui Paulo }
463e28a4053SRui Paulo } else
464e28a4053SRui Paulo *reply = NULL;
46539beb93cSSam Leffler if (*reply == NULL) {
46639beb93cSSam Leffler wpa_printf(MSG_INFO, "WPS UPnP: Failed to get DeviceInfo");
467780fb4a2SCy Schubert os_free(peer);
46839beb93cSSam Leffler return HTTP_INTERNAL_SERVER_ERROR;
46939beb93cSSam Leffler }
470780fb4a2SCy Schubert
471780fb4a2SCy Schubert if (dl_list_len(&iface->peers) > 3) {
472780fb4a2SCy Schubert struct upnp_wps_peer *old;
473780fb4a2SCy Schubert
474780fb4a2SCy Schubert old = dl_list_first(&iface->peers, struct upnp_wps_peer, list);
475780fb4a2SCy Schubert if (old) {
476780fb4a2SCy Schubert wpa_printf(MSG_DEBUG, "WPS UPnP: Drop oldest active session");
477780fb4a2SCy Schubert wps_upnp_peer_del(old);
478780fb4a2SCy Schubert }
479780fb4a2SCy Schubert }
480780fb4a2SCy Schubert dl_list_add_tail(&iface->peers, &peer->list);
481780fb4a2SCy Schubert /* TODO: Could schedule a timeout to free the entry */
482780fb4a2SCy Schubert
48339beb93cSSam Leffler *replyname = name;
48439beb93cSSam Leffler return HTTP_OK;
48539beb93cSSam Leffler }
48639beb93cSSam Leffler
48739beb93cSSam Leffler
48839beb93cSSam Leffler static enum http_reply_code
web_process_put_message(struct upnp_wps_device_sm * sm,char * data,struct wpabuf ** reply,const char ** replyname)48939beb93cSSam Leffler web_process_put_message(struct upnp_wps_device_sm *sm, char *data,
49039beb93cSSam Leffler struct wpabuf **reply, const char **replyname)
49139beb93cSSam Leffler {
49239beb93cSSam Leffler struct wpabuf *msg;
49339beb93cSSam Leffler static const char *name = "NewOutMessage";
49439beb93cSSam Leffler enum http_reply_code ret;
495e28a4053SRui Paulo enum wps_process_res res;
496e28a4053SRui Paulo enum wsc_op_code op_code;
497f05cddf9SRui Paulo struct upnp_wps_device_interface *iface;
498780fb4a2SCy Schubert struct wps_parse_attr attr;
499780fb4a2SCy Schubert struct upnp_wps_peer *tmp, *peer;
500f05cddf9SRui Paulo
501f05cddf9SRui Paulo iface = dl_list_first(&sm->interfaces,
502f05cddf9SRui Paulo struct upnp_wps_device_interface, list);
5035b9c547cSRui Paulo if (!iface)
5045b9c547cSRui Paulo return HTTP_INTERNAL_SERVER_ERROR;
50539beb93cSSam Leffler
50639beb93cSSam Leffler /*
50739beb93cSSam Leffler * PutMessage is used by external UPnP-based Registrar to perform WPS
50839beb93cSSam Leffler * operation with the access point itself; as compared with
50939beb93cSSam Leffler * PutWLANResponse which is for proxying.
51039beb93cSSam Leffler */
51139beb93cSSam Leffler wpa_printf(MSG_DEBUG, "WPS UPnP: PutMessage");
512e28a4053SRui Paulo msg = xml_get_base64_item(data, "NewInMessage", &ret);
51339beb93cSSam Leffler if (msg == NULL)
51439beb93cSSam Leffler return ret;
515780fb4a2SCy Schubert
516780fb4a2SCy Schubert if (wps_parse_msg(msg, &attr)) {
517780fb4a2SCy Schubert wpa_printf(MSG_DEBUG,
518780fb4a2SCy Schubert "WPS UPnP: Could not parse PutMessage - NewInMessage");
519780fb4a2SCy Schubert wpabuf_free(msg);
520780fb4a2SCy Schubert return HTTP_BAD_REQUEST;
521780fb4a2SCy Schubert }
522780fb4a2SCy Schubert
523780fb4a2SCy Schubert /* Find a matching active peer session */
524780fb4a2SCy Schubert peer = NULL;
525780fb4a2SCy Schubert dl_list_for_each(tmp, &iface->peers, struct upnp_wps_peer, list) {
526780fb4a2SCy Schubert if (!tmp->wps)
527780fb4a2SCy Schubert continue;
528780fb4a2SCy Schubert if (attr.enrollee_nonce &&
529780fb4a2SCy Schubert os_memcmp(tmp->wps->nonce_e, attr.enrollee_nonce,
530780fb4a2SCy Schubert WPS_NONCE_LEN) != 0)
531780fb4a2SCy Schubert continue; /* Enrollee nonce mismatch */
532780fb4a2SCy Schubert if (attr.msg_type &&
533780fb4a2SCy Schubert *attr.msg_type != WPS_M2 &&
534780fb4a2SCy Schubert *attr.msg_type != WPS_M2D &&
535780fb4a2SCy Schubert attr.registrar_nonce &&
536780fb4a2SCy Schubert os_memcmp(tmp->wps->nonce_r, attr.registrar_nonce,
537780fb4a2SCy Schubert WPS_NONCE_LEN) != 0)
538780fb4a2SCy Schubert continue; /* Registrar nonce mismatch */
539780fb4a2SCy Schubert peer = tmp;
540780fb4a2SCy Schubert break;
541780fb4a2SCy Schubert }
542780fb4a2SCy Schubert if (!peer) {
543780fb4a2SCy Schubert /*
544780fb4a2SCy Schubert Try to use the first entry in case message could work with
545780fb4a2SCy Schubert * it. The actual handler function will reject this, if needed.
546780fb4a2SCy Schubert * This maintains older behavior where only a single peer entry
547780fb4a2SCy Schubert * was supported.
548780fb4a2SCy Schubert */
549780fb4a2SCy Schubert peer = dl_list_first(&iface->peers, struct upnp_wps_peer, list);
550780fb4a2SCy Schubert }
551780fb4a2SCy Schubert if (!peer || !peer->wps) {
552780fb4a2SCy Schubert wpa_printf(MSG_DEBUG, "WPS UPnP: No active peer entry found");
553780fb4a2SCy Schubert wpabuf_free(msg);
554780fb4a2SCy Schubert return HTTP_BAD_REQUEST;
555780fb4a2SCy Schubert }
556780fb4a2SCy Schubert
557780fb4a2SCy Schubert res = wps_process_msg(peer->wps, WSC_UPnP, msg);
558780fb4a2SCy Schubert if (res == WPS_FAILURE) {
559e28a4053SRui Paulo *reply = NULL;
560780fb4a2SCy Schubert wpa_printf(MSG_DEBUG, "WPS UPnP: Drop active peer session");
561780fb4a2SCy Schubert wps_upnp_peer_del(peer);
562780fb4a2SCy Schubert } else {
563780fb4a2SCy Schubert *reply = wps_get_msg(peer->wps, &op_code);
564780fb4a2SCy Schubert }
56539beb93cSSam Leffler wpabuf_free(msg);
56639beb93cSSam Leffler if (*reply == NULL)
56739beb93cSSam Leffler return HTTP_INTERNAL_SERVER_ERROR;
56839beb93cSSam Leffler *replyname = name;
56939beb93cSSam Leffler return HTTP_OK;
57039beb93cSSam Leffler }
57139beb93cSSam Leffler
57239beb93cSSam Leffler
57339beb93cSSam Leffler static enum http_reply_code
web_process_put_wlan_response(struct upnp_wps_device_sm * sm,char * data,struct wpabuf ** reply,const char ** replyname)57439beb93cSSam Leffler web_process_put_wlan_response(struct upnp_wps_device_sm *sm, char *data,
57539beb93cSSam Leffler struct wpabuf **reply, const char **replyname)
57639beb93cSSam Leffler {
57739beb93cSSam Leffler struct wpabuf *msg;
57839beb93cSSam Leffler enum http_reply_code ret;
57939beb93cSSam Leffler u8 macaddr[ETH_ALEN];
58039beb93cSSam Leffler int ev_type;
58139beb93cSSam Leffler int type;
58239beb93cSSam Leffler char *val;
583f05cddf9SRui Paulo struct upnp_wps_device_interface *iface;
584f05cddf9SRui Paulo int ok = 0;
58539beb93cSSam Leffler
58639beb93cSSam Leffler /*
58739beb93cSSam Leffler * External UPnP-based Registrar is passing us a message to be proxied
58839beb93cSSam Leffler * over to a Wi-Fi -based client of ours.
58939beb93cSSam Leffler */
59039beb93cSSam Leffler
59139beb93cSSam Leffler wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse");
592e28a4053SRui Paulo msg = xml_get_base64_item(data, "NewMessage", &ret);
593e28a4053SRui Paulo if (msg == NULL) {
594e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WPS UPnP: Could not extract NewMessage "
595e28a4053SRui Paulo "from PutWLANResponse");
59639beb93cSSam Leffler return ret;
597e28a4053SRui Paulo }
598e28a4053SRui Paulo val = xml_get_first_item(data, "NewWLANEventType");
599e28a4053SRui Paulo if (val == NULL) {
600e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WPS UPnP: No NewWLANEventType in "
601e28a4053SRui Paulo "PutWLANResponse");
60239beb93cSSam Leffler wpabuf_free(msg);
60339beb93cSSam Leffler return UPNP_ARG_VALUE_INVALID;
60439beb93cSSam Leffler }
60539beb93cSSam Leffler ev_type = atol(val);
60639beb93cSSam Leffler os_free(val);
607e28a4053SRui Paulo val = xml_get_first_item(data, "NewWLANEventMAC");
608e28a4053SRui Paulo if (val == NULL) {
609e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WPS UPnP: No NewWLANEventMAC in "
610e28a4053SRui Paulo "PutWLANResponse");
611e28a4053SRui Paulo wpabuf_free(msg);
612e28a4053SRui Paulo return UPNP_ARG_VALUE_INVALID;
613e28a4053SRui Paulo }
614e28a4053SRui Paulo if (hwaddr_aton(val, macaddr)) {
615e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WPS UPnP: Invalid NewWLANEventMAC in "
616e28a4053SRui Paulo "PutWLANResponse: '%s'", val);
617f05cddf9SRui Paulo #ifdef CONFIG_WPS_STRICT
618f05cddf9SRui Paulo {
619f05cddf9SRui Paulo struct wps_parse_attr attr;
620f05cddf9SRui Paulo if (wps_parse_msg(msg, &attr) < 0 || attr.version2) {
621f05cddf9SRui Paulo wpabuf_free(msg);
622f05cddf9SRui Paulo os_free(val);
623f05cddf9SRui Paulo return UPNP_ARG_VALUE_INVALID;
624f05cddf9SRui Paulo }
625f05cddf9SRui Paulo }
626f05cddf9SRui Paulo #endif /* CONFIG_WPS_STRICT */
627e28a4053SRui Paulo if (hwaddr_aton2(val, macaddr) > 0) {
628e28a4053SRui Paulo /*
629e28a4053SRui Paulo * At least some versions of Intel PROset seem to be
630e28a4053SRui Paulo * using dot-deliminated MAC address format here.
631e28a4053SRui Paulo */
632e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WPS UPnP: Workaround - allow "
633e28a4053SRui Paulo "incorrect MAC address format in "
634f05cddf9SRui Paulo "NewWLANEventMAC: %s -> " MACSTR,
635f05cddf9SRui Paulo val, MAC2STR(macaddr));
636e28a4053SRui Paulo } else {
63739beb93cSSam Leffler wpabuf_free(msg);
63839beb93cSSam Leffler os_free(val);
63939beb93cSSam Leffler return UPNP_ARG_VALUE_INVALID;
64039beb93cSSam Leffler }
641e28a4053SRui Paulo }
64239beb93cSSam Leffler os_free(val);
64339beb93cSSam Leffler if (ev_type == UPNP_WPS_WLANEVENT_TYPE_EAP) {
64439beb93cSSam Leffler struct wps_parse_attr attr;
64539beb93cSSam Leffler if (wps_parse_msg(msg, &attr) < 0 ||
64639beb93cSSam Leffler attr.msg_type == NULL)
64739beb93cSSam Leffler type = -1;
64839beb93cSSam Leffler else
64939beb93cSSam Leffler type = *attr.msg_type;
65039beb93cSSam Leffler wpa_printf(MSG_DEBUG, "WPS UPnP: Message Type %d", type);
65139beb93cSSam Leffler } else
65239beb93cSSam Leffler type = -1;
653f05cddf9SRui Paulo dl_list_for_each(iface, &sm->interfaces,
654f05cddf9SRui Paulo struct upnp_wps_device_interface, list) {
655f05cddf9SRui Paulo if (iface->ctx->rx_req_put_wlan_response &&
656f05cddf9SRui Paulo iface->ctx->rx_req_put_wlan_response(iface->priv, ev_type,
657f05cddf9SRui Paulo macaddr, msg, type)
658f05cddf9SRui Paulo == 0)
659f05cddf9SRui Paulo ok = 1;
660f05cddf9SRui Paulo }
661f05cddf9SRui Paulo
662f05cddf9SRui Paulo if (!ok) {
66339beb93cSSam Leffler wpa_printf(MSG_INFO, "WPS UPnP: Fail: sm->ctx->"
66439beb93cSSam Leffler "rx_req_put_wlan_response");
66539beb93cSSam Leffler wpabuf_free(msg);
66639beb93cSSam Leffler return HTTP_INTERNAL_SERVER_ERROR;
66739beb93cSSam Leffler }
66839beb93cSSam Leffler wpabuf_free(msg);
66939beb93cSSam Leffler *replyname = NULL;
67039beb93cSSam Leffler *reply = NULL;
67139beb93cSSam Leffler return HTTP_OK;
67239beb93cSSam Leffler }
67339beb93cSSam Leffler
67439beb93cSSam Leffler
find_er_addr(struct subscription * s,struct sockaddr_in * cli)675e28a4053SRui Paulo static int find_er_addr(struct subscription *s, struct sockaddr_in *cli)
676e28a4053SRui Paulo {
677e28a4053SRui Paulo struct subscr_addr *a;
678e28a4053SRui Paulo
679e28a4053SRui Paulo dl_list_for_each(a, &s->addr_list, struct subscr_addr, list) {
680e28a4053SRui Paulo if (cli->sin_addr.s_addr == a->saddr.sin_addr.s_addr)
681e28a4053SRui Paulo return 1;
682e28a4053SRui Paulo }
683e28a4053SRui Paulo return 0;
684e28a4053SRui Paulo }
685e28a4053SRui Paulo
686e28a4053SRui Paulo
find_er(struct upnp_wps_device_sm * sm,struct sockaddr_in * cli)687e28a4053SRui Paulo static struct subscription * find_er(struct upnp_wps_device_sm *sm,
688e28a4053SRui Paulo struct sockaddr_in *cli)
689e28a4053SRui Paulo {
690e28a4053SRui Paulo struct subscription *s;
691e28a4053SRui Paulo dl_list_for_each(s, &sm->subscriptions, struct subscription, list)
692e28a4053SRui Paulo if (find_er_addr(s, cli))
693e28a4053SRui Paulo return s;
694e28a4053SRui Paulo return NULL;
695e28a4053SRui Paulo }
696e28a4053SRui Paulo
697e28a4053SRui Paulo
69839beb93cSSam Leffler static enum http_reply_code
web_process_set_selected_registrar(struct upnp_wps_device_sm * sm,struct sockaddr_in * cli,char * data,struct wpabuf ** reply,const char ** replyname)699e28a4053SRui Paulo web_process_set_selected_registrar(struct upnp_wps_device_sm *sm,
700e28a4053SRui Paulo struct sockaddr_in *cli, char *data,
70139beb93cSSam Leffler struct wpabuf **reply,
70239beb93cSSam Leffler const char **replyname)
70339beb93cSSam Leffler {
70439beb93cSSam Leffler struct wpabuf *msg;
70539beb93cSSam Leffler enum http_reply_code ret;
706e28a4053SRui Paulo struct subscription *s;
707f05cddf9SRui Paulo struct upnp_wps_device_interface *iface;
708f05cddf9SRui Paulo int err = 0;
70939beb93cSSam Leffler
71039beb93cSSam Leffler wpa_printf(MSG_DEBUG, "WPS UPnP: SetSelectedRegistrar");
711e28a4053SRui Paulo s = find_er(sm, cli);
712e28a4053SRui Paulo if (s == NULL) {
713e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WPS UPnP: Ignore SetSelectedRegistrar "
714e28a4053SRui Paulo "from unknown ER");
715e28a4053SRui Paulo return UPNP_ACTION_FAILED;
716e28a4053SRui Paulo }
717e28a4053SRui Paulo msg = xml_get_base64_item(data, "NewMessage", &ret);
71839beb93cSSam Leffler if (msg == NULL)
71939beb93cSSam Leffler return ret;
720f05cddf9SRui Paulo dl_list_for_each(iface, &sm->interfaces,
721f05cddf9SRui Paulo struct upnp_wps_device_interface, list) {
722f05cddf9SRui Paulo if (upnp_er_set_selected_registrar(iface->wps->registrar, s,
723f05cddf9SRui Paulo msg))
724f05cddf9SRui Paulo err = 1;
72539beb93cSSam Leffler }
72639beb93cSSam Leffler wpabuf_free(msg);
727f05cddf9SRui Paulo if (err)
728f05cddf9SRui Paulo return HTTP_INTERNAL_SERVER_ERROR;
72939beb93cSSam Leffler *replyname = NULL;
73039beb93cSSam Leffler *reply = NULL;
73139beb93cSSam Leffler return HTTP_OK;
73239beb93cSSam Leffler }
73339beb93cSSam Leffler
73439beb93cSSam Leffler
73539beb93cSSam Leffler static const char *soap_prefix =
73639beb93cSSam Leffler "<?xml version=\"1.0\"?>\n"
73739beb93cSSam Leffler "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
73839beb93cSSam Leffler "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
73939beb93cSSam Leffler "<s:Body>\n";
74039beb93cSSam Leffler static const char *soap_postfix =
74139beb93cSSam Leffler "</s:Body>\n</s:Envelope>\n";
74239beb93cSSam Leffler
74339beb93cSSam Leffler static const char *soap_error_prefix =
74439beb93cSSam Leffler "<s:Fault>\n"
74539beb93cSSam Leffler "<faultcode>s:Client</faultcode>\n"
74639beb93cSSam Leffler "<faultstring>UPnPError</faultstring>\n"
74739beb93cSSam Leffler "<detail>\n"
74839beb93cSSam Leffler "<UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\">\n";
74939beb93cSSam Leffler static const char *soap_error_postfix =
75039beb93cSSam Leffler "<errorDescription>Error</errorDescription>\n"
75139beb93cSSam Leffler "</UPnPError>\n"
75239beb93cSSam Leffler "</detail>\n"
75339beb93cSSam Leffler "</s:Fault>\n";
75439beb93cSSam Leffler
web_connection_send_reply(struct http_request * req,enum http_reply_code ret,const char * action,int action_len,const struct wpabuf * reply,const char * replyname)755e28a4053SRui Paulo static void web_connection_send_reply(struct http_request *req,
75639beb93cSSam Leffler enum http_reply_code ret,
75739beb93cSSam Leffler const char *action, int action_len,
75839beb93cSSam Leffler const struct wpabuf *reply,
75939beb93cSSam Leffler const char *replyname)
76039beb93cSSam Leffler {
76139beb93cSSam Leffler struct wpabuf *buf;
76239beb93cSSam Leffler char *replydata;
76339beb93cSSam Leffler char *put_length_here = NULL;
76439beb93cSSam Leffler char *body_start = NULL;
76539beb93cSSam Leffler
76639beb93cSSam Leffler if (reply) {
76739beb93cSSam Leffler size_t len;
768*c1d255d3SCy Schubert replydata = base64_encode(wpabuf_head(reply), wpabuf_len(reply),
769*c1d255d3SCy Schubert &len);
77039beb93cSSam Leffler } else
77139beb93cSSam Leffler replydata = NULL;
77239beb93cSSam Leffler
77339beb93cSSam Leffler /* Parameters of the response:
77439beb93cSSam Leffler * action(action_len) -- action we are responding to
77539beb93cSSam Leffler * replyname -- a name we need for the reply
77639beb93cSSam Leffler * replydata -- NULL or null-terminated string
77739beb93cSSam Leffler */
77839beb93cSSam Leffler buf = wpabuf_alloc(1000 + (replydata ? os_strlen(replydata) : 0U) +
77939beb93cSSam Leffler (action_len > 0 ? action_len * 2 : 0));
78039beb93cSSam Leffler if (buf == NULL) {
78139beb93cSSam Leffler wpa_printf(MSG_INFO, "WPS UPnP: Cannot allocate reply to "
78239beb93cSSam Leffler "POST");
78339beb93cSSam Leffler os_free(replydata);
784e28a4053SRui Paulo http_request_deinit(req);
78539beb93cSSam Leffler return;
78639beb93cSSam Leffler }
78739beb93cSSam Leffler
78839beb93cSSam Leffler /*
78939beb93cSSam Leffler * Assuming we will be successful, put in the output header first.
79039beb93cSSam Leffler * Note: we do not keep connections alive (and httpread does
79139beb93cSSam Leffler * not support it)... therefore we must have Connection: close.
79239beb93cSSam Leffler */
79339beb93cSSam Leffler if (ret == HTTP_OK) {
79439beb93cSSam Leffler wpabuf_put_str(buf,
79539beb93cSSam Leffler "HTTP/1.1 200 OK\r\n"
79639beb93cSSam Leffler "Content-Type: text/xml; "
79739beb93cSSam Leffler "charset=\"utf-8\"\r\n");
79839beb93cSSam Leffler } else {
79939beb93cSSam Leffler wpabuf_printf(buf, "HTTP/1.1 %d Error\r\n", ret);
80039beb93cSSam Leffler }
80139beb93cSSam Leffler wpabuf_put_str(buf, http_connection_close);
80239beb93cSSam Leffler
80339beb93cSSam Leffler wpabuf_put_str(buf, "Content-Length: ");
80439beb93cSSam Leffler /*
80539beb93cSSam Leffler * We will paste the length in later, leaving some extra whitespace.
80639beb93cSSam Leffler * HTTP code is supposed to be tolerant of extra whitespace.
80739beb93cSSam Leffler */
80839beb93cSSam Leffler put_length_here = wpabuf_put(buf, 0);
80939beb93cSSam Leffler wpabuf_put_str(buf, " \r\n");
81039beb93cSSam Leffler
81139beb93cSSam Leffler http_put_date(buf);
81239beb93cSSam Leffler
81339beb93cSSam Leffler /* terminating empty line */
81439beb93cSSam Leffler wpabuf_put_str(buf, "\r\n");
81539beb93cSSam Leffler
81639beb93cSSam Leffler body_start = wpabuf_put(buf, 0);
81739beb93cSSam Leffler
81839beb93cSSam Leffler if (ret == HTTP_OK) {
81939beb93cSSam Leffler wpabuf_put_str(buf, soap_prefix);
82039beb93cSSam Leffler wpabuf_put_str(buf, "<u:");
82139beb93cSSam Leffler wpabuf_put_data(buf, action, action_len);
82239beb93cSSam Leffler wpabuf_put_str(buf, "Response xmlns:u=\"");
82339beb93cSSam Leffler wpabuf_put_str(buf, urn_wfawlanconfig);
82439beb93cSSam Leffler wpabuf_put_str(buf, "\">\n");
82539beb93cSSam Leffler if (replydata && replyname) {
82639beb93cSSam Leffler /* TODO: might possibly need to escape part of reply
82739beb93cSSam Leffler * data? ...
82839beb93cSSam Leffler * probably not, unlikely to have ampersand(&) or left
82939beb93cSSam Leffler * angle bracket (<) in it...
83039beb93cSSam Leffler */
83139beb93cSSam Leffler wpabuf_printf(buf, "<%s>", replyname);
83239beb93cSSam Leffler wpabuf_put_str(buf, replydata);
83339beb93cSSam Leffler wpabuf_printf(buf, "</%s>\n", replyname);
83439beb93cSSam Leffler }
83539beb93cSSam Leffler wpabuf_put_str(buf, "</u:");
83639beb93cSSam Leffler wpabuf_put_data(buf, action, action_len);
83739beb93cSSam Leffler wpabuf_put_str(buf, "Response>\n");
83839beb93cSSam Leffler wpabuf_put_str(buf, soap_postfix);
83939beb93cSSam Leffler } else {
84039beb93cSSam Leffler /* Error case */
84139beb93cSSam Leffler wpabuf_put_str(buf, soap_prefix);
84239beb93cSSam Leffler wpabuf_put_str(buf, soap_error_prefix);
84339beb93cSSam Leffler wpabuf_printf(buf, "<errorCode>%d</errorCode>\n", ret);
84439beb93cSSam Leffler wpabuf_put_str(buf, soap_error_postfix);
84539beb93cSSam Leffler wpabuf_put_str(buf, soap_postfix);
84639beb93cSSam Leffler }
84739beb93cSSam Leffler os_free(replydata);
84839beb93cSSam Leffler
84939beb93cSSam Leffler /* Now patch in the content length at the end */
85039beb93cSSam Leffler if (body_start && put_length_here) {
85139beb93cSSam Leffler int body_length = (char *) wpabuf_put(buf, 0) - body_start;
85239beb93cSSam Leffler char len_buf[10];
85339beb93cSSam Leffler os_snprintf(len_buf, sizeof(len_buf), "%d", body_length);
85439beb93cSSam Leffler os_memcpy(put_length_here, len_buf, os_strlen(len_buf));
85539beb93cSSam Leffler }
85639beb93cSSam Leffler
857e28a4053SRui Paulo http_request_send_and_deinit(req, buf);
85839beb93cSSam Leffler }
85939beb93cSSam Leffler
86039beb93cSSam Leffler
web_get_action(struct http_request * req,size_t * action_len)861e28a4053SRui Paulo static const char * web_get_action(struct http_request *req,
862e28a4053SRui Paulo size_t *action_len)
86339beb93cSSam Leffler {
86439beb93cSSam Leffler const char *match;
86539beb93cSSam Leffler int match_len;
86639beb93cSSam Leffler char *b;
86739beb93cSSam Leffler char *action;
86839beb93cSSam Leffler
86939beb93cSSam Leffler *action_len = 0;
87039beb93cSSam Leffler /* The SOAPAction line of the header tells us what we want to do */
871e28a4053SRui Paulo b = http_request_get_hdr_line(req, "SOAPAction:");
87239beb93cSSam Leffler if (b == NULL)
87339beb93cSSam Leffler return NULL;
87439beb93cSSam Leffler if (*b == '"')
87539beb93cSSam Leffler b++;
87639beb93cSSam Leffler else
87739beb93cSSam Leffler return NULL;
87839beb93cSSam Leffler match = urn_wfawlanconfig;
87939beb93cSSam Leffler match_len = os_strlen(urn_wfawlanconfig) - 1;
88039beb93cSSam Leffler if (os_strncasecmp(b, match, match_len))
88139beb93cSSam Leffler return NULL;
88239beb93cSSam Leffler b += match_len;
88339beb93cSSam Leffler /* skip over version */
88439beb93cSSam Leffler while (isgraph(*b) && *b != '#')
88539beb93cSSam Leffler b++;
88639beb93cSSam Leffler if (*b != '#')
88739beb93cSSam Leffler return NULL;
88839beb93cSSam Leffler b++;
88939beb93cSSam Leffler /* Following the sharp(#) should be the action and a double quote */
89039beb93cSSam Leffler action = b;
89139beb93cSSam Leffler while (isgraph(*b) && *b != '"')
89239beb93cSSam Leffler b++;
89339beb93cSSam Leffler if (*b != '"')
89439beb93cSSam Leffler return NULL;
89539beb93cSSam Leffler *action_len = b - action;
89639beb93cSSam Leffler return action;
89739beb93cSSam Leffler }
89839beb93cSSam Leffler
89939beb93cSSam Leffler
90039beb93cSSam Leffler /* Given that we have received a header w/ POST, act upon it
90139beb93cSSam Leffler *
90239beb93cSSam Leffler * Format of POST (case-insensitive):
90339beb93cSSam Leffler *
90439beb93cSSam Leffler * First line must be:
90539beb93cSSam Leffler * POST /<file> HTTP/1.1
90639beb93cSSam Leffler * Since we don't do anything fancy we just ignore other lines.
90739beb93cSSam Leffler *
90839beb93cSSam Leffler * Our response (if no error) which includes only required lines is:
90939beb93cSSam Leffler * HTTP/1.1 200 OK
91039beb93cSSam Leffler * Connection: close
91139beb93cSSam Leffler * Content-Type: text/xml
91239beb93cSSam Leffler * Date: <rfc1123-date>
91339beb93cSSam Leffler *
91439beb93cSSam Leffler * Header lines must end with \r\n
91539beb93cSSam Leffler * Per RFC 2616, content-length: is not required but connection:close
91639beb93cSSam Leffler * would appear to be required (given that we will be closing it!).
91739beb93cSSam Leffler */
web_connection_parse_post(struct upnp_wps_device_sm * sm,struct sockaddr_in * cli,struct http_request * req,const char * filename)918e28a4053SRui Paulo static void web_connection_parse_post(struct upnp_wps_device_sm *sm,
919e28a4053SRui Paulo struct sockaddr_in *cli,
920e28a4053SRui Paulo struct http_request *req,
92139beb93cSSam Leffler const char *filename)
92239beb93cSSam Leffler {
92339beb93cSSam Leffler enum http_reply_code ret;
924e28a4053SRui Paulo char *data = http_request_get_data(req); /* body of http msg */
925e28a4053SRui Paulo const char *action = NULL;
926e28a4053SRui Paulo size_t action_len = 0;
92739beb93cSSam Leffler const char *replyname = NULL; /* argument name for the reply */
92839beb93cSSam Leffler struct wpabuf *reply = NULL; /* data for the reply */
92939beb93cSSam Leffler
930e28a4053SRui Paulo if (os_strcasecmp(filename, UPNP_WPS_DEVICE_CONTROL_FILE)) {
931e28a4053SRui Paulo wpa_printf(MSG_INFO, "WPS UPnP: Invalid POST filename %s",
932e28a4053SRui Paulo filename);
933e28a4053SRui Paulo ret = HTTP_NOT_FOUND;
934e28a4053SRui Paulo goto bad;
935e28a4053SRui Paulo }
936e28a4053SRui Paulo
93739beb93cSSam Leffler ret = UPNP_INVALID_ACTION;
938e28a4053SRui Paulo action = web_get_action(req, &action_len);
93939beb93cSSam Leffler if (action == NULL)
94039beb93cSSam Leffler goto bad;
94139beb93cSSam Leffler
94239beb93cSSam Leffler if (!os_strncasecmp("GetDeviceInfo", action, action_len))
94339beb93cSSam Leffler ret = web_process_get_device_info(sm, &reply, &replyname);
94439beb93cSSam Leffler else if (!os_strncasecmp("PutMessage", action, action_len))
94539beb93cSSam Leffler ret = web_process_put_message(sm, data, &reply, &replyname);
94639beb93cSSam Leffler else if (!os_strncasecmp("PutWLANResponse", action, action_len))
94739beb93cSSam Leffler ret = web_process_put_wlan_response(sm, data, &reply,
94839beb93cSSam Leffler &replyname);
94939beb93cSSam Leffler else if (!os_strncasecmp("SetSelectedRegistrar", action, action_len))
950e28a4053SRui Paulo ret = web_process_set_selected_registrar(sm, cli, data, &reply,
95139beb93cSSam Leffler &replyname);
95239beb93cSSam Leffler else
95339beb93cSSam Leffler wpa_printf(MSG_INFO, "WPS UPnP: Unknown POST type");
95439beb93cSSam Leffler
95539beb93cSSam Leffler bad:
95639beb93cSSam Leffler if (ret != HTTP_OK)
95739beb93cSSam Leffler wpa_printf(MSG_INFO, "WPS UPnP: POST failure ret=%d", ret);
958e28a4053SRui Paulo web_connection_send_reply(req, ret, action, action_len, reply,
95939beb93cSSam Leffler replyname);
96039beb93cSSam Leffler wpabuf_free(reply);
96139beb93cSSam Leffler }
96239beb93cSSam Leffler
96339beb93cSSam Leffler
96439beb93cSSam Leffler /* Given that we have received a header w/ SUBSCRIBE, act upon it
96539beb93cSSam Leffler *
96639beb93cSSam Leffler * Format of SUBSCRIBE (case-insensitive):
96739beb93cSSam Leffler *
96839beb93cSSam Leffler * First line must be:
96939beb93cSSam Leffler * SUBSCRIBE /wps_event HTTP/1.1
97039beb93cSSam Leffler *
97139beb93cSSam Leffler * Our response (if no error) which includes only required lines is:
97239beb93cSSam Leffler * HTTP/1.1 200 OK
97339beb93cSSam Leffler * Server: xx, UPnP/1.0, xx
97439beb93cSSam Leffler * SID: uuid:xxxxxxxxx
97539beb93cSSam Leffler * Timeout: Second-<n>
97639beb93cSSam Leffler * Content-Length: 0
97739beb93cSSam Leffler * Date: xxxx
97839beb93cSSam Leffler *
97939beb93cSSam Leffler * Header lines must end with \r\n
98039beb93cSSam Leffler * Per RFC 2616, content-length: is not required but connection:close
98139beb93cSSam Leffler * would appear to be required (given that we will be closing it!).
98239beb93cSSam Leffler */
web_connection_parse_subscribe(struct upnp_wps_device_sm * sm,struct http_request * req,const char * filename)983e28a4053SRui Paulo static void web_connection_parse_subscribe(struct upnp_wps_device_sm *sm,
984e28a4053SRui Paulo struct http_request *req,
98539beb93cSSam Leffler const char *filename)
98639beb93cSSam Leffler {
98739beb93cSSam Leffler struct wpabuf *buf;
98839beb93cSSam Leffler char *b;
989e28a4053SRui Paulo char *hdr = http_request_get_hdr(req);
99039beb93cSSam Leffler char *h;
99139beb93cSSam Leffler char *match;
99239beb93cSSam Leffler int match_len;
99339beb93cSSam Leffler char *end;
99439beb93cSSam Leffler int len;
99539beb93cSSam Leffler int got_nt = 0;
99639beb93cSSam Leffler u8 uuid[UUID_LEN];
99739beb93cSSam Leffler int got_uuid = 0;
99839beb93cSSam Leffler char *callback_urls = NULL;
99939beb93cSSam Leffler struct subscription *s = NULL;
100039beb93cSSam Leffler enum http_reply_code ret = HTTP_INTERNAL_SERVER_ERROR;
100139beb93cSSam Leffler
100239beb93cSSam Leffler buf = wpabuf_alloc(1000);
1003e28a4053SRui Paulo if (buf == NULL) {
1004e28a4053SRui Paulo http_request_deinit(req);
100539beb93cSSam Leffler return;
1006e28a4053SRui Paulo }
100739beb93cSSam Leffler
1008f05cddf9SRui Paulo wpa_hexdump_ascii(MSG_DEBUG, "WPS UPnP: HTTP SUBSCRIBE",
1009f05cddf9SRui Paulo (u8 *) hdr, os_strlen(hdr));
1010f05cddf9SRui Paulo
101139beb93cSSam Leffler /* Parse/validate headers */
101239beb93cSSam Leffler h = hdr;
101339beb93cSSam Leffler /* First line: SUBSCRIBE /wps_event HTTP/1.1
101439beb93cSSam Leffler * has already been parsed.
101539beb93cSSam Leffler */
101639beb93cSSam Leffler if (os_strcasecmp(filename, UPNP_WPS_DEVICE_EVENT_FILE) != 0) {
101739beb93cSSam Leffler ret = HTTP_PRECONDITION_FAILED;
101839beb93cSSam Leffler goto error;
101939beb93cSSam Leffler }
102039beb93cSSam Leffler wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP SUBSCRIBE for event");
102139beb93cSSam Leffler end = os_strchr(h, '\n');
102239beb93cSSam Leffler
10235b9c547cSRui Paulo while (end) {
102439beb93cSSam Leffler /* Option line by option line */
102539beb93cSSam Leffler h = end + 1;
102639beb93cSSam Leffler end = os_strchr(h, '\n');
102739beb93cSSam Leffler if (end == NULL)
102839beb93cSSam Leffler break; /* no unterminated lines allowed */
102939beb93cSSam Leffler
103039beb93cSSam Leffler /* NT assures that it is our type of subscription;
1031f05cddf9SRui Paulo * not used for a renewal.
103239beb93cSSam Leffler **/
103339beb93cSSam Leffler match = "NT:";
103439beb93cSSam Leffler match_len = os_strlen(match);
103539beb93cSSam Leffler if (os_strncasecmp(h, match, match_len) == 0) {
103639beb93cSSam Leffler h += match_len;
103739beb93cSSam Leffler while (*h == ' ' || *h == '\t')
103839beb93cSSam Leffler h++;
103939beb93cSSam Leffler match = "upnp:event";
104039beb93cSSam Leffler match_len = os_strlen(match);
104139beb93cSSam Leffler if (os_strncasecmp(h, match, match_len) != 0) {
104239beb93cSSam Leffler ret = HTTP_BAD_REQUEST;
104339beb93cSSam Leffler goto error;
104439beb93cSSam Leffler }
104539beb93cSSam Leffler got_nt = 1;
104639beb93cSSam Leffler continue;
104739beb93cSSam Leffler }
104839beb93cSSam Leffler /* HOST should refer to us */
104939beb93cSSam Leffler #if 0
105039beb93cSSam Leffler match = "HOST:";
105139beb93cSSam Leffler match_len = os_strlen(match);
105239beb93cSSam Leffler if (os_strncasecmp(h, match, match_len) == 0) {
105339beb93cSSam Leffler h += match_len;
105439beb93cSSam Leffler while (*h == ' ' || *h == '\t')
105539beb93cSSam Leffler h++;
105639beb93cSSam Leffler .....
105739beb93cSSam Leffler }
105839beb93cSSam Leffler #endif
105939beb93cSSam Leffler /* CALLBACK gives one or more URLs for NOTIFYs
106039beb93cSSam Leffler * to be sent as a result of the subscription.
106139beb93cSSam Leffler * Each URL is enclosed in angle brackets.
106239beb93cSSam Leffler */
106339beb93cSSam Leffler match = "CALLBACK:";
106439beb93cSSam Leffler match_len = os_strlen(match);
106539beb93cSSam Leffler if (os_strncasecmp(h, match, match_len) == 0) {
106639beb93cSSam Leffler h += match_len;
106739beb93cSSam Leffler while (*h == ' ' || *h == '\t')
106839beb93cSSam Leffler h++;
106939beb93cSSam Leffler len = end - h;
107039beb93cSSam Leffler os_free(callback_urls);
10715b9c547cSRui Paulo callback_urls = dup_binstr(h, len);
107239beb93cSSam Leffler if (callback_urls == NULL) {
107339beb93cSSam Leffler ret = HTTP_INTERNAL_SERVER_ERROR;
107439beb93cSSam Leffler goto error;
107539beb93cSSam Leffler }
1076325151a3SRui Paulo if (len > 0 && callback_urls[len - 1] == '\r')
1077325151a3SRui Paulo callback_urls[len - 1] = '\0';
107839beb93cSSam Leffler continue;
107939beb93cSSam Leffler }
108039beb93cSSam Leffler /* SID is only for renewal */
108139beb93cSSam Leffler match = "SID:";
108239beb93cSSam Leffler match_len = os_strlen(match);
108339beb93cSSam Leffler if (os_strncasecmp(h, match, match_len) == 0) {
108439beb93cSSam Leffler h += match_len;
108539beb93cSSam Leffler while (*h == ' ' || *h == '\t')
108639beb93cSSam Leffler h++;
108739beb93cSSam Leffler match = "uuid:";
108839beb93cSSam Leffler match_len = os_strlen(match);
108939beb93cSSam Leffler if (os_strncasecmp(h, match, match_len) != 0) {
109039beb93cSSam Leffler ret = HTTP_BAD_REQUEST;
109139beb93cSSam Leffler goto error;
109239beb93cSSam Leffler }
109339beb93cSSam Leffler h += match_len;
109439beb93cSSam Leffler while (*h == ' ' || *h == '\t')
109539beb93cSSam Leffler h++;
109639beb93cSSam Leffler if (uuid_str2bin(h, uuid)) {
109739beb93cSSam Leffler ret = HTTP_BAD_REQUEST;
109839beb93cSSam Leffler goto error;
109939beb93cSSam Leffler }
110039beb93cSSam Leffler got_uuid = 1;
110139beb93cSSam Leffler continue;
110239beb93cSSam Leffler }
110339beb93cSSam Leffler /* TIMEOUT is requested timeout, but apparently we can
110439beb93cSSam Leffler * just ignore this.
110539beb93cSSam Leffler */
110639beb93cSSam Leffler }
110739beb93cSSam Leffler
110839beb93cSSam Leffler if (got_uuid) {
110939beb93cSSam Leffler /* renewal */
1110f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "WPS UPnP: Subscription renewal");
111139beb93cSSam Leffler if (callback_urls) {
111239beb93cSSam Leffler ret = HTTP_BAD_REQUEST;
111339beb93cSSam Leffler goto error;
111439beb93cSSam Leffler }
111539beb93cSSam Leffler s = subscription_renew(sm, uuid);
111639beb93cSSam Leffler if (s == NULL) {
1117f05cddf9SRui Paulo char str[80];
1118f05cddf9SRui Paulo uuid_bin2str(uuid, str, sizeof(str));
1119f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "WPS UPnP: Could not find "
1120f05cddf9SRui Paulo "SID %s", str);
112139beb93cSSam Leffler ret = HTTP_PRECONDITION_FAILED;
112239beb93cSSam Leffler goto error;
112339beb93cSSam Leffler }
112439beb93cSSam Leffler } else if (callback_urls) {
1125f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "WPS UPnP: New subscription");
112639beb93cSSam Leffler if (!got_nt) {
112739beb93cSSam Leffler ret = HTTP_PRECONDITION_FAILED;
112839beb93cSSam Leffler goto error;
112939beb93cSSam Leffler }
113039beb93cSSam Leffler s = subscription_start(sm, callback_urls);
113139beb93cSSam Leffler if (s == NULL) {
113239beb93cSSam Leffler ret = HTTP_INTERNAL_SERVER_ERROR;
113339beb93cSSam Leffler goto error;
113439beb93cSSam Leffler }
113539beb93cSSam Leffler } else {
113639beb93cSSam Leffler ret = HTTP_PRECONDITION_FAILED;
113739beb93cSSam Leffler goto error;
113839beb93cSSam Leffler }
113939beb93cSSam Leffler
114039beb93cSSam Leffler /* success */
114139beb93cSSam Leffler http_put_reply_code(buf, HTTP_OK);
114239beb93cSSam Leffler wpabuf_put_str(buf, http_server_hdr);
114339beb93cSSam Leffler wpabuf_put_str(buf, http_connection_close);
114439beb93cSSam Leffler wpabuf_put_str(buf, "Content-Length: 0\r\n");
114539beb93cSSam Leffler wpabuf_put_str(buf, "SID: uuid:");
114639beb93cSSam Leffler /* subscription id */
114739beb93cSSam Leffler b = wpabuf_put(buf, 0);
114839beb93cSSam Leffler uuid_bin2str(s->uuid, b, 80);
1149f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "WPS UPnP: Assigned SID %s", b);
115039beb93cSSam Leffler wpabuf_put(buf, os_strlen(b));
115139beb93cSSam Leffler wpabuf_put_str(buf, "\r\n");
115239beb93cSSam Leffler wpabuf_printf(buf, "Timeout: Second-%d\r\n", UPNP_SUBSCRIBE_SEC);
115339beb93cSSam Leffler http_put_date(buf);
115439beb93cSSam Leffler /* And empty line to terminate header: */
115539beb93cSSam Leffler wpabuf_put_str(buf, "\r\n");
115639beb93cSSam Leffler
115739beb93cSSam Leffler os_free(callback_urls);
1158e28a4053SRui Paulo http_request_send_and_deinit(req, buf);
115939beb93cSSam Leffler return;
116039beb93cSSam Leffler
116139beb93cSSam Leffler error:
116239beb93cSSam Leffler /* Per UPnP spec:
116339beb93cSSam Leffler * Errors
116439beb93cSSam Leffler * Incompatible headers
116539beb93cSSam Leffler * 400 Bad Request. If SID header and one of NT or CALLBACK headers
116639beb93cSSam Leffler * are present, the publisher must respond with HTTP error
116739beb93cSSam Leffler * 400 Bad Request.
116839beb93cSSam Leffler * Missing or invalid CALLBACK
116939beb93cSSam Leffler * 412 Precondition Failed. If CALLBACK header is missing or does not
117039beb93cSSam Leffler * contain a valid HTTP URL, the publisher must respond with HTTP
117139beb93cSSam Leffler * error 412 Precondition Failed.
117239beb93cSSam Leffler * Invalid NT
117339beb93cSSam Leffler * 412 Precondition Failed. If NT header does not equal upnp:event,
117439beb93cSSam Leffler * the publisher must respond with HTTP error 412 Precondition
117539beb93cSSam Leffler * Failed.
117639beb93cSSam Leffler * [For resubscription, use 412 if unknown uuid].
117739beb93cSSam Leffler * Unable to accept subscription
117839beb93cSSam Leffler * 5xx. If a publisher is not able to accept a subscription (such as
117939beb93cSSam Leffler * due to insufficient resources), it must respond with a
118039beb93cSSam Leffler * HTTP 500-series error code.
118139beb93cSSam Leffler * 599 Too many subscriptions (not a standard HTTP error)
118239beb93cSSam Leffler */
1183f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "WPS UPnP: SUBSCRIBE failed - return %d", ret);
118439beb93cSSam Leffler http_put_empty(buf, ret);
1185e28a4053SRui Paulo http_request_send_and_deinit(req, buf);
11863157ba21SRui Paulo os_free(callback_urls);
118739beb93cSSam Leffler }
118839beb93cSSam Leffler
118939beb93cSSam Leffler
119039beb93cSSam Leffler /* Given that we have received a header w/ UNSUBSCRIBE, act upon it
119139beb93cSSam Leffler *
119239beb93cSSam Leffler * Format of UNSUBSCRIBE (case-insensitive):
119339beb93cSSam Leffler *
119439beb93cSSam Leffler * First line must be:
119539beb93cSSam Leffler * UNSUBSCRIBE /wps_event HTTP/1.1
119639beb93cSSam Leffler *
119739beb93cSSam Leffler * Our response (if no error) which includes only required lines is:
119839beb93cSSam Leffler * HTTP/1.1 200 OK
119939beb93cSSam Leffler * Content-Length: 0
120039beb93cSSam Leffler *
120139beb93cSSam Leffler * Header lines must end with \r\n
120239beb93cSSam Leffler * Per RFC 2616, content-length: is not required but connection:close
120339beb93cSSam Leffler * would appear to be required (given that we will be closing it!).
120439beb93cSSam Leffler */
web_connection_parse_unsubscribe(struct upnp_wps_device_sm * sm,struct http_request * req,const char * filename)1205e28a4053SRui Paulo static void web_connection_parse_unsubscribe(struct upnp_wps_device_sm *sm,
1206e28a4053SRui Paulo struct http_request *req,
120739beb93cSSam Leffler const char *filename)
120839beb93cSSam Leffler {
120939beb93cSSam Leffler struct wpabuf *buf;
1210e28a4053SRui Paulo char *hdr = http_request_get_hdr(req);
121139beb93cSSam Leffler char *h;
121239beb93cSSam Leffler char *match;
121339beb93cSSam Leffler int match_len;
121439beb93cSSam Leffler char *end;
121539beb93cSSam Leffler u8 uuid[UUID_LEN];
121639beb93cSSam Leffler int got_uuid = 0;
121739beb93cSSam Leffler struct subscription *s = NULL;
121839beb93cSSam Leffler enum http_reply_code ret = HTTP_INTERNAL_SERVER_ERROR;
121939beb93cSSam Leffler
122039beb93cSSam Leffler /* Parse/validate headers */
122139beb93cSSam Leffler h = hdr;
122239beb93cSSam Leffler /* First line: UNSUBSCRIBE /wps_event HTTP/1.1
122339beb93cSSam Leffler * has already been parsed.
122439beb93cSSam Leffler */
122539beb93cSSam Leffler if (os_strcasecmp(filename, UPNP_WPS_DEVICE_EVENT_FILE) != 0) {
122639beb93cSSam Leffler ret = HTTP_PRECONDITION_FAILED;
122739beb93cSSam Leffler goto send_msg;
122839beb93cSSam Leffler }
122939beb93cSSam Leffler wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP UNSUBSCRIBE for event");
123039beb93cSSam Leffler end = os_strchr(h, '\n');
123139beb93cSSam Leffler
12325b9c547cSRui Paulo while (end) {
123339beb93cSSam Leffler /* Option line by option line */
123439beb93cSSam Leffler h = end + 1;
123539beb93cSSam Leffler end = os_strchr(h, '\n');
123639beb93cSSam Leffler if (end == NULL)
123739beb93cSSam Leffler break; /* no unterminated lines allowed */
123839beb93cSSam Leffler
123939beb93cSSam Leffler /* HOST should refer to us */
124039beb93cSSam Leffler #if 0
124139beb93cSSam Leffler match = "HOST:";
124239beb93cSSam Leffler match_len = os_strlen(match);
124339beb93cSSam Leffler if (os_strncasecmp(h, match, match_len) == 0) {
124439beb93cSSam Leffler h += match_len;
124539beb93cSSam Leffler while (*h == ' ' || *h == '\t')
124639beb93cSSam Leffler h++;
124739beb93cSSam Leffler .....
124839beb93cSSam Leffler }
124939beb93cSSam Leffler #endif
125039beb93cSSam Leffler match = "SID:";
125139beb93cSSam Leffler match_len = os_strlen(match);
125239beb93cSSam Leffler if (os_strncasecmp(h, match, match_len) == 0) {
125339beb93cSSam Leffler h += match_len;
125439beb93cSSam Leffler while (*h == ' ' || *h == '\t')
125539beb93cSSam Leffler h++;
125639beb93cSSam Leffler match = "uuid:";
125739beb93cSSam Leffler match_len = os_strlen(match);
125839beb93cSSam Leffler if (os_strncasecmp(h, match, match_len) != 0) {
125939beb93cSSam Leffler ret = HTTP_BAD_REQUEST;
126039beb93cSSam Leffler goto send_msg;
126139beb93cSSam Leffler }
126239beb93cSSam Leffler h += match_len;
126339beb93cSSam Leffler while (*h == ' ' || *h == '\t')
126439beb93cSSam Leffler h++;
126539beb93cSSam Leffler if (uuid_str2bin(h, uuid)) {
126639beb93cSSam Leffler ret = HTTP_BAD_REQUEST;
126739beb93cSSam Leffler goto send_msg;
126839beb93cSSam Leffler }
126939beb93cSSam Leffler got_uuid = 1;
127039beb93cSSam Leffler continue;
127139beb93cSSam Leffler }
12725b9c547cSRui Paulo
12735b9c547cSRui Paulo match = "NT:";
12745b9c547cSRui Paulo match_len = os_strlen(match);
12755b9c547cSRui Paulo if (os_strncasecmp(h, match, match_len) == 0) {
12765b9c547cSRui Paulo ret = HTTP_BAD_REQUEST;
12775b9c547cSRui Paulo goto send_msg;
12785b9c547cSRui Paulo }
12795b9c547cSRui Paulo
12805b9c547cSRui Paulo match = "CALLBACK:";
12815b9c547cSRui Paulo match_len = os_strlen(match);
12825b9c547cSRui Paulo if (os_strncasecmp(h, match, match_len) == 0) {
12835b9c547cSRui Paulo ret = HTTP_BAD_REQUEST;
12845b9c547cSRui Paulo goto send_msg;
12855b9c547cSRui Paulo }
128639beb93cSSam Leffler }
128739beb93cSSam Leffler
128839beb93cSSam Leffler if (got_uuid) {
1289325151a3SRui Paulo char str[80];
1290325151a3SRui Paulo
1291325151a3SRui Paulo uuid_bin2str(uuid, str, sizeof(str));
1292325151a3SRui Paulo
129339beb93cSSam Leffler s = subscription_find(sm, uuid);
129439beb93cSSam Leffler if (s) {
1295e28a4053SRui Paulo struct subscr_addr *sa;
1296e28a4053SRui Paulo sa = dl_list_first(&s->addr_list, struct subscr_addr,
1297e28a4053SRui Paulo list);
1298325151a3SRui Paulo wpa_printf(MSG_DEBUG,
1299325151a3SRui Paulo "WPS UPnP: Unsubscribing %p (SID %s) %s",
1300325151a3SRui Paulo s, str, (sa && sa->domain_and_port) ?
1301e28a4053SRui Paulo sa->domain_and_port : "-null-");
1302e28a4053SRui Paulo dl_list_del(&s->list);
130339beb93cSSam Leffler subscription_destroy(s);
13045b9c547cSRui Paulo } else {
1305325151a3SRui Paulo wpa_printf(MSG_INFO,
1306325151a3SRui Paulo "WPS UPnP: Could not find matching subscription to unsubscribe (SID %s)",
1307325151a3SRui Paulo str);
13085b9c547cSRui Paulo ret = HTTP_PRECONDITION_FAILED;
13095b9c547cSRui Paulo goto send_msg;
131039beb93cSSam Leffler }
131139beb93cSSam Leffler } else {
131239beb93cSSam Leffler wpa_printf(MSG_INFO, "WPS UPnP: Unsubscribe fails (not "
131339beb93cSSam Leffler "found)");
131439beb93cSSam Leffler ret = HTTP_PRECONDITION_FAILED;
131539beb93cSSam Leffler goto send_msg;
131639beb93cSSam Leffler }
131739beb93cSSam Leffler
131839beb93cSSam Leffler ret = HTTP_OK;
131939beb93cSSam Leffler
132039beb93cSSam Leffler send_msg:
132139beb93cSSam Leffler buf = wpabuf_alloc(200);
1322e28a4053SRui Paulo if (buf == NULL) {
1323e28a4053SRui Paulo http_request_deinit(req);
132439beb93cSSam Leffler return;
1325e28a4053SRui Paulo }
132639beb93cSSam Leffler http_put_empty(buf, ret);
1327e28a4053SRui Paulo http_request_send_and_deinit(req, buf);
132839beb93cSSam Leffler }
132939beb93cSSam Leffler
133039beb93cSSam Leffler
133139beb93cSSam Leffler /* Send error in response to unknown requests */
web_connection_unimplemented(struct http_request * req)1332e28a4053SRui Paulo static void web_connection_unimplemented(struct http_request *req)
133339beb93cSSam Leffler {
133439beb93cSSam Leffler struct wpabuf *buf;
133539beb93cSSam Leffler buf = wpabuf_alloc(200);
1336e28a4053SRui Paulo if (buf == NULL) {
1337e28a4053SRui Paulo http_request_deinit(req);
133839beb93cSSam Leffler return;
1339e28a4053SRui Paulo }
134039beb93cSSam Leffler http_put_empty(buf, HTTP_UNIMPLEMENTED);
1341e28a4053SRui Paulo http_request_send_and_deinit(req, buf);
134239beb93cSSam Leffler }
134339beb93cSSam Leffler
134439beb93cSSam Leffler
134539beb93cSSam Leffler
134639beb93cSSam Leffler /* Called when we have gotten an apparently valid http request.
134739beb93cSSam Leffler */
web_connection_check_data(void * ctx,struct http_request * req)1348e28a4053SRui Paulo static void web_connection_check_data(void *ctx, struct http_request *req)
134939beb93cSSam Leffler {
1350e28a4053SRui Paulo struct upnp_wps_device_sm *sm = ctx;
1351e28a4053SRui Paulo enum httpread_hdr_type htype = http_request_get_type(req);
1352e28a4053SRui Paulo char *filename = http_request_get_uri(req);
1353e28a4053SRui Paulo struct sockaddr_in *cli = http_request_get_cli_addr(req);
135439beb93cSSam Leffler
135539beb93cSSam Leffler if (!filename) {
135639beb93cSSam Leffler wpa_printf(MSG_INFO, "WPS UPnP: Could not get HTTP URI");
1357e28a4053SRui Paulo http_request_deinit(req);
135839beb93cSSam Leffler return;
135939beb93cSSam Leffler }
136039beb93cSSam Leffler /* Trim leading slashes from filename */
136139beb93cSSam Leffler while (*filename == '/')
136239beb93cSSam Leffler filename++;
136339beb93cSSam Leffler
136439beb93cSSam Leffler wpa_printf(MSG_DEBUG, "WPS UPnP: Got HTTP request type %d from %s:%d",
1365e28a4053SRui Paulo htype, inet_ntoa(cli->sin_addr), htons(cli->sin_port));
136639beb93cSSam Leffler
136739beb93cSSam Leffler switch (htype) {
136839beb93cSSam Leffler case HTTPREAD_HDR_TYPE_GET:
1369e28a4053SRui Paulo web_connection_parse_get(sm, req, filename);
137039beb93cSSam Leffler break;
137139beb93cSSam Leffler case HTTPREAD_HDR_TYPE_POST:
1372e28a4053SRui Paulo web_connection_parse_post(sm, cli, req, filename);
137339beb93cSSam Leffler break;
137439beb93cSSam Leffler case HTTPREAD_HDR_TYPE_SUBSCRIBE:
1375e28a4053SRui Paulo web_connection_parse_subscribe(sm, req, filename);
137639beb93cSSam Leffler break;
137739beb93cSSam Leffler case HTTPREAD_HDR_TYPE_UNSUBSCRIBE:
1378e28a4053SRui Paulo web_connection_parse_unsubscribe(sm, req, filename);
137939beb93cSSam Leffler break;
1380e28a4053SRui Paulo
138139beb93cSSam Leffler /* We are not required to support M-POST; just plain
138239beb93cSSam Leffler * POST is supposed to work, so we only support that.
138339beb93cSSam Leffler * If for some reason we need to support M-POST, it is
138439beb93cSSam Leffler * mostly the same as POST, with small differences.
138539beb93cSSam Leffler */
138639beb93cSSam Leffler default:
138739beb93cSSam Leffler /* Send 501 for anything else */
1388e28a4053SRui Paulo web_connection_unimplemented(req);
138939beb93cSSam Leffler break;
139039beb93cSSam Leffler }
139139beb93cSSam Leffler }
139239beb93cSSam Leffler
139339beb93cSSam Leffler
139439beb93cSSam Leffler /*
139539beb93cSSam Leffler * Listening for web connections
139639beb93cSSam Leffler * We have a single TCP listening port, and hand off connections as we get
139739beb93cSSam Leffler * them.
139839beb93cSSam Leffler */
139939beb93cSSam Leffler
web_listener_stop(struct upnp_wps_device_sm * sm)140039beb93cSSam Leffler void web_listener_stop(struct upnp_wps_device_sm *sm)
140139beb93cSSam Leffler {
1402e28a4053SRui Paulo http_server_deinit(sm->web_srv);
1403e28a4053SRui Paulo sm->web_srv = NULL;
140439beb93cSSam Leffler }
140539beb93cSSam Leffler
140639beb93cSSam Leffler
web_listener_start(struct upnp_wps_device_sm * sm)140739beb93cSSam Leffler int web_listener_start(struct upnp_wps_device_sm *sm)
140839beb93cSSam Leffler {
1409e28a4053SRui Paulo struct in_addr addr;
1410e28a4053SRui Paulo addr.s_addr = sm->ip_addr;
1411e28a4053SRui Paulo sm->web_srv = http_server_init(&addr, -1, web_connection_check_data,
1412e28a4053SRui Paulo sm);
1413e28a4053SRui Paulo if (sm->web_srv == NULL) {
141439beb93cSSam Leffler web_listener_stop(sm);
141539beb93cSSam Leffler return -1;
141639beb93cSSam Leffler }
1417e28a4053SRui Paulo sm->web_port = http_server_get_port(sm->web_srv);
1418e28a4053SRui Paulo
1419e28a4053SRui Paulo return 0;
1420e28a4053SRui Paulo }
1421