2 * UPnP WPS Device - Web connections
3 * Copyright (c) 2000-2003 Intel Corporation
4 * Copyright (c) 2006-2007 Sony Corporation
5 * Copyright (c) 2008-2009 Atheros Communications
6 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
8 * See wps_upnp.c for more details on licensing and code history.
17 #include "http_server.h"
20 #include "wps_upnp_i.h"
23 /***************************************************************************
24 * Web connections (we serve pages of info about ourselves, handle
25 * requests, etc. etc.).
26 **************************************************************************/
28 #define WEB_CONNECTION_TIMEOUT_SEC 30 /* Drop web connection after t.o. */
29 #define WEB_CONNECTION_MAX_READ 8000 /* Max we'll read for TCP request */
30 #define MAX_WEB_CONNECTIONS 10 /* max simultaneous web connects */
33 static const char *urn_wfawlanconfig =
34 "urn:schemas-wifialliance-org:service:WFAWLANConfig:1";
35 static const char *http_server_hdr =
36 "Server: unspecified, UPnP/1.0, unspecified\r\n";
37 static const char *http_connection_close =
38 "Connection: close\r\n";
41 * "Files" that we serve via HTTP. The format of these files is given by
42 * WFA WPS specifications. Extra white space has been removed to save space.
45 static const char wps_scpd_xml[] =
46 "<?xml version=\"1.0\"?>\n"
47 "<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\">\n"
48 "<specVersion><major>1</major><minor>0</minor></specVersion>\n"
51 "<name>GetDeviceInfo</name>\n"
54 "<name>NewDeviceInfo</name>\n"
55 "<direction>out</direction>\n"
56 "<relatedStateVariable>DeviceInfo</relatedStateVariable>\n"
61 "<name>PutMessage</name>\n"
64 "<name>NewInMessage</name>\n"
65 "<direction>in</direction>\n"
66 "<relatedStateVariable>InMessage</relatedStateVariable>\n"
69 "<name>NewOutMessage</name>\n"
70 "<direction>out</direction>\n"
71 "<relatedStateVariable>OutMessage</relatedStateVariable>\n"
76 "<name>PutWLANResponse</name>\n"
79 "<name>NewMessage</name>\n"
80 "<direction>in</direction>\n"
81 "<relatedStateVariable>Message</relatedStateVariable>\n"
84 "<name>NewWLANEventType</name>\n"
85 "<direction>in</direction>\n"
86 "<relatedStateVariable>WLANEventType</relatedStateVariable>\n"
89 "<name>NewWLANEventMAC</name>\n"
90 "<direction>in</direction>\n"
91 "<relatedStateVariable>WLANEventMAC</relatedStateVariable>\n"
96 "<name>SetSelectedRegistrar</name>\n"
99 "<name>NewMessage</name>\n"
100 "<direction>in</direction>\n"
101 "<relatedStateVariable>Message</relatedStateVariable>\n"
106 "<serviceStateTable>\n"
107 "<stateVariable sendEvents=\"no\">\n"
108 "<name>Message</name>\n"
109 "<dataType>bin.base64</dataType>\n"
111 "<stateVariable sendEvents=\"no\">\n"
112 "<name>InMessage</name>\n"
113 "<dataType>bin.base64</dataType>\n"
115 "<stateVariable sendEvents=\"no\">\n"
116 "<name>OutMessage</name>\n"
117 "<dataType>bin.base64</dataType>\n"
119 "<stateVariable sendEvents=\"no\">\n"
120 "<name>DeviceInfo</name>\n"
121 "<dataType>bin.base64</dataType>\n"
123 "<stateVariable sendEvents=\"yes\">\n"
124 "<name>APStatus</name>\n"
125 "<dataType>ui1</dataType>\n"
127 "<stateVariable sendEvents=\"yes\">\n"
128 "<name>STAStatus</name>\n"
129 "<dataType>ui1</dataType>\n"
131 "<stateVariable sendEvents=\"yes\">\n"
132 "<name>WLANEvent</name>\n"
133 "<dataType>bin.base64</dataType>\n"
135 "<stateVariable sendEvents=\"no\">\n"
136 "<name>WLANEventType</name>\n"
137 "<dataType>ui1</dataType>\n"
139 "<stateVariable sendEvents=\"no\">\n"
140 "<name>WLANEventMAC</name>\n"
141 "<dataType>string</dataType>\n"
143 "<stateVariable sendEvents=\"no\">\n"
144 "<name>WLANResponse</name>\n"
145 "<dataType>bin.base64</dataType>\n"
147 "</serviceStateTable>\n"
152 static const char *wps_device_xml_prefix =
153 "<?xml version=\"1.0\"?>\n"
154 "<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\n"
160 "<deviceType>urn:schemas-wifialliance-org:device:WFADevice:1"
163 static const char *wps_device_xml_postfix =
166 "<serviceType>urn:schemas-wifialliance-org:service:WFAWLANConfig:1"
168 "<serviceId>urn:wifialliance-org:serviceId:WFAWLANConfig1</serviceId>"
170 "<SCPDURL>" UPNP_WPS_SCPD_XML_FILE "</SCPDURL>\n"
171 "<controlURL>" UPNP_WPS_DEVICE_CONTROL_FILE "</controlURL>\n"
172 "<eventSubURL>" UPNP_WPS_DEVICE_EVENT_FILE "</eventSubURL>\n"
179 /* format_wps_device_xml -- produce content of "file" wps_device.xml
180 * (UPNP_WPS_DEVICE_XML_FILE)
182 static void format_wps_device_xml(struct upnp_wps_device_interface *iface,
183 struct upnp_wps_device_sm *sm,
187 char uuid_string[80];
189 wpabuf_put_str(buf, wps_device_xml_prefix);
192 * Add required fields with default values if not configured. Add
193 * optional and recommended fields only if configured.
195 s = iface->wps->friendly_name;
196 s = ((s && *s) ? s : "WPS Access Point");
197 xml_add_tagged_data(buf, "friendlyName", s);
199 s = iface->wps->dev.manufacturer;
200 s = ((s && *s) ? s : "");
201 xml_add_tagged_data(buf, "manufacturer", s);
203 if (iface->wps->manufacturer_url)
204 xml_add_tagged_data(buf, "manufacturerURL",
205 iface->wps->manufacturer_url);
207 if (iface->wps->model_description)
208 xml_add_tagged_data(buf, "modelDescription",
209 iface->wps->model_description);
211 s = iface->wps->dev.model_name;
212 s = ((s && *s) ? s : "");
213 xml_add_tagged_data(buf, "modelName", s);
215 if (iface->wps->dev.model_number)
216 xml_add_tagged_data(buf, "modelNumber",
217 iface->wps->dev.model_number);
219 if (iface->wps->model_url)
220 xml_add_tagged_data(buf, "modelURL", iface->wps->model_url);
222 if (iface->wps->dev.serial_number)
223 xml_add_tagged_data(buf, "serialNumber",
224 iface->wps->dev.serial_number);
226 uuid_bin2str(iface->wps->uuid, uuid_string, sizeof(uuid_string));
228 /* Need "uuid:" prefix, thus we can't use xml_add_tagged_data()
231 wpabuf_put_str(buf, "<UDN>uuid:");
232 xml_data_encode(buf, s, os_strlen(s));
233 wpabuf_put_str(buf, "</UDN>\n");
236 xml_add_tagged_data(buf, "UPC", iface->wps->upc);
238 wpabuf_put_str(buf, wps_device_xml_postfix);
242 static void http_put_reply_code(struct wpabuf *buf, enum http_reply_code code)
244 wpabuf_put_str(buf, "HTTP/1.1 ");
247 wpabuf_put_str(buf, "200 OK\r\n");
249 case HTTP_BAD_REQUEST:
250 wpabuf_put_str(buf, "400 Bad request\r\n");
252 case HTTP_PRECONDITION_FAILED:
253 wpabuf_put_str(buf, "412 Precondition failed\r\n");
255 case HTTP_UNIMPLEMENTED:
256 wpabuf_put_str(buf, "501 Unimplemented\r\n");
258 case HTTP_INTERNAL_SERVER_ERROR:
260 wpabuf_put_str(buf, "500 Internal server error\r\n");
266 static void http_put_date(struct wpabuf *buf)
268 wpabuf_put_str(buf, "Date: ");
270 wpabuf_put_str(buf, "\r\n");
274 static void http_put_empty(struct wpabuf *buf, enum http_reply_code code)
276 http_put_reply_code(buf, code);
277 wpabuf_put_str(buf, http_server_hdr);
278 wpabuf_put_str(buf, http_connection_close);
279 wpabuf_put_str(buf, "Content-Length: 0\r\n"
284 /* Given that we have received a header w/ GET, act upon it
286 * Format of GET (case-insensitive):
288 * First line must be:
289 * GET /<file> HTTP/1.1
290 * Since we don't do anything fancy we just ignore other lines.
292 * Our response (if no error) which includes only required lines is:
295 * Content-Type: text/xml
296 * Date: <rfc1123-date>
298 * Header lines must end with \r\n
299 * Per RFC 2616, content-length: is not required but connection:close
300 * would appear to be required (given that we will be closing it!).
302 static void web_connection_parse_get(struct upnp_wps_device_sm *sm,
303 struct http_request *hreq, char *filename)
305 struct wpabuf *buf; /* output buffer, allocated */
306 char *put_length_here;
312 size_t extra_len = 0;
315 struct upnp_wps_device_interface *iface;
317 iface = dl_list_first(&sm->interfaces,
318 struct upnp_wps_device_interface, list);
320 http_request_deinit(hreq);
325 * It is not required that filenames be case insensitive but it is
326 * allowed and cannot hurt here.
328 if (os_strcasecmp(filename, UPNP_WPS_DEVICE_XML_FILE) == 0) {
329 wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP GET for device XML");
330 req = GET_DEVICE_XML_FILE;
332 if (iface->wps->friendly_name)
333 extra_len += os_strlen(iface->wps->friendly_name);
334 if (iface->wps->manufacturer_url)
335 extra_len += os_strlen(iface->wps->manufacturer_url);
336 if (iface->wps->model_description)
337 extra_len += os_strlen(iface->wps->model_description);
338 if (iface->wps->model_url)
339 extra_len += os_strlen(iface->wps->model_url);
341 extra_len += os_strlen(iface->wps->upc);
342 } else if (!os_strcasecmp(filename, UPNP_WPS_SCPD_XML_FILE)) {
343 wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP GET for SCPD XML");
344 req = GET_SCPD_XML_FILE;
345 extra_len = os_strlen(wps_scpd_xml);
348 wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP GET file not found: %s",
350 buf = wpabuf_alloc(200);
352 http_request_deinit(hreq);
356 "HTTP/1.1 404 Not Found\r\n"
357 "Connection: close\r\n");
361 /* terminating empty line */
362 wpabuf_put_str(buf, "\r\n");
367 buf = wpabuf_alloc(1000 + extra_len);
369 http_request_deinit(hreq);
374 "HTTP/1.1 200 OK\r\n"
375 "Content-Type: text/xml; charset=\"utf-8\"\r\n");
376 wpabuf_put_str(buf, "Server: Unspecified, UPnP/1.0, Unspecified\r\n");
377 wpabuf_put_str(buf, "Connection: close\r\n");
378 wpabuf_put_str(buf, "Content-Length: ");
380 * We will paste the length in later, leaving some extra whitespace.
381 * HTTP code is supposed to be tolerant of extra whitespace.
383 put_length_here = wpabuf_put(buf, 0);
384 wpabuf_put_str(buf, " \r\n");
388 /* terminating empty line */
389 wpabuf_put_str(buf, "\r\n");
391 body_start = wpabuf_put(buf, 0);
394 case GET_DEVICE_XML_FILE:
395 format_wps_device_xml(iface, sm, buf);
397 case GET_SCPD_XML_FILE:
398 wpabuf_put_str(buf, wps_scpd_xml);
402 /* Now patch in the content length at the end */
403 body_length = (char *) wpabuf_put(buf, 0) - body_start;
404 os_snprintf(len_buf, 10, "%d", body_length);
405 os_memcpy(put_length_here, len_buf, os_strlen(len_buf));
408 http_request_send_and_deinit(hreq, buf);
412 static enum http_reply_code
413 web_process_get_device_info(struct upnp_wps_device_sm *sm,
414 struct wpabuf **reply, const char **replyname)
416 static const char *name = "NewDeviceInfo";
417 struct wps_config cfg;
418 struct upnp_wps_device_interface *iface;
419 struct upnp_wps_peer *peer;
421 iface = dl_list_first(&sm->interfaces,
422 struct upnp_wps_device_interface, list);
424 wpa_printf(MSG_DEBUG, "WPS UPnP: GetDeviceInfo");
426 if (!iface || iface->ctx->ap_pin == NULL)
427 return HTTP_INTERNAL_SERVER_ERROR;
432 * Request for DeviceInfo, i.e., M1 TLVs. This is a start of WPS
433 * registration over UPnP with the AP acting as an Enrollee. It should
434 * be noted that this is frequently used just to get the device data,
435 * i.e., there may not be any intent to actually complete the
440 wps_deinit(peer->wps);
442 os_memset(&cfg, 0, sizeof(cfg));
443 cfg.wps = iface->wps;
444 cfg.pin = (u8 *) iface->ctx->ap_pin;
445 cfg.pin_len = os_strlen(iface->ctx->ap_pin);
446 peer->wps = wps_init(&cfg);
448 enum wsc_op_code op_code;
449 *reply = wps_get_msg(peer->wps, &op_code);
450 if (*reply == NULL) {
451 wps_deinit(peer->wps);
456 if (*reply == NULL) {
457 wpa_printf(MSG_INFO, "WPS UPnP: Failed to get DeviceInfo");
458 return HTTP_INTERNAL_SERVER_ERROR;
465 static enum http_reply_code
466 web_process_put_message(struct upnp_wps_device_sm *sm, char *data,
467 struct wpabuf **reply, const char **replyname)
470 static const char *name = "NewOutMessage";
471 enum http_reply_code ret;
472 enum wps_process_res res;
473 enum wsc_op_code op_code;
474 struct upnp_wps_device_interface *iface;
476 iface = dl_list_first(&sm->interfaces,
477 struct upnp_wps_device_interface, list);
479 return HTTP_INTERNAL_SERVER_ERROR;
482 * PutMessage is used by external UPnP-based Registrar to perform WPS
483 * operation with the access point itself; as compared with
484 * PutWLANResponse which is for proxying.
486 wpa_printf(MSG_DEBUG, "WPS UPnP: PutMessage");
487 msg = xml_get_base64_item(data, "NewInMessage", &ret);
490 res = wps_process_msg(iface->peer.wps, WSC_UPnP, msg);
491 if (res == WPS_FAILURE)
494 *reply = wps_get_msg(iface->peer.wps, &op_code);
497 return HTTP_INTERNAL_SERVER_ERROR;
503 static enum http_reply_code
504 web_process_put_wlan_response(struct upnp_wps_device_sm *sm, char *data,
505 struct wpabuf **reply, const char **replyname)
508 enum http_reply_code ret;
509 u8 macaddr[ETH_ALEN];
513 struct upnp_wps_device_interface *iface;
517 * External UPnP-based Registrar is passing us a message to be proxied
518 * over to a Wi-Fi -based client of ours.
521 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse");
522 msg = xml_get_base64_item(data, "NewMessage", &ret);
524 wpa_printf(MSG_DEBUG, "WPS UPnP: Could not extract NewMessage "
525 "from PutWLANResponse");
528 val = xml_get_first_item(data, "NewWLANEventType");
530 wpa_printf(MSG_DEBUG, "WPS UPnP: No NewWLANEventType in "
533 return UPNP_ARG_VALUE_INVALID;
537 val = xml_get_first_item(data, "NewWLANEventMAC");
539 wpa_printf(MSG_DEBUG, "WPS UPnP: No NewWLANEventMAC in "
542 return UPNP_ARG_VALUE_INVALID;
544 if (hwaddr_aton(val, macaddr)) {
545 wpa_printf(MSG_DEBUG, "WPS UPnP: Invalid NewWLANEventMAC in "
546 "PutWLANResponse: '%s'", val);
547 #ifdef CONFIG_WPS_STRICT
549 struct wps_parse_attr attr;
550 if (wps_parse_msg(msg, &attr) < 0 || attr.version2) {
553 return UPNP_ARG_VALUE_INVALID;
556 #endif /* CONFIG_WPS_STRICT */
557 if (hwaddr_aton2(val, macaddr) > 0) {
559 * At least some versions of Intel PROset seem to be
560 * using dot-deliminated MAC address format here.
562 wpa_printf(MSG_DEBUG, "WPS UPnP: Workaround - allow "
563 "incorrect MAC address format in "
564 "NewWLANEventMAC: %s -> " MACSTR,
565 val, MAC2STR(macaddr));
569 return UPNP_ARG_VALUE_INVALID;
573 if (ev_type == UPNP_WPS_WLANEVENT_TYPE_EAP) {
574 struct wps_parse_attr attr;
575 if (wps_parse_msg(msg, &attr) < 0 ||
576 attr.msg_type == NULL)
579 type = *attr.msg_type;
580 wpa_printf(MSG_DEBUG, "WPS UPnP: Message Type %d", type);
583 dl_list_for_each(iface, &sm->interfaces,
584 struct upnp_wps_device_interface, list) {
585 if (iface->ctx->rx_req_put_wlan_response &&
586 iface->ctx->rx_req_put_wlan_response(iface->priv, ev_type,
593 wpa_printf(MSG_INFO, "WPS UPnP: Fail: sm->ctx->"
594 "rx_req_put_wlan_response");
596 return HTTP_INTERNAL_SERVER_ERROR;
605 static int find_er_addr(struct subscription *s, struct sockaddr_in *cli)
607 struct subscr_addr *a;
609 dl_list_for_each(a, &s->addr_list, struct subscr_addr, list) {
610 if (cli->sin_addr.s_addr == a->saddr.sin_addr.s_addr)
617 static struct subscription * find_er(struct upnp_wps_device_sm *sm,
618 struct sockaddr_in *cli)
620 struct subscription *s;
621 dl_list_for_each(s, &sm->subscriptions, struct subscription, list)
622 if (find_er_addr(s, cli))
628 static enum http_reply_code
629 web_process_set_selected_registrar(struct upnp_wps_device_sm *sm,
630 struct sockaddr_in *cli, char *data,
631 struct wpabuf **reply,
632 const char **replyname)
635 enum http_reply_code ret;
636 struct subscription *s;
637 struct upnp_wps_device_interface *iface;
640 wpa_printf(MSG_DEBUG, "WPS UPnP: SetSelectedRegistrar");
641 s = find_er(sm, cli);
643 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignore SetSelectedRegistrar "
645 return UPNP_ACTION_FAILED;
647 msg = xml_get_base64_item(data, "NewMessage", &ret);
650 dl_list_for_each(iface, &sm->interfaces,
651 struct upnp_wps_device_interface, list) {
652 if (upnp_er_set_selected_registrar(iface->wps->registrar, s,
658 return HTTP_INTERNAL_SERVER_ERROR;
665 static const char *soap_prefix =
666 "<?xml version=\"1.0\"?>\n"
667 "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
668 "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
670 static const char *soap_postfix =
671 "</s:Body>\n</s:Envelope>\n";
673 static const char *soap_error_prefix =
675 "<faultcode>s:Client</faultcode>\n"
676 "<faultstring>UPnPError</faultstring>\n"
678 "<UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\">\n";
679 static const char *soap_error_postfix =
680 "<errorDescription>Error</errorDescription>\n"
685 static void web_connection_send_reply(struct http_request *req,
686 enum http_reply_code ret,
687 const char *action, int action_len,
688 const struct wpabuf *reply,
689 const char *replyname)
693 char *put_length_here = NULL;
694 char *body_start = NULL;
698 replydata = (char *) base64_encode(wpabuf_head(reply),
699 wpabuf_len(reply), &len);
703 /* Parameters of the response:
704 * action(action_len) -- action we are responding to
705 * replyname -- a name we need for the reply
706 * replydata -- NULL or null-terminated string
708 buf = wpabuf_alloc(1000 + (replydata ? os_strlen(replydata) : 0U) +
709 (action_len > 0 ? action_len * 2 : 0));
711 wpa_printf(MSG_INFO, "WPS UPnP: Cannot allocate reply to "
714 http_request_deinit(req);
719 * Assuming we will be successful, put in the output header first.
720 * Note: we do not keep connections alive (and httpread does
721 * not support it)... therefore we must have Connection: close.
723 if (ret == HTTP_OK) {
725 "HTTP/1.1 200 OK\r\n"
726 "Content-Type: text/xml; "
727 "charset=\"utf-8\"\r\n");
729 wpabuf_printf(buf, "HTTP/1.1 %d Error\r\n", ret);
731 wpabuf_put_str(buf, http_connection_close);
733 wpabuf_put_str(buf, "Content-Length: ");
735 * We will paste the length in later, leaving some extra whitespace.
736 * HTTP code is supposed to be tolerant of extra whitespace.
738 put_length_here = wpabuf_put(buf, 0);
739 wpabuf_put_str(buf, " \r\n");
743 /* terminating empty line */
744 wpabuf_put_str(buf, "\r\n");
746 body_start = wpabuf_put(buf, 0);
748 if (ret == HTTP_OK) {
749 wpabuf_put_str(buf, soap_prefix);
750 wpabuf_put_str(buf, "<u:");
751 wpabuf_put_data(buf, action, action_len);
752 wpabuf_put_str(buf, "Response xmlns:u=\"");
753 wpabuf_put_str(buf, urn_wfawlanconfig);
754 wpabuf_put_str(buf, "\">\n");
755 if (replydata && replyname) {
756 /* TODO: might possibly need to escape part of reply
758 * probably not, unlikely to have ampersand(&) or left
759 * angle bracket (<) in it...
761 wpabuf_printf(buf, "<%s>", replyname);
762 wpabuf_put_str(buf, replydata);
763 wpabuf_printf(buf, "</%s>\n", replyname);
765 wpabuf_put_str(buf, "</u:");
766 wpabuf_put_data(buf, action, action_len);
767 wpabuf_put_str(buf, "Response>\n");
768 wpabuf_put_str(buf, soap_postfix);
771 wpabuf_put_str(buf, soap_prefix);
772 wpabuf_put_str(buf, soap_error_prefix);
773 wpabuf_printf(buf, "<errorCode>%d</errorCode>\n", ret);
774 wpabuf_put_str(buf, soap_error_postfix);
775 wpabuf_put_str(buf, soap_postfix);
779 /* Now patch in the content length at the end */
780 if (body_start && put_length_here) {
781 int body_length = (char *) wpabuf_put(buf, 0) - body_start;
783 os_snprintf(len_buf, sizeof(len_buf), "%d", body_length);
784 os_memcpy(put_length_here, len_buf, os_strlen(len_buf));
787 http_request_send_and_deinit(req, buf);
791 static const char * web_get_action(struct http_request *req,
800 /* The SOAPAction line of the header tells us what we want to do */
801 b = http_request_get_hdr_line(req, "SOAPAction:");
808 match = urn_wfawlanconfig;
809 match_len = os_strlen(urn_wfawlanconfig) - 1;
810 if (os_strncasecmp(b, match, match_len))
813 /* skip over version */
814 while (isgraph(*b) && *b != '#')
819 /* Following the sharp(#) should be the action and a double quote */
821 while (isgraph(*b) && *b != '"')
825 *action_len = b - action;
830 /* Given that we have received a header w/ POST, act upon it
832 * Format of POST (case-insensitive):
834 * First line must be:
835 * POST /<file> HTTP/1.1
836 * Since we don't do anything fancy we just ignore other lines.
838 * Our response (if no error) which includes only required lines is:
841 * Content-Type: text/xml
842 * Date: <rfc1123-date>
844 * Header lines must end with \r\n
845 * Per RFC 2616, content-length: is not required but connection:close
846 * would appear to be required (given that we will be closing it!).
848 static void web_connection_parse_post(struct upnp_wps_device_sm *sm,
849 struct sockaddr_in *cli,
850 struct http_request *req,
851 const char *filename)
853 enum http_reply_code ret;
854 char *data = http_request_get_data(req); /* body of http msg */
855 const char *action = NULL;
856 size_t action_len = 0;
857 const char *replyname = NULL; /* argument name for the reply */
858 struct wpabuf *reply = NULL; /* data for the reply */
860 if (os_strcasecmp(filename, UPNP_WPS_DEVICE_CONTROL_FILE)) {
861 wpa_printf(MSG_INFO, "WPS UPnP: Invalid POST filename %s",
863 ret = HTTP_NOT_FOUND;
867 ret = UPNP_INVALID_ACTION;
868 action = web_get_action(req, &action_len);
872 if (!os_strncasecmp("GetDeviceInfo", action, action_len))
873 ret = web_process_get_device_info(sm, &reply, &replyname);
874 else if (!os_strncasecmp("PutMessage", action, action_len))
875 ret = web_process_put_message(sm, data, &reply, &replyname);
876 else if (!os_strncasecmp("PutWLANResponse", action, action_len))
877 ret = web_process_put_wlan_response(sm, data, &reply,
879 else if (!os_strncasecmp("SetSelectedRegistrar", action, action_len))
880 ret = web_process_set_selected_registrar(sm, cli, data, &reply,
883 wpa_printf(MSG_INFO, "WPS UPnP: Unknown POST type");
887 wpa_printf(MSG_INFO, "WPS UPnP: POST failure ret=%d", ret);
888 web_connection_send_reply(req, ret, action, action_len, reply,
894 /* Given that we have received a header w/ SUBSCRIBE, act upon it
896 * Format of SUBSCRIBE (case-insensitive):
898 * First line must be:
899 * SUBSCRIBE /wps_event HTTP/1.1
901 * Our response (if no error) which includes only required lines is:
903 * Server: xx, UPnP/1.0, xx
904 * SID: uuid:xxxxxxxxx
905 * Timeout: Second-<n>
909 * Header lines must end with \r\n
910 * Per RFC 2616, content-length: is not required but connection:close
911 * would appear to be required (given that we will be closing it!).
913 static void web_connection_parse_subscribe(struct upnp_wps_device_sm *sm,
914 struct http_request *req,
915 const char *filename)
919 char *hdr = http_request_get_hdr(req);
928 char *callback_urls = NULL;
929 struct subscription *s = NULL;
930 enum http_reply_code ret = HTTP_INTERNAL_SERVER_ERROR;
932 buf = wpabuf_alloc(1000);
934 http_request_deinit(req);
938 wpa_hexdump_ascii(MSG_DEBUG, "WPS UPnP: HTTP SUBSCRIBE",
939 (u8 *) hdr, os_strlen(hdr));
941 /* Parse/validate headers */
943 /* First line: SUBSCRIBE /wps_event HTTP/1.1
944 * has already been parsed.
946 if (os_strcasecmp(filename, UPNP_WPS_DEVICE_EVENT_FILE) != 0) {
947 ret = HTTP_PRECONDITION_FAILED;
950 wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP SUBSCRIBE for event");
951 end = os_strchr(h, '\n');
954 /* Option line by option line */
956 end = os_strchr(h, '\n');
958 break; /* no unterminated lines allowed */
960 /* NT assures that it is our type of subscription;
961 * not used for a renewal.
964 match_len = os_strlen(match);
965 if (os_strncasecmp(h, match, match_len) == 0) {
967 while (*h == ' ' || *h == '\t')
969 match = "upnp:event";
970 match_len = os_strlen(match);
971 if (os_strncasecmp(h, match, match_len) != 0) {
972 ret = HTTP_BAD_REQUEST;
978 /* HOST should refer to us */
981 match_len = os_strlen(match);
982 if (os_strncasecmp(h, match, match_len) == 0) {
984 while (*h == ' ' || *h == '\t')
989 /* CALLBACK gives one or more URLs for NOTIFYs
990 * to be sent as a result of the subscription.
991 * Each URL is enclosed in angle brackets.
994 match_len = os_strlen(match);
995 if (os_strncasecmp(h, match, match_len) == 0) {
997 while (*h == ' ' || *h == '\t')
1000 os_free(callback_urls);
1001 callback_urls = dup_binstr(h, len);
1002 if (callback_urls == NULL) {
1003 ret = HTTP_INTERNAL_SERVER_ERROR;
1006 if (len > 0 && callback_urls[len - 1] == '\r')
1007 callback_urls[len - 1] = '\0';
1010 /* SID is only for renewal */
1012 match_len = os_strlen(match);
1013 if (os_strncasecmp(h, match, match_len) == 0) {
1015 while (*h == ' ' || *h == '\t')
1018 match_len = os_strlen(match);
1019 if (os_strncasecmp(h, match, match_len) != 0) {
1020 ret = HTTP_BAD_REQUEST;
1024 while (*h == ' ' || *h == '\t')
1026 if (uuid_str2bin(h, uuid)) {
1027 ret = HTTP_BAD_REQUEST;
1033 /* TIMEOUT is requested timeout, but apparently we can
1040 wpa_printf(MSG_DEBUG, "WPS UPnP: Subscription renewal");
1041 if (callback_urls) {
1042 ret = HTTP_BAD_REQUEST;
1045 s = subscription_renew(sm, uuid);
1048 uuid_bin2str(uuid, str, sizeof(str));
1049 wpa_printf(MSG_DEBUG, "WPS UPnP: Could not find "
1051 ret = HTTP_PRECONDITION_FAILED;
1054 } else if (callback_urls) {
1055 wpa_printf(MSG_DEBUG, "WPS UPnP: New subscription");
1057 ret = HTTP_PRECONDITION_FAILED;
1060 s = subscription_start(sm, callback_urls);
1062 ret = HTTP_INTERNAL_SERVER_ERROR;
1066 ret = HTTP_PRECONDITION_FAILED;
1071 http_put_reply_code(buf, HTTP_OK);
1072 wpabuf_put_str(buf, http_server_hdr);
1073 wpabuf_put_str(buf, http_connection_close);
1074 wpabuf_put_str(buf, "Content-Length: 0\r\n");
1075 wpabuf_put_str(buf, "SID: uuid:");
1076 /* subscription id */
1077 b = wpabuf_put(buf, 0);
1078 uuid_bin2str(s->uuid, b, 80);
1079 wpa_printf(MSG_DEBUG, "WPS UPnP: Assigned SID %s", b);
1080 wpabuf_put(buf, os_strlen(b));
1081 wpabuf_put_str(buf, "\r\n");
1082 wpabuf_printf(buf, "Timeout: Second-%d\r\n", UPNP_SUBSCRIBE_SEC);
1084 /* And empty line to terminate header: */
1085 wpabuf_put_str(buf, "\r\n");
1087 os_free(callback_urls);
1088 http_request_send_and_deinit(req, buf);
1094 * Incompatible headers
1095 * 400 Bad Request. If SID header and one of NT or CALLBACK headers
1096 * are present, the publisher must respond with HTTP error
1098 * Missing or invalid CALLBACK
1099 * 412 Precondition Failed. If CALLBACK header is missing or does not
1100 * contain a valid HTTP URL, the publisher must respond with HTTP
1101 * error 412 Precondition Failed.
1103 * 412 Precondition Failed. If NT header does not equal upnp:event,
1104 * the publisher must respond with HTTP error 412 Precondition
1106 * [For resubscription, use 412 if unknown uuid].
1107 * Unable to accept subscription
1108 * 5xx. If a publisher is not able to accept a subscription (such as
1109 * due to insufficient resources), it must respond with a
1110 * HTTP 500-series error code.
1111 * 599 Too many subscriptions (not a standard HTTP error)
1113 wpa_printf(MSG_DEBUG, "WPS UPnP: SUBSCRIBE failed - return %d", ret);
1114 http_put_empty(buf, ret);
1115 http_request_send_and_deinit(req, buf);
1116 os_free(callback_urls);
1120 /* Given that we have received a header w/ UNSUBSCRIBE, act upon it
1122 * Format of UNSUBSCRIBE (case-insensitive):
1124 * First line must be:
1125 * UNSUBSCRIBE /wps_event HTTP/1.1
1127 * Our response (if no error) which includes only required lines is:
1131 * Header lines must end with \r\n
1132 * Per RFC 2616, content-length: is not required but connection:close
1133 * would appear to be required (given that we will be closing it!).
1135 static void web_connection_parse_unsubscribe(struct upnp_wps_device_sm *sm,
1136 struct http_request *req,
1137 const char *filename)
1140 char *hdr = http_request_get_hdr(req);
1147 struct subscription *s = NULL;
1148 enum http_reply_code ret = HTTP_INTERNAL_SERVER_ERROR;
1150 /* Parse/validate headers */
1152 /* First line: UNSUBSCRIBE /wps_event HTTP/1.1
1153 * has already been parsed.
1155 if (os_strcasecmp(filename, UPNP_WPS_DEVICE_EVENT_FILE) != 0) {
1156 ret = HTTP_PRECONDITION_FAILED;
1159 wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP UNSUBSCRIBE for event");
1160 end = os_strchr(h, '\n');
1163 /* Option line by option line */
1165 end = os_strchr(h, '\n');
1167 break; /* no unterminated lines allowed */
1169 /* HOST should refer to us */
1172 match_len = os_strlen(match);
1173 if (os_strncasecmp(h, match, match_len) == 0) {
1175 while (*h == ' ' || *h == '\t')
1181 match_len = os_strlen(match);
1182 if (os_strncasecmp(h, match, match_len) == 0) {
1184 while (*h == ' ' || *h == '\t')
1187 match_len = os_strlen(match);
1188 if (os_strncasecmp(h, match, match_len) != 0) {
1189 ret = HTTP_BAD_REQUEST;
1193 while (*h == ' ' || *h == '\t')
1195 if (uuid_str2bin(h, uuid)) {
1196 ret = HTTP_BAD_REQUEST;
1204 match_len = os_strlen(match);
1205 if (os_strncasecmp(h, match, match_len) == 0) {
1206 ret = HTTP_BAD_REQUEST;
1210 match = "CALLBACK:";
1211 match_len = os_strlen(match);
1212 if (os_strncasecmp(h, match, match_len) == 0) {
1213 ret = HTTP_BAD_REQUEST;
1221 uuid_bin2str(uuid, str, sizeof(str));
1223 s = subscription_find(sm, uuid);
1225 struct subscr_addr *sa;
1226 sa = dl_list_first(&s->addr_list, struct subscr_addr,
1228 wpa_printf(MSG_DEBUG,
1229 "WPS UPnP: Unsubscribing %p (SID %s) %s",
1230 s, str, (sa && sa->domain_and_port) ?
1231 sa->domain_and_port : "-null-");
1232 dl_list_del(&s->list);
1233 subscription_destroy(s);
1235 wpa_printf(MSG_INFO,
1236 "WPS UPnP: Could not find matching subscription to unsubscribe (SID %s)",
1238 ret = HTTP_PRECONDITION_FAILED;
1242 wpa_printf(MSG_INFO, "WPS UPnP: Unsubscribe fails (not "
1244 ret = HTTP_PRECONDITION_FAILED;
1251 buf = wpabuf_alloc(200);
1253 http_request_deinit(req);
1256 http_put_empty(buf, ret);
1257 http_request_send_and_deinit(req, buf);
1261 /* Send error in response to unknown requests */
1262 static void web_connection_unimplemented(struct http_request *req)
1265 buf = wpabuf_alloc(200);
1267 http_request_deinit(req);
1270 http_put_empty(buf, HTTP_UNIMPLEMENTED);
1271 http_request_send_and_deinit(req, buf);
1276 /* Called when we have gotten an apparently valid http request.
1278 static void web_connection_check_data(void *ctx, struct http_request *req)
1280 struct upnp_wps_device_sm *sm = ctx;
1281 enum httpread_hdr_type htype = http_request_get_type(req);
1282 char *filename = http_request_get_uri(req);
1283 struct sockaddr_in *cli = http_request_get_cli_addr(req);
1286 wpa_printf(MSG_INFO, "WPS UPnP: Could not get HTTP URI");
1287 http_request_deinit(req);
1290 /* Trim leading slashes from filename */
1291 while (*filename == '/')
1294 wpa_printf(MSG_DEBUG, "WPS UPnP: Got HTTP request type %d from %s:%d",
1295 htype, inet_ntoa(cli->sin_addr), htons(cli->sin_port));
1298 case HTTPREAD_HDR_TYPE_GET:
1299 web_connection_parse_get(sm, req, filename);
1301 case HTTPREAD_HDR_TYPE_POST:
1302 web_connection_parse_post(sm, cli, req, filename);
1304 case HTTPREAD_HDR_TYPE_SUBSCRIBE:
1305 web_connection_parse_subscribe(sm, req, filename);
1307 case HTTPREAD_HDR_TYPE_UNSUBSCRIBE:
1308 web_connection_parse_unsubscribe(sm, req, filename);
1311 /* We are not required to support M-POST; just plain
1312 * POST is supposed to work, so we only support that.
1313 * If for some reason we need to support M-POST, it is
1314 * mostly the same as POST, with small differences.
1317 /* Send 501 for anything else */
1318 web_connection_unimplemented(req);
1325 * Listening for web connections
1326 * We have a single TCP listening port, and hand off connections as we get
1330 void web_listener_stop(struct upnp_wps_device_sm *sm)
1332 http_server_deinit(sm->web_srv);
1337 int web_listener_start(struct upnp_wps_device_sm *sm)
1339 struct in_addr addr;
1340 addr.s_addr = sm->ip_addr;
1341 sm->web_srv = http_server_init(&addr, -1, web_connection_check_data,
1343 if (sm->web_srv == NULL) {
1344 web_listener_stop(sm);
1347 sm->web_port = http_server_get_port(sm->web_srv);