wps_upnp_web.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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. #include "includes.h"
  11. #include "common.h"
  12. #include "base64.h"
  13. #include "uuid.h"
  14. #include "httpread.h"
  15. #include "http_server.h"
  16. #include "wps_i.h"
  17. #include "wps_upnp.h"
  18. #include "wps_upnp_i.h"
  19. #include "upnp_xml.h"
  20. /***************************************************************************
  21. * Web connections (we serve pages of info about ourselves, handle
  22. * requests, etc. etc.).
  23. **************************************************************************/
  24. #define WEB_CONNECTION_TIMEOUT_SEC 30 /* Drop web connection after t.o. */
  25. #define WEB_CONNECTION_MAX_READ 8000 /* Max we'll read for TCP request */
  26. #define MAX_WEB_CONNECTIONS 10 /* max simultaneous web connects */
  27. static const char *urn_wfawlanconfig =
  28. "urn:schemas-wifialliance-org:service:WFAWLANConfig:1";
  29. static const char *http_server_hdr =
  30. "Server: unspecified, UPnP/1.0, unspecified\r\n";
  31. static const char *http_connection_close =
  32. "Connection: close\r\n";
  33. /*
  34. * "Files" that we serve via HTTP. The format of these files is given by
  35. * WFA WPS specifications. Extra white space has been removed to save space.
  36. */
  37. static const char wps_scpd_xml[] =
  38. "<?xml version=\"1.0\"?>\n"
  39. "<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\">\n"
  40. "<specVersion><major>1</major><minor>0</minor></specVersion>\n"
  41. "<actionList>\n"
  42. "<action>\n"
  43. "<name>GetDeviceInfo</name>\n"
  44. "<argumentList>\n"
  45. "<argument>\n"
  46. "<name>NewDeviceInfo</name>\n"
  47. "<direction>out</direction>\n"
  48. "<relatedStateVariable>DeviceInfo</relatedStateVariable>\n"
  49. "</argument>\n"
  50. "</argumentList>\n"
  51. "</action>\n"
  52. "<action>\n"
  53. "<name>PutMessage</name>\n"
  54. "<argumentList>\n"
  55. "<argument>\n"
  56. "<name>NewInMessage</name>\n"
  57. "<direction>in</direction>\n"
  58. "<relatedStateVariable>InMessage</relatedStateVariable>\n"
  59. "</argument>\n"
  60. "<argument>\n"
  61. "<name>NewOutMessage</name>\n"
  62. "<direction>out</direction>\n"
  63. "<relatedStateVariable>OutMessage</relatedStateVariable>\n"
  64. "</argument>\n"
  65. "</argumentList>\n"
  66. "</action>\n"
  67. "<action>\n"
  68. "<name>PutWLANResponse</name>\n"
  69. "<argumentList>\n"
  70. "<argument>\n"
  71. "<name>NewMessage</name>\n"
  72. "<direction>in</direction>\n"
  73. "<relatedStateVariable>Message</relatedStateVariable>\n"
  74. "</argument>\n"
  75. "<argument>\n"
  76. "<name>NewWLANEventType</name>\n"
  77. "<direction>in</direction>\n"
  78. "<relatedStateVariable>WLANEventType</relatedStateVariable>\n"
  79. "</argument>\n"
  80. "<argument>\n"
  81. "<name>NewWLANEventMAC</name>\n"
  82. "<direction>in</direction>\n"
  83. "<relatedStateVariable>WLANEventMAC</relatedStateVariable>\n"
  84. "</argument>\n"
  85. "</argumentList>\n"
  86. "</action>\n"
  87. "<action>\n"
  88. "<name>SetSelectedRegistrar</name>\n"
  89. "<argumentList>\n"
  90. "<argument>\n"
  91. "<name>NewMessage</name>\n"
  92. "<direction>in</direction>\n"
  93. "<relatedStateVariable>Message</relatedStateVariable>\n"
  94. "</argument>\n"
  95. "</argumentList>\n"
  96. "</action>\n"
  97. "</actionList>\n"
  98. "<serviceStateTable>\n"
  99. "<stateVariable sendEvents=\"no\">\n"
  100. "<name>Message</name>\n"
  101. "<dataType>bin.base64</dataType>\n"
  102. "</stateVariable>\n"
  103. "<stateVariable sendEvents=\"no\">\n"
  104. "<name>InMessage</name>\n"
  105. "<dataType>bin.base64</dataType>\n"
  106. "</stateVariable>\n"
  107. "<stateVariable sendEvents=\"no\">\n"
  108. "<name>OutMessage</name>\n"
  109. "<dataType>bin.base64</dataType>\n"
  110. "</stateVariable>\n"
  111. "<stateVariable sendEvents=\"no\">\n"
  112. "<name>DeviceInfo</name>\n"
  113. "<dataType>bin.base64</dataType>\n"
  114. "</stateVariable>\n"
  115. "<stateVariable sendEvents=\"yes\">\n"
  116. "<name>APStatus</name>\n"
  117. "<dataType>ui1</dataType>\n"
  118. "</stateVariable>\n"
  119. "<stateVariable sendEvents=\"yes\">\n"
  120. "<name>STAStatus</name>\n"
  121. "<dataType>ui1</dataType>\n"
  122. "</stateVariable>\n"
  123. "<stateVariable sendEvents=\"yes\">\n"
  124. "<name>WLANEvent</name>\n"
  125. "<dataType>bin.base64</dataType>\n"
  126. "</stateVariable>\n"
  127. "<stateVariable sendEvents=\"no\">\n"
  128. "<name>WLANEventType</name>\n"
  129. "<dataType>ui1</dataType>\n"
  130. "</stateVariable>\n"
  131. "<stateVariable sendEvents=\"no\">\n"
  132. "<name>WLANEventMAC</name>\n"
  133. "<dataType>string</dataType>\n"
  134. "</stateVariable>\n"
  135. "<stateVariable sendEvents=\"no\">\n"
  136. "<name>WLANResponse</name>\n"
  137. "<dataType>bin.base64</dataType>\n"
  138. "</stateVariable>\n"
  139. "</serviceStateTable>\n"
  140. "</scpd>\n"
  141. ;
  142. static const char *wps_device_xml_prefix =
  143. "<?xml version=\"1.0\"?>\n"
  144. "<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\n"
  145. "<specVersion>\n"
  146. "<major>1</major>\n"
  147. "<minor>0</minor>\n"
  148. "</specVersion>\n"
  149. "<device>\n"
  150. "<deviceType>urn:schemas-wifialliance-org:device:WFADevice:1"
  151. "</deviceType>\n";
  152. static const char *wps_device_xml_postfix =
  153. "<serviceList>\n"
  154. "<service>\n"
  155. "<serviceType>urn:schemas-wifialliance-org:service:WFAWLANConfig:1"
  156. "</serviceType>\n"
  157. "<serviceId>urn:wifialliance-org:serviceId:WFAWLANConfig1</serviceId>"
  158. "\n"
  159. "<SCPDURL>" UPNP_WPS_SCPD_XML_FILE "</SCPDURL>\n"
  160. "<controlURL>" UPNP_WPS_DEVICE_CONTROL_FILE "</controlURL>\n"
  161. "<eventSubURL>" UPNP_WPS_DEVICE_EVENT_FILE "</eventSubURL>\n"
  162. "</service>\n"
  163. "</serviceList>\n"
  164. "</device>\n"
  165. "</root>\n";
  166. /* format_wps_device_xml -- produce content of "file" wps_device.xml
  167. * (UPNP_WPS_DEVICE_XML_FILE)
  168. */
  169. static void format_wps_device_xml(struct upnp_wps_device_sm *sm,
  170. struct wpabuf *buf)
  171. {
  172. const char *s;
  173. char uuid_string[80];
  174. wpabuf_put_str(buf, wps_device_xml_prefix);
  175. /*
  176. * Add required fields with default values if not configured. Add
  177. * optional and recommended fields only if configured.
  178. */
  179. s = sm->wps->friendly_name;
  180. s = ((s && *s) ? s : "WPS Access Point");
  181. xml_add_tagged_data(buf, "friendlyName", s);
  182. s = sm->wps->dev.manufacturer;
  183. s = ((s && *s) ? s : "");
  184. xml_add_tagged_data(buf, "manufacturer", s);
  185. if (sm->wps->manufacturer_url)
  186. xml_add_tagged_data(buf, "manufacturerURL",
  187. sm->wps->manufacturer_url);
  188. if (sm->wps->model_description)
  189. xml_add_tagged_data(buf, "modelDescription",
  190. sm->wps->model_description);
  191. s = sm->wps->dev.model_name;
  192. s = ((s && *s) ? s : "");
  193. xml_add_tagged_data(buf, "modelName", s);
  194. if (sm->wps->dev.model_number)
  195. xml_add_tagged_data(buf, "modelNumber",
  196. sm->wps->dev.model_number);
  197. if (sm->wps->model_url)
  198. xml_add_tagged_data(buf, "modelURL", sm->wps->model_url);
  199. if (sm->wps->dev.serial_number)
  200. xml_add_tagged_data(buf, "serialNumber",
  201. sm->wps->dev.serial_number);
  202. uuid_bin2str(sm->wps->uuid, uuid_string, sizeof(uuid_string));
  203. s = uuid_string;
  204. /* Need "uuid:" prefix, thus we can't use xml_add_tagged_data()
  205. * easily...
  206. */
  207. wpabuf_put_str(buf, "<UDN>uuid:");
  208. xml_data_encode(buf, s, os_strlen(s));
  209. wpabuf_put_str(buf, "</UDN>\n");
  210. if (sm->wps->upc)
  211. xml_add_tagged_data(buf, "UPC", sm->wps->upc);
  212. wpabuf_put_str(buf, wps_device_xml_postfix);
  213. }
  214. static void http_put_reply_code(struct wpabuf *buf, enum http_reply_code code)
  215. {
  216. wpabuf_put_str(buf, "HTTP/1.1 ");
  217. switch (code) {
  218. case HTTP_OK:
  219. wpabuf_put_str(buf, "200 OK\r\n");
  220. break;
  221. case HTTP_BAD_REQUEST:
  222. wpabuf_put_str(buf, "400 Bad request\r\n");
  223. break;
  224. case HTTP_PRECONDITION_FAILED:
  225. wpabuf_put_str(buf, "412 Precondition failed\r\n");
  226. break;
  227. case HTTP_UNIMPLEMENTED:
  228. wpabuf_put_str(buf, "501 Unimplemented\r\n");
  229. break;
  230. case HTTP_INTERNAL_SERVER_ERROR:
  231. default:
  232. wpabuf_put_str(buf, "500 Internal server error\r\n");
  233. break;
  234. }
  235. }
  236. static void http_put_date(struct wpabuf *buf)
  237. {
  238. wpabuf_put_str(buf, "Date: ");
  239. format_date(buf);
  240. wpabuf_put_str(buf, "\r\n");
  241. }
  242. static void http_put_empty(struct wpabuf *buf, enum http_reply_code code)
  243. {
  244. http_put_reply_code(buf, code);
  245. wpabuf_put_str(buf, http_server_hdr);
  246. wpabuf_put_str(buf, http_connection_close);
  247. wpabuf_put_str(buf, "Content-Length: 0\r\n"
  248. "\r\n");
  249. }
  250. /* Given that we have received a header w/ GET, act upon it
  251. *
  252. * Format of GET (case-insensitive):
  253. *
  254. * First line must be:
  255. * GET /<file> HTTP/1.1
  256. * Since we don't do anything fancy we just ignore other lines.
  257. *
  258. * Our response (if no error) which includes only required lines is:
  259. * HTTP/1.1 200 OK
  260. * Connection: close
  261. * Content-Type: text/xml
  262. * Date: <rfc1123-date>
  263. *
  264. * Header lines must end with \r\n
  265. * Per RFC 2616, content-length: is not required but connection:close
  266. * would appear to be required (given that we will be closing it!).
  267. */
  268. static void web_connection_parse_get(struct upnp_wps_device_sm *sm,
  269. struct http_request *hreq, char *filename)
  270. {
  271. struct wpabuf *buf; /* output buffer, allocated */
  272. char *put_length_here;
  273. char *body_start;
  274. enum {
  275. GET_DEVICE_XML_FILE,
  276. GET_SCPD_XML_FILE
  277. } req;
  278. size_t extra_len = 0;
  279. int body_length;
  280. char len_buf[10];
  281. /*
  282. * It is not required that filenames be case insensitive but it is
  283. * allowed and cannot hurt here.
  284. */
  285. if (filename == NULL)
  286. filename = "(null)"; /* just in case */
  287. if (os_strcasecmp(filename, UPNP_WPS_DEVICE_XML_FILE) == 0) {
  288. wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP GET for device XML");
  289. req = GET_DEVICE_XML_FILE;
  290. extra_len = 3000;
  291. if (sm->wps->friendly_name)
  292. extra_len += os_strlen(sm->wps->friendly_name);
  293. if (sm->wps->manufacturer_url)
  294. extra_len += os_strlen(sm->wps->manufacturer_url);
  295. if (sm->wps->model_description)
  296. extra_len += os_strlen(sm->wps->model_description);
  297. if (sm->wps->model_url)
  298. extra_len += os_strlen(sm->wps->model_url);
  299. if (sm->wps->upc)
  300. extra_len += os_strlen(sm->wps->upc);
  301. } else if (!os_strcasecmp(filename, UPNP_WPS_SCPD_XML_FILE)) {
  302. wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP GET for SCPD XML");
  303. req = GET_SCPD_XML_FILE;
  304. extra_len = os_strlen(wps_scpd_xml);
  305. } else {
  306. /* File not found */
  307. wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP GET file not found: %s",
  308. filename);
  309. buf = wpabuf_alloc(200);
  310. if (buf == NULL) {
  311. http_request_deinit(hreq);
  312. return;
  313. }
  314. wpabuf_put_str(buf,
  315. "HTTP/1.1 404 Not Found\r\n"
  316. "Connection: close\r\n");
  317. http_put_date(buf);
  318. /* terminating empty line */
  319. wpabuf_put_str(buf, "\r\n");
  320. goto send_buf;
  321. }
  322. buf = wpabuf_alloc(1000 + extra_len);
  323. if (buf == NULL) {
  324. http_request_deinit(hreq);
  325. return;
  326. }
  327. wpabuf_put_str(buf,
  328. "HTTP/1.1 200 OK\r\n"
  329. "Content-Type: text/xml; charset=\"utf-8\"\r\n");
  330. wpabuf_put_str(buf, "Server: Unspecified, UPnP/1.0, Unspecified\r\n");
  331. wpabuf_put_str(buf, "Connection: close\r\n");
  332. wpabuf_put_str(buf, "Content-Length: ");
  333. /*
  334. * We will paste the length in later, leaving some extra whitespace.
  335. * HTTP code is supposed to be tolerant of extra whitespace.
  336. */
  337. put_length_here = wpabuf_put(buf, 0);
  338. wpabuf_put_str(buf, " \r\n");
  339. http_put_date(buf);
  340. /* terminating empty line */
  341. wpabuf_put_str(buf, "\r\n");
  342. body_start = wpabuf_put(buf, 0);
  343. switch (req) {
  344. case GET_DEVICE_XML_FILE:
  345. format_wps_device_xml(sm, buf);
  346. break;
  347. case GET_SCPD_XML_FILE:
  348. wpabuf_put_str(buf, wps_scpd_xml);
  349. break;
  350. }
  351. /* Now patch in the content length at the end */
  352. body_length = (char *) wpabuf_put(buf, 0) - body_start;
  353. os_snprintf(len_buf, 10, "%d", body_length);
  354. os_memcpy(put_length_here, len_buf, os_strlen(len_buf));
  355. send_buf:
  356. http_request_send_and_deinit(hreq, buf);
  357. }
  358. static enum http_reply_code
  359. web_process_get_device_info(struct upnp_wps_device_sm *sm,
  360. struct wpabuf **reply, const char **replyname)
  361. {
  362. static const char *name = "NewDeviceInfo";
  363. wpa_printf(MSG_DEBUG, "WPS UPnP: GetDeviceInfo");
  364. if (sm->ctx->rx_req_get_device_info == NULL)
  365. return HTTP_INTERNAL_SERVER_ERROR;
  366. *reply = sm->ctx->rx_req_get_device_info(sm->priv, &sm->peer);
  367. if (*reply == NULL) {
  368. wpa_printf(MSG_INFO, "WPS UPnP: Failed to get DeviceInfo");
  369. return HTTP_INTERNAL_SERVER_ERROR;
  370. }
  371. *replyname = name;
  372. return HTTP_OK;
  373. }
  374. static enum http_reply_code
  375. web_process_put_message(struct upnp_wps_device_sm *sm, char *data,
  376. struct wpabuf **reply, const char **replyname)
  377. {
  378. struct wpabuf *msg;
  379. static const char *name = "NewOutMessage";
  380. enum http_reply_code ret;
  381. /*
  382. * PutMessage is used by external UPnP-based Registrar to perform WPS
  383. * operation with the access point itself; as compared with
  384. * PutWLANResponse which is for proxying.
  385. */
  386. wpa_printf(MSG_DEBUG, "WPS UPnP: PutMessage");
  387. if (sm->ctx->rx_req_put_message == NULL)
  388. return HTTP_INTERNAL_SERVER_ERROR;
  389. msg = xml_get_base64_item(data, "NewInMessage", &ret);
  390. if (msg == NULL)
  391. return ret;
  392. *reply = sm->ctx->rx_req_put_message(sm->priv, &sm->peer, msg);
  393. wpabuf_free(msg);
  394. if (*reply == NULL)
  395. return HTTP_INTERNAL_SERVER_ERROR;
  396. *replyname = name;
  397. return HTTP_OK;
  398. }
  399. static enum http_reply_code
  400. web_process_put_wlan_response(struct upnp_wps_device_sm *sm, char *data,
  401. struct wpabuf **reply, const char **replyname)
  402. {
  403. struct wpabuf *msg;
  404. enum http_reply_code ret;
  405. u8 macaddr[ETH_ALEN];
  406. int ev_type;
  407. int type;
  408. char *val;
  409. /*
  410. * External UPnP-based Registrar is passing us a message to be proxied
  411. * over to a Wi-Fi -based client of ours.
  412. */
  413. wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse");
  414. msg = xml_get_base64_item(data, "NewMessage", &ret);
  415. if (msg == NULL)
  416. return ret;
  417. val = xml_get_first_item(data, "NewWLANEventType");
  418. if (val == NULL) {
  419. wpabuf_free(msg);
  420. return UPNP_ARG_VALUE_INVALID;
  421. }
  422. ev_type = atol(val);
  423. os_free(val);
  424. val = xml_get_first_item(data, "NewWLANEventMAC");
  425. if (val == NULL || hwaddr_aton(val, macaddr)) {
  426. wpabuf_free(msg);
  427. os_free(val);
  428. return UPNP_ARG_VALUE_INVALID;
  429. }
  430. os_free(val);
  431. if (ev_type == UPNP_WPS_WLANEVENT_TYPE_EAP) {
  432. struct wps_parse_attr attr;
  433. if (wps_parse_msg(msg, &attr) < 0 ||
  434. attr.msg_type == NULL)
  435. type = -1;
  436. else
  437. type = *attr.msg_type;
  438. wpa_printf(MSG_DEBUG, "WPS UPnP: Message Type %d", type);
  439. } else
  440. type = -1;
  441. if (!sm->ctx->rx_req_put_wlan_response ||
  442. sm->ctx->rx_req_put_wlan_response(sm->priv, ev_type, macaddr, msg,
  443. type)) {
  444. wpa_printf(MSG_INFO, "WPS UPnP: Fail: sm->ctx->"
  445. "rx_req_put_wlan_response");
  446. wpabuf_free(msg);
  447. return HTTP_INTERNAL_SERVER_ERROR;
  448. }
  449. wpabuf_free(msg);
  450. *replyname = NULL;
  451. *reply = NULL;
  452. return HTTP_OK;
  453. }
  454. static enum http_reply_code
  455. web_process_set_selected_registrar(struct upnp_wps_device_sm *sm, char *data,
  456. struct wpabuf **reply,
  457. const char **replyname)
  458. {
  459. struct wpabuf *msg;
  460. enum http_reply_code ret;
  461. wpa_printf(MSG_DEBUG, "WPS UPnP: SetSelectedRegistrar");
  462. msg = xml_get_base64_item(data, "NewMessage", &ret);
  463. if (msg == NULL)
  464. return ret;
  465. if (!sm->ctx->rx_req_set_selected_registrar ||
  466. sm->ctx->rx_req_set_selected_registrar(sm->priv, msg)) {
  467. wpabuf_free(msg);
  468. return HTTP_INTERNAL_SERVER_ERROR;
  469. }
  470. wpabuf_free(msg);
  471. *replyname = NULL;
  472. *reply = NULL;
  473. return HTTP_OK;
  474. }
  475. static const char *soap_prefix =
  476. "<?xml version=\"1.0\"?>\n"
  477. "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
  478. "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
  479. "<s:Body>\n";
  480. static const char *soap_postfix =
  481. "</s:Body>\n</s:Envelope>\n";
  482. static const char *soap_error_prefix =
  483. "<s:Fault>\n"
  484. "<faultcode>s:Client</faultcode>\n"
  485. "<faultstring>UPnPError</faultstring>\n"
  486. "<detail>\n"
  487. "<UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\">\n";
  488. static const char *soap_error_postfix =
  489. "<errorDescription>Error</errorDescription>\n"
  490. "</UPnPError>\n"
  491. "</detail>\n"
  492. "</s:Fault>\n";
  493. static void web_connection_send_reply(struct http_request *req,
  494. enum http_reply_code ret,
  495. const char *action, int action_len,
  496. const struct wpabuf *reply,
  497. const char *replyname)
  498. {
  499. struct wpabuf *buf;
  500. char *replydata;
  501. char *put_length_here = NULL;
  502. char *body_start = NULL;
  503. if (reply) {
  504. size_t len;
  505. replydata = (char *) base64_encode(wpabuf_head(reply),
  506. wpabuf_len(reply), &len);
  507. } else
  508. replydata = NULL;
  509. /* Parameters of the response:
  510. * action(action_len) -- action we are responding to
  511. * replyname -- a name we need for the reply
  512. * replydata -- NULL or null-terminated string
  513. */
  514. buf = wpabuf_alloc(1000 + (replydata ? os_strlen(replydata) : 0U) +
  515. (action_len > 0 ? action_len * 2 : 0));
  516. if (buf == NULL) {
  517. wpa_printf(MSG_INFO, "WPS UPnP: Cannot allocate reply to "
  518. "POST");
  519. os_free(replydata);
  520. http_request_deinit(req);
  521. return;
  522. }
  523. /*
  524. * Assuming we will be successful, put in the output header first.
  525. * Note: we do not keep connections alive (and httpread does
  526. * not support it)... therefore we must have Connection: close.
  527. */
  528. if (ret == HTTP_OK) {
  529. wpabuf_put_str(buf,
  530. "HTTP/1.1 200 OK\r\n"
  531. "Content-Type: text/xml; "
  532. "charset=\"utf-8\"\r\n");
  533. } else {
  534. wpabuf_printf(buf, "HTTP/1.1 %d Error\r\n", ret);
  535. }
  536. wpabuf_put_str(buf, http_connection_close);
  537. wpabuf_put_str(buf, "Content-Length: ");
  538. /*
  539. * We will paste the length in later, leaving some extra whitespace.
  540. * HTTP code is supposed to be tolerant of extra whitespace.
  541. */
  542. put_length_here = wpabuf_put(buf, 0);
  543. wpabuf_put_str(buf, " \r\n");
  544. http_put_date(buf);
  545. /* terminating empty line */
  546. wpabuf_put_str(buf, "\r\n");
  547. body_start = wpabuf_put(buf, 0);
  548. if (ret == HTTP_OK) {
  549. wpabuf_put_str(buf, soap_prefix);
  550. wpabuf_put_str(buf, "<u:");
  551. wpabuf_put_data(buf, action, action_len);
  552. wpabuf_put_str(buf, "Response xmlns:u=\"");
  553. wpabuf_put_str(buf, urn_wfawlanconfig);
  554. wpabuf_put_str(buf, "\">\n");
  555. if (replydata && replyname) {
  556. /* TODO: might possibly need to escape part of reply
  557. * data? ...
  558. * probably not, unlikely to have ampersand(&) or left
  559. * angle bracket (<) in it...
  560. */
  561. wpabuf_printf(buf, "<%s>", replyname);
  562. wpabuf_put_str(buf, replydata);
  563. wpabuf_printf(buf, "</%s>\n", replyname);
  564. }
  565. wpabuf_put_str(buf, "</u:");
  566. wpabuf_put_data(buf, action, action_len);
  567. wpabuf_put_str(buf, "Response>\n");
  568. wpabuf_put_str(buf, soap_postfix);
  569. } else {
  570. /* Error case */
  571. wpabuf_put_str(buf, soap_prefix);
  572. wpabuf_put_str(buf, soap_error_prefix);
  573. wpabuf_printf(buf, "<errorCode>%d</errorCode>\n", ret);
  574. wpabuf_put_str(buf, soap_error_postfix);
  575. wpabuf_put_str(buf, soap_postfix);
  576. }
  577. os_free(replydata);
  578. /* Now patch in the content length at the end */
  579. if (body_start && put_length_here) {
  580. int body_length = (char *) wpabuf_put(buf, 0) - body_start;
  581. char len_buf[10];
  582. os_snprintf(len_buf, sizeof(len_buf), "%d", body_length);
  583. os_memcpy(put_length_here, len_buf, os_strlen(len_buf));
  584. }
  585. http_request_send_and_deinit(req, buf);
  586. }
  587. static const char * web_get_action(struct http_request *req,
  588. const char *filename, size_t *action_len)
  589. {
  590. const char *match;
  591. int match_len;
  592. char *b;
  593. char *action;
  594. *action_len = 0;
  595. if (os_strcasecmp(filename, UPNP_WPS_DEVICE_CONTROL_FILE)) {
  596. wpa_printf(MSG_INFO, "WPS UPnP: Invalid POST filename %s",
  597. filename);
  598. return NULL;
  599. }
  600. /* The SOAPAction line of the header tells us what we want to do */
  601. b = http_request_get_hdr_line(req, "SOAPAction:");
  602. if (b == NULL)
  603. return NULL;
  604. if (*b == '"')
  605. b++;
  606. else
  607. return NULL;
  608. match = urn_wfawlanconfig;
  609. match_len = os_strlen(urn_wfawlanconfig) - 1;
  610. if (os_strncasecmp(b, match, match_len))
  611. return NULL;
  612. b += match_len;
  613. /* skip over version */
  614. while (isgraph(*b) && *b != '#')
  615. b++;
  616. if (*b != '#')
  617. return NULL;
  618. b++;
  619. /* Following the sharp(#) should be the action and a double quote */
  620. action = b;
  621. while (isgraph(*b) && *b != '"')
  622. b++;
  623. if (*b != '"')
  624. return NULL;
  625. *action_len = b - action;
  626. return action;
  627. }
  628. /* Given that we have received a header w/ POST, act upon it
  629. *
  630. * Format of POST (case-insensitive):
  631. *
  632. * First line must be:
  633. * POST /<file> HTTP/1.1
  634. * Since we don't do anything fancy we just ignore other lines.
  635. *
  636. * Our response (if no error) which includes only required lines is:
  637. * HTTP/1.1 200 OK
  638. * Connection: close
  639. * Content-Type: text/xml
  640. * Date: <rfc1123-date>
  641. *
  642. * Header lines must end with \r\n
  643. * Per RFC 2616, content-length: is not required but connection:close
  644. * would appear to be required (given that we will be closing it!).
  645. */
  646. static void web_connection_parse_post(struct upnp_wps_device_sm *sm,
  647. struct http_request *req,
  648. const char *filename)
  649. {
  650. enum http_reply_code ret;
  651. char *data = http_request_get_data(req); /* body of http msg */
  652. const char *action;
  653. size_t action_len;
  654. const char *replyname = NULL; /* argument name for the reply */
  655. struct wpabuf *reply = NULL; /* data for the reply */
  656. ret = UPNP_INVALID_ACTION;
  657. action = web_get_action(req, filename, &action_len);
  658. if (action == NULL)
  659. goto bad;
  660. /*
  661. * There are quite a few possible actions. Although we appear to
  662. * support them all here, not all of them are necessarily supported by
  663. * callbacks at higher levels.
  664. */
  665. if (!os_strncasecmp("GetDeviceInfo", action, action_len))
  666. ret = web_process_get_device_info(sm, &reply, &replyname);
  667. else if (!os_strncasecmp("PutMessage", action, action_len))
  668. ret = web_process_put_message(sm, data, &reply, &replyname);
  669. else if (!os_strncasecmp("PutWLANResponse", action, action_len))
  670. ret = web_process_put_wlan_response(sm, data, &reply,
  671. &replyname);
  672. else if (!os_strncasecmp("SetSelectedRegistrar", action, action_len))
  673. ret = web_process_set_selected_registrar(sm, data, &reply,
  674. &replyname);
  675. else
  676. wpa_printf(MSG_INFO, "WPS UPnP: Unknown POST type");
  677. bad:
  678. if (ret != HTTP_OK)
  679. wpa_printf(MSG_INFO, "WPS UPnP: POST failure ret=%d", ret);
  680. web_connection_send_reply(req, ret, action, action_len, reply,
  681. replyname);
  682. wpabuf_free(reply);
  683. }
  684. /* Given that we have received a header w/ SUBSCRIBE, act upon it
  685. *
  686. * Format of SUBSCRIBE (case-insensitive):
  687. *
  688. * First line must be:
  689. * SUBSCRIBE /wps_event HTTP/1.1
  690. *
  691. * Our response (if no error) which includes only required lines is:
  692. * HTTP/1.1 200 OK
  693. * Server: xx, UPnP/1.0, xx
  694. * SID: uuid:xxxxxxxxx
  695. * Timeout: Second-<n>
  696. * Content-Length: 0
  697. * Date: xxxx
  698. *
  699. * Header lines must end with \r\n
  700. * Per RFC 2616, content-length: is not required but connection:close
  701. * would appear to be required (given that we will be closing it!).
  702. */
  703. static void web_connection_parse_subscribe(struct upnp_wps_device_sm *sm,
  704. struct http_request *req,
  705. const char *filename)
  706. {
  707. struct wpabuf *buf;
  708. char *b;
  709. char *hdr = http_request_get_hdr(req);
  710. char *h;
  711. char *match;
  712. int match_len;
  713. char *end;
  714. int len;
  715. int got_nt = 0;
  716. u8 uuid[UUID_LEN];
  717. int got_uuid = 0;
  718. char *callback_urls = NULL;
  719. struct subscription *s = NULL;
  720. enum http_reply_code ret = HTTP_INTERNAL_SERVER_ERROR;
  721. buf = wpabuf_alloc(1000);
  722. if (buf == NULL) {
  723. http_request_deinit(req);
  724. return;
  725. }
  726. /* Parse/validate headers */
  727. h = hdr;
  728. /* First line: SUBSCRIBE /wps_event HTTP/1.1
  729. * has already been parsed.
  730. */
  731. if (os_strcasecmp(filename, UPNP_WPS_DEVICE_EVENT_FILE) != 0) {
  732. ret = HTTP_PRECONDITION_FAILED;
  733. goto error;
  734. }
  735. wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP SUBSCRIBE for event");
  736. end = os_strchr(h, '\n');
  737. for (; end != NULL; h = end + 1) {
  738. /* Option line by option line */
  739. h = end + 1;
  740. end = os_strchr(h, '\n');
  741. if (end == NULL)
  742. break; /* no unterminated lines allowed */
  743. /* NT assures that it is our type of subscription;
  744. * not used for a renewl.
  745. **/
  746. match = "NT:";
  747. match_len = os_strlen(match);
  748. if (os_strncasecmp(h, match, match_len) == 0) {
  749. h += match_len;
  750. while (*h == ' ' || *h == '\t')
  751. h++;
  752. match = "upnp:event";
  753. match_len = os_strlen(match);
  754. if (os_strncasecmp(h, match, match_len) != 0) {
  755. ret = HTTP_BAD_REQUEST;
  756. goto error;
  757. }
  758. got_nt = 1;
  759. continue;
  760. }
  761. /* HOST should refer to us */
  762. #if 0
  763. match = "HOST:";
  764. match_len = os_strlen(match);
  765. if (os_strncasecmp(h, match, match_len) == 0) {
  766. h += match_len;
  767. while (*h == ' ' || *h == '\t')
  768. h++;
  769. .....
  770. }
  771. #endif
  772. /* CALLBACK gives one or more URLs for NOTIFYs
  773. * to be sent as a result of the subscription.
  774. * Each URL is enclosed in angle brackets.
  775. */
  776. match = "CALLBACK:";
  777. match_len = os_strlen(match);
  778. if (os_strncasecmp(h, match, match_len) == 0) {
  779. h += match_len;
  780. while (*h == ' ' || *h == '\t')
  781. h++;
  782. len = end - h;
  783. os_free(callback_urls);
  784. callback_urls = os_malloc(len + 1);
  785. if (callback_urls == NULL) {
  786. ret = HTTP_INTERNAL_SERVER_ERROR;
  787. goto error;
  788. }
  789. os_memcpy(callback_urls, h, len);
  790. callback_urls[len] = 0;
  791. continue;
  792. }
  793. /* SID is only for renewal */
  794. match = "SID:";
  795. match_len = os_strlen(match);
  796. if (os_strncasecmp(h, match, match_len) == 0) {
  797. h += match_len;
  798. while (*h == ' ' || *h == '\t')
  799. h++;
  800. match = "uuid:";
  801. match_len = os_strlen(match);
  802. if (os_strncasecmp(h, match, match_len) != 0) {
  803. ret = HTTP_BAD_REQUEST;
  804. goto error;
  805. }
  806. h += match_len;
  807. while (*h == ' ' || *h == '\t')
  808. h++;
  809. if (uuid_str2bin(h, uuid)) {
  810. ret = HTTP_BAD_REQUEST;
  811. goto error;
  812. }
  813. got_uuid = 1;
  814. continue;
  815. }
  816. /* TIMEOUT is requested timeout, but apparently we can
  817. * just ignore this.
  818. */
  819. }
  820. if (got_uuid) {
  821. /* renewal */
  822. if (callback_urls) {
  823. ret = HTTP_BAD_REQUEST;
  824. goto error;
  825. }
  826. s = subscription_renew(sm, uuid);
  827. if (s == NULL) {
  828. ret = HTTP_PRECONDITION_FAILED;
  829. goto error;
  830. }
  831. } else if (callback_urls) {
  832. if (!got_nt) {
  833. ret = HTTP_PRECONDITION_FAILED;
  834. goto error;
  835. }
  836. s = subscription_start(sm, callback_urls);
  837. if (s == NULL) {
  838. ret = HTTP_INTERNAL_SERVER_ERROR;
  839. goto error;
  840. }
  841. callback_urls = NULL; /* is now owned by subscription */
  842. } else {
  843. ret = HTTP_PRECONDITION_FAILED;
  844. goto error;
  845. }
  846. /* success */
  847. http_put_reply_code(buf, HTTP_OK);
  848. wpabuf_put_str(buf, http_server_hdr);
  849. wpabuf_put_str(buf, http_connection_close);
  850. wpabuf_put_str(buf, "Content-Length: 0\r\n");
  851. wpabuf_put_str(buf, "SID: uuid:");
  852. /* subscription id */
  853. b = wpabuf_put(buf, 0);
  854. uuid_bin2str(s->uuid, b, 80);
  855. wpabuf_put(buf, os_strlen(b));
  856. wpabuf_put_str(buf, "\r\n");
  857. wpabuf_printf(buf, "Timeout: Second-%d\r\n", UPNP_SUBSCRIBE_SEC);
  858. http_put_date(buf);
  859. /* And empty line to terminate header: */
  860. wpabuf_put_str(buf, "\r\n");
  861. os_free(callback_urls);
  862. http_request_send_and_deinit(req, buf);
  863. return;
  864. error:
  865. /* Per UPnP spec:
  866. * Errors
  867. * Incompatible headers
  868. * 400 Bad Request. If SID header and one of NT or CALLBACK headers
  869. * are present, the publisher must respond with HTTP error
  870. * 400 Bad Request.
  871. * Missing or invalid CALLBACK
  872. * 412 Precondition Failed. If CALLBACK header is missing or does not
  873. * contain a valid HTTP URL, the publisher must respond with HTTP
  874. * error 412 Precondition Failed.
  875. * Invalid NT
  876. * 412 Precondition Failed. If NT header does not equal upnp:event,
  877. * the publisher must respond with HTTP error 412 Precondition
  878. * Failed.
  879. * [For resubscription, use 412 if unknown uuid].
  880. * Unable to accept subscription
  881. * 5xx. If a publisher is not able to accept a subscription (such as
  882. * due to insufficient resources), it must respond with a
  883. * HTTP 500-series error code.
  884. * 599 Too many subscriptions (not a standard HTTP error)
  885. */
  886. http_put_empty(buf, ret);
  887. http_request_send_and_deinit(req, buf);
  888. }
  889. /* Given that we have received a header w/ UNSUBSCRIBE, act upon it
  890. *
  891. * Format of UNSUBSCRIBE (case-insensitive):
  892. *
  893. * First line must be:
  894. * UNSUBSCRIBE /wps_event HTTP/1.1
  895. *
  896. * Our response (if no error) which includes only required lines is:
  897. * HTTP/1.1 200 OK
  898. * Content-Length: 0
  899. *
  900. * Header lines must end with \r\n
  901. * Per RFC 2616, content-length: is not required but connection:close
  902. * would appear to be required (given that we will be closing it!).
  903. */
  904. static void web_connection_parse_unsubscribe(struct upnp_wps_device_sm *sm,
  905. struct http_request *req,
  906. const char *filename)
  907. {
  908. struct wpabuf *buf;
  909. char *hdr = http_request_get_hdr(req);
  910. char *h;
  911. char *match;
  912. int match_len;
  913. char *end;
  914. u8 uuid[UUID_LEN];
  915. int got_uuid = 0;
  916. struct subscription *s = NULL;
  917. enum http_reply_code ret = HTTP_INTERNAL_SERVER_ERROR;
  918. /* Parse/validate headers */
  919. h = hdr;
  920. /* First line: UNSUBSCRIBE /wps_event HTTP/1.1
  921. * has already been parsed.
  922. */
  923. if (os_strcasecmp(filename, UPNP_WPS_DEVICE_EVENT_FILE) != 0) {
  924. ret = HTTP_PRECONDITION_FAILED;
  925. goto send_msg;
  926. }
  927. wpa_printf(MSG_DEBUG, "WPS UPnP: HTTP UNSUBSCRIBE for event");
  928. end = os_strchr(h, '\n');
  929. for (; end != NULL; h = end + 1) {
  930. /* Option line by option line */
  931. h = end + 1;
  932. end = os_strchr(h, '\n');
  933. if (end == NULL)
  934. break; /* no unterminated lines allowed */
  935. /* HOST should refer to us */
  936. #if 0
  937. match = "HOST:";
  938. match_len = os_strlen(match);
  939. if (os_strncasecmp(h, match, match_len) == 0) {
  940. h += match_len;
  941. while (*h == ' ' || *h == '\t')
  942. h++;
  943. .....
  944. }
  945. #endif
  946. /* SID is only for renewal */
  947. match = "SID:";
  948. match_len = os_strlen(match);
  949. if (os_strncasecmp(h, match, match_len) == 0) {
  950. h += match_len;
  951. while (*h == ' ' || *h == '\t')
  952. h++;
  953. match = "uuid:";
  954. match_len = os_strlen(match);
  955. if (os_strncasecmp(h, match, match_len) != 0) {
  956. ret = HTTP_BAD_REQUEST;
  957. goto send_msg;
  958. }
  959. h += match_len;
  960. while (*h == ' ' || *h == '\t')
  961. h++;
  962. if (uuid_str2bin(h, uuid)) {
  963. ret = HTTP_BAD_REQUEST;
  964. goto send_msg;
  965. }
  966. got_uuid = 1;
  967. continue;
  968. }
  969. }
  970. if (got_uuid) {
  971. s = subscription_find(sm, uuid);
  972. if (s) {
  973. wpa_printf(MSG_DEBUG, "WPS UPnP: Unsubscribing %p %s",
  974. s,
  975. (s && s->addr_list &&
  976. s->addr_list->domain_and_port) ?
  977. s->addr_list->domain_and_port : "-null-");
  978. subscription_unlink(s);
  979. subscription_destroy(s);
  980. }
  981. } else {
  982. wpa_printf(MSG_INFO, "WPS UPnP: Unsubscribe fails (not "
  983. "found)");
  984. ret = HTTP_PRECONDITION_FAILED;
  985. goto send_msg;
  986. }
  987. ret = HTTP_OK;
  988. send_msg:
  989. buf = wpabuf_alloc(200);
  990. if (buf == NULL) {
  991. http_request_deinit(req);
  992. return;
  993. }
  994. http_put_empty(buf, ret);
  995. http_request_send_and_deinit(req, buf);
  996. }
  997. /* Send error in response to unknown requests */
  998. static void web_connection_unimplemented(struct http_request *req)
  999. {
  1000. struct wpabuf *buf;
  1001. buf = wpabuf_alloc(200);
  1002. if (buf == NULL) {
  1003. http_request_deinit(req);
  1004. return;
  1005. }
  1006. http_put_empty(buf, HTTP_UNIMPLEMENTED);
  1007. http_request_send_and_deinit(req, buf);
  1008. }
  1009. /* Called when we have gotten an apparently valid http request.
  1010. */
  1011. static void web_connection_check_data(void *ctx, struct http_request *req)
  1012. {
  1013. struct upnp_wps_device_sm *sm = ctx;
  1014. enum httpread_hdr_type htype = http_request_get_type(req);
  1015. char *filename = http_request_get_uri(req);
  1016. struct sockaddr_in *cli = http_request_get_cli_addr(req);
  1017. if (!filename) {
  1018. wpa_printf(MSG_INFO, "WPS UPnP: Could not get HTTP URI");
  1019. http_request_deinit(req);
  1020. return;
  1021. }
  1022. /* Trim leading slashes from filename */
  1023. while (*filename == '/')
  1024. filename++;
  1025. wpa_printf(MSG_DEBUG, "WPS UPnP: Got HTTP request type %d from %s:%d",
  1026. htype, inet_ntoa(cli->sin_addr), htons(cli->sin_port));
  1027. switch (htype) {
  1028. case HTTPREAD_HDR_TYPE_GET:
  1029. web_connection_parse_get(sm, req, filename);
  1030. break;
  1031. case HTTPREAD_HDR_TYPE_POST:
  1032. web_connection_parse_post(sm, req, filename);
  1033. break;
  1034. case HTTPREAD_HDR_TYPE_SUBSCRIBE:
  1035. web_connection_parse_subscribe(sm, req, filename);
  1036. break;
  1037. case HTTPREAD_HDR_TYPE_UNSUBSCRIBE:
  1038. web_connection_parse_unsubscribe(sm, req, filename);
  1039. break;
  1040. /* We are not required to support M-POST; just plain
  1041. * POST is supposed to work, so we only support that.
  1042. * If for some reason we need to support M-POST, it is
  1043. * mostly the same as POST, with small differences.
  1044. */
  1045. default:
  1046. /* Send 501 for anything else */
  1047. web_connection_unimplemented(req);
  1048. break;
  1049. }
  1050. }
  1051. /*
  1052. * Listening for web connections
  1053. * We have a single TCP listening port, and hand off connections as we get
  1054. * them.
  1055. */
  1056. void web_listener_stop(struct upnp_wps_device_sm *sm)
  1057. {
  1058. http_server_deinit(sm->web_srv);
  1059. sm->web_srv = NULL;
  1060. }
  1061. int web_listener_start(struct upnp_wps_device_sm *sm)
  1062. {
  1063. struct in_addr addr;
  1064. addr.s_addr = sm->ip_addr;
  1065. sm->web_srv = http_server_init(&addr, -1, web_connection_check_data,
  1066. sm);
  1067. if (sm->web_srv == NULL) {
  1068. web_listener_stop(sm);
  1069. return -1;
  1070. }
  1071. sm->web_port = http_server_get_port(sm->web_srv);
  1072. return 0;
  1073. }