automake build system
[mech_eap.orig] / src / wps / wps_upnp_web.c
1 /*
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>
7  *
8  * See wps_upnp.c for more details on licensing and code history.
9  */
10
11 #include "includes.h"
12
13 #include "common.h"
14 #include "base64.h"
15 #include "uuid.h"
16 #include "httpread.h"
17 #include "http_server.h"
18 #include "wps_i.h"
19 #include "wps_upnp.h"
20 #include "wps_upnp_i.h"
21 #include "upnp_xml.h"
22
23 /***************************************************************************
24  * Web connections (we serve pages of info about ourselves, handle
25  * requests, etc. etc.).
26  **************************************************************************/
27
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 */
31
32
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";
39
40 /*
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.
43  */
44
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"
49 "<actionList>\n"
50 "<action>\n"
51 "<name>GetDeviceInfo</name>\n"
52 "<argumentList>\n"
53 "<argument>\n"
54 "<name>NewDeviceInfo</name>\n"
55 "<direction>out</direction>\n"
56 "<relatedStateVariable>DeviceInfo</relatedStateVariable>\n"
57 "</argument>\n"
58 "</argumentList>\n"
59 "</action>\n"
60 "<action>\n"
61 "<name>PutMessage</name>\n"
62 "<argumentList>\n"
63 "<argument>\n"
64 "<name>NewInMessage</name>\n"
65 "<direction>in</direction>\n"
66 "<relatedStateVariable>InMessage</relatedStateVariable>\n"
67 "</argument>\n"
68 "<argument>\n"
69 "<name>NewOutMessage</name>\n"
70 "<direction>out</direction>\n"
71 "<relatedStateVariable>OutMessage</relatedStateVariable>\n"
72 "</argument>\n"
73 "</argumentList>\n"
74 "</action>\n"
75 "<action>\n"
76 "<name>PutWLANResponse</name>\n"
77 "<argumentList>\n"
78 "<argument>\n"
79 "<name>NewMessage</name>\n"
80 "<direction>in</direction>\n"
81 "<relatedStateVariable>Message</relatedStateVariable>\n"
82 "</argument>\n"
83 "<argument>\n"
84 "<name>NewWLANEventType</name>\n"
85 "<direction>in</direction>\n"
86 "<relatedStateVariable>WLANEventType</relatedStateVariable>\n"
87 "</argument>\n"
88 "<argument>\n"
89 "<name>NewWLANEventMAC</name>\n"
90 "<direction>in</direction>\n"
91 "<relatedStateVariable>WLANEventMAC</relatedStateVariable>\n"
92 "</argument>\n"
93 "</argumentList>\n"
94 "</action>\n"
95 "<action>\n"
96 "<name>SetSelectedRegistrar</name>\n"
97 "<argumentList>\n"
98 "<argument>\n"
99 "<name>NewMessage</name>\n"
100 "<direction>in</direction>\n"
101 "<relatedStateVariable>Message</relatedStateVariable>\n"
102 "</argument>\n"
103 "</argumentList>\n"
104 "</action>\n"
105 "</actionList>\n"
106 "<serviceStateTable>\n"
107 "<stateVariable sendEvents=\"no\">\n"
108 "<name>Message</name>\n"
109 "<dataType>bin.base64</dataType>\n"
110 "</stateVariable>\n"
111 "<stateVariable sendEvents=\"no\">\n"
112 "<name>InMessage</name>\n"
113 "<dataType>bin.base64</dataType>\n"
114 "</stateVariable>\n"
115 "<stateVariable sendEvents=\"no\">\n"
116 "<name>OutMessage</name>\n"
117 "<dataType>bin.base64</dataType>\n"
118 "</stateVariable>\n"
119 "<stateVariable sendEvents=\"no\">\n"
120 "<name>DeviceInfo</name>\n"
121 "<dataType>bin.base64</dataType>\n"
122 "</stateVariable>\n"
123 "<stateVariable sendEvents=\"yes\">\n"
124 "<name>APStatus</name>\n"
125 "<dataType>ui1</dataType>\n"
126 "</stateVariable>\n"
127 "<stateVariable sendEvents=\"yes\">\n"
128 "<name>STAStatus</name>\n"
129 "<dataType>ui1</dataType>\n"
130 "</stateVariable>\n"
131 "<stateVariable sendEvents=\"yes\">\n"
132 "<name>WLANEvent</name>\n"
133 "<dataType>bin.base64</dataType>\n"
134 "</stateVariable>\n"
135 "<stateVariable sendEvents=\"no\">\n"
136 "<name>WLANEventType</name>\n"
137 "<dataType>ui1</dataType>\n"
138 "</stateVariable>\n"
139 "<stateVariable sendEvents=\"no\">\n"
140 "<name>WLANEventMAC</name>\n"
141 "<dataType>string</dataType>\n"
142 "</stateVariable>\n"
143 "<stateVariable sendEvents=\"no\">\n"
144 "<name>WLANResponse</name>\n"
145 "<dataType>bin.base64</dataType>\n"
146 "</stateVariable>\n"
147 "</serviceStateTable>\n"
148 "</scpd>\n"
149 ;
150
151
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"
155         "<specVersion>\n"
156         "<major>1</major>\n"
157         "<minor>0</minor>\n"
158         "</specVersion>\n"
159         "<device>\n"
160         "<deviceType>urn:schemas-wifialliance-org:device:WFADevice:1"
161         "</deviceType>\n";
162
163 static const char *wps_device_xml_postfix =
164         "<serviceList>\n"
165         "<service>\n"
166         "<serviceType>urn:schemas-wifialliance-org:service:WFAWLANConfig:1"
167         "</serviceType>\n"
168         "<serviceId>urn:wifialliance-org:serviceId:WFAWLANConfig1</serviceId>"
169         "\n"
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"
173         "</service>\n"
174         "</serviceList>\n"
175         "</device>\n"
176         "</root>\n";
177
178
179 /* format_wps_device_xml -- produce content of "file" wps_device.xml
180  * (UPNP_WPS_DEVICE_XML_FILE)
181  */
182 static void format_wps_device_xml(struct upnp_wps_device_sm *sm,
183                                   struct wpabuf *buf)
184 {
185         const char *s;
186         char uuid_string[80];
187
188         wpabuf_put_str(buf, wps_device_xml_prefix);
189
190         /*
191          * Add required fields with default values if not configured. Add
192          * optional and recommended fields only if configured.
193          */
194         s = sm->wps->friendly_name;
195         s = ((s && *s) ? s : "WPS Access Point");
196         xml_add_tagged_data(buf, "friendlyName", s);
197
198         s = sm->wps->dev.manufacturer;
199         s = ((s && *s) ? s : "");
200         xml_add_tagged_data(buf, "manufacturer", s);
201
202         if (sm->wps->manufacturer_url)
203                 xml_add_tagged_data(buf, "manufacturerURL",
204                                     sm->wps->manufacturer_url);
205
206         if (sm->wps->model_description)
207                 xml_add_tagged_data(buf, "modelDescription",
208                                     sm->wps->model_description);
209
210         s = sm->wps->dev.model_name;
211         s = ((s && *s) ? s : "");
212         xml_add_tagged_data(buf, "modelName", s);
213
214         if (sm->wps->dev.model_number)
215                 xml_add_tagged_data(buf, "modelNumber",
216                                     sm->wps->dev.model_number);
217
218         if (sm->wps->model_url)
219                 xml_add_tagged_data(buf, "modelURL", sm->wps->model_url);
220
221         if (sm->wps->dev.serial_number)
222                 xml_add_tagged_data(buf, "serialNumber",
223                                     sm->wps->dev.serial_number);
224
225         uuid_bin2str(sm->wps->uuid, uuid_string, sizeof(uuid_string));
226         s = uuid_string;
227         /* Need "uuid:" prefix, thus we can't use xml_add_tagged_data()
228          * easily...
229          */
230         wpabuf_put_str(buf, "<UDN>uuid:");
231         xml_data_encode(buf, s, os_strlen(s));
232         wpabuf_put_str(buf, "</UDN>\n");
233
234         if (sm->wps->upc)
235                 xml_add_tagged_data(buf, "UPC", sm->wps->upc);
236
237         wpabuf_put_str(buf, wps_device_xml_postfix);
238 }
239
240
241 static void http_put_reply_code(struct wpabuf *buf, enum http_reply_code code)
242 {
243         wpabuf_put_str(buf, "HTTP/1.1 ");
244         switch (code) {
245         case HTTP_OK:
246                 wpabuf_put_str(buf, "200 OK\r\n");
247                 break;
248         case HTTP_BAD_REQUEST:
249                 wpabuf_put_str(buf, "400 Bad request\r\n");
250                 break;
251         case HTTP_PRECONDITION_FAILED:
252                 wpabuf_put_str(buf, "412 Precondition failed\r\n");
253                 break;
254         case HTTP_UNIMPLEMENTED:
255                 wpabuf_put_str(buf, "501 Unimplemented\r\n");
256                 break;
257         case HTTP_INTERNAL_SERVER_ERROR:
258         default:
259                 wpabuf_put_str(buf, "500 Internal server error\r\n");
260                 break;
261         }
262 }
263
264
265 static void http_put_date(struct wpabuf *buf)
266 {
267         wpabuf_put_str(buf, "Date: ");
268         format_date(buf);
269         wpabuf_put_str(buf, "\r\n");
270 }
271
272
273 static void http_put_empty(struct wpabuf *buf, enum http_reply_code code)
274 {
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"
279                        "\r\n");
280 }
281
282
283 /* Given that we have received a header w/ GET, act upon it
284  *
285  * Format of GET (case-insensitive):
286  *
287  * First line must be:
288  *      GET /<file> HTTP/1.1
289  * Since we don't do anything fancy we just ignore other lines.
290  *
291  * Our response (if no error) which includes only required lines is:
292  * HTTP/1.1 200 OK
293  * Connection: close
294  * Content-Type: text/xml
295  * Date: <rfc1123-date>
296  *
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!).
300  */
301 static void web_connection_parse_get(struct upnp_wps_device_sm *sm,
302                                      struct http_request *hreq, char *filename)
303 {
304         struct wpabuf *buf; /* output buffer, allocated */
305         char *put_length_here;
306         char *body_start;
307         enum {
308                 GET_DEVICE_XML_FILE,
309                 GET_SCPD_XML_FILE
310         } req;
311         size_t extra_len = 0;
312         int body_length;
313         char len_buf[10];
314
315         /*
316          * It is not required that filenames be case insensitive but it is
317          * allowed and cannot hurt here.
318          */
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;
324                 extra_len = 3000;
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);
333                 if (sm->wps->upc)
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);
339         } else {
340                 /* File not found */
341                 wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP GET file not found: %s",
342                            filename);
343                 buf = wpabuf_alloc(200);
344                 if (buf == NULL) {
345                         http_request_deinit(hreq);
346                         return;
347                 }
348                 wpabuf_put_str(buf,
349                                "HTTP/1.1 404 Not Found\r\n"
350                                "Connection: close\r\n");
351
352                 http_put_date(buf);
353
354                 /* terminating empty line */
355                 wpabuf_put_str(buf, "\r\n");
356
357                 goto send_buf;
358         }
359
360         buf = wpabuf_alloc(1000 + extra_len);
361         if (buf == NULL) {
362                 http_request_deinit(hreq);
363                 return;
364         }
365
366         wpabuf_put_str(buf,
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: ");
372         /*
373          * We will paste the length in later, leaving some extra whitespace.
374          * HTTP code is supposed to be tolerant of extra whitespace.
375          */
376         put_length_here = wpabuf_put(buf, 0);
377         wpabuf_put_str(buf, "        \r\n");
378
379         http_put_date(buf);
380
381         /* terminating empty line */
382         wpabuf_put_str(buf, "\r\n");
383
384         body_start = wpabuf_put(buf, 0);
385
386         switch (req) {
387         case GET_DEVICE_XML_FILE:
388                 format_wps_device_xml(sm, buf);
389                 break;
390         case GET_SCPD_XML_FILE:
391                 wpabuf_put_str(buf, wps_scpd_xml);
392                 break;
393         }
394
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));
399
400 send_buf:
401         http_request_send_and_deinit(hreq, buf);
402 }
403
404
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)
408 {
409         static const char *name = "NewDeviceInfo";
410         struct wps_config cfg;
411         struct upnp_wps_peer *peer = &sm->peer;
412
413         wpa_printf(MSG_DEBUG, "WPS UPnP: GetDeviceInfo");
414
415         if (sm->ctx->ap_pin == NULL)
416                 return HTTP_INTERNAL_SERVER_ERROR;
417
418         /*
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
423          * registration.
424          */
425
426         if (peer->wps)
427                 wps_deinit(peer->wps);
428
429         os_memset(&cfg, 0, sizeof(cfg));
430         cfg.wps = sm->wps;
431         cfg.pin = (u8 *) sm->ctx->ap_pin;
432         cfg.pin_len = os_strlen(sm->ctx->ap_pin);
433         peer->wps = wps_init(&cfg);
434         if (peer->wps) {
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);
439                         peer->wps = NULL;
440                 }
441         } else
442                 *reply = NULL;
443         if (*reply == NULL) {
444                 wpa_printf(MSG_INFO, "WPS UPnP: Failed to get DeviceInfo");
445                 return HTTP_INTERNAL_SERVER_ERROR;
446         }
447         *replyname = name;
448         return HTTP_OK;
449 }
450
451
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)
455 {
456         struct wpabuf *msg;
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;
461
462         /*
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.
466          */
467         wpa_printf(MSG_DEBUG, "WPS UPnP: PutMessage");
468         msg = xml_get_base64_item(data, "NewInMessage", &ret);
469         if (msg == NULL)
470                 return ret;
471         res = wps_process_msg(sm->peer.wps, WSC_UPnP, msg);
472         if (res == WPS_FAILURE)
473                 *reply = NULL;
474         else
475                 *reply = wps_get_msg(sm->peer.wps, &op_code);
476         wpabuf_free(msg);
477         if (*reply == NULL)
478                 return HTTP_INTERNAL_SERVER_ERROR;
479         *replyname = name;
480         return HTTP_OK;
481 }
482
483
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)
487 {
488         struct wpabuf *msg;
489         enum http_reply_code ret;
490         u8 macaddr[ETH_ALEN];
491         int ev_type;
492         int type;
493         char *val;
494
495         /*
496          * External UPnP-based Registrar is passing us a message to be proxied
497          * over to a Wi-Fi -based client of ours.
498          */
499
500         wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse");
501         msg = xml_get_base64_item(data, "NewMessage", &ret);
502         if (msg == NULL) {
503                 wpa_printf(MSG_DEBUG, "WPS UPnP: Could not extract NewMessage "
504                            "from PutWLANResponse");
505                 return ret;
506         }
507         val = xml_get_first_item(data, "NewWLANEventType");
508         if (val == NULL) {
509                 wpa_printf(MSG_DEBUG, "WPS UPnP: No NewWLANEventType in "
510                            "PutWLANResponse");
511                 wpabuf_free(msg);
512                 return UPNP_ARG_VALUE_INVALID;
513         }
514         ev_type = atol(val);
515         os_free(val);
516         val = xml_get_first_item(data, "NewWLANEventMAC");
517         if (val == NULL) {
518                 wpa_printf(MSG_DEBUG, "WPS UPnP: No NewWLANEventMAC in "
519                            "PutWLANResponse");
520                 wpabuf_free(msg);
521                 return UPNP_ARG_VALUE_INVALID;
522         }
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
527                 {
528                         struct wps_parse_attr attr;
529                         if (wps_parse_msg(msg, &attr) < 0 || attr.version2) {
530                                 wpabuf_free(msg);
531                                 os_free(val);
532                                 return UPNP_ARG_VALUE_INVALID;
533                         }
534                 }
535 #endif /* CONFIG_WPS_STRICT */
536                 if (hwaddr_aton2(val, macaddr) > 0) {
537                         /*
538                          * At least some versions of Intel PROset seem to be
539                          * using dot-deliminated MAC address format here.
540                          */
541                         wpa_printf(MSG_DEBUG, "WPS UPnP: Workaround - allow "
542                                    "incorrect MAC address format in "
543                                    "NewWLANEventMAC: %s -> " MACSTR,
544                                    val, MAC2STR(macaddr));
545                 } else {
546                         wpabuf_free(msg);
547                         os_free(val);
548                         return UPNP_ARG_VALUE_INVALID;
549                 }
550         }
551         os_free(val);
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)
556                         type = -1;
557                 else
558                         type = *attr.msg_type;
559                 wpa_printf(MSG_DEBUG, "WPS UPnP: Message Type %d", type);
560         } else
561                 type = -1;
562         if (!sm->ctx->rx_req_put_wlan_response ||
563             sm->ctx->rx_req_put_wlan_response(sm->priv, ev_type, macaddr, msg,
564                                               type)) {
565                 wpa_printf(MSG_INFO, "WPS UPnP: Fail: sm->ctx->"
566                            "rx_req_put_wlan_response");
567                 wpabuf_free(msg);
568                 return HTTP_INTERNAL_SERVER_ERROR;
569         }
570         wpabuf_free(msg);
571         *replyname = NULL;
572         *reply = NULL;
573         return HTTP_OK;
574 }
575
576
577 static int find_er_addr(struct subscription *s, struct sockaddr_in *cli)
578 {
579         struct subscr_addr *a;
580
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)
583                         return 1;
584         }
585         return 0;
586 }
587
588
589 static struct subscription * find_er(struct upnp_wps_device_sm *sm,
590                                      struct sockaddr_in *cli)
591 {
592         struct subscription *s;
593         dl_list_for_each(s, &sm->subscriptions, struct subscription, list)
594                 if (find_er_addr(s, cli))
595                         return s;
596         return NULL;
597 }
598
599
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)
605 {
606         struct wpabuf *msg;
607         enum http_reply_code ret;
608         struct subscription *s;
609
610         wpa_printf(MSG_DEBUG, "WPS UPnP: SetSelectedRegistrar");
611         s = find_er(sm, cli);
612         if (s == NULL) {
613                 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignore SetSelectedRegistrar "
614                            "from unknown ER");
615                 return UPNP_ACTION_FAILED;
616         }
617         msg = xml_get_base64_item(data, "NewMessage", &ret);
618         if (msg == NULL)
619                 return ret;
620         if (upnp_er_set_selected_registrar(sm->wps->registrar, s, msg)) {
621                 wpabuf_free(msg);
622                 return HTTP_INTERNAL_SERVER_ERROR;
623         }
624         wpabuf_free(msg);
625         *replyname = NULL;
626         *reply = NULL;
627         return HTTP_OK;
628 }
629
630
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"
635         "<s:Body>\n";
636 static const char *soap_postfix =
637         "</s:Body>\n</s:Envelope>\n";
638
639 static const char *soap_error_prefix =
640         "<s:Fault>\n"
641         "<faultcode>s:Client</faultcode>\n"
642         "<faultstring>UPnPError</faultstring>\n"
643         "<detail>\n"
644         "<UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\">\n";
645 static const char *soap_error_postfix =
646         "<errorDescription>Error</errorDescription>\n"
647         "</UPnPError>\n"
648         "</detail>\n"
649         "</s:Fault>\n";
650
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)
656 {
657         struct wpabuf *buf;
658         char *replydata;
659         char *put_length_here = NULL;
660         char *body_start = NULL;
661
662         if (reply) {
663                 size_t len;
664                 replydata = (char *) base64_encode(wpabuf_head(reply),
665                                                    wpabuf_len(reply), &len);
666         } else
667                 replydata = NULL;
668
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
673          */
674         buf = wpabuf_alloc(1000 + (replydata ? os_strlen(replydata) : 0U) +
675                            (action_len > 0 ? action_len * 2 : 0));
676         if (buf == NULL) {
677                 wpa_printf(MSG_INFO, "WPS UPnP: Cannot allocate reply to "
678                            "POST");
679                 os_free(replydata);
680                 http_request_deinit(req);
681                 return;
682         }
683
684         /*
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.
688          */
689         if (ret == HTTP_OK) {
690                 wpabuf_put_str(buf,
691                                "HTTP/1.1 200 OK\r\n"
692                                "Content-Type: text/xml; "
693                                "charset=\"utf-8\"\r\n");
694         } else {
695                 wpabuf_printf(buf, "HTTP/1.1 %d Error\r\n", ret);
696         }
697         wpabuf_put_str(buf, http_connection_close);
698
699         wpabuf_put_str(buf, "Content-Length: ");
700         /*
701          * We will paste the length in later, leaving some extra whitespace.
702          * HTTP code is supposed to be tolerant of extra whitespace.
703          */
704         put_length_here = wpabuf_put(buf, 0);
705         wpabuf_put_str(buf, "        \r\n");
706
707         http_put_date(buf);
708
709         /* terminating empty line */
710         wpabuf_put_str(buf, "\r\n");
711
712         body_start = wpabuf_put(buf, 0);
713
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
723                          * data? ...
724                          * probably not, unlikely to have ampersand(&) or left
725                          * angle bracket (<) in it...
726                          */
727                         wpabuf_printf(buf, "<%s>", replyname);
728                         wpabuf_put_str(buf, replydata);
729                         wpabuf_printf(buf, "</%s>\n", replyname);
730                 }
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);
735         } else {
736                 /* Error case */
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);
742         }
743         os_free(replydata);
744
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;
748                 char len_buf[10];
749                 os_snprintf(len_buf, sizeof(len_buf), "%d", body_length);
750                 os_memcpy(put_length_here, len_buf, os_strlen(len_buf));
751         }
752
753         http_request_send_and_deinit(req, buf);
754 }
755
756
757 static const char * web_get_action(struct http_request *req,
758                                    size_t *action_len)
759 {
760         const char *match;
761         int match_len;
762         char *b;
763         char *action;
764
765         *action_len = 0;
766         /* The SOAPAction line of the header tells us what we want to do */
767         b = http_request_get_hdr_line(req, "SOAPAction:");
768         if (b == NULL)
769                 return NULL;
770         if (*b == '"')
771                 b++;
772         else
773                 return NULL;
774         match = urn_wfawlanconfig;
775         match_len = os_strlen(urn_wfawlanconfig) - 1;
776         if (os_strncasecmp(b, match, match_len))
777                 return NULL;
778         b += match_len;
779         /* skip over version */
780         while (isgraph(*b) && *b != '#')
781                 b++;
782         if (*b != '#')
783                 return NULL;
784         b++;
785         /* Following the sharp(#) should be the action and a double quote */
786         action = b;
787         while (isgraph(*b) && *b != '"')
788                 b++;
789         if (*b != '"')
790                 return NULL;
791         *action_len = b - action;
792         return action;
793 }
794
795
796 /* Given that we have received a header w/ POST, act upon it
797  *
798  * Format of POST (case-insensitive):
799  *
800  * First line must be:
801  *      POST /<file> HTTP/1.1
802  * Since we don't do anything fancy we just ignore other lines.
803  *
804  * Our response (if no error) which includes only required lines is:
805  * HTTP/1.1 200 OK
806  * Connection: close
807  * Content-Type: text/xml
808  * Date: <rfc1123-date>
809  *
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!).
813  */
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)
818 {
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 */
825
826         if (os_strcasecmp(filename, UPNP_WPS_DEVICE_CONTROL_FILE)) {
827                 wpa_printf(MSG_INFO, "WPS UPnP: Invalid POST filename %s",
828                            filename);
829                 ret = HTTP_NOT_FOUND;
830                 goto bad;
831         }
832
833         ret = UPNP_INVALID_ACTION;
834         action = web_get_action(req, &action_len);
835         if (action == NULL)
836                 goto bad;
837
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,
844                                                     &replyname);
845         else if (!os_strncasecmp("SetSelectedRegistrar", action, action_len))
846                 ret = web_process_set_selected_registrar(sm, cli, data, &reply,
847                                                          &replyname);
848         else
849                 wpa_printf(MSG_INFO, "WPS UPnP: Unknown POST type");
850
851 bad:
852         if (ret != HTTP_OK)
853                 wpa_printf(MSG_INFO, "WPS UPnP: POST failure ret=%d", ret);
854         web_connection_send_reply(req, ret, action, action_len, reply,
855                                   replyname);
856         wpabuf_free(reply);
857 }
858
859
860 /* Given that we have received a header w/ SUBSCRIBE, act upon it
861  *
862  * Format of SUBSCRIBE (case-insensitive):
863  *
864  * First line must be:
865  *      SUBSCRIBE /wps_event HTTP/1.1
866  *
867  * Our response (if no error) which includes only required lines is:
868  * HTTP/1.1 200 OK
869  * Server: xx, UPnP/1.0, xx
870  * SID: uuid:xxxxxxxxx
871  * Timeout: Second-<n>
872  * Content-Length: 0
873  * Date: xxxx
874  *
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!).
878  */
879 static void web_connection_parse_subscribe(struct upnp_wps_device_sm *sm,
880                                            struct http_request *req,
881                                            const char *filename)
882 {
883         struct wpabuf *buf;
884         char *b;
885         char *hdr = http_request_get_hdr(req);
886         char *h;
887         char *match;
888         int match_len;
889         char *end;
890         int len;
891         int got_nt = 0;
892         u8 uuid[UUID_LEN];
893         int got_uuid = 0;
894         char *callback_urls = NULL;
895         struct subscription *s = NULL;
896         enum http_reply_code ret = HTTP_INTERNAL_SERVER_ERROR;
897
898         buf = wpabuf_alloc(1000);
899         if (buf == NULL) {
900                 http_request_deinit(req);
901                 return;
902         }
903
904         /* Parse/validate headers */
905         h = hdr;
906         /* First line: SUBSCRIBE /wps_event HTTP/1.1
907          * has already been parsed.
908          */
909         if (os_strcasecmp(filename, UPNP_WPS_DEVICE_EVENT_FILE) != 0) {
910                 ret = HTTP_PRECONDITION_FAILED;
911                 goto error;
912         }
913         wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP SUBSCRIBE for event");
914         end = os_strchr(h, '\n');
915
916         for (; end != NULL; h = end + 1) {
917                 /* Option line by option line */
918                 h = end + 1;
919                 end = os_strchr(h, '\n');
920                 if (end == NULL)
921                         break; /* no unterminated lines allowed */
922
923                 /* NT assures that it is our type of subscription;
924                  * not used for a renewl.
925                  **/
926                 match = "NT:";
927                 match_len = os_strlen(match);
928                 if (os_strncasecmp(h, match, match_len) == 0) {
929                         h += match_len;
930                         while (*h == ' ' || *h == '\t')
931                                 h++;
932                         match = "upnp:event";
933                         match_len = os_strlen(match);
934                         if (os_strncasecmp(h, match, match_len) != 0) {
935                                 ret = HTTP_BAD_REQUEST;
936                                 goto error;
937                         }
938                         got_nt = 1;
939                         continue;
940                 }
941                 /* HOST should refer to us */
942 #if 0
943                 match = "HOST:";
944                 match_len = os_strlen(match);
945                 if (os_strncasecmp(h, match, match_len) == 0) {
946                         h += match_len;
947                         while (*h == ' ' || *h == '\t')
948                                 h++;
949                         .....
950                 }
951 #endif
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.
955                  */
956                 match = "CALLBACK:";
957                 match_len = os_strlen(match);
958                 if (os_strncasecmp(h, match, match_len) == 0) {
959                         h += match_len;
960                         while (*h == ' ' || *h == '\t')
961                                 h++;
962                         len = end - h;
963                         os_free(callback_urls);
964                         callback_urls = os_malloc(len + 1);
965                         if (callback_urls == NULL) {
966                                 ret = HTTP_INTERNAL_SERVER_ERROR;
967                                 goto error;
968                         }
969                         os_memcpy(callback_urls, h, len);
970                         callback_urls[len] = 0;
971                         continue;
972                 }
973                 /* SID is only for renewal */
974                 match = "SID:";
975                 match_len = os_strlen(match);
976                 if (os_strncasecmp(h, match, match_len) == 0) {
977                         h += match_len;
978                         while (*h == ' ' || *h == '\t')
979                                 h++;
980                         match = "uuid:";
981                         match_len = os_strlen(match);
982                         if (os_strncasecmp(h, match, match_len) != 0) {
983                                 ret = HTTP_BAD_REQUEST;
984                                 goto error;
985                         }
986                         h += match_len;
987                         while (*h == ' ' || *h == '\t')
988                                 h++;
989                         if (uuid_str2bin(h, uuid)) {
990                                 ret = HTTP_BAD_REQUEST;
991                                 goto error;
992                         }
993                         got_uuid = 1;
994                         continue;
995                 }
996                 /* TIMEOUT is requested timeout, but apparently we can
997                  * just ignore this.
998                  */
999         }
1000
1001         if (got_uuid) {
1002                 /* renewal */
1003                 if (callback_urls) {
1004                         ret = HTTP_BAD_REQUEST;
1005                         goto error;
1006                 }
1007                 s = subscription_renew(sm, uuid);
1008                 if (s == NULL) {
1009                         ret = HTTP_PRECONDITION_FAILED;
1010                         goto error;
1011                 }
1012         } else if (callback_urls) {
1013                 if (!got_nt) {
1014                         ret = HTTP_PRECONDITION_FAILED;
1015                         goto error;
1016                 }
1017                 s = subscription_start(sm, callback_urls);
1018                 if (s == NULL) {
1019                         ret = HTTP_INTERNAL_SERVER_ERROR;
1020                         goto error;
1021                 }
1022         } else {
1023                 ret = HTTP_PRECONDITION_FAILED;
1024                 goto error;
1025         }
1026
1027         /* success */
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);
1039         http_put_date(buf);
1040         /* And empty line to terminate header: */
1041         wpabuf_put_str(buf, "\r\n");
1042
1043         os_free(callback_urls);
1044         http_request_send_and_deinit(req, buf);
1045         return;
1046
1047 error:
1048         /* Per UPnP spec:
1049         * Errors
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
1053         *     400 Bad Request.
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.
1058         * Invalid NT
1059         *   412 Precondition Failed. If NT header does not equal upnp:event,
1060         *     the publisher must respond with HTTP error 412 Precondition
1061         *     Failed.
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)
1068         */
1069         http_put_empty(buf, ret);
1070         http_request_send_and_deinit(req, buf);
1071         os_free(callback_urls);
1072 }
1073
1074
1075 /* Given that we have received a header w/ UNSUBSCRIBE, act upon it
1076  *
1077  * Format of UNSUBSCRIBE (case-insensitive):
1078  *
1079  * First line must be:
1080  *      UNSUBSCRIBE /wps_event HTTP/1.1
1081  *
1082  * Our response (if no error) which includes only required lines is:
1083  * HTTP/1.1 200 OK
1084  * Content-Length: 0
1085  *
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!).
1089  */
1090 static void web_connection_parse_unsubscribe(struct upnp_wps_device_sm *sm,
1091                                              struct http_request *req,
1092                                              const char *filename)
1093 {
1094         struct wpabuf *buf;
1095         char *hdr = http_request_get_hdr(req);
1096         char *h;
1097         char *match;
1098         int match_len;
1099         char *end;
1100         u8 uuid[UUID_LEN];
1101         int got_uuid = 0;
1102         struct subscription *s = NULL;
1103         enum http_reply_code ret = HTTP_INTERNAL_SERVER_ERROR;
1104
1105         /* Parse/validate headers */
1106         h = hdr;
1107         /* First line: UNSUBSCRIBE /wps_event HTTP/1.1
1108          * has already been parsed.
1109          */
1110         if (os_strcasecmp(filename, UPNP_WPS_DEVICE_EVENT_FILE) != 0) {
1111                 ret = HTTP_PRECONDITION_FAILED;
1112                 goto send_msg;
1113         }
1114         wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP UNSUBSCRIBE for event");
1115         end = os_strchr(h, '\n');
1116
1117         for (; end != NULL; h = end + 1) {
1118                 /* Option line by option line */
1119                 h = end + 1;
1120                 end = os_strchr(h, '\n');
1121                 if (end == NULL)
1122                         break; /* no unterminated lines allowed */
1123
1124                 /* HOST should refer to us */
1125 #if 0
1126                 match = "HOST:";
1127                 match_len = os_strlen(match);
1128                 if (os_strncasecmp(h, match, match_len) == 0) {
1129                         h += match_len;
1130                         while (*h == ' ' || *h == '\t')
1131                                 h++;
1132                         .....
1133                 }
1134 #endif
1135                 /* SID is only for renewal */
1136                 match = "SID:";
1137                 match_len = os_strlen(match);
1138                 if (os_strncasecmp(h, match, match_len) == 0) {
1139                         h += match_len;
1140                         while (*h == ' ' || *h == '\t')
1141                                 h++;
1142                         match = "uuid:";
1143                         match_len = os_strlen(match);
1144                         if (os_strncasecmp(h, match, match_len) != 0) {
1145                                 ret = HTTP_BAD_REQUEST;
1146                                 goto send_msg;
1147                         }
1148                         h += match_len;
1149                         while (*h == ' ' || *h == '\t')
1150                                 h++;
1151                         if (uuid_str2bin(h, uuid)) {
1152                                 ret = HTTP_BAD_REQUEST;
1153                                 goto send_msg;
1154                         }
1155                         got_uuid = 1;
1156                         continue;
1157                 }
1158         }
1159
1160         if (got_uuid) {
1161                 s = subscription_find(sm, uuid);
1162                 if (s) {
1163                         struct subscr_addr *sa;
1164                         sa = dl_list_first(&s->addr_list, struct subscr_addr,
1165                                            list);
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);
1171                 }
1172         } else {
1173                 wpa_printf(MSG_INFO, "WPS UPnP: Unsubscribe fails (not "
1174                            "found)");
1175                 ret = HTTP_PRECONDITION_FAILED;
1176                 goto send_msg;
1177         }
1178
1179         ret = HTTP_OK;
1180
1181 send_msg:
1182         buf = wpabuf_alloc(200);
1183         if (buf == NULL) {
1184                 http_request_deinit(req);
1185                 return;
1186         }
1187         http_put_empty(buf, ret);
1188         http_request_send_and_deinit(req, buf);
1189 }
1190
1191
1192 /* Send error in response to unknown requests */
1193 static void web_connection_unimplemented(struct http_request *req)
1194 {
1195         struct wpabuf *buf;
1196         buf = wpabuf_alloc(200);
1197         if (buf == NULL) {
1198                 http_request_deinit(req);
1199                 return;
1200         }
1201         http_put_empty(buf, HTTP_UNIMPLEMENTED);
1202         http_request_send_and_deinit(req, buf);
1203 }
1204
1205
1206
1207 /* Called when we have gotten an apparently valid http request.
1208  */
1209 static void web_connection_check_data(void *ctx, struct http_request *req)
1210 {
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);
1215
1216         if (!filename) {
1217                 wpa_printf(MSG_INFO, "WPS UPnP: Could not get HTTP URI");
1218                 http_request_deinit(req);
1219                 return;
1220         }
1221         /* Trim leading slashes from filename */
1222         while (*filename == '/')
1223                 filename++;
1224
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));
1227
1228         switch (htype) {
1229         case HTTPREAD_HDR_TYPE_GET:
1230                 web_connection_parse_get(sm, req, filename);
1231                 break;
1232         case HTTPREAD_HDR_TYPE_POST:
1233                 web_connection_parse_post(sm, cli, req, filename);
1234                 break;
1235         case HTTPREAD_HDR_TYPE_SUBSCRIBE:
1236                 web_connection_parse_subscribe(sm, req, filename);
1237                 break;
1238         case HTTPREAD_HDR_TYPE_UNSUBSCRIBE:
1239                 web_connection_parse_unsubscribe(sm, req, filename);
1240                 break;
1241
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.
1246                  */
1247         default:
1248                 /* Send 501 for anything else */
1249                 web_connection_unimplemented(req);
1250                 break;
1251         }
1252 }
1253
1254
1255 /*
1256  * Listening for web connections
1257  * We have a single TCP listening port, and hand off connections as we get
1258  * them.
1259  */
1260
1261 void web_listener_stop(struct upnp_wps_device_sm *sm)
1262 {
1263         http_server_deinit(sm->web_srv);
1264         sm->web_srv = NULL;
1265 }
1266
1267
1268 int web_listener_start(struct upnp_wps_device_sm *sm)
1269 {
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,
1273                                        sm);
1274         if (sm->web_srv == NULL) {
1275                 web_listener_stop(sm);
1276                 return -1;
1277         }
1278         sm->web_port = http_server_get_port(sm->web_srv);
1279
1280         return 0;
1281 }