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 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406
|
/* SASL server API implementation
* Rob Siemborski
* Tim Martin
*/
/*
* Copyright (c) 1998-2016 Carnegie Mellon University. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The name "Carnegie Mellon University" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For permission or any other legal
* details, please contact
* Carnegie Mellon University
* Center for Technology Transfer and Enterprise Creation
* 4615 Forbes Avenue
* Suite 302
* Pittsburgh, PA 15213
* (412) 268-7393, fax: (412) 268-7395
* innovation@andrew.cmu.edu
*
* 4. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Computing Services
* at Carnegie Mellon University (http://www.cmu.edu/computing/)."
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* local functions/structs don't start with sasl
*/
#include <config.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#ifndef macintosh
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include "sasl.h"
#include "saslint.h"
#include "saslplug.h"
#include "saslutil.h"
#define DEFAULT_CHECKPASS_MECH "auxprop"
/* Contains functions:
*
* sasl_server_init
* sasl_server_new
* sasl_listmech
* sasl_server_start
* sasl_server_step
* sasl_checkpass
* sasl_checkapop
* sasl_user_exists
* sasl_setpass
*/
/* if we've initialized the server sucessfully */
static int _sasl_server_active = 0;
/* For access by other modules */
int _is_sasl_server_active(void) { return _sasl_server_active; }
static int _sasl_checkpass(sasl_conn_t *conn,
const char *user, unsigned userlen,
const char *pass, unsigned passlen);
static mech_list_t *mechlist = NULL; /* global var which holds the list */
static sasl_global_callbacks_t global_callbacks;
/* set the password for a user
* conn -- SASL connection
* user -- user name
* pass -- plaintext password, may be NULL to remove user
* passlen -- length of password, 0 = strlen(pass)
* oldpass -- NULL will sometimes work
* oldpasslen -- length of password, 0 = strlen(oldpass)
* flags -- see flags below
*
* returns:
* SASL_NOCHANGE -- proper entry already exists
* SASL_NOMECH -- no authdb supports password setting as configured
* SASL_NOVERIFY -- user exists, but no settable password present
* SASL_DISABLED -- account disabled
* SASL_PWLOCK -- password locked
* SASL_WEAKPASS -- password too weak for security policy
* SASL_NOUSERPASS -- user-supplied passwords not permitted
* SASL_FAIL -- OS error
* SASL_BADPARAM -- password too long
* SASL_OK -- successful
*/
int sasl_setpass(sasl_conn_t *conn,
const char *user,
const char *pass,
unsigned passlen,
const char *oldpass,
unsigned oldpasslen,
unsigned flags)
{
int result = SASL_OK, tmpresult;
sasl_server_conn_t *s_conn = (sasl_server_conn_t *) conn;
const char *password_request[] = { SASL_AUX_PASSWORD_PROP, NULL };
const char *user_delete_request[] = { SASL_AUX_PASSWORD_PROP, SASL_AUX_ALL, NULL };
sasl_server_userdb_setpass_t *setpass_cb = NULL;
void *context = NULL;
int tried_setpass = 0;
int failed = 0;
mechanism_t *sm;
server_sasl_mechanism_t *m;
char *current_mech;
if (!_sasl_server_active || !mechlist) return SASL_NOTINIT;
/* check params */
if (!conn) return SASL_BADPARAM;
if (conn->type != SASL_CONN_SERVER) PARAMERROR(conn);
if ((!(flags & SASL_SET_DISABLE) && passlen == 0)
|| ((flags & SASL_SET_CREATE) && (flags & SASL_SET_DISABLE)))
PARAMERROR(conn);
/* Check that we have an active SASL mechanism */
if (sasl_getprop (conn,
SASL_MECHNAME,
(const void **) ¤t_mech) != SASL_OK) {
current_mech = NULL;
}
if ( (flags & SASL_SET_CURMECH_ONLY) &&
(current_mech == NULL) ) {
sasl_seterror( conn, SASL_NOLOG,
"No current SASL mechanism available");
RETURN(conn, SASL_BADPARAM);
}
/* Do we want to store SASL_AUX_PASSWORD_PROP (plain text)? and
* Do we have an auxprop backend that can store properties?
*/
if ((flags & SASL_SET_DISABLE || !(flags & SASL_SET_NOPLAIN)) &&
sasl_auxprop_store(NULL, NULL, NULL) == SASL_OK) {
tried_setpass++;
if (flags & SASL_SET_DISABLE) {
pass = NULL;
passlen = 0;
result = prop_request(s_conn->sparams->propctx, user_delete_request);
} else {
result = prop_request(s_conn->sparams->propctx, password_request);
}
if (result == SASL_OK) {
/* NOTE: When deleting users, this will work in a backward compatible way */
result = prop_set(s_conn->sparams->propctx, SASL_AUX_PASSWORD_PROP,
pass, passlen);
}
if (result == SASL_OK && flags & SASL_SET_DISABLE) {
result = prop_set(s_conn->sparams->propctx, SASL_AUX_ALL,
NULL, 0);
}
if (result == SASL_OK) {
result = sasl_auxprop_store(conn, s_conn->sparams->propctx, user);
}
if (result != SASL_OK) {
_sasl_log(conn, SASL_LOG_ERR,
"setpass failed for %s: %z",
user, result);
failed++;
} else {
_sasl_log(conn, SASL_LOG_NOTE,
"setpass succeeded for %s", user);
}
}
/* We want to preserve the current value of result, so we use tmpresult below */
/* call userdb callback function */
tmpresult = _sasl_getcallback(conn, SASL_CB_SERVER_USERDB_SETPASS,
(sasl_callback_ft *)&setpass_cb, &context);
if (tmpresult == SASL_OK && setpass_cb) {
tried_setpass++;
tmpresult = setpass_cb(conn, context, user, pass, passlen,
s_conn->sparams->propctx, flags);
if(tmpresult != SASL_OK) {
if (tmpresult == SASL_CONSTRAINT_VIOLAT) {
if (result == SASL_OK) {
result = tmpresult;
}
} else {
result = tmpresult;
}
_sasl_log(conn, SASL_LOG_ERR,
"setpass callback failed for %s: %z",
user, tmpresult);
failed++;
} else {
_sasl_log(conn, SASL_LOG_NOTE,
"setpass callback succeeded for %s", user);
}
}
/* now we let the mechanisms set their secrets */
for (sm = s_conn->mech_list; sm; sm = sm->next) {
m = &sm->m;
if (!m->plug->setpass) {
/* can't set pass for this mech */
continue;
}
/* Invoke only one setpass for the currently selected mechanism,
if SASL_SET_CURMECH_ONLY is specified */
if ((flags & SASL_SET_CURMECH_ONLY) &&
(strcmp(current_mech, m->plug->mech_name) != 0)) {
continue;
}
tried_setpass++;
tmpresult = m->plug->setpass(m->plug->glob_context,
((sasl_server_conn_t *)conn)->sparams,
user,
pass,
passlen,
oldpass, oldpasslen,
flags);
if (tmpresult == SASL_OK) {
_sasl_log(conn, SASL_LOG_NOTE,
"%s: set secret for %s", m->plug->mech_name, user);
m->condition = SASL_OK; /* if we previously thought the
mechanism didn't have any user secrets
we now think it does */
} else if (tmpresult == SASL_NOCHANGE) {
_sasl_log(conn, SASL_LOG_NOTE,
"%s: secret not changed for %s", m->plug->mech_name, user);
} else if (tmpresult == SASL_CONSTRAINT_VIOLAT) {
_sasl_log(conn, SASL_LOG_ERR,
"%s: failed to set secret for %s: constrain violation",
m->plug->mech_name, user);
if (result == SASL_OK) {
result = tmpresult;
}
failed++;
} else {
result = tmpresult;
_sasl_log(conn, SASL_LOG_ERR,
"%s: failed to set secret for %s: %z (%m)",
m->plug->mech_name, user, tmpresult,
#ifndef WIN32
errno
#else
GetLastError()
#endif
);
failed++;
}
}
if (!tried_setpass) {
_sasl_log(conn, SASL_LOG_WARN,
"secret not changed for %s: "
"no writable auxprop plugin or setpass callback found",
user);
} else if (result == SASL_CONSTRAINT_VIOLAT) {
/* If not all setpass failed with SASL_CONSTRAINT_VIOLAT -
ignore SASL_CONSTRAINT_VIOLAT */
if (failed < tried_setpass) {
result = SASL_OK;
}
}
RETURN(conn, result);
}
/* local mechanism which disposes of server */
static void server_dispose(sasl_conn_t *pconn)
{
sasl_server_conn_t *s_conn= (sasl_server_conn_t *) pconn;
context_list_t *cur, *cur_next;
/* Just sanity check that sasl_server_done wasn't called yet */
if (_sasl_server_active != 0) {
if (s_conn->mech) {
void (*mech_dispose)(void *conn_context, const sasl_utils_t *utils);
mech_dispose = s_conn->mech->m.plug->mech_dispose;
if (mech_dispose) {
mech_dispose(pconn->context, s_conn->sparams->utils);
}
}
pconn->context = NULL;
for(cur = s_conn->mech_contexts; cur; cur=cur_next) {
cur_next = cur->next;
if (cur->context) {
cur->mech->m.plug->mech_dispose(cur->context, s_conn->sparams->utils);
}
sasl_FREE(cur);
}
s_conn->mech_contexts = NULL;
}
_sasl_free_utils(&s_conn->sparams->utils);
if (s_conn->sparams->propctx) {
prop_dispose(&s_conn->sparams->propctx);
}
if (s_conn->appname) {
sasl_FREE(s_conn->appname);
}
if (s_conn->user_realm) {
sasl_FREE(s_conn->user_realm);
}
if (s_conn->sparams) {
sasl_FREE(s_conn->sparams);
}
if (s_conn->mech_list != mechlist->mech_list) {
/* free connection-specific mech_list */
mechanism_t *m, *prevm;
m = s_conn->mech_list; /* m point to beginning of the list */
while (m) {
prevm = m;
m = m->next;
sasl_FREE(prevm);
}
}
_sasl_conn_dispose(pconn);
}
static int init_mechlist(void)
{
sasl_utils_t *newutils = NULL;
/* set util functions - need to do rest */
newutils = _sasl_alloc_utils(NULL, &global_callbacks);
if (newutils == NULL)
return SASL_NOMEM;
newutils->checkpass = &_sasl_checkpass;
mechlist->utils = newutils;
mechlist->mech_list = NULL;
mechlist->mech_length = 0;
return SASL_OK;
}
static int mech_compare(const sasl_server_plug_t *a,
const sasl_server_plug_t *b)
{
unsigned sec_diff;
unsigned features_diff;
/* XXX the following is fairly arbitrary, but its independent
of the order in which the plugins are loaded
*/
sec_diff = a->security_flags ^ b->security_flags;
if (sec_diff & a->security_flags & SASL_SEC_NOANONYMOUS) return 1;
if (sec_diff & b->security_flags & SASL_SEC_NOANONYMOUS) return -1;
if (sec_diff & a->security_flags & SASL_SEC_NOPLAINTEXT) return 1;
if (sec_diff & b->security_flags & SASL_SEC_NOPLAINTEXT) return -1;
if (sec_diff & a->security_flags & SASL_SEC_MUTUAL_AUTH) return 1;
if (sec_diff & b->security_flags & SASL_SEC_MUTUAL_AUTH) return -1;
if (sec_diff & a->security_flags & SASL_SEC_NOACTIVE) return 1;
if (sec_diff & b->security_flags & SASL_SEC_NOACTIVE) return -1;
if (sec_diff & a->security_flags & SASL_SEC_NODICTIONARY) return 1;
if (sec_diff & b->security_flags & SASL_SEC_NODICTIONARY) return -1;
if (sec_diff & a->security_flags & SASL_SEC_FORWARD_SECRECY) return 1;
if (sec_diff & b->security_flags & SASL_SEC_FORWARD_SECRECY) return -1;
features_diff = a->features ^ b->features;
if (features_diff & a->features & SASL_FEAT_CHANNEL_BINDING) return 1;
if (features_diff & b->features & SASL_FEAT_CHANNEL_BINDING) return -1;
if (a->max_ssf > b->max_ssf) return 1;
if (a->max_ssf < b->max_ssf) return -1;
if (SASL_GET_HASH_STRENGTH(a->security_flags) > SASL_GET_HASH_STRENGTH(b->security_flags)) return 1;
if (SASL_GET_HASH_STRENGTH(a->security_flags) < SASL_GET_HASH_STRENGTH(b->security_flags)) return -1;
return 0;
}
/*
* parameters:
* p - entry point
*/
int sasl_server_add_plugin(const char *plugname,
sasl_server_plug_init_t *p)
{
int plugcount;
sasl_server_plug_t *pluglist = NULL;
sasl_server_plug_init_t *entry_point = NULL;
int result;
int version;
int lupe;
if(!plugname || !p) return SASL_BADPARAM;
entry_point = (sasl_server_plug_init_t *)p;
/* call into the shared library asking for information about it */
/* version is filled in with the version of the plugin */
result = entry_point(mechlist->utils, SASL_SERVER_PLUG_VERSION, &version,
&pluglist, &plugcount);
if ((result != SASL_OK) && (result != SASL_NOUSER)
&& (result != SASL_CONTINUE)) {
_sasl_log(NULL, SASL_LOG_DEBUG,
"%s_client_plug_init() failed in sasl_server_add_plugin(): %z\n",
plugname, result);
return result;
}
/* Make sure plugin is using the same SASL version as us */
if (version != SASL_SERVER_PLUG_VERSION)
{
_sasl_log(NULL,
SASL_LOG_ERR,
"version mismatch on sasl_server_add_plugin for '%s': %d expected, but %d reported",
plugname,
SASL_SERVER_PLUG_VERSION,
version);
return SASL_BADVERS;
}
for (lupe=0;lupe < plugcount ;lupe++, pluglist++)
{
mechanism_t *mech, *mp;
mech = sasl_ALLOC(sizeof(mechanism_t));
if (! mech) return SASL_NOMEM;
memset (mech, 0, sizeof(mechanism_t));
mech->m.plug = pluglist;
if(_sasl_strdup(plugname, &mech->m.plugname, NULL) != SASL_OK) {
sasl_FREE(mech);
return SASL_NOMEM;
}
mech->m.version = version;
/* whether this mech actually has any users in it's db */
mech->m.condition = result; /* SASL_OK, SASL_CONTINUE or SASL_NOUSER */
/* mech->m.f = NULL; */
/* sort mech_list by relative "strength" */
mp = mechlist->mech_list;
if (!mp || mech_compare(pluglist, mp->m.plug) >= 0) {
/* add mech to head of list */
mech->next = mechlist->mech_list;
mechlist->mech_list = mech;
} else {
/* find where to insert mech into list */
while (mp->next &&
mech_compare(pluglist, mp->next->m.plug) <= 0) mp = mp->next;
mech->next = mp->next;
mp->next = mech;
}
mechlist->mech_length++;
}
return SASL_OK;
}
int sasl_server_done(void)
{
int result = SASL_CONTINUE;
if (_sasl_server_cleanup_hook == NULL && _sasl_client_cleanup_hook == NULL) {
return SASL_NOTINIT;
}
if (_sasl_server_cleanup_hook) {
result = _sasl_server_cleanup_hook();
if (result == SASL_OK) {
_sasl_server_idle_hook = NULL;
_sasl_server_cleanup_hook = NULL;
} else {
return result;
}
}
if (_sasl_server_cleanup_hook || _sasl_client_cleanup_hook) {
return result;
}
sasl_common_done();
return SASL_OK;
}
static int server_done(void) {
mechanism_t *m;
mechanism_t *prevm;
if(_sasl_server_active == 0)
return SASL_NOTINIT;
else
_sasl_server_active--;
if(_sasl_server_active) {
/* Don't de-init yet! Our refcount is nonzero. */
return SASL_CONTINUE;
}
if (mechlist != NULL)
{
m=mechlist->mech_list; /* m point to beginning of the list */
while (m!=NULL)
{
prevm=m;
m=m->next;
if (prevm->m.plug->mech_free) {
prevm->m.plug->mech_free(prevm->m.plug->glob_context,
mechlist->utils);
}
sasl_FREE(prevm->m.plugname);
sasl_FREE(prevm);
}
_sasl_free_utils(&mechlist->utils);
sasl_FREE(mechlist);
mechlist = NULL;
}
/* Free the auxprop plugins */
_sasl_auxprop_free();
global_callbacks.callbacks = NULL;
global_callbacks.appname = NULL;
sasl_config_done();
return SASL_OK;
}
static int server_idle(sasl_conn_t *conn)
{
sasl_server_conn_t *s_conn = NULL;
mechanism_t *m;
if (! mechlist) {
return 0;
}
if (!conn)
return 1;
s_conn = (sasl_server_conn_t *) conn;
for (m = s_conn->mech_list;
m != NULL;
m = m->next) {
if (m->m.plug->idle
&& m->m.plug->idle(m->m.plug->glob_context,
conn,
s_conn->sparams)) {
return 1;
}
}
return 0;
}
static int load_config(const sasl_callback_t *verifyfile_cb)
{
int result;
const char *path_to_config = NULL;
size_t path_len;
char *config_filename = NULL;
size_t len;
const sasl_callback_t *getconfpath_cb = NULL;
const char * next;
/* If appname was not provided, behave as if there is no config file
(see also sasl_config_init() */
if (global_callbacks.appname == NULL) {
return SASL_CONTINUE;
}
/* get the path to the config file */
getconfpath_cb = _sasl_find_getconfpath_callback( global_callbacks.callbacks );
if (getconfpath_cb == NULL) return SASL_BADPARAM;
/* getconfpath_cb->proc MUST be a sasl_getconfpath_t; if only C had a type
system */
result = ((sasl_getconfpath_t *)(getconfpath_cb->proc))(getconfpath_cb->context,
(char **) &path_to_config);
if (result != SASL_OK) goto done;
if (path_to_config == NULL) path_to_config = "";
next = path_to_config;
while (next != NULL) {
next = strchr(path_to_config, PATHS_DELIMITER);
/* length = length of path + '/' + length of appname + ".conf" + 1
for '\0' */
if (next != NULL) {
path_len = next - path_to_config;
next++; /* Skip to the next path */
} else {
path_len = strlen(path_to_config);
}
len = path_len + 2 + strlen(global_callbacks.appname) + 5 + 1;
if (len > PATH_MAX ) {
result = SASL_FAIL;
goto done;
}
/* construct the filename for the config file */
config_filename = sasl_ALLOC((unsigned)len);
if (! config_filename) {
result = SASL_NOMEM;
goto done;
}
snprintf(config_filename, len, "%.*s%c%s.conf", (int)path_len, path_to_config,
HIER_DELIMITER, global_callbacks.appname);
/* Ask the application if it's safe to use this file */
result = ((sasl_verifyfile_t *)(verifyfile_cb->proc))(verifyfile_cb->context,
config_filename, SASL_VRFY_CONF);
/* returns SASL_CONTINUE if the config file doesn't exist */
if (result == SASL_OK) {
result = sasl_config_init(config_filename);
if (result != SASL_CONTINUE) {
/* We are done */
break;
}
}
if (config_filename) {
sasl_FREE(config_filename);
config_filename = NULL;
}
path_to_config = next;
}
done:
if (config_filename) sasl_FREE(config_filename);
return result;
}
/*
* Verify that all the callbacks are valid
*/
static int verify_server_callbacks(const sasl_callback_t *callbacks)
{
if (callbacks == NULL) return SASL_OK;
while (callbacks->id != SASL_CB_LIST_END) {
if (callbacks->proc==NULL) return SASL_FAIL;
callbacks++;
}
return SASL_OK;
}
static char *grab_field(char *line, char **eofield)
{
int d = 0;
char *field;
while (isspace((int) *line)) line++;
/* find end of field */
while (line[d] && !isspace(((int) line[d]))) d++;
field = sasl_ALLOC(d + 1);
if (!field) { return NULL; }
memcpy(field, line, d);
field[d] = '\0';
*eofield = line + d;
return field;
}
struct secflag_map_s {
char *name;
int value;
};
struct secflag_map_s secflag_map[] = {
{ "noplaintext", SASL_SEC_NOPLAINTEXT },
{ "noactive", SASL_SEC_NOACTIVE },
{ "nodictionary", SASL_SEC_NODICTIONARY },
{ "forward_secrecy", SASL_SEC_FORWARD_SECRECY },
{ "noanonymous", SASL_SEC_NOANONYMOUS },
{ "pass_credentials", SASL_SEC_PASS_CREDENTIALS },
{ "mutual_auth", SASL_SEC_MUTUAL_AUTH },
{ NULL, 0x0 }
};
static int parse_mechlist_file(const char *mechlistfile)
{
FILE *f;
char buf[1024];
char *t, *ptr;
int r = 0;
f = fopen(mechlistfile, "r");
if (!f) return SASL_FAIL;
r = SASL_OK;
while (fgets(buf, sizeof(buf), f) != NULL) {
mechanism_t *n = sasl_ALLOC(sizeof(mechanism_t));
sasl_server_plug_t *nplug;
if (n == NULL) { r = SASL_NOMEM; break; }
n->m.version = SASL_SERVER_PLUG_VERSION;
n->m.condition = SASL_CONTINUE;
nplug = sasl_ALLOC(sizeof(sasl_server_plug_t));
if (nplug == NULL) { r = SASL_NOMEM; break; }
memset(nplug, 0, sizeof(sasl_server_plug_t));
/* each line is:
plugin-file WS mech_name WS max_ssf *(WS security_flag) RET
*/
/* grab file */
n->m.f = grab_field(buf, &ptr);
/* grab mech_name */
nplug->mech_name = grab_field(ptr, &ptr);
/* grab max_ssf */
nplug->max_ssf = strtol(ptr, &ptr, 10);
/* grab security flags */
while (*ptr != '\n') {
struct secflag_map_s *map;
/* read security flag */
t = grab_field(ptr, &ptr);
map = secflag_map;
while (map->name) {
if (!strcasecmp(t, map->name)) {
nplug->security_flags |= map->value;
break;
}
map++;
}
if (!map->name) {
_sasl_log(NULL, SASL_LOG_ERR,
"%s: couldn't identify flag '%s'",
nplug->mech_name, t);
}
free(t);
}
/* insert mechanism into mechlist */
n->m.plug = nplug;
n->next = mechlist->mech_list;
mechlist->mech_list = n;
mechlist->mech_length++;
}
fclose(f);
return r;
}
/* initialize server drivers, done once per process
* callbacks -- callbacks for all server connections; must include
* getopt callback
* appname -- name of calling application
* (for lower level logging and reading of the configuration file)
* results:
* state -- server state
* returns:
* SASL_OK -- success
* SASL_BADPARAM -- error in config file
* SASL_NOMEM -- memory failure
* SASL_BADVERS -- Mechanism version mismatch
*/
int sasl_server_init(const sasl_callback_t *callbacks,
const char *appname)
{
int ret;
const sasl_callback_t *vf;
const char *pluginfile = NULL;
#ifdef PIC
sasl_getopt_t *getopt;
void *context;
#endif
const add_plugin_list_t ep_list[] = {
{ "sasl_server_plug_init", (add_plugin_t *)sasl_server_add_plugin },
{ "sasl_auxprop_plug_init", (add_plugin_t *)sasl_auxprop_add_plugin },
{ "sasl_canonuser_init", (add_plugin_t *)sasl_canonuser_add_plugin },
{ NULL, NULL }
};
/* lock allocation type */
_sasl_allocation_locked++;
/* we require the appname (if present) to be short enough to be a path */
if (appname != NULL && strlen(appname) >= PATH_MAX)
return SASL_BADPARAM;
if (_sasl_server_active) {
/* We're already active, just increase our refcount */
/* xxx do something with the callback structure? */
_sasl_server_active++;
return SASL_OK;
}
ret = _sasl_common_init(&global_callbacks);
if (ret != SASL_OK)
return ret;
/* verify that the callbacks look ok */
ret = verify_server_callbacks(callbacks);
if (ret != SASL_OK)
return ret;
global_callbacks.callbacks = callbacks;
/* A shared library calling sasl_server_init will pass NULL as appname.
This should retain the original appname. */
if (appname != NULL) {
global_callbacks.appname = appname;
}
/* If we fail now, we have to call server_done */
_sasl_server_active = 1;
/* allocate mechlist and set it to empty */
mechlist = sasl_ALLOC(sizeof(mech_list_t));
if (mechlist == NULL) {
server_done();
return SASL_NOMEM;
}
ret = init_mechlist();
if (ret != SASL_OK) {
server_done();
return ret;
}
vf = _sasl_find_verifyfile_callback(callbacks);
/* load config file if applicable */
ret = load_config(vf);
if ((ret != SASL_OK) && (ret != SASL_CONTINUE)) {
server_done();
return ret;
}
/* load internal plugins */
sasl_server_add_plugin("EXTERNAL", &external_server_plug_init);
#ifdef PIC
/* delayed loading of plugins? (DSO only, as it doesn't
* make much [any] sense to delay in the static library case) */
if (_sasl_getcallback(NULL, SASL_CB_GETOPT, (sasl_callback_ft *)&getopt, &context)
== SASL_OK) {
/* No sasl_conn_t was given to getcallback, so we provide the
* global callbacks structure */
ret = getopt(&global_callbacks, NULL, "plugin_list", &pluginfile, NULL);
}
#endif
if (pluginfile != NULL) {
/* this file should contain a list of plugins available.
we'll load on demand. */
/* Ask the application if it's safe to use this file */
ret = ((sasl_verifyfile_t *)(vf->proc))(vf->context,
pluginfile,
SASL_VRFY_CONF);
if (ret != SASL_OK) {
_sasl_log(NULL, SASL_LOG_ERR,
"unable to load plugin list %s: %z", pluginfile, ret);
}
if (ret == SASL_OK) {
ret = parse_mechlist_file(pluginfile);
}
} else {
/* load all plugins now */
ret = _sasl_load_plugins(ep_list,
_sasl_find_getpath_callback(callbacks),
_sasl_find_verifyfile_callback(callbacks));
}
if (ret == SASL_OK) {
_sasl_server_cleanup_hook = &server_done;
_sasl_server_idle_hook = &server_idle;
ret = _sasl_build_mechlist();
} else {
server_done();
}
return ret;
}
/*
* Once we have the users plaintext password we
* may want to transition them. That is put entries
* for them in the passwd database for other
* stronger mechanism
*
* for example PLAIN -> CRAM-MD5
*/
static int
_sasl_transition(sasl_conn_t * conn,
const char * pass,
unsigned passlen)
{
const char *dotrans = "n";
sasl_getopt_t *getopt;
int result = SASL_OK;
void *context;
unsigned flags = 0;
if (! conn)
return SASL_BADPARAM;
if (! conn->oparams.authid)
PARAMERROR(conn);
/* check if this is enabled: default to false */
if (_sasl_getcallback(conn, SASL_CB_GETOPT, (sasl_callback_ft *)&getopt, &context) == SASL_OK)
{
getopt(context, NULL, "auto_transition", &dotrans, NULL);
if (dotrans == NULL) dotrans = "n";
}
if (!strcmp(dotrans, "noplain")) flags |= SASL_SET_NOPLAIN;
if (flags || *dotrans == '1' || *dotrans == 'y' ||
(*dotrans == 'o' && dotrans[1] == 'n') || *dotrans == 't') {
/* ok, it's on! */
_sasl_log(conn, SASL_LOG_NOTE,
"transitioning user %s to auxprop database",
conn->oparams.authid);
result = sasl_setpass(conn,
conn->oparams.authid,
pass,
passlen,
NULL, 0, SASL_SET_CREATE | flags);
}
RETURN(conn,result);
}
/* create context for a single SASL connection
* service -- registered name of the service using SASL (e.g. "imap")
* serverFQDN -- Fully qualified domain name of server. NULL means use
* gethostname() or equivalent.
* Useful for multi-homed servers.
* user_realm -- permits multiple user realms on server, NULL = default
* iplocalport -- server IPv4/IPv6 domain literal string with port
* (if NULL, then mechanisms requiring IPaddr are disabled)
* ipremoteport -- client IPv4/IPv6 domain literal string with port
* (if NULL, then mechanisms requiring IPaddr are disabled)
* callbacks -- callbacks (e.g., authorization, lang, new getopt context)
* flags -- usage flags (see above)
* returns:
* pconn -- new connection context
*
* returns:
* SASL_OK -- success
* SASL_NOMEM -- not enough memory
*/
int sasl_server_new(const char *service,
const char *serverFQDN,
const char *user_realm,
const char *iplocalport,
const char *ipremoteport,
const sasl_callback_t *callbacks,
unsigned flags,
sasl_conn_t **pconn)
{
int result;
sasl_server_conn_t *serverconn;
sasl_utils_t *utils;
sasl_getopt_t *getopt;
void *context;
const char *log_level, *auto_trans;
const char *mlist = NULL;
int plus = 0;
if (_sasl_server_active==0) return SASL_NOTINIT;
if (! pconn) return SASL_FAIL;
if (! service) return SASL_FAIL;
*pconn=sasl_ALLOC(sizeof(sasl_server_conn_t));
if (*pconn==NULL) return SASL_NOMEM;
memset(*pconn, 0, sizeof(sasl_server_conn_t));
serverconn = (sasl_server_conn_t *)*pconn;
/* make sparams */
serverconn->sparams=sasl_ALLOC(sizeof(sasl_server_params_t));
if (serverconn->sparams==NULL)
MEMERROR(*pconn);
memset(serverconn->sparams, 0, sizeof(sasl_server_params_t));
(*pconn)->destroy_conn = &server_dispose;
result = _sasl_conn_init(*pconn, service, flags, SASL_CONN_SERVER,
&server_idle, serverFQDN,
iplocalport, ipremoteport,
callbacks, &global_callbacks);
if (result != SASL_OK)
goto done_error;
/* set util functions - need to do rest */
utils=_sasl_alloc_utils(*pconn, &global_callbacks);
if (!utils) {
result = SASL_NOMEM;
goto done_error;
}
utils->checkpass = &_sasl_checkpass;
/* Setup the propctx -> We'll assume the default size */
serverconn->sparams->propctx=prop_new(0);
if(!serverconn->sparams->propctx) {
result = SASL_NOMEM;
goto done_error;
}
serverconn->sparams->service = (*pconn)->service;
serverconn->sparams->servicelen = (unsigned) strlen((*pconn)->service);
if (global_callbacks.appname && global_callbacks.appname[0] != '\0') {
result = _sasl_strdup (global_callbacks.appname,
&serverconn->appname,
NULL);
if (result != SASL_OK) {
result = SASL_NOMEM;
goto done_error;
}
serverconn->sparams->appname = serverconn->appname;
serverconn->sparams->applen = (unsigned) strlen(serverconn->sparams->appname);
} else {
serverconn->appname = NULL;
serverconn->sparams->appname = NULL;
serverconn->sparams->applen = 0;
}
serverconn->sparams->serverFQDN = (*pconn)->serverFQDN;
serverconn->sparams->slen = (unsigned) strlen((*pconn)->serverFQDN);
if (user_realm) {
result = _sasl_strdup(user_realm, &serverconn->user_realm, NULL);
serverconn->sparams->urlen = (unsigned) strlen(user_realm);
serverconn->sparams->user_realm = serverconn->user_realm;
} else {
serverconn->user_realm = NULL;
/* the sparams is already zeroed */
}
serverconn->sparams->callbacks = callbacks;
log_level = auto_trans = NULL;
if(_sasl_getcallback(*pconn, SASL_CB_GETOPT, (sasl_callback_ft *)&getopt, &context) == SASL_OK) {
getopt(context, NULL, "log_level", &log_level, NULL);
getopt(context, NULL, "auto_transition", &auto_trans, NULL);
getopt(context, NULL, "mech_list", &mlist, NULL);
}
serverconn->sparams->log_level = log_level ? atoi(log_level) : SASL_LOG_ERR;
serverconn->sparams->utils = utils;
if (auto_trans &&
(*auto_trans == '1' || *auto_trans == 'y' || *auto_trans == 't' ||
(*auto_trans == 'o' && auto_trans[1] == 'n') ||
!strcmp(auto_trans, "noplain")) &&
sasl_auxprop_store(NULL, NULL, NULL) == SASL_OK) {
serverconn->sparams->transition = &_sasl_transition;
}
/* if we have a mech_list, create ordered list of avail mechs for this conn */
if (mlist) {
const char *cp;
mechanism_t *mptr, *tail = NULL;
while (*mlist) {
/* find end of current mech name */
for (cp = mlist; *cp && !isspace((int) *cp); cp++);
/* search for mech name in loaded plugins */
for (mptr = mechlist->mech_list; mptr; mptr = mptr->next) {
const sasl_server_plug_t *plug = mptr->m.plug;
if (_sasl_is_equal_mech(mlist, plug->mech_name, (size_t) (cp - mlist), &plus)) {
/* found a match */
break;
}
}
if (mptr) {
mechanism_t *new = sasl_ALLOC(sizeof(mechanism_t));
if (!new) return SASL_NOMEM;
memcpy(&new->m, &mptr->m, sizeof(server_sasl_mechanism_t));
new->next = NULL;
if (!serverconn->mech_list) {
serverconn->mech_list = new;
tail = serverconn->mech_list;
}
else {
if (tail)
tail->next = new;
tail = new;
}
serverconn->mech_length++;
}
/* find next mech name */
mlist = cp;
while (*mlist && isspace((int) *mlist)) mlist++;
}
}
else {
serverconn->mech_list = mechlist->mech_list;
serverconn->mech_length = mechlist->mech_length;
}
serverconn->sparams->canon_user = &_sasl_canon_user_lookup;
serverconn->sparams->props = serverconn->base.props;
serverconn->sparams->flags = flags;
if(result == SASL_OK) return SASL_OK;
done_error:
_sasl_conn_dispose(*pconn);
sasl_FREE(*pconn);
*pconn = NULL;
return result;
}
/*
* The rule is:
* IF mech strength + external strength < min ssf THEN FAIL.
* We also have to look at the security properties and make sure
* that this mechanism has everything we want.
*/
static int mech_permitted(sasl_conn_t *conn,
mechanism_t *mech)
{
sasl_server_conn_t *s_conn = (sasl_server_conn_t *)conn;
const sasl_server_plug_t *plug;
int ret;
int myflags;
context_list_t *cur;
context_list_t *mech_context_list_entry = NULL;
void *context = NULL;
sasl_ssf_t minssf = 0;
if(!conn) return SASL_NOMECH;
if(! mech || ! mech->m.plug) {
PARAMERROR(conn);
return SASL_NOMECH;
}
plug = mech->m.plug;
/* setup parameters for the call to mech_avail */
s_conn->sparams->serverFQDN=conn->serverFQDN;
s_conn->sparams->service=conn->service;
s_conn->sparams->user_realm=s_conn->user_realm;
s_conn->sparams->props=conn->props;
s_conn->sparams->external_ssf=conn->external.ssf;
/* Check if we have banished this one already */
for (cur = s_conn->mech_contexts; cur; cur=cur->next) {
if (cur->mech == mech) {
/* If it's not mech_avail'd, then stop now */
if (!cur->context) {
return SASL_NOMECH;
} else {
context = cur->context;
mech_context_list_entry = cur;
}
break;
}
}
if (conn->props.min_ssf < conn->external.ssf) {
minssf = 0;
} else {
minssf = conn->props.min_ssf - conn->external.ssf;
}
/* Generic mechanism */
if (plug->max_ssf < minssf) {
sasl_seterror(conn, SASL_NOLOG,
"mech %s is too weak", plug->mech_name);
return SASL_TOOWEAK; /* too weak */
}
if (plug->mech_avail
&& (ret = plug->mech_avail(plug->glob_context,
s_conn->sparams,
(void **)&context)) != SASL_OK ) {
if (ret == SASL_NOMECH) {
/* Mark this mech as no good for this connection */
cur = sasl_ALLOC(sizeof(context_list_t));
if (!cur) {
MEMERROR(conn);
return SASL_NOMECH;
}
cur->context = NULL;
cur->mech = mech;
cur->next = s_conn->mech_contexts;
s_conn->mech_contexts = cur;
}
/* SASL_NOTDONE might also get us here */
/* Error should be set by mech_avail call */
return SASL_NOMECH;
} else if (context) {
if (mech_context_list_entry != NULL) {
/* Update the context. It shouldn't have changed, but who knows */
mech_context_list_entry->context = context;
} else {
/* Save this context */
cur = sasl_ALLOC(sizeof(context_list_t));
if (!cur) {
MEMERROR(conn);
return SASL_NOMECH;
}
cur->context = context;
cur->mech = mech;
cur->next = s_conn->mech_contexts;
s_conn->mech_contexts = cur;
}
}
/* Generic mechanism */
if (plug->max_ssf < minssf) {
sasl_seterror(conn, SASL_NOLOG, "too weak");
return SASL_TOOWEAK; /* too weak */
}
/* if there are no users in the secrets database we can't use this
mechanism */
if (mech->m.condition == SASL_NOUSER) {
sasl_seterror(conn, 0, "no users in secrets db");
return SASL_NOMECH;
}
/* Can it meet our features? */
if ((conn->flags & SASL_NEED_PROXY) &&
!(plug->features & SASL_FEAT_ALLOWS_PROXY)) {
return SASL_NOMECH;
}
if ((conn->flags & SASL_NEED_HTTP) &&
!(plug->features & SASL_FEAT_SUPPORTS_HTTP)) {
return SASL_NOMECH;
}
/* security properties---if there are any flags that differ and are
in what the connection are requesting, then fail */
/* special case plaintext */
myflags = conn->props.security_flags;
/* if there's an external layer this is no longer plaintext */
if ((conn->props.min_ssf <= conn->external.ssf) &&
(conn->external.ssf > 1)) {
myflags &= ~SASL_SEC_NOPLAINTEXT;
}
/* do we want to special case SASL_SEC_PASS_CREDENTIALS? nah.. */
if ((myflags &= (myflags ^ plug->security_flags)) != 0) {
sasl_seterror(conn, SASL_NOLOG,
"security flags do not match required");
return (myflags & SASL_SEC_NOPLAINTEXT) ? SASL_ENCRYPT : SASL_NOMECH;
}
/* Check Features */
if (plug->features & SASL_FEAT_GETSECRET) {
/* We no longer support sasl_server_{get,put}secret */
sasl_seterror(conn, 0,
"mech %s requires unprovided secret facility",
plug->mech_name);
return SASL_NOMECH;
}
return SASL_OK;
}
/*
* make the authorization
*
*/
static int do_authorization(sasl_server_conn_t *s_conn)
{
int ret;
sasl_authorize_t *authproc;
void *auth_context;
/* now let's see if authname is allowed to proxy for username! */
/* check the proxy callback */
if (_sasl_getcallback(&s_conn->base, SASL_CB_PROXY_POLICY,
(sasl_callback_ft *)&authproc, &auth_context) != SASL_OK) {
INTERROR(&s_conn->base, SASL_NOAUTHZ);
}
ret = authproc(&(s_conn->base), auth_context,
s_conn->base.oparams.user, s_conn->base.oparams.ulen,
s_conn->base.oparams.authid, s_conn->base.oparams.alen,
s_conn->user_realm,
(s_conn->user_realm ? (unsigned) strlen(s_conn->user_realm) : 0),
s_conn->sparams->propctx);
RETURN(&s_conn->base, ret);
}
/* start a mechanism exchange within a connection context
* mech -- the mechanism name client requested
* clientin -- client initial response (NUL terminated), NULL if empty
* clientinlen -- length of initial response
* serverout -- initial server challenge, NULL if done
* (library handles freeing this string)
* serveroutlen -- length of initial server challenge
* output:
* pconn -- the connection negotiation state on success
*
* Same returns as sasl_server_step() or
* SASL_NOMECH if mechanism not available.
*/
int sasl_server_start(sasl_conn_t *conn,
const char *mech,
const char *clientin,
unsigned clientinlen,
const char **serverout,
unsigned *serveroutlen)
{
sasl_server_conn_t *s_conn=(sasl_server_conn_t *) conn;
int result;
context_list_t *cur, **prev;
mechanism_t *m;
size_t mech_len;
int plus = 0;
if (_sasl_server_active==0) return SASL_NOTINIT;
/* check parameters */
if(!conn) return SASL_BADPARAM;
if (!mech || ((clientin == NULL) && (clientinlen > 0)))
PARAMERROR(conn);
if (serverout) *serverout = NULL;
if (serveroutlen) *serveroutlen = 0;
/* make sure mech is valid mechanism
if not return appropriate error */
m = s_conn->mech_list;
mech_len = strlen(mech);
while (m != NULL) {
if (_sasl_is_equal_mech(mech, m->m.plug->mech_name, mech_len, &plus)) {
break;
}
m = m->next;
}
if (m == NULL) {
sasl_seterror(conn, 0, "Couldn't find mech %s", mech);
result = SASL_NOMECH;
goto done;
}
/* Make sure that we're willing to use this mech */
if ((result = mech_permitted(conn, m)) != SASL_OK) {
goto done;
}
if (m->m.condition == SASL_CONTINUE) {
sasl_server_plug_init_t *entry_point = NULL;
void *library = NULL;
sasl_server_plug_t *pluglist = NULL;
int version, plugcount;
int l = 0;
/* need to load this plugin */
result = _sasl_get_plugin(m->m.f,
_sasl_find_verifyfile_callback(global_callbacks.callbacks),
&library);
if (result == SASL_OK) {
result = _sasl_locate_entry(library, "sasl_server_plug_init",
(void **)&entry_point);
}
if (result == SASL_OK) {
result = entry_point(mechlist->utils, SASL_SERVER_PLUG_VERSION,
&version, &pluglist, &plugcount);
}
if (result == SASL_OK) {
/* find the correct mechanism in this plugin */
for (l = 0; l < plugcount; l++) {
if (!strcasecmp(pluglist[l].mech_name,
m->m.plug->mech_name)) break;
}
if (l == plugcount) {
result = SASL_NOMECH;
}
}
if (result == SASL_OK) {
/* check that the parameters are the same */
if ((pluglist[l].max_ssf != m->m.plug->max_ssf) ||
(pluglist[l].security_flags != m->m.plug->security_flags)) {
_sasl_log(conn, SASL_LOG_ERR,
"%s: security parameters don't match mechlist file",
pluglist[l].mech_name);
result = SASL_NOMECH;
}
}
if (result == SASL_OK) {
/* copy mechlist over */
sasl_FREE((sasl_server_plug_t *) m->m.plug);
m->m.plug = &pluglist[l];
m->m.condition = SASL_OK;
}
if (result != SASL_OK) {
/* The library will eventually be freed, don't sweat it */
RETURN(conn, result);
}
}
if (conn->context) {
s_conn->mech->m.plug->mech_dispose(conn->context,
s_conn->sparams->utils);
conn->context = NULL;
}
/* We used to setup sparams HERE, but now it's done
inside of mech_permitted (which is called above) */
prev = &s_conn->mech_contexts;
for (cur = *prev; cur; prev=&cur->next,cur=cur->next) {
if (cur->mech == m) {
if (!cur->context) {
sasl_seterror(conn, 0,
"Got past mech_permitted with a disallowed mech!");
return SASL_NOMECH;
}
/* If we find it, we need to pull cur out of the
list so it won't be freed later! */
*prev = cur->next;
conn->context = cur->context;
sasl_FREE(cur);
break;
}
}
s_conn->mech = m;
if (!conn->context) {
/* Note that we don't hand over a new challenge */
result = s_conn->mech->m.plug->mech_new(s_conn->mech->m.plug->glob_context,
s_conn->sparams,
NULL,
0,
&(conn->context));
} else {
/* the work was already done by mech_avail! */
result = SASL_OK;
}
if (result == SASL_OK) {
if (clientin) {
if (s_conn->mech->m.plug->features & SASL_FEAT_SERVER_FIRST) {
/* Remote sent first, but mechanism does not support it.
* RFC 2222 says we fail at this point. */
sasl_seterror(conn,
0,
"Remote sent first but mech does not allow it.");
result = SASL_BADPROT;
} else {
/* Mech wants client-first, so let them have it */
result = sasl_server_step(conn,
clientin,
clientinlen,
serverout,
serveroutlen);
}
} else {
if (s_conn->mech->m.plug->features & SASL_FEAT_WANT_CLIENT_FIRST) {
/* Mech wants client first anyway, so we should do that */
if (serverout) *serverout = "";
if (serveroutlen) *serveroutlen = 0;
result = SASL_CONTINUE;
} else {
/* Mech wants server-first, so let them have it */
result = sasl_server_step(conn,
clientin,
clientinlen,
serverout,
serveroutlen);
}
}
}
done:
if ( result != SASL_OK
&& result != SASL_CONTINUE
&& result != SASL_INTERACT) {
if (conn->context) {
s_conn->mech->m.plug->mech_dispose(conn->context,
s_conn->sparams->utils);
conn->context = NULL;
}
conn->oparams.doneflag = 0;
}
RETURN(conn,result);
}
/* perform one step of the SASL exchange
* clientinlen & clientin -- client data
* NULL on first step if no optional client step
* serveroutlen & serverout -- set to the server data to transmit
* to the client in the next step
* (library handles freeing this)
*
* returns:
* SASL_OK -- exchange is complete.
* SASL_CONTINUE -- indicates another step is necessary.
* SASL_TRANS -- entry for user exists, but not for mechanism
* and transition is possible
* SASL_BADPARAM -- service name needed
* SASL_BADPROT -- invalid input from client
* ...
*/
int sasl_server_step(sasl_conn_t *conn,
const char *clientin,
unsigned clientinlen,
const char **serverout,
unsigned *serveroutlen)
{
int ret;
sasl_server_conn_t *s_conn = (sasl_server_conn_t *) conn; /* cast */
/* check parameters */
if (_sasl_server_active==0) return SASL_NOTINIT;
if (!conn) return SASL_BADPARAM;
if ((clientin==NULL) && (clientinlen>0))
PARAMERROR(conn);
/* If we've already done the last send, return! */
if (s_conn->sent_last == 1) {
return SASL_OK;
}
/* Don't do another step if the plugin told us that we're done */
if (conn->oparams.doneflag) {
_sasl_log(conn, SASL_LOG_ERR, "attempting server step after doneflag");
return SASL_FAIL;
}
if (serverout) *serverout = NULL;
if (serveroutlen) *serveroutlen = 0;
ret = s_conn->mech->m.plug->mech_step(conn->context,
s_conn->sparams,
clientin,
clientinlen,
serverout,
serveroutlen,
&conn->oparams);
if (ret == SASL_OK) {
ret = do_authorization(s_conn);
}
if (ret == SASL_OK) {
/* if we're done, we need to watch out for the following:
* 1. the mech does server-send-last
* 2. the protocol does not
*
* in this case, return SASL_CONTINUE and remember we are done.
*/
if(*serverout && !(conn->flags & SASL_SUCCESS_DATA)) {
s_conn->sent_last = 1;
ret = SASL_CONTINUE;
}
if(!conn->oparams.maxoutbuf) {
conn->oparams.maxoutbuf = conn->props.maxbufsize;
}
/* Validate channel bindings */
switch (conn->oparams.cbindingdisp) {
case SASL_CB_DISP_NONE:
if (SASL_CB_CRITICAL(s_conn->sparams)) {
sasl_seterror(conn, 0,
"server requires channel binding but client provided none");
ret = SASL_BADBINDING;
}
break;
case SASL_CB_DISP_WANT:
if (SASL_CB_PRESENT(s_conn->sparams)) {
sasl_seterror(conn, 0,
"client incorrectly assumed server had no channel binding");
ret = SASL_BADAUTH;
}
break;
case SASL_CB_DISP_USED:
if (!SASL_CB_PRESENT(s_conn->sparams)) {
sasl_seterror(conn, 0,
"client provided channel binding but server had none");
ret = SASL_BADBINDING;
} else if (strcmp(conn->oparams.cbindingname,
s_conn->sparams->cbinding->name) != 0) {
sasl_seterror(conn, 0,
"client channel binding %s does not match server %s",
conn->oparams.cbindingname, s_conn->sparams->cbinding->name);
ret = SASL_BADBINDING;
}
break;
}
if (ret == SASL_OK &&
(conn->oparams.user == NULL || conn->oparams.authid == NULL)) {
sasl_seterror(conn, 0,
"mech did not call canon_user for both authzid " \
"and authid");
ret = SASL_BADPROT;
}
}
if ( ret != SASL_OK
&& ret != SASL_CONTINUE
&& ret != SASL_INTERACT) {
if (conn->context) {
s_conn->mech->m.plug->mech_dispose(conn->context,
s_conn->sparams->utils);
conn->context = NULL;
}
conn->oparams.doneflag = 0;
}
RETURN(conn, ret);
}
/* returns the length of all the mechanisms
* added up
*/
static unsigned mech_names_len(mechanism_t *mech_list)
{
mechanism_t *listptr;
unsigned result = 0;
for (listptr = mech_list;
listptr;
listptr = listptr->next)
result += (unsigned) strlen(listptr->m.plug->mech_name);
return result;
}
/* This returns a list of mechanisms in a NUL-terminated string
*
* The default behavior is to separate with spaces if sep == NULL
*/
int _sasl_server_listmech(sasl_conn_t *conn,
const char *user __attribute__((unused)),
const char *prefix,
const char *sep,
const char *suffix,
const char **result,
unsigned *plen,
int *pcount)
{
sasl_server_conn_t *s_conn = (sasl_server_conn_t *) conn; /* cast */
int lup;
mechanism_t *listptr;
int ret;
size_t resultlen;
int flag;
const char *mysep;
/* if there hasn't been a sasl_sever_init() fail */
if (_sasl_server_active==0) return SASL_NOTINIT;
if (!conn) return SASL_BADPARAM;
if (conn->type != SASL_CONN_SERVER) PARAMERROR(conn);
if (! result)
PARAMERROR(conn);
if (plen != NULL)
*plen = 0;
if (pcount != NULL)
*pcount = 0;
if (sep) {
mysep = sep;
} else {
mysep = " ";
}
if (!s_conn->mech_list || s_conn->mech_length <= 0)
INTERROR(conn, SASL_NOMECH);
resultlen = (prefix ? strlen(prefix) : 0)
+ (strlen(mysep) * (s_conn->mech_length - 1) * 2)
+ (mech_names_len(s_conn->mech_list) * 2) /* including -PLUS variant */
+ (s_conn->mech_length * (sizeof("-PLUS") - 1))
+ (suffix ? strlen(suffix) : 0)
+ 1;
ret = _buf_alloc(&conn->mechlist_buf,
&conn->mechlist_buf_len, resultlen);
if(ret != SASL_OK) MEMERROR(conn);
if (prefix)
strcpy (conn->mechlist_buf,prefix);
else
*(conn->mechlist_buf) = '\0';
listptr = s_conn->mech_list;
flag = 0;
/* make list */
for (lup = 0; lup < s_conn->mech_length; lup++) {
/* currently, we don't use the "user" parameter for anything */
if (mech_permitted(conn, listptr) == SASL_OK) {
/*
* If the server would never succeed in the authentication of
* the non-PLUS-variant due to policy reasons, it MUST advertise
* only the PLUS-variant.
*/
if ((listptr->m.plug->features & SASL_FEAT_CHANNEL_BINDING) &&
SASL_CB_PRESENT(s_conn->sparams)) {
if (pcount != NULL) {
(*pcount)++;
}
if (flag) {
strcat(conn->mechlist_buf, mysep);
} else {
flag = 1;
}
strcat(conn->mechlist_buf, listptr->m.plug->mech_name);
strcat(conn->mechlist_buf, "-PLUS");
}
/*
* If the server cannot support channel binding, it SHOULD
* advertise only the non-PLUS-variant. Here, supporting channel
* binding means the underlying SASL mechanism supports it and
* the application has set some channel binding data.
*/
if (!SASL_CB_PRESENT(s_conn->sparams) ||
!SASL_CB_CRITICAL(s_conn->sparams)) {
if (pcount != NULL) {
(*pcount)++;
}
if (flag) {
strcat(conn->mechlist_buf, mysep);
} else {
flag = 1;
}
strcat(conn->mechlist_buf, listptr->m.plug->mech_name);
}
}
listptr = listptr->next;
}
if (suffix)
strcat(conn->mechlist_buf,suffix);
if (plen!=NULL)
*plen = (unsigned) strlen(conn->mechlist_buf);
*result = conn->mechlist_buf;
return SASL_OK;
}
sasl_string_list_t *_sasl_server_mechs(void)
{
mechanism_t *listptr;
sasl_string_list_t *retval = NULL, *next=NULL;
if(!_sasl_server_active) return NULL;
/* make list */
for (listptr = mechlist->mech_list; listptr; listptr = listptr->next) {
next = sasl_ALLOC(sizeof(sasl_string_list_t));
if(!next && !retval) return NULL;
else if(!next) {
next = retval->next;
do {
sasl_FREE(retval);
retval = next;
next = retval->next;
} while(next);
return NULL;
}
next->d = listptr->m.plug->mech_name;
if(!retval) {
next->next = NULL;
retval = next;
} else {
next->next = retval;
retval = next;
}
}
return retval;
}
#define EOSTR(s,n) (((s)[n] == '\0') || ((s)[n] == ' ') || ((s)[n] == '\t'))
static int is_mech(const char *t, const char *m)
{
size_t sl = strlen(m);
return ((!strncasecmp(m, t, sl)) && EOSTR(t, sl));
}
/* returns OK if it's valid */
static int _sasl_checkpass(sasl_conn_t *conn,
const char *user,
unsigned userlen,
const char *pass,
unsigned passlen)
{
sasl_server_conn_t *s_conn = (sasl_server_conn_t *) conn;
int result;
sasl_getopt_t *getopt;
sasl_server_userdb_checkpass_t *checkpass_cb;
void *context;
const char *mlist = NULL, *mech = NULL;
struct sasl_verify_password_s *v;
const char *service = conn->service;
if (!userlen) userlen = (unsigned) strlen(user);
if (!passlen) passlen = (unsigned) strlen(pass);
/* call userdb callback function, if available */
result = _sasl_getcallback(conn, SASL_CB_SERVER_USERDB_CHECKPASS,
(sasl_callback_ft *)&checkpass_cb, &context);
if(result == SASL_OK && checkpass_cb) {
result = checkpass_cb(conn, context, user, pass, passlen,
s_conn->sparams->propctx);
if(result == SASL_OK)
return SASL_OK;
}
/* figure out how to check (i.e. auxprop or saslauthd or pwcheck) */
if (_sasl_getcallback(conn, SASL_CB_GETOPT, (sasl_callback_ft *)&getopt, &context)
== SASL_OK) {
getopt(context, NULL, "pwcheck_method", &mlist, NULL);
}
if(!mlist) mlist = DEFAULT_CHECKPASS_MECH;
result = SASL_NOMECH;
mech = mlist;
while (*mech && result != SASL_OK) {
for (v = _sasl_verify_password; v->name; v++) {
if(is_mech(mech, v->name)) {
result = v->verify(conn, user, pass, service,
s_conn->user_realm);
break;
}
}
if (result != SASL_OK) {
/* skip to next mech in list */
while (*mech && !isspace((int) *mech)) mech++;
while (*mech && isspace((int) *mech)) mech++;
}
else if (!is_mech(mech, "auxprop") && s_conn->sparams->transition) {
s_conn->sparams->transition(conn, pass, passlen);
}
}
if (result == SASL_NOMECH) {
/* no mechanism available ?!? */
_sasl_log(conn, SASL_LOG_ERR, "unknown password verifier(s) %s", mlist);
}
if (result != SASL_OK)
sasl_seterror(conn, SASL_NOLOG, "checkpass failed");
RETURN(conn, result);
}
/* check if a plaintext password is valid
* if user is NULL, check if plaintext passwords are enabled
* inputs:
* user -- user to query in current user_domain
* userlen -- length of username, 0 = strlen(user)
* pass -- plaintext password to check
* passlen -- length of password, 0 = strlen(pass)
* returns
* SASL_OK -- success
* SASL_NOMECH -- mechanism not supported
* SASL_NOVERIFY -- user found, but no verifier
* SASL_NOUSER -- user not found
*/
int sasl_checkpass(sasl_conn_t *conn,
const char *user,
unsigned userlen,
const char *pass,
unsigned passlen)
{
int result;
if (_sasl_server_active==0) return SASL_NOTINIT;
/* check if it's just a query if we are enabled */
if (!user)
return SASL_OK;
if (!conn) return SASL_BADPARAM;
/* check params */
if (pass == NULL)
PARAMERROR(conn);
/* canonicalize the username */
result = _sasl_canon_user(conn, user, userlen,
SASL_CU_AUTHID | SASL_CU_AUTHZID,
&(conn->oparams));
if(result != SASL_OK) RETURN(conn, result);
user = conn->oparams.user;
/* Check the password and lookup additional properties */
result = _sasl_checkpass(conn, user, userlen, pass, passlen);
/* Do authorization */
if(result == SASL_OK) {
result = do_authorization((sasl_server_conn_t *)conn);
}
RETURN(conn,result);
}
/* check if a user exists on server
* conn -- connection context (may be NULL, used to hold last error)
* service -- registered name of the service using SASL (e.g. "imap")
* user_realm -- permits multiple user realms on server, NULL = default
* user -- NUL terminated user name
*
* returns:
* SASL_OK -- success
* SASL_DISABLED -- account disabled [FIXME: currently not detected]
* SASL_NOUSER -- user not found
* SASL_NOVERIFY -- user found, but no usable mechanism [FIXME: not supported]
* SASL_NOMECH -- no mechanisms enabled
* SASL_UNAVAIL -- remote authentication server unavailable, try again later
*/
int sasl_user_exists(sasl_conn_t *conn,
const char *service,
const char *user_realm,
const char *user)
{
int result=SASL_NOMECH;
const char *mlist = NULL, *mech = NULL;
void *context;
sasl_getopt_t *getopt;
struct sasl_verify_password_s *v;
/* check params */
if (_sasl_server_active==0) return SASL_NOTINIT;
if (!conn) return SASL_BADPARAM;
if (!user || conn->type != SASL_CONN_SERVER)
PARAMERROR(conn);
if(!service) service = conn->service;
/* figure out how to check (i.e. auxprop or saslauthd or pwcheck) */
if (_sasl_getcallback(conn, SASL_CB_GETOPT, (sasl_callback_ft *)&getopt, &context)
== SASL_OK) {
getopt(context, NULL, "pwcheck_method", &mlist, NULL);
}
if(!mlist) mlist = DEFAULT_CHECKPASS_MECH;
result = SASL_NOMECH;
mech = mlist;
while (*mech && result != SASL_OK) {
for (v = _sasl_verify_password; v->name; v++) {
if(is_mech(mech, v->name)) {
result = v->verify(conn, user, NULL, service, user_realm);
break;
}
}
if (result != SASL_OK) {
/* skip to next mech in list */
while (*mech && !isspace((int) *mech)) mech++;
while (*mech && isspace((int) *mech)) mech++;
}
}
/* Screen out the SASL_BADPARAM response
* we'll get from not giving a password */
if (result == SASL_BADPARAM) {
result = SASL_OK;
}
if (result == SASL_NOMECH) {
/* no mechanism available ?!? */
_sasl_log(conn, SASL_LOG_ERR, "no plaintext password verifier?");
sasl_seterror(conn, SASL_NOLOG, "no plaintext password verifier?");
}
RETURN(conn, result);
}
/* check if an apop exchange is valid
* (note this is an optional part of the SASL API)
* if challenge is NULL, just check if APOP is enabled
* inputs:
* challenge -- challenge which was sent to client
* challen -- length of challenge, 0 = strlen(challenge)
* response -- client response, "<user> <digest>" (RFC 1939)
* resplen -- length of response, 0 = strlen(response)
* returns
* SASL_OK -- success
* SASL_BADAUTH -- authentication failed
* SASL_BADPARAM -- missing challenge
* SASL_BADPROT -- protocol error (e.g., response in wrong format)
* SASL_NOVERIFY -- user found, but no verifier
* SASL_NOMECH -- mechanism not supported
* SASL_NOUSER -- user not found
*/
int sasl_checkapop(sasl_conn_t *conn,
#ifdef DO_SASL_CHECKAPOP
const char *challenge,
unsigned challen __attribute__((unused)),
const char *response,
unsigned resplen __attribute__((unused)))
#else
const char *challenge __attribute__((unused)),
unsigned challen __attribute__((unused)),
const char *response __attribute__((unused)),
unsigned resplen __attribute__((unused)))
#endif
{
#ifdef DO_SASL_CHECKAPOP
sasl_server_conn_t *s_conn = (sasl_server_conn_t *) conn;
char *user, *user_end;
const char *password_request[] = { SASL_AUX_PASSWORD, NULL };
size_t user_len;
int result;
if (_sasl_server_active==0)
return SASL_NOTINIT;
/* check if it's just a query if we are enabled */
if(!challenge)
return SASL_OK;
/* check params */
if (!conn) return SASL_BADPARAM;
if (!response)
PARAMERROR(conn);
/* Parse out username and digest.
*
* Per RFC 1939, response must be "<user> <digest>", where
* <digest> is a 16-octet value which is sent in hexadecimal
* format, using lower-case ASCII characters.
*/
user_end = strrchr(response, ' ');
if (!user_end || strspn(user_end + 1, "0123456789abcdef") != 32)
{
sasl_seterror(conn, 0, "Bad Digest");
RETURN(conn,SASL_BADPROT);
}
user_len = (size_t)(user_end - response);
user = sasl_ALLOC(user_len + 1);
memcpy(user, response, user_len);
user[user_len] = '\0';
result = prop_request(s_conn->sparams->propctx, password_request);
if(result != SASL_OK)
{
sasl_FREE(user);
RETURN(conn, result);
}
/* erase the plaintext password */
s_conn->sparams->utils->prop_erase(s_conn->sparams->propctx,
password_request[0]);
/* canonicalize the username and lookup any associated properties */
result = _sasl_canon_user_lookup (conn,
user,
user_len,
SASL_CU_AUTHID | SASL_CU_AUTHZID,
&(conn->oparams));
sasl_FREE(user);
if(result != SASL_OK) RETURN(conn, result);
/* Do APOP verification */
result = _sasl_auxprop_verify_apop(conn, conn->oparams.authid,
challenge, user_end + 1, s_conn->user_realm);
/* Do authorization */
if(result == SASL_OK) {
result = do_authorization((sasl_server_conn_t *)conn);
} else {
/* If verification failed, we don't want to encourage getprop to work */
conn->oparams.user = NULL;
conn->oparams.authid = NULL;
}
RETURN(conn, result);
#else /* sasl_checkapop was disabled at compile time */
sasl_seterror(conn, SASL_NOLOG,
"sasl_checkapop called, but was disabled at compile time");
RETURN(conn, SASL_NOMECH);
#endif /* DO_SASL_CHECKAPOP */
}
/* It would be nice if we can show other information like Author, Company, Year, plugin version */
static void
_sasl_print_mechanism (
server_sasl_mechanism_t *m,
sasl_info_callback_stage_t stage,
void *rock __attribute__((unused))
)
{
char delimiter;
if (stage == SASL_INFO_LIST_START) {
printf ("List of server plugins follows\n");
return;
} else if (stage == SASL_INFO_LIST_END) {
return;
}
/* Process the mechanism */
printf ("Plugin \"%s\" ", m->plugname);
switch (m->condition) {
case SASL_OK:
printf ("[loaded]");
break;
case SASL_CONTINUE:
printf ("[delayed]");
break;
case SASL_NOUSER:
printf ("[no users]");
break;
default:
printf ("[unknown]");
break;
}
printf (", \tAPI version: %d\n", m->version);
if (m->plug != NULL) {
printf ("\tSASL mechanism: %s, best SSF: %d, supports setpass: %s\n",
m->plug->mech_name,
m->plug->max_ssf,
(m->plug->setpass != NULL) ? "yes" : "no"
);
printf ("\tsecurity flags:");
delimiter = ' ';
if (m->plug->security_flags & SASL_SEC_NOANONYMOUS) {
printf ("%cNO_ANONYMOUS", delimiter);
delimiter = '|';
}
if (m->plug->security_flags & SASL_SEC_NOPLAINTEXT) {
printf ("%cNO_PLAINTEXT", delimiter);
delimiter = '|';
}
if (m->plug->security_flags & SASL_SEC_NOACTIVE) {
printf ("%cNO_ACTIVE", delimiter);
delimiter = '|';
}
if (m->plug->security_flags & SASL_SEC_NODICTIONARY) {
printf ("%cNO_DICTIONARY", delimiter);
delimiter = '|';
}
if (m->plug->security_flags & SASL_SEC_FORWARD_SECRECY) {
printf ("%cFORWARD_SECRECY", delimiter);
delimiter = '|';
}
if (m->plug->security_flags & SASL_SEC_PASS_CREDENTIALS) {
printf ("%cPASS_CREDENTIALS", delimiter);
delimiter = '|';
}
if (m->plug->security_flags & SASL_SEC_MUTUAL_AUTH) {
printf ("%cMUTUAL_AUTH", delimiter);
delimiter = '|';
}
printf ("\n\tfeatures:");
delimiter = ' ';
if (m->plug->features & SASL_FEAT_WANT_CLIENT_FIRST) {
printf ("%cWANT_CLIENT_FIRST", delimiter);
delimiter = '|';
}
if (m->plug->features & SASL_FEAT_SERVER_FIRST) {
printf ("%cSERVER_FIRST", delimiter);
delimiter = '|';
}
if (m->plug->features & SASL_FEAT_ALLOWS_PROXY) {
printf ("%cPROXY_AUTHENTICATION", delimiter);
delimiter = '|';
}
if (m->plug->features & SASL_FEAT_DONTUSE_USERPASSWD) {
printf ("%cDONTUSE_USERPASSWD", delimiter);
delimiter = '|';
}
if (m->plug->features & SASL_FEAT_NEEDSERVERFQDN) {
printf ("%cNEED_SERVER_FQDN", delimiter);
delimiter = '|';
}
/* Is this one used? */
if (m->plug->features & SASL_FEAT_SERVICE) {
printf ("%cSERVICE", delimiter);
delimiter = '|';
}
if (m->plug->features & SASL_FEAT_GETSECRET) {
printf ("%cNEED_GETSECRET", delimiter);
delimiter = '|';
}
if (m->plug->features & SASL_FEAT_GSS_FRAMING) {
printf ("%cGSS_FRAMING", delimiter);
delimiter = '|';
}
if (m->plug->features & SASL_FEAT_CHANNEL_BINDING) {
printf ("%cCHANNEL_BINDING", delimiter);
delimiter = '|';
}
if (m->plug->features & SASL_FEAT_SUPPORTS_HTTP) {
printf ("%cSUPPORTS_HTTP", delimiter);
delimiter = '|';
}
}
if (m->f) {
printf ("\n\twill be loaded from \"%s\"", m->f);
}
printf ("\n");
}
/* Dump information about available server plugins (separate functions should be
used for canon and auxprop plugins */
int sasl_server_plugin_info (
const char *c_mech_list, /* space separated mechanism list or NULL for ALL */
sasl_server_info_callback_t *info_cb,
void *info_cb_rock
)
{
mechanism_t *m;
server_sasl_mechanism_t plug_data;
char * cur_mech;
char *mech_list = NULL;
char * p;
if (info_cb == NULL) {
info_cb = _sasl_print_mechanism;
}
if (mechlist != NULL) {
info_cb (NULL, SASL_INFO_LIST_START, info_cb_rock);
if (c_mech_list == NULL) {
m = mechlist->mech_list; /* m point to beginning of the list */
while (m != NULL) {
memcpy (&plug_data, &m->m, sizeof(plug_data));
info_cb (&plug_data, SASL_INFO_LIST_MECH, info_cb_rock);
m = m->next;
}
} else {
mech_list = strdup(c_mech_list);
cur_mech = mech_list;
while (cur_mech != NULL) {
p = strchr (cur_mech, ' ');
if (p != NULL) {
*p = '\0';
p++;
}
m = mechlist->mech_list; /* m point to beginning of the list */
while (m != NULL) {
if (strcasecmp (cur_mech, m->m.plug->mech_name) == 0) {
memcpy (&plug_data, &m->m, sizeof(plug_data));
info_cb (&plug_data, SASL_INFO_LIST_MECH, info_cb_rock);
}
m = m->next;
}
cur_mech = p;
}
free (mech_list);
}
info_cb (NULL, SASL_INFO_LIST_END, info_cb_rock);
return (SASL_OK);
}
return (SASL_NOTINIT);
}
|