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
|
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Sara Golemon <pollita@php.net> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ssh2.h"
void *php_ssh2_zval_from_resource_handle(int handle) {
zval *val;
zend_resource *zr;
ZEND_HASH_FOREACH_VAL(&EG(regular_list), val) {
zr = Z_RES_P(val);
if (zr->handle == handle) {
return val;
}
} ZEND_HASH_FOREACH_END();
return NULL;
}
/* **********************
* channel_stream_ops *
********************** */
#if PHP_VERSION_ID < 70400
static size_t php_ssh2_channel_stream_write(php_stream *stream, const char *buf, size_t count)
#else
static ssize_t php_ssh2_channel_stream_write(php_stream *stream, const char *buf, size_t count)
#endif
{
php_ssh2_channel_data *abstract = (php_ssh2_channel_data*)stream->abstract;
ssize_t writestate;
LIBSSH2_SESSION *session;
libssh2_channel_set_blocking(abstract->channel, abstract->is_blocking);
session = (LIBSSH2_SESSION *)zend_fetch_resource(abstract->session_rsrc, PHP_SSH2_SESSION_RES_NAME, le_ssh2_session);
#ifdef PHP_SSH2_SESSION_TIMEOUT
if (abstract->is_blocking) {
libssh2_session_set_timeout(session, abstract->timeout);
}
#endif
writestate = libssh2_channel_write_ex(abstract->channel, abstract->streamid, buf, count);
#ifdef PHP_SSH2_SESSION_TIMEOUT
if (abstract->is_blocking) {
libssh2_session_set_timeout(session, 0);
}
#endif
if (writestate == LIBSSH2_ERROR_EAGAIN) {
#if PHP_VERSION_ID < 70400
writestate = 0;
#endif
} else if (writestate < 0) {
char *error_msg = NULL;
if (libssh2_session_last_error(session, &error_msg, NULL, 0) == writestate) {
php_error_docref(NULL, E_WARNING, "Failure '%s' (%ld)", error_msg, writestate);
}
stream->eof = 1;
#if PHP_VERSION_ID < 70400
writestate = 0;
#endif
}
return writestate;
}
#if PHP_VERSION_ID < 70400
static size_t php_ssh2_channel_stream_read(php_stream *stream, char *buf, size_t count)
#else
static ssize_t php_ssh2_channel_stream_read(php_stream *stream, char *buf, size_t count)
#endif
{
php_ssh2_channel_data *abstract = (php_ssh2_channel_data*)stream->abstract;
ssize_t readstate;
LIBSSH2_SESSION *session;
stream->eof = libssh2_channel_eof(abstract->channel);
libssh2_channel_set_blocking(abstract->channel, abstract->is_blocking);
session = (LIBSSH2_SESSION *)zend_fetch_resource(abstract->session_rsrc, PHP_SSH2_SESSION_RES_NAME, le_ssh2_session);
#ifdef PHP_SSH2_SESSION_TIMEOUT
if (abstract->is_blocking) {
libssh2_session_set_timeout(session, abstract->timeout);
}
#endif
readstate = libssh2_channel_read_ex(abstract->channel, abstract->streamid, buf, count);
#ifdef PHP_SSH2_SESSION_TIMEOUT
if (abstract->is_blocking) {
libssh2_session_set_timeout(session, 0);
}
#endif
if (readstate == LIBSSH2_ERROR_EAGAIN) {
#if PHP_VERSION_ID < 70400
readstate = 0;
#endif
} else if (readstate < 0) {
char *error_msg = NULL;
if (libssh2_session_last_error(session, &error_msg, NULL, 0) == readstate) {
php_error_docref(NULL, E_WARNING, "Failure '%s' (%ld)", error_msg, readstate);
}
stream->eof = 1;
readstate = 0;
}
return readstate;
}
static int php_ssh2_channel_stream_close(php_stream *stream, int close_handle)
{
php_ssh2_channel_data *abstract = (php_ssh2_channel_data*)stream->abstract;
if (!abstract->refcount || (--(*(abstract->refcount)) == 0)) {
/* Last one out, turn off the lights */
if (abstract->refcount) {
efree(abstract->refcount);
}
libssh2_channel_eof(abstract->channel);
libssh2_channel_free(abstract->channel);
zend_list_delete(abstract->session_rsrc);
}
efree(abstract);
return 0;
}
static int php_ssh2_channel_stream_flush(php_stream *stream)
{
php_ssh2_channel_data *abstract = (php_ssh2_channel_data*)stream->abstract;
return libssh2_channel_flush_ex(abstract->channel, abstract->streamid);
}
static int php_ssh2_channel_stream_cast(php_stream *stream, int castas, void **ret)
{
php_ssh2_channel_data *abstract = (php_ssh2_channel_data*)stream->abstract;
LIBSSH2_SESSION *session;
php_ssh2_session_data **session_data;
session = (LIBSSH2_SESSION *)zend_fetch_resource(abstract->session_rsrc, PHP_SSH2_SESSION_RES_NAME, le_ssh2_session);
session_data = (php_ssh2_session_data **)libssh2_session_abstract(session);
switch (castas) {
case PHP_STREAM_AS_FD:
case PHP_STREAM_AS_FD_FOR_SELECT:
case PHP_STREAM_AS_SOCKETD:
if (ret) {
*(php_socket_t *)ret = (*session_data)->socket;
}
return SUCCESS;
default:
return FAILURE;
}
}
static int php_ssh2_channel_stream_set_option(php_stream *stream, int option, int value, void *ptrparam)
{
php_ssh2_channel_data *abstract = (php_ssh2_channel_data*)stream->abstract;
int ret;
switch (option) {
case PHP_STREAM_OPTION_BLOCKING:
ret = abstract->is_blocking;
abstract->is_blocking = value;
return ret;
break;
case PHP_STREAM_OPTION_META_DATA_API:
add_assoc_long((zval*)ptrparam, "exit_status", libssh2_channel_get_exit_status(abstract->channel));
break;
case PHP_STREAM_OPTION_READ_TIMEOUT:
ret = abstract->timeout;
#ifdef PHP_SSH2_SESSION_TIMEOUT
struct timeval tv = *(struct timeval*)ptrparam;
abstract->timeout = tv.tv_sec * 1000 + (tv.tv_usec / 1000);
#else
php_error_docref(NULL, E_WARNING, "No support for ssh2 stream timeout. Please recompile with libssh2 >= 1.2.9");
#endif
return ret;
break;
case PHP_STREAM_OPTION_CHECK_LIVENESS:
return stream->eof = libssh2_channel_eof(abstract->channel);
break;
}
return -1;
}
php_stream_ops php_ssh2_channel_stream_ops = {
php_ssh2_channel_stream_write,
php_ssh2_channel_stream_read,
php_ssh2_channel_stream_close,
php_ssh2_channel_stream_flush,
PHP_SSH2_CHANNEL_STREAM_NAME,
NULL, /* seek */
php_ssh2_channel_stream_cast,
NULL, /* stat */
php_ssh2_channel_stream_set_option,
};
/* *********************
* Magic Path Helper *
********************* */
/* {{{ php_ssh2_fopen_wraper_parse_path
* Parse an ssh2.*:// path
*/
php_url *php_ssh2_fopen_wraper_parse_path(const char *path, char *type, php_stream_context *context,
LIBSSH2_SESSION **psession, zend_resource **presource,
LIBSSH2_SFTP **psftp, zend_resource **psftp_rsrc)
{
php_ssh2_sftp_data *sftp_data = NULL;
LIBSSH2_SESSION *session;
php_url *resource;
zval *methods = NULL, *callbacks = NULL, zsession, *tmpzval;
zend_long resource_id;
char *h, *username = NULL, *password = NULL, *pubkey_file = NULL, *privkey_file = NULL;
int username_len = 0, password_len = 0;
h = strstr(path, "Resource id #");
if (h) {
/* Starting with 5.6.28, 7.0.13 need to be clean, else php_url_parse will fail */
char *tmp = estrdup(path);
strncpy(tmp + (h-path), h + sizeof("Resource id #")-1, strlen(tmp)-sizeof("Resource id #"));
resource = php_url_parse(tmp);
efree(tmp);
} else {
resource = php_url_parse(path);
}
if (!resource || !resource->path) {
return NULL;
}
if (strncmp(SSH2_URL_STR(resource->scheme), "ssh2.", sizeof("ssh2.") - 1)) {
/* Not an ssh wrapper */
php_url_free(resource);
return NULL;
}
if (strcmp(SSH2_URL_STR(resource->scheme) + sizeof("ssh2.") - 1, type)) {
/* Wrong ssh2. wrapper type */
php_url_free(resource);
return NULL;
}
if (!resource->host) {
return NULL;
}
/*
Find resource->path in the path string, then copy the entire string from the original path.
This includes ?query#fragment in the path string
*/
// TODO copy seems uneeded
#if PHP_VERSION_ID < 70300
{
char * s;
s = resource->path;
resource->path = estrdup(strstr(path, resource->path));
efree(s);
}
#else
{
zend_string *tmp;
tmp = resource->path;
resource->path = zend_string_init(ZSTR_VAL(resource->path), ZSTR_LEN(resource->path), 0);
zend_string_release(tmp);
}
#endif
/* Look for a resource ID to reuse a session */
if (is_numeric_string(SSH2_URL_STR(resource->host), SSH2_URL_LEN(resource->host), &resource_id, NULL, 0) == IS_LONG) {
php_ssh2_sftp_data *sftp_data;
zval *zresource;
if ((zresource = php_ssh2_zval_from_resource_handle(resource_id)) == NULL) {
php_url_free(resource);
return NULL;
}
if (psftp) {
/* suppress potential warning by passing NULL as resource_type_name */
sftp_data = (php_ssh2_sftp_data *)zend_fetch_resource(Z_RES_P(zresource), NULL, le_ssh2_sftp);
if (sftp_data) {
/* Want the sftp layer */
Z_ADDREF_P(zresource);
*psftp_rsrc = Z_RES_P(zresource);
*psftp = sftp_data->sftp;
*presource = sftp_data->session_rsrc;
*psession = sftp_data->session;
return resource;
}
}
session = (LIBSSH2_SESSION *)zend_fetch_resource(Z_RES_P(zresource), PHP_SSH2_SESSION_RES_NAME, le_ssh2_session);
if (session) {
if (psftp) {
/* We need an sftp layer too */
LIBSSH2_SFTP *sftp = libssh2_sftp_init(session);
if (!sftp) {
php_url_free(resource);
return NULL;
}
sftp_data = emalloc(sizeof(php_ssh2_sftp_data));
sftp_data->sftp = sftp;
sftp_data->session = session;
sftp_data->session_rsrc = Z_RES_P(zresource);
Z_ADDREF_P(zresource);
*psftp_rsrc = zend_register_resource(sftp_data, le_ssh2_sftp);
*psftp = sftp;
*presource = Z_RES_P(zresource);
*psession = session;
return resource;
}
Z_ADDREF_P(zresource);
*presource = Z_RES_P(zresource);
*psession = session;
return resource;
}
}
/* Fallback on finding it in the context */
if (SSH2_URL_STR(resource->host)[0] == 0 && context && psftp &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "sftp")) != NULL &&
Z_TYPE_P(tmpzval) == IS_RESOURCE) {
php_ssh2_sftp_data *sftp_data;
sftp_data = (php_ssh2_sftp_data *)zend_fetch_resource(Z_RES_P(tmpzval), PHP_SSH2_SFTP_RES_NAME, le_ssh2_sftp);
if (sftp_data) {
Z_ADDREF_P(tmpzval);
*psftp_rsrc = Z_RES_P(tmpzval);
*psftp = sftp_data->sftp;
*presource = sftp_data->session_rsrc;
*psession = sftp_data->session;
return resource;
}
}
if (SSH2_URL_STR(resource->host)[0] == 0 && context &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "session")) != NULL &&
Z_TYPE_P(tmpzval) == IS_RESOURCE) {
session = (LIBSSH2_SESSION *)zend_fetch_resource(Z_RES_P(tmpzval), PHP_SSH2_SESSION_RES_NAME, le_ssh2_session);
if (session) {
if (psftp) {
/* We need an SFTP layer too! */
LIBSSH2_SFTP *sftp = libssh2_sftp_init(session);
php_ssh2_sftp_data *sftp_data;
if (!sftp) {
php_url_free(resource);
return NULL;
}
sftp_data = emalloc(sizeof(php_ssh2_sftp_data));
sftp_data->sftp = sftp;
sftp_data->session = session;
sftp_data->session_rsrc = Z_RES_P(tmpzval);
Z_ADDREF_P(tmpzval);
*psftp_rsrc = zend_register_resource(sftp_data, le_ssh2_sftp);
*psftp = sftp;
*presource = Z_RES_P(tmpzval);
*psession = session;
return resource;
}
Z_ADDREF_P(tmpzval);
*psession = session;
*presource = Z_RES_P(tmpzval);
return resource;
}
}
/* Make our own connection then */
if (!resource->port) {
resource->port = 22;
}
if (context &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "methods")) != NULL &&
Z_TYPE_P(tmpzval) == IS_ARRAY) {
methods = tmpzval;
}
if (context &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "callbacks")) != NULL &&
Z_TYPE_P(tmpzval) == IS_ARRAY) {
callbacks = tmpzval;
}
if (context &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "username")) != NULL &&
Z_TYPE_P(tmpzval) == IS_STRING) {
username = Z_STRVAL_P(tmpzval);
username_len = Z_STRLEN_P(tmpzval);
}
if (context &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "password")) != NULL &&
Z_TYPE_P(tmpzval) == IS_STRING) {
password = Z_STRVAL_P(tmpzval);
password_len = Z_STRLEN_P(tmpzval);
}
if (context &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "pubkey_file")) != NULL &&
Z_TYPE_P(tmpzval) == IS_STRING) {
pubkey_file = Z_STRVAL_P(tmpzval);
}
if (context &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "privkey_file")) != NULL &&
Z_TYPE_P(tmpzval) == IS_STRING) {
privkey_file = Z_STRVAL_P(tmpzval);
}
if (resource->user) {
int len = SSH2_URL_LEN(resource->user);
if (len) {
username = SSH2_URL_STR(resource->user);
username_len = len;
}
}
if (resource->pass) {
int len = SSH2_URL_LEN(resource->pass);
if (len) {
password = SSH2_URL_STR(resource->pass);
password_len = len;
}
}
if (!username) {
/* username is a minimum */
php_url_free(resource);
return NULL;
}
session = php_ssh2_session_connect(SSH2_URL_STR(resource->host), resource->port, methods, callbacks);
if (!session) {
/* Unable to connect! */
php_url_free(resource);
return NULL;
}
/* Authenticate */
if (pubkey_file && privkey_file) {
if (php_check_open_basedir(pubkey_file) || php_check_open_basedir(privkey_file)) {
php_url_free(resource);
return NULL;
}
/* Attempt pubkey authentication */
if (!libssh2_userauth_publickey_fromfile(session, username, pubkey_file, privkey_file, password)) {
goto session_authed;
}
}
if (password) {
/* Attempt password authentication */
if (libssh2_userauth_password_ex(session, username, username_len, password, password_len, NULL) == 0) {
goto session_authed;
}
}
/* Auth failure */
php_url_free(resource);
if (Z_RES(zsession)) {
zend_list_delete(Z_RES(zsession));
}
return NULL;
session_authed:
ZVAL_RES(&zsession, zend_register_resource(session, le_ssh2_session));
if (psftp) {
LIBSSH2_SFTP *sftp;
zval zsftp;
sftp = libssh2_sftp_init(session);
if (!sftp) {
php_url_free(resource);
zend_list_delete(Z_RES(zsession));
return NULL;
}
sftp_data = emalloc(sizeof(php_ssh2_sftp_data));
sftp_data->session = session;
sftp_data->sftp = sftp;
sftp_data->session_rsrc = Z_RES(zsession);
//TODO Sean-Der
//ZEND_REGISTER_RESOURCE(sftp_data, le_ssh2_sftp);
*psftp_rsrc = Z_RES(zsftp);
*psftp = sftp;
}
*presource = Z_RES(zsession);
*psession = session;
return resource;
}
/* }}} */
/* *****************
* Shell Wrapper *
***************** */
/* {{{ php_ssh2_shell_open
* Make a stream from a session
*/
static php_stream *php_ssh2_shell_open(LIBSSH2_SESSION *session, zend_resource *resource, char *term, int term_len, zval *environment, long width, long height, long type)
{
LIBSSH2_CHANNEL *channel;
php_ssh2_channel_data *channel_data;
php_stream *stream;
libssh2_session_set_blocking(session, 1);
channel = libssh2_channel_open_session(session);
if (!channel) {
php_error_docref(NULL, E_WARNING, "Unable to request a channel from remote host");
return NULL;
}
if (environment) {
zend_string *key;
int key_type;
zend_ulong idx;
for(zend_hash_internal_pointer_reset(HASH_OF(environment));
(key_type = zend_hash_get_current_key(HASH_OF(environment), &key, &idx)) != HASH_KEY_NON_EXISTENT;
zend_hash_move_forward(HASH_OF(environment))) {
if (key_type == HASH_KEY_IS_STRING) {
zval *value;
if ((value = zend_hash_get_current_data(HASH_OF(environment))) != NULL) {
zval copyval = *value;
zval_copy_ctor(©val);
convert_to_string(©val);
if (libssh2_channel_setenv_ex(channel, key->val, key->len, Z_STRVAL(copyval), Z_STRLEN(copyval))) {
php_error_docref(NULL, E_WARNING, "Failed setting %s=%s on remote end", ZSTR_VAL(key), Z_STRVAL(copyval));
}
zval_dtor(©val);
}
} else {
php_error_docref(NULL, E_NOTICE, "Skipping numeric index in environment array");
}
}
}
if (type == PHP_SSH2_TERM_UNIT_CHARS) {
if (libssh2_channel_request_pty_ex(channel, term, term_len, NULL, 0, width, height, 0, 0)) {
php_error_docref(NULL, E_WARNING, "Failed allocating %s pty at %ldx%ld characters", term, width, height);
libssh2_channel_free(channel);
return NULL;
}
} else {
if (libssh2_channel_request_pty_ex(channel, term, term_len, NULL, 0, 0, 0, width, height)) {
php_error_docref(NULL, E_WARNING, "Failed allocating %s pty at %ldx%ld pixels", term, width, height);
libssh2_channel_free(channel);
return NULL;
}
}
if (libssh2_channel_shell(channel)) {
php_error_docref(NULL, E_WARNING, "Unable to request shell from remote host");
libssh2_channel_free(channel);
return NULL;
}
/* Turn it into a stream */
channel_data = emalloc(sizeof(php_ssh2_channel_data));
channel_data->channel = channel;
channel_data->streamid = 0;
channel_data->is_blocking = 0;
channel_data->timeout = 0;
channel_data->session_rsrc = resource;
channel_data->refcount = NULL;
stream = php_stream_alloc(&php_ssh2_channel_stream_ops, channel_data, 0, "r+");
return stream;
}
/* }}} */
/* {{{ php_ssh2_fopen_wrapper_shell
* ssh2.shell:// fopen wrapper
*/
static php_stream *php_ssh2_fopen_wrapper_shell(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC)
{
LIBSSH2_SESSION *session = NULL;
php_stream *stream;
zval *tmpzval, *environment = NULL;
char *terminal = PHP_SSH2_DEFAULT_TERMINAL;
zend_long width = PHP_SSH2_DEFAULT_TERM_WIDTH;
zend_long height = PHP_SSH2_DEFAULT_TERM_HEIGHT;
zend_long type = PHP_SSH2_DEFAULT_TERM_UNIT;
zend_resource *rsrc = NULL;
int terminal_len = sizeof(PHP_SSH2_DEFAULT_TERMINAL) - 1;
php_url *resource;
char *s;
resource = php_ssh2_fopen_wraper_parse_path(path, "shell", context, &session, &rsrc, NULL, NULL);
if (!resource || !session) {
return NULL;
}
if (context &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "env")) != NULL && Z_TYPE_P(tmpzval) == IS_ARRAY) {
environment = tmpzval;
}
if (context &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "term")) != NULL && Z_TYPE_P(tmpzval) == IS_STRING) {
terminal = Z_STRVAL_P(tmpzval);
terminal_len = Z_STRLEN_P(tmpzval);
}
if (context &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "term_width")) != NULL) {
zval copyval;
copyval = *tmpzval;
convert_to_long(©val);
width = Z_LVAL_P(©val);
zval_ptr_dtor(©val);
}
if (context &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "term_height")) != NULL) {
zval copyval;
copyval = *tmpzval;
convert_to_long(©val);
height = Z_LVAL_P(©val);
zval_ptr_dtor(©val);
}
if (context &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "term_units")) != NULL) {
zval copyval;
copyval = *tmpzval;
convert_to_long(©val);
type = Z_LVAL_P(©val);
zval_ptr_dtor(©val);
}
s = resource->path ? SSH2_URL_STR(resource->path) : NULL;
if (s && s[0] == '/') {
/* Terminal type encoded into URL overrides context terminal type */
char *p;
s++;
p = strchr(s, '/');
if (p) {
if (p - s) {
terminal = s;
terminal_len = p - terminal;
s += terminal_len + 1;
} else {
/* "null" terminal given, skip it */
s++;
}
} else {
int len;
if ((len = strlen(path + 1))) {
terminal = s;
terminal_len = len;
s += len;
}
}
}
/* TODO: Accept resolution and environment vars as URL style parameters
* ssh2.shell://hostorresource/terminal/99x99c?envvar=envval&envvar=envval....
*/
stream = php_ssh2_shell_open(session, rsrc, terminal, terminal_len, environment, width, height, type);
if (!stream) {
zend_list_delete(rsrc);
}
php_url_free(resource);
return stream;
}
/* }}} */
static php_stream_wrapper_ops php_ssh2_shell_stream_wops = {
php_ssh2_fopen_wrapper_shell,
NULL, /* stream_close */
NULL, /* stat */
NULL, /* stat_url */
NULL, /* opendir */
"ssh2.shell"
};
php_stream_wrapper php_ssh2_stream_wrapper_shell = {
&php_ssh2_shell_stream_wops,
NULL,
0
};
/* {{{ proto stream ssh2_shell(resource session[, string term_type[, array env[, int width, int height[, int width_height_type]]]])
* Open a shell at the remote end and allocate a channel for it
*/
PHP_FUNCTION(ssh2_shell)
{
LIBSSH2_SESSION *session;
php_stream *stream;
zval *zsession;
zval *environment = NULL;
char *term = PHP_SSH2_DEFAULT_TERMINAL;
size_t term_len = sizeof(PHP_SSH2_DEFAULT_TERMINAL) - 1;
zend_long width = PHP_SSH2_DEFAULT_TERM_WIDTH;
zend_long height = PHP_SSH2_DEFAULT_TERM_HEIGHT;
zend_long type = PHP_SSH2_DEFAULT_TERM_UNIT;
int argc = ZEND_NUM_ARGS();
if (argc == 5) {
php_error_docref(NULL, E_ERROR, "width specified without height parameter");
RETURN_FALSE;
}
if (zend_parse_parameters(argc, "r|sa!lll", &zsession, &term, &term_len, &environment, &width, &height, &type) == FAILURE) {
return;
}
SSH2_FETCH_AUTHENTICATED_SESSION(session, zsession);
stream = php_ssh2_shell_open(session, Z_RES_P(zsession), term, term_len, environment, width, height, type);
if (!stream) {
RETURN_FALSE;
}
/* Ensure that channels are freed BEFORE the sessions they belong to */
Z_ADDREF_P(zsession);
php_stream_to_zval(stream, return_value);
}
/* }}} */
/* ****************
* Exec Wrapper *
**************** */
/* {{{ php_ssh2_exec_command
* Make a stream from a session
*/
static php_stream *php_ssh2_exec_command(LIBSSH2_SESSION *session, zend_resource *rsrc, char *command, char *term, int term_len, zval *environment, long width, long height, long type)
{
LIBSSH2_CHANNEL *channel;
php_ssh2_channel_data *channel_data;
php_stream *stream;
libssh2_session_set_blocking(session, 1);
channel = libssh2_channel_open_session(session);
if (!channel) {
php_error_docref(NULL, E_WARNING, "Unable to request a channel from remote host");
return NULL;
}
if (environment) {
zend_string *key = NULL;
int key_type;
zend_ulong idx = 0;
HashPosition pos;
for(zend_hash_internal_pointer_reset_ex(HASH_OF(environment), &pos);
(key_type = zend_hash_get_current_key_ex(HASH_OF(environment), &key, &idx, &pos)) != HASH_KEY_NON_EXISTENT;
zend_hash_move_forward_ex(HASH_OF(environment), &pos)) {
if (key_type == HASH_KEY_IS_STRING) {
zval *value;
if ((value = zend_hash_get_current_data(HASH_OF(environment))) != NULL) {
zval copyval = *value;
zval_copy_ctor(©val);
convert_to_string(©val);
if (libssh2_channel_setenv_ex(channel, key->val, key->len, Z_STRVAL(copyval), Z_STRLEN(copyval))) {
php_error_docref(NULL, E_WARNING, "Failed setting %s=%s on remote end", ZSTR_VAL(key), Z_STRVAL(copyval));
}
zval_dtor(©val);
}
} else {
php_error_docref(NULL, E_NOTICE, "Skipping numeric index in environment array");
}
}
}
if (term) {
if (type == PHP_SSH2_TERM_UNIT_CHARS) {
if (libssh2_channel_request_pty_ex(channel, term, term_len, NULL, 0, width, height, 0, 0)) {
php_error_docref(NULL, E_WARNING, "Failed allocating %s pty at %ldx%ld characters", term, width, height);
libssh2_channel_free(channel);
return NULL;
}
} else {
if (libssh2_channel_request_pty_ex(channel, term, term_len, NULL, 0, 0, 0, width, height)) {
php_error_docref(NULL, E_WARNING, "Failed allocating %s pty at %ldx%ld pixels", term, width, height);
libssh2_channel_free(channel);
return NULL;
}
}
}
if (libssh2_channel_exec(channel, command)) {
php_error_docref(NULL, E_WARNING, "Unable to request command execution on remote host");
libssh2_channel_free(channel);
return NULL;
}
/* Turn it into a stream */
channel_data = emalloc(sizeof(php_ssh2_channel_data));
channel_data->channel = channel;
channel_data->streamid = 0;
channel_data->is_blocking = 0;
channel_data->timeout = 0;
channel_data->session_rsrc = rsrc;
channel_data->refcount = NULL;
stream = php_stream_alloc(&php_ssh2_channel_stream_ops, channel_data, 0, "r+");
return stream;
}
/* }}} */
/* {{{ php_ssh2_fopen_wrapper_exec
* ssh2.exec:// fopen wrapper
*/
static php_stream *php_ssh2_fopen_wrapper_exec(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC)
{
LIBSSH2_SESSION *session = NULL;
php_stream *stream;
zval *tmpzval, *environment = NULL;
zend_resource *rsrc = NULL;
php_url *resource;
char *terminal = NULL;
int terminal_len = 0;
long width = PHP_SSH2_DEFAULT_TERM_WIDTH;
long height = PHP_SSH2_DEFAULT_TERM_HEIGHT;
long type = PHP_SSH2_DEFAULT_TERM_UNIT;
resource = php_ssh2_fopen_wraper_parse_path(path, "exec", context, &session, &rsrc, NULL, NULL);
if (!resource || !session) {
return NULL;
}
if (!resource->path) {
php_url_free(resource);
zend_list_delete(rsrc);
return NULL;
}
if (context &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "env")) != NULL && Z_TYPE_P(tmpzval) == IS_ARRAY) {
environment = tmpzval;
}
if (context &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "term")) != NULL && Z_TYPE_P(tmpzval) == IS_STRING) {
terminal = Z_STRVAL_P(tmpzval);
terminal_len = Z_STRLEN_P(tmpzval);
}
if (context &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "term_width")) != NULL) {
zval copyval;
copyval = *tmpzval;
convert_to_long(©val);
width = Z_LVAL_P(©val);
zval_ptr_dtor(©val);
}
if (context &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "term_height")) != NULL) {
zval copyval;
copyval = *tmpzval;
convert_to_long(©val);
height = Z_LVAL_P(©val);
zval_ptr_dtor(©val);
}
if (context &&
(tmpzval = php_stream_context_get_option(context, "ssh2", "term_units")) != NULL) {
zval *copyval;
copyval = tmpzval;
convert_to_long(copyval);
type = Z_LVAL_P(copyval);
zval_ptr_dtor(copyval);
}
stream = php_ssh2_exec_command(session, rsrc, SSH2_URL_STR(resource->path) + 1, terminal, terminal_len, environment, width, height, type);
if (!stream) {
zend_list_delete(rsrc);
}
php_url_free(resource);
return stream;
}
/* }}} */
static php_stream_wrapper_ops php_ssh2_exec_stream_wops = {
php_ssh2_fopen_wrapper_exec,
NULL, /* stream_close */
NULL, /* stat */
NULL, /* stat_url */
NULL, /* opendir */
"ssh2.exec"
};
php_stream_wrapper php_ssh2_stream_wrapper_exec = {
&php_ssh2_exec_stream_wops,
NULL,
0
};
/* {{{ proto stream ssh2_exec(resource session, string command[, string pty[, array env[, int width[, int height[, int width_height_type]]]]])
* Execute a command at the remote end and allocate a channel for it
*
* This function has a dirty little secret.... pty and env can be in either order.... shhhh... don't tell anyone
*/
PHP_FUNCTION(ssh2_exec)
{
LIBSSH2_SESSION *session;
php_stream *stream;
zval *zsession;
zval *environment = NULL;
zval *zpty = NULL;
char *command;
size_t command_len;
zend_long width = PHP_SSH2_DEFAULT_TERM_WIDTH;
zend_long height = PHP_SSH2_DEFAULT_TERM_HEIGHT;
zend_long type = PHP_SSH2_DEFAULT_TERM_UNIT;
char *term = NULL;
int term_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|z!z!lll", &zsession, &command, &command_len, &zpty, &environment, &width, &height, &type) == FAILURE) {
return;
}
if (zpty && Z_TYPE_P(zpty) == IS_ARRAY) {
/* Swap pty and environment -- old call style */
zval *tmp = zpty;
zpty = environment;
environment = tmp;
}
if (environment && Z_TYPE_P(environment) != IS_ARRAY) {
php_error_docref(NULL, E_WARNING, "ssh2_exec() expects arg 4 to be of type array");
RETURN_FALSE;
}
if (zpty) {
convert_to_string(zpty);
term = Z_STRVAL_P(zpty);
term_len = Z_STRLEN_P(zpty);
}
SSH2_FETCH_AUTHENTICATED_SESSION(session, zsession);
stream = php_ssh2_exec_command(session, Z_RES_P(zsession), command, term, term_len, environment, width, height, type);
if (!stream) {
RETURN_FALSE;
}
/* Ensure that channels are freed BEFORE the sessions they belong to */
Z_ADDREF_P(zsession);
php_stream_to_zval(stream, return_value);
}
/* }}} */
/* ***************
* SCP Wrapper *
*************** */
/* {{{ php_ssh2_scp_xfer
* Make a stream from a session
*/
static php_stream *php_ssh2_scp_xfer(LIBSSH2_SESSION *session, zend_resource *rsrc, char *filename)
{
LIBSSH2_CHANNEL *channel;
php_ssh2_channel_data *channel_data;
php_stream *stream;
channel = libssh2_scp_recv(session, filename, NULL);
if (!channel) {
char *error = "";
libssh2_session_last_error(session, &error, NULL, 0);
php_error_docref(NULL, E_WARNING, "Unable to request a channel from remote host: %s", error);
return NULL;
}
/* Turn it into a stream */
channel_data = emalloc(sizeof(php_ssh2_channel_data));
channel_data->channel = channel;
channel_data->streamid = 0;
channel_data->is_blocking = 0;
channel_data->timeout = 0;
channel_data->session_rsrc = rsrc;
channel_data->refcount = NULL;
stream = php_stream_alloc(&php_ssh2_channel_stream_ops, channel_data, 0, "r");
return stream;
}
/* }}} */
/* {{{ php_ssh2_fopen_wrapper_scp
* ssh2.scp:// fopen wrapper (Read mode only, if you want to know why write mode isn't supported as a stream, take a look at the SCP protocol)
*/
static php_stream *php_ssh2_fopen_wrapper_scp(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC)
{
LIBSSH2_SESSION *session = NULL;
php_stream *stream;
zend_resource *rsrc = NULL;
php_url *resource;
if (strchr(mode, '+') || strchr(mode, 'a') || strchr(mode, 'w')) {
return NULL;
}
resource = php_ssh2_fopen_wraper_parse_path(path, "scp", context, &session, &rsrc, NULL, NULL);
if (!resource || !session) {
return NULL;
}
if (!resource->path) {
php_url_free(resource);
zend_list_delete(rsrc);
return NULL;
}
stream = php_ssh2_scp_xfer(session, rsrc, SSH2_URL_STR(resource->path));
if (!stream) {
zend_list_delete(rsrc);
}
php_url_free(resource);
return stream;
}
/* }}} */
static php_stream_wrapper_ops php_ssh2_scp_stream_wops = {
php_ssh2_fopen_wrapper_scp,
NULL, /* stream_close */
NULL, /* stat */
NULL, /* stat_url */
NULL, /* opendir */
"ssh2.scp"
};
php_stream_wrapper php_ssh2_stream_wrapper_scp = {
&php_ssh2_scp_stream_wops,
NULL,
0
};
/* {{{ proto bool ssh2_scp_recv(resource session, string remote_file, string local_file)
* Request a file via SCP
*/
PHP_FUNCTION(ssh2_scp_recv)
{
LIBSSH2_SESSION *session;
LIBSSH2_CHANNEL *remote_file;
struct stat sb;
php_stream *local_file;
zval *zsession;
char *remote_filename, *local_filename;
size_t remote_filename_len, local_filename_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rss", &zsession, &remote_filename, &remote_filename_len,
&local_filename, &local_filename_len) == FAILURE) {
return;
}
SSH2_FETCH_AUTHENTICATED_SESSION(session, zsession);
remote_file = libssh2_scp_recv(session, remote_filename, &sb);
if (!remote_file) {
php_error_docref(NULL, E_WARNING, "Unable to receive remote file");
RETURN_FALSE;
}
libssh2_channel_set_blocking(remote_file, 1);
local_file = php_stream_open_wrapper(local_filename, "wb", REPORT_ERRORS, NULL);
if (!local_file) {
php_error_docref(NULL, E_WARNING, "Unable to write to local file");
libssh2_channel_free(remote_file);
RETURN_FALSE;
}
while (sb.st_size) {
char buffer[8192];
int bytes_read;
bytes_read = libssh2_channel_read(remote_file, buffer, sb.st_size > 8192 ? 8192 : sb.st_size);
if (bytes_read < 0) {
php_error_docref(NULL, E_WARNING, "Error reading from remote file");
libssh2_channel_free(remote_file);
php_stream_close(local_file);
RETURN_FALSE;
}
php_stream_write(local_file, buffer, bytes_read);
sb.st_size -= bytes_read;
}
libssh2_channel_free(remote_file);
php_stream_close(local_file);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto stream ssh2_scp_send(resource session, string local_file, string remote_file[, int create_mode = 0644])
* Send a file via SCP
*/
PHP_FUNCTION(ssh2_scp_send)
{
LIBSSH2_SESSION *session;
LIBSSH2_CHANNEL *remote_file;
php_stream *local_file;
zval *zsession;
char *local_filename, *remote_filename;
size_t local_filename_len, remote_filename_len;
zend_long create_mode = 0644;
php_stream_statbuf ssb;
int argc = ZEND_NUM_ARGS();
if (zend_parse_parameters(argc, "rss|l", &zsession, &local_filename, &local_filename_len,
&remote_filename, &remote_filename_len, &create_mode) == FAILURE) {
return;
}
SSH2_FETCH_AUTHENTICATED_SESSION(session, zsession);
local_file = php_stream_open_wrapper(local_filename, "rb", REPORT_ERRORS, NULL);
if (!local_file) {
php_error_docref(NULL, E_WARNING, "Unable to read source file");
RETURN_FALSE;
}
if (php_stream_stat(local_file, &ssb)) {
php_error_docref(NULL, E_WARNING, "Failed statting local file");
php_stream_close(local_file);
RETURN_FALSE;
}
if (argc < 4) {
create_mode = ssb.sb.st_mode & 0777;
}
remote_file = libssh2_scp_send_ex(session, remote_filename, create_mode, ssb.sb.st_size, ssb.sb.st_atime, ssb.sb.st_mtime);
if (!remote_file) {
int last_error = 0;
char *error_msg = NULL;
last_error = libssh2_session_last_error(session, &error_msg, NULL, 0);
php_error_docref(NULL, E_WARNING, "Failure creating remote file: %s (%d)", error_msg, last_error);
php_stream_close(local_file);
RETURN_FALSE;
}
libssh2_channel_set_blocking(remote_file, 1);
while (ssb.sb.st_size) {
char buffer[8192];
size_t toread = MIN(8192, ssb.sb.st_size);
size_t bytesread = php_stream_read(local_file, buffer, toread);
size_t sent = 0;
size_t justsent = 0;
if (bytesread <= 0 || bytesread > toread) {
php_error_docref(NULL, E_WARNING, "Failed copying file 2");
php_stream_close(local_file);
libssh2_channel_free(remote_file);
RETURN_FALSE;
}
while (bytesread - sent > 0) {
if ((justsent = libssh2_channel_write(remote_file, (buffer + sent), bytesread - sent)) < 0) {
switch (justsent) {
case LIBSSH2_ERROR_EAGAIN:
php_error_docref(NULL, E_WARNING, "Operation would block");
break;
case LIBSSH2_ERROR_ALLOC:
php_error_docref(NULL,E_WARNING, "An internal memory allocation call failed");
break;
case LIBSSH2_ERROR_SOCKET_SEND:
php_error_docref(NULL,E_WARNING, "Unable to send data on socket");
break;
case LIBSSH2_ERROR_CHANNEL_CLOSED:
php_error_docref(NULL,E_WARNING, "The channel has been closed");
break;
case LIBSSH2_ERROR_CHANNEL_EOF_SENT:
php_error_docref(NULL,E_WARNING, "The channel has been requested to be closed");
break;
}
php_stream_close(local_file);
libssh2_channel_free(remote_file);
RETURN_FALSE;
}
sent = sent + justsent;
}
ssb.sb.st_size -= bytesread;
}
libssh2_channel_flush_ex(remote_file, LIBSSH2_CHANNEL_FLUSH_ALL);
php_stream_close(local_file);
libssh2_channel_free(remote_file);
RETURN_TRUE;
}
/* }}} */
/* ***************************
* Direct TCP/IP Transport *
*************************** */
/* {{{ php_ssh2_direct_tcpip
* Make a stream from a session
*/
static php_stream *php_ssh2_direct_tcpip(LIBSSH2_SESSION *session, zend_resource *rsrc, char *host, int port)
{
LIBSSH2_CHANNEL *channel;
php_ssh2_channel_data *channel_data;
php_stream *stream;
libssh2_session_set_blocking(session, 1);
channel = libssh2_channel_direct_tcpip(session, host, port);
if (!channel) {
php_error_docref(NULL, E_WARNING, "Unable to request a channel from remote host");
return NULL;
}
/* Turn it into a stream */
channel_data = emalloc(sizeof(php_ssh2_channel_data));
channel_data->channel = channel;
channel_data->streamid = 0;
channel_data->is_blocking = 0;
channel_data->timeout = 0;
channel_data->session_rsrc = rsrc;
channel_data->refcount = NULL;
stream = php_stream_alloc(&php_ssh2_channel_stream_ops, channel_data, 0, "r+");
return stream;
}
/* }}} */
/* {{{ php_ssh2_fopen_wrapper_tunnel
* ssh2.tunnel:// fopen wrapper
*/
static php_stream *php_ssh2_fopen_wrapper_tunnel(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC)
{
LIBSSH2_SESSION *session = NULL;
php_stream *stream = NULL;
php_url *resource;
char *host = NULL;
int port = 0;
zend_resource *rsrc;
resource = php_ssh2_fopen_wraper_parse_path(path, "tunnel", context, &session, &rsrc, NULL, NULL);
if (!resource || !session) {
return NULL;
}
if (resource->path && SSH2_URL_STR(resource->path)[0] == '/') {
char *colon;
host = SSH2_URL_STR(resource->path) + 1;
if (*host == '[') {
/* IPv6 Encapsulated Format */
host++;
colon = strstr(host, "]:");
if (colon) {
*colon = 0;
colon += 2;
}
} else {
colon = strchr(host, ':');
if (colon) {
*(colon++) = 0;
}
}
if (colon) {
port = atoi(colon);
}
}
if ((port <= 0) || (port > 65535) || !host || (strlen(host) == 0)) {
/* Invalid connection criteria */
php_url_free(resource);
zend_list_delete(rsrc);
return NULL;
}
stream = php_ssh2_direct_tcpip(session, rsrc, host, port);
if (!stream) {
zend_list_delete(rsrc);
}
php_url_free(resource);
return stream;
}
/* }}} */
static php_stream_wrapper_ops php_ssh2_tunnel_stream_wops = {
php_ssh2_fopen_wrapper_tunnel,
NULL, /* stream_close */
NULL, /* stat */
NULL, /* stat_url */
NULL, /* opendir */
"ssh2.tunnel"
};
php_stream_wrapper php_ssh2_stream_wrapper_tunnel = {
&php_ssh2_tunnel_stream_wops,
NULL,
0
};
/* {{{ proto stream ssh2_tunnel(resource session, string host, int port)
* Tunnel to remote TCP/IP host/port
*/
PHP_FUNCTION(ssh2_tunnel)
{
LIBSSH2_SESSION *session;
php_stream *stream;
zval *zsession;
char *host;
size_t host_len;
zend_long port;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rsl", &zsession, &host, &host_len, &port) == FAILURE) {
return;
}
SSH2_FETCH_AUTHENTICATED_SESSION(session, zsession);
stream = php_ssh2_direct_tcpip(session, Z_RES_P(zsession), host, port);
if (!stream) {
RETURN_FALSE;
}
/* Ensure that channels are freed BEFORE the sessions they belong to */
Z_ADDREF_P(zsession);
php_stream_to_zval(stream, return_value);
}
/* }}} */
/* ******************
* Generic Helper *
****************** */
/* {{{ proto stream ssh2_fetch_stream(stream channel, int streamid)
* Fetch an extended data stream
*/
PHP_FUNCTION(ssh2_fetch_stream)
{
php_ssh2_channel_data *data, *stream_data;
php_stream *parent, *stream;
zval *zparent;
zend_long streamid;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zparent, &streamid) == FAILURE) {
return;
}
if (streamid < 0) {
php_error_docref(NULL, E_WARNING, "Invalid stream ID requested");
RETURN_FALSE;
}
php_stream_from_zval(parent, zparent);
if (parent->ops != &php_ssh2_channel_stream_ops) {
php_error_docref(NULL, E_WARNING, "Provided stream is not of type " PHP_SSH2_CHANNEL_STREAM_NAME);
RETURN_FALSE;
}
data = (php_ssh2_channel_data*)parent->abstract;
if (!data->refcount) {
data->refcount = emalloc(sizeof(unsigned char));
*(data->refcount) = 1;
}
if (*(data->refcount) == 255) {
php_error_docref(NULL, E_WARNING, "Too many streams associated to a single channel");
RETURN_FALSE;
}
(*(data->refcount))++;
stream_data = emalloc(sizeof(php_ssh2_channel_data));
memcpy(stream_data, data, sizeof(php_ssh2_channel_data));
stream_data->streamid = streamid;
stream = php_stream_alloc(&php_ssh2_channel_stream_ops, stream_data, 0, "r+");
if (!stream) {
php_error_docref(NULL, E_WARNING, "Error opening substream");
efree(stream_data);
(data->refcount)--;
RETURN_FALSE;
}
php_stream_to_zval(stream, return_value);
}
/* }}} */
/* {{{ proto stream ssh2_send_eof(stream channel)
* Sends EOF to a stream. Primary use is to close stdin of an stdio stream.
*/
PHP_FUNCTION(ssh2_send_eof)
{
php_ssh2_channel_data *data;
php_stream *parent;
zval *zparent;
int ssh2_ret;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zparent) == FAILURE) {
return;
}
php_stream_from_zval(parent, zparent);
if (parent->ops != &php_ssh2_channel_stream_ops) {
php_error_docref(NULL, E_WARNING, "Provided stream is not of type " PHP_SSH2_CHANNEL_STREAM_NAME);
RETURN_FALSE;
}
data = (php_ssh2_channel_data*)parent->abstract;
if (!data) {
php_error_docref(NULL, E_WARNING, "Abstract in stream is null");
RETURN_FALSE;
}
ssh2_ret = libssh2_channel_send_eof(data->channel);
if (ssh2_ret < 0) {
php_error_docref(NULL, E_WARNING, "Couldn't send EOF to channel (Return code %d)", ssh2_ret);
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
|