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_sm *sm,
186 char uuid_string[80];
188 wpabuf_put_str(buf, wps_device_xml_prefix);
191 * Add required fields with default values if not configured. Add
192 * optional and recommended fields only if configured.
194 s = sm->wps->friendly_name;
195 s = ((s && *s) ? s : "WPS Access Point");
196 xml_add_tagged_data(buf, "friendlyName", s);
198 s = sm->wps->dev.manufacturer;
199 s = ((s && *s) ? s : "");
200 xml_add_tagged_data(buf, "manufacturer", s);
202 if (sm->wps->manufacturer_url)
203 xml_add_tagged_data(buf, "manufacturerURL",
204 sm->wps->manufacturer_url);
206 if (sm->wps->model_description)
207 xml_add_tagged_data(buf, "modelDescription",
208 sm->wps->model_description);
210 s = sm->wps->dev.model_name;
211 s = ((s && *s) ? s : "");
212 xml_add_tagged_data(buf, "modelName", s);
214 if (sm->wps->dev.model_number)
215 xml_add_tagged_data(buf, "modelNumber",
216 sm->wps->dev.model_number);
218 if (sm->wps->model_url)
219 xml_add_tagged_data(buf, "modelURL", sm->wps->model_url);
221 if (sm->wps->dev.serial_number)
222 xml_add_tagged_data(buf, "serialNumber",
223 sm->wps->dev.serial_number);
225 uuid_bin2str(sm->wps->uuid, uuid_string, sizeof(uuid_string));
227 /* Need "uuid:" prefix, thus we can't use xml_add_tagged_data()
230 wpabuf_put_str(buf, "<UDN>uuid:");
231 xml_data_encode(buf, s, os_strlen(s));
232 wpabuf_put_str(buf, "</UDN>\n");
235 xml_add_tagged_data(buf, "UPC", sm->wps->upc);
237 wpabuf_put_str(buf, wps_device_xml_postfix);
241 static void http_put_reply_code(struct wpabuf *buf, enum http_reply_code code)
243 wpabuf_put_str(buf, "HTTP/1.1 ");
246 wpabuf_put_str(buf, "200 OK\r\n");
248 case HTTP_BAD_REQUEST:
249 wpabuf_put_str(buf, "400 Bad request\r\n");
251 case HTTP_PRECONDITION_FAILED:
252 wpabuf_put_str(buf, "412 Precondition failed\r\n");
254 case HTTP_UNIMPLEMENTED:
255 wpabuf_put_str(buf, "501 Unimplemented\r\n");
257 case HTTP_INTERNAL_SERVER_ERROR:
259 wpabuf_put_str(buf, "500 Internal server error\r\n");
265 static void http_put_date(struct wpabuf *buf)
267 wpabuf_put_str(buf, "Date: ");
269 wpabuf_put_str(buf, "\r\n");
273 static void http_put_empty(struct wpabuf *buf, enum http_reply_code code)
275 http_put_reply_code(buf, code);
276 wpabuf_put_str(buf, http_server_hdr);
277 wpabuf_put_str(buf, http_connection_close);
278 wpabuf_put_str(buf, "Content-Length: 0\r\n"
283 /* Given that we have received a header w/ GET, act upon it
285 * Format of GET (case-insensitive):
287 * First line must be:
288 * GET /<file> HTTP/1.1
289 * Since we don't do anything fancy we just ignore other lines.
291 * Our response (if no error) which includes only required lines is:
294 * Content-Type: text/xml
295 * Date: <rfc1123-date>
297 * Header lines must end with \r\n
298 * Per RFC 2616, content-length: is not required but connection:close
299 * would appear to be required (given that we will be closing it!).
301 static void web_connection_parse_get(struct upnp_wps_device_sm *sm,
302 struct http_request *hreq, char *filename)
304 struct wpabuf *buf; /* output buffer, allocated */
305 char *put_length_here;
311 size_t extra_len = 0;
316 * It is not required that filenames be case insensitive but it is
317 * allowed and cannot hurt here.
319 if (filename == NULL)
320 filename = "(null)"; /* just in case */
321 if (os_strcasecmp(filename, UPNP_WPS_DEVICE_XML_FILE) == 0) {
322 wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP GET for device XML");
323 req = GET_DEVICE_XML_FILE;
325 if (sm->wps->friendly_name)
326 extra_len += os_strlen(sm->wps->friendly_name);
327 if (sm->wps->manufacturer_url)
328 extra_len += os_strlen(sm->wps->manufacturer_url);
329 if (sm->wps->model_description)
330 extra_len += os_strlen(sm->wps->model_description);
331 if (sm->wps->model_url)
332 extra_len += os_strlen(sm->wps->model_url);
334 extra_len += os_strlen(sm->wps->upc);
335 } else if (!os_strcasecmp(filename, UPNP_WPS_SCPD_XML_FILE)) {
336 wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP GET for SCPD XML");
337 req = GET_SCPD_XML_FILE;
338 extra_len = os_strlen(wps_scpd_xml);
341 wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP GET file not found: %s",
343 buf = wpabuf_alloc(200);
345 http_request_deinit(hreq);
349 "HTTP/1.1 404 Not Found\r\n"
350 "Connection: close\r\n");
354 /* terminating empty line */
355 wpabuf_put_str(buf, "\r\n");
360 buf = wpabuf_alloc(1000 + extra_len);
362 http_request_deinit(hreq);
367 "HTTP/1.1 200 OK\r\n"
368 "Content-Type: text/xml; charset=\"utf-8\"\r\n");
369 wpabuf_put_str(buf, "Server: Unspecified, UPnP/1.0, Unspecified\r\n");
370 wpabuf_put_str(buf, "Connection: close\r\n");
371 wpabuf_put_str(buf, "Content-Length: ");
373 * We will paste the length in later, leaving some extra whitespace.
374 * HTTP code is supposed to be tolerant of extra whitespace.
376 put_length_here = wpabuf_put(buf, 0);
377 wpabuf_put_str(buf, " \r\n");
381 /* terminating empty line */
382 wpabuf_put_str(buf, "\r\n");
384 body_start = wpabuf_put(buf, 0);
387 case GET_DEVICE_XML_FILE:
388 format_wps_device_xml(sm, buf);
390 case GET_SCPD_XML_FILE:
391 wpabuf_put_str(buf, wps_scpd_xml);
395 /* Now patch in the content length at the end */
396 body_length = (char *) wpabuf_put(buf, 0) - body_start;
397 os_snprintf(len_buf, 10, "%d", body_length);
398 os_memcpy(put_length_here, len_buf, os_strlen(len_buf));
401 http_request_send_and_deinit(hreq, buf);
405 static enum http_reply_code
406 web_process_get_device_info(struct upnp_wps_device_sm *sm,
407 struct wpabuf **reply, const char **replyname)
409 static const char *name = "NewDeviceInfo";
410 struct wps_config cfg;
411 struct upnp_wps_peer *peer = &sm->peer;
413 wpa_printf(MSG_DEBUG, "WPS UPnP: GetDeviceInfo");
416 * Request for DeviceInfo, i.e., M1 TLVs. This is a start of WPS
417 * registration over UPnP with the AP acting as an Enrollee. It should
418 * be noted that this is frequently used just to get the device data,
419 * i.e., there may not be any intent to actually complete the
424 wps_deinit(peer->wps);
426 os_memset(&cfg, 0, sizeof(cfg));
428 cfg.pin = (u8 *) sm->ctx->ap_pin;
429 cfg.pin_len = os_strlen(sm->ctx->ap_pin);
430 peer->wps = wps_init(&cfg);
432 enum wsc_op_code op_code;
433 *reply = wps_get_msg(peer->wps, &op_code);
434 if (*reply == NULL) {
435 wps_deinit(peer->wps);
440 if (*reply == NULL) {
441 wpa_printf(MSG_INFO, "WPS UPnP: Failed to get DeviceInfo");
442 return HTTP_INTERNAL_SERVER_ERROR;
449 static enum http_reply_code
450 web_process_put_message(struct upnp_wps_device_sm *sm, char *data,
451 struct wpabuf **reply, const char **replyname)
454 static const char *name = "NewOutMessage";
455 enum http_reply_code ret;
456 enum wps_process_res res;
457 enum wsc_op_code op_code;
460 * PutMessage is used by external UPnP-based Registrar to perform WPS
461 * operation with the access point itself; as compared with
462 * PutWLANResponse which is for proxying.
464 wpa_printf(MSG_DEBUG, "WPS UPnP: PutMessage");
465 msg = xml_get_base64_item(data, "NewInMessage", &ret);
468 res = wps_process_msg(sm->peer.wps, WSC_UPnP, msg);
469 if (res == WPS_FAILURE)
472 *reply = wps_get_msg(sm->peer.wps, &op_code);
475 return HTTP_INTERNAL_SERVER_ERROR;
481 static enum http_reply_code
482 web_process_put_wlan_response(struct upnp_wps_device_sm *sm, char *data,
483 struct wpabuf **reply, const char **replyname)
486 enum http_reply_code ret;
487 u8 macaddr[ETH_ALEN];
493 * External UPnP-based Registrar is passing us a message to be proxied
494 * over to a Wi-Fi -based client of ours.
497 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse");
498 msg = xml_get_base64_item(data, "NewMessage", &ret);
501 val = xml_get_first_item(data, "NewWLANEventType");
504 return UPNP_ARG_VALUE_INVALID;
508 val = xml_get_first_item(data, "NewWLANEventMAC");
509 if (val == NULL || hwaddr_aton(val, macaddr)) {
512 return UPNP_ARG_VALUE_INVALID;
515 if (ev_type == UPNP_WPS_WLANEVENT_TYPE_EAP) {
516 struct wps_parse_attr attr;
517 if (wps_parse_msg(msg, &attr) < 0 ||
518 attr.msg_type == NULL)
521 type = *attr.msg_type;
522 wpa_printf(MSG_DEBUG, "WPS UPnP: Message Type %d", type);
525 if (!sm->ctx->rx_req_put_wlan_response ||
526 sm->ctx->rx_req_put_wlan_response(sm->priv, ev_type, macaddr, msg,
528 wpa_printf(MSG_INFO, "WPS UPnP: Fail: sm->ctx->"
529 "rx_req_put_wlan_response");
531 return HTTP_INTERNAL_SERVER_ERROR;
540 static int find_er_addr(struct subscription *s, struct sockaddr_in *cli)
542 struct subscr_addr *a;
544 dl_list_for_each(a, &s->addr_list, struct subscr_addr, list) {
545 if (cli->sin_addr.s_addr == a->saddr.sin_addr.s_addr)
552 static struct subscription * find_er(struct upnp_wps_device_sm *sm,
553 struct sockaddr_in *cli)
555 struct subscription *s;
557 s = sm->subscriptions;
559 if (find_er_addr(s, cli))
562 if (s == sm->subscriptions)
570 static enum http_reply_code
571 web_process_set_selected_registrar(struct upnp_wps_device_sm *sm,
572 struct sockaddr_in *cli, char *data,
573 struct wpabuf **reply,
574 const char **replyname)
577 enum http_reply_code ret;
578 struct subscription *s;
580 wpa_printf(MSG_DEBUG, "WPS UPnP: SetSelectedRegistrar");
581 s = find_er(sm, cli);
583 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignore SetSelectedRegistrar "
585 return UPNP_ACTION_FAILED;
587 msg = xml_get_base64_item(data, "NewMessage", &ret);
590 if (upnp_er_set_selected_registrar(sm->wps->registrar, s, msg)) {
592 return HTTP_INTERNAL_SERVER_ERROR;
601 static const char *soap_prefix =
602 "<?xml version=\"1.0\"?>\n"
603 "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
604 "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
606 static const char *soap_postfix =
607 "</s:Body>\n</s:Envelope>\n";
609 static const char *soap_error_prefix =
611 "<faultcode>s:Client</faultcode>\n"
612 "<faultstring>UPnPError</faultstring>\n"
614 "<UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\">\n";
615 static const char *soap_error_postfix =
616 "<errorDescription>Error</errorDescription>\n"
621 static void web_connection_send_reply(struct http_request *req,
622 enum http_reply_code ret,
623 const char *action, int action_len,
624 const struct wpabuf *reply,
625 const char *replyname)
629 char *put_length_here = NULL;
630 char *body_start = NULL;
634 replydata = (char *) base64_encode(wpabuf_head(reply),
635 wpabuf_len(reply), &len);
639 /* Parameters of the response:
640 * action(action_len) -- action we are responding to
641 * replyname -- a name we need for the reply
642 * replydata -- NULL or null-terminated string
644 buf = wpabuf_alloc(1000 + (replydata ? os_strlen(replydata) : 0U) +
645 (action_len > 0 ? action_len * 2 : 0));
647 wpa_printf(MSG_INFO, "WPS UPnP: Cannot allocate reply to "
650 http_request_deinit(req);
655 * Assuming we will be successful, put in the output header first.
656 * Note: we do not keep connections alive (and httpread does
657 * not support it)... therefore we must have Connection: close.
659 if (ret == HTTP_OK) {
661 "HTTP/1.1 200 OK\r\n"
662 "Content-Type: text/xml; "
663 "charset=\"utf-8\"\r\n");
665 wpabuf_printf(buf, "HTTP/1.1 %d Error\r\n", ret);
667 wpabuf_put_str(buf, http_connection_close);
669 wpabuf_put_str(buf, "Content-Length: ");
671 * We will paste the length in later, leaving some extra whitespace.
672 * HTTP code is supposed to be tolerant of extra whitespace.
674 put_length_here = wpabuf_put(buf, 0);
675 wpabuf_put_str(buf, " \r\n");
679 /* terminating empty line */
680 wpabuf_put_str(buf, "\r\n");
682 body_start = wpabuf_put(buf, 0);
684 if (ret == HTTP_OK) {
685 wpabuf_put_str(buf, soap_prefix);
686 wpabuf_put_str(buf, "<u:");
687 wpabuf_put_data(buf, action, action_len);
688 wpabuf_put_str(buf, "Response xmlns:u=\"");
689 wpabuf_put_str(buf, urn_wfawlanconfig);
690 wpabuf_put_str(buf, "\">\n");
691 if (replydata && replyname) {
692 /* TODO: might possibly need to escape part of reply
694 * probably not, unlikely to have ampersand(&) or left
695 * angle bracket (<) in it...
697 wpabuf_printf(buf, "<%s>", replyname);
698 wpabuf_put_str(buf, replydata);
699 wpabuf_printf(buf, "</%s>\n", replyname);
701 wpabuf_put_str(buf, "</u:");
702 wpabuf_put_data(buf, action, action_len);
703 wpabuf_put_str(buf, "Response>\n");
704 wpabuf_put_str(buf, soap_postfix);
707 wpabuf_put_str(buf, soap_prefix);
708 wpabuf_put_str(buf, soap_error_prefix);
709 wpabuf_printf(buf, "<errorCode>%d</errorCode>\n", ret);
710 wpabuf_put_str(buf, soap_error_postfix);
711 wpabuf_put_str(buf, soap_postfix);
715 /* Now patch in the content length at the end */
716 if (body_start && put_length_here) {
717 int body_length = (char *) wpabuf_put(buf, 0) - body_start;
719 os_snprintf(len_buf, sizeof(len_buf), "%d", body_length);
720 os_memcpy(put_length_here, len_buf, os_strlen(len_buf));
723 http_request_send_and_deinit(req, buf);
727 static const char * web_get_action(struct http_request *req,
736 /* The SOAPAction line of the header tells us what we want to do */
737 b = http_request_get_hdr_line(req, "SOAPAction:");
744 match = urn_wfawlanconfig;
745 match_len = os_strlen(urn_wfawlanconfig) - 1;
746 if (os_strncasecmp(b, match, match_len))
749 /* skip over version */
750 while (isgraph(*b) && *b != '#')
755 /* Following the sharp(#) should be the action and a double quote */
757 while (isgraph(*b) && *b != '"')
761 *action_len = b - action;
766 /* Given that we have received a header w/ POST, act upon it
768 * Format of POST (case-insensitive):
770 * First line must be:
771 * POST /<file> HTTP/1.1
772 * Since we don't do anything fancy we just ignore other lines.
774 * Our response (if no error) which includes only required lines is:
777 * Content-Type: text/xml
778 * Date: <rfc1123-date>
780 * Header lines must end with \r\n
781 * Per RFC 2616, content-length: is not required but connection:close
782 * would appear to be required (given that we will be closing it!).
784 static void web_connection_parse_post(struct upnp_wps_device_sm *sm,
785 struct sockaddr_in *cli,
786 struct http_request *req,
787 const char *filename)
789 enum http_reply_code ret;
790 char *data = http_request_get_data(req); /* body of http msg */
791 const char *action = NULL;
792 size_t action_len = 0;
793 const char *replyname = NULL; /* argument name for the reply */
794 struct wpabuf *reply = NULL; /* data for the reply */
796 if (os_strcasecmp(filename, UPNP_WPS_DEVICE_CONTROL_FILE)) {
797 wpa_printf(MSG_INFO, "WPS UPnP: Invalid POST filename %s",
799 ret = HTTP_NOT_FOUND;
803 ret = UPNP_INVALID_ACTION;
804 action = web_get_action(req, &action_len);
808 if (!os_strncasecmp("GetDeviceInfo", action, action_len))
809 ret = web_process_get_device_info(sm, &reply, &replyname);
810 else if (!os_strncasecmp("PutMessage", action, action_len))
811 ret = web_process_put_message(sm, data, &reply, &replyname);
812 else if (!os_strncasecmp("PutWLANResponse", action, action_len))
813 ret = web_process_put_wlan_response(sm, data, &reply,
815 else if (!os_strncasecmp("SetSelectedRegistrar", action, action_len))
816 ret = web_process_set_selected_registrar(sm, cli, data, &reply,
819 wpa_printf(MSG_INFO, "WPS UPnP: Unknown POST type");
823 wpa_printf(MSG_INFO, "WPS UPnP: POST failure ret=%d", ret);
824 web_connection_send_reply(req, ret, action, action_len, reply,
830 /* Given that we have received a header w/ SUBSCRIBE, act upon it
832 * Format of SUBSCRIBE (case-insensitive):
834 * First line must be:
835 * SUBSCRIBE /wps_event HTTP/1.1
837 * Our response (if no error) which includes only required lines is:
839 * Server: xx, UPnP/1.0, xx
840 * SID: uuid:xxxxxxxxx
841 * Timeout: Second-<n>
845 * Header lines must end with \r\n
846 * Per RFC 2616, content-length: is not required but connection:close
847 * would appear to be required (given that we will be closing it!).
849 static void web_connection_parse_subscribe(struct upnp_wps_device_sm *sm,
850 struct http_request *req,
851 const char *filename)
855 char *hdr = http_request_get_hdr(req);
864 char *callback_urls = NULL;
865 struct subscription *s = NULL;
866 enum http_reply_code ret = HTTP_INTERNAL_SERVER_ERROR;
868 buf = wpabuf_alloc(1000);
870 http_request_deinit(req);
874 /* Parse/validate headers */
876 /* First line: SUBSCRIBE /wps_event HTTP/1.1
877 * has already been parsed.
879 if (os_strcasecmp(filename, UPNP_WPS_DEVICE_EVENT_FILE) != 0) {
880 ret = HTTP_PRECONDITION_FAILED;
883 wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP SUBSCRIBE for event");
884 end = os_strchr(h, '\n');
886 for (; end != NULL; h = end + 1) {
887 /* Option line by option line */
889 end = os_strchr(h, '\n');
891 break; /* no unterminated lines allowed */
893 /* NT assures that it is our type of subscription;
894 * not used for a renewl.
897 match_len = os_strlen(match);
898 if (os_strncasecmp(h, match, match_len) == 0) {
900 while (*h == ' ' || *h == '\t')
902 match = "upnp:event";
903 match_len = os_strlen(match);
904 if (os_strncasecmp(h, match, match_len) != 0) {
905 ret = HTTP_BAD_REQUEST;
911 /* HOST should refer to us */
914 match_len = os_strlen(match);
915 if (os_strncasecmp(h, match, match_len) == 0) {
917 while (*h == ' ' || *h == '\t')
922 /* CALLBACK gives one or more URLs for NOTIFYs
923 * to be sent as a result of the subscription.
924 * Each URL is enclosed in angle brackets.
927 match_len = os_strlen(match);
928 if (os_strncasecmp(h, match, match_len) == 0) {
930 while (*h == ' ' || *h == '\t')
933 os_free(callback_urls);
934 callback_urls = os_malloc(len + 1);
935 if (callback_urls == NULL) {
936 ret = HTTP_INTERNAL_SERVER_ERROR;
939 os_memcpy(callback_urls, h, len);
940 callback_urls[len] = 0;
943 /* SID is only for renewal */
945 match_len = os_strlen(match);
946 if (os_strncasecmp(h, match, match_len) == 0) {
948 while (*h == ' ' || *h == '\t')
951 match_len = os_strlen(match);
952 if (os_strncasecmp(h, match, match_len) != 0) {
953 ret = HTTP_BAD_REQUEST;
957 while (*h == ' ' || *h == '\t')
959 if (uuid_str2bin(h, uuid)) {
960 ret = HTTP_BAD_REQUEST;
966 /* TIMEOUT is requested timeout, but apparently we can
974 ret = HTTP_BAD_REQUEST;
977 s = subscription_renew(sm, uuid);
979 ret = HTTP_PRECONDITION_FAILED;
982 } else if (callback_urls) {
984 ret = HTTP_PRECONDITION_FAILED;
987 s = subscription_start(sm, callback_urls);
989 ret = HTTP_INTERNAL_SERVER_ERROR;
993 ret = HTTP_PRECONDITION_FAILED;
998 http_put_reply_code(buf, HTTP_OK);
999 wpabuf_put_str(buf, http_server_hdr);
1000 wpabuf_put_str(buf, http_connection_close);
1001 wpabuf_put_str(buf, "Content-Length: 0\r\n");
1002 wpabuf_put_str(buf, "SID: uuid:");
1003 /* subscription id */
1004 b = wpabuf_put(buf, 0);
1005 uuid_bin2str(s->uuid, b, 80);
1006 wpabuf_put(buf, os_strlen(b));
1007 wpabuf_put_str(buf, "\r\n");
1008 wpabuf_printf(buf, "Timeout: Second-%d\r\n", UPNP_SUBSCRIBE_SEC);
1010 /* And empty line to terminate header: */
1011 wpabuf_put_str(buf, "\r\n");
1013 os_free(callback_urls);
1014 http_request_send_and_deinit(req, buf);
1020 * Incompatible headers
1021 * 400 Bad Request. If SID header and one of NT or CALLBACK headers
1022 * are present, the publisher must respond with HTTP error
1024 * Missing or invalid CALLBACK
1025 * 412 Precondition Failed. If CALLBACK header is missing or does not
1026 * contain a valid HTTP URL, the publisher must respond with HTTP
1027 * error 412 Precondition Failed.
1029 * 412 Precondition Failed. If NT header does not equal upnp:event,
1030 * the publisher must respond with HTTP error 412 Precondition
1032 * [For resubscription, use 412 if unknown uuid].
1033 * Unable to accept subscription
1034 * 5xx. If a publisher is not able to accept a subscription (such as
1035 * due to insufficient resources), it must respond with a
1036 * HTTP 500-series error code.
1037 * 599 Too many subscriptions (not a standard HTTP error)
1039 http_put_empty(buf, ret);
1040 http_request_send_and_deinit(req, buf);
1041 os_free(callback_urls);
1045 /* Given that we have received a header w/ UNSUBSCRIBE, act upon it
1047 * Format of UNSUBSCRIBE (case-insensitive):
1049 * First line must be:
1050 * UNSUBSCRIBE /wps_event HTTP/1.1
1052 * Our response (if no error) which includes only required lines is:
1056 * Header lines must end with \r\n
1057 * Per RFC 2616, content-length: is not required but connection:close
1058 * would appear to be required (given that we will be closing it!).
1060 static void web_connection_parse_unsubscribe(struct upnp_wps_device_sm *sm,
1061 struct http_request *req,
1062 const char *filename)
1065 char *hdr = http_request_get_hdr(req);
1072 struct subscription *s = NULL;
1073 enum http_reply_code ret = HTTP_INTERNAL_SERVER_ERROR;
1075 /* Parse/validate headers */
1077 /* First line: UNSUBSCRIBE /wps_event HTTP/1.1
1078 * has already been parsed.
1080 if (os_strcasecmp(filename, UPNP_WPS_DEVICE_EVENT_FILE) != 0) {
1081 ret = HTTP_PRECONDITION_FAILED;
1084 wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP UNSUBSCRIBE for event");
1085 end = os_strchr(h, '\n');
1087 for (; end != NULL; h = end + 1) {
1088 /* Option line by option line */
1090 end = os_strchr(h, '\n');
1092 break; /* no unterminated lines allowed */
1094 /* HOST should refer to us */
1097 match_len = os_strlen(match);
1098 if (os_strncasecmp(h, match, match_len) == 0) {
1100 while (*h == ' ' || *h == '\t')
1105 /* SID is only for renewal */
1107 match_len = os_strlen(match);
1108 if (os_strncasecmp(h, match, match_len) == 0) {
1110 while (*h == ' ' || *h == '\t')
1113 match_len = os_strlen(match);
1114 if (os_strncasecmp(h, match, match_len) != 0) {
1115 ret = HTTP_BAD_REQUEST;
1119 while (*h == ' ' || *h == '\t')
1121 if (uuid_str2bin(h, uuid)) {
1122 ret = HTTP_BAD_REQUEST;
1131 s = subscription_find(sm, uuid);
1133 struct subscr_addr *sa;
1134 sa = dl_list_first(&s->addr_list, struct subscr_addr,
1136 wpa_printf(MSG_DEBUG, "WPS UPnP: Unsubscribing %p %s",
1137 s, (sa && sa->domain_and_port) ?
1138 sa->domain_and_port : "-null-");
1139 subscription_unlink(s);
1140 subscription_destroy(s);
1143 wpa_printf(MSG_INFO, "WPS UPnP: Unsubscribe fails (not "
1145 ret = HTTP_PRECONDITION_FAILED;
1152 buf = wpabuf_alloc(200);
1154 http_request_deinit(req);
1157 http_put_empty(buf, ret);
1158 http_request_send_and_deinit(req, buf);
1162 /* Send error in response to unknown requests */
1163 static void web_connection_unimplemented(struct http_request *req)
1166 buf = wpabuf_alloc(200);
1168 http_request_deinit(req);
1171 http_put_empty(buf, HTTP_UNIMPLEMENTED);
1172 http_request_send_and_deinit(req, buf);
1177 /* Called when we have gotten an apparently valid http request.
1179 static void web_connection_check_data(void *ctx, struct http_request *req)
1181 struct upnp_wps_device_sm *sm = ctx;
1182 enum httpread_hdr_type htype = http_request_get_type(req);
1183 char *filename = http_request_get_uri(req);
1184 struct sockaddr_in *cli = http_request_get_cli_addr(req);
1187 wpa_printf(MSG_INFO, "WPS UPnP: Could not get HTTP URI");
1188 http_request_deinit(req);
1191 /* Trim leading slashes from filename */
1192 while (*filename == '/')
1195 wpa_printf(MSG_DEBUG, "WPS UPnP: Got HTTP request type %d from %s:%d",
1196 htype, inet_ntoa(cli->sin_addr), htons(cli->sin_port));
1199 case HTTPREAD_HDR_TYPE_GET:
1200 web_connection_parse_get(sm, req, filename);
1202 case HTTPREAD_HDR_TYPE_POST:
1203 web_connection_parse_post(sm, cli, req, filename);
1205 case HTTPREAD_HDR_TYPE_SUBSCRIBE:
1206 web_connection_parse_subscribe(sm, req, filename);
1208 case HTTPREAD_HDR_TYPE_UNSUBSCRIBE:
1209 web_connection_parse_unsubscribe(sm, req, filename);
1212 /* We are not required to support M-POST; just plain
1213 * POST is supposed to work, so we only support that.
1214 * If for some reason we need to support M-POST, it is
1215 * mostly the same as POST, with small differences.
1218 /* Send 501 for anything else */
1219 web_connection_unimplemented(req);
1226 * Listening for web connections
1227 * We have a single TCP listening port, and hand off connections as we get
1231 void web_listener_stop(struct upnp_wps_device_sm *sm)
1233 http_server_deinit(sm->web_srv);
1238 int web_listener_start(struct upnp_wps_device_sm *sm)
1240 struct in_addr addr;
1241 addr.s_addr = sm->ip_addr;
1242 sm->web_srv = http_server_init(&addr, -1, web_connection_check_data,
1244 if (sm->web_srv == NULL) {
1245 web_listener_stop(sm);
1248 sm->web_port = http_server_get_port(sm->web_srv);