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
|
/*
* User, system, and password routines for CUPS.
*
* Copyright © 2020-2024 by OpenPrinting.
* Copyright © 2007-2019 by Apple Inc.
* Copyright © 1997-2006 by Easy Software Products.
*
* Licensed under Apache License v2.0. See the file "LICENSE" for more
* information.
*/
/*
* Include necessary headers...
*/
#include "cups-private.h"
#include "debug-internal.h"
#include <stdlib.h>
#include <sys/stat.h>
#ifdef _WIN32
# include <windows.h>
#else
# include <pwd.h>
# include <termios.h>
# include <sys/utsname.h>
#endif /* _WIN32 */
#ifdef __APPLE__
# include <sys/sysctl.h>
#endif /* __APPLE__ */
/*
* Local constants...
*/
#ifdef __APPLE__
# if TARGET_OS_OSX
# define kCUPSPrintingPrefs CFSTR("org.cups.PrintingPrefs")
# define kPREFIX ""
# else
# define kCUPSPrintingPrefs CFSTR(".GlobalPreferences")
# define kPREFIX "AirPrint"
# endif /* TARGET_OS_OSX */
# define kDigestOptionsKey CFSTR(kPREFIX "DigestOptions")
# define kUserKey CFSTR(kPREFIX "User")
# define kUserAgentTokensKey CFSTR(kPREFIX "UserAgentTokens")
# define kAllowAnyRootKey CFSTR(kPREFIX "AllowAnyRoot")
# define kAllowExpiredCertsKey CFSTR(kPREFIX "AllowExpiredCerts")
# define kEncryptionKey CFSTR(kPREFIX "Encryption")
# define kGSSServiceNameKey CFSTR(kPREFIX "GSSServiceName")
# define kSSLOptionsKey CFSTR(kPREFIX "SSLOptions")
# define kTrustOnFirstUseKey CFSTR(kPREFIX "TrustOnFirstUse")
# define kValidateCertsKey CFSTR(kPREFIX "ValidateCerts")
/* Deprecated */
# define kAllowRC4 CFSTR(kPREFIX "AllowRC4")
# define kAllowSSL3 CFSTR(kPREFIX "AllowSSL3")
# define kAllowDH CFSTR(kPREFIX "AllowDH")
#endif /* __APPLE__ */
#define _CUPS_PASSCHAR '*' /* Character that is echoed for password */
/*
* Local types...
*/
typedef struct _cups_client_conf_s /**** client.conf config data ****/
{
_cups_digestoptions_t digestoptions; /* DigestOptions values */
_cups_uatokens_t uatokens; /* UserAgentTokens values */
#ifdef HAVE_TLS
int ssl_options, /* SSLOptions values */
ssl_min_version,/* Minimum SSL/TLS version */
ssl_max_version;/* Maximum SSL/TLS version */
#endif /* HAVE_TLS */
int trust_first, /* Trust on first use? */
any_root, /* Allow any (e.g., self-signed) root */
expired_certs, /* Allow expired certs */
validate_certs; /* Validate certificates */
http_encryption_t encryption; /* Encryption setting */
char user[65], /* User name */
server_name[256];
/* Server hostname */
#ifdef HAVE_GSSAPI
char gss_service_name[32];
/* Kerberos service name */
#endif /* HAVE_GSSAPI */
} _cups_client_conf_t;
/*
* Local functions...
*/
#ifdef __APPLE__
# ifdef HAVE_TLS
static int cups_apple_get_boolean(CFStringRef key, int *value);
# endif /* HAVE_TLS */
static int cups_apple_get_string(CFStringRef key, char *value, size_t valsize);
#endif /* __APPLE__ */
static int cups_boolean_value(const char *value);
static void cups_finalize_client_conf(_cups_client_conf_t *cc);
static void cups_init_client_conf(_cups_client_conf_t *cc);
static void cups_read_client_conf(cups_file_t *fp, _cups_client_conf_t *cc);
static void cups_set_default_ipp_port(_cups_globals_t *cg);
static void cups_set_digestoptions(_cups_client_conf_t *cc, const char *value);
static void cups_set_encryption(_cups_client_conf_t *cc, const char *value);
#ifdef HAVE_GSSAPI
static void cups_set_gss_service_name(_cups_client_conf_t *cc, const char *value);
#endif /* HAVE_GSSAPI */
static void cups_set_server_name(_cups_client_conf_t *cc, const char *value);
#ifdef HAVE_TLS
static void cups_set_ssl_options(_cups_client_conf_t *cc, const char *value);
#endif /* HAVE_TLS */
static void cups_set_uatokens(_cups_client_conf_t *cc, const char *value);
static void cups_set_user(_cups_client_conf_t *cc, const char *value);
/*
* 'cupsEncryption()' - Get the current encryption settings.
*
* The default encryption setting comes from the CUPS_ENCRYPTION
* environment variable, then the ~/.cups/client.conf file, and finally the
* /etc/cups/client.conf file. If not set, the default is
* @code HTTP_ENCRYPTION_IF_REQUESTED@.
*
* Note: The current encryption setting is tracked separately for each thread
* in a program. Multi-threaded programs that override the setting via the
* @link cupsSetEncryption@ function need to do so in each thread for the same
* setting to be used.
*/
http_encryption_t /* O - Encryption settings */
cupsEncryption(void)
{
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
if (cg->encryption == (http_encryption_t)-1)
_cupsSetDefaults();
return (cg->encryption);
}
/*
* 'cupsGetPassword()' - Get a password from the user.
*
* Uses the current password callback function. Returns @code NULL@ if the
* user does not provide a password.
*
* Note: The current password callback function is tracked separately for each
* thread in a program. Multi-threaded programs that override the setting via
* the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to
* do so in each thread for the same function to be used.
*
* @exclude all@
*/
const char * /* O - Password */
cupsGetPassword(const char *prompt) /* I - Prompt string */
{
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
return ((cg->password_cb)(prompt, NULL, NULL, NULL, cg->password_data));
}
/*
* 'cupsGetPassword2()' - Get a password from the user using the current
* password callback.
*
* Uses the current password callback function. Returns @code NULL@ if the
* user does not provide a password.
*
* Note: The current password callback function is tracked separately for each
* thread in a program. Multi-threaded programs that override the setting via
* the @link cupsSetPasswordCB2@ function need to do so in each thread for the
* same function to be used.
*
* @since CUPS 1.4/macOS 10.6@
*/
const char * /* O - Password */
cupsGetPassword2(const char *prompt, /* I - Prompt string */
http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
const char *method, /* I - Request method ("GET", "POST", "PUT") */
const char *resource) /* I - Resource path */
{
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
if (!http)
http = _cupsConnect();
return ((cg->password_cb)(prompt, http, method, resource, cg->password_data));
}
/*
* 'cupsServer()' - Return the hostname/address of the current server.
*
* The default server comes from the CUPS_SERVER environment variable, then the
* ~/.cups/client.conf file, and finally the /etc/cups/client.conf file. If not
* set, the default is the local system - either "localhost" or a domain socket
* path.
*
* The returned value can be a fully-qualified hostname, a numeric IPv4 or IPv6
* address, or a domain socket pathname.
*
* Note: The current server is tracked separately for each thread in a program.
* Multi-threaded programs that override the server via the
* @link cupsSetServer@ function need to do so in each thread for the same
* server to be used.
*/
const char * /* O - Server name */
cupsServer(void)
{
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
if (!cg->server[0])
_cupsSetDefaults();
return (cg->server);
}
/*
* 'cupsSetClientCertCB()' - Set the client certificate callback.
*
* Pass @code NULL@ to restore the default callback.
*
* Note: The current certificate callback is tracked separately for each thread
* in a program. Multi-threaded programs that override the callback need to do
* so in each thread for the same callback to be used.
*
* @since CUPS 1.5/macOS 10.7@
*/
void
cupsSetClientCertCB(
cups_client_cert_cb_t cb, /* I - Callback function */
void *user_data) /* I - User data pointer */
{
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
cg->client_cert_cb = cb;
cg->client_cert_data = user_data;
}
/*
* 'cupsSetCredentials()' - Set the default credentials to be used for SSL/TLS
* connections.
*
* Note: The default credentials are tracked separately for each thread in a
* program. Multi-threaded programs that override the setting need to do so in
* each thread for the same setting to be used.
*
* @since CUPS 1.5/macOS 10.7@
*/
int /* O - Status of call (0 = success) */
cupsSetCredentials(
cups_array_t *credentials) /* I - Array of credentials */
{
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
if (cupsArrayCount(credentials) < 1)
return (-1);
#ifdef HAVE_TLS
_httpFreeCredentials(cg->tls_credentials);
cg->tls_credentials = _httpCreateCredentials(credentials);
#endif /* HAVE_TLS */
return (cg->tls_credentials ? 0 : -1);
}
/*
* 'cupsSetEncryption()' - Set the encryption preference.
*
* The default encryption setting comes from the CUPS_ENCRYPTION
* environment variable, then the ~/.cups/client.conf file, and finally the
* /etc/cups/client.conf file. If not set, the default is
* @code HTTP_ENCRYPTION_IF_REQUESTED@.
*
* Note: The current encryption setting is tracked separately for each thread
* in a program. Multi-threaded programs that override the setting need to do
* so in each thread for the same setting to be used.
*/
void
cupsSetEncryption(http_encryption_t e) /* I - New encryption preference */
{
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
cg->encryption = e;
if (cg->http)
httpEncryption(cg->http, e);
}
/*
* 'cupsSetOAuthCB()' - Set the OAuth 2.0 callback for CUPS.
*
* This function sets the OAuth 2.0 callback for the various CUPS APIs that
* send HTTP requests. Pass @code NULL@ to restore the default (console-based)
* callback.
*
* The OAuth callback receives the HTTP connection, realm name, scope name (if
* any), resource path, and the "user_data" pointer for each request that
* requires an OAuth access token. The function then returns either the Bearer
* token string or `NULL` if no authorization could be obtained.
*
* Beyond reusing the Bearer token for subsequent requests on the same HTTP
* connection, no caching of the token is done by the CUPS library. The
* callback can determine whether to refresh a cached token by examining any
* existing token returned by the @link httpGetAuthString@ function.
*
* Note: The current OAuth callback is tracked separately for each thread in a
* program. Multi-threaded programs that override the callback need to do so in
* each thread for the same callback to be used.
*
* @since CUPS 2.4@
*/
void
cupsSetOAuthCB(
cups_oauth_cb_t cb, /* I - Callback function */
void *user_data) /* I - User data pointer */
{
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
cg->oauth_cb = cb;
cg->oauth_data = user_data;
}
/*
* 'cupsSetPasswordCB()' - Set the password callback for CUPS.
*
* Pass @code NULL@ to restore the default (console) password callback, which
* reads the password from the console. Programs should call either this
* function or @link cupsSetPasswordCB2@, as only one callback can be registered
* by a program per thread.
*
* Note: The current password callback is tracked separately for each thread
* in a program. Multi-threaded programs that override the callback need to do
* so in each thread for the same callback to be used.
*
* @exclude all@
*/
void
cupsSetPasswordCB(cups_password_cb_t cb)/* I - Callback function */
{
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
if (cb == (cups_password_cb_t)0)
cg->password_cb = (cups_password_cb2_t)_cupsGetPassword;
else
cg->password_cb = (cups_password_cb2_t)cb;
cg->password_data = NULL;
}
/*
* 'cupsSetPasswordCB2()' - Set the advanced password callback for CUPS.
*
* Pass @code NULL@ to restore the default (console) password callback, which
* reads the password from the console. Programs should call either this
* function or @link cupsSetPasswordCB2@, as only one callback can be registered
* by a program per thread.
*
* Note: The current password callback is tracked separately for each thread
* in a program. Multi-threaded programs that override the callback need to do
* so in each thread for the same callback to be used.
*
* @since CUPS 1.4/macOS 10.6@
*/
void
cupsSetPasswordCB2(
cups_password_cb2_t cb, /* I - Callback function */
void *user_data) /* I - User data pointer */
{
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
if (cb == (cups_password_cb2_t)0)
cg->password_cb = (cups_password_cb2_t)_cupsGetPassword;
else
cg->password_cb = cb;
cg->password_data = user_data;
}
/*
* 'cupsSetServer()' - Set the default server name and port.
*
* The "server" string can be a fully-qualified hostname, a numeric
* IPv4 or IPv6 address, or a domain socket pathname. Hostnames and numeric IP
* addresses can be optionally followed by a colon and port number to override
* the default port 631, e.g. "hostname:8631". Pass @code NULL@ to restore the
* default server name and port.
*
* Note: The current server is tracked separately for each thread in a program.
* Multi-threaded programs that override the server need to do so in each
* thread for the same server to be used.
*/
void
cupsSetServer(const char *server) /* I - Server name */
{
char *options, /* Options */
*port; /* Pointer to port */
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
if (server)
{
strlcpy(cg->server, server, sizeof(cg->server));
if (cg->server[0] != '/' && (options = strrchr(cg->server, '/')) != NULL)
{
*options++ = '\0';
if (!strcmp(options, "version=1.0"))
cg->server_version = 10;
else if (!strcmp(options, "version=1.1"))
cg->server_version = 11;
else if (!strcmp(options, "version=2.0"))
cg->server_version = 20;
else if (!strcmp(options, "version=2.1"))
cg->server_version = 21;
else if (!strcmp(options, "version=2.2"))
cg->server_version = 22;
}
else
cg->server_version = 20;
if (cg->server[0] != '/' && (port = strrchr(cg->server, ':')) != NULL &&
!strchr(port, ']') && isdigit(port[1] & 255))
{
*port++ = '\0';
cg->ipp_port = atoi(port);
}
if (!cg->ipp_port)
cups_set_default_ipp_port(cg);
if (cg->server[0] == '/')
strlcpy(cg->servername, "localhost", sizeof(cg->servername));
else
strlcpy(cg->servername, cg->server, sizeof(cg->servername));
}
else
{
cg->server[0] = '\0';
cg->servername[0] = '\0';
cg->server_version = 20;
cg->ipp_port = 0;
}
if (cg->http)
{
httpClose(cg->http);
cg->http = NULL;
}
}
/*
* 'cupsSetServerCertCB()' - Set the server certificate callback.
*
* Pass @code NULL@ to restore the default callback.
*
* Note: The current credentials callback is tracked separately for each thread
* in a program. Multi-threaded programs that override the callback need to do
* so in each thread for the same callback to be used.
*
* @since CUPS 1.5/macOS 10.7@
*/
void
cupsSetServerCertCB(
cups_server_cert_cb_t cb, /* I - Callback function */
void *user_data) /* I - User data pointer */
{
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
cg->server_cert_cb = cb;
cg->server_cert_data = user_data;
}
/*
* 'cupsSetUser()' - Set the default user name.
*
* Pass @code NULL@ to restore the default user name.
*
* Note: The current user name is tracked separately for each thread in a
* program. Multi-threaded programs that override the user name need to do so
* in each thread for the same user name to be used.
*/
void
cupsSetUser(const char *user) /* I - User name */
{
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
if (user)
strlcpy(cg->user, user, sizeof(cg->user));
else
cg->user[0] = '\0';
}
/*
* 'cupsSetUserAgent()' - Set the default HTTP User-Agent string.
*
* Setting the string to NULL forces the default value containing the CUPS
* version, IPP version, and operating system version and architecture.
*
* @since CUPS 1.7/macOS 10.9@
*/
void
cupsSetUserAgent(const char *user_agent)/* I - User-Agent string or @code NULL@ */
{
_cups_globals_t *cg = _cupsGlobals();
/* Thread globals */
#ifdef _WIN32
SYSTEM_INFO sysinfo; /* System information */
OSVERSIONINFOW version; /* OS version info */
const char *machine; /* Hardware/machine name */
#elif defined(__APPLE__)
struct utsname name; /* uname info */
char version[256]; /* macOS/iOS version */
size_t len; /* Length of value */
#else
struct utsname name; /* uname info */
#endif /* _WIN32 */
if (user_agent)
{
strlcpy(cg->user_agent, user_agent, sizeof(cg->user_agent));
return;
}
if (cg->uatokens < _CUPS_UATOKENS_OS)
{
switch (cg->uatokens)
{
default :
case _CUPS_UATOKENS_NONE :
cg->user_agent[0] = '\0';
break;
case _CUPS_UATOKENS_PRODUCT_ONLY :
strlcpy(cg->user_agent, "CUPS IPP", sizeof(cg->user_agent));
break;
case _CUPS_UATOKENS_MAJOR :
snprintf(cg->user_agent, sizeof(cg->user_agent), "CUPS/%d IPP/2", CUPS_VERSION_MAJOR);
break;
case _CUPS_UATOKENS_MINOR :
snprintf(cg->user_agent, sizeof(cg->user_agent), "CUPS/%d.%d IPP/2.1", CUPS_VERSION_MAJOR, CUPS_VERSION_MINOR);
break;
case _CUPS_UATOKENS_MINIMAL :
strlcpy(cg->user_agent, CUPS_MINIMAL " IPP/2.1", sizeof(cg->user_agent));
break;
}
}
#ifdef _WIN32
/*
* Gather Windows version information for the User-Agent string...
*/
typedef NTSTATUS(WINAPI * RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
memset(&version, 0, sizeof(version));
version.dwOSVersionInfoSize = sizeof(version);
/* RtlGetVersion gets the current native version of Windows, even when running in compatibility mode */
RtlGetVersionPtr RtlGetVersionInternal = (RtlGetVersionPtr)GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlGetVersion");
if (RtlGetVersionInternal)
{
RtlGetVersionInternal((PRTL_OSVERSIONINFOW)&version);
}
else
{
/* Should not happen, but just in case, fallback to deprecated GetVersionExW */
GetVersionExW(&version);
}
GetNativeSystemInfo(&sysinfo);
switch (sysinfo.wProcessorArchitecture)
{
case PROCESSOR_ARCHITECTURE_AMD64 :
machine = "amd64";
break;
case PROCESSOR_ARCHITECTURE_ARM :
machine = "arm";
break;
case PROCESSOR_ARCHITECTURE_IA64 :
machine = "ia64";
break;
case PROCESSOR_ARCHITECTURE_INTEL :
machine = "intel";
break;
default :
machine = "unknown";
break;
}
if (cg->uatokens == _CUPS_UATOKENS_OS)
snprintf(cg->user_agent, sizeof(cg->user_agent), CUPS_MINIMAL " (Windows %lu.%lu) IPP/2.0", version.dwMajorVersion, version.dwMinorVersion);
else
snprintf(cg->user_agent, sizeof(cg->user_agent), CUPS_MINIMAL " (Windows %lu.%lu; %s) IPP/2.0", version.dwMajorVersion, version.dwMinorVersion, machine);
#elif defined(__APPLE__)
/*
* Gather macOS/iOS version information for the User-Agent string...
*/
uname(&name);
len = sizeof(version) - 1;
if (!sysctlbyname("kern.osproductversion", version, &len, NULL, 0))
version[len] = '\0';
else
strlcpy(version, "unknown", sizeof(version));
# if TARGET_OS_OSX
if (cg->uatokens == _CUPS_UATOKENS_OS)
snprintf(cg->user_agent, sizeof(cg->user_agent), CUPS_MINIMAL " (macOS %s) IPP/2.0", version);
else
snprintf(cg->user_agent, sizeof(cg->user_agent), CUPS_MINIMAL " (macOS %s; %s) IPP/2.0", version, name.machine);
# else
if (cg->uatokens == _CUPS_UATOKENS_OS)
snprintf(cg->user_agent, sizeof(cg->user_agent), CUPS_MINIMAL " (iOS %s) IPP/2.0", version);
else
snprintf(cg->user_agent, sizeof(cg->user_agent), CUPS_MINIMAL " (iOS %s; %s) IPP/2.0", version, name.machine);
# endif /* TARGET_OS_OSX */
#else
/*
* Gather generic UNIX version information for the User-Agent string...
*/
uname(&name);
if (cg->uatokens == _CUPS_UATOKENS_OS)
snprintf(cg->user_agent, sizeof(cg->user_agent), CUPS_MINIMAL " (%s %s) IPP/2.0", name.sysname, name.release);
else
snprintf(cg->user_agent, sizeof(cg->user_agent), CUPS_MINIMAL " (%s %s; %s) IPP/2.0", name.sysname, name.release, name.machine);
#endif /* _WIN32 */
}
/*
* 'cupsUser()' - Return the current user's name.
*
* Note: The current user name is tracked separately for each thread in a
* program. Multi-threaded programs that override the user name with the
* @link cupsSetUser@ function need to do so in each thread for the same user
* name to be used.
*/
const char * /* O - User name */
cupsUser(void)
{
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
if (!cg->user[0])
_cupsSetDefaults();
return (cg->user);
}
/*
* 'cupsUserAgent()' - Return the default HTTP User-Agent string.
*
* @since CUPS 1.7/macOS 10.9@
*/
const char * /* O - User-Agent string */
cupsUserAgent(void)
{
_cups_globals_t *cg = _cupsGlobals(); /* Thread globals */
if (!cg->user_agent[0])
cupsSetUserAgent(NULL);
return (cg->user_agent);
}
/*
* '_cupsGetPassword()' - Get a password from the user.
*/
const char * /* O - Password or @code NULL@ if none */
_cupsGetPassword(const char *prompt) /* I - Prompt string */
{
#ifdef _WIN32
HANDLE tty; /* Console handle */
DWORD mode; /* Console mode */
char passch, /* Current key press */
*passptr, /* Pointer into password string */
*passend; /* End of password string */
DWORD passbytes; /* Bytes read */
_cups_globals_t *cg = _cupsGlobals();
/* Thread globals */
/*
* Disable input echo and set raw input...
*/
if ((tty = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE)
return (NULL);
if (!GetConsoleMode(tty, &mode))
return (NULL);
if (!SetConsoleMode(tty, 0))
return (NULL);
/*
* Display the prompt...
*/
printf("%s ", prompt);
fflush(stdout);
/*
* Read the password string from /dev/tty until we get interrupted or get a
* carriage return or newline...
*/
passptr = cg->password;
passend = cg->password + sizeof(cg->password) - 1;
while (ReadFile(tty, &passch, 1, &passbytes, NULL))
{
if (passch == 0x0A || passch == 0x0D)
{
/*
* Enter/return...
*/
break;
}
else if (passch == 0x08 || passch == 0x7F)
{
/*
* Backspace/delete (erase character)...
*/
if (passptr > cg->password)
{
passptr --;
fputs("\010 \010", stdout);
}
else
putchar(0x07);
}
else if (passch == 0x15)
{
/*
* CTRL+U (erase line)
*/
if (passptr > cg->password)
{
while (passptr > cg->password)
{
passptr --;
fputs("\010 \010", stdout);
}
}
else
putchar(0x07);
}
else if (passch == 0x03)
{
/*
* CTRL+C...
*/
passptr = cg->password;
break;
}
else if ((passch & 255) < 0x20 || passptr >= passend)
putchar(0x07);
else
{
*passptr++ = passch;
putchar(_CUPS_PASSCHAR);
}
fflush(stdout);
}
putchar('\n');
fflush(stdout);
/*
* Cleanup...
*/
SetConsoleMode(tty, mode);
/*
* Return the proper value...
*/
if (passbytes == 1 && passptr > cg->password)
{
*passptr = '\0';
return (cg->password);
}
else
{
memset(cg->password, 0, sizeof(cg->password));
return (NULL);
}
#else
int tty; /* /dev/tty - never read from stdin */
struct termios original, /* Original input mode */
noecho; /* No echo input mode */
char passch, /* Current key press */
*passptr, /* Pointer into password string */
*passend; /* End of password string */
ssize_t passbytes; /* Bytes read */
_cups_globals_t *cg = _cupsGlobals();
/* Thread globals */
/*
* Disable input echo and set raw input...
*/
if ((tty = open("/dev/tty", O_RDONLY)) < 0)
return (NULL);
if (tcgetattr(tty, &original))
{
close(tty);
return (NULL);
}
noecho = original;
noecho.c_lflag &= (tcflag_t)~(ICANON | ECHO | ECHOE | ISIG);
noecho.c_cc[VMIN] = 1;
noecho.c_cc[VTIME] = 0;
if (tcsetattr(tty, TCSAFLUSH, &noecho))
{
close(tty);
return (NULL);
}
/*
* Display the prompt...
*/
printf("%s ", prompt);
fflush(stdout);
/*
* Read the password string from /dev/tty until we get interrupted or get a
* carriage return or newline...
*/
passptr = cg->password;
passend = cg->password + sizeof(cg->password) - 1;
while ((passbytes = read(tty, &passch, 1)) == 1)
{
if (passch == noecho.c_cc[VEOL] ||
# ifdef VEOL2
passch == noecho.c_cc[VEOL2] ||
# endif /* VEOL2 */
passch == 0x0A || passch == 0x0D)
{
/*
* Enter/return...
*/
break;
}
else if (passch == noecho.c_cc[VERASE] ||
passch == 0x08 || passch == 0x7F)
{
/*
* Backspace/delete (erase character)...
*/
if (passptr > cg->password)
{
passptr --;
fputs("\010 \010", stdout);
}
else
putchar(0x07);
}
else if (passch == noecho.c_cc[VKILL])
{
/*
* CTRL+U (erase line)
*/
if (passptr > cg->password)
{
while (passptr > cg->password)
{
passptr --;
fputs("\010 \010", stdout);
}
}
else
putchar(0x07);
}
else if (passch == noecho.c_cc[VINTR] || passch == noecho.c_cc[VQUIT] ||
passch == noecho.c_cc[VEOF])
{
/*
* CTRL+C, CTRL+D, or CTRL+Z...
*/
passptr = cg->password;
break;
}
else if ((passch & 255) < 0x20 || passptr >= passend)
putchar(0x07);
else
{
*passptr++ = passch;
putchar(_CUPS_PASSCHAR);
}
fflush(stdout);
}
putchar('\n');
fflush(stdout);
/*
* Cleanup...
*/
tcsetattr(tty, TCSAFLUSH, &original);
close(tty);
/*
* Return the proper value...
*/
if (passbytes == 1 && passptr > cg->password)
{
*passptr = '\0';
return (cg->password);
}
else
{
memset(cg->password, 0, sizeof(cg->password));
return (NULL);
}
#endif /* _WIN32 */
}
#ifdef HAVE_GSSAPI
/*
* '_cupsGSSServiceName()' - Get the GSS (Kerberos) service name.
*/
const char *
_cupsGSSServiceName(void)
{
_cups_globals_t *cg = _cupsGlobals(); /* Thread globals */
if (!cg->gss_service_name[0])
_cupsSetDefaults();
return (cg->gss_service_name);
}
#endif /* HAVE_GSSAPI */
/*
* '_cupsSetDefaults()' - Set the default server, port, and encryption.
*/
void
_cupsSetDefaults(void)
{
cups_file_t *fp; /* File */
char filename[1024]; /* Filename */
_cups_client_conf_t cc; /* client.conf values */
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
DEBUG_puts("_cupsSetDefaults()");
/*
* Load initial client.conf values...
*/
cups_init_client_conf(&cc);
/*
* Read the /etc/cups/client.conf and ~/.cups/client.conf files, if
* present.
*/
snprintf(filename, sizeof(filename), "%s/client.conf", cg->cups_serverroot);
if ((fp = cupsFileOpen(filename, "r")) != NULL)
{
cups_read_client_conf(fp, &cc);
cupsFileClose(fp);
}
if (cg->home)
{
/*
* Look for ~/.cups/client.conf...
*/
#if _WIN32
snprintf(filename, sizeof(filename), "%s/AppData/Local/cups/client.conf", cg->home);
#else
snprintf(filename, sizeof(filename), "%s/.cups/client.conf", cg->home);
#endif // _WIN32
if ((fp = cupsFileOpen(filename, "r")) != NULL)
{
cups_read_client_conf(fp, &cc);
cupsFileClose(fp);
}
}
/*
* Finalize things so every client.conf value is set...
*/
cups_finalize_client_conf(&cc);
cg->uatokens = cc.uatokens;
if (cg->encryption == (http_encryption_t)-1)
cg->encryption = cc.encryption;
if (!cg->server[0] || !cg->ipp_port)
cupsSetServer(cc.server_name);
if (!cg->ipp_port)
cups_set_default_ipp_port(cg);
if (!cg->user[0])
strlcpy(cg->user, cc.user, sizeof(cg->user));
#ifdef HAVE_GSSAPI
if (!cg->gss_service_name[0])
strlcpy(cg->gss_service_name, cc.gss_service_name, sizeof(cg->gss_service_name));
#endif /* HAVE_GSSAPI */
if (cg->trust_first < 0)
cg->trust_first = cc.trust_first;
if (cg->any_root < 0)
cg->any_root = cc.any_root;
if (cg->expired_certs < 0)
cg->expired_certs = cc.expired_certs;
if (cg->validate_certs < 0)
cg->validate_certs = cc.validate_certs;
#ifdef HAVE_TLS
_httpTLSSetOptions(cc.ssl_options | _HTTP_TLS_SET_DEFAULT, cc.ssl_min_version, cc.ssl_max_version);
#endif /* HAVE_TLS */
}
#ifdef __APPLE__
# ifdef HAVE_TLS
/*
* 'cups_apple_get_boolean()' - Get a boolean setting from the CUPS preferences.
*/
static int /* O - 1 if set, 0 otherwise */
cups_apple_get_boolean(
CFStringRef key, /* I - Key (name) */
int *value) /* O - Boolean value */
{
Boolean bval, /* Preference value */
bval_set; /* Value is set? */
bval = CFPreferencesGetAppBooleanValue(key, kCUPSPrintingPrefs, &bval_set);
if (bval_set)
*value = (int)bval;
return ((int)bval_set);
}
# endif /* HAVE_TLS */
/*
* 'cups_apple_get_string()' - Get a string setting from the CUPS preferences.
*/
static int /* O - 1 if set, 0 otherwise */
cups_apple_get_string(
CFStringRef key, /* I - Key (name) */
char *value, /* O - String value */
size_t valsize) /* I - Size of value buffer */
{
CFStringRef sval; /* String value */
if ((sval = CFPreferencesCopyAppValue(key, kCUPSPrintingPrefs)) != NULL)
{
Boolean result = CFStringGetCString(sval, value, (CFIndex)valsize, kCFStringEncodingUTF8);
CFRelease(sval);
if (result)
return (1);
}
return (0);
}
#endif /* __APPLE__ */
/*
* 'cups_boolean_value()' - Convert a string to a boolean value.
*/
static int /* O - Boolean value */
cups_boolean_value(const char *value) /* I - String value */
{
return (!_cups_strcasecmp(value, "yes") || !_cups_strcasecmp(value, "on") || !_cups_strcasecmp(value, "true"));
}
/*
* 'cups_finalize_client_conf()' - Finalize client.conf values.
*/
static void
cups_finalize_client_conf(
_cups_client_conf_t *cc) /* I - client.conf values */
{
const char *value; /* Environment variable */
if ((value = getenv("CUPS_TRUSTFIRST")) != NULL)
cc->trust_first = cups_boolean_value(value);
if ((value = getenv("CUPS_ANYROOT")) != NULL)
cc->any_root = cups_boolean_value(value);
if ((value = getenv("CUPS_ENCRYPTION")) != NULL)
cups_set_encryption(cc, value);
if ((value = getenv("CUPS_EXPIREDCERTS")) != NULL)
cc->expired_certs = cups_boolean_value(value);
#ifdef HAVE_GSSAPI
if ((value = getenv("CUPS_GSSSERVICENAME")) != NULL)
cups_set_gss_service_name(cc, value);
#endif /* HAVE_GSSAPI */
if ((value = getenv("CUPS_SERVER")) != NULL)
cups_set_server_name(cc, value);
if ((value = getenv("CUPS_USER")) != NULL)
cups_set_user(cc, value);
if ((value = getenv("CUPS_VALIDATECERTS")) != NULL)
cc->validate_certs = cups_boolean_value(value);
/*
* Then apply defaults for those values that haven't been set...
*/
if (cc->trust_first < 0)
cc->trust_first = 1;
if (cc->any_root < 0)
cc->any_root = 1;
if (cc->encryption == (http_encryption_t)-1)
cc->encryption = HTTP_ENCRYPTION_IF_REQUESTED;
if (cc->expired_certs < 0)
cc->expired_certs = 0;
#ifdef HAVE_GSSAPI
if (!cc->gss_service_name[0])
cups_set_gss_service_name(cc, CUPS_DEFAULT_GSSSERVICENAME);
#endif /* HAVE_GSSAPI */
if (!cc->server_name[0])
{
/*
* If we are compiled with domain socket support, only use the
* domain socket if it exists and has the right permissions...
*/
#if defined(__APPLE__) && !TARGET_OS_OSX
cups_set_server_name(cc, "/private/var/run/printd");
#else
# ifdef CUPS_DEFAULT_DOMAINSOCKET
if (!access(CUPS_DEFAULT_DOMAINSOCKET, R_OK))
cups_set_server_name(cc, CUPS_DEFAULT_DOMAINSOCKET);
else
# endif /* CUPS_DEFAULT_DOMAINSOCKET */
cups_set_server_name(cc, "localhost");
#endif /* __APPLE__ && !TARGET_OS_OSX */
}
if (!cc->user[0])
{
#ifdef _WIN32
/*
* Get the current user name from the OS...
*/
DWORD size; /* Size of string */
size = sizeof(cc->user);
if (!GetUserNameA(cc->user, &size))
#else
/*
* Try the USER environment variable as the default username...
*/
const char *envuser = getenv("USER"); /* Default username */
struct passwd pw; /* Account information */
struct passwd *result = NULL; /* Auxiliary pointer */
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
if (envuser)
{
/*
* Validate USER matches the current UID, otherwise don't allow it to
* override things... This makes sure that printing after doing su
* or sudo records the correct username.
*/
getpwnam_r(envuser, &pw, cg->pw_buf, PW_BUF_SIZE, &result);
if (result && pw.pw_uid != getuid())
result = NULL;
}
if (!result)
getpwuid_r(getuid(), &pw, cg->pw_buf, PW_BUF_SIZE, &result);
if (result)
strlcpy(cc->user, pw.pw_name, sizeof(cc->user));
else
#endif /* _WIN32 */
{
/*
* Use the default "unknown" user name...
*/
strlcpy(cc->user, "unknown", sizeof(cc->user));
}
}
if (cc->validate_certs < 0)
cc->validate_certs = 0;
}
/*
* 'cups_init_client_conf()' - Initialize client.conf values.
*/
static void
cups_init_client_conf(
_cups_client_conf_t *cc) /* I - client.conf values */
{
/*
* Clear all values to "not set"...
*/
memset(cc, 0, sizeof(_cups_client_conf_t));
cc->uatokens = _CUPS_UATOKENS_MINIMAL;
#if defined(__APPLE__) && !TARGET_OS_OSX
cups_set_user(cc, "mobile");
#endif /* __APPLE__ && !TARGET_OS_OSX */
#ifdef HAVE_TLS
cc->ssl_min_version = _HTTP_TLS_1_0;
cc->ssl_max_version = _HTTP_TLS_MAX;
#endif /* HAVE_TLS */
cc->encryption = (http_encryption_t)-1;
cc->trust_first = -1;
cc->any_root = -1;
cc->expired_certs = -1;
cc->validate_certs = -1;
/*
* Load settings from the org.cups.PrintingPrefs plist (which trump
* everything...)
*/
#if defined(__APPLE__)
char sval[1024]; /* String value */
# ifdef HAVE_TLS
int bval; /* Boolean value */
if (cups_apple_get_boolean(kAllowAnyRootKey, &bval))
cc->any_root = bval;
if (cups_apple_get_boolean(kAllowExpiredCertsKey, &bval))
cc->expired_certs = bval;
if (cups_apple_get_string(kEncryptionKey, sval, sizeof(sval)))
cups_set_encryption(cc, sval);
if (cups_apple_get_string(kSSLOptionsKey, sval, sizeof(sval)))
{
cups_set_ssl_options(cc, sval);
}
else
{
sval[0] = '\0';
if (cups_apple_get_boolean(kAllowRC4, &bval) && bval)
strlcat(sval, " AllowRC4", sizeof(sval));
if (cups_apple_get_boolean(kAllowSSL3, &bval) && bval)
strlcat(sval, " AllowSSL3", sizeof(sval));
if (cups_apple_get_boolean(kAllowDH, &bval) && bval)
strlcat(sval, " AllowDH", sizeof(sval));
if (sval[0])
cups_set_ssl_options(cc, sval);
}
if (cups_apple_get_boolean(kTrustOnFirstUseKey, &bval))
cc->trust_first = bval;
if (cups_apple_get_boolean(kValidateCertsKey, &bval))
cc->validate_certs = bval;
# endif /* HAVE_TLS */
if (cups_apple_get_string(kDigestOptionsKey, sval, sizeof(sval)))
cups_set_digestoptions(cc, sval);
if (cups_apple_get_string(kUserKey, sval, sizeof(sval)))
strlcpy(cc->user, sval, sizeof(cc->user));
if (cups_apple_get_string(kUserAgentTokensKey, sval, sizeof(sval)))
cups_set_uatokens(cc, sval);
#endif /* __APPLE__ */
}
/*
* 'cups_read_client_conf()' - Read a client.conf file.
*/
static void
cups_read_client_conf(
cups_file_t *fp, /* I - File to read */
_cups_client_conf_t *cc) /* I - client.conf values */
{
int linenum; /* Current line number */
char line[1024], /* Line from file */
*value; /* Pointer into line */
/*
* Read from the file...
*/
linenum = 0;
while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
{
if (!_cups_strcasecmp(line, "DigestOptions") && value)
cups_set_digestoptions(cc, value);
else if (!_cups_strcasecmp(line, "Encryption") && value)
cups_set_encryption(cc, value);
#ifndef __APPLE__
/*
* The ServerName directive is not supported on macOS due to app
* sandboxing restrictions, i.e. not all apps request network access.
*/
else if (!_cups_strcasecmp(line, "ServerName") && value)
cups_set_server_name(cc, value);
#endif /* !__APPLE__ */
else if (!_cups_strcasecmp(line, "User") && value)
cups_set_user(cc, value);
else if (!_cups_strcasecmp(line, "UserAgentTokens") && value)
cups_set_uatokens(cc, value);
else if (!_cups_strcasecmp(line, "TrustOnFirstUse") && value)
cc->trust_first = cups_boolean_value(value);
else if (!_cups_strcasecmp(line, "AllowAnyRoot") && value)
cc->any_root = cups_boolean_value(value);
else if (!_cups_strcasecmp(line, "AllowExpiredCerts") &&
value)
cc->expired_certs = cups_boolean_value(value);
else if (!_cups_strcasecmp(line, "ValidateCerts") && value)
cc->validate_certs = cups_boolean_value(value);
#ifdef HAVE_GSSAPI
else if (!_cups_strcasecmp(line, "GSSServiceName") && value)
cups_set_gss_service_name(cc, value);
#endif /* HAVE_GSSAPI */
#ifdef HAVE_TLS
else if (!_cups_strcasecmp(line, "SSLOptions") && value)
cups_set_ssl_options(cc, value);
#endif /* HAVE_TLS */
}
}
/*
* 'cups_set_default_ipp_port()' - Set the default IPP port value.
*/
static void
cups_set_default_ipp_port(
_cups_globals_t *cg) /* I - Global data */
{
const char *ipp_port; /* IPP_PORT environment variable */
if ((ipp_port = getenv("IPP_PORT")) != NULL)
{
if ((cg->ipp_port = atoi(ipp_port)) <= 0)
cg->ipp_port = CUPS_DEFAULT_IPP_PORT;
}
else
cg->ipp_port = CUPS_DEFAULT_IPP_PORT;
}
/*
* 'cups_set_digestoptions()' - Set the DigestOptions value.
*/
static void
cups_set_digestoptions(
_cups_client_conf_t *cc, /* I - client.conf values */
const char *value) /* I - Value */
{
if (!_cups_strcasecmp(value, "DenyMD5"))
cc->digestoptions = _CUPS_DIGESTOPTIONS_DENYMD5;
else if (!_cups_strcasecmp(value, "None"))
cc->digestoptions = _CUPS_DIGESTOPTIONS_NONE;
}
/*
* 'cups_set_encryption()' - Set the Encryption value.
*/
static void
cups_set_encryption(
_cups_client_conf_t *cc, /* I - client.conf values */
const char *value) /* I - Value */
{
if (!_cups_strcasecmp(value, "never"))
cc->encryption = HTTP_ENCRYPTION_NEVER;
else if (!_cups_strcasecmp(value, "always"))
cc->encryption = HTTP_ENCRYPTION_ALWAYS;
else if (!_cups_strcasecmp(value, "required"))
cc->encryption = HTTP_ENCRYPTION_REQUIRED;
else
cc->encryption = HTTP_ENCRYPTION_IF_REQUESTED;
}
/*
* 'cups_set_gss_service_name()' - Set the GSSServiceName value.
*/
#ifdef HAVE_GSSAPI
static void
cups_set_gss_service_name(
_cups_client_conf_t *cc, /* I - client.conf values */
const char *value) /* I - Value */
{
strlcpy(cc->gss_service_name, value, sizeof(cc->gss_service_name));
}
#endif /* HAVE_GSSAPI */
/*
* 'cups_set_server_name()' - Set the ServerName value.
*/
static void
cups_set_server_name(
_cups_client_conf_t *cc, /* I - client.conf values */
const char *value) /* I - Value */
{
strlcpy(cc->server_name, value, sizeof(cc->server_name));
}
/*
* 'cups_set_ssl_options()' - Set the SSLOptions value.
*/
#ifdef HAVE_TLS
static void
cups_set_ssl_options(
_cups_client_conf_t *cc, /* I - client.conf values */
const char *value) /* I - Value */
{
/*
* SSLOptions [AllowRC4] [AllowSSL3] [AllowDH] [DenyTLS1.0] [None]
*/
int options = _HTTP_TLS_NONE, /* SSL/TLS options */
min_version = _HTTP_TLS_1_0, /* Minimum SSL/TLS version */
max_version = _HTTP_TLS_MAX; /* Maximum SSL/TLS version */
char temp[256], /* Copy of value */
*start, /* Start of option */
*end; /* End of option */
strlcpy(temp, value, sizeof(temp));
for (start = temp; *start; start = end)
{
/*
* Find end of keyword...
*/
end = start;
while (*end && !_cups_isspace(*end))
end ++;
if (*end)
*end++ = '\0';
/*
* Compare...
*/
if (!_cups_strcasecmp(start, "AllowRC4"))
options |= _HTTP_TLS_ALLOW_RC4;
else if (!_cups_strcasecmp(start, "AllowSSL3"))
min_version = _HTTP_TLS_SSL3;
else if (!_cups_strcasecmp(start, "AllowDH"))
options |= _HTTP_TLS_ALLOW_DH;
else if (!_cups_strcasecmp(start, "DenyCBC"))
options |= _HTTP_TLS_DENY_CBC;
else if (!_cups_strcasecmp(start, "DenyTLS1.0"))
min_version = _HTTP_TLS_1_1;
else if (!_cups_strcasecmp(start, "MaxTLS1.0"))
max_version = _HTTP_TLS_1_0;
else if (!_cups_strcasecmp(start, "MaxTLS1.1"))
max_version = _HTTP_TLS_1_1;
else if (!_cups_strcasecmp(start, "MaxTLS1.2"))
max_version = _HTTP_TLS_1_2;
else if (!_cups_strcasecmp(start, "MaxTLS1.3"))
max_version = _HTTP_TLS_1_3;
else if (!_cups_strcasecmp(start, "MinTLS1.0"))
min_version = _HTTP_TLS_1_0;
else if (!_cups_strcasecmp(start, "MinTLS1.1"))
min_version = _HTTP_TLS_1_1;
else if (!_cups_strcasecmp(start, "MinTLS1.2"))
min_version = _HTTP_TLS_1_2;
else if (!_cups_strcasecmp(start, "MinTLS1.3"))
min_version = _HTTP_TLS_1_3;
else if (!_cups_strcasecmp(start, "None"))
options = _HTTP_TLS_NONE;
}
cc->ssl_options = options;
cc->ssl_max_version = max_version;
cc->ssl_min_version = min_version;
DEBUG_printf(("4cups_set_ssl_options(cc=%p, value=\"%s\") options=%x, min_version=%d, max_version=%d", (void *)cc, value, options, min_version, max_version));
}
#endif /* HAVE_TLS */
/*
* 'cups_set_uatokens()' - Set the UserAgentTokens value.
*/
static void
cups_set_uatokens(
_cups_client_conf_t *cc, /* I - client.conf values */
const char *value) /* I - Value */
{
int i; /* Looping var */
static const char * const uatokens[] =/* UserAgentTokens values */
{
"NONE",
"PRODUCTONLY",
"MAJOR",
"MINOR",
"MINIMAL",
"OS",
"FULL"
};
for (i = 0; i < (int)(sizeof(uatokens) / sizeof(uatokens[0])); i ++)
{
if (!_cups_strcasecmp(value, uatokens[i]))
{
cc->uatokens = (_cups_uatokens_t)i;
return;
}
}
}
/*
* 'cups_set_user()' - Set the User value.
*/
static void
cups_set_user(
_cups_client_conf_t *cc, /* I - client.conf values */
const char *value) /* I - Value */
{
strlcpy(cc->user, value, sizeof(cc->user));
}
|