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
|
/*
* remctl PECL extension for PHP 7 and later
*
* Provides bindings for PHP very similar to the libremctl library for C or
* the Net::Remctl bindings for Perl.
*
* Originally written by Andrew Mortensen <admorten@umich.edu>
* Copyright 2016, 2018, 2020 Russ Allbery <eagle@eyrie.org>
* Copyright 2008 Andrew Mortensen <admorten@umich.edu>
* Copyright 2008, 2011-2012, 2014
* The Board of Trustees of the Leland Stanford Junior University
*
* SPDX-License-Identifier: MIT
*/
#include <config.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <client/remctl.h>
#include <php.h>
#include <php_remctl.h>
static int le_remctl_internal;
/* clang-format off */
ZEND_BEGIN_ARG_INFO_EX(arginfo_remctl, 0, 0, 4)
ZEND_ARG_INFO(0, "host")
ZEND_ARG_INFO(0, "port")
ZEND_ARG_INFO(0, "principal")
ZEND_ARG_INFO(0, "command")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_remctl_new, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_remctl_set_ccache, 0, 0, 2)
ZEND_ARG_INFO(0, "remctl")
ZEND_ARG_INFO(0, "path")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_remctl_set_source_ip, 0, 0, 2)
ZEND_ARG_INFO(0, "remctl")
ZEND_ARG_INFO(0, "source_ip")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_remctl_set_timeout, 0, 0, 2)
ZEND_ARG_INFO(0, "remctl")
ZEND_ARG_INFO(0, "timeout")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_remctl_open, 0, 0, 2)
ZEND_ARG_INFO(0, "remctl")
ZEND_ARG_INFO(0, "host")
ZEND_ARG_INFO(0, "port")
ZEND_ARG_INFO(0, "principal")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_remctl_close, 0, 0, 2)
ZEND_ARG_INFO(0, "remctl")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_remctl_command, 0, 0, 2)
ZEND_ARG_INFO(0, "remctl")
ZEND_ARG_INFO(0, "command")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_remctl_output, 0, 0, 2)
ZEND_ARG_INFO(0, "remctl")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_remctl_noop, 0, 0, 2)
ZEND_ARG_INFO(0, "remctl")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_remctl_error, 0, 0, 2)
ZEND_ARG_INFO(0, "remctl")
ZEND_END_ARG_INFO()
static zend_function_entry remctl_functions[] = {
ZEND_FE(remctl, arginfo_remctl)
ZEND_FE(remctl_new, arginfo_remctl_new)
ZEND_FE(remctl_set_ccache, arginfo_remctl_set_ccache)
ZEND_FE(remctl_set_source_ip, arginfo_remctl_set_source_ip)
ZEND_FE(remctl_set_timeout, arginfo_remctl_set_timeout)
ZEND_FE(remctl_open, arginfo_remctl_open)
ZEND_FE(remctl_close, arginfo_remctl_close)
ZEND_FE(remctl_command, arginfo_remctl_command)
ZEND_FE(remctl_output, arginfo_remctl_output)
ZEND_FE(remctl_noop, arginfo_remctl_noop)
ZEND_FE(remctl_error, arginfo_remctl_error)
{NULL, NULL, NULL, 0, 0}
};
zend_module_entry remctl_module_entry = {
STANDARD_MODULE_HEADER,
PHP_REMCTL_EXTNAME,
remctl_functions,
PHP_MINIT(remctl),
NULL,
NULL,
NULL,
NULL,
PHP_REMCTL_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* clang-format on */
#ifdef COMPILE_DL_REMCTL
ZEND_GET_MODULE(remctl)
#endif
/*
* Destructor for a remctl object. Close the underlying connection.
*/
static void
php_remctl_dtor(zend_resource *rsrc)
{
struct remctl *r = (struct remctl *) rsrc->ptr;
if (r != NULL)
remctl_close(r);
}
/*
* Initialize the module and register the destructor. Stores the resource
* number of the module in le_remctl_internal.
*/
PHP_MINIT_FUNCTION(remctl)
{
le_remctl_internal = zend_register_list_destructors_ex(
php_remctl_dtor, NULL, PHP_REMCTL_RES_NAME, module_number);
return SUCCESS;
}
/*
* The simplified interface. Make a call and return the results as an
* object.
*/
ZEND_FUNCTION(remctl)
{
zval *cmd_array;
zval *data = NULL;
HashTable *hash;
char *host, *principal = NULL;
const char **command = NULL;
long port;
size_t hlen, plen;
int count, i, status;
int success = 0;
struct remctl_result *result = NULL;
/*
* Read the arguments (host, port, principal, and command) and check their
* validity. Host and command are required, so all arguments must be
* provided, but an empty string can be passed in as the principal.
*/
status = zend_parse_parameters(ZEND_NUM_ARGS(), "slsa", &host, &hlen,
&port, &principal, &plen, &cmd_array);
if (status == FAILURE) {
zend_error(E_WARNING, "remctl: invalid parameters\n");
RETURN_NULL();
}
if (hlen == 0) {
zend_error(E_WARNING, "remctl: host must be a valid string\n");
RETURN_NULL();
}
if (plen == 0)
principal = NULL;
hash = Z_ARRVAL_P(cmd_array);
count = zend_hash_num_elements(hash);
if (count < 1) {
zend_error(E_WARNING, "remctl: command must not be empty\n");
RETURN_NULL();
}
/*
* Convert the command array into the char ** needed by libremctl. This
* is less than ideal because we make another copy of all of the
* arguments. There should be some way to do this without making a copy.
*/
command = ecalloc(count + 1, sizeof(char *));
if (command == NULL) {
zend_error(E_WARNING, "remctl: ecalloc failed\n");
RETURN_NULL();
}
i = 0;
ZEND_HASH_FOREACH_VAL(hash, data)
{
if (Z_TYPE_P(data) != IS_STRING) {
zend_error(E_WARNING, "remctl: command contains non-string\n");
goto cleanup;
}
if (i >= count) {
zend_error(E_WARNING, "remctl: internal error: incorrect count\n");
goto cleanup;
}
command[i] = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
if (command[i] == NULL) {
zend_error(E_WARNING, "remctl: estrndup failed\n");
count = i;
goto cleanup;
}
i++;
}
ZEND_HASH_FOREACH_END();
command[count] = NULL;
/* Run the actual remctl call. */
result = remctl(host, port, principal, command);
if (result == NULL) {
zend_error(E_WARNING, "remctl: %s\n", strerror(errno));
goto cleanup;
}
/*
* Convert the remctl result to an object. return_value is defined for us
* by Zend.
*/
object_init(return_value);
if (result->error == NULL)
add_property_string(return_value, "error", "");
else
add_property_string(return_value, "error", result->error);
add_property_stringl(return_value, "stdout", result->stdout_buf,
result->stdout_len);
add_property_long(return_value, "stdout_len", result->stdout_len);
add_property_stringl(return_value, "stderr", result->stderr_buf,
result->stderr_len);
add_property_long(return_value, "stderr_len", result->stderr_len);
add_property_long(return_value, "status", result->status);
success = 1;
cleanup:
if (command != NULL) {
for (i = 0; i < count; i++)
efree((char *) command[i]);
efree(command);
}
if (result != NULL)
remctl_result_free(result);
if (!success)
RETURN_NULL();
}
/*
* Now the full interface. First, the constructor.
*/
ZEND_FUNCTION(remctl_new)
{
struct remctl *r;
r = remctl_new();
if (r == NULL) {
zend_error(E_WARNING, "remctl_new: %s", strerror(errno));
RETURN_NULL();
}
RETURN_RES(zend_register_resource(r, le_remctl_internal));
}
/*
* Set the credential cache for subsequent connections with remctl_open.
*/
ZEND_FUNCTION(remctl_set_ccache)
{
struct remctl *r;
zval *zrem;
char *ccache;
size_t clen;
int status;
status =
zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &zrem, &ccache, &clen);
if (status == FAILURE) {
zend_error(E_WARNING, "remctl_set_ccache: invalid parameters\n");
RETURN_FALSE;
}
r = zend_fetch_resource(Z_RES_P(zrem), PHP_REMCTL_RES_NAME,
le_remctl_internal);
if (!remctl_set_ccache(r, ccache))
RETURN_FALSE;
RETURN_TRUE;
}
/*
* Set the source IP for subsequent connections with remctl_open.
*/
ZEND_FUNCTION(remctl_set_source_ip)
{
struct remctl *r;
zval *zrem;
char *source;
size_t slen;
int status;
status =
zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &zrem, &source, &slen);
if (status == FAILURE) {
zend_error(E_WARNING, "remctl_set_source_ip: invalid parameters\n");
RETURN_FALSE;
}
r = zend_fetch_resource(Z_RES_P(zrem), PHP_REMCTL_RES_NAME,
le_remctl_internal);
if (!remctl_set_source_ip(r, source))
RETURN_FALSE;
RETURN_TRUE;
}
/*
* Set the timeout for subsequent operations.
*/
ZEND_FUNCTION(remctl_set_timeout)
{
struct remctl *r;
zval *zrem;
zend_long timeout;
int status;
status = zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zrem, &timeout);
if (status == FAILURE) {
zend_error(E_WARNING, "remctl_set_timeout: invalid parameters\n");
RETURN_FALSE;
}
r = zend_fetch_resource(Z_RES_P(zrem), PHP_REMCTL_RES_NAME,
le_remctl_internal);
if (!remctl_set_timeout(r, timeout))
RETURN_FALSE;
RETURN_TRUE;
}
/*
* Open a connection to the remote host. Only the host parameter is required;
* the rest are optional. PHP may require something be passed in for
* principal, but the empty string is taken to mean "use the library default."
*/
ZEND_FUNCTION(remctl_open)
{
struct remctl *r;
zval *zrem;
char *host;
char *principal = NULL;
zend_long port = 0;
size_t hlen, plen;
int status;
/* Parse and verify arguments. */
status = zend_parse_parameters(ZEND_NUM_ARGS(), "rs|ls", &zrem, &host,
&hlen, &port, &principal, &plen);
if (status == FAILURE) {
zend_error(E_WARNING, "remctl_open: invalid parameters\n");
RETURN_FALSE;
}
if (plen == 0)
principal = NULL;
r = zend_fetch_resource(Z_RES_P(zrem), PHP_REMCTL_RES_NAME,
le_remctl_internal);
/* Now we have all the arguments and can do the real work. */
if (!remctl_open(r, host, port, principal))
RETURN_FALSE;
RETURN_TRUE;
}
/*
* Send a command to the remote server.
*/
ZEND_FUNCTION(remctl_command)
{
struct remctl *r;
zval *zrem, *cmd_array;
zval *data = NULL;
HashTable *hash;
struct iovec *cmd_vec = NULL;
int i, count, status;
int success = 0;
/* Parse and verify arguments. */
status = zend_parse_parameters(ZEND_NUM_ARGS(), "ra", &zrem, &cmd_array);
if (status == FAILURE) {
zend_error(E_WARNING, "remctl_command: invalid parameters\n");
RETURN_FALSE;
}
r = zend_fetch_resource(Z_RES_P(zrem), PHP_REMCTL_RES_NAME,
le_remctl_internal);
hash = Z_ARRVAL_P(cmd_array);
count = zend_hash_num_elements(hash);
if (count < 1) {
zend_error(E_WARNING, "remctl_command: command must not be empty\n");
RETURN_NULL();
}
/*
* Transform the PHP array into an array of struct iovec. This is less
* than ideal because it makes another copy of all of the data. There
* should be some way to do this without copying.
*/
cmd_vec = ecalloc(count, sizeof(struct iovec));
if (cmd_vec == NULL) {
zend_error(E_WARNING, "remctl_command: ecalloc failed\n");
RETURN_FALSE;
}
i = 0;
ZEND_HASH_FOREACH_VAL(hash, data)
{
if (Z_TYPE_P(data) != IS_STRING) {
zend_error(E_WARNING,
"remctl_command: command contains non-string\n");
goto cleanup;
}
if (i >= count) {
zend_error(E_WARNING,
"remctl_command: internal error: incorrect count\n");
goto cleanup;
}
cmd_vec[i].iov_base = emalloc(Z_STRLEN_P(data) + 1);
if (cmd_vec[i].iov_base == NULL) {
zend_error(E_WARNING, "remctl_command: emalloc failed\n");
count = i;
goto cleanup;
}
cmd_vec[i].iov_len = Z_STRLEN_P(data);
memcpy(cmd_vec[i].iov_base, Z_STRVAL_P(data), cmd_vec[i].iov_len);
i++;
}
ZEND_HASH_FOREACH_END();
/* Finally, we can do the work. */
if (!remctl_commandv(r, cmd_vec, count))
goto cleanup;
success = 1;
cleanup:
if (cmd_vec != NULL) {
for (i = 0; i < count; i++)
efree(cmd_vec[i].iov_base);
efree(cmd_vec);
}
if (!success)
RETURN_FALSE;
RETURN_TRUE;
}
/*
* Get an output token from the server and return it as an object.
*/
ZEND_FUNCTION(remctl_output)
{
struct remctl *r;
struct remctl_output *output;
zval *zrem;
int status;
/* Parse and verify arguments. */
status = zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zrem);
if (status == FAILURE) {
zend_error(E_WARNING, "remctl_output: invalid parameters\n");
RETURN_NULL();
}
r = zend_fetch_resource(Z_RES_P(zrem), PHP_REMCTL_RES_NAME,
le_remctl_internal);
/* Get the output token. */
output = remctl_output(r);
if (output == NULL)
RETURN_NULL();
/*
* Populate an object with the output results. return_value is defined
* for us by Zend.
*/
object_init(return_value);
switch (output->type) {
case REMCTL_OUT_OUTPUT:
add_property_string(return_value, "type", "output");
add_property_stringl(return_value, "data", output->data,
output->length);
add_property_long(return_value, "stream", output->stream);
break;
case REMCTL_OUT_ERROR:
add_property_string(return_value, "type", "error");
add_property_stringl(return_value, "data", output->data,
output->length);
add_property_long(return_value, "error", output->error);
break;
case REMCTL_OUT_STATUS:
add_property_string(return_value, "type", "status");
add_property_long(return_value, "status", output->status);
break;
case REMCTL_OUT_DONE:
add_property_string(return_value, "type", "done");
break;
}
}
/*
* Sends a NOOP message to the server and reads the reply.
*/
ZEND_FUNCTION(remctl_noop)
{
struct remctl *r;
zval *zrem;
int status;
status = zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zrem);
if (status == FAILURE) {
zend_error(E_WARNING, "remctl_noop: invalid parameters\n");
RETURN_FALSE;
}
r = zend_fetch_resource(Z_RES_P(zrem), PHP_REMCTL_RES_NAME,
le_remctl_internal);
if (!remctl_noop(r))
RETURN_FALSE;
RETURN_TRUE;
}
/*
* Returns the error message from a previously failed remctl call.
*/
ZEND_FUNCTION(remctl_error)
{
struct remctl *r;
zval *zrem;
const char *error;
int status;
/* Parse and verify arguments. */
status = zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zrem);
if (status == FAILURE) {
zend_error(E_WARNING, "remctl_error: invalid parameters\n");
RETURN_NULL();
}
r = zend_fetch_resource(Z_RES_P(zrem), PHP_REMCTL_RES_NAME,
le_remctl_internal);
/* Do the work. */
error = remctl_error(r);
RETURN_STRING((char *) error);
}
/*
* Close the connection. This isn't strictly necessary since the destructor
* will close the connection for us, but it's part of the interface.
*/
ZEND_FUNCTION(remctl_close)
{
zval *zrem;
int status;
/* Parse and verify arguments. */
status = zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zrem);
if (status == FAILURE) {
zend_error(E_WARNING, "remctl_error: invalid parameters\n");
RETURN_NULL();
}
/* This delete invokes php_remctl_dtor, which calls remctl_close. */
zend_list_delete(Z_RES_P(zrem));
RETURN_TRUE;
}
|