1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
|
/*
* Copyright (C) 2008 - 2012 Vivien Malerba <malerba@gnome-db.org>
* Copyright (C) 2009 Bas Driessen <bas.driessen@xobas.com>
* Copyright (C) 2010 David King <davidk@openismus.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "web-server.h"
#include <libsoup/soup.h>
#include "html-doc.h"
#include <libgda/binreloc/gda-binreloc.h>
/* Use the RSA reference implementation included in the RFC-1321, http://www.freesoft.org/CIE/RFC/1321/ */
#include "global.h"
#include "md5.h"
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#define MAX_CHALLENGES 10
#define MAX_AUTH_COOKIES 10
/*
* Main static functions
*/
static void web_server_class_init (WebServerClass * class);
static void web_server_init (WebServer *server);
static void web_server_dispose (GObject *object);
static void web_server_finalize (GObject *object);
/* get a pointer to the parents to be able to call their destructor */
static GObjectClass *parent_class = NULL;
struct _WebServerPrivate
{
SoupServer *server;
GHashTable *resources_hash; /* key = a path without the starting '/', value = a TmpResource pointer */
GSList *resources_list; /* list of the TmpResource pointers in @resources_hash, memory not managed here */
guint resources_timer;
/* authentication */
gchar *token; /* FIXME: protect it! */
GArray *challenges; /* array of TimedString representing the currently valid challenges */
GArray *cookies; /* array of TimedString representing the currently valid authentication cookie values */
guint auth_timer;
GSList *terminals_list; /* list of SqlConsole */
guint term_timer;
};
typedef struct {
gchar *string;
GTimeVal validity;
} TimedString;
static TimedString *timed_string_new (guint duration);
static void timed_string_free (TimedString *ts);
static TimedString *challenge_add (WebServer *server);
static void challenges_manage (WebServer *server);
static TimedString *auth_cookie_add (WebServer *server);
static void auth_cookies_manage (WebServer *server);
/*
* Temporary available data
*
* Each TmpResource structure represents a resource which will be available for some time (until it has
* expired).
*
* If the expiration_date attribute is set to 0, then there is no expiration at all.
*/
typedef struct {
gchar *path;
gchar *data;
gsize size;
int expiration_date; /* 0 to avoid expiration */
} TmpResource;
static TmpResource *tmp_resource_add (WebServer *server, const gchar *path, gchar *data, gsize data_length);
static gboolean delete_tmp_resource (WebServer *server);
static void tmp_resource_free (TmpResource *data);
/* module error */
GQuark web_server_error_quark (void)
{
static GQuark quark;
if (!quark)
quark = g_quark_from_static_string ("web_server_error");
return quark;
}
GType
web_server_get_type (void)
{
static GType type = 0;
if (G_UNLIKELY (type == 0)) {
static GMutex registering;
static const GTypeInfo info = {
sizeof (WebServerClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) web_server_class_init,
NULL,
NULL,
sizeof (WebServer),
0,
(GInstanceInitFunc) web_server_init,
0
};
g_mutex_lock (®istering);
if (type == 0)
type = g_type_register_static (G_TYPE_OBJECT, "WebServer", &info, 0);
g_mutex_unlock (®istering);
}
return type;
}
static void
web_server_class_init (WebServerClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
parent_class = g_type_class_peek_parent (class);
/* virtual functions */
object_class->dispose = web_server_dispose;
object_class->finalize = web_server_finalize;
}
static void
web_server_init (WebServer *server)
{
server->priv = g_new0 (WebServerPrivate, 1);
server->priv->resources_timer = 0;
server->priv->resources_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, (GDestroyNotify) tmp_resource_free);
server->priv->resources_list = NULL;
server->priv->token = g_strdup ("");
server->priv->challenges = g_array_new (FALSE, FALSE, sizeof (TimedString*));
server->priv->cookies = g_array_new (FALSE, FALSE, sizeof (TimedString*));
server->priv->auth_timer = 0;
server->priv->terminals_list = NULL;
server->priv->term_timer = 0;
}
static void get_cookies (SoupMessage *msg, ...);
static gboolean get_file (WebServer *server, SoupMessage *msg, const char *path, GError **error);
static void get_root (WebServer *server, SoupMessage *msg);
static void get_for_console (WebServer *server, SoupMessage *msg);
static gboolean get_for_cnc (WebServer *webserver, SoupMessage *msg,
const ConnectionSetting *cs, gchar **extra, GError **error);
static gboolean get_auth (WebServer *server, SoupMessage *msg, GHashTable *query);
static gboolean get_post_for_irb (WebServer *webserver, SoupMessage *msg,
const ConnectionSetting *cs, GHashTable *query, GError **error);
static void get_for_cnclist (WebServer *webserver, SoupMessage *msg, gboolean is_authenticated);
/*#define DEBUG_SERVER*/
#ifdef DEBUG_SERVER
static void
debug_display_query (gchar *key, gchar *value, gpointer data)
{
g_print ("\t%s => %s\n", key, value);
}
#endif
static void
server_callback (G_GNUC_UNUSED SoupServer *server, SoupMessage *msg,
const char *path, GHashTable *query,
G_GNUC_UNUSED SoupClientContext *context, WebServer *webserver)
{
#ifdef DEBUG_SERVER
printf ("%s %s HTTP/1.%d\n", msg->method, path, soup_message_get_http_version (msg));
/*
SoupMessageHeadersIter iter;
const char *name, *value;
soup_message_headers_iter_init (&iter, msg->request_headers);
while (soup_message_headers_iter_next (&iter, &name, &value))
printf ("%s: %s\n", name, value);
*/
if (msg->request_body->length)
printf ("Request body: %s\n", msg->request_body->data);
if (query) {
printf ("Query parts:\n");
g_hash_table_foreach (query, (GHFunc) debug_display_query, NULL);
}
#endif
GError *error = NULL;
gboolean ok = TRUE;
gboolean done = FALSE;
TmpResource *tmpdata;
if ((*path != '/') || (*path && (path[1] == '/'))) {
soup_message_set_status_full (msg, SOUP_STATUS_UNAUTHORIZED, "Wrong path name");
return;
}
path++;
/* check for authentication */
gboolean auth_needed = TRUE;
if (g_str_has_suffix (path, ".js") ||
g_str_has_suffix (path, ".css"))
auth_needed = FALSE;
if (auth_needed) {
/* check cookie named "coa" */
gchar *cookie;
get_cookies (msg, "coa", &cookie, NULL);
if (cookie) {
gsize n;
for (n = 0; n < webserver->priv->cookies->len; n++) {
TimedString *ts = g_array_index (webserver->priv->cookies, TimedString *, n);
#ifdef DEBUG_SERVER
g_print ("CMP Cookie %s with msg's cookie %s\n",
ts->string, cookie);
#endif
if (!strcmp (ts->string, cookie)) {
/* cookie exists => we are authenticated */
auth_needed = FALSE;
break;
}
}
g_free (cookie);
}
if (auth_needed && !g_str_has_suffix (path, "~cnclist")) {
if (!get_auth (webserver, msg, query))
return;
}
}
if (*path == 0) {
if (msg->method == SOUP_METHOD_GET) {
get_root (webserver, msg);
done = TRUE;
}
}
else if ((tmpdata = g_hash_table_lookup (webserver->priv->resources_hash, path))) {
if (msg->method == SOUP_METHOD_GET) {
soup_message_body_append (msg->response_body, SOUP_MEMORY_STATIC,
tmpdata->data, tmpdata->size);
soup_message_set_status (msg, SOUP_STATUS_OK);
done = TRUE;
}
}
else {
gchar **array = NULL;
array = g_strsplit (path, "/", 0);
if (array) {
const ConnectionSetting *cs;
cs = gda_sql_get_connection (array[0]);
if (cs) {
if (msg->method == SOUP_METHOD_GET) {
ok = get_for_cnc (webserver, msg, cs, array[1] ? &(array[1]) : NULL, &error);
done = TRUE;
}
}
else if (!strcmp (path, "~console")) {
get_for_console (webserver, msg);
done = TRUE;
}
else if (!strcmp (path, "~irb")) {
ok = get_post_for_irb (webserver, msg, cs, query, &error);
done = TRUE;
}
else if (!strcmp (path, "~cnclist")) {
get_for_cnclist (webserver, msg, !auth_needed);
done = TRUE;
}
else {
if (msg->method == SOUP_METHOD_GET) {
ok = get_file (webserver, msg, path, &error);
done = TRUE;
}
}
g_strfreev (array);
}
else {
ok= FALSE;
done = TRUE;
}
}
if (!ok) {
if (error) {
soup_message_set_status_full (msg, error->code, error->message);
g_error_free (error);
}
else
soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
}
if (!done)
soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
#ifdef DEBUG_SERVER
printf (" -> %d %s\n\n", msg->status_code, msg->reason_phrase);
#endif
}
/**
* web_server_new
* @type: the #GType requested
* @auth_token: (allow-none): the authentication token, or %NULL
*
* Creates a new server of type @type
*
* Returns: a new #WebServer object
*/
WebServer *
web_server_new (gint port, const gchar *auth_token)
{
WebServer *server;
server = (WebServer*) g_object_new (WEB_TYPE_SERVER, NULL);
server->priv->server = soup_server_new (SOUP_SERVER_PORT, port,
SOUP_SERVER_SERVER_HEADER, "gda-sql-httpd ",
NULL);
soup_server_add_handler (server->priv->server, NULL,
(SoupServerCallback) server_callback, server, NULL);
if (auth_token) {
g_free (server->priv->token);
server->priv->token = g_strdup (auth_token);
}
soup_server_run_async (server->priv->server);
return server;
}
static void
web_server_dispose (GObject *object)
{
WebServer *server;
server = WEB_SERVER (object);
if (server->priv) {
if (server->priv->resources_hash) {
g_hash_table_destroy (server->priv->resources_hash);
server->priv->resources_hash = NULL;
}
if (server->priv->resources_list) {
g_slist_free (server->priv->resources_list);
server->priv->resources_list = NULL;
}
if (server->priv->server) {
g_object_unref (server->priv->server);
server->priv->server = NULL;
}
if (server->priv->resources_timer) {
g_source_remove (server->priv->resources_timer);
server->priv->resources_timer = 0;
}
if (server->priv->auth_timer) {
g_source_remove (server->priv->auth_timer);
server->priv->auth_timer = 0;
}
if (server->priv->term_timer) {
g_source_remove (server->priv->term_timer);
server->priv->term_timer = 0;
}
}
/* parent class */
parent_class->dispose (object);
}
static void
web_server_finalize (GObject * object)
{
WebServer *server;
g_return_if_fail (object != NULL);
g_return_if_fail (WEB_IS_SERVER (object));
server = WEB_SERVER (object);
if (server->priv) {
gsize i;
for (i = 0; i < server->priv->challenges->len; i++) {
TimedString *ts = g_array_index (server->priv->challenges, TimedString *, i);
timed_string_free (ts);
}
g_array_free (server->priv->challenges, TRUE);
for (i = 0; i < server->priv->cookies->len; i++) {
TimedString *ts = g_array_index (server->priv->cookies, TimedString *, i);
timed_string_free (ts);
}
g_array_free (server->priv->cookies, TRUE);
if (server->priv->terminals_list) {
g_slist_foreach (server->priv->terminals_list, (GFunc) gda_sql_console_free, NULL);
g_slist_free (server->priv->terminals_list);
}
g_free (server->priv);
}
/* parent class */
parent_class->finalize (object);
}
/*
*
* Server GET/POST methods
*
*/
static HtmlDoc *create_new_htmldoc (WebServer *webserver, const ConnectionSetting *cs);
static void get_variables (SoupMessage *msg, GHashTable *query, ...);
/*
* GET for a file
*/
static gboolean
get_file (G_GNUC_UNUSED WebServer *server, SoupMessage *msg, const char *path, GError **error)
{
GMappedFile *mfile;
gchar *real_path;
real_path = gda_gbr_get_file_path (GDA_DATA_DIR, "libgda-5.0", "web", path, NULL);
if (!real_path)
return FALSE;
/*g_print ("get_file () => %s\n", real_path);*/
if (!g_file_test (real_path, G_FILE_TEST_EXISTS)) {
/* test if we are in the compilation directory */
gchar *cwd, *tmp;
cwd = g_get_current_dir ();
tmp = g_build_filename (cwd, "gda-sql.c", NULL);
if (g_file_test (tmp, G_FILE_TEST_EXISTS)) {
g_free (real_path);
real_path = g_build_filename (cwd, path, NULL);
}
else {
g_free (cwd);
return FALSE;
}
g_free (cwd);
}
mfile = g_mapped_file_new (real_path, FALSE, error);
g_free (real_path);
if (!mfile)
return FALSE;
SoupBuffer *buffer;
buffer = soup_buffer_new_with_owner (g_mapped_file_get_contents (mfile),
g_mapped_file_get_length (mfile),
mfile, (GDestroyNotify) g_mapped_file_unref);
soup_message_body_append_buffer (msg->response_body, buffer);
soup_buffer_free (buffer);
soup_message_set_status (msg, SOUP_STATUS_OK);
return TRUE;
}
#define PAD_LEN 64 /* PAD length */
#define SIG_LEN 16 /* MD5 digest length */
/*
* From RFC 2104
*/
static void
hmac_md5 (uint8_t* text, /* pointer to data stream */
int text_len, /* length of data stream */
uint8_t* key, /* pointer to authentication key */
int key_len, /* length of authentication key */
uint8_t *hmac) /* returned hmac-md5 */
{
MD5_CTX md5c;
uint8_t k_ipad[PAD_LEN]; /* inner padding - key XORd with ipad */
uint8_t k_opad[PAD_LEN]; /* outer padding - key XORd with opad */
uint8_t keysig[SIG_LEN];
int i;
/* if key is longer than PAD length, reset it to key=MD5(key) */
if (key_len > PAD_LEN) {
MD5_CTX md5key;
MD5Init (&md5key);
MD5Update (&md5key, key, key_len);
MD5Final (keysig, &md5key);
key = keysig;
key_len = SIG_LEN;
}
/*
* the HMAC_MD5 transform looks like:
*
* MD5(Key XOR opad, MD5(Key XOR ipad, text))
*
* where Key is an n byte key
* ipad is the byte 0x36 repeated 64 times
* opad is the byte 0x5c repeated 64 times
* and text is the data being protected
*/
/* Zero pads and store key */
memset (k_ipad, 0, PAD_LEN);
memcpy (k_ipad, key, key_len);
memcpy (k_opad, k_ipad, PAD_LEN);
/* XOR key with ipad and opad values */
for (i=0; i<PAD_LEN; i++) {
k_ipad[i] ^= 0x36;
k_opad[i] ^= 0x5c;
}
/* perform inner MD5 */
MD5Init (&md5c); /* start inner hash */
MD5Update (&md5c, k_ipad, PAD_LEN); /* hash inner pad */
MD5Update (&md5c, text, text_len); /* hash text */
MD5Final (hmac, &md5c); /* store inner hash */
/* perform outer MD5 */
MD5Init (&md5c); /* start outer hash */
MD5Update (&md5c, k_opad, PAD_LEN); /* hash outer pad */
MD5Update (&md5c, hmac, SIG_LEN); /* hash inner hash */
MD5Final (hmac, &md5c); /* store results */
}
/*
* Creates a login form
*
* Returns: TRUE if the user is now authenticated, or FALSE if the user needs to authenticate
*/
static gboolean
get_auth (WebServer *server, SoupMessage *msg, GHashTable *query)
{
HtmlDoc *hdoc;
xmlChar *xstr;
SoupBuffer *buffer;
gsize size;
hdoc = html_doc_new (_("Authentication required"));
xmlNewChild (hdoc->content, NULL, BAD_CAST "h1", BAD_CAST _("Authentication required"));
xmlNewChild (xmlNewChild (hdoc->content, NULL, BAD_CAST "p", NULL),
NULL, BAD_CAST "small", BAD_CAST _("Enter authentification token as set "
"from console, or leave empty if "
"none required"));
soup_message_headers_replace (msg->response_headers,
"Content-Type", "text/html");
/* check to see if this page is called as an answer to authentication */
gchar *token = NULL;
get_variables (msg, query, "etoken", &token, NULL);
if (token) {
gsize n;
for (n = 0; n < server->priv->challenges->len; n++) {
TimedString *ts = g_array_index (server->priv->challenges, TimedString *, n);
uint8_t hmac[16];
GString *md5str;
gint i;
hmac_md5 ((uint8_t *) ts->string, strlen (ts->string),
(uint8_t *) server->priv->token, strlen (server->priv->token), hmac);
md5str = g_string_new ("");
for (i = 0; i < 16; i++)
g_string_append_printf (md5str, "%02x", hmac[i]);
if (!strcmp (md5str->str, token)) {
/* forge a new location change message */
GString *cook;
gchar *tmp;
TimedString *new_cookie;
new_cookie = auth_cookie_add (server);
cook = g_string_new ("");
tmp = gda_rfc1738_encode (new_cookie->string);
g_string_append_printf (cook, "coa=%s; path=/", tmp);
g_free (tmp);
soup_message_headers_append (msg->response_headers, "Set-Cookie", cook->str);
g_string_free (cook, TRUE);
/*soup_message_set_status (msg, SOUP_STATUS_TEMPORARY_REDIRECT);
soup_message_headers_append (msg->response_headers, "Location", "/");*/
g_string_free (md5str, TRUE);
g_free (token);
return TRUE;
}
g_string_free (md5str, TRUE);
}
g_free (token);
}
/* Add javascript */
xmlNodePtr form, node;
node = xmlNewChild (hdoc->head, NULL, BAD_CAST "script", BAD_CAST " ");
xmlSetProp (node, BAD_CAST "type", BAD_CAST "text/javascript");
xmlSetProp (node, BAD_CAST "src", BAD_CAST "/md5.js");
/* login form */
TimedString *new_challenge = challenge_add (server);
form = xmlNewChild (hdoc->content, NULL, BAD_CAST "form", NULL);
gchar *str = g_strdup_printf ("javascript:etoken.value=hex_hmac_md5(token.value, '%s'); javascript:token.value=''", new_challenge->string);
xmlSetProp (form, BAD_CAST "onsubmit", BAD_CAST str);
g_free (str);
node = xmlNewChild (form, NULL, BAD_CAST "input", BAD_CAST _("Token:"));
xmlSetProp (node, BAD_CAST "type", BAD_CAST "hidden");
xmlSetProp (node, BAD_CAST "name", BAD_CAST "etoken");
node = xmlNewChild (form, NULL, BAD_CAST "input", NULL);
xmlSetProp (node, BAD_CAST "type", BAD_CAST "password");
xmlSetProp (node, BAD_CAST "name", BAD_CAST "token");
node = xmlNewChild (form, NULL, BAD_CAST "input", NULL);
xmlSetProp (node, BAD_CAST "type", BAD_CAST "submit");
xmlSetProp (node, BAD_CAST "value", BAD_CAST "login");
xmlSetProp (node, BAD_CAST "colspan", BAD_CAST "2");
xstr = html_doc_to_string (hdoc, &size);
buffer = soup_buffer_new_with_owner (xstr, size, xstr, (GDestroyNotify)xmlFree);
soup_message_body_append_buffer (msg->response_body, buffer);
soup_buffer_free (buffer);
html_doc_free (hdoc);
soup_message_set_status (msg, SOUP_STATUS_OK);
return FALSE;
}
/*
* GET for the / path
*/
static void
get_root (WebServer *server, SoupMessage *msg)
{
HtmlDoc *hdoc;
xmlChar *xstr;
SoupBuffer *buffer;
gsize size;
const GSList *list;
list = gda_sql_get_all_connections ();
if (list && !list->next) {
/* only 1 connection => go to this one */
ConnectionSetting *cs = (ConnectionSetting*) list->data;
soup_message_set_status (msg, SOUP_STATUS_TEMPORARY_REDIRECT);
soup_message_headers_append (msg->response_headers, "Location", cs->name);
return;
}
else if (list) {
/* more than one connection, redirect to the current one */
const ConnectionSetting *cs = gda_sql_get_current_connection ();
soup_message_set_status (msg, SOUP_STATUS_TEMPORARY_REDIRECT);
soup_message_headers_append (msg->response_headers, "Location", cs->name);
return;
}
hdoc = create_new_htmldoc (server, NULL);
soup_message_headers_replace (msg->response_headers,
"Content-Type", "text/html");
xstr = html_doc_to_string (hdoc, &size);
buffer = soup_buffer_new_with_owner (xstr, size, xstr, (GDestroyNotify)xmlFree);
soup_message_body_append_buffer (msg->response_body, buffer);
soup_buffer_free (buffer);
html_doc_free (hdoc);
soup_message_set_status (msg, SOUP_STATUS_OK);
}
static xmlNodePtr
cnc_ul (gboolean is_authenticated)
{
xmlNodePtr ul, li, a;
const GSList *clist, *list;
gchar *str;
/* other connections in the sidebar */
if (is_authenticated)
list = gda_sql_get_all_connections ();
else
list = NULL;
ul = xmlNewNode (NULL, BAD_CAST "ul");
xmlNodeSetContent(ul, BAD_CAST _("Connections"));
xmlSetProp (ul, BAD_CAST "id", BAD_CAST "cnclist");
if (!list) {
/* no connection at all or not authenticated */
str = g_strdup_printf ("(%s)", _("None"));
li = xmlNewChild (ul, NULL, BAD_CAST "li", NULL);
xmlNewChild (li, NULL, BAD_CAST "a", BAD_CAST str);
g_free (str);
}
else {
for (clist = list; clist; clist = clist->next) {
gchar *tmp;
ConnectionSetting *cs2 = (ConnectionSetting*) clist->data;
li = xmlNewChild (ul, NULL, BAD_CAST "li", NULL);
a = xmlNewChild (li, NULL, BAD_CAST "a", BAD_CAST cs2->name);
tmp = gda_rfc1738_encode (cs2->name);
str = g_strdup_printf ("/%s", tmp);
g_free (tmp);
xmlSetProp (a, BAD_CAST "href", BAD_CAST str);
g_free (str);
}
}
return ul;
}
static void
get_for_cnclist (G_GNUC_UNUSED WebServer *webserver, SoupMessage *msg, gboolean is_authenticated)
{
xmlNodePtr ul;
SoupBuffer *buffer;
ul = cnc_ul (is_authenticated);
soup_message_headers_replace (msg->response_headers,
"Content-Type", "text/html");
xmlBufferPtr buf;
buf = xmlBufferCreate ();
xmlNodeDump (buf, NULL, ul, 1, 1);
xmlFreeNode (ul);
buffer = soup_buffer_new (SOUP_MEMORY_TEMPORARY, (gchar *) xmlBufferContent (buf),
strlen ((gchar *) xmlBufferContent (buf)));
soup_message_body_append_buffer (msg->response_body, buffer);
soup_buffer_free (buffer);
xmlBufferFree (buf);
soup_message_set_status (msg, SOUP_STATUS_OK);
}
/*
* GET for the /~console path
*/
static void
get_for_console (WebServer *server, SoupMessage *msg)
{
HtmlDoc *hdoc;
xmlChar *xstr;
SoupBuffer *buffer;
gsize size;
xmlNodePtr div = NULL, node;
hdoc = create_new_htmldoc (server, NULL);
xmlNewChild (hdoc->content, NULL, BAD_CAST "h1", BAD_CAST _("SQL console:"));
div = xmlNewChild (hdoc->content, NULL, BAD_CAST "div", NULL);
xmlSetProp (div, BAD_CAST "id", BAD_CAST "terminal");
div = xmlNewChild (div, NULL, BAD_CAST "div", BAD_CAST "");
xmlSetProp (div, BAD_CAST "id", BAD_CAST "irb");
div = xmlNewChild (hdoc->footer, NULL, BAD_CAST "input", NULL);
xmlSetProp (div, BAD_CAST "class", BAD_CAST "keyboard-selector-input");
xmlSetProp (div, BAD_CAST "type", BAD_CAST "text");
xmlSetProp (div, BAD_CAST "id", BAD_CAST "irb_input");
xmlSetProp (div, BAD_CAST "autocomplete", BAD_CAST "off");
node = xmlNewChild (hdoc->head, NULL, BAD_CAST "script", BAD_CAST " ");
xmlSetProp (node, BAD_CAST "type", BAD_CAST "text/javascript");
xmlSetProp (node, BAD_CAST "src", BAD_CAST "/jquery.js");
node = xmlNewChild (hdoc->head, NULL, BAD_CAST "script", BAD_CAST " ");
xmlSetProp (node, BAD_CAST "type", BAD_CAST "text/javascript");
xmlSetProp (node, BAD_CAST "src", BAD_CAST "/jquery-ui.js");
node = xmlNewChild (hdoc->head, NULL, BAD_CAST "script", BAD_CAST " ");
xmlSetProp (node, BAD_CAST "type", BAD_CAST "text/javascript");
xmlSetProp (node, BAD_CAST "src", BAD_CAST "/mouseapp_2.js");
node = xmlNewChild (hdoc->head, NULL, BAD_CAST "script", BAD_CAST " ");
xmlSetProp (node, BAD_CAST "type", BAD_CAST "text/javascript");
xmlSetProp (node, BAD_CAST "src", BAD_CAST "/mouseirb_2.js");
node = xmlNewChild (hdoc->head, NULL, BAD_CAST "script", BAD_CAST " ");
xmlSetProp (node, BAD_CAST "type", BAD_CAST "text/javascript");
xmlSetProp (node, BAD_CAST "src", BAD_CAST "/irb.js");
node = xmlNewChild (hdoc->head, NULL, BAD_CAST "script", BAD_CAST " ");
xmlSetProp (node, BAD_CAST "type", BAD_CAST "text/javascript");
xmlSetProp (node, BAD_CAST "src", BAD_CAST "/cnc.js");
node = xmlNewChild (hdoc->head, NULL, BAD_CAST "link", BAD_CAST "");
xmlSetProp(node, BAD_CAST "href", (xmlChar*)"/irb.css");
xmlSetProp(node, BAD_CAST "rel", (xmlChar*)"stylesheet");
xmlSetProp(node, BAD_CAST "type", (xmlChar*)"text/css");
soup_message_headers_replace (msg->response_headers,
"Content-Type", "text/html");
xstr = html_doc_to_string (hdoc, &size);
buffer = soup_buffer_new_with_owner (xstr, size, xstr, (GDestroyNotify)xmlFree);
soup_message_body_append_buffer (msg->response_body, buffer);
soup_buffer_free (buffer);
html_doc_free (hdoc);
soup_message_set_status (msg, SOUP_STATUS_OK);
}
static gboolean compute_all_objects_content (HtmlDoc *hdoc, const ConnectionSetting *cs,
const gchar *human_obj_type,
const gchar *human_obj_type_in_schema,
const gchar *table_name,
const gchar *obj_prefix,
const gchar *filter,
GError **error);
static gboolean compute_all_triggers_content (HtmlDoc *hdoc, const ConnectionSetting *cs, GError **error);
static gboolean compute_object_content (HtmlDoc *hdoc, WebServer *webserver, const ConnectionSetting *cs,
const gchar *schema, const gchar *name, GError **error);
/*
* GET method for a connection
*/
static gboolean
get_for_cnc (WebServer *webserver, SoupMessage *msg, const ConnectionSetting *cs, gchar **extra, GError **error)
{
gboolean retval = FALSE;
HtmlDoc *hdoc;
xmlChar *xstr;
SoupBuffer *buffer;
gsize size;
hdoc = create_new_htmldoc (webserver, cs);
if (!extra || !strcmp (extra[0], "~tables")) {
if (! compute_all_objects_content (hdoc, cs,
_("Tables"), _("Tables in the '%s' schema"),
"_tables", "table", "table_type LIKE \"%TABLE%\"", error))
goto onerror;
}
else if (!strcmp (extra[0], "~views")) {
if (! compute_all_objects_content (hdoc, cs,
_("Views"), _("Views in the '%s' schema"),
"_tables", "table", "table_type LIKE \"%VIEW%\"", error))
goto onerror;
}
else if (!strcmp (extra[0], "~triggers")) {
if (! compute_all_triggers_content (hdoc, cs, error))
goto onerror;
}
else {
/* extra [0] has to be a schema */
if (extra [1]) {
if (! compute_object_content (hdoc, webserver, cs, extra [0], extra [1], error))
goto onerror;
}
else
xmlNewChild (hdoc->content, NULL, BAD_CAST "h1", BAD_CAST "Not yet implemented");
}
soup_message_headers_replace (msg->response_headers,
"Content-Type", "text/html");
xstr = html_doc_to_string (hdoc, &size);
buffer = soup_buffer_new_with_owner (xstr, size, xstr, (GDestroyNotify)xmlFree);
soup_message_body_append_buffer (msg->response_body, buffer);
soup_buffer_free (buffer);
soup_message_set_status (msg, SOUP_STATUS_OK);
retval = TRUE;
onerror:
html_doc_free (hdoc);
return retval;
}
static void
meta_table_column_foreach_attribute_func (const gchar *att_name, const GValue *value, GString **string)
{
if (!strcmp (att_name, GDA_ATTRIBUTE_AUTO_INCREMENT) &&
(G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) &&
g_value_get_boolean (value)) {
if (*string) {
g_string_append (*string, ", ");
g_string_append (*string, _("Auto increment"));
}
else
*string = g_string_new (_("Auto increment"));
}
}
static gchar *meta_struct_dump_as_graph (const ConnectionSetting *cs, GdaMetaStruct *mstruct,
GdaMetaDbObject *central_dbo, GError **error);
static gboolean
compute_table_details (const ConnectionSetting *cs, HtmlDoc *hdoc, WebServer *webserver,
GdaMetaStruct *mstruct, GdaMetaDbObject *dbo, GError **error)
{
gchar *tmp;
xmlNodePtr div, table, tr, td;
GdaMetaTable *mt = GDA_META_TABLE (dbo);
GSList *list;
GdaMetaStore *store;
tmp = g_strdup_printf (_("Columns for the '%s' table:"), dbo->obj_short_name);
xmlNewChild (hdoc->content, NULL, BAD_CAST "h1", BAD_CAST tmp);
g_free (tmp);
div = xmlNewChild (hdoc->content, NULL, BAD_CAST "div", NULL);
table = xmlNewChild (div, NULL, BAD_CAST "table", NULL);
xmlSetProp (table, BAD_CAST "class", (xmlChar*) BAD_CAST "ctable");
tr = xmlNewChild (table, NULL, BAD_CAST "tr", NULL);
td = xmlNewChild (tr, NULL, BAD_CAST "th", BAD_CAST _("Column"));
td = xmlNewChild (tr, NULL, BAD_CAST "th", BAD_CAST _("Type"));
td = xmlNewChild (tr, NULL, BAD_CAST "th", BAD_CAST _("Nullable"));
td = xmlNewChild (tr, NULL, BAD_CAST "th", BAD_CAST _("Default"));
td = xmlNewChild (tr, NULL, BAD_CAST "th", BAD_CAST _("Extra"));
for (list = mt->columns; list; list = list->next) {
GdaMetaTableColumn *tcol = GDA_META_TABLE_COLUMN (list->data);
GString *string = NULL;
tr = xmlNewChild (table, NULL, BAD_CAST "tr", NULL);
td = xmlNewChild (tr, NULL, BAD_CAST "td", BAD_CAST tcol->column_name);
if (tcol->pkey)
xmlSetProp (td, BAD_CAST "class", BAD_CAST "pkey");
td = xmlNewChild (tr, NULL, BAD_CAST "td", BAD_CAST tcol->column_type);
td = xmlNewChild (tr, NULL, BAD_CAST "td", tcol->nullok ? BAD_CAST _("yes") : BAD_CAST _("no"));
td = xmlNewChild (tr, NULL, BAD_CAST "td", BAD_CAST tcol->default_value);
gda_meta_table_column_foreach_attribute (tcol,
(GdaAttributesManagerFunc) meta_table_column_foreach_attribute_func, &string);
if (string) {
td = xmlNewChild (tr, NULL, BAD_CAST "td", BAD_CAST string->str);
g_string_free (string, TRUE);
}
else
td = xmlNewChild (tr, NULL, BAD_CAST "td", NULL);
}
/* finished if we don't have a table */
if (dbo->obj_type != GDA_META_DB_TABLE)
return TRUE;
/* show primary key */
GdaMetaTable *dbo_table = GDA_META_TABLE (dbo);
#ifdef NONO
if (dbo_table->pk_cols_nb > 0) {
gint ipk;
xmlNodePtr ul = NULL;
xmlNewChild (hdoc->content, NULL, BAD_CAST "h1", BAD_CAST _("Primary key:"));
div = xmlNewChild (hdoc->content, NULL, BAD_CAST "div", NULL);
for (ipk = 0; ipk < dbo_table->pk_cols_nb; ipk++) {
GdaMetaTableColumn *tcol;
if (!ul)
ul = xmlNewChild (div, NULL, BAD_CAST "ul", NULL);
tcol = g_slist_nth_data (dbo_table->columns, ipk);
xmlNewChild (ul, NULL, BAD_CAST "li", tcol->column_name);
}
}
#endif
/* Add objects depending on this table */
if (!gda_meta_struct_complement_depend (mstruct, dbo, error))
return FALSE;
/* Add tables from which this one depends */
GValue *v0, *v1;
GdaDataModel *model;
g_object_get (G_OBJECT (mstruct), "meta-store", &store, NULL);
g_value_set_string ((v0 = gda_value_new (G_TYPE_STRING)), dbo->obj_schema);
g_value_set_string ((v1 = gda_value_new (G_TYPE_STRING)), dbo->obj_name);
model = gda_meta_store_extract (store, "SELECT table_catalog, table_schema, table_name FROM "
"_referential_constraints WHERE "
"ref_table_schema = ##tschema::string AND ref_table_name = ##tname::string",
error, "tschema", v0, "tname", v1, NULL);
gda_value_free (v0);
gda_value_free (v1);
g_object_unref (store);
if (model) {
gint i, nrows;
nrows = gda_data_model_get_n_rows (model);
for (i = 0; i < nrows; i++) {
const GValue *cv0, *cv1 = NULL, *cv2 = NULL;
cv0 = gda_data_model_get_value_at (model, 0, i, NULL);
if (cv0) {
cv1 = gda_data_model_get_value_at (model, 1, i, NULL);
if (cv1)
cv2 = gda_data_model_get_value_at (model, 2, i, NULL);
}
if (cv0 && cv1 && cv2)
gda_meta_struct_complement (mstruct, GDA_META_DB_TABLE, cv0, cv1, cv2, NULL);
}
g_object_unref (model);
}
/* create a graph */
gchar *graph, *tmp_filename = NULL;
xmlNodePtr map_node = NULL;
graph = meta_struct_dump_as_graph (cs, mstruct, dbo, NULL);
if (graph) {
gchar *dotname, *suffix;
static gint counter = 0;
suffix = g_strdup_printf (".gda_graph_tmp-%d.dot", ++counter);
dotname = g_build_filename (g_get_tmp_dir (), suffix, NULL);
g_free (suffix);
if (g_file_set_contents (dotname, graph, -1, error)) {
gchar *pngname, *mapname;
gchar *argv[] = {"dot", "-Tpng", "-o", NULL, "-Tcmapx", "-o", NULL, NULL, NULL};
suffix = g_strdup_printf (".gda_graph_tmp-%d", counter);
pngname = g_build_filename (g_get_tmp_dir (), suffix, NULL);
g_free (suffix);
suffix = g_strdup_printf (".gda_graph_tmp-%d.map", counter);
mapname = g_build_filename (g_get_tmp_dir (), suffix, NULL);
g_free (suffix);
argv[3] = pngname;
argv[6] = mapname;
argv[7] = dotname;
if (g_spawn_sync (NULL, argv, NULL,
G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
NULL, NULL,
NULL, NULL, NULL, NULL)) {
xmlDocPtr map;
map = xmlParseFile (mapname);
if (map) {
map_node = xmlDocGetRootElement (map);
xmlUnlinkNode (map_node);
xmlFreeDoc (map);
}
gchar *file_data;
gsize file_data_len;
if (g_file_get_contents (pngname, &file_data, &file_data_len, NULL)) {
tmp_filename = g_strdup_printf ("~tmp/g%d", counter);
tmp_resource_add (webserver, tmp_filename, file_data, file_data_len);
}
}
g_unlink(pngname);
g_free (pngname);
g_unlink(mapname);
g_free (mapname);
}
g_unlink(dotname);
g_free (dotname);
g_free (graph);
}
if (tmp_filename) {
xmlNodePtr obj;
gchar *tmp;
xmlNewChild (hdoc->content, NULL, BAD_CAST "h1", BAD_CAST _("Relations:"));
div = xmlNewChild (hdoc->content, NULL, BAD_CAST "div", NULL);
if (map_node)
xmlAddChild (div, map_node);
xmlSetProp (div, BAD_CAST "class", BAD_CAST "graph");
obj = xmlNewChild (div, NULL, BAD_CAST "img", NULL);
tmp = g_strdup_printf ("/%s", tmp_filename);
xmlSetProp (obj, BAD_CAST "src", BAD_CAST tmp);
xmlSetProp (obj, BAD_CAST "usemap", BAD_CAST "#G");
g_free (tmp);
g_free (tmp_filename);
}
else {
/* list foreign keys as we don't have a graph */
if (dbo_table->fk_list) {
xmlNewChild (hdoc->content, NULL, BAD_CAST "h1", BAD_CAST _("Foreign keys:"));
GSList *list;
for (list = dbo_table->fk_list; list; list = list->next) {
GdaMetaTableForeignKey *tfk = GDA_META_TABLE_FOREIGN_KEY (list->data);
GdaMetaDbObject *fkdbo = tfk->depend_on;
xmlNodePtr ul = NULL;
gint ifk;
div = xmlNewChild (hdoc->content, NULL, BAD_CAST "div", NULL);
for (ifk = 0; ifk < tfk->cols_nb; ifk++) {
gchar *tmp;
if (!ul) {
tmp = g_strdup_printf (_("To '%s':"), fkdbo->obj_short_name);
ul = xmlNewChild (div, NULL, BAD_CAST "ul", BAD_CAST tmp);
g_free (tmp);
}
tmp = g_strdup_printf ("%s --> %s.%s",
tfk->fk_names_array[ifk],
fkdbo->obj_short_name, tfk->ref_pk_names_array[ifk]);
xmlNewChild (ul, NULL, BAD_CAST "li", BAD_CAST tmp);
g_free (tmp);
}
}
}
}
return TRUE;
}
/*
* Creates a new graph (in the GraphViz syntax) representation of @mstruct.
*
* Returns: a new string, or %NULL if an error occurred.
*/
static gchar *
meta_struct_dump_as_graph (const ConnectionSetting *cs, GdaMetaStruct *mstruct, GdaMetaDbObject *central_dbo,
G_GNUC_UNUSED GError **error)
{
GString *string;
gchar *result;
gchar *rfc_cnc_name;
g_return_val_if_fail (GDA_IS_META_STRUCT (mstruct), NULL);
rfc_cnc_name = gda_rfc1738_encode (cs->name);
string = g_string_new ("digraph G {\nrankdir = LR;\ndpi = 70;\nfontname = Helvetica;\nnode [shape = plaintext];\n");
GSList *dbo_list, *list;
dbo_list = gda_meta_struct_get_all_db_objects (mstruct);
for (list = dbo_list; list; list = list->next) {
gchar *objname, *fullname;
GdaMetaDbObject *dbo = GDA_META_DB_OBJECT (list->data);
GSList *list;
/* obj human readable name, and full name */
fullname = g_strdup_printf ("%s.%s.%s", dbo->obj_catalog, dbo->obj_schema, dbo->obj_name);
if (dbo->obj_short_name)
objname = g_strdup (dbo->obj_short_name);
else if (dbo->obj_schema)
objname = g_strdup_printf ("%s.%s", dbo->obj_schema, dbo->obj_name);
else
objname = g_strdup (dbo->obj_name);
/* URL */
gchar *e0, *e1, *url;
e0 = gda_rfc1738_encode (dbo->obj_schema);
e1 = gda_rfc1738_encode (dbo->obj_name);
url = g_strdup_printf ("/%s/%s/%s", rfc_cnc_name, e0, e1);
g_free (e0);
g_free (e1);
/* node */
switch (dbo->obj_type) {
case GDA_META_DB_UNKNOWN:
break;
case GDA_META_DB_TABLE:
g_string_append_printf (string, "\"%s\" [URL=\"%s\", tooltip=\"%s\", label=<<TABLE BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\">", fullname, url, objname);
if (dbo == central_dbo)
g_string_append_printf (string, "<TR><TD COLSPAN=\"2\" BGCOLOR=\"#729FCF;\" BORDER=\"3\">%s</TD></TR>", objname);
else
g_string_append_printf (string, "<TR><TD COLSPAN=\"2\" BGCOLOR=\"lightgrey\" BORDER=\"1\">%s</TD></TR>", objname);
break;
case GDA_META_DB_VIEW:
g_string_append_printf (string, "\"%s\" [URL=\"%s\", tooltip=\"%s\", label=<<TABLE BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\">", fullname, url, objname);
g_string_append_printf (string, "<TR><TD BGCOLOR=\"yellow\" BORDER=\"1\">%s</TD></TR>", objname);
break;
default:
TO_IMPLEMENT;
g_string_append_printf (string, "\"%s\" [ shape = note label = \"%s\" ];", fullname, objname);
break;
}
g_free (url);
/* columns, only for tables */
if (dbo->obj_type == GDA_META_DB_TABLE) {
GdaMetaTable *mt = GDA_META_TABLE (dbo);
GSList *depend_dbo_list = NULL;
for (list = mt->columns; list; list = list->next) {
GdaMetaTableColumn *tcol = GDA_META_TABLE_COLUMN (list->data);
if (tcol->pkey)
g_string_append_printf (string, "<TR><TD ALIGN=\"left\"><FONT COLOR=\"blue\">%s</FONT></TD></TR>",
tcol->column_name);
else
g_string_append_printf (string, "<TR><TD ALIGN=\"left\">%s</TD></TR>",
tcol->column_name);
}
g_string_append (string, "</TABLE>>];\n");
/* foreign keys */
for (list = mt->fk_list; list; list = list->next) {
GdaMetaTableForeignKey *tfk = GDA_META_TABLE_FOREIGN_KEY (list->data);
GString *st = NULL;
gint fki;
for (fki = 0; fki < tfk->cols_nb; fki++) {
if (tfk->fk_names_array && tfk->ref_pk_names_array) {
if (!st)
st = g_string_new ("");
else
g_string_append (st, "\\n");
g_string_append_printf (st, "%s = %s",
tfk->fk_names_array [fki],
tfk->ref_pk_names_array [fki]);
}
else
break;
}
if (tfk->depend_on->obj_type != GDA_META_DB_UNKNOWN) {
g_string_append_printf (string, "\"%s\" -> \"%s.%s.%s\" [fontsize=12, label=\"%s\"];\n",
fullname,
tfk->depend_on->obj_catalog, tfk->depend_on->obj_schema,
tfk->depend_on->obj_name,
st ? st->str : "");
depend_dbo_list = g_slist_prepend (depend_dbo_list, tfk->depend_on);
}
if (st)
g_string_free (st, TRUE);
}
/* dependencies other than foreign keys */
for (list = dbo->depend_list; list; list = list->next) {
if (!g_slist_find (depend_dbo_list, list->data)) {
GdaMetaDbObject *dep_dbo = GDA_META_DB_OBJECT (list->data);
if (dep_dbo->obj_type != GDA_META_DB_UNKNOWN)
g_string_append_printf (string, "\"%s\" -> \"%s.%s.%s\";\n",
fullname,
dep_dbo->obj_catalog, dep_dbo->obj_schema,
dep_dbo->obj_name);
}
}
g_slist_free (depend_dbo_list);
}
else if (dbo->obj_type == GDA_META_DB_VIEW) {
GdaMetaTable *mt = GDA_META_TABLE (dbo);
for (list = mt->columns; list; list = list->next) {
GdaMetaTableColumn *tcol = GDA_META_TABLE_COLUMN (list->data);
g_string_append_printf (string, "<TR><TD ALIGN=\"left\">%s</TD></TR>", tcol->column_name);
}
g_string_append (string, "</TABLE>>];\n");
/* dependencies */
for (list = dbo->depend_list; list; list = list->next) {
GdaMetaDbObject *ddbo = GDA_META_DB_OBJECT (list->data);
if (ddbo->obj_type != GDA_META_DB_UNKNOWN)
g_string_append_printf (string, "\"%s\" -> \"%s.%s.%s\";\n", fullname,
ddbo->obj_catalog, ddbo->obj_schema,
ddbo->obj_name);
}
}
g_free (objname);
g_free (fullname);
}
g_string_append_c (string, '}');
g_slist_free (dbo_list);
result = string->str;
g_string_free (string, FALSE);
return result;
}
static gboolean
compute_view_details (G_GNUC_UNUSED const ConnectionSetting *cs, HtmlDoc *hdoc,
G_GNUC_UNUSED GdaMetaStruct *mstruct, GdaMetaDbObject *dbo,
G_GNUC_UNUSED GError **error)
{
GdaMetaView *view = GDA_META_VIEW (dbo);
if (view->view_def) {
xmlNodePtr div, code;
xmlNewChild (hdoc->content, NULL, BAD_CAST "h1", BAD_CAST _("View definition:"));
div = xmlNewChild (hdoc->content, NULL, BAD_CAST "div", NULL);
code = xmlNewChild (div, NULL, BAD_CAST "code", BAD_CAST view->view_def);
xmlSetProp (code, BAD_CAST "class", BAD_CAST "ccode");
}
return TRUE;
}
/*
* compute information for an object assuming it's a trigger
*
* Returns: TRUE if the object was really a trigger
*/
static gboolean
compute_trigger_content (HtmlDoc *hdoc, G_GNUC_UNUSED WebServer *webserver, const ConnectionSetting *cs,
const gchar *schema, const gchar *name, GError **error)
{
GdaMetaStore *store;
GdaDataModel *model;
GValue *v0, *v1;
GValue *tschema = NULL, *tname = NULL;
gint i, nrows;
xmlNodePtr code, div = NULL, sdiv, ul, li;
store = gda_connection_get_meta_store (cs->cnc);
g_value_set_string ((v0 = gda_value_new (G_TYPE_STRING)), schema);
g_value_set_string ((v1 = gda_value_new (G_TYPE_STRING)), name);
model = gda_meta_store_extract (store, "SELECT trigger_short_name, action_statement, event_manipulation, "
"event_object_schema, event_object_table, trigger_comments, "
"action_orientation, condition_timing FROM _triggers "
"WHERE trigger_schema = ##tschema::string AND trigger_name = ##tname::string "
"ORDER BY event_object_schema, event_object_table, event_manipulation",
error, "tschema", v0, "tname", v1, NULL);
gda_value_free (v0);
gda_value_free (v1);
if (!model)
return FALSE;
nrows = gda_data_model_get_n_rows (model);
if (nrows == 0) {
g_object_unref (model);
return FALSE;
}
for (i = 0; i < nrows; i++) {
const GValue *cv0, *cv1, *cv2, *cv3, *cv4, *cv5, *cv6, *cv7;
gchar *tmp;
if (! (cv0 = gda_data_model_get_value_at (model, 0, i, error)))
break;
if (! (cv1 = gda_data_model_get_value_at (model, 1, i, error)))
break;
if (! (cv2 = gda_data_model_get_value_at (model, 2, i, error)))
break;
if (! (cv3 = gda_data_model_get_value_at (model, 3, i, error)))
break;
if (! (cv4 = gda_data_model_get_value_at (model, 4, i, error)))
break;
if (! (cv5 = gda_data_model_get_value_at (model, 5, i, error)))
break;
if (! (cv6 = gda_data_model_get_value_at (model, 6, i, error)))
break;
if (! (cv7 = gda_data_model_get_value_at (model, 7, i, error)))
break;
if ((!tschema || gda_value_differ (tschema, cv3)) ||
(!tname || gda_value_differ (tname, cv4))) {
if (tschema)
xmlNewChild (div, NULL, BAD_CAST "br", NULL);
if (tschema)
gda_value_free (tschema);
if (tname)
gda_value_free (tname);
tschema = gda_value_copy (cv3);
tname = gda_value_copy (cv4);
tmp = g_strdup_printf (_("Trigger '%s' for the '%s.%s' table:"),
g_value_get_string (cv0),
g_value_get_string (tschema),
g_value_get_string (tname));
xmlNewChild (hdoc->content, NULL, BAD_CAST "h1", BAD_CAST tmp);
g_free (tmp);
div = xmlNewChild (hdoc->content, NULL, BAD_CAST "div", NULL);
}
if (G_VALUE_TYPE (cv5) != GDA_TYPE_NULL)
tmp = g_strdup_printf ("On %s (%s):", g_value_get_string (cv2), g_value_get_string (cv5));
else
tmp = g_strdup_printf ("On %s:", g_value_get_string (cv2));
xmlNewChild (div, NULL, BAD_CAST "h2", BAD_CAST tmp);
g_free (tmp);
sdiv = xmlNewChild (div, NULL, BAD_CAST "div", NULL);
ul = xmlNewChild (sdiv, NULL, BAD_CAST "ul", NULL);
tmp = g_strdup_printf (_("Trigger fired for: %s"), g_value_get_string (cv6));
li = xmlNewChild (ul, NULL, BAD_CAST "li", BAD_CAST tmp);
g_free (tmp);
tmp = g_strdup_printf (_("Time at which the trigger is fired: %s"), g_value_get_string (cv7));
li = xmlNewChild (ul, NULL, BAD_CAST "li", BAD_CAST tmp);
g_free (tmp);
li = xmlNewChild (ul, NULL, BAD_CAST "li", BAD_CAST _("Action:"));
code = xmlNewChild (li, NULL, BAD_CAST "code", BAD_CAST g_value_get_string (cv1));
xmlSetProp (code, BAD_CAST "class", BAD_CAST "ccode");
}
g_object_unref (model);
return TRUE;
}
/*
* Give details about a database object
*/
static gboolean
compute_object_content (HtmlDoc *hdoc, WebServer *webserver, const ConnectionSetting *cs,
const gchar *schema, const gchar *name, GError **error)
{
GdaMetaStruct *mstruct;
GdaMetaDbObject *dbo;
GValue *v0, *v1;
gboolean retval;
g_value_take_string ((v0 = gda_value_new (G_TYPE_STRING)),
gda_sql_identifier_quote (schema, cs->cnc, NULL, FALSE, FALSE));
g_value_take_string ((v1 = gda_value_new (G_TYPE_STRING)),
gda_sql_identifier_quote (name, cs->cnc, NULL, FALSE, FALSE));
mstruct = gda_meta_struct_new (gda_connection_get_meta_store (cs->cnc),
GDA_META_STRUCT_FEATURE_ALL);
dbo = gda_meta_struct_complement (mstruct, GDA_META_DB_UNKNOWN, NULL, v0, v1, error);
gda_value_free (v0);
gda_value_free (v1);
if (!dbo) {
if (compute_trigger_content (hdoc, webserver, cs, schema, name, error))
return TRUE;
TO_IMPLEMENT;
return FALSE;
}
switch (dbo->obj_type) {
case GDA_META_DB_TABLE:
retval = compute_table_details (cs, hdoc, webserver, mstruct, dbo, error);
break;
case GDA_META_DB_VIEW:
retval = compute_table_details (cs, hdoc, webserver, mstruct, dbo, error);
if (retval)
retval = compute_view_details (cs, hdoc, mstruct, dbo, error);
break;
default:
TO_IMPLEMENT;
}
g_object_unref (mstruct);
return TRUE;
}
/*
* Lists all objects of a type (tables, view, ...)
*/
static gboolean
compute_all_objects_content (HtmlDoc *hdoc, const ConnectionSetting *cs,
const gchar *human_obj_type,
const gchar *human_obj_type_in_schema,
const gchar *table_name,
const gchar *obj_prefix,
const gchar *filter,
GError **error)
{
gboolean retval = FALSE;
gchar *rfc_cnc_name;
GdaMetaStore *store;
GdaDataModel *model;
gint i, nrows;
gchar *sql;
GValue *schema = NULL;
xmlNodePtr ul, li, a, div = NULL;
gboolean content_added = FALSE;
rfc_cnc_name = gda_rfc1738_encode (cs->name);
store = gda_connection_get_meta_store (cs->cnc);
/* objects directly accessible */
if (filter)
sql = g_strdup_printf ("SELECT %s_schema, %s_name FROM %s WHERE %s "
"AND %s_short_name != %s_full_name ORDER BY %s_schema, %s_name",
obj_prefix, obj_prefix, table_name, filter, obj_prefix, obj_prefix,
obj_prefix, obj_prefix);
else
sql = g_strdup_printf ("SELECT %s_schema, %s_name FROM %s WHERE "
"%s_short_name != %s_full_name ORDER BY %s_schema, %s_name",
obj_prefix, obj_prefix, table_name, obj_prefix, obj_prefix,
obj_prefix, obj_prefix);
model = gda_meta_store_extract (store, sql, error, NULL);
g_free (sql);
if (!model)
goto out;
nrows = gda_data_model_get_n_rows (model);
if (nrows > 0) {
xmlNewChild (hdoc->content, NULL, BAD_CAST "h1", BAD_CAST human_obj_type);
div = xmlNewChild (hdoc->content, NULL, BAD_CAST "div", NULL);
xmlSetProp (div, BAD_CAST "class", BAD_CAST "clist");
ul = xmlNewChild (div, NULL, BAD_CAST "ul", NULL);
content_added = TRUE;
}
for (i = 0; i < nrows; i++) {
const GValue *cv0, *cv1;
gchar *tmp, *e0, *e1;
cv0 = gda_data_model_get_value_at (model, 0, i, error);
if (!cv0)
goto out;
cv1 = gda_data_model_get_value_at (model, 1, i, error);
if (!cv1)
goto out;
li = xmlNewChild (ul, NULL, BAD_CAST "li", NULL);
a = xmlNewChild (li, NULL, BAD_CAST "a", BAD_CAST g_value_get_string (cv1));
e0 = gda_rfc1738_encode (g_value_get_string (cv0));
e1 = gda_rfc1738_encode (g_value_get_string (cv1));
tmp = g_strdup_printf ("/%s/%s/%s", rfc_cnc_name, e0, e1);
g_free (e0);
g_free (e1);
xmlSetProp (a, BAD_CAST "href", BAD_CAST tmp);
g_free (tmp);
tmp = g_strdup_printf ("%s.%s", g_value_get_string (cv0), g_value_get_string (cv1));
xmlSetProp (a, BAD_CAST "title", BAD_CAST tmp);
g_free (tmp);
}
if (nrows > 0)
xmlNewChild (div, NULL, BAD_CAST "br", NULL);
g_object_unref (model);
/* objects listed by schema */
if (filter)
sql = g_strdup_printf ("SELECT %s_schema, %s_name FROM %s WHERE %s "
"ORDER BY %s_schema, %s_name",
obj_prefix, obj_prefix, table_name, filter, obj_prefix, obj_prefix);
else
sql = g_strdup_printf ("SELECT %s_schema, %s_name FROM %s "
"ORDER BY %s_schema, %s_name",
obj_prefix, obj_prefix, table_name, obj_prefix, obj_prefix);
model = gda_meta_store_extract (store, sql, error, NULL);
g_free (sql);
if (!model)
goto out;
nrows = gda_data_model_get_n_rows (model);
for (i = 0; i < nrows; i++) {
const GValue *cv0, *cv1;
gchar *tmp, *e0, *e1;
cv0 = gda_data_model_get_value_at (model, 0, i, error);
if (!cv0)
goto out;
cv1 = gda_data_model_get_value_at (model, 1, i, error);
if (!cv1)
goto out;
if (!schema || gda_value_differ (schema, cv0)) {
gchar *tmp;
if (schema) {
xmlNewChild (div, NULL, BAD_CAST "br", NULL);
gda_value_free (schema);
}
schema = gda_value_copy (cv0);
tmp = g_strdup_printf (human_obj_type_in_schema, g_value_get_string (schema));
xmlNewChild (hdoc->content, NULL, BAD_CAST "h1", BAD_CAST tmp);
g_free (tmp);
content_added = TRUE;
div = xmlNewChild (hdoc->content, NULL, BAD_CAST "div", NULL);
xmlSetProp (div, BAD_CAST "class", BAD_CAST "clist");
ul = xmlNewChild (div, NULL, BAD_CAST "ul", NULL);
}
li = xmlNewChild (ul, NULL, BAD_CAST "li", NULL);
a = xmlNewChild (li, NULL, BAD_CAST "a", BAD_CAST g_value_get_string (cv1));
e0 = gda_rfc1738_encode (g_value_get_string (cv0));
e1 = gda_rfc1738_encode (g_value_get_string (cv1));
tmp = g_strdup_printf ("/%s/%s/%s", rfc_cnc_name, e0, e1);
g_free (e0);
g_free (e1);
xmlSetProp (a, BAD_CAST "href", BAD_CAST tmp);
g_free (tmp);
}
if (nrows != 0)
xmlNewChild (div, NULL, BAD_CAST "br", NULL);
retval = TRUE;
if (! content_added)
xmlNewChild (hdoc->content, NULL, BAD_CAST "br", NULL);
out:
if (schema)
gda_value_free (schema);
if (model)
g_object_unref (model);
g_free (rfc_cnc_name);
return retval;
}
/*
* Lists all objects of a type (tables, view, ...)
*/
static gboolean
compute_all_triggers_content (HtmlDoc *hdoc, const ConnectionSetting *cs, GError **error)
{
gboolean retval = FALSE;
gchar *rfc_cnc_name;
GdaMetaStore *store;
GdaDataModel *model;
gint i, nrows;
GValue *schema = NULL, *tschema = NULL, *tname = NULL;
xmlNodePtr sul, li, a, div = NULL, sdiv = NULL;
gboolean content_added = FALSE;
rfc_cnc_name = gda_rfc1738_encode (cs->name);
store = gda_connection_get_meta_store (cs->cnc);
model = gda_meta_store_extract (store, "SELECT trigger_schema, trigger_name, event_manipulation, "
"event_object_schema, event_object_table FROM _triggers WHERE "
"trigger_short_name != trigger_full_name "
"ORDER BY trigger_schema, trigger_name, event_object_schema, "
"event_object_table, event_manipulation",
error, NULL);
if (!model)
goto out;
nrows = gda_data_model_get_n_rows (model);
if (nrows > 0) {
xmlNewChild (hdoc->content, NULL, BAD_CAST "h1", BAD_CAST _("Triggers:"));
div = xmlNewChild (hdoc->content, NULL, BAD_CAST "div", NULL);
xmlSetProp (div, BAD_CAST "class", BAD_CAST "clist");
content_added = TRUE;
}
for (i = 0; i < nrows; i++) {
const GValue *cv0, *cv1, *cv2, *cv3, *cv4;
gchar *tmp, *e0, *e1;
if (! (cv0 = gda_data_model_get_value_at (model, 0, i, error)))
goto out;
if (! (cv1 = gda_data_model_get_value_at (model, 1, i, error)))
goto out;
if (! (cv2 = gda_data_model_get_value_at (model, 2, i, error)))
goto out;
if (! (cv3 = gda_data_model_get_value_at (model, 3, i, error)))
goto out;
if (! (cv4 = gda_data_model_get_value_at (model, 4, i, error)))
goto out;
if ((!tschema || gda_value_differ (tschema, cv3)) ||
(!tname || gda_value_differ (tname, cv4))) {
gchar *tmp;
if (tschema)
xmlNewChild (sdiv, NULL, BAD_CAST "br", NULL);
if (tschema)
gda_value_free (tschema);
if (tname)
gda_value_free (tname);
tschema = gda_value_copy (cv3);
tname = gda_value_copy (cv4);
tmp = g_strdup_printf (_("For the '%s.%s' table:"), g_value_get_string (tschema),
g_value_get_string (tname));
xmlNewChild (div, NULL, BAD_CAST "h2", BAD_CAST tmp);
g_free (tmp);
sdiv = xmlNewChild (div, NULL, BAD_CAST "div", NULL);
xmlSetProp (sdiv, BAD_CAST "class", BAD_CAST "clist");
sul = xmlNewChild (sdiv, NULL, BAD_CAST "ul", NULL);
}
li = xmlNewChild (sul, NULL, BAD_CAST "li", NULL);
tmp = g_strdup_printf ("%s (%s)", g_value_get_string (cv1), g_value_get_string (cv2));
a = xmlNewChild (li, NULL, BAD_CAST "a", BAD_CAST tmp);
g_free (tmp);
e0 = gda_rfc1738_encode (g_value_get_string (cv0));
e1 = gda_rfc1738_encode (g_value_get_string (cv1));
tmp = g_strdup_printf ("/%s/%s/%s", rfc_cnc_name, e0, e1);
g_free (e0);
g_free (e1);
xmlSetProp (a, BAD_CAST "href", BAD_CAST tmp);
g_free (tmp);
}
if (nrows > 0)
xmlNewChild (sdiv, NULL, BAD_CAST "br", NULL);
g_object_unref (model);
if (tschema) {
gda_value_free (tschema);
tschema = NULL;
}
if (tname) {
gda_value_free (tname);
tname = NULL;
}
/* objects listed by schema */
model = gda_meta_store_extract (store, "SELECT trigger_schema, trigger_name, event_manipulation, "
"event_object_schema, event_object_table FROM _triggers "
"ORDER BY trigger_schema, trigger_name, event_object_schema, "
"event_object_table, event_manipulation",
error, NULL);
if (!model)
goto out;
nrows = gda_data_model_get_n_rows (model);
for (i = 0; i < nrows; i++) {
const GValue *cv0, *cv1, *cv2, *cv3, *cv4;
gchar *tmp, *e0, *e1;
if (! (cv0 = gda_data_model_get_value_at (model, 0, i, error)))
goto out;
if (! (cv1 = gda_data_model_get_value_at (model, 1, i, error)))
goto out;
if (! (cv2 = gda_data_model_get_value_at (model, 2, i, error)))
goto out;
if (! (cv3 = gda_data_model_get_value_at (model, 3, i, error)))
goto out;
if (! (cv4 = gda_data_model_get_value_at (model, 4, i, error)))
goto out;
if (!schema || gda_value_differ (schema, cv0)) {
gchar *tmp;
if (schema) {
xmlNewChild (sdiv, NULL, BAD_CAST "br", NULL);
gda_value_free (schema);
}
schema = gda_value_copy (cv0);
tmp = g_strdup_printf (_("Triggers in the '%s' schema:"), g_value_get_string (schema));
xmlNewChild (hdoc->content, NULL, BAD_CAST "h1", BAD_CAST tmp);
g_free (tmp);
content_added = TRUE;
div = xmlNewChild (hdoc->content, NULL, BAD_CAST "div", NULL);
xmlSetProp (div, BAD_CAST "class", BAD_CAST "clist");
if (tschema) {
gda_value_free (tschema);
tschema = NULL;
}
if (tname) {
gda_value_free (tname);
tname = NULL;
}
}
if ((!tschema || gda_value_differ (tschema, cv3)) ||
(!tname || gda_value_differ (tname, cv4))) {
gchar *tmp;
if (tschema)
xmlNewChild (sdiv, NULL, BAD_CAST "br", NULL);
if (tschema)
gda_value_free (tschema);
if (tname)
gda_value_free (tname);
tschema = gda_value_copy (cv3);
tname = gda_value_copy (cv4);
tmp = g_strdup_printf (_("For the '%s.%s' table:"), g_value_get_string (tschema),
g_value_get_string (tname));
xmlNewChild (div, NULL, BAD_CAST "h2", BAD_CAST tmp);
g_free (tmp);
sdiv = xmlNewChild (div, NULL, BAD_CAST "div", NULL);
xmlSetProp (sdiv, BAD_CAST "class", BAD_CAST "clist");
sul = xmlNewChild (sdiv, NULL, BAD_CAST "ul", NULL);
}
li = xmlNewChild (sul, NULL, BAD_CAST "li", NULL);
tmp = g_strdup_printf ("%s (%s)", g_value_get_string (cv1), g_value_get_string (cv2));
a = xmlNewChild (li, NULL, BAD_CAST "a", BAD_CAST tmp);
g_free (tmp);
e0 = gda_rfc1738_encode (g_value_get_string (cv0));
e1 = gda_rfc1738_encode (g_value_get_string (cv1));
tmp = g_strdup_printf ("/%s/%s/%s", rfc_cnc_name, e0, e1);
g_free (e0);
g_free (e1);
xmlSetProp (a, BAD_CAST "href", BAD_CAST tmp);
g_free (tmp);
}
if (nrows != 0)
xmlNewChild (sdiv, NULL, BAD_CAST "br", NULL);
retval = TRUE;
if (! content_added)
xmlNewChild (hdoc->content, NULL, BAD_CAST "br", NULL);
out:
if (schema)
gda_value_free (schema);
if (tschema)
gda_value_free (tschema);
if (tname)
gda_value_free (tname);
if (model)
g_object_unref (model);
g_free (rfc_cnc_name);
return retval;
}
/*
* Get variables
*
* ...: a NULL terminated list of:
* - variable name: const gchar*
* - place holder for tha variable's contents: gchar **
*/
static void
get_variables (SoupMessage *msg, GHashTable *query, ...)
{
va_list ap;
gchar *name;
GHashTable *rquery;
if (query)
rquery = query;
else if (msg->request_body->length)
rquery = soup_form_decode (msg->request_body->data);
else
return;
va_start (ap, query);
for (name = va_arg (ap, gchar*); name; name = va_arg (ap, gchar*)) {
gchar **stringadr = va_arg (ap, gchar**);
const gchar *tmp;
tmp = g_hash_table_lookup (rquery, name);
if (tmp)
*stringadr = g_strdup (tmp);
else
*stringadr = NULL;
}
va_end (ap);
if (!query)
g_hash_table_destroy (rquery);
}
/*
* Get variables
*
* ...: a NULL terminated list of:
* - variable name: const gchar*
* - place holder for tha variable's contents: gchar **
*/
static void
get_cookies (SoupMessage *msg, ...)
{
const gchar *cookies;
GdaQuarkList *ql;
va_list ap;
gchar *name;
cookies = soup_message_headers_get (msg->request_headers, "Cookie");
ql = gda_quark_list_new_from_string (cookies);
va_start (ap, msg);
for (name = va_arg (ap, gchar*); name; name = va_arg (ap, gchar*)) {
gchar **stringadr = va_arg (ap, gchar**);
const gchar *tmp;
tmp = gda_quark_list_find (ql, name);
if (tmp)
*stringadr = g_strdup (tmp);
else
*stringadr = NULL;
}
va_end (ap);
gda_quark_list_free (ql);
}
static HtmlDoc*
create_new_htmldoc (G_GNUC_UNUSED WebServer *webserver, const ConnectionSetting *cs)
{
HtmlDoc *hdoc;
gchar *str;
gchar *rfc_cnc_name;
xmlNodePtr ul, li, a;
if (cs) {
str = g_strdup_printf (_("Database information for '%s'"), cs->name);
hdoc = html_doc_new (str);
g_free (str);
}
else
hdoc = html_doc_new (_("Database information"));
/* other connections in the sidebar */
ul = cnc_ul (TRUE);
xmlAddChild (hdoc->sidebar, ul);
/* list all database object's types for which information can be obtained */
if (cs) {
rfc_cnc_name = gda_rfc1738_encode (cs->name);
ul = xmlNewChild (hdoc->sidebar, NULL, BAD_CAST "ul", BAD_CAST _("Objects"));
li = xmlNewChild (ul, NULL, BAD_CAST "li", NULL);
a = xmlNewChild (li, NULL, BAD_CAST "a", BAD_CAST _("Tables"));
str = g_strdup_printf ("/%s/~tables", rfc_cnc_name);
xmlSetProp (a, BAD_CAST "href", BAD_CAST str);
g_free (str);
li = xmlNewChild (ul, NULL, BAD_CAST "li", NULL);
a = xmlNewChild (li, NULL, BAD_CAST "a", BAD_CAST _("Views"));
str = g_strdup_printf ("/%s/~views", rfc_cnc_name);
xmlSetProp (a, BAD_CAST "href", BAD_CAST str);
g_free (str);
li = xmlNewChild (ul, NULL, BAD_CAST "li", NULL);
a = xmlNewChild (li, NULL, BAD_CAST "a", BAD_CAST _("Triggers"));
str = g_strdup_printf ("/%s/~triggers", rfc_cnc_name);
xmlSetProp (a, BAD_CAST "href", BAD_CAST str);
g_free (str);
g_free (rfc_cnc_name);
}
return hdoc;
}
/*
*
* IRB
*
*/
static gboolean
delete_consoles (WebServer *server)
{
GSList *list;
GTimeVal tv;
g_get_current_time (&tv);
for (list = server->priv->terminals_list; list; ) {
SqlConsole *con = (SqlConsole *) list->data;
if (con->last_time_used.tv_sec + 600 < tv.tv_sec) {
GSList *n = list->next;
server->priv->terminals_list = g_slist_delete_link (server->priv->terminals_list, list);
list = n;
gda_sql_console_free (con);
}
else
list = list->next;
}
if (! server->priv->terminals_list) {
server->priv->term_timer = 0;
return FALSE;
}
else
return TRUE;
}
/*
* GET/POST method for IRB
*/
static gboolean
get_post_for_irb (WebServer *webserver, SoupMessage *msg, G_GNUC_UNUSED const ConnectionSetting *cs,
GHashTable *query, G_GNUC_UNUSED GError **error)
{
gboolean retval = FALSE;
SoupBuffer *buffer;
xmlChar *contents = NULL;
static gint counter = 0;
gchar *cmd;
gchar *cid;
SqlConsole *console = NULL;
/* fetch variables */
get_variables (msg, query, "cmd", &cmd, "cid", &cid, NULL);
if (!cmd)
return FALSE;
if (cid) {
GSList *list;
for (list = webserver->priv->terminals_list; list; list = list->next) {
if (((SqlConsole*) list->data)->id && !strcmp (((SqlConsole*) list->data)->id, cid)) {
console = (SqlConsole*) list->data;
break;
}
}
}
if (!console) {
if (!cid || !strcmp (cid, "none")) {
gchar *str;
str = g_strdup_printf ("console%d", counter++);
console = gda_sql_console_new (str);
g_free (str);
}
else {
console = gda_sql_console_new (cid);
}
webserver->priv->terminals_list = g_slist_prepend (webserver->priv->terminals_list,
console);
g_get_current_time (&(console->last_time_used));
if (!webserver->priv->term_timer)
webserver->priv->term_timer = g_timeout_add_seconds (5, (GSourceFunc) delete_consoles,
webserver);
if (!cid || !strcmp (cid, "none")) {
soup_message_headers_replace (msg->response_headers,
"Content-Type", "text/xml");
soup_message_set_status (msg, SOUP_STATUS_OK);
g_free (cmd);
/* send console's ID */
xmlDocPtr doc;
xmlNodePtr topnode;
gchar *tmp;
doc = xmlNewDoc (BAD_CAST "1.0");
topnode = xmlNewDocNode (doc, NULL, BAD_CAST "result", NULL);
xmlDocSetRootElement (doc, topnode);
xmlNewChild (topnode, NULL, BAD_CAST "cid", BAD_CAST (console->id));
tmp = gda_sql_console_compute_prompt (console, TOOL_OUTPUT_FORMAT_HTML);
xmlNewChild (topnode, NULL, BAD_CAST "prompt", BAD_CAST tmp);
g_free (tmp);
int size;
xmlDocDumpFormatMemory (doc, &contents, &size, 1);
xmlFreeDoc (doc);
goto resp;
}
}
/* create response */
g_get_current_time (&(console->last_time_used));
cmd = g_strstrip (cmd);
if (*cmd) {
xmlDocPtr doc;
GError *lerror = NULL;
xmlNodePtr topnode;
gchar *tmp;
doc = xmlNewDoc (BAD_CAST "1.0");
topnode = xmlNewDocNode (doc, NULL, BAD_CAST "result", NULL);
xmlDocSetRootElement (doc, topnode);
tmp = gda_sql_console_execute (console, cmd, &lerror, TOOL_OUTPUT_FORMAT_HTML);
if (!tmp)
tmp = g_strdup_printf (_("Error: %s"),
lerror && lerror->message ? lerror->message : _("No detail"));
if (lerror)
g_error_free (lerror);
xmlNewChild (topnode, NULL, BAD_CAST "cmde", BAD_CAST tmp);
g_free (tmp);
tmp = gda_sql_console_compute_prompt (console, TOOL_OUTPUT_FORMAT_HTML);
xmlNewChild (topnode, NULL, BAD_CAST "prompt", BAD_CAST tmp);
g_free (tmp);
int size;
xmlDocDumpFormatMemory (doc, &contents, &size, 1);
xmlFreeDoc (doc);
}
g_free (cmd);
/* send response */
resp:
soup_message_headers_replace (msg->response_headers,
"Content-Type", "text/xml");
if (contents) {
buffer = soup_buffer_new_with_owner (contents, strlen ((gchar*)contents), contents,
(GDestroyNotify) xmlFree);
soup_message_body_append_buffer (msg->response_body, buffer);
soup_buffer_free (buffer);
}
soup_message_set_status (msg, SOUP_STATUS_OK);
retval = TRUE;
return retval;
}
/*
*
* Temporary data management
*
*/
/*
* @data is stolen!
*/
static TmpResource *
tmp_resource_add (WebServer *server, const gchar *path, gchar *data, gsize data_length)
{
TmpResource *td;
GTimeVal tv;
g_get_current_time (&tv);
td = g_new0 (TmpResource, 1);
td->path = g_strdup (path);
td->data = data;
td->size = data_length;
td->expiration_date = tv.tv_sec + 30;
g_hash_table_insert (server->priv->resources_hash, g_strdup (path), td);
server->priv->resources_list = g_slist_prepend (server->priv->resources_list, td);
if (!server->priv->resources_timer)
server->priv->resources_timer = g_timeout_add_seconds (5, (GSourceFunc) delete_tmp_resource, server);
return td;
}
static gboolean
delete_tmp_resource (WebServer *server)
{
GSList *list;
GTimeVal tv;
gint n_timed = 0;
g_get_current_time (&tv);
for (list = server->priv->resources_list; list; ) {
TmpResource *td = (TmpResource *) list->data;
if ((td->expiration_date > 0) && (td->expiration_date < tv.tv_sec)) {
GSList *n = list->next;
g_hash_table_remove (server->priv->resources_hash, td->path);
server->priv->resources_list = g_slist_delete_link (server->priv->resources_list, list);
list = n;
}
else {
if (td->expiration_date > 0)
n_timed ++;
list = list->next;
}
}
if (n_timed == 0) {
server->priv->resources_timer = 0;
return FALSE;
}
else
return TRUE;
}
static void
tmp_resource_free (TmpResource *data)
{
g_free (data->data);
g_free (data);
}
/*
*
* Misc functions
*
*/
static TimedString *
timed_string_new (guint duration)
{
#define STRING_SIZE 16
TimedString *ts;
gint i;
GString *string;
ts = g_new0 (TimedString, 1);
string = g_string_new ("");
for (i = 0; i < STRING_SIZE; i++) {
gushort r = (((gfloat) rand ()) / (gfloat)RAND_MAX) * 255;
g_string_append_printf (string, "%0x", r);
}
ts->string = string->str;
g_string_free (string, FALSE);
g_get_current_time (& (ts->validity));
ts->validity.tv_sec += duration;
return ts;
}
static void
timed_string_free (TimedString *ts)
{
g_free (ts->string);
g_free (ts);
}
static gboolean
auth_timer_manage (WebServer *server)
{
challenges_manage (server);
auth_cookies_manage (server);
if ((server->priv->challenges->len == 0) &&
(server->priv->cookies->len == 0)) {
/* remove timer */
server->priv->auth_timer = 0;
return FALSE;
}
else
return TRUE;
}
static TimedString *
challenge_add (WebServer *server)
{
TimedString *ts = timed_string_new (180);
g_array_append_val (server->priv->challenges, ts);
if (!server->priv->auth_timer)
server->priv->auth_timer = g_timeout_add_seconds (5, (GSourceFunc) auth_timer_manage, server);
else
challenges_manage (server);
return ts;
}
static void
challenges_manage (WebServer *server)
{
gsize i;
GTimeVal current_ts;
if (server->priv->challenges->len > MAX_CHALLENGES) {
/* remove the oldest challenge */
TimedString *ts = g_array_index (server->priv->challenges, TimedString *, 0);
timed_string_free (ts);
g_array_remove_index (server->priv->challenges, 0);
}
g_get_current_time (¤t_ts);
for (i = 0; i < server->priv->challenges->len; ) {
TimedString *ts = g_array_index (server->priv->challenges, TimedString *, 0);
if (ts->validity.tv_sec < current_ts.tv_sec) {
timed_string_free (ts);
g_array_remove_index (server->priv->challenges, i);
}
else
i++;
}
}
static TimedString *
auth_cookie_add (WebServer *server)
{
TimedString *ts = timed_string_new (1800); /* half hour availability */
g_array_append_val (server->priv->cookies, ts);
if (!server->priv->auth_timer)
server->priv->auth_timer = g_timeout_add_seconds (5, (GSourceFunc) auth_timer_manage,
server);
else
auth_cookies_manage (server);
#ifdef DEBUG_SERVER
g_print ("Added cookie %s\n", ts->string);
#endif
return ts;
}
static void
auth_cookies_manage (WebServer *server)
{
gsize i;
GTimeVal current_ts;
if (server->priv->cookies->len > MAX_AUTH_COOKIES) {
/* remove the oldest auth_cookie */
TimedString *ts = g_array_index (server->priv->cookies, TimedString *, 0);
timed_string_free (ts);
g_array_remove_index (server->priv->cookies, 0);
}
g_get_current_time (¤t_ts);
for (i = 0; i < server->priv->cookies->len; ) {
TimedString *ts = g_array_index (server->priv->cookies, TimedString *, 0);
if (ts->validity.tv_sec < current_ts.tv_sec) {
#ifdef DEBUG_SERVER
g_print ("Removed cookie %s\n", ts->string);
#endif
timed_string_free (ts);
g_array_remove_index (server->priv->cookies, i);
}
else
i++;
}
}
|