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");
415 if (sm->ctx->ap_pin == NULL)
416 return HTTP_INTERNAL_SERVER_ERROR;
419 * Request for DeviceInfo, i.e., M1 TLVs. This is a start of WPS
420 * registration over UPnP with the AP acting as an Enrollee. It should
421 * be noted that this is frequently used just to get the device data,
422 * i.e., there may not be any intent to actually complete the
427 wps_deinit(peer->wps);
429 os_memset(&cfg, 0, sizeof(cfg));
431 cfg.pin = (u8 *) sm->ctx->ap_pin;
432 cfg.pin_len = os_strlen(sm->ctx->ap_pin);
433 peer->wps = wps_init(&cfg);
435 enum wsc_op_code op_code;
436 *reply = wps_get_msg(peer->wps, &op_code);
437 if (*reply == NULL) {
438 wps_deinit(peer->wps);
443 if (*reply == NULL) {
444 wpa_printf(MSG_INFO, "WPS UPnP: Failed to get DeviceInfo");
445 return HTTP_INTERNAL_SERVER_ERROR;
452 static enum http_reply_code
453 web_process_put_message(struct upnp_wps_device_sm *sm, char *data,
454 struct wpabuf **reply, const char **replyname)
457 static const char *name = "NewOutMessage";
458 enum http_reply_code ret;
459 enum wps_process_res res;
460 enum wsc_op_code op_code;
463 * PutMessage is used by external UPnP-based Registrar to perform WPS
464 * operation with the access point itself; as compared with
465 * PutWLANResponse which is for proxying.
467 wpa_printf(MSG_DEBUG, "WPS UPnP: PutMessage");
468 msg = xml_get_base64_item(data, "NewInMessage", &ret);
471 res = wps_process_msg(sm->peer.wps, WSC_UPnP, msg);
472 if (res == WPS_FAILURE)
475 *reply = wps_get_msg(sm->peer.wps, &op_code);
478 return HTTP_INTERNAL_SERVER_ERROR;
484 static enum http_reply_code
485 web_process_put_wlan_response(struct upnp_wps_device_sm *sm, char *data,
486 struct wpabuf **reply, const char **replyname)
489 enum http_reply_code ret;
490 u8 macaddr[ETH_ALEN];
496 * External UPnP-based Registrar is passing us a message to be proxied
497 * over to a Wi-Fi -based client of ours.
500 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse");
501 msg = xml_get_base64_item(data, "NewMessage", &ret);
503 wpa_printf(MSG_DEBUG, "WPS UPnP: Could not extract NewMessage "
504 "from PutWLANResponse");
507 val = xml_get_first_item(data, "NewWLANEventType");
509 wpa_printf(MSG_DEBUG, "WPS UPnP: No NewWLANEventType in "
512 return UPNP_ARG_VALUE_INVALID;
516 val = xml_get_first_item(data, "NewWLANEventMAC");
518 wpa_printf(MSG_DEBUG, "WPS UPnP: No NewWLANEventMAC in "
521 return UPNP_ARG_VALUE_INVALID;
523 if (hwaddr_aton(val, macaddr)) {
524 wpa_printf(MSG_DEBUG, "WPS UPnP: Invalid NewWLANEventMAC in "
525 "PutWLANResponse: '%s'", val);
526 #ifdef CONFIG_WPS_STRICT
528 struct wps_parse_attr attr;
529 if (wps_parse_msg(msg, &attr) < 0 || attr.version2) {
532 return UPNP_ARG_VALUE_INVALID;
535 #endif /* CONFIG_WPS_STRICT */
536 if (hwaddr_aton2(val, macaddr) > 0) {
538 * At least some versions of Intel PROset seem to be
539 * using dot-deliminated MAC address format here.
541 wpa_printf(MSG_DEBUG, "WPS UPnP: Workaround - allow "
542 "incorrect MAC address format in "
543 "NewWLANEventMAC: %s -> " MACSTR,
544 val, MAC2STR(macaddr));
548 return UPNP_ARG_VALUE_INVALID;
552 if (ev_type == UPNP_WPS_WLANEVENT_TYPE_EAP) {
553 struct wps_parse_attr attr;
554 if (wps_parse_msg(msg, &attr) < 0 ||
555 attr.msg_type == NULL)
558 type = *attr.msg_type;
559 wpa_printf(MSG_DEBUG, "WPS UPnP: Message Type %d", type);
562 if (!sm->ctx->rx_req_put_wlan_response ||
563 sm->ctx->rx_req_put_wlan_response(sm->priv, ev_type, macaddr, msg,
565 wpa_printf(MSG_INFO, "WPS UPnP: Fail: sm->ctx->"
566 "rx_req_put_wlan_response");
568 return HTTP_INTERNAL_SERVER_ERROR;
577 static int find_er_addr(struct subscription *s, struct sockaddr_in *cli)
579 struct subscr_addr *a;
581 dl_list_for_each(a, &s->addr_list, struct subscr_addr, list) {
582 if (cli->sin_addr.s_addr == a->saddr.sin_addr.s_addr)
589 static struct subscription * find_er(struct upnp_wps_device_sm *sm,
590 struct sockaddr_in *cli)
592 struct subscription *s;
593 dl_list_for_each(s, &sm->subscriptions, struct subscription, list)
594 if (find_er_addr(s, cli))
600 static enum http_reply_code
601 web_process_set_selected_registrar(struct upnp_wps_device_sm *sm,
602 struct sockaddr_in *cli, char *data,
603 struct wpabuf **reply,
604 const char **replyname)
607 enum http_reply_code ret;
608 struct subscription *s;
610 wpa_printf(MSG_DEBUG, "WPS UPnP: SetSelectedRegistrar");
611 s = find_er(sm, cli);
613 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignore SetSelectedRegistrar "
615 return UPNP_ACTION_FAILED;
617 msg = xml_get_base64_item(data, "NewMessage", &ret);
620 if (upnp_er_set_selected_registrar(sm->wps->registrar, s, msg)) {
622 return HTTP_INTERNAL_SERVER_ERROR;
631 static const char *soap_prefix =
632 "<?xml version=\"1.0\"?>\n"
633 "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
634 "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
636 static const char *soap_postfix =
637 "</s:Body>\n</s:Envelope>\n";
639 static const char *soap_error_prefix =
641 "<faultcode>s:Client</faultcode>\n"
642 "<faultstring>UPnPError</faultstring>\n"
644 "<UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\">\n";
645 static const char *soap_error_postfix =
646 "<errorDescription>Error</errorDescription>\n"
651 static void web_connection_send_reply(struct http_request *req,
652 enum http_reply_code ret,
653 const char *action, int action_len,
654 const struct wpabuf *reply,
655 const char *replyname)
659 char *put_length_here = NULL;
660 char *body_start = NULL;
664 replydata = (char *) base64_encode(wpabuf_head(reply),
665 wpabuf_len(reply), &len);
669 /* Parameters of the response:
670 * action(action_len) -- action we are responding to
671 * replyname -- a name we need for the reply
672 * replydata -- NULL or null-terminated string
674 buf = wpabuf_alloc(1000 + (replydata ? os_strlen(replydata) : 0U) +
675 (action_len > 0 ? action_len * 2 : 0));
677 wpa_printf(MSG_INFO, "WPS UPnP: Cannot allocate reply to "
680 http_request_deinit(req);
685 * Assuming we will be successful, put in the output header first.
686 * Note: we do not keep connections alive (and httpread does
687 * not support it)... therefore we must have Connection: close.
689 if (ret == HTTP_OK) {
691 "HTTP/1.1 200 OK\r\n"
692 "Content-Type: text/xml; "
693 "charset=\"utf-8\"\r\n");
695 wpabuf_printf(buf, "HTTP/1.1 %d Error\r\n", ret);
697 wpabuf_put_str(buf, http_connection_close);
699 wpabuf_put_str(buf, "Content-Length: ");
701 * We will paste the length in later, leaving some extra whitespace.
702 * HTTP code is supposed to be tolerant of extra whitespace.
704 put_length_here = wpabuf_put(buf, 0);
705 wpabuf_put_str(buf, " \r\n");
709 /* terminating empty line */
710 wpabuf_put_str(buf, "\r\n");
712 body_start = wpabuf_put(buf, 0);
714 if (ret == HTTP_OK) {
715 wpabuf_put_str(buf, soap_prefix);
716 wpabuf_put_str(buf, "<u:");
717 wpabuf_put_data(buf, action, action_len);
718 wpabuf_put_str(buf, "Response xmlns:u=\"");
719 wpabuf_put_str(buf, urn_wfawlanconfig);
720 wpabuf_put_str(buf, "\">\n");
721 if (replydata && replyname) {
722 /* TODO: might possibly need to escape part of reply
724 * probably not, unlikely to have ampersand(&) or left
725 * angle bracket (<) in it...
727 wpabuf_printf(buf, "<%s>", replyname);
728 wpabuf_put_str(buf, replydata);
729 wpabuf_printf(buf, "</%s>\n", replyname);
731 wpabuf_put_str(buf, "</u:");
732 wpabuf_put_data(buf, action, action_len);
733 wpabuf_put_str(buf, "Response>\n");
734 wpabuf_put_str(buf, soap_postfix);
737 wpabuf_put_str(buf, soap_prefix);
738 wpabuf_put_str(buf, soap_error_prefix);
739 wpabuf_printf(buf, "<errorCode>%d</errorCode>\n", ret);
740 wpabuf_put_str(buf, soap_error_postfix);
741 wpabuf_put_str(buf, soap_postfix);
745 /* Now patch in the content length at the end */
746 if (body_start && put_length_here) {
747 int body_length = (char *) wpabuf_put(buf, 0) - body_start;
749 os_snprintf(len_buf, sizeof(len_buf), "%d", body_length);
750 os_memcpy(put_length_here, len_buf, os_strlen(len_buf));
753 http_request_send_and_deinit(req, buf);
757 static const char * web_get_action(struct http_request *req,
766 /* The SOAPAction line of the header tells us what we want to do */
767 b = http_request_get_hdr_line(req, "SOAPAction:");
774 match = urn_wfawlanconfig;
775 match_len = os_strlen(urn_wfawlanconfig) - 1;
776 if (os_strncasecmp(b, match, match_len))
779 /* skip over version */
780 while (isgraph(*b) && *b != '#')
785 /* Following the sharp(#) should be the action and a double quote */
787 while (isgraph(*b) && *b != '"')
791 *action_len = b - action;
796 /* Given that we have received a header w/ POST, act upon it
798 * Format of POST (case-insensitive):
800 * First line must be:
801 * POST /<file> HTTP/1.1
802 * Since we don't do anything fancy we just ignore other lines.
804 * Our response (if no error) which includes only required lines is:
807 * Content-Type: text/xml
808 * Date: <rfc1123-date>
810 * Header lines must end with \r\n
811 * Per RFC 2616, content-length: is not required but connection:close
812 * would appear to be required (given that we will be closing it!).
814 static void web_connection_parse_post(struct upnp_wps_device_sm *sm,
815 struct sockaddr_in *cli,
816 struct http_request *req,
817 const char *filename)
819 enum http_reply_code ret;
820 char *data = http_request_get_data(req); /* body of http msg */
821 const char *action = NULL;
822 size_t action_len = 0;
823 const char *replyname = NULL; /* argument name for the reply */
824 struct wpabuf *reply = NULL; /* data for the reply */
826 if (os_strcasecmp(filename, UPNP_WPS_DEVICE_CONTROL_FILE)) {
827 wpa_printf(MSG_INFO, "WPS UPnP: Invalid POST filename %s",
829 ret = HTTP_NOT_FOUND;
833 ret = UPNP_INVALID_ACTION;
834 action = web_get_action(req, &action_len);
838 if (!os_strncasecmp("GetDeviceInfo", action, action_len))
839 ret = web_process_get_device_info(sm, &reply, &replyname);
840 else if (!os_strncasecmp("PutMessage", action, action_len))
841 ret = web_process_put_message(sm, data, &reply, &replyname);
842 else if (!os_strncasecmp("PutWLANResponse", action, action_len))
843 ret = web_process_put_wlan_response(sm, data, &reply,
845 else if (!os_strncasecmp("SetSelectedRegistrar", action, action_len))
846 ret = web_process_set_selected_registrar(sm, cli, data, &reply,
849 wpa_printf(MSG_INFO, "WPS UPnP: Unknown POST type");
853 wpa_printf(MSG_INFO, "WPS UPnP: POST failure ret=%d", ret);
854 web_connection_send_reply(req, ret, action, action_len, reply,
860 /* Given that we have received a header w/ SUBSCRIBE, act upon it
862 * Format of SUBSCRIBE (case-insensitive):
864 * First line must be:
865 * SUBSCRIBE /wps_event HTTP/1.1
867 * Our response (if no error) which includes only required lines is:
869 * Server: xx, UPnP/1.0, xx
870 * SID: uuid:xxxxxxxxx
871 * Timeout: Second-<n>
875 * Header lines must end with \r\n
876 * Per RFC 2616, content-length: is not required but connection:close
877 * would appear to be required (given that we will be closing it!).
879 static void web_connection_parse_subscribe(struct upnp_wps_device_sm *sm,
880 struct http_request *req,
881 const char *filename)
885 char *hdr = http_request_get_hdr(req);
894 char *callback_urls = NULL;
895 struct subscription *s = NULL;
896 enum http_reply_code ret = HTTP_INTERNAL_SERVER_ERROR;
898 buf = wpabuf_alloc(1000);
900 http_request_deinit(req);
904 /* Parse/validate headers */
906 /* First line: SUBSCRIBE /wps_event HTTP/1.1
907 * has already been parsed.
909 if (os_strcasecmp(filename, UPNP_WPS_DEVICE_EVENT_FILE) != 0) {
910 ret = HTTP_PRECONDITION_FAILED;
913 wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP SUBSCRIBE for event");
914 end = os_strchr(h, '\n');
916 for (; end != NULL; h = end + 1) {
917 /* Option line by option line */
919 end = os_strchr(h, '\n');
921 break; /* no unterminated lines allowed */
923 /* NT assures that it is our type of subscription;
924 * not used for a renewl.
927 match_len = os_strlen(match);
928 if (os_strncasecmp(h, match, match_len) == 0) {
930 while (*h == ' ' || *h == '\t')
932 match = "upnp:event";
933 match_len = os_strlen(match);
934 if (os_strncasecmp(h, match, match_len) != 0) {
935 ret = HTTP_BAD_REQUEST;
941 /* HOST should refer to us */
944 match_len = os_strlen(match);
945 if (os_strncasecmp(h, match, match_len) == 0) {
947 while (*h == ' ' || *h == '\t')
952 /* CALLBACK gives one or more URLs for NOTIFYs
953 * to be sent as a result of the subscription.
954 * Each URL is enclosed in angle brackets.
957 match_len = os_strlen(match);
958 if (os_strncasecmp(h, match, match_len) == 0) {
960 while (*h == ' ' || *h == '\t')
963 os_free(callback_urls);
964 callback_urls = os_malloc(len + 1);
965 if (callback_urls == NULL) {
966 ret = HTTP_INTERNAL_SERVER_ERROR;
969 os_memcpy(callback_urls, h, len);
970 callback_urls[len] = 0;
973 /* SID is only for renewal */
975 match_len = os_strlen(match);
976 if (os_strncasecmp(h, match, match_len) == 0) {
978 while (*h == ' ' || *h == '\t')
981 match_len = os_strlen(match);
982 if (os_strncasecmp(h, match, match_len) != 0) {
983 ret = HTTP_BAD_REQUEST;
987 while (*h == ' ' || *h == '\t')
989 if (uuid_str2bin(h, uuid)) {
990 ret = HTTP_BAD_REQUEST;
996 /* TIMEOUT is requested timeout, but apparently we can
1003 if (callback_urls) {
1004 ret = HTTP_BAD_REQUEST;
1007 s = subscription_renew(sm, uuid);
1009 ret = HTTP_PRECONDITION_FAILED;
1012 } else if (callback_urls) {
1014 ret = HTTP_PRECONDITION_FAILED;
1017 s = subscription_start(sm, callback_urls);
1019 ret = HTTP_INTERNAL_SERVER_ERROR;
1023 ret = HTTP_PRECONDITION_FAILED;
1028 http_put_reply_code(buf, HTTP_OK);
1029 wpabuf_put_str(buf, http_server_hdr);
1030 wpabuf_put_str(buf, http_connection_close);
1031 wpabuf_put_str(buf, "Content-Length: 0\r\n");
1032 wpabuf_put_str(buf, "SID: uuid:");
1033 /* subscription id */
1034 b = wpabuf_put(buf, 0);
1035 uuid_bin2str(s->uuid, b, 80);
1036 wpabuf_put(buf, os_strlen(b));
1037 wpabuf_put_str(buf, "\r\n");
1038 wpabuf_printf(buf, "Timeout: Second-%d\r\n", UPNP_SUBSCRIBE_SEC);
1040 /* And empty line to terminate header: */
1041 wpabuf_put_str(buf, "\r\n");
1043 os_free(callback_urls);
1044 http_request_send_and_deinit(req, buf);
1050 * Incompatible headers
1051 * 400 Bad Request. If SID header and one of NT or CALLBACK headers
1052 * are present, the publisher must respond with HTTP error
1054 * Missing or invalid CALLBACK
1055 * 412 Precondition Failed. If CALLBACK header is missing or does not
1056 * contain a valid HTTP URL, the publisher must respond with HTTP
1057 * error 412 Precondition Failed.
1059 * 412 Precondition Failed. If NT header does not equal upnp:event,
1060 * the publisher must respond with HTTP error 412 Precondition
1062 * [For resubscription, use 412 if unknown uuid].
1063 * Unable to accept subscription
1064 * 5xx. If a publisher is not able to accept a subscription (such as
1065 * due to insufficient resources), it must respond with a
1066 * HTTP 500-series error code.
1067 * 599 Too many subscriptions (not a standard HTTP error)
1069 http_put_empty(buf, ret);
1070 http_request_send_and_deinit(req, buf);
1071 os_free(callback_urls);
1075 /* Given that we have received a header w/ UNSUBSCRIBE, act upon it
1077 * Format of UNSUBSCRIBE (case-insensitive):
1079 * First line must be:
1080 * UNSUBSCRIBE /wps_event HTTP/1.1
1082 * Our response (if no error) which includes only required lines is:
1086 * Header lines must end with \r\n
1087 * Per RFC 2616, content-length: is not required but connection:close
1088 * would appear to be required (given that we will be closing it!).
1090 static void web_connection_parse_unsubscribe(struct upnp_wps_device_sm *sm,
1091 struct http_request *req,
1092 const char *filename)
1095 char *hdr = http_request_get_hdr(req);
1102 struct subscription *s = NULL;
1103 enum http_reply_code ret = HTTP_INTERNAL_SERVER_ERROR;
1105 /* Parse/validate headers */
1107 /* First line: UNSUBSCRIBE /wps_event HTTP/1.1
1108 * has already been parsed.
1110 if (os_strcasecmp(filename, UPNP_WPS_DEVICE_EVENT_FILE) != 0) {
1111 ret = HTTP_PRECONDITION_FAILED;
1114 wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP UNSUBSCRIBE for event");
1115 end = os_strchr(h, '\n');
1117 for (; end != NULL; h = end + 1) {
1118 /* Option line by option line */
1120 end = os_strchr(h, '\n');
1122 break; /* no unterminated lines allowed */
1124 /* HOST should refer to us */
1127 match_len = os_strlen(match);
1128 if (os_strncasecmp(h, match, match_len) == 0) {
1130 while (*h == ' ' || *h == '\t')
1135 /* SID is only for renewal */
1137 match_len = os_strlen(match);
1138 if (os_strncasecmp(h, match, match_len) == 0) {
1140 while (*h == ' ' || *h == '\t')
1143 match_len = os_strlen(match);
1144 if (os_strncasecmp(h, match, match_len) != 0) {
1145 ret = HTTP_BAD_REQUEST;
1149 while (*h == ' ' || *h == '\t')
1151 if (uuid_str2bin(h, uuid)) {
1152 ret = HTTP_BAD_REQUEST;
1161 s = subscription_find(sm, uuid);
1163 struct subscr_addr *sa;
1164 sa = dl_list_first(&s->addr_list, struct subscr_addr,
1166 wpa_printf(MSG_DEBUG, "WPS UPnP: Unsubscribing %p %s",
1167 s, (sa && sa->domain_and_port) ?
1168 sa->domain_and_port : "-null-");
1169 dl_list_del(&s->list);
1170 subscription_destroy(s);
1173 wpa_printf(MSG_INFO, "WPS UPnP: Unsubscribe fails (not "
1175 ret = HTTP_PRECONDITION_FAILED;
1182 buf = wpabuf_alloc(200);
1184 http_request_deinit(req);
1187 http_put_empty(buf, ret);
1188 http_request_send_and_deinit(req, buf);
1192 /* Send error in response to unknown requests */
1193 static void web_connection_unimplemented(struct http_request *req)
1196 buf = wpabuf_alloc(200);
1198 http_request_deinit(req);
1201 http_put_empty(buf, HTTP_UNIMPLEMENTED);
1202 http_request_send_and_deinit(req, buf);
1207 /* Called when we have gotten an apparently valid http request.
1209 static void web_connection_check_data(void *ctx, struct http_request *req)
1211 struct upnp_wps_device_sm *sm = ctx;
1212 enum httpread_hdr_type htype = http_request_get_type(req);
1213 char *filename = http_request_get_uri(req);
1214 struct sockaddr_in *cli = http_request_get_cli_addr(req);
1217 wpa_printf(MSG_INFO, "WPS UPnP: Could not get HTTP URI");
1218 http_request_deinit(req);
1221 /* Trim leading slashes from filename */
1222 while (*filename == '/')
1225 wpa_printf(MSG_DEBUG, "WPS UPnP: Got HTTP request type %d from %s:%d",
1226 htype, inet_ntoa(cli->sin_addr), htons(cli->sin_port));
1229 case HTTPREAD_HDR_TYPE_GET:
1230 web_connection_parse_get(sm, req, filename);
1232 case HTTPREAD_HDR_TYPE_POST:
1233 web_connection_parse_post(sm, cli, req, filename);
1235 case HTTPREAD_HDR_TYPE_SUBSCRIBE:
1236 web_connection_parse_subscribe(sm, req, filename);
1238 case HTTPREAD_HDR_TYPE_UNSUBSCRIBE:
1239 web_connection_parse_unsubscribe(sm, req, filename);
1242 /* We are not required to support M-POST; just plain
1243 * POST is supposed to work, so we only support that.
1244 * If for some reason we need to support M-POST, it is
1245 * mostly the same as POST, with small differences.
1248 /* Send 501 for anything else */
1249 web_connection_unimplemented(req);
1256 * Listening for web connections
1257 * We have a single TCP listening port, and hand off connections as we get
1261 void web_listener_stop(struct upnp_wps_device_sm *sm)
1263 http_server_deinit(sm->web_srv);
1268 int web_listener_start(struct upnp_wps_device_sm *sm)
1270 struct in_addr addr;
1271 addr.s_addr = sm->ip_addr;
1272 sm->web_srv = http_server_init(&addr, -1, web_connection_check_data,
1274 if (sm->web_srv == NULL) {
1275 web_listener_stop(sm);
1278 sm->web_port = http_server_get_port(sm->web_srv);